add port information when accepting clients

This commit is contained in:
F. Duncanh
2025-09-14 04:38:39 -04:00
parent 830c7131fe
commit e5d8a02952
3 changed files with 13 additions and 6 deletions

View File

@@ -268,6 +268,7 @@ httpd_accept_connection(httpd_t *httpd, int server_fd, int is_ipv6)
struct sockaddr_storage local_saddr;
socklen_t local_saddrlen;
unsigned char *local, *remote;
unsigned short port;
unsigned int local_zone_id, remote_zone_id;
int local_len, remote_len;
int ret, fd;
@@ -287,10 +288,10 @@ httpd_accept_connection(httpd_t *httpd, int server_fd, int is_ipv6)
return 0;
}
logger_log(httpd->logger, LOGGER_INFO, "Accepted %s client on socket %d",
(is_ipv6 ? "IPv6" : "IPv4"), fd);
local = netutils_get_address(&local_saddr, &local_len, &local_zone_id);
remote = netutils_get_address(&remote_saddr, &remote_len, &remote_zone_id);
local = netutils_get_address(&local_saddr, &local_len, &local_zone_id, &port);
logger_log(httpd->logger, LOGGER_INFO, "Accepted %s client on socket %d, port %u",
(is_ipv6 ? "IPv6" : "IPv4"), fd, port);
remote = netutils_get_address(&remote_saddr, &remote_len, &remote_zone_id, NULL);
assert (local_zone_id == remote_zone_id);
ret = httpd_add_connection(httpd, fd, local, local_len, remote, remote_len, local_zone_id);

View File

@@ -53,7 +53,7 @@ netutils_cleanup()
}
unsigned char *
netutils_get_address(void *sockaddr, int *length, unsigned int *zone_id)
netutils_get_address(void *sockaddr, int *length, unsigned int *zone_id, unsigned short *port)
{
unsigned char ipv4_prefix[] = { 0,0,0,0,0,0,0,0,0,0,255,255 };
struct sockaddr *address = sockaddr;
@@ -66,11 +66,17 @@ netutils_get_address(void *sockaddr, int *length, unsigned int *zone_id)
*zone_id = 0;
sin = (struct sockaddr_in *)address;
*length = sizeof(sin->sin_addr.s_addr);
if (port) {
*port = ntohs(sin->sin_port);
}
return (unsigned char *)&sin->sin_addr.s_addr;
} else if (address->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)address;
if (port) {
*port = ntohs(sin6->sin6_port);
}
if (!memcmp(sin6->sin6_addr.s6_addr, ipv4_prefix, 12)) {
/* Actually an embedded IPv4 address */
*zone_id = 0;

View File

@@ -19,7 +19,7 @@ int netutils_init();
void netutils_cleanup();
int netutils_init_socket(unsigned short *port, int use_ipv6, int use_udp);
unsigned char *netutils_get_address(void *sockaddr, int *length, unsigned int *zone_id);
unsigned char *netutils_get_address(void *sockaddr, int *length, unsigned int *zone_id, unsigned short *port);
int netutils_parse_address(int family, const char *src, void *dst, int dstlen);
#endif