]> git.saurik.com Git - redis.git/blob - deps/hiredis/net.c
88171461e2135f22c5d181f54fca44740c35aebd
[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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Redis nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "fmacros.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <netinet/in.h>
37 #include <netinet/tcp.h>
38 #include <arpa/inet.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <netdb.h>
43 #include <errno.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46
47 #include "net.h"
48 #include "sds.h"
49
50 /* Forward declaration */
51 void __redisSetError(redisContext *c, int type, sds err);
52
53 static int redisCreateSocket(redisContext *c, int type) {
54 int s, on = 1;
55 if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
56 __redisSetError(c,REDIS_ERR_IO,NULL);
57 return REDIS_ERR;
58 }
59 if (type == AF_INET) {
60 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
61 __redisSetError(c,REDIS_ERR_IO,NULL);
62 close(s);
63 return REDIS_ERR;
64 }
65 }
66 return s;
67 }
68
69 static int redisSetNonBlock(redisContext *c, int fd) {
70 int flags;
71
72 /* Set the socket nonblocking.
73 * Note that fcntl(2) for F_GETFL and F_SETFL can't be
74 * interrupted by a signal. */
75 if ((flags = fcntl(fd, F_GETFL)) == -1) {
76 __redisSetError(c,REDIS_ERR_IO,
77 sdscatprintf(sdsempty(), "fcntl(F_GETFL): %s", strerror(errno)));
78 close(fd);
79 return REDIS_ERR;
80 }
81 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
82 __redisSetError(c,REDIS_ERR_IO,
83 sdscatprintf(sdsempty(), "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno)));
84 close(fd);
85 return REDIS_ERR;
86 }
87 return REDIS_OK;
88 }
89
90 static int redisSetTcpNoDelay(redisContext *c, int fd) {
91 int yes = 1;
92 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
93 __redisSetError(c,REDIS_ERR_IO,
94 sdscatprintf(sdsempty(), "setsockopt(TCP_NODELAY): %s", strerror(errno)));
95 return REDIS_ERR;
96 }
97 return REDIS_OK;
98 }
99
100 int redisContextConnectTcp(redisContext *c, const char *addr, int port) {
101 int s;
102 int blocking = (c->flags & REDIS_BLOCK);
103 struct sockaddr_in sa;
104
105 if ((s = redisCreateSocket(c,AF_INET)) == REDIS_ERR)
106 return REDIS_ERR;
107 if (!blocking && redisSetNonBlock(c,s) == REDIS_ERR)
108 return REDIS_ERR;
109
110 sa.sin_family = AF_INET;
111 sa.sin_port = htons(port);
112 if (inet_aton(addr, &sa.sin_addr) == 0) {
113 struct hostent *he;
114
115 he = gethostbyname(addr);
116 if (he == NULL) {
117 __redisSetError(c,REDIS_ERR_OTHER,
118 sdscatprintf(sdsempty(),"Can't resolve: %s",addr));
119 close(s);
120 return REDIS_ERR;
121 }
122 memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
123 }
124
125 if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
126 if (errno == EINPROGRESS && !blocking) {
127 /* This is ok. */
128 } else {
129 __redisSetError(c,REDIS_ERR_IO,NULL);
130 close(s);
131 return REDIS_ERR;
132 }
133 }
134
135 if (redisSetTcpNoDelay(c,s) != REDIS_OK) {
136 close(s);
137 return REDIS_ERR;
138 }
139
140 c->fd = s;
141 c->flags |= REDIS_CONNECTED;
142 return REDIS_OK;
143 }
144
145 int redisContextConnectUnix(redisContext *c, const char *path) {
146 int s;
147 int blocking = (c->flags & REDIS_BLOCK);
148 struct sockaddr_un sa;
149
150 if ((s = redisCreateSocket(c,AF_LOCAL)) == REDIS_ERR)
151 return REDIS_ERR;
152 if (!blocking && redisSetNonBlock(c,s) != REDIS_OK)
153 return REDIS_ERR;
154
155 sa.sun_family = AF_LOCAL;
156 strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
157 if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
158 if (errno == EINPROGRESS && !blocking) {
159 /* This is ok. */
160 } else {
161 __redisSetError(c,REDIS_ERR_IO,NULL);
162 close(s);
163 return REDIS_ERR;
164 }
165 }
166
167 c->fd = s;
168 c->flags |= REDIS_CONNECTED;
169 return REDIS_OK;
170 }