Coverage Report

Created: 2025-08-26 06:33

/src/h2o/deps/hiredis/net.c
Line
Count
Source (jump to first uncovered line)
1
/* Extracted from anet.c to work properly with Hiredis error reporting.
2
 *
3
 * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
4
 * Copyright (c) 2010-2014, Pieter Noordhuis <pcnoordhuis at gmail dot com>
5
 * Copyright (c) 2015, Matt Stancliff <matt at genges dot com>,
6
 *                     Jan-Erik Rediger <janerik at fnordig dot com>
7
 *
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 *
13
 *   * Redistributions of source code must retain the above copyright notice,
14
 *     this list of conditions and the following disclaimer.
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in the
17
 *     documentation and/or other materials provided with the distribution.
18
 *   * Neither the name of Redis nor the names of its contributors may be used
19
 *     to endorse or promote products derived from this software without
20
 *     specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 */
34
35
#include "fmacros.h"
36
#include <sys/types.h>
37
#include <fcntl.h>
38
#include <string.h>
39
#include <errno.h>
40
#include <stdarg.h>
41
#include <stdio.h>
42
#include <limits.h>
43
#include <stdlib.h>
44
45
#include "net.h"
46
#include "sds.h"
47
#include "sockcompat.h"
48
#include "win32.h"
49
50
/* Defined in hiredis.c */
51
void __redisSetError(redisContext *c, int type, const char *str);
52
53
int redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout);
54
55
0
void redisNetClose(redisContext *c) {
56
0
    if (c && c->fd != REDIS_INVALID_FD) {
57
0
        close(c->fd);
58
0
        c->fd = REDIS_INVALID_FD;
59
0
    }
60
0
}
61
62
0
ssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap) {
63
0
    ssize_t nread = recv(c->fd, buf, bufcap, 0);
64
0
    if (nread == -1) {
65
0
        if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {
66
            /* Try again later */
67
0
            return 0;
68
0
        } else if(errno == ETIMEDOUT && (c->flags & REDIS_BLOCK)) {
69
            /* especially in windows */
70
0
            __redisSetError(c, REDIS_ERR_TIMEOUT, "recv timeout");
71
0
            return -1;
72
0
        } else {
73
0
            __redisSetError(c, REDIS_ERR_IO, strerror(errno));
74
0
            return -1;
75
0
        }
76
0
    } else if (nread == 0) {
77
0
        __redisSetError(c, REDIS_ERR_EOF, "Server closed the connection");
78
0
        return -1;
79
0
    } else {
80
0
        return nread;
81
0
    }
82
0
}
83
84
0
ssize_t redisNetWrite(redisContext *c) {
85
0
    ssize_t nwritten;
86
87
0
    nwritten = send(c->fd, c->obuf, sdslen(c->obuf), 0);
88
0
    if (nwritten < 0) {
89
0
        if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {
90
            /* Try again */
91
0
            return 0;
92
0
        } else {
93
0
            __redisSetError(c, REDIS_ERR_IO, strerror(errno));
94
0
            return -1;
95
0
        }
96
0
    }
97
98
0
    return nwritten;
99
0
}
100
101
0
static void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) {
102
0
    int errorno = errno;  /* snprintf() may change errno */
103
0
    char buf[128] = { 0 };
104
0
    size_t len = 0;
105
106
0
    if (prefix != NULL)
107
0
        len = snprintf(buf,sizeof(buf),"%s: ",prefix);
108
0
    strerror_r(errorno, (char *)(buf + len), sizeof(buf) - len);
109
0
    __redisSetError(c,type,buf);
110
0
}
111
112
0
static int redisSetReuseAddr(redisContext *c) {
113
0
    int on = 1;
114
0
    if (setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
115
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
116
0
        redisNetClose(c);
117
0
        return REDIS_ERR;
118
0
    }
119
0
    return REDIS_OK;
120
0
}
121
122
0
static int redisCreateSocket(redisContext *c, int type) {
123
0
    redisFD s;
124
0
    if ((s = socket(type, SOCK_STREAM, 0)) == REDIS_INVALID_FD) {
125
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
126
0
        return REDIS_ERR;
127
0
    }
128
0
    c->fd = s;
129
0
    if (type == AF_INET) {
130
0
        if (redisSetReuseAddr(c) == REDIS_ERR) {
131
0
            return REDIS_ERR;
132
0
        }
133
0
    }
134
0
    return REDIS_OK;
135
0
}
136
137
0
static int redisSetBlocking(redisContext *c, int blocking) {
138
0
#ifndef _WIN32
139
0
    int flags;
140
141
    /* Set the socket nonblocking.
142
     * Note that fcntl(2) for F_GETFL and F_SETFL can't be
143
     * interrupted by a signal. */
144
0
    if ((flags = fcntl(c->fd, F_GETFL)) == -1) {
145
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_GETFL)");
146
0
        redisNetClose(c);
147
0
        return REDIS_ERR;
148
0
    }
