aboutsummaryrefslogtreecommitdiff
path: root/src/options.c
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2015-08-21 15:39:34 -0300
committerDiego Nehab <diego.nehab@gmail.com>2015-08-21 15:39:34 -0300
commite75444ccd1f30a3b5fbc7cec4a85e831bd0560ed (patch)
tree71475c18fee070c770fc0fe25d0859b7d54c8fbb /src/options.c
parent321c0c9b1f7b6b83cd83b58e7e259f53eca69373 (diff)
downloadluasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.tar.gz
luasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.tar.bz2
luasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.zip
New compat.h module implements luaL_setfuncs.
Makes initialization code simpler everywhere.
Diffstat (limited to 'src/options.c')
-rw-r--r--src/options.c46
1 files changed, 23 insertions, 23 deletions
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);
20static int opt_getboolean(lua_State *L, p_socket ps, int level, int name); 20static int opt_getboolean(lua_State *L, p_socket ps, int level, int name);
21static int opt_setint(lua_State *L, p_socket ps, int level, int name); 21static int opt_setint(lua_State *L, p_socket ps, int level, int name);
22static int opt_getint(lua_State *L, p_socket ps, int level, int name); 22static int opt_getint(lua_State *L, p_socket ps, int level, int name);
23static int opt_set(lua_State *L, p_socket ps, int level, int name, 23static int opt_set(lua_State *L, p_socket ps, int level, int name,
24 void *val, int len); 24 void *val, int len);
25static int opt_get(lua_State *L, p_socket ps, int level, int name, 25static 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 */
61int opt_set_reuseaddr(lua_State *L, p_socket ps) 61int 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
66int opt_get_reuseaddr(lua_State *L, p_socket ps) 66int 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 */
72int opt_set_reuseport(lua_State *L, p_socket ps) 72int 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
77int opt_get_reuseport(lua_State *L, p_socket ps) 77int 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 */
83int opt_set_tcp_nodelay(lua_State *L, p_socket ps) 83int 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
88int opt_get_tcp_nodelay(lua_State *L, p_socket ps) 88int 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
93int opt_set_keepalive(lua_State *L, p_socket ps) 93int 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
98int opt_get_keepalive(lua_State *L, p_socket ps) 98int 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
103int opt_set_dontroute(lua_State *L, p_socket ps) 103int 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
294static 294static
295int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) 295int 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
307static 307static
308int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) 308int 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) {