]>
git.saurik.com Git - redis.git/blob - deps/hiredis/net.c
88171461e2135f22c5d181f54fca44740c35aebd
1 /* Extracted from anet.c to work properly with Hiredis error reporting.
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4 * Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
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>
50 /* Forward declaration */
51 void __redisSetError(redisContext
*c
, int type
, sds err
);
53 static int redisCreateSocket(redisContext
*c
, int type
) {
55 if ((s
= socket(type
, SOCK_STREAM
, 0)) == -1) {
56 __redisSetError(c
,REDIS_ERR_IO
,NULL
);
59 if (type
== AF_INET
) {
60 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) == -1) {
61 __redisSetError(c
,REDIS_ERR_IO
,NULL
);
69 static int redisSetNonBlock(redisContext
*c
, int fd
) {
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
)));
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
)));
90 static int redisSetTcpNoDelay(redisContext
*c
, int fd
) {
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
)));
100 int redisContextConnectTcp(redisContext
*c
, const char *addr
, int port
) {
102 int blocking
= (c
->flags
& REDIS_BLOCK
);
103 struct sockaddr_in sa
;
105 if ((s
= redisCreateSocket(c
,AF_INET
)) == REDIS_ERR
)
107 if (!blocking
&& redisSetNonBlock(c
,s
) == REDIS_ERR
)
110 sa
.sin_family
= AF_INET
;
111 sa
.sin_port
= htons(port
);
112 if (inet_aton(addr
, &sa
.sin_addr
) == 0) {
115 he
= gethostbyname(addr
);
117 __redisSetError(c
,REDIS_ERR_OTHER
,
118 sdscatprintf(sdsempty(),"Can't resolve: %s",addr
));
122 memcpy(&sa
.sin_addr
, he
->h_addr
, sizeof(struct in_addr
));
125 if (connect(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1) {
126 if (errno
== EINPROGRESS
&& !blocking
) {
129 __redisSetError(c
,REDIS_ERR_IO
,NULL
);
135 if (redisSetTcpNoDelay(c
,s
) != REDIS_OK
) {
141 c
->flags
|= REDIS_CONNECTED
;
145 int redisContextConnectUnix(redisContext
*c
, const char *path
) {
147 int blocking
= (c
->flags
& REDIS_BLOCK
);
148 struct sockaddr_un sa
;
150 if ((s
= redisCreateSocket(c
,AF_LOCAL
)) == REDIS_ERR
)
152 if (!blocking
&& redisSetNonBlock(c
,s
) != REDIS_OK
)
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
) {
161 __redisSetError(c
,REDIS_ERR_IO
,NULL
);
168 c
->flags
|= REDIS_CONNECTED
;