Created
December 8, 2010 22:42
-
-
Save samueltardieu/734054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int netaccept(int fd, char *server, int *port) | |
{ | |
int cfd = 0; | |
int one = 0; | |
union { | |
struct sockaddr_in ipv4; | |
struct sockaddr_in6 ipv6; | |
} sa; | |
socklen_t len = sizeof sa; | |
int rc = 0, af; | |
rc = fdwait(fd, 'r'); | |
check(rc != -1, "Failed waiting on non-block accept."); | |
taskstate("netaccept"); | |
cfd = accept(fd, (void*)&sa, &len); | |
check(cfd != -1, "Failed calling accept on socket that was ready."); | |
af = sa.ipv4.sin_family; | |
if(server) { | |
check(inet_ntop(af, af == AF_INET ? &sa.ipv4.sin_addr : &sa.ipv6.sin6_addr, | |
server, IPADDR_SIZE) != NULL, | |
"Major failure, cannot ntop ipaddresses."); | |
} | |
if(port) { | |
*port = ntohs(af == AF_INET ? sa.ipv4.sin_port : sa.ipv6.sin6_port); | |
} | |
fdnoblock(cfd); | |
if(SET_NODELAY) { | |
one = 1; | |
setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one); | |
} | |
taskstate("netaccept succeeded"); | |
return cfd; | |
error: | |
taskstate("accept failed"); | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment