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