]> git.saurik.com Git - redis.git/blame - deps/hiredis/net.c
Merge pull request #628 from pietern/unstable-zip
[redis.git] / deps / hiredis / net.c
CommitLineData
24f753a8
PN
1/* Extracted from anet.c to work properly with Hiredis error reporting.
2 *
b66e5add 3 * Copyright (c) 2006-2011, Salvatore Sanfilippo <antirez at gmail dot com>
4 * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>
24f753a8 5 *
a1e97d69 6 * All rights reserved.
9703b1b3 7 *
24f753a8
PN
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
33#include "fmacros.h"
34#include <sys/types.h>
35#include <sys/socket.h>
9703b1b3 36#include <sys/select.h>
24f753a8
PN
37#include <sys/un.h>
38#include <netinet/in.h>
39#include <netinet/tcp.h>
40#include <arpa/inet.h>
41#include <unistd.h>
42#include <fcntl.h>
43#include <string.h>
44#include <netdb.h>
45#include <errno.h>
46#include <stdarg.h>
47#include <stdio.h>
d6704c9b 48#include <poll.h>
49#include <limits.h>
24f753a8 50
a1e97d69 51#include "net.h"
24f753a8
PN
52#include "sds.h"
53
b66e5add 54/* Defined in hiredis.c */
55void __redisSetError(redisContext *c, int type, const char *str);
56
57static void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) {
58 char buf[128];
59 size_t len = 0;
60
61 if (prefix != NULL)
62 len = snprintf(buf,sizeof(buf),"%s: ",prefix);
63 strerror_r(errno,buf+len,sizeof(buf)-len);
64 __redisSetError(c,type,buf);
65}
66
67static int redisSetReuseAddr(redisContext *c, int fd) {
68 int on = 1;
69 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
70 __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
71 close(fd);
72 return REDIS_ERR;
73 }
74 return REDIS_OK;
75}
24f753a8
PN
76
77static int redisCreateSocket(redisContext *c, int type) {
b66e5add 78 int s;
24f753a8 79 if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
b66e5add 80 __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
24f753a8
PN
81 return REDIS_ERR;
82 }
83 if (type == AF_INET) {
b66e5add 84 if (redisSetReuseAddr(c,s) == REDIS_ERR) {
24f753a8
PN
85 return REDIS_ERR;
86 }
87 }
88 return s;
89}
90
9703b1b3 91static int redisSetBlocking(redisContext *c, int fd, int blocking) {
24f753a8
PN
92 int flags;
93
94 /* Set the socket nonblocking.
95 * Note that fcntl(2) for F_GETFL and F_SETFL can't be
96 * interrupted by a signal. */
97 if ((flags = fcntl(fd, F_GETFL)) == -1) {
b66e5add 98 __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_GETFL)");
24f753a8
PN
99 close(fd);
100 return REDIS_ERR;
101 }
9703b1b3
PN
102
103 if (blocking)
104 flags &= ~O_NONBLOCK;
105 else
106 flags |= O_NONBLOCK;
107
108 if (fcntl(fd, F_SETFL, flags) == -1) {
b66e5add 109 __redisSetErrorFromErrno(c,REDIS_ERR_IO,"fcntl(F_SETFL)");
24f753a8
PN
110 close(fd);
111 return REDIS_ERR;
112 }
113 return REDIS_OK;
114}
115
116static int redisSetTcpNoDelay(redisContext *c, int fd) {
117 int yes = 1;
118 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
b66e5add 119 __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_NODELAY)");
9703b1b3 120 close(fd);
24f753a8
PN
121 return REDIS_ERR;
122 }
123 return REDIS_OK;
124}
125
d6704c9b 126#define __MAX_MSEC (((LONG_MAX) - 999) / 1000)
127
9703b1b3 128static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *timeout) {
d6704c9b 129 struct pollfd wfd[1];
130 long msec;
131
132 msec = -1;
133 wfd[0].fd = fd;
134 wfd[0].events = POLLOUT;
9703b1b3
PN
135
136 /* Only use timeout when not NULL. */
137 if (timeout != NULL) {
d6704c9b 138 if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) {
139 close(fd);
140 return REDIS_ERR;
141 }
142
143 msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000);
144
145 if (msec < 0 || msec > INT_MAX) {
146 msec = INT_MAX;
147 }
9703b1b3
PN
148 }
149
150 if (errno == EINPROGRESS) {
d6704c9b 151 int res;
9703b1b3 152
d6704c9b 153 if ((res = poll(wfd, 1, msec)) == -1) {
154 __redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)");
9703b1b3
PN
155 close(fd);
156 return REDIS_ERR;
d6704c9b 157 } else if (res == 0) {
9703b1b3 158 errno = ETIMEDOUT;
b66e5add 159 __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
9703b1b3
PN
160 close(fd);
161 return REDIS_ERR;
162 }
163
b66e5add 164 if (redisCheckSocketError(c, fd) != REDIS_OK)
9703b1b3 165 return REDIS_ERR;
9703b1b3
PN
166
167 return REDIS_OK;
168 }
169
b66e5add 170 __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
9703b1b3
PN
171 close(fd);
172 return REDIS_ERR;
173}
174
b66e5add 175int redisCheckSocketError(redisContext *c, int fd) {
176 int err = 0;
177 socklen_t errlen = sizeof(err);
178
179 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
180 __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)");
181 close(fd);
182 return REDIS_ERR;
183 }
184
185 if (err) {
186 errno = err;
187 __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
188 close(fd);
189 return REDIS_ERR;
190 }
191
192 return REDIS_OK;
193}
194
9703b1b3
PN
195int redisContextSetTimeout(redisContext *c, struct timeval tv) {
196 if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
b66e5add 197 __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
9703b1b3
PN
198 return REDIS_ERR;
199 }
200 if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) {
b66e5add 201 __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)");
9703b1b3
PN
202 return REDIS_ERR;
203 }
204 return REDIS_OK;
205}
206
207int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout) {
b66e5add 208 int s, rv;
209 char _port[6]; /* strlen("65535"); */
210 struct addrinfo hints, *servinfo, *p;
24f753a8 211 int blocking = (c->flags & REDIS_BLOCK);
24f753a8 212
b66e5add 213 snprintf(_port, 6, "%d", port);
214 memset(&hints,0,sizeof(hints));
215 hints.ai_family = AF_INET;
216 hints.ai_socktype = SOCK_STREAM;
24f753a8 217
b66e5add 218 if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {
219 __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv));
220 return REDIS_ERR;
24f753a8 221 }
b66e5add 222 for (p = servinfo; p != NULL; p = p->ai_next) {
223 if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
224 continue;
225
226 if (redisSetBlocking(c,s,0) != REDIS_OK)
227 goto error;
228 if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
229 if (errno == EHOSTUNREACH) {
230 close(s);
231 continue;
232 } else if (errno == EINPROGRESS && !blocking) {
233 /* This is ok. */
234 } else {
235 if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
236 goto error;
237 }
24f753a8 238 }
b66e5add 239 if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
240 goto error;
241 if (redisSetTcpNoDelay(c,s) != REDIS_OK)
242 goto error;
243
244 c->fd = s;
245 c->flags |= REDIS_CONNECTED;
246 rv = REDIS_OK;
247 goto end;
248 }
249 if (p == NULL) {
250 char buf[128];
251 snprintf(buf,sizeof(buf),"Can't create socket: %s",strerror(errno));
252 __redisSetError(c,REDIS_ERR_OTHER,buf);
253 goto error;
24f753a8
PN
254 }
255
b66e5add 256error:
257 rv = REDIS_ERR;
258end:
259 freeaddrinfo(servinfo);
260 return rv; // Need to return REDIS_OK if alright
24f753a8
PN
261}
262
9703b1b3 263int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout) {
24f753a8
PN
264 int s;
265 int blocking = (c->flags & REDIS_BLOCK);
266 struct sockaddr_un sa;
267
9703b1b3 268 if ((s = redisCreateSocket(c,AF_LOCAL)) < 0)
24f753a8 269 return REDIS_ERR;
9703b1b3 270 if (redisSetBlocking(c,s,0) != REDIS_OK)
24f753a8
PN
271 return REDIS_ERR;
272
273 sa.sun_family = AF_LOCAL;
274 strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
275 if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
276 if (errno == EINPROGRESS && !blocking) {
277 /* This is ok. */
278 } else {
9703b1b3
PN
279 if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
280 return REDIS_ERR;
24f753a8
PN
281 }
282 }
283
9703b1b3
PN
284 /* Reset socket to be blocking after connect(2). */
285 if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
286 return REDIS_ERR;
287
24f753a8 288 c->fd = s;
a1e97d69 289 c->flags |= REDIS_CONNECTED;
24f753a8
PN
290 return REDIS_OK;
291}