From: Marco d'Itri <md@linux.it>
Date: Sat, 14 May 2022 02:57:49 +0800
Subject: expand_remote_port

---
 eval.c         | 22 ++++++++++++++++++++++
 hosts_access.5 |  2 ++
 percent_x.c    |  2 ++
 tcpd.h         |  5 +++++
 4 files changed, 31 insertions(+)

diff --git a/eval.c b/eval.c
index d68358f..4c0bd8d 100644
--- a/eval.c
+++ b/eval.c
@@ -98,6 +98,28 @@ struct host_info *host;
     }
 }
 
+/* eval_port - return string with the port */
+char   *eval_port(saddr)
+#ifdef INET6
+struct sockaddr *saddr;
+#else
+struct sockaddr_in *saddr;
+#endif
+{
+    static char port[16];
+    if (saddr != 0) {
+        sprintf(port, "%u",
+#ifdef INET6
+            ntohs(((struct sockaddr_in *)saddr)->sin_port));
+#else
+            ntohs(saddr->sin_port));
+#endif
+    } else {
+	strcpy(port, "0");
+    }
+    return (port);
+}
+
 /* eval_client - return string with as much about the client as we know */
 
 char   *eval_client(request)
diff --git a/hosts_access.5 b/hosts_access.5
index bb05289..bd8903a 100644
--- a/hosts_access.5
+++ b/hosts_access.5
@@ -175,6 +175,8 @@ The client (server) host name or address, if the host name is
 unavailable.
 .IP "%n (%N)"
 The client (server) host name (or "unknown" or "paranoid").
+.IP "%r (%R)"
+The clients (servers) port number (or "0").
 .IP %p
 The daemon process id.
 .IP %s
diff --git a/percent_x.c b/percent_x.c
index c95a1ea..1b5b83d 100644
--- a/percent_x.c
+++ b/percent_x.c
@@ -63,6 +63,8 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 		ch == 'n' ? eval_hostname(request->client) :
 		ch == 'N' ? eval_hostname(request->server) :
 		ch == 'p' ? eval_pid(request) :
+		ch == 'r' ? eval_port(request->client->sin) :
+		ch == 'R' ? eval_port(request->server->sin) :
 		ch == 's' ? eval_server(request) :
 		ch == 'u' ? eval_user(request) :
 		ch == '%' ? "%" : (tcpd_warn("unrecognized %%%c", ch), "");
diff --git a/tcpd.h b/tcpd.h
index b6690d9..1277cea 100644
--- a/tcpd.h
+++ b/tcpd.h
@@ -155,6 +155,11 @@ extern char *eval_hostaddr(struct host_info *);	/* printable host address */
 extern char *eval_hostinfo(struct host_info *);	/* host name or address */
 extern char *eval_client(struct request_info *);/* whatever is available */
 extern char *eval_server(struct request_info *);/* whatever is available */
+#ifdef INET6
+extern char *eval_port(struct sockaddr *);
+#else
+extern char *eval_port(struct sockaddr_in *);
+#endif
 #define eval_daemon(r)	((r)->daemon)	/* daemon process name */
 #define eval_pid(r)	((r)->pid)	/* process id */
 
