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