149
150
0
    if (blocking)
151
0
        flags &= ~O_NONBLOCK;
152
0
    else
153
0
        flags |= O_NONBLOCK;
154
155
0
    if (fcntl(c->fd, F_SETFL, flags) == -1) {
156
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_SETFL)");
157
0
        redisNetClose(c);
158
0
        return REDIS_ERR;
159
0
    }
160
#else
161
    u_long mode = blocking ? 0 : 1;
162
    if (ioctl(c->fd, FIONBIO, &mode) == -1) {
163
        __redisSetErrorFromErrno(c, REDIS_ERR_IO, "ioctl(FIONBIO)");
164
        redisNetClose(c);
165
        return REDIS_ERR;
166
    }
167
#endif /* _WIN32 */
168
0
    return REDIS_OK;
169
0
}
170
171
0
int redisKeepAlive(redisContext *c, int interval) {
172
0
    int val = 1;
173
0
    redisFD fd = c->fd;
174
175
0
#ifndef _WIN32
176
0
    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){
177
0
        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
178
0
        return REDIS_ERR;
179
0
    }
180
181
0
    val = interval;
182
183
#if defined(__APPLE__) && defined(__MACH__)
184
    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) {
185
        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
186
        return REDIS_ERR;
187
    }
188
#else
189
0
#if defined(__GLIBC__) && !defined(__FreeBSD_kernel__)
190
0
    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {
191
0
        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
192
0
        return REDIS_ERR;
193
0
    }
194
195
0
    val = interval/3;
196
0
    if (val == 0) val = 1;
197
0
    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
198
0
        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
199
0
        return REDIS_ERR;
200
0
    }
201
202
0
    val = 3;
203
0
    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
204
0
        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
205
0
        return REDIS_ERR;
206
0
    }
207
0
#endif
208
0
#endif
209
#else
210
    int res;
211
212
    res = win32_redisKeepAlive(fd, interval * 1000);
213
    if (res != 0) {
214
        __redisSetError(c, REDIS_ERR_OTHER, strerror(res));
215
        return REDIS_ERR;
216
    }
217
#endif
218
0
    return REDIS_OK;
219
0
}
220
221
0
int redisSetTcpNoDelay(redisContext *c) {
222
0
    int yes = 1;
223
0
    if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
224
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_NODELAY)");
225
0
        redisNetClose(c);
226
0
        return REDIS_ERR;
227
0
    }
228
0
    return REDIS_OK;
229
0
}
230
231
0
int redisContextSetTcpUserTimeout(redisContext *c, unsigned int timeout) {
232
0
    int res;
233
0
#ifdef TCP_USER_TIMEOUT
234
0
    res = setsockopt(c->fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout, sizeof(timeout));
235
#else
236
    res = -1;
237
    errno = ENOTSUP;
238
    (void)timeout;
239
#endif
240
0
    if (res == -1) {
241
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_USER_TIMEOUT)");
242
0
        redisNetClose(c);
243
0
        return REDIS_ERR;
244
0
    }
245
0
    return REDIS_OK;
