]>
git.saurik.com Git - redis.git/blob - src/anet.c
1 /* anet.c -- Basic TCP socket stuff made a bit less boring
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/tcp.h>
38 #include <arpa/inet.h>
49 static void anetSetError(char *err
, const char *fmt
, ...)
55 vsnprintf(err
, ANET_ERR_LEN
, fmt
, ap
);
59 int anetNonBlock(char *err
, int fd
)
63 /* Set the socket nonblocking.
64 * Note that fcntl(2) for F_GETFL and F_SETFL can't be
65 * interrupted by a signal. */
66 if ((flags
= fcntl(fd
, F_GETFL
)) == -1) {
67 anetSetError(err
, "fcntl(F_GETFL): %s", strerror(errno
));
70 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
) == -1) {
71 anetSetError(err
, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno
));
77 int anetTcpNoDelay(char *err
, int fd
)
80 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &yes
, sizeof(yes
)) == -1)
82 anetSetError(err
, "setsockopt TCP_NODELAY: %s", strerror(errno
));
88 int anetSetSendBuffer(char *err
, int fd
, int buffsize
)
90 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &buffsize
, sizeof(buffsize
)) == -1)
92 anetSetError(err
, "setsockopt SO_SNDBUF: %s", strerror(errno
));
98 int anetTcpKeepAlive(char *err
, int fd
)
101 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &yes
, sizeof(yes
)) == -1) {
102 anetSetError(err
, "setsockopt SO_KEEPALIVE: %s", strerror(errno
));
108 int anetResolve(char *err
, char *host
, char *ipbuf
)
110 struct sockaddr_in sa
;
112 sa
.sin_family
= AF_INET
;
113 if (inet_aton(host
, &sa
.sin_addr
) == 0) {
116 he
= gethostbyname(host
);
118 anetSetError(err
, "can't resolve: %s", host
);
121 memcpy(&sa
.sin_addr
, he
->h_addr
, sizeof(struct in_addr
));
123 strcpy(ipbuf
,inet_ntoa(sa
.sin_addr
));
127 static int anetCreateSocket(char *err
, int domain
) {
129 if ((s
= socket(domain
, SOCK_STREAM
, 0)) == -1) {
130 anetSetError(err
, "creating socket: %s", strerror(errno
));
134 /* Make sure connection-intensive things like the redis benckmark
135 * will be able to close/open sockets a zillion of times */
136 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) == -1) {
137 anetSetError(err
, "setsockopt SO_REUSEADDR: %s", strerror(errno
));
143 #define ANET_CONNECT_NONE 0
144 #define ANET_CONNECT_NONBLOCK 1
145 static int anetTcpGenericConnect(char *err
, char *addr
, int port
, int flags
)
148 struct sockaddr_in sa
;
150 if ((s
= anetCreateSocket(err
,AF_INET
)) == ANET_ERR
)
153 sa
.sin_family
= AF_INET
;
154 sa
.sin_port
= htons(port
);
155 if (inet_aton(addr
, &sa
.sin_addr
) == 0) {
158 he
= gethostbyname(addr
);
160 anetSetError(err
, "can't resolve: %s", addr
);
164 memcpy(&sa
.sin_addr
, he
->h_addr
, sizeof(struct in_addr
));
166 if (flags
& ANET_CONNECT_NONBLOCK
) {
167 if (anetNonBlock(err
,s
) != ANET_OK
)
170 if (connect(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1) {
171 if (errno
== EINPROGRESS
&&
172 flags
& ANET_CONNECT_NONBLOCK
)
175 anetSetError(err
, "connect: %s", strerror(errno
));
182 int anetTcpConnect(char *err
, char *addr
, int port
)
184 return anetTcpGenericConnect(err
,addr
,port
,ANET_CONNECT_NONE
);
187 int anetTcpNonBlockConnect(char *err
, char *addr
, int port
)
189 return anetTcpGenericConnect(err
,addr
,port
,ANET_CONNECT_NONBLOCK
);
192 int anetUnixGenericConnect(char *err
, char *path
, int flags
)
195 struct sockaddr_un sa
;
197 if ((s
= anetCreateSocket(err
,AF_LOCAL
)) == ANET_ERR
)
200 sa
.sun_family
= AF_LOCAL
;
201 strncpy(sa
.sun_path
,path
,sizeof(sa
.sun_path
)-1);
202 if (flags
& ANET_CONNECT_NONBLOCK
) {
203 if (anetNonBlock(err
,s
) != ANET_OK
)
206 if (connect(s
,(struct sockaddr
*)&sa
,sizeof(sa
)) == -1) {
207 if (errno
== EINPROGRESS
&&
208 flags
& ANET_CONNECT_NONBLOCK
)
211 anetSetError(err
, "connect: %s", strerror(errno
));
218 int anetUnixConnect(char *err
, char *path
)
220 return anetUnixGenericConnect(err
,path
,ANET_CONNECT_NONE
);
223 int anetUnixNonBlockConnect(char *err
, char *path
)
225 return anetUnixGenericConnect(err
,path
,ANET_CONNECT_NONBLOCK
);
228 /* Like read(2) but make sure 'count' is read before to return
229 * (unless error or EOF condition is encountered) */
230 int anetRead(int fd
, char *buf
, int count
)
232 int nread
, totlen
= 0;
233 while(totlen
!= count
) {
234 nread
= read(fd
,buf
,count
-totlen
);
235 if (nread
== 0) return totlen
;
236 if (nread
== -1) return -1;
243 /* Like write(2) but make sure 'count' is read before to return
244 * (unless error is encountered) */
245 int anetWrite(int fd
, char *buf
, int count
)
247 int nwritten
, totlen
= 0;
248 while(totlen
!= count
) {
249 nwritten
= write(fd
,buf
,count
-totlen
);
250 if (nwritten
== 0) return totlen
;
251 if (nwritten
== -1) return -1;
258 static int anetListen(char *err
, int s
, struct sockaddr
*sa
, socklen_t len
) {
259 if (bind(s
,sa
,len
) == -1) {
260 anetSetError(err
, "bind: %s", strerror(errno
));
264 if (listen(s
, 511) == -1) { /* the magic 511 constant is from nginx */
265 anetSetError(err
, "listen: %s", strerror(errno
));
272 int anetTcpServer(char *err
, int port
, char *bindaddr
)
275 struct sockaddr_in sa
;
277 if ((s
= anetCreateSocket(err
,AF_INET
)) == ANET_ERR
)
280 memset(&sa
,0,sizeof(sa
));
281 sa
.sin_family
= AF_INET
;
282 sa
.sin_port
= htons(port
);
283 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
284 if (bindaddr
&& inet_aton(bindaddr
, &sa
.sin_addr
) == 0) {
285 anetSetError(err
, "invalid bind address");
289 if (anetListen(err
,s
,(struct sockaddr
*)&sa
,sizeof(sa
)) == ANET_ERR
)
294 int anetUnixServer(char *err
, char *path
)
297 struct sockaddr_un sa
;
299 if ((s
= anetCreateSocket(err
,AF_LOCAL
)) == ANET_ERR
)
302 memset(&sa
,0,sizeof(sa
));
303 sa
.sun_family
= AF_LOCAL
;
304 strncpy(sa
.sun_path
,path
,sizeof(sa
.sun_path
)-1);
305 if (anetListen(err
,s
,(struct sockaddr
*)&sa
,sizeof(sa
)) == ANET_ERR
)
310 static int anetGenericAccept(char *err
, int s
, struct sockaddr
*sa
, socklen_t
*len
) {
313 fd
= accept(s
,sa
,len
);
318 anetSetError(err
, "accept: %s", strerror(errno
));
327 int anetTcpAccept(char *err
, int s
, char *ip
, int *port
) {
329 struct sockaddr_in sa
;
330 socklen_t salen
= sizeof(sa
);
331 if ((fd
= anetGenericAccept(err
,s
,(struct sockaddr
*)&sa
,&salen
)) == ANET_ERR
)
334 if (ip
) strcpy(ip
,inet_ntoa(sa
.sin_addr
));
335 if (port
) *port
= ntohs(sa
.sin_port
);
339 int anetUnixAccept(char *err
, int s
) {
341 struct sockaddr_un sa
;
342 socklen_t salen
= sizeof(sa
);
343 if ((fd
= anetGenericAccept(err
,s
,(struct sockaddr
*)&sa
,&salen
)) == ANET_ERR
)
349 int anetPeerToString(int fd
, char *ip
, int *port
) {
350 struct sockaddr_in sa
;
351 socklen_t salen
= sizeof(sa
);
353 if (getpeername(fd
,(struct sockaddr
*)&sa
,&salen
) == -1) return -1;
354 if (ip
) strcpy(ip
,inet_ntoa(sa
.sin_addr
));
355 if (port
) *port
= ntohs(sa
.sin_port
);