2
0

net.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "net.h"
  48. #include "sds.h"
  49. /* Defined in hiredis.c */
  50. void __redisSetError(redisContext *c, int type, const char *str);
  51. static void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) {
  52. char buf[128];
  53. size_t len = 0;
  54. if (prefix != NULL)
  55. len = snprintf(buf,sizeof(buf),"%s: ",prefix);
  56. strerror_r(errno,buf+len,sizeof(buf)-len);
  57. __redisSetError(c,type,buf);
  58. }
  59. static int redisSetReuseAddr(redisContext *c, int fd) {
  60. int on = 1;
  61. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
  62. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  63. close(fd);
  64. return REDIS_ERR;
  65. }
  66. return REDIS_OK;
  67. }
  68. static int redisCreateSocket(redisContext *c, int type) {
  69. int s;
  70. if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
  71. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  72. return REDIS_ERR;
  73. }
  74. if (type == AF_INET) {
  75. if (redisSetReuseAddr(c,s) == REDIS_ERR) {
  76. return REDIS_ERR;
  77. }
  78. }
  79. return s;
  80. }
  81. static int redisSetBlocking(redisContext *c, int fd, int blocking) {
  82. int flags;
  83. /* Set the socket nonblocking.
  84. * Note that fcntl(2) for F_GETFL and F_SETFL can't be
  85. * interrupted by a signal. */
  86. if ((flags = fcntl(fd, F_GETFL)) == -1) {
  87. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_GETFL)");
  88. close(fd);
  89. return REDIS_ERR;
  90. }
  91. if (blocking)
  92. flags &= ~O_NONBLOCK;
  93. else
  94. flags |= O_NONBLOCK;
  95. if (fcntl(fd, F_SETFL, flags) == -1) {
  96. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_SETFL)");
  97. close(fd);
  98. return REDIS_ERR;
  99. }
  100. return REDIS_OK;
  101. }
  102. static int redisSetTcpNoDelay(redisContext *c, int fd) {
  103. int yes = 1;
  104. if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
  105. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_NODELAY)");
  106. close(fd);
  107. return REDIS_ERR;
  108. }
  109. return REDIS_OK;
  110. }
  111. static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *timeout) {
  112. struct timeval to;
  113. struct timeval *toptr = NULL;
  114. fd_set wfd;
  115. /* Only use timeout when not NULL. */
  116. if (timeout != NULL) {
  117. to = *timeout;
  118. toptr = &to;
  119. }
  120. if (errno == EINPROGRESS) {
  121. FD_ZERO(&wfd);
  122. FD_SET(fd, &wfd);
  123. if (select(FD_SETSIZE, NULL, &wfd, NULL, toptr) == -1) {
  124. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"select(2)");
  125. close(fd);
  126. return REDIS_ERR;
  127. }
  128. if (!FD_ISSET(fd, &wfd)) {
  129. errno = ETIMEDOUT;
  130. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  131. close(fd);
  132. return REDIS_ERR;
  133. }
  134. if (redisCheckSocketError(c, fd) != REDIS_OK)
  135. return REDIS_ERR;
  136. return REDIS_OK;
  137. }
  138. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  139. close(fd);
  140. return REDIS_ERR;
  141. }
  142. int redisCheckSocketError(redisContext *c, int fd) {
  143. int err = 0;
  144. socklen_t errlen = sizeof(err);
  145. if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
  146. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)");
  147. close(fd);
  148. return REDIS_ERR;
  149. }
  150. if (err) {
  151. errno = err;
  152. __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
  153. close(fd);
  154. return REDIS_ERR;
  155. }
  156. return REDIS_OK;
  157. }
  158. int redisContextSetTimeout(redisContext *c, struct timeval tv) {
  159. if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
  160. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
  161. return REDIS_ERR;
  162. }
  163. if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) {
  164. __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)");
  165. return REDIS_ERR;
  166. }
  167. return REDIS_OK;
  168. }
  169. int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout) {
  170. int s, rv;
  171. char _port[6]; /* strlen("65535"); */
  172. struct addrinfo hints, *servinfo, *p;
  173. int blocking = (c->flags & REDIS_BLOCK);
  174. snprintf(_port, 6, "%d", port);
  175. memset(&hints,0,sizeof(hints));
  176. hints.ai_family = AF_INET;
  177. hints.ai_socktype = SOCK_STREAM;
  178. if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {
  179. __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv));
  180. return REDIS_ERR;
  181. }
  182. for (p = servinfo; p != NULL; p = p->ai_next) {
  183. if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
  184. continue;
  185. if (redisSetBlocking(c,s,0) != REDIS_OK)
  186. goto error;
  187. if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
  188. if (errno == EHOSTUNREACH) {
  189. close(s);
  190. continue;
  191. } else if (errno == EINPROGRESS && !blocking) {
  192. /* This is ok. */
  193. } else {
  194. if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
  195. goto error;
  196. }
  197. }
  198. if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
  199. goto error;
  200. if (redisSetTcpNoDelay(c,s) != REDIS_OK)
  201. goto error;
  202. c->fd = s;
  203. c->flags |= REDIS_CONNECTED;
  204. rv = REDIS_OK;
  205. goto end;
  206. }
  207. if (p == NULL) {
  208. char buf[128];
  209. snprintf(buf,sizeof(buf),"Can't create socket: %s",strerror(errno));
  210. __redisSetError(c,REDIS_ERR_OTHER,buf);
  211. goto error;
  212. }
  213. error:
  214. rv = REDIS_ERR;
  215. end:
  216. freeaddrinfo(servinfo);
  217. return rv; // Need to return REDIS_OK if alright
  218. }
  219. int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout) {
  220. int s;
  221. int blocking = (c->flags & REDIS_BLOCK);
  222. struct sockaddr_un sa;
  223. if ((s = redisCreateSocket(c,AF_LOCAL)) < 0)
  224. return REDIS_ERR;
  225. if (redisSetBlocking(c,s,0) != REDIS_OK)
  226. return REDIS_ERR;
  227. sa.sun_family = AF_LOCAL;
  228. strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
  229. if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
  230. if (errno == EINPROGRESS && !blocking) {
  231. /* This is ok. */
  232. } else {
  233. if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
  234. return REDIS_ERR;
  235. }
  236. }
  237. /* Reset socket to be blocking after connect(2). */
  238. if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
  239. return REDIS_ERR;
  240. c->fd = s;
  241. c->flags |= REDIS_CONNECTED;
  242. return REDIS_OK;
  243. }