246
0
}
247
248
0
#define __MAX_MSEC (((LONG_MAX) - 999) / 1000)
249
250
static int redisContextTimeoutMsec(redisContext *c, long *result)
251
0
{
252
0
    const struct timeval *timeout = c->connect_timeout;
253
0
    long msec = -1;
254
255
    /* Only use timeout when not NULL. */
256
0
    if (timeout != NULL) {
257
0
        if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) {
258
0
            __redisSetError(c, REDIS_ERR_IO, "Invalid timeout specified");
259
0
            *result = msec;
260
0
            return REDIS_ERR;
261
0
        }
262
263
0
        msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000);
264
265
0
        if (msec < 0 || msec > INT_MAX) {
266
0
            msec = INT_MAX;
267
0
        }
268
0
    }
269
270
0
    *result = msec;
271
0
    return REDIS_OK;
272
0
}
273
274
0
static int redisContextWaitReady(redisContext *c, long msec) {
275
0
    struct pollfd   wfd[1];
276
277
0
    wfd[0].fd     = c->fd;
278
0
    wfd[0].events = POLLOUT;
279
280
0
    if (errno == EINPROGRESS) {
281
0
        int res;
282
283
0
        if ((res = poll(wfd, 1, msec)) == -1) {
284
0
            __redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)");
285
0
            redisNetClose(c);
286
0
            return REDIS_ERR;
287
0
        } else if (res == 0) {
288
0
            errno = ETIMEDOUT;
289
0
            __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
290
0
            redisNetClose(c);
291
0
            return REDIS_ERR;
292
0
        }
293
294
0
        if (redisCheckConnectDone(c, &res) != REDIS_OK || res == 0) {
295
0
            redisCheckSocketError(c);
296
0
            return REDIS_ERR;
297
0
        }
298
299
0
        return REDIS_OK;
300
0
    }
301
302
0
    __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
303
0
    redisNetClose(c);
304
0
    return REDIS_ERR;
305
0
}
306
307
0
int redisCheckConnectDone(redisContext *c, int *completed) {
308
0
    int rc = connect(c->fd, (const struct sockaddr *)c->saddr, c->addrlen);
309
0
    if (rc == 0) {
310
0
        *completed = 1;
311
0
        return REDIS_OK;
312
0
    }
313
0
    int error = errno;
314
0
    if (error == EINPROGRESS) {
315
        /* must check error to see if connect failed.  Get the socket error */
316
0
        int fail, so_error;
317
0
        socklen_t optlen = sizeof(so_error);
318
0
        fail = getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &so_error, &optlen);
319
0
        if (fail == 0) {
320
0
            if (so_error == 0) {
321
                /* Socket is connected! */
322
0
                *completed = 1;
323
0
                return REDIS_OK;
324
0
            }
325
            /* connection error; */
326
0
            errno = so_error;
327
0
            error = so_error;
328
0
        }
329
0
    }
330
0
    switch (error) {
331
0
    case EISCONN:
332
0
        *completed = 1;
333
0
        return REDIS_OK;
334
0
    case EALREADY:
335
0
    case EWOULDBLOCK:
336
0
        *completed = 0;
337
0
        return REDIS_OK;
338
0
    default:
339
0
        return REDIS_ERR;
340
0
    }
341
0
}
342
343
0
int redisCheckSocketError(redisContext *c) {
344
0
    int err = 0, errno_saved = errno;
345
0
    socklen_t errlen = sizeof(err);
346
347
0
    if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
348
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)");
349
0
        return REDIS_ERR;
350
0
    }
351
352
0
    if (err == 0) {
353
0
        err = errno_saved;
354
0
    }
355
356
0
    if (err) {
357
0
        errno = err;
358
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
359
0
        return REDIS_ERR;
360
0
    }
361
362
0
    return REDIS_OK;
363
0
}
364
365
0
int redisContextSetTimeout(redisContext *c, const struct timeval tv) {
366
0
    const void *to_ptr = &tv;
367
0
    size_t to_sz = sizeof(tv);
368
369
0
    if (redisContextUpdateCommandTimeout(c, &tv) != REDIS_OK) {
370
0
        __redisSetError(c, REDIS_ERR_OOM, "Out of memory");
371
0
        return REDIS_ERR;
372
0
    }
373
0
    if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,to_ptr,to_sz) == -1) {
374
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
375
0
        return REDIS_ERR;
376
0
    }
377
0
    if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,to_ptr,to_sz) == -1) {
378
0
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)");
379
0
        return REDIS_ERR;
380
0
    }
381
0
    return REDIS_OK;
382
0
}
383
384
0
int redisContextUpdateConnectTimeout(redisContext *c, const struct timeval *timeout) {
385
    /* Same timeval struct, short circuit */
386
0
    if (c->connect_timeout == timeout)
387
0
        return REDIS_OK;
388
389
    /* Allocate context timeval if we need to */
390
0
    if (c->connect_timeout == NULL) {
391
0
        c->connect_timeout = hi_malloc(sizeof(*c->connect_timeout));
392
0
        if (c->connect_timeout == NULL)
393
0
            return REDIS_ERR;
394
0
    }
395
396
0
    memcpy(c->connect_timeout, timeout, sizeof(*c->connect_timeout));
397
0
    return REDIS_OK;
398
0
}
399
400
0
int redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout) {
401
    /* Same timeval struct, short circuit */
402
0
    if (c->command_timeout == timeout)
403
0
        return REDIS_OK;
404
405
    /* Allocate context timeval if we need to */
406
0
    if (c->command_timeout == NULL) {
407
0
        c->command_timeout = hi_malloc(sizeof(*c->command_timeout));
408
0
        if (c->command_timeout == NULL)
409
0
            return REDIS_ERR;
410
0
    }
411
412
0
    memcpy(c->command_timeout, timeout, sizeof(*c->command_timeout));
413
0
    return REDIS_OK;
414
0
}
415
416
static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
417
                                   const struct timeval *timeout,
418
0
                                   const char *source_addr) {
419
0
    redisFD s;
420
0
    int rv, n;
421
0
    char _port[6];  /* strlen("65535"); */
422
0
    struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
423
0
    int blocking = (c->flags & REDIS_BLOCK);
424
0
    int reuseaddr = (c->flags & REDIS_REUSEADDR);
425
0
    int reuses = 0;
426
0
    long timeout_msec = -1;
427
428
0
    servinfo = NULL;
429
0
    c->connection_type = REDIS_CONN_TCP;
430
0
    c->tcp.port = port;
431
432
    /* We need to take possession of the passed parameters
433
     * to make them reusable for a reconnect.
434
     * We also carefully check we don't free data we already own,
435
     * as in the case of the reconnect method.
436
     *
437
     * This is a bit ugly, but atleast it works and doesn't leak memory.
438
     **/
439
0
    if (c->tcp.host != addr) {
440
0
        hi_free(c->tcp.host);
441
442
0
        c->tcp.host = hi_strdup(addr);
443
0
        if (c->tcp.host == NULL)
444
0
            goto oom;
445
0
    }
446
447
0
    if (timeout) {
448
0
        if (redisContextUpdateConnectTimeout(c, timeout) == REDIS_ERR)
449
0
            goto oom;
450
0
    } else {
451
0
        hi_free(c->connect_timeout);
452
0
        c->connect_timeout = NULL;
453
0
    }
454
455
0
    if (redisContextTimeoutMsec(c, &timeout_msec) != REDIS_OK) {
456
0
        goto error;
457
0
    }
458
459
0
    if (source_addr == NULL) {
460
0
        hi_free(c->tcp.source_addr);
461
0
        c->tcp.source_addr = NULL;
462
0
    } else if (c->tcp.source_addr != source_addr) {
463
0
        hi_free(c->tcp.source_addr);
464
0
        c->tcp.source_addr = hi_strdup(source_addr);
465
0
    }
466
467
0
    snprintf(_port, 6, "%d", port);
468
0
    memset(&hints,0,sizeof(hints));
469
0
    hints.ai_family = AF_INET;
470
0
    hints.ai_socktype = SOCK_STREAM;
471
472
    /* DNS lookup. To use dual stack, set both flags to prefer both IPv4 and
473
     * IPv6. By default, for historical reasons, we try IPv4 first and then we
474
     * try IPv6 only if no IPv4 address was found. */
475
0
    if (c->flags & REDIS_PREFER_IPV6 && c->flags & REDIS_PREFER_IPV4)
476
0
        hints.ai_family = AF_UNSPEC;
477
0
    else if (c->flags & REDIS_PREFER_IPV6)
478
0
        hints.ai_family = AF_INET6;
479
0
    else
480
0
        hints.ai_family = AF_INET;
481
482
0
    rv = getaddrinfo(c->tcp.host, _port, &hints, &servinfo);
483
0
    if (rv != 0 && hints.ai_family != AF_UNSPEC) {
484
        /* Try again with the other IP version. */
485
0
        hints.ai_family = (hints.ai_family == AF_INET) ? AF_INET6 : AF_INET;
486
0
        rv = getaddrinfo(c->tcp.host, _port, &hints, &servinfo);
487
0
    }
488
0
    if (rv != 0) {
489
0
        __redisSetError(c, REDIS_ERR_OTHER, gai_strerror(rv));
490
0
        return REDIS_ERR;
491
0
    }
492
0
    for (p = servinfo; p != NULL; p = p->ai_next) {
493
0
addrretry:
494
0
        if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == REDIS_INVALID_FD)
495
0
            continue;
496
497
0
        c->fd = s;
498
0
        if (redisSetTcpNoDelay(c) != REDIS_OK)
499
0
            goto error;
500
0
        if (redisSetBlocking(c,0) != REDIS_OK)
501
0
            goto error;
502
0
        if (c->tcp.source_addr) {
503
0
            int bound = 0;
504
            /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
505
0
            if ((rv = getaddrinfo(c->tcp.source_addr, NULL, &hints, &bservinfo)) != 0) {
506
0
                char buf[128];
507
0
                snprintf(buf,sizeof(buf),"Can't get addr: %s",gai_strerror(rv));
508
0
                __redisSetError(c,REDIS_ERR_OTHER,buf);
509
0
                goto error;
510
0
            }
511
512
0
            if (reuseaddr) {
513
0
                n = 1;
514
0
                if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*) &n,
515
0
                               sizeof(n)) < 0) {
516
0
                    freeaddrinfo(bservinfo);
517
0
                    goto error;
518
0
                }
519
0
            }
520
521
0
            for (b = bservinfo; b != NULL; b = b->ai_next) {
522
0
                if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
523
0
                    bound = 1;
524
0
                    break;
525
0
                }
526
0
            }
527
0
            freeaddrinfo(bservinfo);
528
0
            if (!bound) {
529
0
                char buf[128];
530
0
                snprintf(buf,sizeof(buf),"Can't bind socket: %s",strerror(errno));
531
0
                __redisSetError(c,REDIS_ERR_OTHER,buf);
532
0
                goto error;
533
0
            }
534
0
        }
535
536
        /* For repeat connection */
537
0
        hi_free(c->saddr);
538
0
        c->saddr = hi_malloc(p->ai_addrlen);
539
0
        if (c->saddr == NULL)
540
0
            goto oom;
541
542
0
        memcpy(c->saddr, p->ai_addr, p->ai_addrlen);
543
0
        c->addrlen = p->ai_addrlen;
544
545
0
        if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
546
0
            if (errno == EHOSTUNREACH) {
547
0
                redisNetClose(c);
548
0
                continue;
549
0
            } else if (errno == EINPROGRESS) {
550
0
                if (blocking) {
551
0
                    goto wait_for_ready;
552
0
                }
553
                /* This is ok.
554
                 * Note that even when it's in blocking mode, we unset blocking
555
                 * for `connect()`
556
                 */
557
0
            } else if (errno == EADDRNOTAVAIL && reuseaddr) {
558
0
                if (++reuses >= REDIS_CONNECT_RETRIES) {
559
0
                    goto error;
560
0
                } else {
561
0
                    redisNetClose(c);
562
0
                    goto addrretry;
563
0
                }
564
0
            } else {
565
0
                wait_for_ready:
566
0
                if (redisContextWaitReady(c,timeout_msec) != REDIS_OK)
567
0
                    goto error;
568
0
            }
569
0
        }
570
0
        if (blocking && redisSetBlocking(c,1) != REDIS_OK)
571
0
            goto error;
572
573
0
        c->flags |= REDIS_CONNECTED;
