]>
git.saurik.com Git - redis.git/blob - deps/hiredis/net.c
599ba9d6b951b6ddcc6a234002746663a6e579c0
1 /* Extracted from anet.c to work properly with Hiredis error reporting.
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.
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
37 #include <arpa/inet.h>
49 /* Forward declaration */
50 void __redisSetError(redisContext
*c
, int type
, sds err
);
52 static int redisCreateSocket(redisContext
*c
, int type
) {
54 if ((s
= socket(type
, SOCK_STREAM
, 0)) == -1) {
55 __redisSetError(c
,REDIS_ERR_IO
,NULL
);
58 if (type
== AF_INET
) {
59 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) == -1) {
60 __redisSetError(c
,REDIS_ERR_IO
,NULL
);
68 static int redisSetNonBlock(redisContext
*c
, int fd
) {
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
)));
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
)));
89 static int redisSetTcpNoDelay(redisContext
*c
, int fd
) {
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
)));
99 int redisContextConnectTcp(redisContext
*c
, const char *addr
, int port
) {
101 int blocking
= (c
->flags
& REDIS_BLOCK
);
102 struct sockaddr_in sa
;
104 if ((s
= redisCreateSocket(c
,AF_INET
)) == REDIS_ERR
)
106 if (!blocking
&& redisSetNonBlock(c
,s
) == REDIS_ERR
)
109 sa
.sin_family
= AF_INET
;
110 sa
.sin_port
= htons(port
);
111 if (inet_aton(addr
, &sa
.sin_addr
) == 0) {
114 he
= gethostbyname(addr
);
116 __redisSetError(c
,REDIS_ERR_OTHER
,
117 sdscatprintf(sdsempty(),"can't resolve: %s",addr
));
121 memcpy(&sa
.sin_addr
, he
->h_addr
, sizeof(struct in_addr
));
124 if (connect(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1) {
125 if (errno
== EINPROGRESS
&& !blocking
) {
128 __redisSetError(c
,REDIS_ERR_IO
,NULL
);
134 if (redisSetTcpNoDelay(c
,s
) != REDIS_OK
) {
143 int redisContextConnectUnix(redisContext
*c
, const char *path
) {
145 int blocking
= (c
->flags
& REDIS_BLOCK
);
146 struct sockaddr_un sa
;
148 if ((s
= redisCreateSocket(c
,AF_LOCAL
)) == REDIS_ERR
)
150 if (!blocking
&& redisSetNonBlock(c
,s
) != REDIS_OK
)
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
) {
159 __redisSetError(c
,REDIS_ERR_IO
,NULL
);