Leaving if in src/ but out of build for now.

This commit is contained in:
Diego Nehab 2013-05-30 16:20:34 +08:00
parent 5341131cd0
commit a233e27865
8 changed files with 266 additions and 197 deletions

11
README
View File

@ -1,6 +1,11 @@
This is the LuaSocket 2.1. It has been tested on --[[WinXP--]], Mac OS X, This is the LuaSocket 2.1. It has been tested on Windows 7, Mac OS X,
and --[[Linux--]]. Please use the Lua mailing list to report any bugs and Linux.
(or "features") you encounter.
Please use the project page at GitHub
https://github.com/diegonehab/luasocket
to file bug reports or propose changes.
Have fun, Have fun,
Diego Nehab. Diego Nehab.

View File

@ -3,6 +3,7 @@
* LuaSocket toolkit * LuaSocket toolkit
\*=========================================================================*/ \*=========================================================================*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "lua.h" #include "lua.h"
@ -227,7 +228,6 @@ static int inet_global_gethostname(lua_State *L)
} }
} }
/*=========================================================================*\ /*=========================================================================*\
* Lua methods * Lua methods
\*=========================================================================*/ \*=========================================================================*/
@ -236,44 +236,33 @@ static int inet_global_gethostname(lua_State *L)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int inet_meth_getpeername(lua_State *L, p_socket ps, int family) int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
{ {
switch (family) { int err;
case PF_INET: { struct sockaddr_storage peer;
struct sockaddr_in peer; socklen_t peer_len = sizeof(peer);
socklen_t peer_len = sizeof(peer); char name[INET6_ADDRSTRLEN];
char name[INET_ADDRSTRLEN]; char port[6]; /* 65535 = 5 bytes + 0 to terminate it */
if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, socket_strerror(errno)); lua_pushstring(L, socket_strerror(errno));
return 2; return 2;
} else {
inet_ntop(family, &peer.sin_addr, name, sizeof(name));
lua_pushstring(L, name);
lua_pushnumber(L, ntohs(peer.sin_port));
lua_pushliteral(L, "inet");
return 3;
}
}
case PF_INET6: {
struct sockaddr_in6 peer;
socklen_t peer_len = sizeof(peer);
char name[INET6_ADDRSTRLEN];
if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) {
lua_pushnil(L);
lua_pushstring(L, socket_strerror(errno));
return 2;
} else {
inet_ntop(family, &peer.sin6_addr, name, sizeof(name));
lua_pushstring(L, name);
lua_pushnumber(L, ntohs(peer.sin6_port));
lua_pushliteral(L, "inet6");
return 3;
}
}
default:
lua_pushnil(L);
lua_pushfstring(L, "unknown family %d", family);
return 2;
} }
if ((err = getnameinfo((struct sockaddr *) &peer, peer_len,
name, INET6_ADDRSTRLEN,
port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV))) {
lua_pushnil(L);
lua_pushstring(L, gai_strerror(err));
return 2;
}
lua_pushstring(L, name);
lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10));
if (family == PF_INET) {
lua_pushliteral(L, "inet");
} else if (family == PF_INET6) {
lua_pushliteral(L, "inet6");
} else {
lua_pushliteral(L, "uknown family");
}
return 3;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
@ -281,44 +270,33 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int inet_meth_getsockname(lua_State *L, p_socket ps, int family) int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
{ {
switch (family) { int err;
case PF_INET: { struct sockaddr_storage peer;
struct sockaddr_in local; socklen_t peer_len = sizeof(peer);
socklen_t local_len = sizeof(local); char name[INET6_ADDRSTRLEN];
char name[INET_ADDRSTRLEN]; char port[6]; /* 65535 = 5 bytes + 0 to terminate it */
if (getsockname(*ps, (SA *) &local, &local_len) < 0) { if (getsockname(*ps, (SA *) &peer, &peer_len) < 0) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, socket_strerror(errno)); lua_pushstring(L, socket_strerror(errno));
return 2; return 2;
} else {
inet_ntop(family, &local.sin_addr, name, sizeof(name));
lua_pushstring(L, name);
lua_pushnumber(L, ntohs(local.sin_port));
lua_pushliteral(L, "inet");
return 3;
}
}
case PF_INET6: {
struct sockaddr_in6 local;
socklen_t local_len = sizeof(local);
char name[INET6_ADDRSTRLEN];
if (getsockname(*ps, (SA *) &local, &local_len) < 0) {
lua_pushnil(L);
lua_pushstring(L, socket_strerror(errno));
return 2;
} else {
inet_ntop(family, &local.sin6_addr, name, sizeof(name));
lua_pushstring(L, name);
lua_pushnumber(L, ntohs(local.sin6_port));
lua_pushliteral(L, "inet6");
return 3;
}
}
default:
lua_pushnil(L);
lua_pushfstring(L, "unknown family %d", family);
return 2;
} }
if ((err=getnameinfo((struct sockaddr *)&peer, peer_len,
name, INET6_ADDRSTRLEN,
port, 6, NI_NUMERICHOST | NI_NUMERICSERV))) {
lua_pushnil(L);
lua_pushstring(L, gai_strerror(err));
return 2;
}
lua_pushstring(L, name);
lua_pushstring(L, port);
if (family == PF_INET) {
lua_pushliteral(L, "inet");
} else if (family == PF_INET6) {
lua_pushliteral(L, "inet6");
} else {
lua_pushliteral(L, "uknown family");
}
return 3;
} }
/*=========================================================================*\ /*=========================================================================*\
@ -456,7 +434,8 @@ const char *inet_tryaccept(p_socket server, int family, p_socket client,
} else { } else {
len = sizeof(struct sockaddr_in); len = sizeof(struct sockaddr_in);
} }
return socket_strerror(socket_accept(server, client, (SA *) &addr, &len, tm)); return socket_strerror(socket_accept(server, client, (SA *) &addr,
&len, tm));
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\

View File

@ -30,7 +30,7 @@ DEBUG?=NODEBUG
COMPAT?=NOCOMPAT COMPAT?=NOCOMPAT
# where lua headers are found for macosx builds # where lua headers are found for macosx builds
# LUAINC_macosx: # LUAINC_macosx:
# /opt/local/include # /opt/local/include
LUAINC_macosx_base?=/opt/local/include LUAINC_macosx_base?=/opt/local/include
LUAINC_macosx?=$(LUAINC_macosx_base)/lua/$(LUAV) LUAINC_macosx?=$(LUAINC_macosx_base)/lua/$(LUAV)
@ -41,9 +41,9 @@ CDIR_macosx?=lib/lua/$(LUAV)
LDIR_macosx?=share/lua/$(LUAV) LDIR_macosx?=share/lua/$(LUAV)
# LUAINC_linux: # LUAINC_linux:
# /usr/include/lua$(LUAV) # /usr/include/lua$(LUAV)
# /usr/local/include # /usr/local/include
# /usr/local/include/lua$(LUAV) # /usr/local/include/lua$(LUAV)
# where lua headers are found for linux builds # where lua headers are found for linux builds
LUAINC_linux_base?=/usr/include LUAINC_linux_base?=/usr/include
@ -134,7 +134,7 @@ DEF_macosx= -DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN -DLUA_$(COMPAT)_MODULE \
-DMIME_API='__attribute__((visibility("default")))' -DMIME_API='__attribute__((visibility("default")))'
CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \
-fvisibility=hidden -fvisibility=hidden
LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o
LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
SOCKET_macosx=usocket.o SOCKET_macosx=usocket.o
@ -308,15 +308,15 @@ none:
all: $(SOCKET_SO) $(MIME_SO) all: $(SOCKET_SO) $(MIME_SO)
$(SOCKET_SO): $(SOCKET_OBJS) $(SOCKET_SO): $(SOCKET_OBJS)
$(LD) $(SOCKET_OBJS) $(LDFLAGS)$@ $(LD) $(SOCKET_OBJS) $(LDFLAGS)$@
$(MIME_SO): $(MIME_OBJS) $(MIME_SO): $(MIME_OBJS)
$(LD) $(MIME_OBJS) $(LDFLAGS)$@ $(LD) $(MIME_OBJS) $(LDFLAGS)$@
all-unix: all $(UNIX_SO) $(SERIAL_SO) all-unix: all $(UNIX_SO) $(SERIAL_SO)
$(UNIX_SO): $(UNIX_OBJS) $(UNIX_SO): $(UNIX_OBJS)
$(LD) $(UNIX_OBJS) $(LDFLAGS)$@ $(LD) $(UNIX_OBJS) $(LDFLAGS)$@
$(SERIAL_SO): $(SERIAL_OBJS) $(SERIAL_SO): $(SERIAL_OBJS)
$(LD) $(SERIAL_OBJS) $(LDFLAGS)$@ $(LD) $(SERIAL_OBJS) $(LDFLAGS)$@

View File

@ -3,6 +3,9 @@
* LuaSocket toolkit * LuaSocket toolkit
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include "lauxlib.h" #include "lauxlib.h"
@ -10,12 +13,30 @@
#include "options.h" #include "options.h"
#include "inet.h" #include "inet.h"
/* Some platforms use IPV6_JOIN_GROUP instead if
* IPV6_ADD_MEMBERSHIP. The semantics are same, though. */
#ifndef IPV6_ADD_MEMBERSHIP
#ifdef IPV6_JOIN_GROUP
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#endif /* IPV6_JOIN_GROUP */
#endif /* !IPV6_ADD_MEMBERSHIP */
/* Same with IPV6_DROP_MEMBERSHIP / IPV6_LEAVE_GROUP. */
#ifndef IPV6_DROP_MEMBERSHIP
#ifdef IPV6_LEAVE_GROUP
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
#endif /* IPV6_LEAVE_GROUP */
#endif /* !IPV6_DROP_MEMBERSHIP */
/*=========================================================================*\ /*=========================================================================*\
* Internal functions prototypes * Internal functions prototypes
\*=========================================================================*/ \*=========================================================================*/
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name);
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
static int opt_getboolean(lua_State *L, p_socket ps, int level, int name); static int opt_getboolean(lua_State *L, p_socket ps, int level, int name);
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);
static int opt_set(lua_State *L, p_socket ps, int level, int name, static int opt_set(lua_State *L, p_socket ps, int level, int name,
void *val, int len); void *val, int len);
static int opt_get(lua_State *L, p_socket ps, int level, int name, static int opt_get(lua_State *L, p_socket ps, int level, int name,
@ -106,6 +127,26 @@ int opt_set_broadcast(lua_State *L, p_socket ps)
return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
} }
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);
}
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps) int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
{ {
return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
@ -116,6 +157,16 @@ int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
} }
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);
}
int opt_set_linger(lua_State *L, p_socket ps) int opt_set_linger(lua_State *L, p_socket ps)
{ {
struct linger li; /* obj, name, table */ struct linger li; /* obj, name, table */
@ -150,9 +201,7 @@ int opt_get_linger(lua_State *L, p_socket ps)
int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps) int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
{ {
int val = (int) luaL_checknumber(L, 3); /* obj, name, int */ return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_TTL,
(char *) &val, sizeof(val));
} }
int opt_set_ip_multicast_if(lua_State *L, p_socket ps) int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
@ -189,6 +238,21 @@ int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
} }
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);
}
int opt_set_ip6_v6only(lua_State *L, p_socket ps) int opt_set_ip6_v6only(lua_State *L, p_socket ps)
{ {
return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
@ -218,6 +282,37 @@ static int opt_setmembership(lua_State *L, p_socket ps, int level, int name)
return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
} }
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);
if (!lua_isstring(L, -1))
luaL_argerror(L, 3, "string 'multiaddr' field expected");
if (!inet_pton(AF_INET6, lua_tostring(L, -1), &val.ipv6mr_multiaddr))
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
* (sigh). However, interface= can override it. We support either
* number, or name for it. */
if (!lua_isnil(L, -1)) {
if (lua_isnumber(L, -1)) {
val.ipv6mr_interface = lua_tonumber(L, -1);
} else if (lua_isstring(L, -1)) {
if (!(val.ipv6mr_interface = if_nametoindex(lua_tostring(L, -1)))) {
lua_pushnil(L);
lua_pushstring(L, "nonexistent interface");
return 2;
}
} else
luaL_argerror(L, -1, "number/string 'interface' field expected");
}
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
}
static static
int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len)
{ {
@ -256,15 +351,15 @@ static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
int opt_get_error(lua_State *L, p_socket ps) int opt_get_error(lua_State *L, p_socket ps)
{ {
int val = 0; int val = 0;
socklen_t len = sizeof(val); socklen_t len = sizeof(val);
if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) { if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, "getsockopt failed"); lua_pushstring(L, "getsockopt failed");
return 2; return 2;
} }
lua_pushstring(L, socket_strerror(val)); lua_pushstring(L, socket_strerror(val));
return 1; return 1;
} }
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
@ -273,3 +368,19 @@ static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
} }
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));
}

