Tested in windows. Still needs more testing, but progress has been made.
This commit is contained in:
parent
89f3ecf782
commit
076451c753
11 changed files with 620 additions and 296 deletions
75
src/udp.c
75
src/udp.c
|
@ -35,6 +35,11 @@ static int meth_fd(lua_State *L);
|
|||
static int meth_dirty(lua_State *L);
|
||||
static int opt_dontroute(lua_State *L);
|
||||
static int opt_broadcast(lua_State *L);
|
||||
static int opt_reuseaddr(lua_State *L);
|
||||
static int opt_ip_multicast_ttl(lua_State *L);
|
||||
static int opt_ip_multicast_loop(lua_State *L);
|
||||
static int opt_ip_add_membership(lua_State *L);
|
||||
static int opt_ip_drop_membersip(lua_State *L);
|
||||
|
||||
/* udp object methods */
|
||||
static luaL_reg udp[] = {
|
||||
|
@ -57,8 +62,13 @@ static luaL_reg udp[] = {
|
|||
|
||||
/* socket options */
|
||||
static luaL_reg opt[] = {
|
||||
{"dontroute", opt_dontroute},
|
||||
{"broadcast", opt_broadcast},
|
||||
{"dontroute", opt_dontroute},
|
||||
{"broadcast", opt_broadcast},
|
||||
{"reuseaddr", opt_reuseaddr},
|
||||
{"ip-multicast-ttl", opt_ip_multicast_ttl},
|
||||
{"ip-multicast-loop", opt_ip_multicast_loop},
|
||||
{"ip-add-membership", opt_ip_add_membership},
|
||||
{"ip-drop-membership", opt_ip_drop_membersip},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
@ -244,11 +254,72 @@ static int opt_dontroute(lua_State *L)
|
|||
return opt_boolean(L, SOL_SOCKET, SO_DONTROUTE);
|
||||
}
|
||||
|
||||
static int opt_reuseaddr(lua_State *L)
|
||||
{
|
||||
return opt_boolean(L, SOL_SOCKET, SO_REUSEADDR);
|
||||
}
|
||||
|
||||
static int opt_broadcast(lua_State *L)
|
||||
{
|
||||
return opt_boolean(L, SOL_SOCKET, SO_BROADCAST);
|
||||
}
|
||||
|
||||
static int opt_ip_multicast_loop(lua_State *L)
|
||||
{
|
||||
return opt_boolean(L, IPPROTO_IP, IP_MULTICAST_LOOP);
|
||||
}
|
||||
|
||||
static int opt_ip_multicast_ttl(lua_State *L)
|
||||
{
|
||||
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
|
||||
int val = (int) luaL_checknumber(L, 2);
|
||||
if (setsockopt(udp->sock, IPPROTO_IP, IP_MULTICAST_TTL,
|
||||
(char *) &val, sizeof(val)) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "setsockopt failed");
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int opt_membership(lua_State *L, int level, int name)
|
||||
{
|
||||
p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
|
||||
struct ip_mreq val;
|
||||
if (!lua_istable(L, 2))
|
||||
luaL_typerror(L, 2, lua_typename(L, LUA_TTABLE));
|
||||
lua_pushstring(L, "multiaddr");
|
||||
lua_gettable(L, 2);
|
||||
if (!lua_isstring(L, -1)) luaL_argerror(L, 2, "invalid 'group' field");
|
||||
if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr))
|
||||
luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
|
||||
lua_pushstring(L, "interface");
|
||||
lua_gettable(L, 2);
|
||||
if (!lua_isstring(L, -1)) luaL_argerror(L, 2, "invalid 'interface' field");
|
||||
val.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
if (strcmp(lua_tostring(L, -1), "*") &&
|
||||
!inet_aton(lua_tostring(L, -1), &val.imr_interface))
|
||||
luaL_argerror(L, 3, "invalid 'interface' ip address");
|
||||
if (setsockopt(udp->sock, level, name, (char *) &val, sizeof(val)) < 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "setsockopt failed");
|
||||
return 2;
|
||||
}
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int opt_ip_add_membership(lua_State *L)
|
||||
{
|
||||
return opt_membership(L, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
||||
}
|
||||
|
||||
static int opt_ip_drop_membersip(lua_State *L)
|
||||
{
|
||||
return opt_membership(L, IPPROTO_IP, IP_DROP_MEMBERSHIP);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Just call tm methods
|
||||
\*-------------------------------------------------------------------------*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue