net.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. #include "fmacros.h"
  35. #include <sys/types.h>
  36. #include <fcntl.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #include <stdarg.h>
  40. #include <stdio.h>
  41. #include <limits.h>
  42. #include <stdlib.h>
  43. #include "net.h"
  44. #include "sds.h"
  45. #include "sockcompat.h"
  46. #include "win32.h"
  47. /* Defined in hiredis.c */
  48. void __redisSetError(redisContext *c, int type, const char *str);
  49. void redisNetClose(redisContext *c) {
  50. if (c && c->fd != REDIS_INVALID_FD) {
  51. close(c->fd);
  52. c->fd = REDIS_INVALID_FD;
  53. }
  54. }
  55. ssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap) {
  56. ssize_t nread = recv(c->fd, buf, bufcap, 0);
  57. if (nread == -1) {
  58. if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {
  59. /* Try again later */
  60. return 0;
  61. } else if(errno == ETIMEDOUT && (c->flags & REDIS_BLOCK)) {
  62. /* especially in windows */
  63. __redisSetError(c, REDIS_ERR_TIMEOUT, "recv timeout");
  64. return -1;
  65. } else {
  66. __redisSetError(c, REDIS_ERR_IO, NULL);
  67. return -1;
  68. }
  69. } else if (nread == 0) {
  70. __redisSetError(c, REDIS_ERR_EOF, "Server closed the connection");
  71. return -1;
  72. } else {
  73. return nread;
  74. }
  75. }
  76. ssize_t redisNetWrite(redisContext *c) {
  77. ssize_t nwritten = send(c->fd, c->obuf, hi_sdslen(c->obuf), 0);
  78. if (nwritten < 0) {
  79. if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {
  80. /* Try again later */
  81. } else {
  82. __redisSetError(c, REDIS_ERR_IO, NULL);
  83. return -1;
  84. }
  85. }
  86. return nwritten;
  87. }
  88. static void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) {
  89. int errorno = errno; /* snprintf() may change errno */
  90. char buf[128] = { 0 };
  91. size_t len = 0;
  92. if (prefix != NULL)
  93. len = snprintf(buf,sizeof(buf),"%s: ",prefix);
  94. strerror_r(errorno, (char *)(buf + len), sizeof(buf) - len);
  95. __redisSetError(c,type,buf);
  96. }
  97. static int redisSetReuseAddr(redisContext *c) {
  98. int on = 1;
  99. if (setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
  100. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  101. redisNetClose(c);
  102. return REDIS_ERR;
  103. }
  104. return REDIS_OK;
  105. }
  106. static int redisCreateSocket(redisContext *c, int type) {
  107. redisFD s;
  108. if ((s = socket(type, SOCK_STREAM, 0)) == REDIS_INVALID_FD) {
  109. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  110. return REDIS_ERR;
  111. }
  112. c->fd = s;
  113. if (type == AF_INET) {
  114. if (redisSetReuseAddr(c) == REDIS_ERR) {
  115. return REDIS_ERR;
  116. }
  117. }
  118. return REDIS_OK;
  119. }
  120. static int redisSetBlocking(redisContext *c, int blocking) {
  121. #ifndef _WIN32
  122. int flags;
  123. /* Set the socket nonblocking.
  124. * Note that fcntl(2) for F_GETFL and F_SETFL can't be
  125. * interrupted by a signal. */
  126. if ((flags = fcntl(c->fd, F_GETFL)) == -1) {
  127. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_GETFL)");
  128. redisNetClose(c);
  129. return REDIS_ERR;
  130. }
  131. if (blocking)
  132. flags &= ~O_NONBLOCK;
  133. else
  134. flags |= O_NONBLOCK;
  135. if (fcntl(c->fd, F_SETFL, flags) == -1) {
  136. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_SETFL)");
  137. redisNetClose(c);
  138. return REDIS_ERR;
  139. }
  140. #else
  141. u_long mode = blocking ? 0 : 1;
  142. if (ioctl(c->fd, FIONBIO, &mode) == -1) {
  143. __redisSetErrorFromErrno(c, REDIS_ERR_IO, "ioctl(FIONBIO)");
  144. redisNetClose(c);
  145. return REDIS_ERR;
  146. }
  147. #endif /* _WIN32 */
  148. return REDIS_OK;
  149. }
  150. int redisKeepAlive(redisContext *c, int interval) {
  151. int val = 1;
  152. redisFD fd = c->fd;
  153. if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){
  154. __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
  155. return REDIS_ERR;
  156. }
  157. val = interval;
  158. #if defined(__APPLE__) && defined(__MACH__)
  159. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) {
  160. __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
  161. return REDIS_ERR;
  162. }
  163. #else
  164. #if defined(__GLIBC__) && !defined(__FreeBSD_kernel__)
  165. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {
  166. __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
  167. return REDIS_ERR;
  168. }
  169. val = interval/3;
  170. if (val == 0) val = 1;
  171. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
  172. __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
  173. return REDIS_ERR;
  174. }
  175. val = 3;
  176. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
  177. __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
  178. return REDIS_ERR;
  179. }
  180. #endif
  181. #endif
  182. return REDIS_OK;
  183. }
  184. int redisSetTcpNoDelay(redisContext *c) {
  185. int yes = 1;
  186. if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
  187. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_NODELAY)");
  188. redisNetClose(c);
  189. return REDIS_ERR;
  190. }
  191. return REDIS_OK;
  192. }
  193. #define __MAX_MSEC (((LONG_MAX) - 999) / 1000)
  194. static int redisContextTimeoutMsec(redisContext *c, long *result)
  195. {
  196. const struct timeval *timeout = c->connect_timeout;
  197. long msec = -1;
  198. /* Only use timeout when not NULL. */
  199. if (timeout != NULL) {
  200. if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) {
  201. *result = msec;
  202. return REDIS_ERR;
  203. }
  204. msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000);
  205. if (msec < 0 || msec > INT_MAX) {
  206. msec = INT_MAX;
  207. }
  208. }
  209. *result = msec;
  210. return REDIS_OK;
  211. }
  212. static int redisContextWaitReady(redisContext *c, long msec) {
  213. struct pollfd wfd[1];
  214. wfd[0].fd = c->fd;
  215. wfd[0].events = POLLOUT;
  216. if (errno == EINPROGRESS) {
  217. int res;
  218. if ((res = poll(wfd, 1, msec)) == -1) {
  219. __redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)");
  220. redisNetClose(c);
  221. return REDIS_ERR;
  222. } else if (res == 0) {
  223. errno = ETIMEDOUT;
  224. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  225. redisNetClose(c);
  226. return REDIS_ERR;
  227. }
  228. if (redisCheckConnectDone(c, &res) != REDIS_OK || res == 0) {
  229. redisCheckSocketError(c);
  230. return REDIS_ERR;
  231. }
  232. return REDIS_OK;
  233. }
  234. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  235. redisNetClose(c);
  236. return REDIS_ERR;
  237. }
  238. int redisCheckConnectDone(redisContext *c, int *completed) {
  239. int rc = connect(c->fd, (const struct sockaddr *)c->saddr, c->addrlen);
  240. if (rc == 0) {
  241. *completed = 1;
  242. return REDIS_OK;
  243. }
  244. switch (errno) {
  245. case EISCONN:
  246. *completed = 1;
  247. return REDIS_OK;
  248. case EALREADY:
  249. case EINPROGRESS:
  250. case EWOULDBLOCK:
  251. *completed = 0;
  252. return REDIS_OK;
  253. default:
  254. return REDIS_ERR;
  255. }
  256. }
  257. int redisCheckSocketError(redisContext *c) {
  258. int err = 0, errno_saved = errno;
  259. socklen_t errlen = sizeof(err);
  260. if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
  261. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)");
  262. return REDIS_ERR;
  263. }
  264. if (err == 0) {
  265. err = errno_saved;
  266. }
  267. if (err) {
  268. errno = err;
  269. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  270. return REDIS_ERR;
  271. }
  272. return REDIS_OK;
  273. }
  274. int redisContextSetTimeout(redisContext *c, const struct timeval tv) {
  275. const void *to_ptr = &tv;
  276. size_t to_sz = sizeof(tv);
  277. if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,to_ptr,to_sz) == -1) {
  278. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
  279. return REDIS_ERR;
  280. }
  281. if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,to_ptr,to_sz) == -1) {
  282. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)");
  283. return REDIS_ERR;
  284. }
  285. return REDIS_OK;
  286. }
  287. int redisContextUpdateConnectTimeout(redisContext *c, const struct timeval *timeout) {
  288. /* Same timeval struct, short circuit */
  289. if (c->connect_timeout == timeout)
  290. return REDIS_OK;
  291. /* Allocate context timeval if we need to */
  292. if (c->connect_timeout == NULL) {
  293. c->connect_timeout = hi_malloc(sizeof(*c->connect_timeout));
  294. if (c->connect_timeout == NULL)
  295. return REDIS_ERR;
  296. }
  297. memcpy(c->connect_timeout, timeout, sizeof(*c->connect_timeout));
  298. return REDIS_OK;
  299. }
  300. int redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout) {
  301. /* Same timeval struct, short circuit */
  302. if (c->command_timeout == timeout)
  303. return REDIS_OK;
  304. /* Allocate context timeval if we need to */
  305. if (c->command_timeout == NULL) {
  306. c->command_timeout = hi_malloc(sizeof(*c->command_timeout));
  307. if (c->command_timeout == NULL)
  308. return REDIS_ERR;
  309. }
  310. memcpy(c->command_timeout, timeout, sizeof(*c->command_timeout));
  311. return REDIS_OK;
  312. }
  313. static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
  314. const struct timeval *timeout,
  315. const char *source_addr) {
  316. redisFD s;
  317. int rv, n;
  318. char _port[6]; /* strlen("65535"); */
  319. struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
  320. int blocking = (c->flags & REDIS_BLOCK);
  321. int reuseaddr = (c->flags & REDIS_REUSEADDR);
  322. int reuses = 0;
  323. long timeout_msec = -1;
  324. servinfo = NULL;
  325. c->connection_type = REDIS_CONN_TCP;
  326. c->tcp.port = port;
  327. /* We need to take possession of the passed parameters
  328. * to make them reusable for a reconnect.
  329. * We also carefully check we don't free data we already own,
  330. * as in the case of the reconnect method.
  331. *
  332. * This is a bit ugly, but atleast it works and doesn't leak memory.
  333. **/
  334. if (c->tcp.host != addr) {
  335. hi_free(c->tcp.host);
  336. c->tcp.host = hi_strdup(addr);
  337. if (c->tcp.host == NULL)
  338. goto oom;
  339. }
  340. if (timeout) {
  341. if (redisContextUpdateConnectTimeout(c, timeout) == REDIS_ERR)
  342. goto oom;
  343. } else {
  344. hi_free(c->connect_timeout);
  345. c->connect_timeout = NULL;
  346. }
  347. if (redisContextTimeoutMsec(c, &timeout_msec) != REDIS_OK) {
  348. __redisSetError(c, REDIS_ERR_IO, "Invalid timeout specified");
  349. goto error;
  350. }
  351. if (source_addr == NULL) {
  352. hi_free(c->tcp.source_addr);
  353. c->tcp.source_addr = NULL;
  354. } else if (c->tcp.source_addr != source_addr) {
  355. hi_free(c->tcp.source_addr);
  356. c->tcp.source_addr = hi_strdup(source_addr);
  357. }
  358. snprintf(_port, 6, "%d", port);
  359. memset(&hints,0,sizeof(hints));
  360. hints.ai_family = AF_INET;
  361. hints.ai_socktype = SOCK_STREAM;
  362. /* Try with IPv6 if no IPv4 address was found. We do it in this order since
  363. * in a Redis client you can't afford to test if you have IPv6 connectivity
  364. * as this would add latency to every connect. Otherwise a more sensible
  365. * route could be: Use IPv6 if both addresses are available and there is IPv6
  366. * connectivity. */
  367. if ((rv = getaddrinfo(c->tcp.host,_port,&hints,&servinfo)) != 0) {
  368. hints.ai_family = AF_INET6;
  369. if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {
  370. __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv));
  371. return REDIS_ERR;
  372. }
  373. }
  374. for (p = servinfo; p != NULL; p = p->ai_next) {
  375. addrretry:
  376. if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == REDIS_INVALID_FD)
  377. continue;
  378. c->fd = s;
  379. if (redisSetBlocking(c,0) != REDIS_OK)
  380. goto error;
  381. if (c->tcp.source_addr) {
  382. int bound = 0;
  383. /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
  384. if ((rv = getaddrinfo(c->tcp.source_addr, NULL, &hints, &bservinfo)) != 0) {
  385. char buf[128];
  386. snprintf(buf,sizeof(buf),"Can't get addr: %s",gai_strerror(rv));
  387. __redisSetError(c,REDIS_ERR_OTHER,buf);
  388. goto error;
  389. }
  390. if (reuseaddr) {
  391. n = 1;
  392. if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*) &n,
  393. sizeof(n)) < 0) {
  394. freeaddrinfo(bservinfo);
  395. goto error;
  396. }
  397. }
  398. for (b = bservinfo; b != NULL; b = b->ai_next) {
  399. if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
  400. bound = 1;
  401. break;
  402. }
  403. }
  404. freeaddrinfo(bservinfo);
  405. if (!bound) {
  406. char buf[128];
  407. snprintf(buf,sizeof(buf),"Can't bind socket: %s",strerror(errno));
  408. __redisSetError(c,REDIS_ERR_OTHER,buf);
  409. goto error;
  410. }
  411. }
  412. /* For repeat connection */
  413. hi_free(c->saddr);
  414. c->saddr = hi_malloc(p->ai_addrlen);
  415. if (c->saddr == NULL)
  416. goto oom;
  417. memcpy(c->saddr, p->ai_addr, p->ai_addrlen);
  418. c->addrlen = p->ai_addrlen;
  419. if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
  420. if (errno == EHOSTUNREACH) {
  421. redisNetClose(c);
  422. continue;
  423. } else if (errno == EINPROGRESS) {
  424. if (blocking) {
  425. goto wait_for_ready;
  426. }
  427. /* This is ok.
  428. * Note that even when it's in blocking mode, we unset blocking
  429. * for `connect()`
  430. */
  431. } else if (errno == EADDRNOTAVAIL && reuseaddr) {
  432. if (++reuses >= REDIS_CONNECT_RETRIES) {
  433. goto error;
  434. } else {
  435. redisNetClose(c);
  436. goto addrretry;
  437. }
  438. } else {
  439. wait_for_ready:
  440. if (redisContextWaitReady(c,timeout_msec) != REDIS_OK)
  441. goto error;
  442. if (redisSetTcpNoDelay(c) != REDIS_OK)
  443. goto error;
  444. }
  445. }
  446. if (blocking && redisSetBlocking(c,1) != REDIS_OK)
  447. goto error;
  448. c->flags |= REDIS_CONNECTED;
  449. rv = REDIS_OK;
  450. goto end;
  451. }
  452. if (p == NULL) {
  453. char buf[128];
  454. snprintf(buf,sizeof(buf),"Can't create socket: %s",strerror(errno));
  455. __redisSetError(c,REDIS_ERR_OTHER,buf);
  456. goto error;
  457. }
  458. oom:
  459. __redisSetError(c, REDIS_ERR_OOM, "Out of memory");
  460. error:
  461. rv = REDIS_ERR;
  462. end:
  463. if(servinfo) {
  464. freeaddrinfo(servinfo);
  465. }
  466. return rv; // Need to return REDIS_OK if alright
  467. }
  468. int redisContextConnectTcp(redisContext *c, const char *addr, int port,
  469. const struct timeval *timeout) {
  470. return _redisContextConnectTcp(c, addr, port, timeout, NULL);
  471. }
  472. int redisContextConnectBindTcp(redisContext *c, const char *addr, int port,
  473. const struct timeval *timeout,
  474. const char *source_addr) {
  475. return _redisContextConnectTcp(c, addr, port, timeout, source_addr);
  476. }
  477. int redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout) {
  478. #ifndef _WIN32
  479. int blocking = (c->flags & REDIS_BLOCK);
  480. struct sockaddr_un *sa;
  481. long timeout_msec = -1;
  482. if (redisCreateSocket(c,AF_UNIX) < 0)
  483. return REDIS_ERR;
  484. if (redisSetBlocking(c,0) != REDIS_OK)
  485. return REDIS_ERR;
  486. c->connection_type = REDIS_CONN_UNIX;
  487. if (c->unix_sock.path != path) {
  488. hi_free(c->unix_sock.path);
  489. c->unix_sock.path = hi_strdup(path);
  490. if (c->unix_sock.path == NULL)
  491. goto oom;
  492. }
  493. if (timeout) {
  494. if (redisContextUpdateConnectTimeout(c, timeout) == REDIS_ERR)
  495. goto oom;
  496. } else {
  497. hi_free(c->connect_timeout);
  498. c->connect_timeout = NULL;
  499. }
  500. if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK)
  501. return REDIS_ERR;
  502. /* Don't leak sockaddr if we're reconnecting */
  503. if (c->saddr) hi_free(c->saddr);
  504. sa = (struct sockaddr_un*)(c->saddr = hi_malloc(sizeof(struct sockaddr_un)));
  505. if (sa == NULL)
  506. goto oom;
  507. c->addrlen = sizeof(struct sockaddr_un);
  508. sa->sun_family = AF_UNIX;
  509. strncpy(sa->sun_path, path, sizeof(sa->sun_path) - 1);
  510. if (connect(c->fd, (struct sockaddr*)sa, sizeof(*sa)) == -1) {
  511. if (errno == EINPROGRESS && !blocking) {
  512. /* This is ok. */
  513. } else {
  514. if (redisContextWaitReady(c,timeout_msec) != REDIS_OK)
  515. return REDIS_ERR;
  516. }
  517. }
  518. /* Reset socket to be blocking after connect(2). */
  519. if (blocking && redisSetBlocking(c,1) != REDIS_OK)
  520. return REDIS_ERR;
  521. c->flags |= REDIS_CONNECTED;
  522. return REDIS_OK;
  523. #else
  524. /* We currently do not support Unix sockets for Windows. */
  525. /* TODO(m): https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ */
  526. errno = EPROTONOSUPPORT;
  527. return REDIS_ERR;
  528. #endif /* _WIN32 */
  529. oom:
  530. __redisSetError(c, REDIS_ERR_OOM, "Out of memory");
  531. return REDIS_ERR;
  532. }