aboutsummaryrefslogtreecommitdiff
path: root/src/udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/udp.c')
-rw-r--r--src/udp.c99
1 files changed, 5 insertions, 94 deletions
diff --git a/src/udp.c b/src/udp.c
index c5e14d1..115e749 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -14,6 +14,7 @@
14#include "auxiliar.h" 14#include "auxiliar.h"
15#include "socket.h" 15#include "socket.h"
16#include "inet.h" 16#include "inet.h"
17#include "options.h"
17#include "udp.h" 18#include "udp.h"
18 19
19/*=========================================================================*\ 20/*=========================================================================*\
@@ -34,13 +35,6 @@ static int meth_setoption(lua_State *L);
34static int meth_settimeout(lua_State *L); 35static int meth_settimeout(lua_State *L);
35static int meth_fd(lua_State *L); 36static int meth_fd(lua_State *L);
36static int meth_dirty(lua_State *L); 37static int meth_dirty(lua_State *L);
37static int opt_dontroute(lua_State *L);
38static int opt_broadcast(lua_State *L);
39static int opt_reuseaddr(lua_State *L);
40static int opt_ip_multicast_ttl(lua_State *L);
41static int opt_ip_multicast_loop(lua_State *L);
42static int opt_ip_add_membership(lua_State *L);
43static int opt_ip_drop_membersip(lua_State *L);
44 38
45/* udp object methods */ 39/* udp object methods */
46static luaL_reg udp[] = { 40static luaL_reg udp[] = {
@@ -63,7 +57,7 @@ static luaL_reg udp[] = {
63}; 57};
64 58
65/* socket options */ 59/* socket options */
66static luaL_reg opt[] = { 60static t_opt opt[] = {
67 {"dontroute", opt_dontroute}, 61 {"dontroute", opt_dontroute},
68 {"broadcast", opt_broadcast}, 62 {"broadcast", opt_broadcast},
69 {"reuseaddr", opt_reuseaddr}, 63 {"reuseaddr", opt_reuseaddr},
@@ -231,95 +225,12 @@ static int meth_getsockname(lua_State *L)
231} 225}
232 226
233/*-------------------------------------------------------------------------*\ 227/*-------------------------------------------------------------------------*\
234* Option handlers 228* Just call option handler
235\*-------------------------------------------------------------------------*/ 229\*-------------------------------------------------------------------------*/
236static int meth_setoption(lua_State *L) 230static int meth_setoption(lua_State *L)
237{ 231{
238 return aux_meth_setoption(L, opt);
239}
240
241static int opt_boolean(lua_State *L, int level, int name)
242{
243 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
244 int val = aux_checkboolean(L, 2);
245 if (setsockopt(udp->sock, level, name, (char *) &val, sizeof(val)) < 0) {
246 lua_pushnil(L);
247 lua_pushstring(L, "setsockopt failed");
248 return 2;
249 }
250 lua_pushnumber(L, 1);
251 return 1;
252}
253
254static int opt_dontroute(lua_State *L)
255{
256 return opt_boolean(L, SOL_SOCKET, SO_DONTROUTE);
257}
258
259static int opt_reuseaddr(lua_State *L)
260{
261 return opt_boolean(L, SOL_SOCKET, SO_REUSEADDR);
262}
263
264static int opt_broadcast(lua_State *L)
265{
266 return opt_boolean(L, SOL_SOCKET, SO_BROADCAST);
267}
268
269static int opt_ip_multicast_loop(lua_State *L)
270{
271 return opt_boolean(L, IPPROTO_IP, IP_MULTICAST_LOOP);
272}
273
274static int opt_ip_multicast_ttl(lua_State *L)
275{
276 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
277 int val = (int) luaL_checknumber(L, 2);
278 if (setsockopt(udp->sock, IPPROTO_IP, IP_MULTICAST_TTL,
279 (char *) &val, sizeof(val)) < 0) {
280 lua_pushnil(L);
281 lua_pushstring(L, "setsockopt failed");
282 return 2;
283 }
284 lua_pushnumber(L, 1);
285 return 1;
286}
287
288static int opt_membership(lua_State *L, int level, int name)
289{
290 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 232 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
291 struct ip_mreq val; 233 return opt_meth_setoption(L, opt, &udp->sock);
292 if (!lua_istable(L, 2))
293 luaL_typerror(L, 2, lua_typename(L, LUA_TTABLE));
294 lua_pushstring(L, "multiaddr");
295 lua_gettable(L, 2);
296 if (!lua_isstring(L, -1)) luaL_argerror(L, 2, "invalid 'group' field");
297 if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr))
298 luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
299 lua_pushstring(L, "interface");
300 lua_gettable(L, 2);
301 if (!lua_isstring(L, -1)) luaL_argerror(L, 2, "invalid 'interface' field");
302 val.imr_interface.s_addr = htonl(INADDR_ANY);
303 if (strcmp(lua_tostring(L, -1), "*") &&
304 !inet_aton(lua_tostring(L, -1), &val.imr_interface))
305 luaL_argerror(L, 3, "invalid 'interface' ip address");
306 if (setsockopt(udp->sock, level, name, (char *) &val, sizeof(val)) < 0) {
307 lua_pushnil(L);
308 lua_pushstring(L, "setsockopt failed");
309 return 2;
310 }
311 lua_pushnumber(L, 1);
312 return 1;
313}
314
315static int opt_ip_add_membership(lua_State *L)
316{
317 return opt_membership(L, IPPROTO_IP, IP_ADD_MEMBERSHIP);
318}
319
320static int opt_ip_drop_membersip(lua_State *L)
321{
322 return opt_membership(L, IPPROTO_IP, IP_DROP_MEMBERSHIP);
323} 234}
324 235
325/*-------------------------------------------------------------------------*\ 236/*-------------------------------------------------------------------------*\
@@ -343,7 +254,7 @@ static int meth_setpeername(lua_State *L)
343 unsigned short port = connecting ? 254 unsigned short port = connecting ?
344 (unsigned short) luaL_checknumber(L, 3) : 255 (unsigned short) luaL_checknumber(L, 3) :
345 (unsigned short) luaL_optnumber(L, 3, 0); 256 (unsigned short) luaL_optnumber(L, 3, 0);
346 const char *err = inet_tryconnect(&udp->sock, tm, address, port); 257 const char *err = inet_tryconnect(&udp->sock, address, port, tm);
347 if (err) { 258 if (err) {
348 lua_pushnil(L); 259 lua_pushnil(L);
349 lua_pushstring(L, err); 260 lua_pushstring(L, err);