100 |
/* -------------------------------------------------------------- Prototypes */ |
/* -------------------------------------------------------------- Prototypes */ |
101 |
|
|
102 |
|
|
103 |
static int do_connect(int s, const struct sockaddr *addr, socklen_t addrlen); |
static int do_connect(int s, const struct sockaddr *addr, |
104 |
|
socklen_t addrlen, int timeout); |
105 |
|
|
106 |
|
|
107 |
/* ------------------------------------------------------------------ Public */ |
/* ------------------------------------------------------------------ Public */ |
123 |
|
|
124 |
ASSERT(hostname); |
ASSERT(hostname); |
125 |
|
|
126 |
if((socket= create_socket(hostname, port, protocol)) < 0) { |
if((socket= create_socket(hostname, port, protocol, 3)) < 0) { |
127 |
rv= FALSE; |
rv= FALSE; |
128 |
} else if(! check_socket(socket)) { |
} else if(! check_socket(socket)) { |
129 |
rv= FALSE; |
rv= FALSE; |
210 |
* @param hostname The host to open a socket at |
* @param hostname The host to open a socket at |
211 |
* @param port The port number to connect to |
* @param port The port number to connect to |
212 |
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM) |
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM) |
213 |
|
* @param timeout If not connected within timeout seconds abort and return -1 |
214 |
* @return The socket or -1 if an error occured. |
* @return The socket or -1 if an error occured. |
215 |
*/ |
*/ |
216 |
int create_socket(char *hostname, int port, int protocol) { |
int create_socket(char *hostname, int port, int protocol, int timeout) { |
217 |
|
|
218 |
int s; |
int s; |
219 |
struct hostent *hp; |
struct hostent *hp; |
237 |
goto error; |
goto error; |
238 |
} |
} |
239 |
|
|
240 |
if(do_connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { |
if(do_connect(s, (struct sockaddr *)&sin, sizeof(sin), timeout) < 0) { |
241 |
goto error; |
goto error; |
242 |
} |
} |
243 |
|
|
264 |
|
|
265 |
switch(p->family) { |
switch(p->family) { |
266 |
case AF_UNIX: |
case AF_UNIX: |
267 |
socket_fd= create_unix_socket(p->pathname); |
socket_fd= create_unix_socket(p->pathname, p->timeout); |
268 |
break; |
break; |
269 |
case AF_INET: |
case AF_INET: |
270 |
socket_fd= create_socket(p->hostname, p->port, p->type); |
socket_fd= create_socket(p->hostname, p->port, p->type, p->timeout); |
271 |
break; |
break; |
272 |
default: |
default: |
273 |
socket_fd= -1; |
socket_fd= -1; |
281 |
/** |
/** |
282 |
* Create a non-blocking UNIX socket. |
* Create a non-blocking UNIX socket. |
283 |
* @param pathname The pathname to use for the unix socket |
* @param pathname The pathname to use for the unix socket |
284 |
|
* @param timeout If not connected within timeout seconds abort and return -1 |
285 |
* @return The socket or -1 if an error occured. |
* @return The socket or -1 if an error occured. |
286 |
*/ |
*/ |
287 |
int create_unix_socket(char *pathname) { |
int create_unix_socket(char *pathname, int timeout) { |
288 |
|
|
289 |
int s; |
int s; |
290 |
struct sockaddr_un unixsocket; |
struct sockaddr_un unixsocket; |
302 |
goto error; |
goto error; |
303 |
} |
} |
304 |
|
|
305 |
if(do_connect(s, (struct sockaddr *)&unixsocket, sizeof(unixsocket)) < 0) { |
if(do_connect(s, (struct sockaddr *)&unixsocket, sizeof(unixsocket), |
306 |
|
timeout) < 0) { |
307 |
goto error; |
goto error; |
308 |
} |
} |
309 |
|
|
558 |
|
|
559 |
|
|
560 |
/* |
/* |
561 |
* Do a non blocking connect, timeout if not connected within 5 seconds |
* Do a non blocking connect, timeout if not connected within timeout seconds |
562 |
*/ |
*/ |
563 |
static int do_connect(int s, const struct sockaddr *addr, socklen_t addrlen) { |
static int do_connect(int s, const struct sockaddr *addr, |
564 |
|
socklen_t addrlen, int timeout) { |
565 |
|
|
566 |
int n, error; |
int n, error; |
567 |
fd_set wset, rset; |
fd_set wset, rset; |
577 |
FD_ZERO(&rset); |
FD_ZERO(&rset); |
578 |
FD_SET(s, &rset); |
FD_SET(s, &rset); |
579 |
wset= rset; |
wset= rset; |
580 |
tv.tv_sec= 5; |
tv.tv_sec= timeout; |
581 |
tv.tv_usec= 0; |
tv.tv_usec= 0; |
582 |
|
|
583 |
if(select(s+1, NULL, &rset, &wset, &tv)==0) { |
if(select(s+1, NULL, &rset, &wset, &tv)==0) { |
594 |
return -1; |
return -1; |
595 |
|
|
596 |
if(error) { |
if(error) { |
|
close(s); |
|
597 |
errno= error; |
errno= error; |
598 |
return -1; |
return -1; |
599 |
} |
} |