2004-06-16 03:02:07 +02:00
|
|
|
/*=========================================================================*\
|
2015-08-21 20:39:34 +02:00
|
|
|
* Common option interface
|
2004-06-16 03:02:07 +02:00
|
|
|
* LuaSocket toolkit
|
|
|
|
\*=========================================================================*/
|
2015-08-21 20:39:34 +02:00
|
|
|
#include <string.h>
|
2004-01-21 19:40:52 +01:00
|
|
|
|
2005-09-29 08:11:42 +02:00
|
|
|
#include "lauxlib.h"
|
|
|
|
|
2004-01-21 19:40:52 +01:00
|
|
|
#include "auxiliar.h"
|
|
|
|
#include "options.h"
|
2004-01-21 21:16:48 +01:00
|
|
|
#include "inet.h"
|
2004-01-21 19:40:52 +01:00
|
|
|
|
2013-05-30 10:20:34 +02:00
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Internal functions prototypes
|
|
|
|
\*=========================================================================*/
|
2005-10-07 06:40:59 +02:00
|
|
|
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
|
2013-05-30 10:20:34 +02:00
|
|
|
static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name);
|
2005-10-07 06:40:59 +02:00
|
|
|
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
|
2009-05-27 11:31:38 +02:00
|
|
|
static int opt_getboolean(lua_State *L, p_socket ps, int level, int name);
|
2013-05-30 10:20:34 +02:00
|
|
|
static int opt_setint(lua_State *L, p_socket ps, int level, int name);
|
|
|
|
static int opt_getint(lua_State *L, p_socket ps, int level, int name);
|
2015-08-21 20:39:34 +02:00
|
|
|
static int opt_set(lua_State *L, p_socket ps, int level, int name,
|
2004-01-21 19:40:52 +01:00
|
|
|
void *val, int len);
|
2015-08-21 20:39:34 +02:00
|
|
|
static int opt_get(lua_State *L, p_socket ps, int level, int name,
|
2012-02-27 22:26:23 +01:00
|
|
|
void *val, int* len);
|
2004-01-21 19:40:52 +01:00
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Exported functions
|
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Calls appropriate option handler
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2005-10-07 06:40:59 +02:00
|
|
|
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
|
|
|
while (opt->name && strcmp(name, opt->name))
|
|
|
|
opt++;
|
|
|
|
if (!opt->func) {
|
2017-12-23 00:57:20 +01:00
|
|
|
char msg[57];
|
2004-01-21 19:40:52 +01:00
|
|
|
sprintf(msg, "unsupported option `%.35s'", name);
|
|
|
|
luaL_argerror(L, 2, msg);
|
|
|
|
}
|
|
|
|
return opt->func(L, ps);
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
|
|
|
|
{
|
|
|
|
const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
|
|
|
|
while (opt->name && strcmp(name, opt->name))
|
|
|
|
opt++;
|
|
|
|
if (!opt->func) {
|
2017-12-23 00:57:20 +01:00
|
|
|
char msg[57];
|
2009-05-27 11:31:38 +02:00
|
|
|
sprintf(msg, "unsupported option `%.35s'", name);
|
|
|
|
luaL_argerror(L, 2, msg);
|
|
|
|
}
|
|
|
|
return opt->func(L, ps);
|
|
|
|
}
|
|
|
|
|
2004-01-21 19:40:52 +01:00
|
|
|
/* enables reuse of local address */
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_reuseaddr(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
2004-01-21 19:40:52 +01:00
|
|
|
}
|
|
|
|
|
2012-02-27 22:26:23 +01:00
|
|
|
int opt_get_reuseaddr(lua_State *L, p_socket ps)
|
|
|
|
{
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
|
2012-02-27 22:26:23 +01:00
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
/* enables reuse of local port */
|
|
|
|
int opt_set_reuseport(lua_State *L, p_socket ps)
|
|
|
|
{
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
2009-05-27 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-11 23:18:20 +02:00
|
|
|
int opt_get_reuseport(lua_State *L, p_socket ps)
|
|
|
|
{
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
|
2012-04-11 23:18:20 +02:00
|
|
|
}
|
|
|
|
|
2004-01-21 19:40:52 +01:00
|
|
|
/* disables the Naggle algorithm */
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
2004-01-21 19:40:52 +01:00
|
|
|
}
|
|
|
|
|
2012-02-27 22:26:23 +01:00
|
|
|
int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:17:50 +01:00
|
|
|
#ifdef TCP_KEEPIDLE
|
|
|
|
int opt_get_tcp_keepidle(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_set_tcp_keepidle(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TCP_KEEPCNT
|
|
|
|
int opt_get_tcp_keepcnt(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_set_tcp_keepcnt(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TCP_KEEPINTVL
|
|
|
|
int opt_get_tcp_keepintvl(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_set_tcp_keepintvl(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_keepalive(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
2004-01-21 19:40:52 +01:00
|
|
|
}
|
|
|
|
|
2012-02-27 22:26:23 +01:00
|
|
|
int opt_get_keepalive(lua_State *L, p_socket ps)
|
|
|
|
{
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
|
2012-02-27 22:26:23 +01:00
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_dontroute(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:02:20 +02:00
|
|
|
int opt_get_dontroute(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_broadcast(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
|
|
|
}
|
|
|
|
|
2018-03-25 08:58:10 +02:00
|
|
|
int opt_set_recv_buf_size(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_get_recv_buf_size(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_get_send_buf_size(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_set_send_buf_size(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF);
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:02:20 +02:00
|
|
|
int opt_get_broadcast(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
|
|
|
|
}
|
|
|
|
|
2013-05-30 10:20:34 +02:00
|
|
|
int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
|
|
|
|
}
|
|
|
|
|
2013-05-30 10:20:34 +02:00
|
|
|
int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_linger(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
struct linger li; /* obj, name, table */
|
2012-04-16 14:41:48 +02:00
|
|
|
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
2004-01-21 19:40:52 +01:00
|
|
|
lua_pushstring(L, "on");
|
|
|
|
lua_gettable(L, 3);
|
2015-08-21 20:39:34 +02:00
|
|
|
if (!lua_isboolean(L, -1))
|
2004-01-21 19:40:52 +01:00
|
|
|
luaL_argerror(L, 3, "boolean 'on' field expected");
|
2005-11-20 08:20:26 +01:00
|
|
|
li.l_onoff = (u_short) lua_toboolean(L, -1);
|
2004-01-21 19:40:52 +01:00
|
|
|
lua_pushstring(L, "timeout");
|
|
|
|
lua_gettable(L, 3);
|
2015-08-21 20:39:34 +02:00
|
|
|
if (!lua_isnumber(L, -1))
|
2004-01-21 19:40:52 +01:00
|
|
|
luaL_argerror(L, 3, "number 'timeout' field expected");
|
2005-11-20 08:20:26 +01:00
|
|
|
li.l_linger = (u_short) lua_tonumber(L, -1);
|
2004-01-21 19:40:52 +01:00
|
|
|
return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
|
|
|
|
}
|
|
|
|
|
2012-02-27 22:26:23 +01:00
|
|
|
int opt_get_linger(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
struct linger li; /* obj, name */
|
|
|
|
int len = sizeof(li);
|
|
|
|
int err = opt_get(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, &len);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushboolean(L, li.l_onoff);
|
|
|
|
lua_setfield(L, -2, "on");
|
|
|
|
lua_pushinteger(L, li.l_linger);
|
|
|
|
lua_setfield(L, -2, "timeout");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
2013-05-30 10:20:34 +02:00
|
|
|
return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
|
2009-05-27 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
const char *address = luaL_checkstring(L, 3); /* obj, name, ip */
|
|
|
|
struct in_addr val;
|
|
|
|
val.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (strcmp(address, "*") && !inet_aton(address, &val))
|
|
|
|
luaL_argerror(L, 3, "ip expected");
|
2015-08-21 20:39:34 +02:00
|
|
|
return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF,
|
2009-05-27 11:31:38 +02:00
|
|
|
(char *) &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
struct in_addr val;
|
|
|
|
socklen_t len = sizeof(val);
|
|
|
|
if (getsockopt(*ps, IPPROTO_IP, IP_MULTICAST_IF, (char *) &val, &len) < 0) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushstring(L, "getsockopt failed");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
lua_pushstring(L, inet_ntoa(val));
|
|
|
|
return 1;
|
2004-01-21 19:40:52 +01:00
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_ip_add_membership(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
|
|
|
|
}
|
|
|
|
|
2013-05-30 10:20:34 +02:00
|
|
|
int opt_set_ip6_add_membership(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
int opt_get_ip6_v6only(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
|
|
|
|
}
|
|
|
|
|
2011-06-14 02:47:43 +02:00
|
|
|
int opt_set_ip6_v6only(lua_State *L, p_socket ps)
|
|
|
|
{
|
|
|
|
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
|
|
|
|
}
|
|
|
|
|
2004-01-21 19:40:52 +01:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Auxiliar functions
|
|
|
|
\*=========================================================================*/
|
2005-10-07 06:40:59 +02:00
|
|
|
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
struct ip_mreq val; /* obj, name, table */
|
2012-04-16 14:41:48 +02:00
|
|
|
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
2004-01-21 19:40:52 +01:00
|
|
|
lua_pushstring(L, "multiaddr");
|
|
|
|
lua_gettable(L, 3);
|
2015-08-21 20:39:34 +02:00
|
|
|
if (!lua_isstring(L, -1))
|
2004-01-21 19:40:52 +01:00
|
|
|
luaL_argerror(L, 3, "string 'multiaddr' field expected");
|
2015-08-21 20:39:34 +02:00
|
|
|
if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr))
|
2004-01-21 19:40:52 +01:00
|
|
|
luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
|
|
|
|
lua_pushstring(L, "interface");
|
|
|
|
lua_gettable(L, 3);
|
2015-08-21 20:39:34 +02:00
|
|
|
if (!lua_isstring(L, -1))
|
2004-01-21 19:40:52 +01:00
|
|
|
luaL_argerror(L, 3, "string 'interface' field expected");
|
|
|
|
val.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (strcmp(lua_tostring(L, -1), "*") &&
|
2015-08-21 20:39:34 +02:00
|
|
|
!inet_aton(lua_tostring(L, -1), &val.imr_interface))
|
2004-01-21 19:40:52 +01:00
|
|
|
luaL_argerror(L, 3, "invalid 'interface' ip address");
|
|
|
|
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
2013-05-30 10:20:34 +02:00
|
|
|
static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name)
|
|
|
|
{
|
|
|
|
struct ipv6_mreq val; /* obj, opt-name, table */
|
|
|
|
memset(&val, 0, sizeof(val));
|
|
|
|
if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
|
|
|
|
lua_pushstring(L, "multiaddr");
|
|
|
|
lua_gettable(L, 3);
|
2015-08-21 20:39:34 +02:00
|
|
|
if (!lua_isstring(L, -1))
|
2013-05-30 10:20:34 +02:00
|
|
|
luaL_argerror(L, 3, "string 'multiaddr' field expected");
|
2015-08-21 20:39:34 +02:00
|
|
|
if (!inet_pton(AF_INET6, lua_tostring(L, -1), &val.ipv6mr_multiaddr))
|
2013-05-30 10:20:34 +02:00
|
|
|
luaL_argerror(L, 3, "invalid 'multiaddr' ip address");
|
|
|
|
lua_pushstring(L, "interface");
|
|
|
|
lua_gettable(L, 3);
|
|
|
|
/* By default we listen to interface on default route
|
2015-08-21 20:39:34 +02:00
|
|
|
* (sigh). However, interface= can override it. We should
|
2013-05-30 10:50:28 +02:00
|
|
|
* support either number, or name for it. Waiting for
|
|
|
|
* windows port of if_nametoindex */
|
2013-05-30 10:20:34 +02:00
|
|
|
if (!lua_isnil(L, -1)) {
|
|
|
|
if (lua_isnumber(L, -1)) {
|
2013-05-30 10:50:28 +02:00
|
|
|
val.ipv6mr_interface = (unsigned int) lua_tonumber(L, -1);
|
2013-05-30 10:20:34 +02:00
|
|
|
} else
|
2013-05-30 10:50:28 +02:00
|
|
|
luaL_argerror(L, -1, "number 'interface' field expected");
|
2013-05-30 10:20:34 +02:00
|
|
|
}
|
|
|
|
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
2015-08-21 20:39:34 +02:00
|
|
|
static
|
2012-02-27 22:26:23 +01:00
|
|
|
int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len)
|
|
|
|
{
|
|
|
|
socklen_t socklen = *len;
|
|
|
|
if (getsockopt(*ps, level, name, (char *) val, &socklen) < 0) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushstring(L, "getsockopt failed");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
*len = socklen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-21 20:39:34 +02:00
|
|
|
static
|
2005-10-07 06:40:59 +02:00
|
|
|
int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
|
|
|
if (setsockopt(*ps, level, name, (char *) val, len) < 0) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushstring(L, "setsockopt failed");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
lua_pushnumber(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-27 11:31:38 +02:00
|
|
|
static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
|
|
|
|
{
|
|
|
|
int val = 0;
|
2012-02-27 22:26:23 +01:00
|
|
|
int len = sizeof(val);
|
|
|
|
int err = opt_get(L, ps, level, name, (char *) &val, &len);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2009-05-27 11:31:38 +02:00
|
|
|
lua_pushboolean(L, val);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-29 12:33:27 +02:00
|
|
|
int opt_get_error(lua_State *L, p_socket ps)
|
|
|
|
{
|
2013-05-30 10:20:34 +02:00
|
|
|
int val = 0;
|
|
|
|
socklen_t len = sizeof(val);
|
|
|
|
if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushstring(L, "getsockopt failed");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
lua_pushstring(L, socket_strerror(val));
|
|
|
|
return 1;
|
2013-05-29 12:33:27 +02:00
|
|
|
}
|
|
|
|
|
2005-10-07 06:40:59 +02:00
|
|
|
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
|
2004-01-21 19:40:52 +01:00
|
|
|
{
|
2005-10-07 06:40:59 +02:00
|
|
|
int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
|
2004-01-21 19:40:52 +01:00
|
|
|
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
2013-05-30 10:20:34 +02:00
|
|
|
static int opt_getint(lua_State *L, p_socket ps, int level, int name)
|
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
int len = sizeof(val);
|
|
|
|
int err = opt_get(L, ps, level, name, (char *) &val, &len);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
lua_pushnumber(L, val);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opt_setint(lua_State *L, p_socket ps, int level, int name)
|
|
|
|
{
|
|
|
|
int val = (int) lua_tonumber(L, 3); /* obj, name, int */
|
|
|
|
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
|
|
|
|
}
|