net.c 20 KB

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