]> git.saurik.com Git - redis.git/blob - deps/hiredis/net.c
Update hiredis
[redis.git] / deps / hiredis / net.c
1 /* Extracted from anet.c to work properly with Hiredis error reporting.
2 *
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4 * Copyright (c) 2010, 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
33 #include "fmacros.h"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/select.h>
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
49 #include "net.h"
50 #include "sds.h"
51
52 /* Forward declaration */
53 void __redisSetError(redisContext *c, int type, sds err);
54
55 static int redisCreateSocket(redisContext *c, int type) {
56 int s, on = 1;
57 if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
58 __redisSetError(c,REDIS_ERR_IO,NULL);
59 return REDIS_ERR;
60 }
61 if (type == AF_INET) {
62 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
63 __redisSetError(c,REDIS_ERR_IO,NULL);
64 close(s);
65 return REDIS_ERR;
66 }
67 }
68 return s;
69 }
70
71 static int redisSetBlocking(redisContext *c, int fd, int blocking) {
72 int flags;
73
74 /* Set the socket nonblocking.
75 * Note that fcntl(2) for F_GETFL and F_SETFL can't be
76 * interrupted by a signal. */
77 if ((flags = fcntl(fd, F_GETFL)) == -1) {
78 __redisSetError(c,REDIS_ERR_IO,
79 sdscatprintf(sdsempty(), "fcntl(F_GETFL): %s", strerror(errno)));
80 close(fd);
81 return REDIS_ERR;
82 }
83
84 if (blocking)
85 flags &= ~O_NONBLOCK;
86 else
87 flags |= O_NONBLOCK;
88
89 if (fcntl(fd, F_SETFL, flags) == -1) {
90 __redisSetError(c,REDIS_ERR_IO,
91 sdscatprintf(sdsempty(), "fcntl(F_SETFL): %s", strerror(errno)));
92 close(fd);
93 return REDIS_ERR;
94 }
95 return REDIS_OK;
96 }
97
98 static int redisSetTcpNoDelay(redisContext *c, int fd) {
99 int yes = 1;
100 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
101 __redisSetError(c,REDIS_ERR_IO,
102 sdscatprintf(sdsempty(), "setsockopt(TCP_NODELAY): %s", strerror(errno)));
103 close(fd);
104 return REDIS_ERR;
105 }
106 return REDIS_OK;
107 }
108
109 static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *timeout) {
110 struct timeval to;
111 struct timeval *toptr = NULL;
112 fd_set wfd;
113 int err;
114 socklen_t errlen;
115
116 /* Only use timeout when not NULL. */
117 if (timeout != NULL) {
118 to = *timeout;
119 toptr = &to;
120 }
121
122 if (errno == EINPROGRESS) {
123 FD_ZERO(&wfd);
124 FD_SET(fd, &wfd);
125
126 if (select(FD_SETSIZE, NULL, &wfd, NULL, toptr) == -1) {
127 __redisSetError(c,REDIS_ERR_IO,
128 sdscatprintf(sdsempty(), "select(2): %s", strerror(errno)));
129 close(fd);
130 return REDIS_ERR;
131 }
132
133 if (!FD_ISSET(fd, &wfd)) {
134 errno = ETIMEDOUT;
135 __redisSetError(c,REDIS_ERR_IO,NULL);
136 close(fd);
137 return REDIS_ERR;
138 }
139
140 err = 0;
141 errlen = sizeof(err);
142 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
143 __redisSetError(c,REDIS_ERR_IO,
144 sdscatprintf(sdsempty(), "getsockopt(SO_ERROR): %s", strerror(errno)));
145 close(fd);
146 return REDIS_ERR;
147 }
148
149 if (err) {
150 errno = err;
151 __redisSetError(c,REDIS_ERR_IO,NULL);
152 close(fd);
153 return REDIS_ERR;
154 }
155
156 return REDIS_OK;
157 }
158
159 __redisSetError(c,REDIS_ERR_IO,NULL);
160 close(fd);
161 return REDIS_ERR;
162 }
163
164 int redisContextSetTimeout(redisContext *c, struct timeval tv) {
165 if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
166 __redisSetError(c,REDIS_ERR_IO,
167 sdscatprintf(sdsempty(), "setsockopt(SO_RCVTIMEO): %s", strerror(errno)));
168 return REDIS_ERR;
169 }
170 if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) {
171 __redisSetError(c,REDIS_ERR_IO,
172 sdscatprintf(sdsempty(), "setsockopt(SO_SNDTIMEO): %s", strerror(errno)));
173 return REDIS_ERR;
174 }
175 return REDIS_OK;
176 }
177
178 int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout) {
179 int s;
180 int blocking = (c->flags & REDIS_BLOCK);
181 struct sockaddr_in sa;
182
183 if ((s = redisCreateSocket(c,AF_INET)) < 0)
184 return REDIS_ERR;
185 if (redisSetBlocking(c,s,0) != REDIS_OK)
186 return REDIS_ERR;
187
188 sa.sin_family = AF_INET;
189 sa.sin_port = htons(port);
190 if (inet_aton(addr, &sa.sin_addr) == 0) {
191 struct hostent *he;
192
193 he = gethostbyname(addr);
194 if (he == NULL) {
195 __redisSetError(c,REDIS_ERR_OTHER,
196 sdscatprintf(sdsempty(),"Can't resolve: %s",addr));
197 close(s);
198 return REDIS_ERR;
199 }
200 memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
201 }
202
203 if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
204 if (errno == EINPROGRESS && !blocking) {
205 /* This is ok. */
206 } else {
207 if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
208 return REDIS_ERR;
209 }
210 }
211
212 /* Reset socket to be blocking after connect(2). */
213 if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
214 return REDIS_ERR;
215
216 if (redisSetTcpNoDelay(c,s) != REDIS_OK)
217 return REDIS_ERR;
218
219 c->fd = s;
220 c->flags |= REDIS_CONNECTED;
221 return REDIS_OK;
222 }
223
224 int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout) {
225 int s;
226 int blocking = (c->flags & REDIS_BLOCK);
227 struct sockaddr_un sa;
228
229 if ((s = redisCreateSocket(c,AF_LOCAL)) < 0)
230 return REDIS_ERR;
231 if (redisSetBlocking(c,s,0) != REDIS_OK)
232 return REDIS_ERR;
233
234 sa.sun_family = AF_LOCAL;
235 strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
236 if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
237 if (errno == EINPROGRESS && !blocking) {
238 /* This is ok. */
239 } else {
240 if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
241 return REDIS_ERR;
242 }
243 }
244
245 /* Reset socket to be blocking after connect(2). */
246 if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
247 return REDIS_ERR;
248
249 c->fd = s;
250 c->flags |= REDIS_CONNECTED;
251 return REDIS_OK;
252 }