diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2009-05-27 09:31:38 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2009-05-27 09:31:38 +0000 |
commit | bce60be30fe8e9c1b0eb33128c23c93d7bca5303 (patch) | |
tree | 3927343c777fcb7764a0f2f89754a0ceab141c21 /src/options.c | |
parent | d1a72435d5bd3528f3c334cd4d1da16dcead47bf (diff) | |
download | luasocket-bce60be30fe8e9c1b0eb33128c23c93d7bca5303.tar.gz luasocket-bce60be30fe8e9c1b0eb33128c23c93d7bca5303.tar.bz2 luasocket-bce60be30fe8e9c1b0eb33128c23c93d7bca5303.zip |
Decent makefiles!
Diffstat (limited to 'src/options.c')
-rw-r--r-- | src/options.c | 86 |
1 files changed, 74 insertions, 12 deletions
diff --git a/src/options.c b/src/options.c index a464a4b..1d4c950 100644 --- a/src/options.c +++ b/src/options.c | |||
@@ -12,12 +12,12 @@ | |||
12 | #include "options.h" | 12 | #include "options.h" |
13 | #include "inet.h" | 13 | #include "inet.h" |
14 | 14 | ||
15 | |||
16 | /*=========================================================================*\ | 15 | /*=========================================================================*\ |
17 | * Internal functions prototypes | 16 | * Internal functions prototypes |
18 | \*=========================================================================*/ | 17 | \*=========================================================================*/ |
19 | static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); | 18 | static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); |
20 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); | 19 | 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); | ||
21 | static int opt_set(lua_State *L, p_socket ps, int level, int name, | 21 | static int opt_set(lua_State *L, p_socket ps, int level, int name, |
22 | void *val, int len); | 22 | void *val, int len); |
23 | 23 | ||
@@ -40,39 +40,63 @@ int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps) | |||
40 | return opt->func(L, ps); | 40 | return opt->func(L, ps); |
41 | } | 41 | } |
42 | 42 | ||
43 | int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps) | ||
44 | { | ||
45 | const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ | ||
46 | while (opt->name && strcmp(name, opt->name)) | ||
47 | opt++; | ||
48 | if (!opt->func) { | ||
49 | char msg[45]; | ||
50 | sprintf(msg, "unsupported option `%.35s'", name); | ||
51 | luaL_argerror(L, 2, msg); | ||
52 | } | ||
53 | return opt->func(L, ps); | ||
54 | } | ||
55 | |||
43 | /* enables reuse of local address */ | 56 | /* enables reuse of local address */ |
44 | int opt_reuseaddr(lua_State *L, p_socket ps) | 57 | int opt_set_reuseaddr(lua_State *L, p_socket ps) |
45 | { | 58 | { |
46 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); | 59 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); |
47 | } | 60 | } |
48 | 61 | ||
62 | /* enables reuse of local port */ | ||
63 | int opt_set_reuseport(lua_State *L, p_socket ps) | ||
64 | { | ||
65 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); | ||
66 | } | ||
67 | |||
49 | /* disables the Naggle algorithm */ | 68 | /* disables the Naggle algorithm */ |
50 | int opt_tcp_nodelay(lua_State *L, p_socket ps) | 69 | int opt_set_tcp_nodelay(lua_State *L, p_socket ps) |
51 | { | 70 | { |
52 | return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); | 71 | return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); |
53 | } | 72 | } |
54 | 73 | ||
55 | int opt_keepalive(lua_State *L, p_socket ps) | 74 | int opt_set_keepalive(lua_State *L, p_socket ps) |
56 | { | 75 | { |
57 | return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); | 76 | return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); |
58 | } | 77 | } |
59 | 78 | ||
60 | int opt_dontroute(lua_State *L, p_socket ps) | 79 | int opt_set_dontroute(lua_State *L, p_socket ps) |
61 | { | 80 | { |
62 | return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); | 81 | return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); |
63 | } | 82 | } |
64 | 83 | ||
65 | int opt_broadcast(lua_State *L, p_socket ps) | 84 | int opt_set_broadcast(lua_State *L, p_socket ps) |
66 | { | 85 | { |
67 | return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); | 86 | return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); |
68 | } | 87 | } |
69 | 88 | ||
70 | int opt_ip_multicast_loop(lua_State *L, p_socket ps) | 89 | int opt_set_ip_multicast_loop(lua_State *L, p_socket ps) |
71 | { | 90 | { |
72 | return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); | 91 | return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); |
73 | } | 92 | } |
74 | 93 | ||
75 | int opt_linger(lua_State *L, p_socket ps) | 94 | int opt_get_ip_multicast_loop(lua_State *L, p_socket ps) |
95 | { | ||
96 | return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); | ||
97 | } | ||
98 | |||
99 | int opt_set_linger(lua_State *L, p_socket ps) | ||
76 | { | 100 | { |
77 | struct linger li; /* obj, name, table */ | 101 | struct linger li; /* obj, name, table */ |
78 | if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); | 102 | if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); |
@@ -89,18 +113,43 @@ int opt_linger(lua_State *L, p_socket ps) | |||
89 | return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); | 113 | return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); |
90 | } | 114 | } |
91 | 115 | ||
92 | int opt_ip_multicast_ttl(lua_State *L, p_socket ps) | 116 | int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps) |
93 | { | 117 | { |
94 | int val = (int) luaL_checknumber(L, 3); /* obj, name, int */ | 118 | int val = (int) luaL_checknumber(L, 3); /* obj, name, int */ |
95 | return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &val, sizeof(val)); | 119 | return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_TTL, |
120 | (char *) &val, sizeof(val)); | ||
121 | } | ||
122 | |||
123 | int opt_set_ip_multicast_if(lua_State *L, p_socket ps) | ||
124 | { | ||
125 | const char *address = luaL_checkstring(L, 3); /* obj, name, ip */ | ||
126 | struct in_addr val; | ||
127 | val.s_addr = htonl(INADDR_ANY); | ||
128 | if (strcmp(address, "*") && !inet_aton(address, &val)) | ||
129 | luaL_argerror(L, 3, "ip expected"); | ||
130 | return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF, | ||
131 | (char *) &val, sizeof(val)); | ||
132 | } | ||
133 | |||
134 | int opt_get_ip_multicast_if(lua_State *L, p_socket ps) | ||
135 | { | ||
136 | struct in_addr val; | ||
137 | socklen_t len = sizeof(val); | ||
138 | if (getsockopt(*ps, IPPROTO_IP, IP_MULTICAST_IF, (char *) &val, &len) < 0) { | ||
139 | lua_pushnil(L); | ||
140 | lua_pushstring(L, "getsockopt failed"); | ||
141 | return 2; | ||
142 | } | ||
143 | lua_pushstring(L, inet_ntoa(val)); | ||
144 | return 1; | ||
96 | } | 145 | } |
97 | 146 | ||
98 | int opt_ip_add_membership(lua_State *L, p_socket ps) | 147 | int opt_set_ip_add_membership(lua_State *L, p_socket ps) |
99 | { | 148 | { |
100 | return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); | 149 | return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); |
101 | } | 150 | } |
102 | 151 | ||
103 | int opt_ip_drop_membersip(lua_State *L, p_socket ps) | 152 | int opt_set_ip_drop_membersip(lua_State *L, p_socket ps) |
104 | { | 153 | { |
105 | return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); | 154 | return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); |
106 | } | 155 | } |
@@ -141,6 +190,19 @@ int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) | |||
141 | return 1; | 190 | return 1; |
142 | } | 191 | } |
143 | 192 | ||
193 | static int opt_getboolean(lua_State *L, p_socket ps, int level, int name) | ||
194 | { | ||
195 | int val = 0; | ||
196 | socklen_t len = sizeof(val); | ||
197 | if (getsockopt(*ps, level, name, (char *) &val, &len) < 0) { | ||
198 | lua_pushnil(L); | ||
199 | lua_pushstring(L, "getsockopt failed"); | ||
200 | return 2; | ||
201 | } | ||
202 | lua_pushboolean(L, val); | ||
203 | return 1; | ||
204 | } | ||
205 | |||
144 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) | 206 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) |
145 | { | 207 | { |
146 | int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ | 208 | int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ |