Always obtain IP address during accept(), and not do it later as a call to getsockname().

Signed-off-by: Jan Vidar Krey <janvidar@extatic.org>
This commit is contained in:
Jan Vidar Krey 2009-03-23 15:05:27 +01:00
parent 95b741bb5e
commit 5d5dda2f9b
3 changed files with 29 additions and 5 deletions

View File

@ -260,12 +260,13 @@ void net_on_accept(int server_fd, short ev, void *arg)
{
struct hub_info* hub = (struct hub_info*) arg;
struct user* user = 0;
struct ip_addr_encap ipaddr;
const char* addr;
struct timeval timeout = { TIMEOUT_CONNECTED, 0 };
for (;;)
{
int fd = net_accept(server_fd);
int fd = net_accept(server_fd, &ipaddr);
if (fd == -1)
{
if (net_error() == EWOULDBLOCK)
@ -279,7 +280,7 @@ void net_on_accept(int server_fd, short ev, void *arg)
}
}
addr = net_get_peer_address(fd);
addr = ip_convert_to_string(&ipaddr);
/* FIXME: Should have a plugin log this */
hub_log(log_trace, "Got connection from %s", addr);
@ -301,7 +302,7 @@ void net_on_accept(int server_fd, short ev, void *arg)
}
/* Store IP address in user object */
ip_convert_to_binary(addr, &user->ipaddr);
memcpy(&user->ipaddr, &ipaddr, sizeof(ipaddr));
net_set_nonblocking(fd, 1);
net_set_nosigpipe(fd, 1);

View File

@ -236,13 +236,19 @@ int net_close(int fd)
}
int net_accept(int fd)
int net_accept(int fd, struct ip_addr_encap* ipaddr)
{
struct sockaddr_storage addr;
struct sockaddr_in* addr4;
struct sockaddr_in6* addr6;
socklen_t addr_size;
int ret = 0;
addr_size = sizeof(struct sockaddr_storage);
memset(&addr, 0, addr_size);
addr4 = (struct sockaddr_in*) &addr;
addr6 = (struct sockaddr_in6*) &addr;
ret = accept(fd, (struct sockaddr*) &addr, &addr_size);
if (ret == -1)
@ -270,6 +276,20 @@ int net_accept(int fd)
else
{
net_stats_add_accept();
if (ipaddr)
{
memset(ipaddr, 0, sizeof(struct ip_addr_encap));
ipaddr->af = addr4->sin_family;
if (ipaddr->af == AF_INET6)
{
memcpy(&ipaddr->internal_ip_data.in6, &addr6->sin6_addr, sizeof(struct in6_addr));
}
else
{
memcpy(&ipaddr->internal_ip_data.in, &addr4->sin_addr, sizeof(struct in_addr));
}
}
}
return ret;

View File

@ -83,8 +83,11 @@ extern int net_close(int fd);
/**
* A wrapper for the accept() function call.
* @param fd socket descriptor
* @param ipaddr (in/out) if non-NULL the ip address of the
* accepted peer is filled in.
*/
extern int net_accept(int fd);
extern int net_accept(int fd, struct ip_addr_encap* ipaddr);
/**
* A wrapper for the connect() call.