574
0
        rv = REDIS_OK;
575
0
        goto end;
576
0
    }
577
0
    if (p == NULL) {
578
0
        char buf[128];
579
0
        snprintf(buf,sizeof(buf),"Can't create socket: %s",strerror(errno));
580
0
        __redisSetError(c,REDIS_ERR_OTHER,buf);
581
0
        goto error;
582
0
    }
583
584
0
oom:
585
0
    __redisSetError(c, REDIS_ERR_OOM, "Out of memory");
586
0
error:
587
0
    rv = REDIS_ERR;
588
0
end:
589
0
    if(servinfo) {
590
0
        freeaddrinfo(servinfo);
591
0
    }
592
593
0
    return rv;  // Need to return REDIS_OK if alright
594
0
}
595
596
int redisContextConnectTcp(redisContext *c, const char *addr, int port,
597
0
                           const struct timeval *timeout) {
598
0
    return _redisContextConnectTcp(c, addr, port, timeout, NULL);
599
0
}
600
601
int redisContextConnectBindTcp(redisContext *c, const char *addr, int port,
602
                               const struct timeval *timeout,
603
0
                               const char *source_addr) {
604
0
    return _redisContextConnectTcp(c, addr, port, timeout, source_addr);
605
0
}
606
607
0
int redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout) {
608
0
#ifndef _WIN32
609
0
    int blocking = (c->flags & REDIS_BLOCK);
610
0
    struct sockaddr_un *sa;
611
0
    long timeout_msec = -1;
612
613
0
    if (redisCreateSocket(c,AF_UNIX) < 0)
614
0
        return REDIS_ERR;
615
0
    if (redisSetBlocking(c,0) != REDIS_OK)
616
0
        return REDIS_ERR;
617
618
0
    c->connection_type = REDIS_CONN_UNIX;
619
0
    if (c->unix_sock.path != path) {
620
0
        hi_free(c->unix_sock.path);
621
622
0
        c->unix_sock.path = hi_strdup(path);
623
0
        if (c->unix_sock.path == NULL)
624
0
            goto oom;
625
0
    }
626
627
0
    if (timeout) {
628
0
        if (redisContextUpdateConnectTimeout(c, timeout) == REDIS_ERR)
629
0
            goto oom;
630
0
    } else {
631
0
        hi_free(c->connect_timeout);
632
0
        c->connect_timeout = NULL;
633
0
    }
634
635
0
    if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK)
636
0
        return REDIS_ERR;
637
638
    /* Don't leak sockaddr if we're reconnecting */
639
0
    if (c->saddr) hi_free(c->saddr);
640
641
0
    sa = (struct sockaddr_un*)(c->saddr = hi_malloc(sizeof(struct sockaddr_un)));
642
0
    if (sa == NULL)
643
0
        goto oom;
644
645
0
    c->addrlen = sizeof(struct sockaddr_un);
646
0
    sa->sun_family = AF_UNIX;
647
0
    strncpy(sa->sun_path, path, sizeof(sa->sun_path) - 1);
648
0
    if (connect(c->fd, (struct sockaddr*)sa, sizeof(*sa)) == -1) {
649
0
        if (errno == EINPROGRESS && !blocking) {
650
            /* This is ok. */
651
0
        } else {
652
0
            if (redisContextWaitReady(c,timeout_msec) != REDIS_OK)
653
0
                return REDIS_ERR;
654
0
        }
655
0
    }
656
657
    /* Reset socket to be blocking after connect(2). */
658
0
    if (blocking && redisSetBlocking(c,1) != REDIS_OK)
659
0
        return REDIS_ERR;
660
661
0
    c->flags |= REDIS_CONNECTED;
662
0
    return REDIS_OK;
663
#else
664
    /* We currently do not support Unix sockets for Windows. */
665
    /* TODO(m): https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ */
666
    errno = EPROTONOSUPPORT;
667
    return REDIS_ERR;
668
#endif /* _WIN32 */
669
0
oom:
670
0
    __redisSetError(c, REDIS_ERR_OOM, "Out of memory");
671
0
    return REDIS_ERR;
672
0
}