aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/timeout.c1
-rw-r--r--src/wsocket.c42
2 files changed, 34 insertions, 9 deletions
diff --git a/src/timeout.c b/src/timeout.c
index df199a0..09cb53d 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -21,6 +21,7 @@
21#include <sys/times.h> 21#include <sys/times.h>
22#include <unistd.h> 22#include <unistd.h>
23#ifndef CLK_TCK 23#ifndef CLK_TCK
24/* CLI_TCK is now obsolete in Linux */
24#define CLK_TCK (sysconf(_SC_CLK_TCK)); 25#define CLK_TCK (sysconf(_SC_CLK_TCK));
25#endif 26#endif
26#endif 27#endif
diff --git a/src/wsocket.c b/src/wsocket.c
index bf92a90..c0e28d9 100644
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -53,33 +53,57 @@ void sock_shutdown(p_sock ps, int how)
53/*-------------------------------------------------------------------------*\ 53/*-------------------------------------------------------------------------*\
54* Creates and sets up a socket 54* Creates and sets up a socket
55\*-------------------------------------------------------------------------*/ 55\*-------------------------------------------------------------------------*/
56const char *sock_create(p_sock ps, int domain, int type, int protocol) 56int sock_create(p_sock ps, int domain, int type, int protocol)
57{ 57{
58 int val = 1; 58 int val = 1;
59 t_sock sock = socket(domain, type, protocol); 59 t_sock sock = socket(domain, type, protocol);
60 if (sock == SOCK_INVALID) return sock_createstrerror(); 60 if (sock == SOCK_INVALID) return IO_ERROR;
61 *ps = sock; 61 *ps = sock;
62 sock_setnonblocking(ps); 62 sock_setnonblocking(ps);
63 setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val)); 63 setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
64 return NULL; 64 return IO_DONE;
65} 65}
66 66
67/*-------------------------------------------------------------------------*\ 67/*-------------------------------------------------------------------------*\
68* Connects or returns error message 68* Connects or returns error message
69\*-------------------------------------------------------------------------*/ 69\*-------------------------------------------------------------------------*/
70const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len) 70int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout)
71{ 71{
72 if (connect(*ps, addr, addr_len) < 0) return sock_connectstrerror(); 72 t_sock sock = *ps;
73 else return NULL; 73 if (sock == SOCK_INVALID) return IO_CLOSED;
74 /* if connect fails, we have to find out why */
75 if (connect(sock, addr, addr_len) < 0) {
76 int err;
77 struct timeval tv;
78 fd_set efds, wfds;
79 /* make sure the system is trying to connect */
80 if (WSAGetLastError() != WSAEWOULDBLOCK) return IO_ERROR;
81 tv.tv_sec = timeout / 1000;
82 tv.tv_usec = (timeout % 1000) * 1000;
83 FD_ZERO(&wfds); FD_SET(sock, &wfds);
84 FD_ZERO(&efds); FD_SET(sock, &efds);
85 /* we run select to avoid busy waiting */
86 err = select(0, NULL, &wfds, &efds, timeout >= 0? &tv: NULL);
87 /* if select returned due to an event */
88 if (err > 0 ) {
89 /* the sets tell whether it was a sucess or failure */
90 if (FD_ISSET(sock,&efds) || !FD_ISSET(sock,&wfds)) return IO_ERROR;
91 else return IO_DONE;
92 /* if nothing happened, we timed out */
93 } else if (err == 0) return IO_TIMEOUT;
94 /* otherwise, I don't know what happened */
95 else return IO_ERROR;
96 /* otherwise, it worked */
97 } else return IO_DONE;
74} 98}
75 99
76/*-------------------------------------------------------------------------*\ 100/*-------------------------------------------------------------------------*\
77* Binds or returns error message 101* Binds or returns error message
78\*-------------------------------------------------------------------------*/ 102\*-------------------------------------------------------------------------*/
79const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) 103int sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
80{ 104{
81 if (bind(*ps, addr, addr_len) < 0) return sock_bindstrerror(); 105 if (bind(*ps, addr, addr_len) < 0) return IO_ERROR;
82 else return NULL; 106 else return IO_DONE;
83} 107}
84 108
85/*-------------------------------------------------------------------------*\ 109/*-------------------------------------------------------------------------*\