2
0

net.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* Extracted from anet.c to work properly with Hiredis error reporting.
  2. *
  3. * Copyright (c) 2006-2011, Salvatore Sanfilippo <antirez at gmail dot com>
  4. * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of Redis nor the names of its contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "fmacros.h"
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <sys/select.h>
  36. #include <sys/un.h>
  37. #include <netinet/in.h>
  38. #include <netinet/tcp.h>
  39. #include <arpa/inet.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <string.h>
  43. #include <netdb.h>
  44. #include <errno.h>
  45. #include <stdarg.h>
  46. #include <stdio.h>
  47. #include <poll.h>
  48. #include <limits.h>
  49. #include "net.h"
  50. #include "sds.h"
  51. /* Defined in hiredis.c */
  52. void __redisSetError(redisContext *c, int type, const char *str);
  53. static void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) {
  54. char buf[128];
  55. size_t len = 0;
  56. if (prefix != NULL)
  57. len = snprintf(buf,sizeof(buf),"%s: ",prefix);
  58. strerror_r(errno,buf+len,sizeof(buf)-len);
  59. __redisSetError(c,type,buf);
  60. }
  61. static int redisSetReuseAddr(redisContext *c, int fd) {
  62. int on = 1;
  63. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
  64. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  65. close(fd);
  66. return REDIS_ERR;
  67. }
  68. return REDIS_OK;
  69. }
  70. static int redisCreateSocket(redisContext *c, int type) {
  71. int s;
  72. if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
  73. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  74. return REDIS_ERR;
  75. }
  76. if (type == AF_INET) {
  77. if (redisSetReuseAddr(c,s) == REDIS_ERR) {
  78. return REDIS_ERR;
  79. }
  80. }
  81. return s;
  82. }
  83. static int redisSetBlocking(redisContext *c, int fd, int blocking) {
  84. int flags;
  85. /* Set the socket nonblocking.
  86. * Note that fcntl(2) for F_GETFL and F_SETFL can't be
  87. * interrupted by a signal. */
  88. if ((flags = fcntl(fd, F_GETFL)) == -1) {
  89. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_GETFL)");
  90. close(fd);
  91. return REDIS_ERR;
  92. }
  93. if (blocking)
  94. flags &= ~O_NONBLOCK;
  95. else
  96. flags |= O_NONBLOCK;
  97. if (fcntl(fd, F_SETFL, flags) == -1) {
  98. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_SETFL)");
  99. close(fd);
  100. return REDIS_ERR;
  101. }
  102. return REDIS_OK;
  103. }
  104. static int redisSetTcpNoDelay(redisContext *c, int fd) {
  105. int yes = 1;
  106. if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
  107. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_NODELAY)");
  108. close(fd);
  109. return REDIS_ERR;
  110. }
  111. return REDIS_OK;
  112. }
  113. #define __MAX_MSEC (((LONG_MAX) - 999) / 1000)
  114. static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *timeout) {
  115. struct pollfd wfd[1];
  116. long msec;
  117. msec = -1;
  118. wfd[0].fd = fd;
  119. wfd[0].events = POLLOUT;
  120. /* Only use timeout when not NULL. */
  121. if (timeout != NULL) {
  122. if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) {
  123. close(fd);
  124. return REDIS_ERR;
  125. }
  126. msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000);
  127. if (msec < 0 || msec > INT_MAX) {
  128. msec = INT_MAX;
  129. }
  130. }
  131. if (errno == EINPROGRESS) {
  132. int res;
  133. if ((res = poll(wfd, 1, msec)) == -1) {
  134. __redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)");
  135. close(fd);
  136. return REDIS_ERR;
  137. } else if (res == 0) {
  138. errno = ETIMEDOUT;
  139. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  140. close(fd);
  141. return REDIS_ERR;
  142. }
  143. if (redisCheckSocketError(c, fd) != REDIS_OK)
  144. return REDIS_ERR;
  145. return REDIS_OK;
  146. }
  147. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  148. close(fd);
  149. return REDIS_ERR;
  150. }
  151. int redisCheckSocketError(redisContext *c, int fd) {
  152. int err = 0;
  153. socklen_t errlen = sizeof(err);
  154. if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
  155. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)");
  156. close(fd);
  157. return REDIS_ERR;
  158. }
  159. if (err) {
  160. errno = err;
  161. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  162. close(fd);
  163. return REDIS_ERR;
  164. }
  165. return REDIS_OK;
  166. }
  167. int redisContextSetTimeout(redisContext *c, struct timeval tv) {
  168. if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
  169. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
  170. return REDIS_ERR;
  171. }
  172. if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) {
  173. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)");
  174. return REDIS_ERR;
  175. }
  176. return REDIS_OK;
  177. }
  178. int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout) {
  179. int s, rv;
  180. char _port[6]; /* strlen("65535"); */
  181. struct addrinfo hints, *servinfo, *p;
  182. int blocking = (c->flags & REDIS_BLOCK);
  183. snprintf(_port, 6, "%d", port);
  184. memset(&hints,0,sizeof(hints));
  185. hints.ai_family = AF_INET;
  186. hints.ai_socktype = SOCK_STREAM;
  187. /* Try with IPv6 if no IPv4 address was found. We do it in this order since
  188. * in a Redis client you can't afford to test if you have IPv6 connectivity
  189. * as this would add latency to every connect. Otherwise a more sensible
  190. * route could be: Use IPv6 if both addresses are available and there is IPv6
  191. * connectivity. */
  192. if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {
  193. hints.ai_family = AF_INET6;
  194. if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {
  195. __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv));
  196. return REDIS_ERR;
  197. }
  198. }
  199. for (p = servinfo; p != NULL; p = p->ai_next) {
  200. if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
  201. continue;
  202. if (redisSetBlocking(c,s,0) != REDIS_OK)
  203. goto error;
  204. if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
  205. if (errno == EHOSTUNREACH) {
  206. close(s);
  207. continue;
  208. } else if (errno == EINPROGRESS && !blocking) {
  209. /* This is ok. */
  210. } else {
  211. if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
  212. goto error;
  213. }
  214. }
  215. if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
  216. goto error;
  217. if (redisSetTcpNoDelay(c,s) != REDIS_OK)
  218. goto error;
  219. c->fd = s;
  220. c->flags |= REDIS_CONNECTED;
  221. rv = REDIS_OK;
  222. goto end;
  223. }
  224. if (p == NULL) {
  225. char buf[128];
  226. snprintf(buf,sizeof(buf),"Can't create socket: %s",strerror(errno));
  227. __redisSetError(c,REDIS_ERR_OTHER,buf);
  228. goto error;
  229. }
  230. error:
  231. rv = REDIS_ERR;
  232. end:
  233. freeaddrinfo(servinfo);
  234. return rv; // Need to return REDIS_OK if alright
  235. }
  236. int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout) {
  237. int s;
  238. int blocking = (c->flags & REDIS_BLOCK);
  239. struct sockaddr_un sa;
  240. if ((s = redisCreateSocket(c,AF_LOCAL)) < 0)
  241. return REDIS_ERR;
  242. if (redisSetBlocking(c,s,0) != REDIS_OK)
  243. return REDIS_ERR;
  244. sa.sun_family = AF_LOCAL;
  245. strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
  246. if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
  247. if (errno == EINPROGRESS && !blocking) {
  248. /* This is ok. */
  249. } else {
  250. if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
  251. return REDIS_ERR;
  252. }
  253. }
  254. /* Reset socket to be blocking after connect(2). */
  255. if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
  256. return REDIS_ERR;
  257. c->fd = s;
  258. c->flags |= REDIS_CONNECTED;
  259. return REDIS_OK;
  260. }