View File

@ -32,6 +32,11 @@ int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps);
int opt_set_ip_multicast_loop(lua_State *L, p_socket ps); int opt_set_ip_multicast_loop(lua_State *L, p_socket ps);
int opt_set_ip_add_membership(lua_State *L, p_socket ps); int opt_set_ip_add_membership(lua_State *L, p_socket ps);
int opt_set_ip_drop_membersip(lua_State *L, p_socket ps); int opt_set_ip_drop_membersip(lua_State *L, p_socket ps);
int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps);
int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps);
int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps);
int opt_set_ip6_add_membership(lua_State *L, p_socket ps);
int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps);
int opt_set_ip6_v6only(lua_State *L, p_socket ps); int opt_set_ip6_v6only(lua_State *L, p_socket ps);
/* supported options for getoption */ /* supported options for getoption */
@ -43,6 +48,10 @@ int opt_get_reuseaddr(lua_State *L, p_socket ps);
int opt_get_ip_multicast_loop(lua_State *L, p_socket ps); int opt_get_ip_multicast_loop(lua_State *L, p_socket ps);
int opt_get_ip_multicast_if(lua_State *L, p_socket ps); int opt_get_ip_multicast_if(lua_State *L, p_socket ps);
int opt_get_error(lua_State *L, p_socket ps); int opt_get_error(lua_State *L, p_socket ps);
int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps);
int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps);
int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps);
int opt_get_ip6_v6only(lua_State *L, p_socket ps);
/* invokes the appropriate option handler */ /* invokes the appropriate option handler */
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps); int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);

