diff options
-rw-r--r-- | src/auxiliar.c | 12 | ||||
-rw-r--r-- | src/auxiliar.h | 11 | ||||
-rw-r--r-- | src/buffer.c | 1 | ||||
-rw-r--r-- | src/compat.c | 19 | ||||
-rw-r--r-- | src/compat.h | 11 | ||||
-rw-r--r-- | src/except.c | 5 | ||||
-rw-r--r-- | src/inet.c | 5 | ||||
-rw-r--r-- | src/io.c | 2 | ||||
-rw-r--r-- | src/luasocket.c | 5 | ||||
-rw-r--r-- | src/makefile | 5 | ||||
-rw-r--r-- | src/mime.c | 9 | ||||
-rw-r--r-- | src/options.c | 46 | ||||
-rw-r--r-- | src/select.c | 29 | ||||
-rw-r--r-- | src/serial.c | 14 | ||||
-rw-r--r-- | src/tcp.c | 5 | ||||
-rw-r--r-- | src/timeout.c | 13 | ||||
-rw-r--r-- | src/udp.c | 5 | ||||
-rw-r--r-- | src/unix.c | 56 | ||||
-rw-r--r-- | src/wsocket.c | 82 | ||||
-rw-r--r-- | test/testclnt.lua | 2 |
20 files changed, 164 insertions, 173 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c index de625e9..18fa8e4 100644 --- a/src/auxiliar.c +++ b/src/auxiliar.c | |||
@@ -26,7 +26,7 @@ void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) { | |||
26 | luaL_newmetatable(L, classname); /* mt */ | 26 | luaL_newmetatable(L, classname); /* mt */ |
27 | /* create __index table to place methods */ | 27 | /* create __index table to place methods */ |
28 | lua_pushstring(L, "__index"); /* mt,"__index" */ | 28 | lua_pushstring(L, "__index"); /* mt,"__index" */ |
29 | lua_newtable(L); /* mt,"__index",it */ | 29 | lua_newtable(L); /* mt,"__index",it */ |
30 | /* put class name into class metatable */ | 30 | /* put class name into class metatable */ |
31 | lua_pushstring(L, "class"); /* mt,"__index",it,"class" */ | 31 | lua_pushstring(L, "class"); /* mt,"__index",it,"class" */ |
32 | lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */ | 32 | lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */ |
@@ -84,7 +84,7 @@ int auxiliar_checkboolean(lua_State *L, int objidx) { | |||
84 | } | 84 | } |
85 | 85 | ||
86 | /*-------------------------------------------------------------------------*\ | 86 | /*-------------------------------------------------------------------------*\ |
87 | * Return userdata pointer if object belongs to a given class, abort with | 87 | * Return userdata pointer if object belongs to a given class, abort with |
88 | * error otherwise | 88 | * error otherwise |
89 | \*-------------------------------------------------------------------------*/ | 89 | \*-------------------------------------------------------------------------*/ |
90 | void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { | 90 | void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { |
@@ -98,7 +98,7 @@ void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { | |||
98 | } | 98 | } |
99 | 99 | ||
100 | /*-------------------------------------------------------------------------*\ | 100 | /*-------------------------------------------------------------------------*\ |
101 | * Return userdata pointer if object belongs to a given group, abort with | 101 | * Return userdata pointer if object belongs to a given group, abort with |
102 | * error otherwise | 102 | * error otherwise |
103 | \*-------------------------------------------------------------------------*/ | 103 | \*-------------------------------------------------------------------------*/ |
104 | void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) { | 104 | void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) { |
@@ -121,7 +121,7 @@ void auxiliar_setclass(lua_State *L, const char *classname, int objidx) { | |||
121 | } | 121 | } |
122 | 122 | ||
123 | /*-------------------------------------------------------------------------*\ | 123 | /*-------------------------------------------------------------------------*\ |
124 | * Get a userdata pointer if object belongs to a given group. Return NULL | 124 | * Get a userdata pointer if object belongs to a given group. Return NULL |
125 | * otherwise | 125 | * otherwise |
126 | \*-------------------------------------------------------------------------*/ | 126 | \*-------------------------------------------------------------------------*/ |
127 | void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { | 127 | void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { |
@@ -139,7 +139,7 @@ void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { | |||
139 | } | 139 | } |
140 | 140 | ||
141 | /*-------------------------------------------------------------------------*\ | 141 | /*-------------------------------------------------------------------------*\ |
142 | * Get a userdata pointer if object belongs to a given class. Return NULL | 142 | * Get a userdata pointer if object belongs to a given class. Return NULL |
143 | * otherwise | 143 | * otherwise |
144 | \*-------------------------------------------------------------------------*/ | 144 | \*-------------------------------------------------------------------------*/ |
145 | void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { | 145 | void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { |
@@ -151,7 +151,7 @@ void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { | |||
151 | * Used to be part of lauxlib in Lua 5.1, was dropped from 5.2. | 151 | * Used to be part of lauxlib in Lua 5.1, was dropped from 5.2. |
152 | \*-------------------------------------------------------------------------*/ | 152 | \*-------------------------------------------------------------------------*/ |
153 | int auxiliar_typeerror (lua_State *L, int narg, const char *tname) { | 153 | int auxiliar_typeerror (lua_State *L, int narg, const char *tname) { |
154 | const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, | 154 | const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, |
155 | luaL_typename(L, narg)); | 155 | luaL_typename(L, narg)); |
156 | return luaL_argerror(L, narg, msg); | 156 | return luaL_argerror(L, narg, msg); |
157 | } | 157 | } |
diff --git a/src/auxiliar.h b/src/auxiliar.h index ea99013..65511d4 100644 --- a/src/auxiliar.h +++ b/src/auxiliar.h | |||
@@ -4,12 +4,12 @@ | |||
4 | * Auxiliar routines for class hierarchy manipulation | 4 | * Auxiliar routines for class hierarchy manipulation |
5 | * LuaSocket toolkit (but completely independent of other LuaSocket modules) | 5 | * LuaSocket toolkit (but completely independent of other LuaSocket modules) |
6 | * | 6 | * |
7 | * A LuaSocket class is a name associated with Lua metatables. A LuaSocket | 7 | * A LuaSocket class is a name associated with Lua metatables. A LuaSocket |
8 | * group is a name associated with a class. A class can belong to any number | 8 | * group is a name associated with a class. A class can belong to any number |
9 | * of groups. This module provides the functionality to: | 9 | * of groups. This module provides the functionality to: |
10 | * | 10 | * |
11 | * - create new classes | 11 | * - create new classes |
12 | * - add classes to groups | 12 | * - add classes to groups |
13 | * - set the class of objects | 13 | * - set the class of objects |
14 | * - check if an object belongs to a given class or group | 14 | * - check if an object belongs to a given class or group |
15 | * - get the userdata associated to objects | 15 | * - get the userdata associated to objects |
@@ -26,11 +26,12 @@ | |||
26 | * "class" with the class name. | 26 | * "class" with the class name. |
27 | * | 27 | * |
28 | * The mapping from class name to the corresponding metatable and the | 28 | * The mapping from class name to the corresponding metatable and the |
29 | * reverse mapping are done using lauxlib. | 29 | * reverse mapping are done using lauxlib. |
30 | \*=========================================================================*/ | 30 | \*=========================================================================*/ |
31 | 31 | ||
32 | #include "lua.h" | 32 | #include "lua.h" |
33 | #include "lauxlib.h" | 33 | #include "lauxlib.h" |
34 | #include "compat.h" | ||
34 | 35 | ||
35 | int auxiliar_open(lua_State *L); | 36 | int auxiliar_open(lua_State *L); |
36 | void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func); | 37 | void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func); |
diff --git a/src/buffer.c b/src/buffer.c index 423d804..8fc1166 100644 --- a/src/buffer.c +++ b/src/buffer.c | |||
@@ -4,6 +4,7 @@ | |||
4 | \*=========================================================================*/ | 4 | \*=========================================================================*/ |
5 | #include "lua.h" | 5 | #include "lua.h" |
6 | #include "lauxlib.h" | 6 | #include "lauxlib.h" |
7 | #include "compat.h" | ||
7 | 8 | ||
8 | #include "buffer.h" | 9 | #include "buffer.h" |
9 | 10 | ||
diff --git a/src/compat.c b/src/compat.c new file mode 100644 index 0000000..bc5cc0e --- /dev/null +++ b/src/compat.c | |||
@@ -0,0 +1,19 @@ | |||
1 | #include "compat.h" | ||
2 | |||
3 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM==501 | ||
4 | /* | ||
5 | ** Adapted from Lua 5.2 | ||
6 | */ | ||
7 | void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { | ||
8 | luaL_checkstack(L, nup+1, "too many upvalues"); | ||
9 | for (; l->name != NULL; l++) { /* fill the table with given functions */ | ||
10 | int i; | ||
11 | lua_pushstring(L, l->name); | ||
12 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | ||
13 | lua_pushvalue(L, -(nup+1)); | ||
14 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ | ||
15 | lua_settable(L, -(nup + 3)); | ||
16 | } | ||
17 | lua_pop(L, nup); /* remove upvalues */ | ||
18 | } | ||
19 | #endif | ||
diff --git a/src/compat.h b/src/compat.h new file mode 100644 index 0000000..7bf8010 --- /dev/null +++ b/src/compat.h | |||
@@ -0,0 +1,11 @@ | |||
1 | #ifndef COMPAT_H | ||
2 | #define COMPAT_H | ||
3 | |||
4 | #include "lua.h" | ||
5 | #include "lauxlib.h" | ||
6 | |||
7 | #if LUA_VERSION_NUM==501 | ||
8 | void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); | ||
9 | #endif | ||
10 | |||
11 | #endif | ||
diff --git a/src/except.c b/src/except.c index 4faa208..261ac98 100644 --- a/src/except.c +++ b/src/except.c | |||
@@ -6,6 +6,7 @@ | |||
6 | 6 | ||
7 | #include "lua.h" | 7 | #include "lua.h" |
8 | #include "lauxlib.h" | 8 | #include "lauxlib.h" |
9 | #include "compat.h" | ||
9 | 10 | ||
10 | #include "except.h" | 11 | #include "except.h" |
11 | 12 | ||
@@ -117,10 +118,6 @@ static int global_protect(lua_State *L) { | |||
117 | * Init module | 118 | * Init module |
118 | \*-------------------------------------------------------------------------*/ | 119 | \*-------------------------------------------------------------------------*/ |
119 | int except_open(lua_State *L) { | 120 | int except_open(lua_State *L) { |
120 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
121 | luaL_setfuncs(L, func, 0); | 121 | luaL_setfuncs(L, func, 0); |
122 | #else | ||
123 | luaL_openlib(L, NULL, func, 0); | ||
124 | #endif | ||
125 | return 0; | 122 | return 0; |
126 | } | 123 | } |
@@ -8,6 +8,7 @@ | |||
8 | 8 | ||
9 | #include "lua.h" | 9 | #include "lua.h" |
10 | #include "lauxlib.h" | 10 | #include "lauxlib.h" |
11 | #include "compat.h" | ||
11 | 12 | ||
12 | #include "inet.h" | 13 | #include "inet.h" |
13 | 14 | ||
@@ -41,11 +42,7 @@ int inet_open(lua_State *L) | |||
41 | { | 42 | { |
42 | lua_pushstring(L, "dns"); | 43 | lua_pushstring(L, "dns"); |
43 | lua_newtable(L); | 44 | lua_newtable(L); |
44 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
45 | luaL_setfuncs(L, func, 0); | 45 | luaL_setfuncs(L, func, 0); |
46 | #else | ||
47 | luaL_openlib(L, NULL, func, 0); | ||
48 | #endif | ||
49 | lua_settable(L, -3); | 46 | lua_settable(L, -3); |
50 | return 0; | 47 | return 0; |
51 | } | 48 | } |
@@ -25,6 +25,6 @@ const char *io_strerror(int err) { | |||
25 | case IO_DONE: return NULL; | 25 | case IO_DONE: return NULL; |
26 | case IO_CLOSED: return "closed"; | 26 | case IO_CLOSED: return "closed"; |
27 | case IO_TIMEOUT: return "timeout"; | 27 | case IO_TIMEOUT: return "timeout"; |
28 | default: return "unknown error"; | 28 | default: return "unknown error"; |
29 | } | 29 | } |
30 | } | 30 | } |
diff --git a/src/luasocket.c b/src/luasocket.c index c4eeab3..7d9c802 100644 --- a/src/luasocket.c +++ b/src/luasocket.c | |||
@@ -17,6 +17,7 @@ | |||
17 | \*=========================================================================*/ | 17 | \*=========================================================================*/ |
18 | #include "lua.h" | 18 | #include "lua.h" |
19 | #include "lauxlib.h" | 19 | #include "lauxlib.h" |
20 | #include "compat.h" | ||
20 | 21 | ||
21 | /*=========================================================================*\ | 22 | /*=========================================================================*\ |
22 | * LuaSocket includes | 23 | * LuaSocket includes |
@@ -83,12 +84,8 @@ static int global_unload(lua_State *L) { | |||
83 | static int base_open(lua_State *L) { | 84 | static int base_open(lua_State *L) { |
84 | if (socket_open()) { | 85 | if (socket_open()) { |
85 | /* export functions (and leave namespace table on top of stack) */ | 86 | /* export functions (and leave namespace table on top of stack) */ |
86 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
87 | lua_newtable(L); | 87 | lua_newtable(L); |
88 | luaL_setfuncs(L, func, 0); | 88 | luaL_setfuncs(L, func, 0); |
89 | #else | ||
90 | luaL_openlib(L, "socket", func, 0); | ||
91 | #endif | ||
92 | #ifdef LUASOCKET_DEBUG | 89 | #ifdef LUASOCKET_DEBUG |
93 | lua_pushstring(L, "_DEBUG"); | 90 | lua_pushstring(L, "_DEBUG"); |
94 | lua_pushboolean(L, 1); | 91 | lua_pushboolean(L, 1); |
diff --git a/src/makefile b/src/makefile index 7f118a7..2704a92 100644 --- a/src/makefile +++ b/src/makefile | |||
@@ -260,6 +260,7 @@ SOCKET_OBJS= \ | |||
260 | buffer.$(O) \ | 260 | buffer.$(O) \ |
261 | io.$(O) \ | 261 | io.$(O) \ |
262 | auxiliar.$(O) \ | 262 | auxiliar.$(O) \ |
263 | compat.$(O) \ | ||
263 | options.$(O) \ | 264 | options.$(O) \ |
264 | inet.$(O) \ | 265 | inet.$(O) \ |
265 | $(SOCKET) \ | 266 | $(SOCKET) \ |
@@ -272,7 +273,8 @@ SOCKET_OBJS= \ | |||
272 | # Modules belonging mime-core | 273 | # Modules belonging mime-core |
273 | # | 274 | # |
274 | MIME_OBJS= \ | 275 | MIME_OBJS= \ |
275 | mime.$(O) | 276 | mime.$(O) \ |
277 | compat.$(O) | ||
276 | 278 | ||
277 | #------ | 279 | #------ |
278 | # Modules belonging unix (local domain sockets) | 280 | # Modules belonging unix (local domain sockets) |
@@ -383,6 +385,7 @@ clean: | |||
383 | #------ | 385 | #------ |
384 | # List of dependencies | 386 | # List of dependencies |
385 | # | 387 | # |
388 | compat.$(O): compat.c compat.h | ||
386 | auxiliar.$(O): auxiliar.c auxiliar.h | 389 | auxiliar.$(O): auxiliar.c auxiliar.h |
387 | buffer.$(O): buffer.c buffer.h io.h timeout.h | 390 | buffer.$(O): buffer.c buffer.h io.h timeout.h |
388 | except.$(O): except.c except.h | 391 | except.$(O): except.c except.h |
@@ -6,10 +6,7 @@ | |||
6 | 6 | ||
7 | #include "lua.h" | 7 | #include "lua.h" |
8 | #include "lauxlib.h" | 8 | #include "lauxlib.h" |
9 | 9 | #include "compat.h" | |
10 | #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501) | ||
11 | #include "compat-5.1.h" | ||
12 | #endif | ||
13 | 10 | ||
14 | #include "mime.h" | 11 | #include "mime.h" |
15 | 12 | ||
@@ -81,12 +78,8 @@ static UC b64unbase[256]; | |||
81 | \*-------------------------------------------------------------------------*/ | 78 | \*-------------------------------------------------------------------------*/ |
82 | MIME_API int luaopen_mime_core(lua_State *L) | 79 | MIME_API int luaopen_mime_core(lua_State *L) |
83 | { | 80 | { |
84 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
85 | lua_newtable(L); | 81 | lua_newtable(L); |
86 | luaL_setfuncs(L, func, 0); | 82 | luaL_setfuncs(L, func, 0); |
87 | #else | ||
88 | luaL_openlib(L, "mime", func, 0); | ||
89 | #endif | ||
90 | /* make version string available to scripts */ | 83 | /* make version string available to scripts */ |
91 | lua_pushstring(L, "_VERSION"); | 84 | lua_pushstring(L, "_VERSION"); |
92 | lua_pushstring(L, MIME_VERSION); | 85 | lua_pushstring(L, MIME_VERSION); |
diff --git a/src/options.c b/src/options.c index 8ac2a14..f41a5e5 100644 --- a/src/options.c +++ b/src/options.c | |||
@@ -1,8 +1,8 @@ | |||
1 | /*=========================================================================*\ | 1 | /*=========================================================================*\ |
2 | * Common option interface | 2 | * Common option interface |
3 | * LuaSocket toolkit | 3 | * LuaSocket toolkit |
4 | \*=========================================================================*/ | 4 | \*=========================================================================*/ |
5 | #include <string.h> | 5 | #include <string.h> |
6 | 6 | ||
7 | #include "lauxlib.h" | 7 | #include "lauxlib.h" |
8 | 8 | ||
@@ -20,9 +20,9 @@ static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); | |||
20 | static int opt_getboolean(lua_State *L, p_socket ps, int level, int name); | 20 | static int opt_getboolean(lua_State *L, p_socket ps, int level, int name); |
21 | static int opt_setint(lua_State *L, p_socket ps, int level, int name); | 21 | static int opt_setint(lua_State *L, p_socket ps, int level, int name); |
22 | static int opt_getint(lua_State *L, p_socket ps, int level, int name); | 22 | static int opt_getint(lua_State *L, p_socket ps, int level, int name); |
23 | static int opt_set(lua_State *L, p_socket ps, int level, int name, | 23 | static int opt_set(lua_State *L, p_socket ps, int level, int name, |
24 | void *val, int len); | 24 | void *val, int len); |
25 | static int opt_get(lua_State *L, p_socket ps, int level, int name, | 25 | static int opt_get(lua_State *L, p_socket ps, int level, int name, |
26 | void *val, int* len); | 26 | void *val, int* len); |
27 | 27 | ||
28 | /*=========================================================================*\ | 28 | /*=========================================================================*\ |
@@ -60,29 +60,29 @@ int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps) | |||
60 | /* enables reuse of local address */ | 60 | /* enables reuse of local address */ |
61 | int opt_set_reuseaddr(lua_State *L, p_socket ps) | 61 | int opt_set_reuseaddr(lua_State *L, p_socket ps) |
62 | { | 62 | { |
63 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); | 63 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); |
64 | } | 64 | } |
65 | 65 | ||
66 | int opt_get_reuseaddr(lua_State *L, p_socket ps) | 66 | int opt_get_reuseaddr(lua_State *L, p_socket ps) |
67 | { | 67 | { |
68 | return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); | 68 | return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); |
69 | } | 69 | } |
70 | 70 | ||
71 | /* enables reuse of local port */ | 71 | /* enables reuse of local port */ |
72 | int opt_set_reuseport(lua_State *L, p_socket ps) | 72 | int opt_set_reuseport(lua_State *L, p_socket ps) |
73 | { | 73 | { |
74 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); | 74 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); |
75 | } | 75 | } |
76 | 76 | ||
77 | int opt_get_reuseport(lua_State *L, p_socket ps) | 77 | int opt_get_reuseport(lua_State *L, p_socket ps) |
78 | { | 78 | { |
79 | return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); | 79 | return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); |
80 | } | 80 | } |
81 | 81 | ||
82 | /* disables the Naggle algorithm */ | 82 | /* disables the Naggle algorithm */ |
83 | int opt_set_tcp_nodelay(lua_State *L, p_socket ps) | 83 | int opt_set_tcp_nodelay(lua_State *L, p_socket ps) |
84 | { | 84 | { |
85 | return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); | 85 | return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); |
86 | } | 86 | } |
87 | 87 | ||
88 | int opt_get_tcp_nodelay(lua_State *L, p_socket ps) | 88 | int opt_get_tcp_nodelay(lua_State *L, p_socket ps) |
@@ -92,12 +92,12 @@ int opt_get_tcp_nodelay(lua_State *L, p_socket ps) | |||
92 | 92 | ||
93 | int opt_set_keepalive(lua_State *L, p_socket ps) | 93 | int opt_set_keepalive(lua_State *L, p_socket ps) |
94 | { | 94 | { |
95 | return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); | 95 | return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); |
96 | } | 96 | } |
97 | 97 | ||
98 | int opt_get_keepalive(lua_State *L, p_socket ps) | 98 | int opt_get_keepalive(lua_State *L, p_socket ps) |
99 | { | 99 | { |
100 | return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); | 100 | return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); |
101 | } | 101 | } |
102 | 102 | ||
103 | int opt_set_dontroute(lua_State *L, p_socket ps) | 103 | int opt_set_dontroute(lua_State *L, p_socket ps) |
@@ -156,12 +156,12 @@ int opt_set_linger(lua_State *L, p_socket ps) | |||
156 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); | 156 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); |
157 | lua_pushstring(L, "on"); | 157 | lua_pushstring(L, "on"); |
158 | lua_gettable(L, 3); | 158 | lua_gettable(L, 3); |
159 | if (!lua_isboolean(L, -1)) | 159 | if (!lua_isboolean(L, -1)) |
160 | luaL_argerror(L, 3, "boolean 'on' field expected"); | 160 | luaL_argerror(L, 3, "boolean 'on' field expected"); |
161 | li.l_onoff = (u_short) lua_toboolean(L, -1); | 161 | li.l_onoff = (u_short) lua_toboolean(L, -1); |
162 | lua_pushstring(L, "timeout"); | 162 | lua_pushstring(L, "timeout"); |
163 | lua_gettable(L, 3); | 163 | lua_gettable(L, 3); |
164 | if (!lua_isnumber(L, -1)) | 164 | if (!lua_isnumber(L, -1)) |
165 | luaL_argerror(L, 3, "number 'timeout' field expected"); | 165 | luaL_argerror(L, 3, "number 'timeout' field expected"); |
166 | li.l_linger = (u_short) lua_tonumber(L, -1); | 166 | li.l_linger = (u_short) lua_tonumber(L, -1); |
167 | return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); | 167 | return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); |
@@ -194,7 +194,7 @@ int opt_set_ip_multicast_if(lua_State *L, p_socket ps) | |||
194 | val.s_addr = htonl(INADDR_ANY); | 194 | val.s_addr = htonl(INADDR_ANY); |
195 | if (strcmp(address, "*") && !inet_aton(address, &val)) | 195 | if (strcmp(address, "*") && !inet_aton(address, &val)) |
196 | luaL_argerror(L, 3, "ip expected"); | 196 | luaL_argerror(L, 3, "ip expected"); |
197 | return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF, | 197 | return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF, |
198 | (char *) &val, sizeof(val)); | 198 | (char *) &val, sizeof(val)); |
199 | } | 199 | } |
200 | 200 | ||
@@ -250,17 +250,17 @@ static int opt_setmembership(lua_State *L, p_socket ps, int level, int name) | |||
250 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); | 250 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); |
251 | lua_pushstring(L, "multiaddr"); | 251 | lua_pushstring(L, "multiaddr"); |
252 | lua_gettable(L, 3); | 252 | lua_gettable(L, 3); |
253 | if (!lua_isstring(L, -1)) | 253 | if (!lua_isstring(L, -1)) |
254 | luaL_argerror(L, 3, "string 'multiaddr' field expected"); | 254 | luaL_argerror(L, 3, "string 'multiaddr' field expected"); |
255 | if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr)) | 255 | if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr)) |
256 | luaL_argerror(L, 3, "invalid 'multiaddr' ip address"); | 256 | luaL_argerror(L, 3, "invalid 'multiaddr' ip address"); |
257 | lua_pushstring(L, "interface"); | 257 | lua_pushstring(L, "interface"); |
258 | lua_gettable(L, 3); | 258 | lua_gettable(L, 3); |
259 | if (!lua_isstring(L, -1)) | 259 | if (!lua_isstring(L, -1)) |
260 | luaL_argerror(L, 3, "string 'interface' field expected"); | 260 | luaL_argerror(L, 3, "string 'interface' field expected"); |
261 | val.imr_interface.s_addr = htonl(INADDR_ANY); | 261 | val.imr_interface.s_addr = htonl(INADDR_ANY); |
262 | if (strcmp(lua_tostring(L, -1), "*") && | 262 | if (strcmp(lua_tostring(L, -1), "*") && |
263 | !inet_aton(lua_tostring(L, -1), &val.imr_interface)) | 263 | !inet_aton(lua_tostring(L, -1), &val.imr_interface)) |
264 | luaL_argerror(L, 3, "invalid 'interface' ip address"); | 264 | luaL_argerror(L, 3, "invalid 'interface' ip address"); |
265 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); | 265 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); |
266 | } | 266 | } |
@@ -272,14 +272,14 @@ static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name) | |||
272 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); | 272 | if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); |
273 | lua_pushstring(L, "multiaddr"); | 273 | lua_pushstring(L, "multiaddr"); |
274 | lua_gettable(L, 3); | 274 | lua_gettable(L, 3); |
275 | if (!lua_isstring(L, -1)) | 275 | if (!lua_isstring(L, -1)) |
276 | luaL_argerror(L, 3, "string 'multiaddr' field expected"); | 276 | luaL_argerror(L, 3, "string 'multiaddr' field expected"); |
277 | if (!inet_pton(AF_INET6, lua_tostring(L, -1), &val.ipv6mr_multiaddr)) | 277 | if (!inet_pton(AF_INET6, lua_tostring(L, -1), &val.ipv6mr_multiaddr)) |
278 | luaL_argerror(L, 3, "invalid 'multiaddr' ip address"); | 278 | luaL_argerror(L, 3, "invalid 'multiaddr' ip address"); |
279 | lua_pushstring(L, "interface"); | 279 | lua_pushstring(L, "interface"); |
280 | lua_gettable(L, 3); | 280 | lua_gettable(L, 3); |
281 | /* By default we listen to interface on default route | 281 | /* By default we listen to interface on default route |
282 | * (sigh). However, interface= can override it. We should | 282 | * (sigh). However, interface= can override it. We should |
283 | * support either number, or name for it. Waiting for | 283 | * support either number, or name for it. Waiting for |
284 | * windows port of if_nametoindex */ | 284 | * windows port of if_nametoindex */ |
285 | if (!lua_isnil(L, -1)) { | 285 | if (!lua_isnil(L, -1)) { |
@@ -291,7 +291,7 @@ static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name) | |||
291 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); | 291 | return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); |
292 | } | 292 | } |
293 | 293 | ||
294 | static | 294 | static |
295 | int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) | 295 | int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) |
296 | { | 296 | { |
297 | socklen_t socklen = *len; | 297 | socklen_t socklen = *len; |
@@ -304,7 +304,7 @@ int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) | |||
304 | return 0; | 304 | return 0; |
305 | } | 305 | } |
306 | 306 | ||
307 | static | 307 | static |
308 | int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) | 308 | int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) |
309 | { | 309 | { |
310 | if (setsockopt(*ps, level, name, (char *) val, len) < 0) { | 310 | if (setsockopt(*ps, level, name, (char *) val, len) < 0) { |
diff --git a/src/select.c b/src/select.c index fafaa62..d14c40a 100644 --- a/src/select.c +++ b/src/select.c | |||
@@ -6,6 +6,7 @@ | |||
6 | 6 | ||
7 | #include "lua.h" | 7 | #include "lua.h" |
8 | #include "lauxlib.h" | 8 | #include "lauxlib.h" |
9 | #include "compat.h" | ||
9 | 10 | ||
10 | #include "socket.h" | 11 | #include "socket.h" |
11 | #include "timeout.h" | 12 | #include "timeout.h" |
@@ -16,10 +17,10 @@ | |||
16 | \*=========================================================================*/ | 17 | \*=========================================================================*/ |
17 | static t_socket getfd(lua_State *L); | 18 | static t_socket getfd(lua_State *L); |
18 | static int dirty(lua_State *L); | 19 | static int dirty(lua_State *L); |
19 | static void collect_fd(lua_State *L, int tab, int itab, | 20 | static void collect_fd(lua_State *L, int tab, int itab, |
20 | fd_set *set, t_socket *max_fd); | 21 | fd_set *set, t_socket *max_fd); |
21 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); | 22 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); |
22 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, | 23 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, |
23 | int itab, int tab, int start); | 24 | int itab, int tab, int start); |
24 | static void make_assoc(lua_State *L, int tab); | 25 | static void make_assoc(lua_State *L, int tab); |
25 | static int global_select(lua_State *L); | 26 | static int global_select(lua_State *L); |
@@ -40,11 +41,7 @@ int select_open(lua_State *L) { | |||
40 | lua_pushstring(L, "_SETSIZE"); | 41 | lua_pushstring(L, "_SETSIZE"); |
41 | lua_pushnumber(L, FD_SETSIZE); | 42 | lua_pushnumber(L, FD_SETSIZE); |
42 | lua_rawset(L, -3); | 43 | lua_rawset(L, -3); |
43 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
44 | luaL_setfuncs(L, func, 0); | 44 | luaL_setfuncs(L, func, 0); |
45 | #else | ||
46 | luaL_openlib(L, NULL, func, 0); | ||
47 | #endif | ||
48 | return 0; | 45 | return 0; |
49 | } | 46 | } |
50 | 47 | ||
@@ -98,10 +95,10 @@ static t_socket getfd(lua_State *L) { | |||
98 | lua_pushvalue(L, -2); | 95 | lua_pushvalue(L, -2); |
99 | lua_call(L, 1, 1); | 96 | lua_call(L, 1, 1); |
100 | if (lua_isnumber(L, -1)) { | 97 | if (lua_isnumber(L, -1)) { |
101 | double numfd = lua_tonumber(L, -1); | 98 | double numfd = lua_tonumber(L, -1); |
102 | fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID; | 99 | fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID; |
103 | } | 100 | } |
104 | } | 101 | } |
105 | lua_pop(L, 1); | 102 | lua_pop(L, 1); |
106 | return fd; | 103 | return fd; |
107 | } | 104 | } |
@@ -114,12 +111,12 @@ static int dirty(lua_State *L) { | |||
114 | lua_pushvalue(L, -2); | 111 | lua_pushvalue(L, -2); |
115 | lua_call(L, 1, 1); | 112 | lua_call(L, 1, 1); |
116 | is = lua_toboolean(L, -1); | 113 | is = lua_toboolean(L, -1); |
117 | } | 114 | } |
118 | lua_pop(L, 1); | 115 | lua_pop(L, 1); |
119 | return is; | 116 | return is; |
120 | } | 117 | } |
121 | 118 | ||
122 | static void collect_fd(lua_State *L, int tab, int itab, | 119 | static void collect_fd(lua_State *L, int tab, int itab, |
123 | fd_set *set, t_socket *max_fd) { | 120 | fd_set *set, t_socket *max_fd) { |
124 | int i = 1, n = 0; | 121 | int i = 1, n = 0; |
125 | /* nil is the same as an empty table */ | 122 | /* nil is the same as an empty table */ |
@@ -139,16 +136,16 @@ static void collect_fd(lua_State *L, int tab, int itab, | |||
139 | if (fd != SOCKET_INVALID) { | 136 | if (fd != SOCKET_INVALID) { |
140 | /* make sure we don't overflow the fd_set */ | 137 | /* make sure we don't overflow the fd_set */ |
141 | #ifdef _WIN32 | 138 | #ifdef _WIN32 |
142 | if (n >= FD_SETSIZE) | 139 | if (n >= FD_SETSIZE) |
143 | luaL_argerror(L, tab, "too many sockets"); | 140 | luaL_argerror(L, tab, "too many sockets"); |
144 | #else | 141 | #else |
145 | if (fd >= FD_SETSIZE) | 142 | if (fd >= FD_SETSIZE) |
146 | luaL_argerror(L, tab, "descriptor too large for set size"); | 143 | luaL_argerror(L, tab, "descriptor too large for set size"); |
147 | #endif | 144 | #endif |
148 | FD_SET(fd, set); | 145 | FD_SET(fd, set); |
149 | n++; | 146 | n++; |
150 | /* keep track of the largest descriptor so far */ | 147 | /* keep track of the largest descriptor so far */ |
151 | if (*max_fd == SOCKET_INVALID || *max_fd < fd) | 148 | if (*max_fd == SOCKET_INVALID || *max_fd < fd) |
152 | *max_fd = fd; | 149 | *max_fd = fd; |
153 | /* make sure we can map back from descriptor to the object */ | 150 | /* make sure we can map back from descriptor to the object */ |
154 | lua_pushnumber(L, (lua_Number) fd); | 151 | lua_pushnumber(L, (lua_Number) fd); |
@@ -162,9 +159,9 @@ static void collect_fd(lua_State *L, int tab, int itab, | |||
162 | 159 | ||
163 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { | 160 | static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { |
164 | int ndirty = 0, i = 1; | 161 | int ndirty = 0, i = 1; |
165 | if (lua_isnil(L, tab)) | 162 | if (lua_isnil(L, tab)) |
166 | return 0; | 163 | return 0; |
167 | for ( ;; ) { | 164 | for ( ;; ) { |
168 | t_socket fd; | 165 | t_socket fd; |
169 | lua_pushnumber(L, i); | 166 | lua_pushnumber(L, i); |
170 | lua_gettable(L, tab); | 167 | lua_gettable(L, tab); |
@@ -185,7 +182,7 @@ static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { | |||
185 | return ndirty; | 182 | return ndirty; |
186 | } | 183 | } |
187 | 184 | ||
188 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, | 185 | static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, |
189 | int itab, int tab, int start) { | 186 | int itab, int tab, int start) { |
190 | t_socket fd; | 187 | t_socket fd; |
191 | for (fd = 0; fd < max_fd; fd++) { | 188 | for (fd = 0; fd < max_fd; fd++) { |
diff --git a/src/serial.c b/src/serial.c index 583d4e5..dedbeb5 100644 --- a/src/serial.c +++ b/src/serial.c | |||
@@ -2,7 +2,7 @@ | |||
2 | * Serial stream | 2 | * Serial stream |
3 | * LuaSocket toolkit | 3 | * LuaSocket toolkit |
4 | \*=========================================================================*/ | 4 | \*=========================================================================*/ |
5 | #include <string.h> | 5 | #include <string.h> |
6 | 6 | ||
7 | #include "lua.h" | 7 | #include "lua.h" |
8 | #include "lauxlib.h" | 8 | #include "lauxlib.h" |
@@ -11,7 +11,7 @@ | |||
11 | #include "socket.h" | 11 | #include "socket.h" |
12 | #include "options.h" | 12 | #include "options.h" |
13 | #include "unix.h" | 13 | #include "unix.h" |
14 | #include <sys/un.h> | 14 | #include <sys/un.h> |
15 | 15 | ||
16 | /* | 16 | /* |
17 | Reuses userdata definition from unix.h, since it is useful for all | 17 | Reuses userdata definition from unix.h, since it is useful for all |
@@ -55,7 +55,7 @@ static luaL_Reg serial_methods[] = { | |||
55 | }; | 55 | }; |
56 | 56 | ||
57 | /* our socket creation function */ | 57 | /* our socket creation function */ |
58 | /* this is an ad-hoc module that returns a single function | 58 | /* this is an ad-hoc module that returns a single function |
59 | * as such, do not include other functions in this array. */ | 59 | * as such, do not include other functions in this array. */ |
60 | static luaL_Reg func[] = { | 60 | static luaL_Reg func[] = { |
61 | {"serial", global_create}, | 61 | {"serial", global_create}, |
@@ -120,7 +120,7 @@ static int meth_getfd(lua_State *L) { | |||
120 | /* this is very dangerous, but can be handy for those that are brave enough */ | 120 | /* this is very dangerous, but can be handy for those that are brave enough */ |
121 | static int meth_setfd(lua_State *L) { | 121 | static int meth_setfd(lua_State *L) { |
122 | p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); | 122 | p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); |
123 | un->sock = (t_socket) luaL_checknumber(L, 2); | 123 | un->sock = (t_socket) luaL_checknumber(L, 2); |
124 | return 0; | 124 | return 0; |
125 | } | 125 | } |
126 | 126 | ||
@@ -131,7 +131,7 @@ static int meth_dirty(lua_State *L) { | |||
131 | } | 131 | } |
132 | 132 | ||
133 | /*-------------------------------------------------------------------------*\ | 133 | /*-------------------------------------------------------------------------*\ |
134 | * Closes socket used by object | 134 | * Closes socket used by object |
135 | \*-------------------------------------------------------------------------*/ | 135 | \*-------------------------------------------------------------------------*/ |
136 | static int meth_close(lua_State *L) | 136 | static int meth_close(lua_State *L) |
137 | { | 137 | { |
@@ -156,7 +156,7 @@ static int meth_settimeout(lua_State *L) { | |||
156 | 156 | ||
157 | 157 | ||
158 | /*-------------------------------------------------------------------------*\ | 158 | /*-------------------------------------------------------------------------*\ |
159 | * Creates a serial object | 159 | * Creates a serial object |
160 | \*-------------------------------------------------------------------------*/ | 160 | \*-------------------------------------------------------------------------*/ |
161 | static int global_create(lua_State *L) { | 161 | static int global_create(lua_State *L) { |
162 | const char* path = luaL_checkstring(L, 1); | 162 | const char* path = luaL_checkstring(L, 1); |
@@ -180,7 +180,7 @@ static int global_create(lua_State *L) { | |||
180 | /* initialize remaining structure fields */ | 180 | /* initialize remaining structure fields */ |
181 | socket_setnonblocking(&sock); | 181 | socket_setnonblocking(&sock); |
182 | un->sock = sock; | 182 | un->sock = sock; |
183 | io_init(&un->io, (p_send) socket_write, (p_recv) socket_read, | 183 | io_init(&un->io, (p_send) socket_write, (p_recv) socket_read, |
184 | (p_error) socket_ioerror, &un->sock); | 184 | (p_error) socket_ioerror, &un->sock); |
185 | timeout_init(&un->tm, -1, -1); | 185 | timeout_init(&un->tm, -1, -1); |
186 | buffer_init(&un->buf, &un->io, &un->tm); | 186 | buffer_init(&un->buf, &un->io, &un->tm); |
@@ -6,6 +6,7 @@ | |||
6 | 6 | ||
7 | #include "lua.h" | 7 | #include "lua.h" |
8 | #include "lauxlib.h" | 8 | #include "lauxlib.h" |
9 | #include "compat.h" | ||
9 | 10 | ||
10 | #include "auxiliar.h" | 11 | #include "auxiliar.h" |
11 | #include "socket.h" | 12 | #include "socket.h" |
@@ -108,11 +109,7 @@ int tcp_open(lua_State *L) | |||
108 | auxiliar_add2group(L, "tcp{client}", "tcp{any}"); | 109 | auxiliar_add2group(L, "tcp{client}", "tcp{any}"); |
109 | auxiliar_add2group(L, "tcp{server}", "tcp{any}"); | 110 | auxiliar_add2group(L, "tcp{server}", "tcp{any}"); |
110 | /* define library functions */ | 111 | /* define library functions */ |
111 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
112 | luaL_setfuncs(L, func, 0); | 112 | luaL_setfuncs(L, func, 0); |
113 | #else | ||
114 | luaL_openlib(L, NULL, func, 0); | ||
115 | #endif | ||
116 | return 0; | 113 | return 0; |
117 | } | 114 | } |
118 | 115 | ||
diff --git a/src/timeout.c b/src/timeout.c index bdd5e1c..087d033 100644 --- a/src/timeout.c +++ b/src/timeout.c | |||
@@ -8,6 +8,7 @@ | |||
8 | 8 | ||
9 | #include "lua.h" | 9 | #include "lua.h" |
10 | #include "lauxlib.h" | 10 | #include "lauxlib.h" |
11 | #include "compat.h" | ||
11 | 12 | ||
12 | #include "auxiliar.h" | 13 | #include "auxiliar.h" |
13 | #include "timeout.h" | 14 | #include "timeout.h" |
@@ -52,7 +53,7 @@ void timeout_init(p_timeout tm, double block, double total) { | |||
52 | 53 | ||
53 | /*-------------------------------------------------------------------------*\ | 54 | /*-------------------------------------------------------------------------*\ |
54 | * Determines how much time we have left for the next system call, | 55 | * Determines how much time we have left for the next system call, |
55 | * if the previous call was successful | 56 | * if the previous call was successful |
56 | * Input | 57 | * Input |
57 | * tm: timeout control structure | 58 | * tm: timeout control structure |
58 | * Returns | 59 | * Returns |
@@ -107,7 +108,7 @@ double timeout_getretry(p_timeout tm) { | |||
107 | } | 108 | } |
108 | 109 | ||
109 | /*-------------------------------------------------------------------------*\ | 110 | /*-------------------------------------------------------------------------*\ |
110 | * Marks the operation start time in structure | 111 | * Marks the operation start time in structure |
111 | * Input | 112 | * Input |
112 | * tm: timeout control structure | 113 | * tm: timeout control structure |
113 | \*-------------------------------------------------------------------------*/ | 114 | \*-------------------------------------------------------------------------*/ |
@@ -117,7 +118,7 @@ p_timeout timeout_markstart(p_timeout tm) { | |||
117 | } | 118 | } |
118 | 119 | ||
119 | /*-------------------------------------------------------------------------*\ | 120 | /*-------------------------------------------------------------------------*\ |
120 | * Gets time in s, relative to January 1, 1970 (UTC) | 121 | * Gets time in s, relative to January 1, 1970 (UTC) |
121 | * Returns | 122 | * Returns |
122 | * time in s. | 123 | * time in s. |
123 | \*-------------------------------------------------------------------------*/ | 124 | \*-------------------------------------------------------------------------*/ |
@@ -144,11 +145,7 @@ double timeout_gettime(void) { | |||
144 | * Initializes module | 145 | * Initializes module |
145 | \*-------------------------------------------------------------------------*/ | 146 | \*-------------------------------------------------------------------------*/ |
146 | int timeout_open(lua_State *L) { | 147 | int timeout_open(lua_State *L) { |
147 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
148 | luaL_setfuncs(L, func, 0); | 148 | luaL_setfuncs(L, func, 0); |
149 | #else | ||
150 | luaL_openlib(L, NULL, func, 0); | ||
151 | #endif | ||
152 | return 0; | 149 | return 0; |
153 | } | 150 | } |
154 | 151 | ||
@@ -163,7 +160,7 @@ int timeout_meth_settimeout(lua_State *L, p_timeout tm) { | |||
163 | const char *mode = luaL_optstring(L, 3, "b"); | 160 | const char *mode = luaL_optstring(L, 3, "b"); |
164 | switch (*mode) { | 161 | switch (*mode) { |
165 | case 'b': | 162 | case 'b': |
166 | tm->block = t; | 163 | tm->block = t; |
167 | break; | 164 | break; |
168 | case 'r': case 't': | 165 | case 'r': case 't': |
169 | tm->total = t; | 166 | tm->total = t; |
@@ -7,6 +7,7 @@ | |||
7 | 7 | ||
8 | #include "lua.h" | 8 | #include "lua.h" |
9 | #include "lauxlib.h" | 9 | #include "lauxlib.h" |
10 | #include "compat.h" | ||
10 | 11 | ||
11 | #include "auxiliar.h" | 12 | #include "auxiliar.h" |
12 | #include "socket.h" | 13 | #include "socket.h" |
@@ -120,11 +121,7 @@ int udp_open(lua_State *L) | |||
120 | auxiliar_add2group(L, "udp{connected}", "select{able}"); | 121 | auxiliar_add2group(L, "udp{connected}", "select{able}"); |
121 | auxiliar_add2group(L, "udp{unconnected}", "select{able}"); | 122 | auxiliar_add2group(L, "udp{unconnected}", "select{able}"); |
122 | /* define library functions */ | 123 | /* define library functions */ |
123 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
124 | luaL_setfuncs(L, func, 0); | 124 | luaL_setfuncs(L, func, 0); |
125 | #else | ||
126 | luaL_openlib(L, NULL, func, 0); | ||
127 | #endif | ||
128 | return 0; | 125 | return 0; |
129 | } | 126 | } |
130 | 127 | ||
@@ -1,8 +1,8 @@ | |||
1 | /*=========================================================================*\ | 1 | /*=========================================================================*\ |
2 | * Unix domain socket | 2 | * Unix domain socket |
3 | * LuaSocket toolkit | 3 | * LuaSocket toolkit |
4 | \*=========================================================================*/ | 4 | \*=========================================================================*/ |
5 | #include <string.h> | 5 | #include <string.h> |
6 | 6 | ||
7 | #include "lua.h" | 7 | #include "lua.h" |
8 | #include "lauxlib.h" | 8 | #include "lauxlib.h" |
@@ -11,7 +11,7 @@ | |||
11 | #include "socket.h" | 11 | #include "socket.h" |
12 | #include "options.h" | 12 | #include "options.h" |
13 | #include "unix.h" | 13 | #include "unix.h" |
14 | #include <sys/un.h> | 14 | #include <sys/un.h> |
15 | 15 | ||
16 | /*=========================================================================*\ | 16 | /*=========================================================================*\ |
17 | * Internal function prototypes | 17 | * Internal function prototypes |
@@ -68,15 +68,6 @@ static t_opt optset[] = { | |||
68 | {NULL, NULL} | 68 | {NULL, NULL} |
69 | }; | 69 | }; |
70 | 70 | ||
71 | /* our socket creation function */ | ||
72 | /* this is an ad-hoc module that returns a single function | ||
73 | * as such, do not include other functions in this array. */ | ||
74 | static luaL_Reg func[] = { | ||
75 | {"unix", global_create}, | ||
76 | {NULL, NULL} | ||
77 | }; | ||
78 | |||
79 | |||
80 | /*-------------------------------------------------------------------------*\ | 71 | /*-------------------------------------------------------------------------*\ |
81 | * Initializes module | 72 | * Initializes module |
82 | \*-------------------------------------------------------------------------*/ | 73 | \*-------------------------------------------------------------------------*/ |
@@ -89,15 +80,8 @@ int luaopen_socket_unix(lua_State *L) { | |||
89 | auxiliar_add2group(L, "unix{master}", "unix{any}"); | 80 | auxiliar_add2group(L, "unix{master}", "unix{any}"); |
90 | auxiliar_add2group(L, "unix{client}", "unix{any}"); | 81 | auxiliar_add2group(L, "unix{client}", "unix{any}"); |
91 | auxiliar_add2group(L, "unix{server}", "unix{any}"); | 82 | auxiliar_add2group(L, "unix{server}", "unix{any}"); |
92 | #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) | ||
93 | lua_pushcfunction(L, global_create); | ||
94 | (void) func; | ||
95 | #else | ||
96 | /* set function into socket namespace */ | ||
97 | luaL_openlib(L, "socket", func, 0); | ||
98 | lua_pushcfunction(L, global_create); | ||
99 | #endif | ||
100 | /* return the function instead of the 'socket' table */ | 83 | /* return the function instead of the 'socket' table */ |
84 | lua_pushcfunction(L, global_create); | ||
101 | return 1; | 85 | return 1; |
102 | } | 86 | } |
103 | 87 | ||
@@ -147,7 +131,7 @@ static int meth_getfd(lua_State *L) { | |||
147 | /* this is very dangerous, but can be handy for those that are brave enough */ | 131 | /* this is very dangerous, but can be handy for those that are brave enough */ |
148 | static int meth_setfd(lua_State *L) { | 132 | static int meth_setfd(lua_State *L) { |
149 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); | 133 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
150 | un->sock = (t_socket) luaL_checknumber(L, 2); | 134 | un->sock = (t_socket) luaL_checknumber(L, 2); |
151 | return 0; | 135 | return 0; |
152 | } | 136 | } |
153 | 137 | ||
@@ -158,8 +142,8 @@ static int meth_dirty(lua_State *L) { | |||
158 | } | 142 | } |
159 | 143 | ||
160 | /*-------------------------------------------------------------------------*\ | 144 | /*-------------------------------------------------------------------------*\ |
161 | * Waits for and returns a client object attempting connection to the | 145 | * Waits for and returns a client object attempting connection to the |
162 | * server object | 146 | * server object |
163 | \*-------------------------------------------------------------------------*/ | 147 | \*-------------------------------------------------------------------------*/ |
164 | static int meth_accept(lua_State *L) { | 148 | static int meth_accept(lua_State *L) { |
165 | p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); | 149 | p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); |
@@ -173,20 +157,20 @@ static int meth_accept(lua_State *L) { | |||
173 | /* initialize structure fields */ | 157 | /* initialize structure fields */ |
174 | socket_setnonblocking(&sock); | 158 | socket_setnonblocking(&sock); |
175 | clnt->sock = sock; | 159 | clnt->sock = sock; |
176 | io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, | 160 | io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, |
177 | (p_error) socket_ioerror, &clnt->sock); | 161 | (p_error) socket_ioerror, &clnt->sock); |
178 | timeout_init(&clnt->tm, -1, -1); | 162 | timeout_init(&clnt->tm, -1, -1); |
179 | buffer_init(&clnt->buf, &clnt->io, &clnt->tm); | 163 | buffer_init(&clnt->buf, &clnt->io, &clnt->tm); |
180 | return 1; | 164 | return 1; |
181 | } else { | 165 | } else { |
182 | lua_pushnil(L); | 166 | lua_pushnil(L); |
183 | lua_pushstring(L, socket_strerror(err)); | 167 | lua_pushstring(L, socket_strerror(err)); |
184 | return 2; | 168 | return 2; |
185 | } | 169 | } |
186 | } | 170 | } |
187 | 171 | ||
188 | /*-------------------------------------------------------------------------*\ | 172 | /*-------------------------------------------------------------------------*\ |
189 | * Binds an object to an address | 173 | * Binds an object to an address |
190 | \*-------------------------------------------------------------------------*/ | 174 | \*-------------------------------------------------------------------------*/ |
191 | static const char *unix_trybind(p_unix un, const char *path) { | 175 | static const char *unix_trybind(p_unix un, const char *path) { |
192 | struct sockaddr_un local; | 176 | struct sockaddr_un local; |
@@ -197,16 +181,16 @@ static const char *unix_trybind(p_unix un, const char *path) { | |||
197 | strcpy(local.sun_path, path); | 181 | strcpy(local.sun_path, path); |
198 | local.sun_family = AF_UNIX; | 182 | local.sun_family = AF_UNIX; |
199 | #ifdef UNIX_HAS_SUN_LEN | 183 | #ifdef UNIX_HAS_SUN_LEN |
200 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) | 184 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) |
201 | + len + 1; | 185 | + len + 1; |
202 | err = socket_bind(&un->sock, (SA *) &local, local.sun_len); | 186 | err = socket_bind(&un->sock, (SA *) &local, local.sun_len); |
203 | 187 | ||
204 | #else | 188 | #else |
205 | err = socket_bind(&un->sock, (SA *) &local, | 189 | err = socket_bind(&un->sock, (SA *) &local, |
206 | sizeof(local.sun_family) + len); | 190 | sizeof(local.sun_family) + len); |
207 | #endif | 191 | #endif |
208 | if (err != IO_DONE) socket_destroy(&un->sock); | 192 | if (err != IO_DONE) socket_destroy(&un->sock); |
209 | return socket_strerror(err); | 193 | return socket_strerror(err); |
210 | } | 194 | } |
211 | 195 | ||
212 | static int meth_bind(lua_State *L) { | 196 | static int meth_bind(lua_State *L) { |
@@ -236,11 +220,11 @@ static const char *unix_tryconnect(p_unix un, const char *path) | |||
236 | remote.sun_family = AF_UNIX; | 220 | remote.sun_family = AF_UNIX; |
237 | timeout_markstart(&un->tm); | 221 | timeout_markstart(&un->tm); |
238 | #ifdef UNIX_HAS_SUN_LEN | 222 | #ifdef UNIX_HAS_SUN_LEN |
239 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) | 223 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) |
240 | + len + 1; | 224 | + len + 1; |
241 | err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); | 225 | err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); |
242 | #else | 226 | #else |
243 | err = socket_connect(&un->sock, (SA *) &remote, | 227 | err = socket_connect(&un->sock, (SA *) &remote, |
244 | sizeof(remote.sun_family) + len, &un->tm); | 228 | sizeof(remote.sun_family) + len, &un->tm); |
245 | #endif | 229 | #endif |
246 | if (err != IO_DONE) socket_destroy(&un->sock); | 230 | if (err != IO_DONE) socket_destroy(&un->sock); |
@@ -264,7 +248,7 @@ static int meth_connect(lua_State *L) | |||
264 | } | 248 | } |
265 | 249 | ||
266 | /*-------------------------------------------------------------------------*\ | 250 | /*-------------------------------------------------------------------------*\ |
267 | * Closes socket used by object | 251 | * Closes socket used by object |
268 | \*-------------------------------------------------------------------------*/ | 252 | \*-------------------------------------------------------------------------*/ |
269 | static int meth_close(lua_State *L) | 253 | static int meth_close(lua_State *L) |
270 | { | 254 | { |
@@ -319,13 +303,13 @@ static int meth_settimeout(lua_State *L) { | |||
319 | * Library functions | 303 | * Library functions |
320 | \*=========================================================================*/ | 304 | \*=========================================================================*/ |
321 | /*-------------------------------------------------------------------------*\ | 305 | /*-------------------------------------------------------------------------*\ |
322 | * Creates a master unix object | 306 | * Creates a master unix object |
323 | \*-------------------------------------------------------------------------*/ | 307 | \*-------------------------------------------------------------------------*/ |
324 | static int global_create(lua_State *L) { | 308 | static int global_create(lua_State *L) { |
325 | t_socket sock; | 309 | t_socket sock; |
326 | int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); | 310 | int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); |
327 | /* try to allocate a system socket */ | 311 | /* try to allocate a system socket */ |
328 | if (err == IO_DONE) { | 312 | if (err == IO_DONE) { |
329 | /* allocate unix object */ | 313 | /* allocate unix object */ |
330 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); | 314 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); |
331 | /* set its type as master object */ | 315 | /* set its type as master object */ |
@@ -333,7 +317,7 @@ static int global_create(lua_State *L) { | |||
333 | /* initialize remaining structure fields */ | 317 | /* initialize remaining structure fields */ |
334 | socket_setnonblocking(&sock); | 318 | socket_setnonblocking(&sock); |
335 | un->sock = sock; | 319 | un->sock = sock; |
336 | io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, | 320 | io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, |
337 | (p_error) socket_ioerror, &un->sock); | 321 | (p_error) socket_ioerror, &un->sock); |
338 | timeout_init(&un->tm, -1, -1); | 322 | timeout_init(&un->tm, -1, -1); |
339 | buffer_init(&un->buf, &un->io, &un->tm); | 323 | buffer_init(&un->buf, &un->io, &un->tm); |
diff --git a/src/wsocket.c b/src/wsocket.c index 10800e3..8ecb0fc 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
@@ -3,7 +3,7 @@ | |||
3 | * LuaSocket toolkit | 3 | * LuaSocket toolkit |
4 | * | 4 | * |
5 | * The penalty of calling select to avoid busy-wait is only paid when | 5 | * The penalty of calling select to avoid busy-wait is only paid when |
6 | * the I/O call fail in the first place. | 6 | * the I/O call fail in the first place. |
7 | \*=========================================================================*/ | 7 | \*=========================================================================*/ |
8 | #include <string.h> | 8 | #include <string.h> |
9 | 9 | ||
@@ -14,23 +14,23 @@ | |||
14 | static const char *wstrerror(int err); | 14 | static const char *wstrerror(int err); |
15 | 15 | ||
16 | /*-------------------------------------------------------------------------*\ | 16 | /*-------------------------------------------------------------------------*\ |
17 | * Initializes module | 17 | * Initializes module |
18 | \*-------------------------------------------------------------------------*/ | 18 | \*-------------------------------------------------------------------------*/ |
19 | int socket_open(void) { | 19 | int socket_open(void) { |
20 | WSADATA wsaData; | 20 | WSADATA wsaData; |
21 | WORD wVersionRequested = MAKEWORD(2, 0); | 21 | WORD wVersionRequested = MAKEWORD(2, 0); |
22 | int err = WSAStartup(wVersionRequested, &wsaData ); | 22 | int err = WSAStartup(wVersionRequested, &wsaData ); |
23 | if (err != 0) return 0; | 23 | if (err != 0) return 0; |
24 | if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) && | 24 | if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) && |
25 | (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) { | 25 | (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) { |
26 | WSACleanup(); | 26 | WSACleanup(); |
27 | return 0; | 27 | return 0; |
28 | } | 28 | } |
29 | return 1; | 29 | return 1; |
30 | } | 30 | } |
31 | 31 | ||
32 | /*-------------------------------------------------------------------------*\ | 32 | /*-------------------------------------------------------------------------*\ |
33 | * Close module | 33 | * Close module |
34 | \*-------------------------------------------------------------------------*/ | 34 | \*-------------------------------------------------------------------------*/ |
35 | int socket_close(void) { | 35 | int socket_close(void) { |
36 | WSACleanup(); | 36 | WSACleanup(); |
@@ -51,10 +51,10 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) { | |||
51 | struct timeval tv, *tp = NULL; | 51 | struct timeval tv, *tp = NULL; |
52 | double t; | 52 | double t; |
53 | if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ | 53 | if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ |
54 | if (sw & WAITFD_R) { | 54 | if (sw & WAITFD_R) { |
55 | FD_ZERO(&rfds); | 55 | FD_ZERO(&rfds); |
56 | FD_SET(*ps, &rfds); | 56 | FD_SET(*ps, &rfds); |
57 | rp = &rfds; | 57 | rp = &rfds; |
58 | } | 58 | } |
59 | if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } | 59 | if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } |
60 | if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } | 60 | if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } |
@@ -73,9 +73,9 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) { | |||
73 | /*-------------------------------------------------------------------------*\ | 73 | /*-------------------------------------------------------------------------*\ |
74 | * Select with int timeout in ms | 74 | * Select with int timeout in ms |
75 | \*-------------------------------------------------------------------------*/ | 75 | \*-------------------------------------------------------------------------*/ |
76 | int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, | 76 | int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, |
77 | p_timeout tm) { | 77 | p_timeout tm) { |
78 | struct timeval tv; | 78 | struct timeval tv; |
79 | double t = timeout_get(tm); | 79 | double t = timeout_get(tm); |
80 | tv.tv_sec = (int) t; | 80 | tv.tv_sec = (int) t; |
81 | tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); | 81 | tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); |
@@ -97,7 +97,7 @@ void socket_destroy(p_socket ps) { | |||
97 | } | 97 | } |
98 | 98 | ||
99 | /*-------------------------------------------------------------------------*\ | 99 | /*-------------------------------------------------------------------------*\ |
100 | * | 100 | * |
101 | \*-------------------------------------------------------------------------*/ | 101 | \*-------------------------------------------------------------------------*/ |
102 | void socket_shutdown(p_socket ps, int how) { | 102 | void socket_shutdown(p_socket ps, int how) { |
103 | socket_setblocking(ps); | 103 | socket_setblocking(ps); |
@@ -135,10 +135,10 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { | |||
135 | /* give windows time to set the error (yes, disgusting) */ | 135 | /* give windows time to set the error (yes, disgusting) */ |
136 | Sleep(10); | 136 | Sleep(10); |
137 | /* find out why we failed */ | 137 | /* find out why we failed */ |
138 | getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); | 138 | getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); |
139 | /* we KNOW there was an error. if 'why' is 0, we will return | 139 | /* we KNOW there was an error. if 'why' is 0, we will return |
140 | * "unknown error", but it's not really our fault */ | 140 | * "unknown error", but it's not really our fault */ |
141 | return err > 0? err: IO_UNKNOWN; | 141 | return err > 0? err: IO_UNKNOWN; |
142 | } else return err; | 142 | } else return err; |
143 | 143 | ||
144 | } | 144 | } |
@@ -155,7 +155,7 @@ int socket_bind(p_socket ps, SA *addr, socklen_t len) { | |||
155 | } | 155 | } |
156 | 156 | ||
157 | /*-------------------------------------------------------------------------*\ | 157 | /*-------------------------------------------------------------------------*\ |
158 | * | 158 | * |
159 | \*-------------------------------------------------------------------------*/ | 159 | \*-------------------------------------------------------------------------*/ |
160 | int socket_listen(p_socket ps, int backlog) { | 160 | int socket_listen(p_socket ps, int backlog) { |
161 | int err = IO_DONE; | 161 | int err = IO_DONE; |
@@ -168,7 +168,7 @@ int socket_listen(p_socket ps, int backlog) { | |||
168 | /*-------------------------------------------------------------------------*\ | 168 | /*-------------------------------------------------------------------------*\ |
169 | * Accept with timeout | 169 | * Accept with timeout |
170 | \*-------------------------------------------------------------------------*/ | 170 | \*-------------------------------------------------------------------------*/ |
171 | int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, | 171 | int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, |
172 | p_timeout tm) { | 172 | p_timeout tm) { |
173 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 173 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
174 | for ( ;; ) { | 174 | for ( ;; ) { |
@@ -176,21 +176,21 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, | |||
176 | /* try to get client socket */ | 176 | /* try to get client socket */ |
177 | if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; | 177 | if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; |
178 | /* find out why we failed */ | 178 | /* find out why we failed */ |
179 | err = WSAGetLastError(); | 179 | err = WSAGetLastError(); |
180 | /* if we failed because there was no connectoin, keep trying */ | 180 | /* if we failed because there was no connectoin, keep trying */ |
181 | if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err; | 181 | if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err; |
182 | /* call select to avoid busy wait */ | 182 | /* call select to avoid busy wait */ |
183 | if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; | 183 | if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; |
184 | } | 184 | } |
185 | } | 185 | } |
186 | 186 | ||
187 | /*-------------------------------------------------------------------------*\ | 187 | /*-------------------------------------------------------------------------*\ |
188 | * Send with timeout | 188 | * Send with timeout |
189 | * On windows, if you try to send 10MB, the OS will buffer EVERYTHING | 189 | * On windows, if you try to send 10MB, the OS will buffer EVERYTHING |
190 | * this can take an awful lot of time and we will end up blocked. | 190 | * this can take an awful lot of time and we will end up blocked. |
191 | * Therefore, whoever calls this function should not pass a huge buffer. | 191 | * Therefore, whoever calls this function should not pass a huge buffer. |
192 | \*-------------------------------------------------------------------------*/ | 192 | \*-------------------------------------------------------------------------*/ |
193 | int socket_send(p_socket ps, const char *data, size_t count, | 193 | int socket_send(p_socket ps, const char *data, size_t count, |
194 | size_t *sent, p_timeout tm) | 194 | size_t *sent, p_timeout tm) |
195 | { | 195 | { |
196 | int err; | 196 | int err; |
@@ -207,18 +207,18 @@ int socket_send(p_socket ps, const char *data, size_t count, | |||
207 | return IO_DONE; | 207 | return IO_DONE; |
208 | } | 208 | } |
209 | /* deal with failure */ | 209 | /* deal with failure */ |
210 | err = WSAGetLastError(); | 210 | err = WSAGetLastError(); |
211 | /* we can only proceed if there was no serious error */ | 211 | /* we can only proceed if there was no serious error */ |
212 | if (err != WSAEWOULDBLOCK) return err; | 212 | if (err != WSAEWOULDBLOCK) return err; |
213 | /* avoid busy wait */ | 213 | /* avoid busy wait */ |
214 | if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; | 214 | if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; |
215 | } | 215 | } |
216 | } | 216 | } |
217 | 217 | ||
218 | /*-------------------------------------------------------------------------*\ | 218 | /*-------------------------------------------------------------------------*\ |
219 | * Sendto with timeout | 219 | * Sendto with timeout |
220 | \*-------------------------------------------------------------------------*/ | 220 | \*-------------------------------------------------------------------------*/ |
221 | int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, | 221 | int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, |
222 | SA *addr, socklen_t len, p_timeout tm) | 222 | SA *addr, socklen_t len, p_timeout tm) |
223 | { | 223 | { |
224 | int err; | 224 | int err; |
@@ -230,17 +230,17 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, | |||
230 | *sent = put; | 230 | *sent = put; |
231 | return IO_DONE; | 231 | return IO_DONE; |
232 | } | 232 | } |
233 | err = WSAGetLastError(); | 233 | err = WSAGetLastError(); |
234 | if (err != WSAEWOULDBLOCK) return err; | 234 | if (err != WSAEWOULDBLOCK) return err; |
235 | if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; | 235 | if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; |
236 | } | 236 | } |
237 | } | 237 | } |
238 | 238 | ||
239 | /*-------------------------------------------------------------------------*\ | 239 | /*-------------------------------------------------------------------------*\ |
240 | * Receive with timeout | 240 | * Receive with timeout |
241 | \*-------------------------------------------------------------------------*/ | 241 | \*-------------------------------------------------------------------------*/ |
242 | int socket_recv(p_socket ps, char *data, size_t count, size_t *got, | 242 | int socket_recv(p_socket ps, char *data, size_t count, size_t *got, |
243 | p_timeout tm) | 243 | p_timeout tm) |
244 | { | 244 | { |
245 | int err, prev = IO_DONE; | 245 | int err, prev = IO_DONE; |
246 | *got = 0; | 246 | *got = 0; |
@@ -253,9 +253,9 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, | |||
253 | } | 253 | } |
254 | if (taken == 0) return IO_CLOSED; | 254 | if (taken == 0) return IO_CLOSED; |
255 | err = WSAGetLastError(); | 255 | err = WSAGetLastError(); |
256 | /* On UDP, a connreset simply means the previous send failed. | 256 | /* On UDP, a connreset simply means the previous send failed. |
257 | * So we try again. | 257 | * So we try again. |
258 | * On TCP, it means our socket is now useless, so the error passes. | 258 | * On TCP, it means our socket is now useless, so the error passes. |
259 | * (We will loop again, exiting because the same error will happen) */ | 259 | * (We will loop again, exiting because the same error will happen) */ |
260 | if (err != WSAEWOULDBLOCK) { | 260 | if (err != WSAEWOULDBLOCK) { |
261 | if (err != WSAECONNRESET || prev == WSAECONNRESET) return err; | 261 | if (err != WSAECONNRESET || prev == WSAECONNRESET) return err; |
@@ -268,8 +268,8 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, | |||
268 | /*-------------------------------------------------------------------------*\ | 268 | /*-------------------------------------------------------------------------*\ |
269 | * Recvfrom with timeout | 269 | * Recvfrom with timeout |
270 | \*-------------------------------------------------------------------------*/ | 270 | \*-------------------------------------------------------------------------*/ |
271 | int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, | 271 | int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, |
272 | SA *addr, socklen_t *len, p_timeout tm) | 272 | SA *addr, socklen_t *len, p_timeout tm) |
273 | { | 273 | { |
274 | int err, prev = IO_DONE; | 274 | int err, prev = IO_DONE; |
275 | *got = 0; | 275 | *got = 0; |
@@ -282,8 +282,8 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, | |||
282 | } | 282 | } |
283 | if (taken == 0) return IO_CLOSED; | 283 | if (taken == 0) return IO_CLOSED; |
284 | err = WSAGetLastError(); | 284 | err = WSAGetLastError(); |
285 | /* On UDP, a connreset simply means the previous send failed. | 285 | /* On UDP, a connreset simply means the previous send failed. |
286 | * So we try again. | 286 | * So we try again. |
287 | * On TCP, it means our socket is now useless, so the error passes. | 287 | * On TCP, it means our socket is now useless, so the error passes. |
288 | * (We will loop again, exiting because the same error will happen) */ | 288 | * (We will loop again, exiting because the same error will happen) */ |
289 | if (err != WSAEWOULDBLOCK) { | 289 | if (err != WSAEWOULDBLOCK) { |
@@ -311,7 +311,7 @@ void socket_setnonblocking(p_socket ps) { | |||
311 | } | 311 | } |
312 | 312 | ||
313 | /*-------------------------------------------------------------------------*\ | 313 | /*-------------------------------------------------------------------------*\ |
314 | * DNS helpers | 314 | * DNS helpers |
315 | \*-------------------------------------------------------------------------*/ | 315 | \*-------------------------------------------------------------------------*/ |
316 | int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { | 316 | int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { |
317 | *hp = gethostbyaddr(addr, len, AF_INET); | 317 | *hp = gethostbyaddr(addr, len, AF_INET); |
@@ -332,7 +332,7 @@ const char *socket_hoststrerror(int err) { | |||
332 | if (err <= 0) return io_strerror(err); | 332 | if (err <= 0) return io_strerror(err); |
333 | switch (err) { | 333 | switch (err) { |
334 | case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; | 334 | case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; |
335 | default: return wstrerror(err); | 335 | default: return wstrerror(err); |
336 | } | 336 | } |
337 | } | 337 | } |
338 | 338 | ||
@@ -374,7 +374,7 @@ static const char *wstrerror(int err) { | |||
374 | case WSAESOCKTNOSUPPORT: return PIE_SOCKTYPE; // "Socket type not supported"; | 374 | case WSAESOCKTNOSUPPORT: return PIE_SOCKTYPE; // "Socket type not supported"; |
375 | case WSAEOPNOTSUPP: return "Operation not supported"; | 375 | case WSAEOPNOTSUPP: return "Operation not supported"; |
376 | case WSAEPFNOSUPPORT: return "Protocol family not supported"; | 376 | case WSAEPFNOSUPPORT: return "Protocol family not supported"; |
377 | case WSAEAFNOSUPPORT: return PIE_FAMILY; // "Address family not supported by protocol family"; | 377 | case WSAEAFNOSUPPORT: return PIE_FAMILY; // "Address family not supported by protocol family"; |
378 | case WSAEADDRINUSE: return PIE_ADDRINUSE; // "Address already in use"; | 378 | case WSAEADDRINUSE: return PIE_ADDRINUSE; // "Address already in use"; |
379 | case WSAEADDRNOTAVAIL: return "Cannot assign requested address"; | 379 | case WSAEADDRNOTAVAIL: return "Cannot assign requested address"; |
380 | case WSAENETDOWN: return "Network is down"; | 380 | case WSAENETDOWN: return "Network is down"; |
@@ -393,19 +393,19 @@ static const char *wstrerror(int err) { | |||
393 | case WSAEPROCLIM: return "Too many processes"; | 393 | case WSAEPROCLIM: return "Too many processes"; |
394 | case WSASYSNOTREADY: return "Network subsystem is unavailable"; | 394 | case WSASYSNOTREADY: return "Network subsystem is unavailable"; |
395 | case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range"; | 395 | case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range"; |
396 | case WSANOTINITIALISED: | 396 | case WSANOTINITIALISED: |
397 | return "Successful WSAStartup not yet performed"; | 397 | return "Successful WSAStartup not yet performed"; |
398 | case WSAEDISCON: return "Graceful shutdown in progress"; | 398 | case WSAEDISCON: return "Graceful shutdown in progress"; |
399 | case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; // "Host not found"; | 399 | case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; // "Host not found"; |
400 | case WSATRY_AGAIN: return "Nonauthoritative host not found"; | 400 | case WSATRY_AGAIN: return "Nonauthoritative host not found"; |
401 | case WSANO_RECOVERY: return PIE_FAIL; // "Nonrecoverable name lookup error"; | 401 | case WSANO_RECOVERY: return PIE_FAIL; // "Nonrecoverable name lookup error"; |
402 | case WSANO_DATA: return "Valid name, no data record of requested type"; | 402 | case WSANO_DATA: return "Valid name, no data record of requested type"; |
403 | default: return "Unknown error"; | 403 | default: return "Unknown error"; |
404 | } | 404 | } |
405 | } | 405 | } |
406 | 406 | ||
407 | const char *socket_gaistrerror(int err) { | 407 | const char *socket_gaistrerror(int err) { |
408 | if (err == 0) return NULL; | 408 | if (err == 0) return NULL; |
409 | switch (err) { | 409 | switch (err) { |
410 | case EAI_AGAIN: return PIE_AGAIN; | 410 | case EAI_AGAIN: return PIE_AGAIN; |
411 | case EAI_BADFLAGS: return PIE_BADFLAGS; | 411 | case EAI_BADFLAGS: return PIE_BADFLAGS; |
@@ -425,7 +425,7 @@ const char *socket_gaistrerror(int err) { | |||
425 | case EAI_SERVICE: return PIE_SERVICE; | 425 | case EAI_SERVICE: return PIE_SERVICE; |
426 | case EAI_SOCKTYPE: return PIE_SOCKTYPE; | 426 | case EAI_SOCKTYPE: return PIE_SOCKTYPE; |
427 | #ifdef EAI_SYSTEM | 427 | #ifdef EAI_SYSTEM |
428 | case EAI_SYSTEM: return strerror(errno); | 428 | case EAI_SYSTEM: return strerror(errno); |
429 | #endif | 429 | #endif |
430 | default: return gai_strerror(err); | 430 | default: return gai_strerror(err); |
431 | } | 431 | } |
diff --git a/test/testclnt.lua b/test/testclnt.lua index 315783b..0014781 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
@@ -590,7 +590,7 @@ function test_writeafterclose() | |||
590 | data = nil | 590 | data = nil |
591 | ]])) | 591 | ]])) |
592 | local sent, err, errsent | 592 | local sent, err, errsent |
593 | while not err do | 593 | while not err do |
594 | sent, err, errsent, time = data:send(str) | 594 | sent, err, errsent, time = data:send(str) |
595 | end | 595 | end |
596 | assert(err == "closed", "should have returned 'closed'") | 596 | assert(err == "closed", "should have returned 'closed'") |