diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buffer.c | 2 | ||||
-rw-r--r-- | src/inet.c | 1 | ||||
-rw-r--r-- | src/luasocket.c | 4 | ||||
-rw-r--r-- | src/luasocket.h | 4 | ||||
-rw-r--r-- | src/mime.c | 4 | ||||
-rw-r--r-- | src/mime.h | 2 | ||||
-rw-r--r-- | src/mime.lua | 3 | ||||
-rw-r--r-- | src/socket.h | 4 | ||||
-rw-r--r-- | src/socket.lua | 3 | ||||
-rw-r--r-- | src/tcp.c | 22 | ||||
-rw-r--r-- | src/unix.c | 27 | ||||
-rw-r--r-- | src/unix.h | 2 | ||||
-rw-r--r-- | src/usocket.c | 26 | ||||
-rw-r--r-- | src/wsocket.c | 15 |
14 files changed, 88 insertions, 31 deletions
diff --git a/src/buffer.c b/src/buffer.c index 0ec7b4d..45cd0f2 100644 --- a/src/buffer.c +++ b/src/buffer.c | |||
@@ -123,7 +123,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) { | |||
123 | else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); | 123 | else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); |
124 | else luaL_argcheck(L, 0, 2, "invalid receive pattern"); | 124 | else luaL_argcheck(L, 0, 2, "invalid receive pattern"); |
125 | /* get a fixed number of bytes */ | 125 | /* get a fixed number of bytes */ |
126 | } else err = recvraw(buf, (size_t) lua_tonumber(L, 2), &b); | 126 | } else err = recvraw(buf, (size_t) lua_tonumber(L, 2)-size, &b); |
127 | /* check if there was an error */ | 127 | /* check if there was an error */ |
128 | if (err != IO_DONE) { | 128 | if (err != IO_DONE) { |
129 | /* we can't push anyting in the stack before pushing the | 129 | /* we can't push anyting in the stack before pushing the |
@@ -220,7 +220,6 @@ const char *inet_tryconnect(p_sock ps, const char *address, | |||
220 | } | 220 | } |
221 | } else remote.sin_family = AF_UNSPEC; | 221 | } else remote.sin_family = AF_UNSPEC; |
222 | err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); | 222 | err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); |
223 | if (err != IO_DONE) sock_destroy(ps); | ||
224 | return sock_strerror(err); | 223 | return sock_strerror(err); |
225 | } | 224 | } |
226 | 225 | ||
diff --git a/src/luasocket.c b/src/luasocket.c index 4b829f8..8f13dbc 100644 --- a/src/luasocket.c +++ b/src/luasocket.c | |||
@@ -87,7 +87,7 @@ static int global_unload(lua_State *L) { | |||
87 | static int base_open(lua_State *L) { | 87 | static int base_open(lua_State *L) { |
88 | if (sock_open()) { | 88 | if (sock_open()) { |
89 | /* export functions (and leave namespace table on top of stack) */ | 89 | /* export functions (and leave namespace table on top of stack) */ |
90 | luaL_module(L, "socket", func, 0); | 90 | luaL_openlib(L, "socket", func, 0); |
91 | #ifdef LUASOCKET_DEBUG | 91 | #ifdef LUASOCKET_DEBUG |
92 | lua_pushstring(L, "DEBUG"); | 92 | lua_pushstring(L, "DEBUG"); |
93 | lua_pushboolean(L, 1); | 93 | lua_pushboolean(L, 1); |
@@ -108,7 +108,7 @@ static int base_open(lua_State *L) { | |||
108 | /*-------------------------------------------------------------------------*\ | 108 | /*-------------------------------------------------------------------------*\ |
109 | * Initializes all library modules. | 109 | * Initializes all library modules. |
110 | \*-------------------------------------------------------------------------*/ | 110 | \*-------------------------------------------------------------------------*/ |
111 | LUASOCKET_API int luaopen_lsocket(lua_State *L) { | 111 | LUASOCKET_API int luaopen_csocket(lua_State *L) { |
112 | int i; | 112 | int i; |
113 | base_open(L); | 113 | base_open(L); |
114 | for (i = 0; mod[i].name; i++) mod[i].func(L); | 114 | for (i = 0; mod[i].name; i++) mod[i].func(L); |
diff --git a/src/luasocket.h b/src/luasocket.h index db54a18..768e335 100644 --- a/src/luasocket.h +++ b/src/luasocket.h | |||
@@ -13,7 +13,7 @@ | |||
13 | /*-------------------------------------------------------------------------*\ | 13 | /*-------------------------------------------------------------------------*\ |
14 | * Current luasocket version | 14 | * Current luasocket version |
15 | \*-------------------------------------------------------------------------*/ | 15 | \*-------------------------------------------------------------------------*/ |
16 | #define LUASOCKET_VERSION "LuaSocket 2.0 (beta3)" | 16 | #define LUASOCKET_VERSION "LuaSocket 2.0" |
17 | #define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab" | 17 | #define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab" |
18 | #define LUASOCKET_AUTHORS "Diego Nehab" | 18 | #define LUASOCKET_AUTHORS "Diego Nehab" |
19 | 19 | ||
@@ -27,6 +27,6 @@ | |||
27 | /*-------------------------------------------------------------------------*\ | 27 | /*-------------------------------------------------------------------------*\ |
28 | * Initializes the library. | 28 | * Initializes the library. |
29 | \*-------------------------------------------------------------------------*/ | 29 | \*-------------------------------------------------------------------------*/ |
30 | LUASOCKET_API int luaopen_socket(lua_State *L); | 30 | LUASOCKET_API int luaopen_csocket(lua_State *L); |
31 | 31 | ||
32 | #endif /* LUASOCKET_H */ | 32 | #endif /* LUASOCKET_H */ |
@@ -78,9 +78,9 @@ static UC b64unbase[256]; | |||
78 | /*-------------------------------------------------------------------------*\ | 78 | /*-------------------------------------------------------------------------*\ |
79 | * Initializes module | 79 | * Initializes module |
80 | \*-------------------------------------------------------------------------*/ | 80 | \*-------------------------------------------------------------------------*/ |
81 | MIME_API int luaopen_lmime(lua_State *L) | 81 | MIME_API int luaopen_cmime(lua_State *L) |
82 | { | 82 | { |
83 | luaL_module(L, "mime", func, 0); | 83 | luaL_openlib(L, "mime", func, 0); |
84 | /* initialize lookup tables */ | 84 | /* initialize lookup tables */ |
85 | qpsetup(qpclass, qpunbase); | 85 | qpsetup(qpclass, qpunbase); |
86 | b64setup(b64unbase); | 86 | b64setup(b64unbase); |
@@ -19,6 +19,6 @@ | |||
19 | #define MIME_API extern | 19 | #define MIME_API extern |
20 | #endif | 20 | #endif |
21 | 21 | ||
22 | MIME_API int luaopen_mime(lua_State *L); | 22 | MIME_API int luaopen_cmime(lua_State *L); |
23 | 23 | ||
24 | #endif /* MIME_H */ | 24 | #endif /* MIME_H */ |
diff --git a/src/mime.lua b/src/mime.lua index 4d5bdba..6492a96 100644 --- a/src/mime.lua +++ b/src/mime.lua | |||
@@ -8,9 +8,10 @@ | |||
8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
9 | -- Declare module and import dependencies | 9 | -- Declare module and import dependencies |
10 | ----------------------------------------------------------------------------- | 10 | ----------------------------------------------------------------------------- |
11 | package.loaded.base = _G | ||
11 | local base = require("base") | 12 | local base = require("base") |
12 | local ltn12 = require("ltn12") | 13 | local ltn12 = require("ltn12") |
13 | local mime = require("lmime") | 14 | local mime = require("cmime") |
14 | module("mime") | 15 | module("mime") |
15 | 16 | ||
16 | -- encode, decode and wrap algorithm tables | 17 | -- encode, decode and wrap algorithm tables |
diff --git a/src/socket.h b/src/socket.h index 368c2b6..639229d 100644 --- a/src/socket.h +++ b/src/socket.h | |||
@@ -45,11 +45,15 @@ int sock_sendto(p_sock ps, const char *data, size_t count, | |||
45 | size_t *sent, SA *addr, socklen_t addr_len, p_tm tm); | 45 | size_t *sent, SA *addr, socklen_t addr_len, p_tm tm); |
46 | int sock_recvfrom(p_sock ps, char *data, size_t count, | 46 | int sock_recvfrom(p_sock ps, char *data, size_t count, |
47 | size_t *got, SA *addr, socklen_t *addr_len, p_tm tm); | 47 | size_t *got, SA *addr, socklen_t *addr_len, p_tm tm); |
48 | |||
48 | void sock_setnonblocking(p_sock ps); | 49 | void sock_setnonblocking(p_sock ps); |
49 | void sock_setblocking(p_sock ps); | 50 | void sock_setblocking(p_sock ps); |
51 | |||
52 | int sock_waitfd(int fd, int sw, p_tm tm); | ||
50 | int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm); | 53 | int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm); |
51 | 54 | ||
52 | int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); | 55 | int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); |
56 | int sock_connected(p_sock ps, p_tm tm); | ||
53 | int sock_create(p_sock ps, int domain, int type, int protocol); | 57 | int sock_create(p_sock ps, int domain, int type, int protocol); |
54 | int sock_bind(p_sock ps, SA *addr, socklen_t addr_len); | 58 | int sock_bind(p_sock ps, SA *addr, socklen_t addr_len); |
55 | int sock_listen(p_sock ps, int backlog); | 59 | int sock_listen(p_sock ps, int backlog); |
diff --git a/src/socket.lua b/src/socket.lua index 1c82750..f3563e7 100644 --- a/src/socket.lua +++ b/src/socket.lua | |||
@@ -7,10 +7,11 @@ | |||
7 | ----------------------------------------------------------------------------- | 7 | ----------------------------------------------------------------------------- |
8 | -- Declare module and import dependencies | 8 | -- Declare module and import dependencies |
9 | ----------------------------------------------------------------------------- | 9 | ----------------------------------------------------------------------------- |
10 | package.loaded.base = _G | ||
10 | local base = require("base") | 11 | local base = require("base") |
11 | local string = require("string") | 12 | local string = require("string") |
12 | local math = require("math") | 13 | local math = require("math") |
13 | local socket = require("lsocket") | 14 | local socket = require("csocket") |
14 | module("socket") | 15 | module("socket") |
15 | 16 | ||
16 | ----------------------------------------------------------------------------- | 17 | ----------------------------------------------------------------------------- |
@@ -20,6 +20,7 @@ | |||
20 | \*=========================================================================*/ | 20 | \*=========================================================================*/ |
21 | static int global_create(lua_State *L); | 21 | static int global_create(lua_State *L); |
22 | static int meth_connect(lua_State *L); | 22 | static int meth_connect(lua_State *L); |
23 | static int meth_connected(lua_State *L); | ||
23 | static int meth_listen(lua_State *L); | 24 | static int meth_listen(lua_State *L); |
24 | static int meth_bind(lua_State *L); | 25 | static int meth_bind(lua_State *L); |
25 | static int meth_send(lua_State *L); | 26 | static int meth_send(lua_State *L); |
@@ -45,6 +46,7 @@ static luaL_reg tcp[] = { | |||
45 | {"bind", meth_bind}, | 46 | {"bind", meth_bind}, |
46 | {"close", meth_close}, | 47 | {"close", meth_close}, |
47 | {"connect", meth_connect}, | 48 | {"connect", meth_connect}, |
49 | {"connected", meth_connected}, | ||
48 | {"dirty", meth_dirty}, | 50 | {"dirty", meth_dirty}, |
49 | {"getfd", meth_getfd}, | 51 | {"getfd", meth_getfd}, |
50 | {"getpeername", meth_getpeername}, | 52 | {"getpeername", meth_getpeername}, |
@@ -113,12 +115,12 @@ static int meth_receive(lua_State *L) { | |||
113 | } | 115 | } |
114 | 116 | ||
115 | static int meth_getstats(lua_State *L) { | 117 | static int meth_getstats(lua_State *L) { |
116 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 118 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); |
117 | return buf_meth_getstats(L, &tcp->buf); | 119 | return buf_meth_getstats(L, &tcp->buf); |
118 | } | 120 | } |
119 | 121 | ||
120 | static int meth_setstats(lua_State *L) { | 122 | static int meth_setstats(lua_State *L) { |
121 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 123 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); |
122 | return buf_meth_setstats(L, &tcp->buf); | 124 | return buf_meth_setstats(L, &tcp->buf); |
123 | } | 125 | } |
124 | 126 | ||
@@ -224,6 +226,22 @@ static int meth_connect(lua_State *L) | |||
224 | return 1; | 226 | return 1; |
225 | } | 227 | } |
226 | 228 | ||
229 | static int meth_connected(lua_State *L) | ||
230 | { | ||
231 | static t_tm tm = {-1, -1}; | ||
232 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | ||
233 | int err = sock_connected(&tcp->sock, &tm); | ||
234 | if (err != IO_DONE) { | ||
235 | lua_pushnil(L); | ||
236 | lua_pushstring(L, sock_strerror(err)); | ||
237 | return 2; | ||
238 | } | ||
239 | /* turn master object into a client object */ | ||
240 | aux_setclass(L, "tcp{client}", 1); | ||
241 | lua_pushnumber(L, 1); | ||
242 | return 1; | ||
243 | } | ||
244 | |||
227 | /*-------------------------------------------------------------------------*\ | 245 | /*-------------------------------------------------------------------------*\ |
228 | * Closes socket used by object | 246 | * Closes socket used by object |
229 | \*-------------------------------------------------------------------------*/ | 247 | \*-------------------------------------------------------------------------*/ |
@@ -32,6 +32,8 @@ static int meth_settimeout(lua_State *L); | |||
32 | static int meth_getfd(lua_State *L); | 32 | static int meth_getfd(lua_State *L); |
33 | static int meth_setfd(lua_State *L); | 33 | static int meth_setfd(lua_State *L); |
34 | static int meth_dirty(lua_State *L); | 34 | static int meth_dirty(lua_State *L); |
35 | static int meth_getstats(lua_State *L); | ||
36 | static int meth_setstats(lua_State *L); | ||
35 | 37 | ||
36 | static const char *unix_tryconnect(p_unix un, const char *path); | 38 | static const char *unix_tryconnect(p_unix un, const char *path); |
37 | static const char *unix_trybind(p_unix un, const char *path); | 39 | static const char *unix_trybind(p_unix un, const char *path); |
@@ -46,6 +48,8 @@ static luaL_reg un[] = { | |||
46 | {"connect", meth_connect}, | 48 | {"connect", meth_connect}, |
47 | {"dirty", meth_dirty}, | 49 | {"dirty", meth_dirty}, |
48 | {"getfd", meth_getfd}, | 50 | {"getfd", meth_getfd}, |
51 | {"getstats", meth_getstats}, | ||
52 | {"setstats", meth_setstats}, | ||
49 | {"listen", meth_listen}, | 53 | {"listen", meth_listen}, |
50 | {"receive", meth_receive}, | 54 | {"receive", meth_receive}, |
51 | {"send", meth_send}, | 55 | {"send", meth_send}, |
@@ -75,7 +79,7 @@ static luaL_reg func[] = { | |||
75 | /*-------------------------------------------------------------------------*\ | 79 | /*-------------------------------------------------------------------------*\ |
76 | * Initializes module | 80 | * Initializes module |
77 | \*-------------------------------------------------------------------------*/ | 81 | \*-------------------------------------------------------------------------*/ |
78 | int unix_open(lua_State *L) { | 82 | int luaopen_socketunix(lua_State *L) { |
79 | /* create classes */ | 83 | /* create classes */ |
80 | aux_newclass(L, "unix{master}", un); | 84 | aux_newclass(L, "unix{master}", un); |
81 | aux_newclass(L, "unix{client}", un); | 85 | aux_newclass(L, "unix{client}", un); |
@@ -84,11 +88,9 @@ int unix_open(lua_State *L) { | |||
84 | aux_add2group(L, "unix{master}", "unix{any}"); | 88 | aux_add2group(L, "unix{master}", "unix{any}"); |
85 | aux_add2group(L, "unix{client}", "unix{any}"); | 89 | aux_add2group(L, "unix{client}", "unix{any}"); |
86 | aux_add2group(L, "unix{server}", "unix{any}"); | 90 | aux_add2group(L, "unix{server}", "unix{any}"); |
87 | aux_add2group(L, "unix{client}", "unix{client,server}"); | ||
88 | aux_add2group(L, "unix{server}", "unix{client,server}"); | ||
89 | /* define library functions */ | 91 | /* define library functions */ |
90 | luaL_openlib(L, NULL, func, 0); | 92 | luaL_openlib(L, "socket", func, 0); |
91 | return 0; | 93 | return 1; |
92 | } | 94 | } |
93 | 95 | ||
94 | /*=========================================================================*\ | 96 | /*=========================================================================*\ |
@@ -107,6 +109,16 @@ static int meth_receive(lua_State *L) { | |||
107 | return buf_meth_receive(L, &un->buf); | 109 | return buf_meth_receive(L, &un->buf); |
108 | } | 110 | } |
109 | 111 | ||
112 | static int meth_getstats(lua_State *L) { | ||
113 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | ||
114 | return buf_meth_getstats(L, &un->buf); | ||
115 | } | ||
116 | |||
117 | static int meth_setstats(lua_State *L) { | ||
118 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | ||
119 | return buf_meth_setstats(L, &un->buf); | ||
120 | } | ||
121 | |||
110 | /*-------------------------------------------------------------------------*\ | 122 | /*-------------------------------------------------------------------------*\ |
111 | * Just call option handler | 123 | * Just call option handler |
112 | \*-------------------------------------------------------------------------*/ | 124 | \*-------------------------------------------------------------------------*/ |
@@ -250,7 +262,8 @@ static int meth_close(lua_State *L) | |||
250 | { | 262 | { |
251 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 263 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); |
252 | sock_destroy(&un->sock); | 264 | sock_destroy(&un->sock); |
253 | return 0; | 265 | lua_pushnumber(L, 1); |
266 | return 1; | ||
254 | } | 267 | } |
255 | 268 | ||
256 | /*-------------------------------------------------------------------------*\ | 269 | /*-------------------------------------------------------------------------*\ |
@@ -277,7 +290,7 @@ static int meth_listen(lua_State *L) | |||
277 | \*-------------------------------------------------------------------------*/ | 290 | \*-------------------------------------------------------------------------*/ |
278 | static int meth_shutdown(lua_State *L) | 291 | static int meth_shutdown(lua_State *L) |
279 | { | 292 | { |
280 | p_unix un = (p_unix) aux_checkgroup(L, "unix{client}", 1); | 293 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); |
281 | const char *how = luaL_optstring(L, 2, "both"); | 294 | const char *how = luaL_optstring(L, 2, "both"); |
282 | switch (how[0]) { | 295 | switch (how[0]) { |
283 | case 'b': | 296 | case 'b': |
@@ -23,6 +23,6 @@ typedef struct t_unix_ { | |||
23 | } t_unix; | 23 | } t_unix; |
24 | typedef t_unix *p_unix; | 24 | typedef t_unix *p_unix; |
25 | 25 | ||
26 | int unix_open(lua_State *L); | 26 | int luaopen_socketunix(lua_State *L); |
27 | 27 | ||
28 | #endif /* UNIX_H */ | 28 | #endif /* UNIX_H */ |
diff --git a/src/usocket.c b/src/usocket.c index c1ab725..3428a0c 100644 --- a/src/usocket.c +++ b/src/usocket.c | |||
@@ -22,7 +22,7 @@ | |||
22 | #define WAITFD_R POLLIN | 22 | #define WAITFD_R POLLIN |
23 | #define WAITFD_W POLLOUT | 23 | #define WAITFD_W POLLOUT |
24 | #define WAITFD_C (POLLIN|POLLOUT) | 24 | #define WAITFD_C (POLLIN|POLLOUT) |
25 | static int sock_waitfd(int fd, int sw, p_tm tm) { | 25 | int sock_waitfd(int fd, int sw, p_tm tm) { |
26 | int ret; | 26 | int ret; |
27 | struct pollfd pfd; | 27 | struct pollfd pfd; |
28 | pfd.fd = fd; | 28 | pfd.fd = fd; |
@@ -44,7 +44,7 @@ static int sock_waitfd(int fd, int sw, p_tm tm) { | |||
44 | #define WAITFD_W 2 | 44 | #define WAITFD_W 2 |
45 | #define WAITFD_C (WAITFD_R|WAITFD_W) | 45 | #define WAITFD_C (WAITFD_R|WAITFD_W) |
46 | 46 | ||
47 | static int sock_waitfd(int fd, int sw, p_tm tm) { | 47 | int sock_waitfd(int fd, int sw, p_tm tm) { |
48 | int ret; | 48 | int ret; |
49 | fd_set rfds, wfds, *rp, *wp; | 49 | fd_set rfds, wfds, *rp, *wp; |
50 | struct timeval tv, *tp; | 50 | struct timeval tv, *tp; |
@@ -166,12 +166,20 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { | |||
166 | while ((err = errno) == EINTR); | 166 | while ((err = errno) == EINTR); |
167 | /* if connection failed immediately, return error code */ | 167 | /* if connection failed immediately, return error code */ |
168 | if (err != EINPROGRESS && err != EAGAIN) return err; | 168 | if (err != EINPROGRESS && err != EAGAIN) return err; |
169 | /* zero timeout case optimization */ | ||
170 | if (tm_iszero(tm)) return IO_TIMEOUT; | ||
169 | /* wait until we have the result of the connection attempt or timeout */ | 171 | /* wait until we have the result of the connection attempt or timeout */ |
170 | if ((err = sock_waitfd(*ps, WAITFD_C, tm)) == IO_CLOSED) { | 172 | return sock_connected(ps, tm); |
171 | /* finaly find out if we succeeded connecting */ | 173 | } |
174 | |||
175 | /*-------------------------------------------------------------------------*\ | ||
176 | * Checks if socket is connected, or return reason for failure | ||
177 | \*-------------------------------------------------------------------------*/ | ||
178 | int sock_connected(p_sock ps, p_tm tm) { | ||
179 | int err; | ||
180 | if ((err = sock_waitfd(*ps, WAITFD_C, tm) == IO_CLOSED)) { | ||
172 | if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; | 181 | if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; |
173 | else return errno; | 182 | else return errno; |
174 | /* timed out or some weirder error */ | ||
175 | } else return err; | 183 | } else return err; |
176 | } | 184 | } |
177 | 185 | ||
@@ -321,13 +329,17 @@ void sock_setnonblocking(p_sock ps) { | |||
321 | int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { | 329 | int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { |
322 | *hp = gethostbyaddr(addr, len, AF_INET); | 330 | *hp = gethostbyaddr(addr, len, AF_INET); |
323 | if (*hp) return IO_DONE; | 331 | if (*hp) return IO_DONE; |
324 | else return h_errno; | 332 | else if (h_errno) return h_errno; |
333 | else if (errno) return errno; | ||
334 | else return IO_UNKNOWN; | ||
325 | } | 335 | } |
326 | 336 | ||
327 | int sock_gethostbyname(const char *addr, struct hostent **hp) { | 337 | int sock_gethostbyname(const char *addr, struct hostent **hp) { |
328 | *hp = gethostbyname(addr); | 338 | *hp = gethostbyname(addr); |
329 | if (*hp) return IO_DONE; | 339 | if (*hp) return IO_DONE; |
330 | else return h_errno; | 340 | else if (h_errno) return h_errno; |
341 | else if (errno) return errno; | ||
342 | else return IO_UNKNOWN; | ||
331 | } | 343 | } |
332 | 344 | ||
333 | /*-------------------------------------------------------------------------*\ | 345 | /*-------------------------------------------------------------------------*\ |
diff --git a/src/wsocket.c b/src/wsocket.c index 69fac4d..c0686cd 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
@@ -45,7 +45,7 @@ int sock_close(void) { | |||
45 | #define WAITFD_E 4 | 45 | #define WAITFD_E 4 |
46 | #define WAITFD_C (WAITFD_E|WAITFD_W) | 46 | #define WAITFD_C (WAITFD_E|WAITFD_W) |
47 | 47 | ||
48 | static int sock_waitfd(t_sock fd, int sw, p_tm tm) { | 48 | int sock_waitfd(t_sock fd, int sw, p_tm tm) { |
49 | int ret; | 49 | int ret; |
50 | fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; | 50 | fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; |
51 | struct timeval tv, *tp = NULL; | 51 | struct timeval tv, *tp = NULL; |
@@ -118,7 +118,17 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { | |||
118 | /* make sure the system is trying to connect */ | 118 | /* make sure the system is trying to connect */ |
119 | err = WSAGetLastError(); | 119 | err = WSAGetLastError(); |
120 | if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err; | 120 | if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err; |
121 | /* zero timeout case optimization */ | ||
122 | if (tm_iszero(tm)) return IO_TIMEOUT; | ||
121 | /* we wait until something happens */ | 123 | /* we wait until something happens */ |
124 | return sock_connected(ps, tm); | ||
125 | } | ||
126 | |||
127 | /*-------------------------------------------------------------------------*\ | ||
128 | * Check if socket is connected | ||
129 | \*-------------------------------------------------------------------------*/ | ||
130 | int sock_connected(p_sock ps) { | ||
131 | int err; | ||
122 | if ((err = sock_waitfd(*ps, WAITFD_C, tm)) == IO_CLOSED) { | 132 | if ((err = sock_waitfd(*ps, WAITFD_C, tm)) == IO_CLOSED) { |
123 | int len = sizeof(err); | 133 | int len = sizeof(err); |
124 | /* give windows time to set the error (yes, disgusting) */ | 134 | /* give windows time to set the error (yes, disgusting) */ |
@@ -126,9 +136,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { | |||
126 | /* find out why we failed */ | 136 | /* find out why we failed */ |
127 | getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); | 137 | getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); |
128 | /* we KNOW there was an error. if why is 0, we will return | 138 | /* we KNOW there was an error. if why is 0, we will return |
129 | * "unknown error", but it's not really our fault */ | 139 | * "unknown error", but it's not really our fault */ |
130 | return err > 0? err: IO_UNKNOWN; | 140 | return err > 0? err: IO_UNKNOWN; |
131 | /* here we deal with the case in which it worked, timedout or weird errors */ | ||
132 | } else return err; | 141 | } else return err; |
133 | } | 142 | } |
134 | 143 | ||