xref: /redis-3.2.3/src/anet.c (revision 65090401)
1 /* anet.c -- Basic TCP socket stuff made a bit less boring
2  *
3  * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   * Neither the name of Redis nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "fmacros.h"
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/un.h>
37 #include <sys/time.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <arpa/inet.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <string.h>
44 #include <netdb.h>
45 #include <errno.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 
49 #include "anet.h"
50 
51 static void anetSetError(char *err, const char *fmt, ...)
52 {
53     va_list ap;
54 
55     if (!err) return;
56     va_start(ap, fmt);
57     vsnprintf(err, ANET_ERR_LEN, fmt, ap);
58     va_end(ap);
59 }
60 
61 int anetSetBlock(char *err, int fd, int non_block) {
62     int flags;
63 
64     /* Set the socket blocking (if non_block is zero) or non-blocking.
65      * Note that fcntl(2) for F_GETFL and F_SETFL can't be
66      * interrupted by a signal. */
67     if ((flags = fcntl(fd, F_GETFL)) == -1) {
68         anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno));
69         return ANET_ERR;
70     }
71 
72     if (non_block)
73         flags |= O_NONBLOCK;
74     else
75         flags &= ~O_NONBLOCK;
76 
77     if (fcntl(fd, F_SETFL, flags) == -1) {
78         anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno));
79         return ANET_ERR;
80     }
81     return ANET_OK;
82 }
83 
84 int anetNonBlock(char *err, int fd) {
85     return anetSetBlock(err,fd,1);
86 }
87 
88 int anetBlock(char *err, int fd) {
89     return anetSetBlock(err,fd,0);
90 }
91 
92 /* Set TCP keep alive option to detect dead peers. The interval option
93  * is only used for Linux as we are using Linux-specific APIs to set
94  * the probe send time, interval, and count. */
95 int anetKeepAlive(char *err, int fd, int interval)
96 {
97     int val = 1;
98 
99     if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1)
100     {
101         anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
102         return ANET_ERR;
103     }
104 
105 #ifdef __linux__
106     /* Default settings are more or less garbage, with the keepalive time
107      * set to 7200 by default on Linux. Modify settings to make the feature
108      * actually useful. */
109 
110     /* Send first probe after interval. */
111     val = interval;
112     if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {
113         anetSetError(err, "setsockopt TCP_KEEPIDLE: %s\n", strerror(errno));
114         return ANET_ERR;
115     }
116 
117     /* Send next probes after the specified interval. Note that we set the
118      * delay as interval / 3, as we send three probes before detecting
119      * an error (see the next setsockopt call). */
120     val = interval/3;
121     if (val == 0) val = 1;
122     if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
123         anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
124         return ANET_ERR;
125     }
126 
127     /* Consider the socket in error state after three we send three ACK
128      * probes without getting a reply. */
129     val = 3;
130     if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
131         anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
132         return ANET_ERR;
133     }
134 #else
135     ((void) interval); /* Avoid unused var warning for non Linux systems. */
136 #endif
137 
138     return ANET_OK;
139 }
140 
141 static int anetSetTcpNoDelay(char *err, int fd, int val)
142 {
143     if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1)
144     {
145         anetSetError(err, "setsockopt TCP_NODELAY: %s", strerror(errno));
146         return ANET_ERR;
147     }
148     return ANET_OK;
149 }
150 
151 int anetEnableTcpNoDelay(char *err, int fd)
152 {
153     return anetSetTcpNoDelay(err, fd, 1);
154 }
155 
156 int anetDisableTcpNoDelay(char *err, int fd)
157 {
158     return anetSetTcpNoDelay(err, fd, 0);
159 }
160 
161 
162 int anetSetSendBuffer(char *err, int fd, int buffsize)
163 {
164     if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof(buffsize)) == -1)
165     {
166         anetSetError(err, "setsockopt SO_SNDBUF: %s", strerror(errno));
167         return ANET_ERR;
168     }
169     return ANET_OK;
170 }
171 
172 int anetTcpKeepAlive(char *err, int fd)
173 {
174     int yes = 1;
175     if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1) {
176         anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
177         return ANET_ERR;
178     }
179     return ANET_OK;
180 }
181 
182 /* Set the socket send timeout (SO_SNDTIMEO socket option) to the specified
183  * number of milliseconds, or disable it if the 'ms' argument is zero. */
184 int anetSendTimeout(char *err, int fd, long long ms) {
185     struct timeval tv;
186 
187     tv.tv_sec = ms/1000;
188     tv.tv_usec = (ms%1000)*1000;
189     if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
190         anetSetError(err, "setsockopt SO_SNDTIMEO: %s", strerror(errno));
191         return ANET_ERR;
192     }
193     return ANET_OK;
194 }
195 
196 /* anetGenericResolve() is called by anetResolve() and anetResolveIP() to
197  * do the actual work. It resolves the hostname "host" and set the string
198  * representation of the IP address into the buffer pointed by "ipbuf".
199  *
200  * If flags is set to ANET_IP_ONLY the function only resolves hostnames
201  * that are actually already IPv4 or IPv6 addresses. This turns the function
202  * into a validating / normalizing function. */
203 int anetGenericResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len,
204                        int flags)
205 {
206     struct addrinfo hints, *info;
207     int rv;
208 
209     memset(&hints,0,sizeof(hints));
210     if (flags & ANET_IP_ONLY) hints.ai_flags = AI_NUMERICHOST;
211     hints.ai_family = AF_UNSPEC;
212     hints.ai_socktype = SOCK_STREAM;  /* specify socktype to avoid dups */
213 
214     if ((rv = getaddrinfo(host, NULL, &hints, &info)) != 0) {
215         anetSetError(err, "%s", gai_strerror(rv));
216         return ANET_ERR;
217     }
218     if (info->ai_family == AF_INET) {
219         struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr;
220         inet_ntop(AF_INET, &(sa->sin_addr), ipbuf, ipbuf_len);
221     } else {
222         struct sockaddr_in6 *sa = (struct sockaddr_in6 *)info->ai_addr;
223         inet_ntop(AF_INET6, &(sa->sin6_addr), ipbuf, ipbuf_len);
224     }
225 
226     freeaddrinfo(info);
227     return ANET_OK;
228 }
229 
230 int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len) {
231     return anetGenericResolve(err,host,ipbuf,ipbuf_len,ANET_NONE);
232 }
233 
234 int anetResolveIP(char *err, char *host, char *ipbuf, size_t ipbuf_len) {
235     return anetGenericResolve(err,host,ipbuf,ipbuf_len,ANET_IP_ONLY);
236 }
237 
238 static int anetSetReuseAddr(char *err, int fd) {
239     int yes = 1;
240     /* Make sure connection-intensive things like the redis benckmark
241      * will be able to close/open sockets a zillion of times */
242     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
243         anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno));
244         return ANET_ERR;
245     }
246     return ANET_OK;
247 }
248 
249 static int anetCreateSocket(char *err, int domain) {
250     int s;
251     if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {
252         anetSetError(err, "creating socket: %s", strerror(errno));
253         return ANET_ERR;
254     }
255 
256     /* Make sure connection-intensive things like the redis benchmark
257      * will be able to close/open sockets a zillion of times */
258     if (anetSetReuseAddr(err,s) == ANET_ERR) {
259         close(s);
260         return ANET_ERR;
261     }
262     return s;
263 }
264 
265 #define ANET_CONNECT_NONE 0
266 #define ANET_CONNECT_NONBLOCK 1
267 static int anetTcpGenericConnect(char *err, char *addr, int port,
268                                  char *source_addr, int flags)
269 {
270     int s = ANET_ERR, rv;
271     char portstr[6];  /* strlen("65535") + 1; */
272     struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
273 
274     snprintf(portstr,sizeof(portstr),"%d",port);
275     memset(&hints,0,sizeof(hints));
276     hints.ai_family = AF_UNSPEC;
277     hints.ai_socktype = SOCK_STREAM;
278 
279     if ((rv = getaddrinfo(addr,portstr,&hints,&servinfo)) != 0) {
280         anetSetError(err, "%s", gai_strerror(rv));
281         return ANET_ERR;
282     }
283     for (p = servinfo; p != NULL; p = p->ai_next) {
284         /* Try to create the socket and to connect it.
285          * If we fail in the socket() call, or on connect(), we retry with
286          * the next entry in servinfo. */
287         if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
288             continue;
289         if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;
290         if (flags & ANET_CONNECT_NONBLOCK && anetNonBlock(err,s) != ANET_OK)
291             goto error;
292         if (source_addr) {
293             int bound = 0;
294             /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
295             if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0)
296             {
297                 anetSetError(err, "%s", gai_strerror(rv));
298                 goto end;
299             }
300             for (b = bservinfo; b != NULL; b = b->ai_next) {
301                 if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
302                     bound = 1;
303                     break;
304                 }
305             }
306             freeaddrinfo(bservinfo);
307             if (!bound) {
308                 anetSetError(err, "bind: %s", strerror(errno));
309                 goto end;
310             }
311         }
312         if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
313             /* If the socket is non-blocking, it is ok for connect() to
314              * return an EINPROGRESS error here. */
315             if (errno == EINPROGRESS && flags & ANET_CONNECT_NONBLOCK)
316                 goto end;
317             close(s);
318             s = ANET_ERR;
319             continue;
320         }
321 
322         /* If we ended an iteration of the for loop without errors, we
323          * have a connected socket. Let's return to the caller. */
324         goto end;
325     }
326     if (p == NULL)
327         anetSetError(err, "creating socket: %s", strerror(errno));
328 
329 error:
330     if (s != ANET_ERR) {
331         close(s);
332         s = ANET_ERR;
333     }
334 end:
335     freeaddrinfo(servinfo);
336     return s;
337 }
338 
339 int anetTcpConnect(char *err, char *addr, int port)
340 {
341     return anetTcpGenericConnect(err,addr,port,NULL,ANET_CONNECT_NONE);
342 }
343 
344 int anetTcpNonBlockConnect(char *err, char *addr, int port)
345 {
346     return anetTcpGenericConnect(err,addr,port,NULL,ANET_CONNECT_NONBLOCK);
347 }
348 
349 int anetTcpNonBlockBindConnect(char *err, char *addr, int port, char *source_addr)
350 {
351     return anetTcpGenericConnect(err,addr,port,source_addr,ANET_CONNECT_NONBLOCK);
352 }
353 
354 int anetUnixGenericConnect(char *err, char *path, int flags)
355 {
356     int s;
357     struct sockaddr_un sa;
358 
359     if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
360         return ANET_ERR;
361 
362     sa.sun_family = AF_LOCAL;
363     strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
364     if (flags & ANET_CONNECT_NONBLOCK) {
365         if (anetNonBlock(err,s) != ANET_OK)
366             return ANET_ERR;
367     }
368     if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) == -1) {
369         if (errno == EINPROGRESS &&
370             flags & ANET_CONNECT_NONBLOCK)
371             return s;
372 
373         anetSetError(err, "connect: %s", strerror(errno));
374         close(s);
375         return ANET_ERR;
376     }
377     return s;
378 }
379 
380 int anetUnixConnect(char *err, char *path)
381 {
382     return anetUnixGenericConnect(err,path,ANET_CONNECT_NONE);
383 }
384 
385 int anetUnixNonBlockConnect(char *err, char *path)
386 {
387     return anetUnixGenericConnect(err,path,ANET_CONNECT_NONBLOCK);
388 }
389 
390 /* Like read(2) but make sure 'count' is read before to return
391  * (unless error or EOF condition is encountered) */
392 int anetRead(int fd, char *buf, int count)
393 {
394     ssize_t nread, totlen = 0;
395     while(totlen != count) {
396         nread = read(fd,buf,count-totlen);
397         if (nread == 0) return totlen;
398         if (nread == -1) return -1;
399         totlen += nread;
400         buf += nread;
401     }
402     return totlen;
403 }
404 
405 /* Like write(2) but make sure 'count' is read before to return
406  * (unless error is encountered) */
407 int anetWrite(int fd, char *buf, int count)
408 {
409     ssize_t nwritten, totlen = 0;
410     while(totlen != count) {
411         nwritten = write(fd,buf,count-totlen);
412         if (nwritten == 0) return totlen;
413         if (nwritten == -1) return -1;
414         totlen += nwritten;
415         buf += nwritten;
416     }
417     return totlen;
418 }
419 
420 static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len, int backlog) {
421     if (bind(s,sa,len) == -1) {
422         anetSetError(err, "bind: %s", strerror(errno));
423         close(s);
424         return ANET_ERR;
425     }
426 
427     if (listen(s, backlog) == -1) {
428         anetSetError(err, "listen: %s", strerror(errno));
429         close(s);
430         return ANET_ERR;
431     }
432     return ANET_OK;
433 }
434 
435 static int anetV6Only(char *err, int s) {
436     int yes = 1;
437     if (setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&yes,sizeof(yes)) == -1) {
438         anetSetError(err, "setsockopt: %s", strerror(errno));
439         close(s);
440         return ANET_ERR;
441     }
442     return ANET_OK;
443 }
444 
445 static int _anetTcpServer(char *err, int port, char *bindaddr, int af, int backlog)
446 {
447     int s, rv;
448     char _port[6];  /* strlen("65535") */
449     struct addrinfo hints, *servinfo, *p;
450 
451     snprintf(_port,6,"%d",port);
452     memset(&hints,0,sizeof(hints));
453     hints.ai_family = af;
454     hints.ai_socktype = SOCK_STREAM;
455     hints.ai_flags = AI_PASSIVE;    /* No effect if bindaddr != NULL */
456 
457     if ((rv = getaddrinfo(bindaddr,_port,&hints,&servinfo)) != 0) {
458         anetSetError(err, "%s", gai_strerror(rv));
459         return ANET_ERR;
460     }
461     for (p = servinfo; p != NULL; p = p->ai_next) {
462         if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
463             continue;
464 
465         if (af == AF_INET6 && anetV6Only(err,s) == ANET_ERR) goto error;
466         if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;
467         if (anetListen(err,s,p->ai_addr,p->ai_addrlen,backlog) == ANET_ERR) goto error;
468         goto end;
469     }
470     if (p == NULL) {
471         anetSetError(err, "unable to bind socket");
472         goto error;
473     }
474 
475 error:
476     s = ANET_ERR;
477 end:
478     freeaddrinfo(servinfo);
479     return s;
480 }
481 
482 int anetTcpServer(char *err, int port, char *bindaddr, int backlog)
483 {
484     return _anetTcpServer(err, port, bindaddr, AF_INET, backlog);
485 }
486 
487 int anetTcp6Server(char *err, int port, char *bindaddr, int backlog)
488 {
489     return _anetTcpServer(err, port, bindaddr, AF_INET6, backlog);
490 }
491 
492 int anetUnixServer(char *err, char *path, mode_t perm, int backlog)
493 {
494     int s;
495     struct sockaddr_un sa;
496 
497     if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
498         return ANET_ERR;
499 
500     memset(&sa,0,sizeof(sa));
501     sa.sun_family = AF_LOCAL;
502     strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
503     if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa),backlog) == ANET_ERR)
504         return ANET_ERR;
505     if (perm)
506         chmod(sa.sun_path, perm);
507     return s;
508 }
509 
510 static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) {
511     int fd;
512     while(1) {
513         fd = accept(s,sa,len);
514         if (fd == -1) {
515             if (errno == EINTR)
516                 continue;
517             else {
518                 anetSetError(err, "accept: %s", strerror(errno));
519                 return ANET_ERR;
520             }
521         }
522         break;
523     }
524     return fd;
525 }
526 
527 int anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {
528     int fd;
529     struct sockaddr_storage sa;
530     socklen_t salen = sizeof(sa);
531     if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == -1)
532         return ANET_ERR;
533 
534     if (sa.ss_family == AF_INET) {
535         struct sockaddr_in *s = (struct sockaddr_in *)&sa;
536         if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);
537         if (port) *port = ntohs(s->sin_port);
538     } else {
539         struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
540         if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);
541         if (port) *port = ntohs(s->sin6_port);
542     }
543     return fd;
544 }
545 
546 int anetUnixAccept(char *err, int s) {
547     int fd;
548     struct sockaddr_un sa;
549     socklen_t salen = sizeof(sa);
550     if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == -1)
551         return ANET_ERR;
552 
553     return fd;
554 }
555 
556 int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
557     struct sockaddr_storage sa;
558     socklen_t salen = sizeof(sa);
559 
560     if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) goto error;
561     if (ip_len == 0) goto error;
562 
563     if (sa.ss_family == AF_INET) {
564         struct sockaddr_in *s = (struct sockaddr_in *)&sa;
565         if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);
566         if (port) *port = ntohs(s->sin_port);
567     } else if (sa.ss_family == AF_INET6) {
568         struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
569         if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);
570         if (port) *port = ntohs(s->sin6_port);
571     } else if (sa.ss_family == AF_UNIX) {
572         if (ip) strncpy(ip,"/unixsocket",ip_len);
573         if (port) *port = 0;
574     } else {
575         goto error;
576     }
577     return 0;
578 
579 error:
580     if (ip) {
581         if (ip_len >= 2) {
582             ip[0] = '?';
583             ip[1] = '\0';
584         } else if (ip_len == 1) {
585             ip[0] = '\0';
586         }
587     }
588     if (port) *port = 0;
589     return -1;
590 }
591 
592 /* Format an IP,port pair into something easy to parse. If IP is IPv6
593  * (matches for ":"), the ip is surrounded by []. IP and port are just
594  * separated by colons. This the standard to display addresses within Redis. */
595 int anetFormatAddr(char *buf, size_t buf_len, char *ip, int port) {
596     return snprintf(buf,buf_len, strchr(ip,':') ?
597            "[%s]:%d" : "%s:%d", ip, port);
598 }
599 
600 /* Like anetFormatAddr() but extract ip and port from the socket's peer. */
601 int anetFormatPeer(int fd, char *buf, size_t buf_len) {
602     char ip[INET6_ADDRSTRLEN];
603     int port;
604 
605     anetPeerToString(fd,ip,sizeof(ip),&port);
606     return anetFormatAddr(buf, buf_len, ip, port);
607 }
608 
609 int anetSockName(int fd, char *ip, size_t ip_len, int *port) {
610     struct sockaddr_storage sa;
611     socklen_t salen = sizeof(sa);
612 
613     if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) {
614         if (port) *port = 0;
615         ip[0] = '?';
616         ip[1] = '\0';
617         return -1;
618     }
619     if (sa.ss_family == AF_INET) {
620         struct sockaddr_in *s = (struct sockaddr_in *)&sa;
621         if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);
622         if (port) *port = ntohs(s->sin_port);
623     } else {
624         struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
625         if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);
626         if (port) *port = ntohs(s->sin6_port);
627     }
628     return 0;
629 }
630 
631 int anetFormatSock(int fd, char *fmt, size_t fmt_len) {
632     char ip[INET6_ADDRSTRLEN];
633     int port;
634 
635     anetSockName(fd,ip,sizeof(ip),&port);
636     return anetFormatAddr(fmt, fmt_len, ip, port);
637 }
638