X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/7b845b62285230d9015f80a6c2ea7ecb0b25df6e..2861cd84f9035f887715da8f51a622b8b5dfe306:/src/anet.c diff --git a/src/anet.c b/src/anet.c index ba4e6cce..ae8e9a65 100644 --- a/src/anet.c +++ b/src/anet.c @@ -1,6 +1,6 @@ /* anet.c -- Basic TCP socket stuff made a bit less boring * - * Copyright (c) 2006-2010, Salvatore Sanfilippo + * Copyright (c) 2006-2012, Salvatore Sanfilippo * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -262,7 +262,11 @@ static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len) { close(s); return ANET_ERR; } - if (listen(s, 511) == -1) { /* the magic 511 constant is from nginx */ + + /* Use a backlog of 512 entries. We pass 511 to the listen() call because + * the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1); + * which will thus give us a backlog of 512 entries */ + if (listen(s, 511) == -1) { anetSetError(err, "listen: %s", strerror(errno)); close(s); return ANET_ERR; @@ -363,3 +367,18 @@ int anetPeerToString(int fd, char *ip, int *port) { if (port) *port = ntohs(sa.sin_port); return 0; } + +int anetSockName(int fd, char *ip, int *port) { + struct sockaddr_in sa; + socklen_t salen = sizeof(sa); + + if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) { + *port = 0; + ip[0] = '?'; + ip[1] = '\0'; + return -1; + } + if (ip) strcpy(ip,inet_ntoa(sa.sin_addr)); + if (port) *port = ntohs(sa.sin_port); + return 0; +}