View File

@ -73,6 +73,7 @@ LUASOCKET_API int luaopen_socket_serial(lua_State *L) {
auxiliar_add2group(L, "serial{client}", "serial{any}"); auxiliar_add2group(L, "serial{client}", "serial{any}");
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
lua_pushcfunction(L, global_create); lua_pushcfunction(L, global_create);
(void) func;
#else #else
/* set function into socket namespace */ /* set function into socket namespace */
luaL_openlib(L, "socket", func, 0); luaL_openlib(L, "socket", func, 0);

159
src/udp.c
View File

@ -3,6 +3,7 @@
* LuaSocket toolkit * LuaSocket toolkit
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
@ -68,25 +69,34 @@ static luaL_Reg udp_methods[] = {
/* socket options for setoption */ /* socket options for setoption */
static t_opt optset[] = { static t_opt optset[] = {
{"dontroute", opt_set_dontroute}, {"dontroute", opt_set_dontroute},
{"broadcast", opt_set_broadcast}, {"broadcast", opt_set_broadcast},
{"reuseaddr", opt_set_reuseaddr}, {"reuseaddr", opt_set_reuseaddr},
{"reuseport", opt_set_reuseport}, {"reuseport", opt_set_reuseport},
{"ip-multicast-if", opt_set_ip_multicast_if}, {"ip-multicast-if", opt_set_ip_multicast_if},
{"ip-multicast-ttl", opt_set_ip_multicast_ttl}, {"ip-multicast-ttl", opt_set_ip_multicast_ttl},
{"ip-multicast-loop", opt_set_ip_multicast_loop}, {"ip-multicast-loop", opt_set_ip_multicast_loop},
{"ip-add-membership", opt_set_ip_add_membership}, {"ip-add-membership", opt_set_ip_add_membership},
{"ip-drop-membership", opt_set_ip_drop_membersip}, {"ip-drop-membership", opt_set_ip_drop_membersip},
{"ipv6-v6only", opt_set_ip6_v6only}, {"ipv6-unicast-hops", opt_set_ip6_unicast_hops},
{NULL, NULL} {"ipv6-multicast-hops", opt_set_ip6_unicast_hops},
{"ipv6-multicast-loop", opt_set_ip6_multicast_loop},
{"ipv6-add-membership", opt_set_ip6_add_membership},
{"ipv6-drop-membership", opt_set_ip6_drop_membersip},
{"ipv6-v6only", opt_set_ip6_v6only},
{NULL, NULL}
}; };
/* socket options for getoption */ /* socket options for getoption */
static t_opt optget[] = { static t_opt optget[] = {
{"ip-multicast-if", opt_get_ip_multicast_if}, {"ip-multicast-if", opt_get_ip_multicast_if},
{"ip-multicast-loop", opt_get_ip_multicast_loop}, {"ip-multicast-loop", opt_get_ip_multicast_loop},
{"error", opt_get_error}, {"error", opt_get_error},
{NULL, NULL} {"ipv6-unicast-hops", opt_get_ip6_unicast_hops},
{"ipv6-multicast-hops", opt_get_ip6_unicast_hops},
{"ipv6-multicast-loop", opt_get_ip6_multicast_loop},
{"ipv6-v6only", opt_get_ip6_v6only},
{NULL, NULL}
}; };
/* functions in library namespace */ /* functions in library namespace */
@ -156,39 +166,24 @@ static int meth_sendto(lua_State *L) {
size_t count, sent = 0; size_t count, sent = 0;
const char *data = luaL_checklstring(L, 2, &count); const char *data = luaL_checklstring(L, 2, &count);
const char *ip = luaL_checkstring(L, 3); const char *ip = luaL_checkstring(L, 3);
unsigned short port = (unsigned short) luaL_checknumber(L, 4); const char *port = luaL_checkstring(L, 4);
p_timeout tm = &udp->tm; p_timeout tm = &udp->tm;
int err; int err;
switch (udp->family) { struct addrinfo aihint;
case PF_INET: { struct addrinfo *ai;
struct sockaddr_in addr; memset(&aihint, 0, sizeof(aihint));
memset(&addr, 0, sizeof(addr)); aihint.ai_family = udp->family;
if (inet_pton(AF_INET, ip, &addr.sin_addr) != 1) aihint.ai_socktype = SOCK_DGRAM;
luaL_argerror(L, 3, "invalid ip address"); aihint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
addr.sin_family = AF_INET; if ((err = getaddrinfo(ip, port, &aihint, &ai))) {
addr.sin_port = htons(port);
timeout_markstart(tm);
err = socket_sendto(&udp->sock, data, count, &sent,
(SA *) &addr, sizeof(addr), tm);
break;
}
case PF_INET6: {
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(addr));
if (inet_pton(AF_INET6, ip, &addr.sin6_addr) != 1)
luaL_argerror(L, 3, "invalid ip address");
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
timeout_markstart(tm);
err = socket_sendto(&udp->sock, data, count, &sent,
(SA *) &addr, sizeof(addr), tm);
break;
}
default:
lua_pushnil(L); lua_pushnil(L);
lua_pushfstring(L, "unknown family %d", udp->family); lua_pushstring(L, udp_strerror(err));
return 2; return 2;
} }
timeout_markstart(tm);
err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr,
ai->ai_addrlen, tm);
freeaddrinfo(ai);
if (err != IO_DONE) { if (err != IO_DONE) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, udp_strerror(err)); lua_pushstring(L, udp_strerror(err));
@ -225,71 +220,39 @@ static int meth_receive(lua_State *L) {
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Receives data and sender from a UDP socket * Receives data and sender from a UDP socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_receivefrom(lua_State *L) { static int meth_receivefrom(lua_State *L)
{
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
char buffer[UDP_DATAGRAMSIZE]; char buffer[UDP_DATAGRAMSIZE];
size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
int err; int err;
p_timeout tm = &udp->tm; p_timeout tm = &udp->tm;
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
char addrstr[INET6_ADDRSTRLEN];
char portstr[6];
timeout_markstart(tm); timeout_markstart(tm);
count = MIN(count, sizeof(buffer)); count = MIN(count, sizeof(buffer));
switch (udp->family) { err = socket_recvfrom(&udp->sock, buffer, count, &got, (SA *) &addr,
case PF_INET: { &addr_len, tm);
struct sockaddr_in addr; /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
socklen_t addr_len = sizeof(addr); if (err == IO_CLOSED)
err = socket_recvfrom(&udp->sock, buffer, count, &got, err = IO_DONE;
(SA *) &addr, &addr_len, tm); if (err != IO_DONE) {
/* Unlike TCP, recv() of zero is not closed,
* but a zero-length packet. */
if (err == IO_CLOSED)
err = IO_DONE;
if (err == IO_DONE) {
char addrstr[INET_ADDRSTRLEN];
lua_pushlstring(L, buffer, got);
if (!inet_ntop(AF_INET, &addr.sin_addr,
addrstr, sizeof(addrstr))) {
lua_pushnil(L);
lua_pushstring(L, "invalid source address");
return 2;
}
lua_pushstring(L, addrstr);
lua_pushnumber(L, ntohs(addr.sin_port));
return 3;
}
break;
}
case PF_INET6: {
struct sockaddr_in6 addr;
socklen_t addr_len = sizeof(addr);
err = socket_recvfrom(&udp->sock, buffer, count, &got,
(SA *) &addr, &addr_len, tm);
/* Unlike TCP, recv() of zero is not closed,
* but a zero-length packet. */
if (err == IO_CLOSED)
err = IO_DONE;
if (err == IO_DONE) {
char addrstr[INET6_ADDRSTRLEN];
lua_pushlstring(L, buffer, got);
if (!inet_ntop(AF_INET6, &addr.sin6_addr,
addrstr, sizeof(addrstr))) {
lua_pushnil(L);
lua_pushstring(L, "invalid source address");
return 2;
}
lua_pushstring(L, addrstr);
lua_pushnumber(L, ntohs(addr.sin6_port));
return 3;
}
break;
}
default:
lua_pushnil(L); lua_pushnil(L);
lua_pushfstring(L, "unknown family %d", udp->family); lua_pushstring(L, udp_strerror(err));
return 2; return 2;
} }
lua_pushnil(L); if ((err = getnameinfo((struct sockaddr *)&addr, addr_len, addrstr,
lua_pushstring(L, udp_strerror(err)); INET6_ADDRSTRLEN, portstr, 6, NI_NUMERICHOST | NI_NUMERICSERV))) {
return 2; lua_pushnil(L);
lua_pushstring(L, gai_strerror(err));
return 2;
}
lua_pushlstring(L, buffer, got);
lua_pushstring(L, addrstr);
lua_pushinteger(L, (int) strtol(portstr, (char **) NULL, 10));
return 3;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\

View File

@ -91,6 +91,7 @@ int luaopen_socket_unix(lua_State *L) {
auxiliar_add2group(L, "unix{server}", "unix{any}"); auxiliar_add2group(L, "unix{server}", "unix{any}");
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
lua_pushcfunction(L, global_create); lua_pushcfunction(L, global_create);
(void) func;
#else #else
/* set function into socket namespace */ /* set function into socket namespace */
luaL_openlib(L, "socket", func, 0); luaL_openlib(L, "socket", func, 0);