]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | /* anet.c -- Basic TCP socket stuff made a bit less boring |
2 | * | |
d288ee65 | 3 | * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com> |
ed9b544e | 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 | ||
23d4709d | 31 | #include "fmacros.h" |
32 | ||
ed9b544e | 33 | #include <sys/types.h> |
34 | #include <sys/socket.h> | |
85238765 | 35 | #include <sys/stat.h> |
c61e6925 | 36 | #include <sys/un.h> |
ed9b544e | 37 | #include <netinet/in.h> |
38 | #include <netinet/tcp.h> | |
39 | #include <arpa/inet.h> | |
40 | #include <unistd.h> | |
41 | #include <fcntl.h> | |
42 | #include <string.h> | |
43 | #include <netdb.h> | |
44 | #include <errno.h> | |
45 | #include <stdarg.h> | |
46 | #include <stdio.h> | |
47 | ||
48 | #include "anet.h" | |
49 | ||
50 | static void anetSetError(char *err, const char *fmt, ...) | |
51 | { | |
52 | va_list ap; | |
53 | ||
54 | if (!err) return; | |
55 | va_start(ap, fmt); | |
56 | vsnprintf(err, ANET_ERR_LEN, fmt, ap); | |
57 | va_end(ap); | |
58 | } | |
59 | ||
60 | int anetNonBlock(char *err, int fd) | |
61 | { | |
62 | int flags; | |
63 | ||
64 | /* Set the socket nonblocking. | |
65 | * Note that fcntl(2) for F_GETFL and F_SETFL can't be | |
66 | * interrupted by a signal. */ | |
67 | if ((flags = fcntl(fd, F_GETFL)) == -1) { | |
9b1d738f | 68 | anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno)); |
ed9b544e | 69 | return ANET_ERR; |
70 | } | |
71 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { | |
9b1d738f | 72 | anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno)); |
ed9b544e | 73 | return ANET_ERR; |
74 | } | |
75 | return ANET_OK; | |
76 | } | |
77 | ||
78 | int anetTcpNoDelay(char *err, int fd) | |
79 | { | |
80 | int yes = 1; | |
81 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) | |
82 | { | |
9b1d738f | 83 | anetSetError(err, "setsockopt TCP_NODELAY: %s", strerror(errno)); |
ed9b544e | 84 | return ANET_ERR; |
85 | } | |
86 | return ANET_OK; | |
87 | } | |
88 | ||
89 | int anetSetSendBuffer(char *err, int fd, int buffsize) | |
90 | { | |
91 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof(buffsize)) == -1) | |
92 | { | |
9b1d738f | 93 | anetSetError(err, "setsockopt SO_SNDBUF: %s", strerror(errno)); |
ed9b544e | 94 | return ANET_ERR; |
95 | } | |
96 | return ANET_OK; | |
97 | } | |
98 | ||
99 | int anetTcpKeepAlive(char *err, int fd) | |
100 | { | |
101 | int yes = 1; | |
102 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1) { | |
9b1d738f | 103 | anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno)); |
ed9b544e | 104 | return ANET_ERR; |
105 | } | |
106 | return ANET_OK; | |
107 | } | |
108 | ||
109 | int anetResolve(char *err, char *host, char *ipbuf) | |
110 | { | |
111 | struct sockaddr_in sa; | |
112 | ||
113 | sa.sin_family = AF_INET; | |
114 | if (inet_aton(host, &sa.sin_addr) == 0) { | |
115 | struct hostent *he; | |
116 | ||
117 | he = gethostbyname(host); | |
118 | if (he == NULL) { | |
9b1d738f | 119 | anetSetError(err, "can't resolve: %s", host); |
ed9b544e | 120 | return ANET_ERR; |
121 | } | |
122 | memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr)); | |
123 | } | |
124 | strcpy(ipbuf,inet_ntoa(sa.sin_addr)); | |
125 | return ANET_OK; | |
126 | } | |
127 | ||
704bd093 PN |
128 | static int anetCreateSocket(char *err, int domain) { |
129 | int s, on = 1; | |
130 | if ((s = socket(domain, SOCK_STREAM, 0)) == -1) { | |
9b1d738f | 131 | anetSetError(err, "creating socket: %s", strerror(errno)); |
704bd093 PN |
132 | return ANET_ERR; |
133 | } | |
134 | ||
135 | /* Make sure connection-intensive things like the redis benckmark | |
136 | * will be able to close/open sockets a zillion of times */ | |
137 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { | |
9b1d738f | 138 | anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno)); |
704bd093 PN |
139 | return ANET_ERR; |
140 | } | |
141 | return s; | |
142 | } | |
143 | ||
ed9b544e | 144 | #define ANET_CONNECT_NONE 0 |
145 | #define ANET_CONNECT_NONBLOCK 1 | |
146 | static int anetTcpGenericConnect(char *err, char *addr, int port, int flags) | |
147 | { | |
704bd093 | 148 | int s; |
ed9b544e | 149 | struct sockaddr_in sa; |
150 | ||
704bd093 | 151 | if ((s = anetCreateSocket(err,AF_INET)) == ANET_ERR) |
ed9b544e | 152 | return ANET_ERR; |
ed9b544e | 153 | |
154 | sa.sin_family = AF_INET; | |
155 | sa.sin_port = htons(port); | |
156 | if (inet_aton(addr, &sa.sin_addr) == 0) { | |
157 | struct hostent *he; | |
158 | ||
159 | he = gethostbyname(addr); | |
160 | if (he == NULL) { | |
9b1d738f | 161 | anetSetError(err, "can't resolve: %s", addr); |
ed9b544e | 162 | close(s); |
163 | return ANET_ERR; | |
164 | } | |
165 | memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr)); | |
166 | } | |
167 | if (flags & ANET_CONNECT_NONBLOCK) { | |
168 | if (anetNonBlock(err,s) != ANET_OK) | |
169 | return ANET_ERR; | |
170 | } | |
171 | if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) { | |
172 | if (errno == EINPROGRESS && | |
173 | flags & ANET_CONNECT_NONBLOCK) | |
174 | return s; | |
175 | ||
9b1d738f | 176 | anetSetError(err, "connect: %s", strerror(errno)); |
ed9b544e | 177 | close(s); |
178 | return ANET_ERR; | |
179 | } | |
180 | return s; | |
181 | } | |
182 | ||
183 | int anetTcpConnect(char *err, char *addr, int port) | |
184 | { | |
185 | return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONE); | |
186 | } | |
187 | ||
188 | int anetTcpNonBlockConnect(char *err, char *addr, int port) | |
189 | { | |
190 | return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONBLOCK); | |
191 | } | |
192 | ||
c61e6925 PN |
193 | int anetUnixGenericConnect(char *err, char *path, int flags) |
194 | { | |
195 | int s; | |
196 | struct sockaddr_un sa; | |
197 | ||
704bd093 | 198 | if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR) |
c61e6925 | 199 | return ANET_ERR; |
704bd093 | 200 | |
c61e6925 PN |
201 | sa.sun_family = AF_LOCAL; |
202 | strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); | |
203 | if (flags & ANET_CONNECT_NONBLOCK) { | |
204 | if (anetNonBlock(err,s) != ANET_OK) | |
205 | return ANET_ERR; | |
206 | } | |
207 | if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) == -1) { | |
208 | if (errno == EINPROGRESS && | |
209 | flags & ANET_CONNECT_NONBLOCK) | |
210 | return s; | |
211 | ||
9b1d738f | 212 | anetSetError(err, "connect: %s", strerror(errno)); |
c61e6925 PN |
213 | close(s); |
214 | return ANET_ERR; | |
215 | } | |
216 | return s; | |
217 | } | |
218 | ||
219 | int anetUnixConnect(char *err, char *path) | |
220 | { | |
221 | return anetUnixGenericConnect(err,path,ANET_CONNECT_NONE); | |
222 | } | |
223 | ||
224 | int anetUnixNonBlockConnect(char *err, char *path) | |
225 | { | |
226 | return anetUnixGenericConnect(err,path,ANET_CONNECT_NONBLOCK); | |
227 | } | |
228 | ||
ed9b544e | 229 | /* Like read(2) but make sure 'count' is read before to return |
230 | * (unless error or EOF condition is encountered) */ | |
a4d1ba9a | 231 | int anetRead(int fd, char *buf, int count) |
ed9b544e | 232 | { |
233 | int nread, totlen = 0; | |
234 | while(totlen != count) { | |
235 | nread = read(fd,buf,count-totlen); | |
236 | if (nread == 0) return totlen; | |
237 | if (nread == -1) return -1; | |
238 | totlen += nread; | |
239 | buf += nread; | |
240 | } | |
241 | return totlen; | |
242 | } | |
243 | ||
244 | /* Like write(2) but make sure 'count' is read before to return | |
245 | * (unless error is encountered) */ | |
a4d1ba9a | 246 | int anetWrite(int fd, char *buf, int count) |
ed9b544e | 247 | { |
248 | int nwritten, totlen = 0; | |
249 | while(totlen != count) { | |
250 | nwritten = write(fd,buf,count-totlen); | |
251 | if (nwritten == 0) return totlen; | |
252 | if (nwritten == -1) return -1; | |
253 | totlen += nwritten; | |
254 | buf += nwritten; | |
255 | } | |
256 | return totlen; | |
257 | } | |
258 | ||
704bd093 PN |
259 | static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len) { |
260 | if (bind(s,sa,len) == -1) { | |
9b1d738f | 261 | anetSetError(err, "bind: %s", strerror(errno)); |
704bd093 | 262 | close(s); |
ed9b544e | 263 | return ANET_ERR; |
264 | } | |
b9cd703b ED |
265 | |
266 | /* Use a backlog of 512 entries. We pass 511 to the listen() call because | |
267 | * the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1); | |
268 | * which will thus give us a backlog of 512 entries */ | |
269 | if (listen(s, 511) == -1) { | |
9b1d738f | 270 | anetSetError(err, "listen: %s", strerror(errno)); |
ed9b544e | 271 | close(s); |
272 | return ANET_ERR; | |
273 | } | |
704bd093 PN |
274 | return ANET_OK; |
275 | } | |
276 | ||
277 | int anetTcpServer(char *err, int port, char *bindaddr) | |
278 | { | |
279 | int s; | |
280 | struct sockaddr_in sa; | |
281 | ||
282 | if ((s = anetCreateSocket(err,AF_INET)) == ANET_ERR) | |
283 | return ANET_ERR; | |
284 | ||
ed9b544e | 285 | memset(&sa,0,sizeof(sa)); |
286 | sa.sin_family = AF_INET; | |
287 | sa.sin_port = htons(port); | |
288 | sa.sin_addr.s_addr = htonl(INADDR_ANY); | |
704bd093 | 289 | if (bindaddr && inet_aton(bindaddr, &sa.sin_addr) == 0) { |
9b1d738f | 290 | anetSetError(err, "invalid bind address"); |
ed9b544e | 291 | close(s); |
292 | return ANET_ERR; | |
293 | } | |
704bd093 | 294 | if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR) |
ed9b544e | 295 | return ANET_ERR; |
ed9b544e | 296 | return s; |
297 | } | |
298 | ||
85238765 | 299 | int anetUnixServer(char *err, char *path, mode_t perm) |
c61e6925 PN |
300 | { |
301 | int s; | |
302 | struct sockaddr_un sa; | |
303 | ||
704bd093 | 304 | if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR) |
c61e6925 | 305 | return ANET_ERR; |
704bd093 | 306 | |
c61e6925 PN |
307 | memset(&sa,0,sizeof(sa)); |
308 | sa.sun_family = AF_LOCAL; | |
309 | strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); | |
704bd093 | 310 | if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR) |
c61e6925 | 311 | return ANET_ERR; |
85238765 NF |
312 | if (perm) |
313 | chmod(sa.sun_path, perm); | |
c61e6925 PN |
314 | return s; |
315 | } | |
316 | ||
ab17b909 | 317 | static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) { |
ed9b544e | 318 | int fd; |
ed9b544e | 319 | while(1) { |
ab17b909 | 320 | fd = accept(s,sa,len); |
ed9b544e | 321 | if (fd == -1) { |
322 | if (errno == EINTR) | |
323 | continue; | |
324 | else { | |
9b1d738f | 325 | anetSetError(err, "accept: %s", strerror(errno)); |
ed9b544e | 326 | return ANET_ERR; |
327 | } | |
328 | } | |
329 | break; | |
330 | } | |
ab17b909 PN |
331 | return fd; |
332 | } | |
333 | ||
334 | int anetTcpAccept(char *err, int s, char *ip, int *port) { | |
335 | int fd; | |
336 | struct sockaddr_in sa; | |
337 | socklen_t salen = sizeof(sa); | |
338 | if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR) | |
339 | return ANET_ERR; | |
340 | ||
ed9b544e | 341 | if (ip) strcpy(ip,inet_ntoa(sa.sin_addr)); |
342 | if (port) *port = ntohs(sa.sin_port); | |
343 | return fd; | |
344 | } | |
ab17b909 | 345 | |
4fe83b55 | 346 | int anetUnixAccept(char *err, int s) { |
ab17b909 PN |
347 | int fd; |
348 | struct sockaddr_un sa; | |
349 | socklen_t salen = sizeof(sa); | |
350 | if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR) | |
351 | return ANET_ERR; | |
352 | ||
ab17b909 PN |
353 | return fd; |
354 | } | |
3cd12b56 | 355 | |
356 | int anetPeerToString(int fd, char *ip, int *port) { | |
357 | struct sockaddr_in sa; | |
358 | socklen_t salen = sizeof(sa); | |
359 | ||
7b845b62 | 360 | if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) { |
361 | *port = 0; | |
362 | ip[0] = '?'; | |
363 | ip[1] = '\0'; | |
364 | return -1; | |
365 | } | |
3cd12b56 | 366 | if (ip) strcpy(ip,inet_ntoa(sa.sin_addr)); |
367 | if (port) *port = ntohs(sa.sin_port); | |
368 | return 0; | |
369 | } | |
120ba392 | 370 | |
371 | int anetSockName(int fd, char *ip, int *port) { | |
372 | struct sockaddr_in sa; | |
373 | socklen_t salen = sizeof(sa); | |
374 | ||
375 | if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) { | |
376 | *port = 0; | |
377 | ip[0] = '?'; | |
378 | ip[1] = '\0'; | |
379 | return -1; | |
380 | } | |
381 | if (ip) strcpy(ip,inet_ntoa(sa.sin_addr)); | |
382 | if (port) *port = ntohs(sa.sin_port); | |
383 | return 0; | |
384 | } |