From 5ea74680b646ff0a8b844e013006fb7c693cdd36 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Sat, 12 Jul 2014 16:10:46 +0300 Subject: [PATCH] Resolve the address name of new clients in the connector --- src/connector.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/connector.c b/src/connector.c index 78c5c4bf..66c879e5 100644 --- a/src/connector.c +++ b/src/connector.c @@ -47,7 +47,8 @@ struct client_instance { struct client_instance *next; struct sockaddr address; - socklen_t address_len; + char address_name[INET6_ADDRSTRLEN]; + char buf[PAGESIZE]; int bufofs; }; @@ -87,21 +88,43 @@ void *acceptor(void *arg) { conn_instance_t *ci = (conn_instance_t *)arg; client_instance_t *client, *old_client; + socklen_t address_len; int fd; rename_proc("acceptor"); retry: client = ckzalloc(sizeof(client_instance_t)); - client->address_len = sizeof(client->address); + address_len = sizeof(client->address); while (!ci->accept) sleep(1); - fd = accept(ci->serverfd, &client->address, &client->address_len); + fd = accept(ci->serverfd, &client->address, &address_len); if (unlikely(fd < 0)) { LOGERR("Failed to accept on socket %d in acceptor", ci->serverfd); dealloc(client); goto out; } + + switch (client->address.sa_family) { + const struct sockaddr_in *inet4_in; + const struct sockaddr_in6 *inet6_in; + + case AF_INET: + inet4_in = (struct sockaddr_in *)&client->address; + inet_ntop(AF_INET, &inet4_in->sin_addr, client->address_name, INET6_ADDRSTRLEN); + break; + case AF_INET6: + inet6_in = (struct sockaddr_in6 *)&client->address; + inet_ntop(AF_INET6, &inet6_in->sin6_addr, client->address_name, INET6_ADDRSTRLEN); + break; + default: + LOGWARNING("Unknown INET type for client %d on socket %d", + ci->nfds, fd); + close(fd); + free(client); + goto retry; + } + keep_sockalive(fd); LOGINFO("Connected new client %d on socket %d", ci->nfds, fd);