Bug Summary

File:src/anet.c
Warning:line 624, column 22
The left operand of '==' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name anet.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D REDIS_STATIC= -I ../deps/hiredis -I ../deps/linenoise -I ../deps/lua/src -I ../deps/hdr_histogram -D USE_JEMALLOC -I ../deps/jemalloc/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-c11-extensions -Wno-missing-field-initializers -std=c11 -fdebug-compilation-dir /home/netto/Desktop/redis-6.2.1/src -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-03-14-133648-8817-1 -x c anet.c
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(*__errno_location ()).h>
46#include <stdarg.h>
47#include <stdio.h>
48
49#include "anet.h"
50
51static void anetSetError(char *err, const char *fmt, ...)
52{
53 va_list ap;
54
55 if (!err) return;
56 va_start(ap, fmt)__builtin_va_start(ap, fmt);
57 vsnprintf(err, ANET_ERR_LEN256, fmt, ap);
58 va_end(ap)__builtin_va_end(ap);
59}
60
61int 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_GETFL3)) == -1) {
68 anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno(*__errno_location ())));
69 return ANET_ERR-1;
70 }
71
72 /* Check if this flag has been set or unset, if so,
73 * then there is no need to call fcntl to set/unset it again. */
74 if (!!(flags & O_NONBLOCK04000) == !!non_block)
75 return ANET_OK0;
76
77 if (non_block)
78 flags |= O_NONBLOCK04000;
79 else
80 flags &= ~O_NONBLOCK04000;
81
82 if (fcntl(fd, F_SETFL4, flags) == -1) {
83 anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno(*__errno_location ())));
84 return ANET_ERR-1;
85 }
86 return ANET_OK0;
87}
88
89int anetNonBlock(char *err, int fd) {
90 return anetSetBlock(err,fd,1);
91}
92
93int anetBlock(char *err, int fd) {
94 return anetSetBlock(err,fd,0);
95}
96
97/* Enable the FD_CLOEXEC on the given fd to avoid fd leaks.
98 * This function should be invoked for fd's on specific places
99 * where fork + execve system calls are called. */
100int anetCloexec(int fd) {
101 int r;
102 int flags;
103
104 do {
105 r = fcntl(fd, F_GETFD1);
106 } while (r == -1 && errno(*__errno_location ()) == EINTR4);
107
108 if (r == -1 || (r & FD_CLOEXEC1))
109 return r;
110
111 flags = r | FD_CLOEXEC1;
112
113 do {
114 r = fcntl(fd, F_SETFD2, flags);
115 } while (r == -1 && errno(*__errno_location ()) == EINTR4);
116
117 return r;
118}
119
120/* Set TCP keep alive option to detect dead peers. The interval option
121 * is only used for Linux as we are using Linux-specific APIs to set
122 * the probe send time, interval, and count. */
123int anetKeepAlive(char *err, int fd, int interval)
124{
125 int val = 1;
126
127 if (setsockopt(fd, SOL_SOCKET1, SO_KEEPALIVE9, &val, sizeof(val)) == -1)
128 {
129 anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno(*__errno_location ())));
130 return ANET_ERR-1;
131 }
132
133#ifdef __linux__1
134 /* Default settings are more or less garbage, with the keepalive time
135 * set to 7200 by default on Linux. Modify settings to make the feature
136 * actually useful. */
137
138 /* Send first probe after interval. */
139 val = interval;
140 if (setsockopt(fd, IPPROTO_TCPIPPROTO_TCP, TCP_KEEPIDLE4, &val, sizeof(val)) < 0) {
141 anetSetError(err, "setsockopt TCP_KEEPIDLE: %s\n", strerror(errno(*__errno_location ())));
142 return ANET_ERR-1;
143 }
144
145 /* Send next probes after the specified interval. Note that we set the
146 * delay as interval / 3, as we send three probes before detecting
147 * an error (see the next setsockopt call). */
148 val = interval/3;
149 if (val == 0) val = 1;
150 if (setsockopt(fd, IPPROTO_TCPIPPROTO_TCP, TCP_KEEPINTVL5, &val, sizeof(val)) < 0) {
151 anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno(*__errno_location ())));
152 return ANET_ERR-1;
153 }
154
155 /* Consider the socket in error state after three we send three ACK
156 * probes without getting a reply. */
157 val = 3;
158 if (setsockopt(fd, IPPROTO_TCPIPPROTO_TCP, TCP_KEEPCNT6, &val, sizeof(val)) < 0) {
159 anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno(*__errno_location ())));
160 return ANET_ERR-1;
161 }
162#else
163 ((void) interval); /* Avoid unused var warning for non Linux systems. */
164#endif
165
166 return ANET_OK0;
167}
168
169static int anetSetTcpNoDelay(char *err, int fd, int val)
170{
171 if (setsockopt(fd, IPPROTO_TCPIPPROTO_TCP, TCP_NODELAY1, &val, sizeof(val)) == -1)
172 {
173 anetSetError(err, "setsockopt TCP_NODELAY: %s", strerror(errno(*__errno_location ())));
174 return ANET_ERR-1;
175 }
176 return ANET_OK0;
177}
178
179int anetEnableTcpNoDelay(char *err, int fd)
180{
181 return anetSetTcpNoDelay(err, fd, 1);
182}
183
184int anetDisableTcpNoDelay(char *err, int fd)
185{
186 return anetSetTcpNoDelay(err, fd, 0);
187}
188
189
190int anetSetSendBuffer(char *err, int fd, int buffsize)
191{
192 if (setsockopt(fd, SOL_SOCKET1, SO_SNDBUF7, &buffsize, sizeof(buffsize)) == -1)
193 {
194 anetSetError(err, "setsockopt SO_SNDBUF: %s", strerror(errno(*__errno_location ())));
195 return ANET_ERR-1;
196 }
197 return ANET_OK0;
198}
199
200int anetTcpKeepAlive(char *err, int fd)
201{
202 int yes = 1;
203 if (setsockopt(fd, SOL_SOCKET1, SO_KEEPALIVE9, &yes, sizeof(yes)) == -1) {
204 anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno(*__errno_location ())));
205 return ANET_ERR-1;
206 }
207 return ANET_OK0;
208}
209
210/* Set the socket send timeout (SO_SNDTIMEO socket option) to the specified
211 * number of milliseconds, or disable it if the 'ms' argument is zero. */
212int anetSendTimeout(char *err, int fd, long long ms) {
213 struct timeval tv;
214
215 tv.tv_sec = ms/1000;
216 tv.tv_usec = (ms%1000)*1000;
217 if (setsockopt(fd, SOL_SOCKET1, SO_SNDTIMEO21, &tv, sizeof(tv)) == -1) {
218 anetSetError(err, "setsockopt SO_SNDTIMEO: %s", strerror(errno(*__errno_location ())));
219 return ANET_ERR-1;
220 }
221 return ANET_OK0;
222}
223
224/* Set the socket receive timeout (SO_RCVTIMEO socket option) to the specified
225 * number of milliseconds, or disable it if the 'ms' argument is zero. */
226int anetRecvTimeout(char *err, int fd, long long ms) {
227 struct timeval tv;
228
229 tv.tv_sec = ms/1000;
230 tv.tv_usec = (ms%1000)*1000;
231 if (setsockopt(fd, SOL_SOCKET1, SO_RCVTIMEO20, &tv, sizeof(tv)) == -1) {
232 anetSetError(err, "setsockopt SO_RCVTIMEO: %s", strerror(errno(*__errno_location ())));
233 return ANET_ERR-1;
234 }
235 return ANET_OK0;
236}
237
238/* Resolve the hostname "host" and set the string representation of the
239 * IP address into the buffer pointed by "ipbuf".
240 *
241 * If flags is set to ANET_IP_ONLY the function only resolves hostnames
242 * that are actually already IPv4 or IPv6 addresses. This turns the function
243 * into a validating / normalizing function. */
244int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len,
245 int flags)
246{
247 struct addrinfo hints, *info;
248 int rv;
249
250 memset(&hints,0,sizeof(hints));
251 if (flags & ANET_IP_ONLY(1<<0)) hints.ai_flags = AI_NUMERICHOST0x0004;
252 hints.ai_family = AF_UNSPEC0;
253 hints.ai_socktype = SOCK_STREAMSOCK_STREAM; /* specify socktype to avoid dups */
254
255 if ((rv = getaddrinfo(host, NULL((void*)0), &hints, &info)) != 0) {
256 anetSetError(err, "%s", gai_strerror(rv));
257 return ANET_ERR-1;
258 }
259 if (info->ai_family == AF_INET2) {
260 struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr;
261 inet_ntop(AF_INET2, &(sa->sin_addr), ipbuf, ipbuf_len);
262 } else {
263 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)info->ai_addr;
264 inet_ntop(AF_INET610, &(sa->sin6_addr), ipbuf, ipbuf_len);
265 }
266
267 freeaddrinfo(info);
268 return ANET_OK0;
269}
270
271static int anetSetReuseAddr(char *err, int fd) {
272 int yes = 1;
273 /* Make sure connection-intensive things like the redis benchmark
274 * will be able to close/open sockets a zillion of times */
275 if (setsockopt(fd, SOL_SOCKET1, SO_REUSEADDR2, &yes, sizeof(yes)) == -1) {
276 anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno(*__errno_location ())));
277 return ANET_ERR-1;
278 }
279 return ANET_OK0;
280}
281
282static int anetCreateSocket(char *err, int domain) {
283 int s;
284 if ((s = socket(domain, SOCK_STREAMSOCK_STREAM, 0)) == -1) {
285 anetSetError(err, "creating socket: %s", strerror(errno(*__errno_location ())));
286 return ANET_ERR-1;
287 }
288
289 /* Make sure connection-intensive things like the redis benchmark
290 * will be able to close/open sockets a zillion of times */
291 if (anetSetReuseAddr(err,s) == ANET_ERR-1) {
292 close(s);
293 return ANET_ERR-1;
294 }
295 return s;
296}
297
298#define ANET_CONNECT_NONE0 0
299#define ANET_CONNECT_NONBLOCK1 1
300#define ANET_CONNECT_BE_BINDING2 2 /* Best effort binding. */
301static int anetTcpGenericConnect(char *err, const char *addr, int port,
302 const char *source_addr, int flags)
303{
304 int s = ANET_ERR-1, rv;
305 char portstr[6]; /* strlen("65535") + 1; */
306 struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
307
308 snprintf(portstr,sizeof(portstr),"%d",port);
309 memset(&hints,0,sizeof(hints));
310 hints.ai_family = AF_UNSPEC0;
311 hints.ai_socktype = SOCK_STREAMSOCK_STREAM;
312
313 if ((rv = getaddrinfo(addr,portstr,&hints,&servinfo)) != 0) {
314 anetSetError(err, "%s", gai_strerror(rv));
315 return ANET_ERR-1;
316 }
317 for (p = servinfo; p != NULL((void*)0); p = p->ai_next) {
318 /* Try to create the socket and to connect it.
319 * If we fail in the socket() call, or on connect(), we retry with
320 * the next entry in servinfo. */
321 if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
322 continue;
323 if (anetSetReuseAddr(err,s) == ANET_ERR-1) goto error;
324 if (flags & ANET_CONNECT_NONBLOCK1 && anetNonBlock(err,s) != ANET_OK0)
325 goto error;
326 if (source_addr) {
327 int bound = 0;
328 /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
329 if ((rv = getaddrinfo(source_addr, NULL((void*)0), &hints, &bservinfo)) != 0)
330 {
331 anetSetError(err, "%s", gai_strerror(rv));
332 goto error;
333 }
334 for (b = bservinfo; b != NULL((void*)0); b = b->ai_next) {
335 if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
336 bound = 1;
337 break;
338 }
339 }
340 freeaddrinfo(bservinfo);
341 if (!bound) {
342 anetSetError(err, "bind: %s", strerror(errno(*__errno_location ())));
343 goto error;
344 }
345 }
346 if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
347 /* If the socket is non-blocking, it is ok for connect() to
348 * return an EINPROGRESS error here. */
349 if (errno(*__errno_location ()) == EINPROGRESS115 && flags & ANET_CONNECT_NONBLOCK1)
350 goto end;
351 close(s);
352 s = ANET_ERR-1;
353 continue;
354 }
355
356 /* If we ended an iteration of the for loop without errors, we
357 * have a connected socket. Let's return to the caller. */
358 goto end;
359 }
360 if (p == NULL((void*)0))
361 anetSetError(err, "creating socket: %s", strerror(errno(*__errno_location ())));
362
363error:
364 if (s != ANET_ERR-1) {
365 close(s);
366 s = ANET_ERR-1;
367 }
368
369end:
370 freeaddrinfo(servinfo);
371
372 /* Handle best effort binding: if a binding address was used, but it is
373 * not possible to create a socket, try again without a binding address. */
374 if (s == ANET_ERR-1 && source_addr && (flags & ANET_CONNECT_BE_BINDING2)) {
375 return anetTcpGenericConnect(err,addr,port,NULL((void*)0),flags);
376 } else {
377 return s;
378 }
379}
380
381int anetTcpConnect(char *err, const char *addr, int port)
382{
383 return anetTcpGenericConnect(err,addr,port,NULL((void*)0),ANET_CONNECT_NONE0);
384}
385
386int anetTcpNonBlockConnect(char *err, const char *addr, int port)
387{
388 return anetTcpGenericConnect(err,addr,port,NULL((void*)0),ANET_CONNECT_NONBLOCK1);
389}
390
391int anetTcpNonBlockBindConnect(char *err, const char *addr, int port,
392 const char *source_addr)
393{
394 return anetTcpGenericConnect(err,addr,port,source_addr,
395 ANET_CONNECT_NONBLOCK1);
396}
397
398int anetTcpNonBlockBestEffortBindConnect(char *err, const char *addr, int port,
399 const char *source_addr)
400{
401 return anetTcpGenericConnect(err,addr,port,source_addr,
402 ANET_CONNECT_NONBLOCK1|ANET_CONNECT_BE_BINDING2);
403}
404
405int anetUnixGenericConnect(char *err, const char *path, int flags)
406{
407 int s;
408 struct sockaddr_un sa;
409
410 if ((s = anetCreateSocket(err,AF_LOCAL1)) == ANET_ERR-1)
411 return ANET_ERR-1;
412
413 sa.sun_family = AF_LOCAL1;
414 strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
415 if (flags & ANET_CONNECT_NONBLOCK1) {
416 if (anetNonBlock(err,s) != ANET_OK0) {
417 close(s);
418 return ANET_ERR-1;
419 }
420 }
421 if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) == -1) {
422 if (errno(*__errno_location ()) == EINPROGRESS115 &&
423 flags & ANET_CONNECT_NONBLOCK1)
424 return s;
425
426 anetSetError(err, "connect: %s", strerror(errno(*__errno_location ())));
427 close(s);
428 return ANET_ERR-1;
429 }
430 return s;
431}
432
433int anetUnixConnect(char *err, const char *path)
434{
435 return anetUnixGenericConnect(err,path,ANET_CONNECT_NONE0);
436}
437
438int anetUnixNonBlockConnect(char *err, const char *path)
439{
440 return anetUnixGenericConnect(err,path,ANET_CONNECT_NONBLOCK1);
441}
442
443/* Like read(2) but make sure 'count' is read before to return
444 * (unless error or EOF condition is encountered) */
445int anetRead(int fd, char *buf, int count)
446{
447 ssize_t nread, totlen = 0;
448 while(totlen != count) {
449 nread = read(fd,buf,count-totlen);
450 if (nread == 0) return totlen;
451 if (nread == -1) return -1;
452 totlen += nread;
453 buf += nread;
454 }
455 return totlen;
456}
457
458/* Like write(2) but make sure 'count' is written before to return
459 * (unless error is encountered) */
460int anetWrite(int fd, char *buf, int count)
461{
462 ssize_t nwritten, totlen = 0;
463 while(totlen != count) {
464 nwritten = write(fd,buf,count-totlen);
465 if (nwritten == 0) return totlen;
466 if (nwritten == -1) return -1;
467 totlen += nwritten;
468 buf += nwritten;
469 }
470 return totlen;
471}
472
473static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len, int backlog) {
474 if (bind(s,sa,len) == -1) {
475 anetSetError(err, "bind: %s", strerror(errno(*__errno_location ())));
476 close(s);
477 return ANET_ERR-1;
478 }
479
480 if (listen(s, backlog) == -1) {
481 anetSetError(err, "listen: %s", strerror(errno(*__errno_location ())));
482 close(s);
483 return ANET_ERR-1;
484 }
485 return ANET_OK0;
486}
487
488static int anetV6Only(char *err, int s) {
489 int yes = 1;
490 if (setsockopt(s,IPPROTO_IPV6IPPROTO_IPV6,IPV6_V6ONLY26,&yes,sizeof(yes)) == -1) {
491 anetSetError(err, "setsockopt: %s", strerror(errno(*__errno_location ())));
492 return ANET_ERR-1;
493 }
494 return ANET_OK0;
495}
496
497static int _anetTcpServer(char *err, int port, char *bindaddr, int af, int backlog)
498{
499 int s = -1, rv;
500 char _port[6]; /* strlen("65535") */
501 struct addrinfo hints, *servinfo, *p;
502
503 snprintf(_port,6,"%d",port);
504 memset(&hints,0,sizeof(hints));
505 hints.ai_family = af;
506 hints.ai_socktype = SOCK_STREAMSOCK_STREAM;
507 hints.ai_flags = AI_PASSIVE0x0001; /* No effect if bindaddr != NULL */
508 if (bindaddr && !strcmp("*", bindaddr))
509 bindaddr = NULL((void*)0);
510 if (af == AF_INET610 && bindaddr && !strcmp("::*", bindaddr))
511 bindaddr = NULL((void*)0);
512
513 if ((rv = getaddrinfo(bindaddr,_port,&hints,&servinfo)) != 0) {
514 anetSetError(err, "%s", gai_strerror(rv));
515 return ANET_ERR-1;
516 }
517 for (p = servinfo; p != NULL((void*)0); p = p->ai_next) {
518 if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
519 continue;
520
521 if (af == AF_INET610 && anetV6Only(err,s) == ANET_ERR-1) goto error;
522 if (anetSetReuseAddr(err,s) == ANET_ERR-1) goto error;
523 if (anetListen(err,s,p->ai_addr,p->ai_addrlen,backlog) == ANET_ERR-1) s = ANET_ERR-1;
524 goto end;
525 }
526 if (p == NULL((void*)0)) {
527 anetSetError(err, "unable to bind socket, errno: %d", errno(*__errno_location ()));
528 goto error;
529 }
530
531error:
532 if (s != -1) close(s);
533 s = ANET_ERR-1;
534end:
535 freeaddrinfo(servinfo);
536 return s;
537}
538
539int anetTcpServer(char *err, int port, char *bindaddr, int backlog)
540{
541 return _anetTcpServer(err, port, bindaddr, AF_INET2, backlog);
542}
543
544int anetTcp6Server(char *err, int port, char *bindaddr, int backlog)
545{
546 return _anetTcpServer(err, port, bindaddr, AF_INET610, backlog);
547}
548
549int anetUnixServer(char *err, char *path, mode_t perm, int backlog)
550{
551 int s;
552 struct sockaddr_un sa;
553
554 if ((s = anetCreateSocket(err,AF_LOCAL1)) == ANET_ERR-1)
555 return ANET_ERR-1;
556
557 memset(&sa,0,sizeof(sa));
558 sa.sun_family = AF_LOCAL1;
559 strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
560 if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa),backlog) == ANET_ERR-1)
561 return ANET_ERR-1;
562 if (perm)
563 chmod(sa.sun_path, perm);
564 return s;
565}
566
567static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) {
568 int fd;
569 while(1) {
570 fd = accept(s,sa,len);
571 if (fd == -1) {
572 if (errno(*__errno_location ()) == EINTR4)
573 continue;
574 else {
575 anetSetError(err, "accept: %s", strerror(errno(*__errno_location ())));
576 return ANET_ERR-1;
577 }
578 }
579 break;
580 }
581 return fd;
582}
583
584int anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {
585 int fd;
586 struct sockaddr_storage sa;
587 socklen_t salen = sizeof(sa);
588 if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == -1)
589 return ANET_ERR-1;
590
591 if (sa.ss_family == AF_INET2) {
592 struct sockaddr_in *s = (struct sockaddr_in *)&sa;
593 if (ip) inet_ntop(AF_INET2,(void*)&(s->sin_addr),ip,ip_len);
594 if (port) *port = ntohs(s->sin_port)__bswap_16 (s->sin_port);
595 } else {
596 struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
597 if (ip) inet_ntop(AF_INET610,(void*)&(s->sin6_addr),ip,ip_len);
598 if (port) *port = ntohs(s->sin6_port)__bswap_16 (s->sin6_port);
599 }
600 return fd;
601}
602
603int anetUnixAccept(char *err, int s) {
604 int fd;
605 struct sockaddr_un sa;
606 socklen_t salen = sizeof(sa);
607 if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == -1)
608 return ANET_ERR-1;
609
610 return fd;
611}
612
613int anetFdToString(int fd, char *ip, size_t ip_len, int *port, int fd_to_str_type) {
614 struct sockaddr_storage sa;
615 socklen_t salen = sizeof(sa);
616
617 if (fd_to_str_type == FD_TO_PEER_NAME0) {
2
Assuming 'fd_to_str_type' is not equal to FD_TO_PEER_NAME
3
Taking false branch
618 if (getpeername(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;
619 } else {
620 if (getsockname(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;
4
Assuming the condition is false
5
Taking false branch
621 }
622 if (ip_len
5.1
'ip_len' is not equal to 0
== 0) goto error;
6
Taking false branch
623
624 if (sa.ss_family == AF_INET2) {
7
The left operand of '==' is a garbage value
625 struct sockaddr_in *s = (struct sockaddr_in *)&sa;
626 if (ip) inet_ntop(AF_INET2,(void*)&(s->sin_addr),ip,ip_len);
627 if (port) *port = ntohs(s->sin_port)__bswap_16 (s->sin_port);
628 } else if (sa.ss_family == AF_INET610) {
629 struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
630 if (ip) inet_ntop(AF_INET610,(void*)&(s->sin6_addr),ip,ip_len);
631 if (port) *port = ntohs(s->sin6_port)__bswap_16 (s->sin6_port);
632 } else if (sa.ss_family == AF_UNIX1) {
633 if (ip) snprintf(ip, ip_len, "/unixsocket");
634 if (port) *port = 0;
635 } else {
636 goto error;
637 }
638 return 0;
639
640error:
641 if (ip) {
642 if (ip_len >= 2) {
643 ip[0] = '?';
644 ip[1] = '\0';
645 } else if (ip_len == 1) {
646 ip[0] = '\0';
647 }
648 }
649 if (port) *port = 0;
650 return -1;
651}
652
653/* Format an IP,port pair into something easy to parse. If IP is IPv6
654 * (matches for ":"), the ip is surrounded by []. IP and port are just
655 * separated by colons. This the standard to display addresses within Redis. */
656int anetFormatAddr(char *buf, size_t buf_len, char *ip, int port) {
657 return snprintf(buf,buf_len, strchr(ip,':') ?
658 "[%s]:%d" : "%s:%d", ip, port);
659}
660
661/* Like anetFormatAddr() but extract ip and port from the socket's peer/sockname. */
662int anetFormatFdAddr(int fd, char *buf, size_t buf_len, int fd_to_str_type) {
663 char ip[INET6_ADDRSTRLEN46];
664 int port;
665
666 anetFdToString(fd,ip,sizeof(ip),&port,fd_to_str_type);
1
Calling 'anetFdToString'
667 return anetFormatAddr(buf, buf_len, ip, port);
668}