Merge 9ed54c2eaf
into a4b45edef2
This commit is contained in:
commit
f603b933a6
10 changed files with 523 additions and 182 deletions
113
src/if.c
Normal file
113
src/if.c
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
/*
|
||||||
|
* $Id: if.c $
|
||||||
|
*
|
||||||
|
* Author: Markus Stenberg <fingon@iki.fi>
|
||||||
|
*
|
||||||
|
* Copyright (c) 2012 cisco Systems, Inc.
|
||||||
|
*
|
||||||
|
* Created: Tue Dec 4 14:50:34 2012 mstenber
|
||||||
|
* Last modified: Wed Dec 5 18:51:08 2012 mstenber
|
||||||
|
* Edit time: 24 min
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
|
||||||
|
#include "if.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
|
||||||
|
static int if_global_indextoname(lua_State *L);
|
||||||
|
static int if_global_nametoindex(lua_State *L);
|
||||||
|
static int if_global_nameindex(lua_State *L);
|
||||||
|
|
||||||
|
static luaL_Reg func[] = {
|
||||||
|
{ "indextoname", if_global_indextoname},
|
||||||
|
{ "nametoindex", if_global_nametoindex},
|
||||||
|
{ "nameindex", if_global_nameindex},
|
||||||
|
{ NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
int if_open(lua_State *L)
|
||||||
|
{
|
||||||
|
lua_pushstring(L, "iface");
|
||||||
|
lua_newtable(L);
|
||||||
|
luaL_openlib(L, NULL, func, 0);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int if_global_indextoname(lua_State *L)
|
||||||
|
{
|
||||||
|
unsigned int ifnumber;
|
||||||
|
const char *name;
|
||||||
|
char buf[IF_NAMESIZE+1];
|
||||||
|
|
||||||
|
if (!lua_isnumber(L, 1))
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, "indextoname expects only number argument");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
ifnumber = lua_tonumber(L, 1);
|
||||||
|
if (!(name = if_indextoname(ifnumber, buf)))
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, "nonexistent interface");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
lua_pushstring(L, name);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int if_global_nametoindex(lua_State *L)
|
||||||
|
{
|
||||||
|
unsigned int ifnumber;
|
||||||
|
if (!lua_isstring(L, 1))
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, "nametoindex expects only string argument");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
if (!(ifnumber = if_nametoindex(lua_tostring(L, 1))))
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, "nonexistent interface");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
lua_pushnumber(L, ifnumber);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int if_global_nameindex(lua_State *L)
|
||||||
|
{
|
||||||
|
struct if_nameindex *ni, *oni;
|
||||||
|
int i = 1;
|
||||||
|
oni = ni = if_nameindex();
|
||||||
|
lua_newtable(L);
|
||||||
|
while (ni && ni->if_index && *(ni->if_name))
|
||||||
|
{
|
||||||
|
/* at result[i], we store.. */
|
||||||
|
lua_pushnumber(L, i);
|
||||||
|
|
||||||
|
/* new table with two items - index, name*/
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_pushstring(L, "index");
|
||||||
|
lua_pushnumber(L, ni->if_index);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
|
||||||
|
lua_pushstring(L, "name");
|
||||||
|
lua_pushstring(L, ni->if_name);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
|
||||||
|
/* Then, actually store it */
|
||||||
|
lua_settable(L, -3);
|
||||||
|
|
||||||
|
i++;
|
||||||
|
ni++;
|
||||||
|
}
|
||||||
|
if_freenameindex(oni);
|
||||||
|
return 1;
|
||||||
|
}
|
27
src/if.h
Normal file
27
src/if.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* $Id: if.h $
|
||||||
|
*
|
||||||
|
* Author: Markus Stenberg <fingon@iki.fi>
|
||||||
|
*
|
||||||
|
* Copyright (c) 2012 cisco Systems, Inc.
|
||||||
|
*
|
||||||
|
* Created: Tue Dec 4 14:37:24 2012 mstenber
|
||||||
|
* Last modified: Tue Dec 4 14:51:43 2012 mstenber
|
||||||
|
* Edit time: 7 min
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* This module provides Lua wrapping for the advanced socket API
|
||||||
|
* defined in RFC3542, or mainly, the access to the system's interface
|
||||||
|
* list. It is necessary for use of recvmsg/sendmsg.
|
||||||
|
*
|
||||||
|
* TODO - Do something clever with Windows?
|
||||||
|
*/
|
||||||
|
#ifndef IF_H
|
||||||
|
#define IF_H
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
int if_open(lua_State *L);
|
||||||
|
|
||||||
|
#endif /* IF_H */
|
127
src/inet.c
127
src/inet.c
|
@ -231,45 +231,31 @@ 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));
|
if ((err=getnameinfo((struct sockaddr *)&peer, peer_len,
|
||||||
lua_pushstring(L, name);
|
name, INET6_ADDRSTRLEN,
|
||||||
lua_pushnumber(L, ntohs(peer.sin_port));
|
port, 6,
|
||||||
lua_pushliteral(L, "inet");
|
NI_NUMERICHOST | NI_NUMERICSERV))) {
|
||||||
return 3;
|
lua_pushnil(L);
|
||||||
}
|
lua_pushstring(L, gai_strerror(err));
|
||||||
}
|
return 2;
|
||||||
case PF_INET6: {
|
}
|
||||||
struct sockaddr_in6 peer;
|
lua_pushstring(L, name);
|
||||||
socklen_t peer_len = sizeof(peer);
|
lua_pushstring(L, port);
|
||||||
char name[INET6_ADDRSTRLEN];
|
if (family == PF_INET)
|
||||||
if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) {
|
lua_pushliteral(L, "inet");
|
||||||
lua_pushnil(L);
|
else
|
||||||
lua_pushstring(L, socket_strerror(errno));
|
lua_pushliteral(L, "inet6");
|
||||||
return 2;
|
return 3;
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
lua_pushnil(L);
|
|
||||||
lua_pushfstring(L, "unknown family %d", family);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
|
@ -277,44 +263,31 @@ 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));
|
if ((err=getnameinfo((struct sockaddr *)&peer, peer_len,
|
||||||
lua_pushstring(L, name);
|
name, INET6_ADDRSTRLEN,
|
||||||
lua_pushnumber(L, ntohs(local.sin_port));
|
port, 6,
|
||||||
lua_pushliteral(L, "inet");
|
NI_NUMERICHOST | NI_NUMERICSERV))) {
|
||||||
return 3;
|
lua_pushnil(L);
|
||||||
}
|
lua_pushstring(L, gai_strerror(err));
|
||||||
}
|
return 2;
|
||||||
case PF_INET6: {
|
}
|
||||||
struct sockaddr_in6 local;
|
lua_pushstring(L, name);
|
||||||
socklen_t local_len = sizeof(local);
|
lua_pushstring(L, port);
|
||||||
char name[INET6_ADDRSTRLEN];
|
if (family == PF_INET)
|
||||||
if (getsockname(*ps, (SA *) &local, &local_len) < 0) {
|
lua_pushliteral(L, "inet");
|
||||||
lua_pushnil(L);
|
else
|
||||||
lua_pushstring(L, socket_strerror(errno));
|
lua_pushliteral(L, "inet6");
|
||||||
return 2;
|
return 3;
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include "tcp.h"
|
#include "tcp.h"
|
||||||
#include "udp.h"
|
#include "udp.h"
|
||||||
#include "select.h"
|
#include "select.h"
|
||||||
|
#include "if.h"
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Internal function prototypes
|
* Internal function prototypes
|
||||||
|
@ -54,6 +55,7 @@ static const luaL_Reg mod[] = {
|
||||||
{"tcp", tcp_open},
|
{"tcp", tcp_open},
|
||||||
{"udp", udp_open},
|
{"udp", udp_open},
|
||||||
{"select", select_open},
|
{"select", select_open},
|
||||||
|
{"iface", if_open},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
25
src/makefile
25
src/makefile
|
@ -26,17 +26,18 @@ LUAV?=5.1
|
||||||
DEBUG?=NODEBUG
|
DEBUG?=NODEBUG
|
||||||
|
|
||||||
# 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)
|
||||||
|
LUAINC_macosx?=$(LUAINC_macosx_base)
|
||||||
# FIXME default should this default to fink or to macports?
|
# FIXME default should this default to fink or to macports?
|
||||||
# What happens when more than one Lua version is installed?
|
# What happens when more than one Lua version is installed?
|
||||||
LUAPREFIX_macosx?=/opt/local/
|
LUAPREFIX_macosx?=/opt/local/
|
||||||
|
|
||||||
# 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
|
||||||
|
@ -104,7 +105,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
|
||||||
|
|
||||||
|
@ -119,7 +120,7 @@ DEF_linux=-DLUASOCKET_$(DEBUG) \
|
||||||
-DMIME_API='__attribute__((visibility("default")))'
|
-DMIME_API='__attribute__((visibility("default")))'
|
||||||
CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra -Wimplicit -O2 -ggdb3 -fpic \
|
CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra -Wimplicit -O2 -ggdb3 -fpic \
|
||||||
-fvisibility=hidden
|
-fvisibility=hidden
|
||||||
LDFLAGS_linux=-O -shared -fpic -o
|
LDFLAGS_linux=-O -shared -fpic -o
|
||||||
LD_linux=gcc
|
LD_linux=gcc
|
||||||
SOCKET_linux=usocket.o
|
SOCKET_linux=usocket.o
|
||||||
|
|
||||||
|
@ -183,6 +184,7 @@ SOCKET_OBJS= \
|
||||||
auxiliar.$(O) \
|
auxiliar.$(O) \
|
||||||
options.$(O) \
|
options.$(O) \
|
||||||
inet.$(O) \
|
inet.$(O) \
|
||||||
|
if.$(O) \
|
||||||
$(SOCKET) \
|
$(SOCKET) \
|
||||||
except.$(O) \
|
except.$(O) \
|
||||||
select.$(O) \
|
select.$(O) \
|
||||||
|
@ -258,20 +260,20 @@ 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)$@
|
||||||
|
|
||||||
install:
|
install:
|
||||||
$(INSTALL_DIR) $(INSTALL_TOP_SHARE)
|
$(INSTALL_DIR) $(INSTALL_TOP_SHARE)
|
||||||
$(INSTALL_DATA) $(TO_TOP_SHARE) $(INSTALL_TOP_SHARE)
|
$(INSTALL_DATA) $(TO_TOP_SHARE) $(INSTALL_TOP_SHARE)
|
||||||
$(INSTALL_DIR) $(INSTALL_SOCKET_SHARE)
|
$(INSTALL_DIR) $(INSTALL_SOCKET_SHARE)
|
||||||
|
@ -301,6 +303,7 @@ auxiliar.$(O): auxiliar.c auxiliar.h
|
||||||
buffer.$(O): buffer.c buffer.h io.h timeout.h
|
buffer.$(O): buffer.c buffer.h io.h timeout.h
|
||||||
except.$(O): except.c except.h
|
except.$(O): except.c except.h
|
||||||
inet.$(O): inet.c inet.h socket.h io.h timeout.h usocket.h
|
inet.$(O): inet.c inet.h socket.h io.h timeout.h usocket.h
|
||||||
|
if.$(O): if.c if.h
|
||||||
io.$(O): io.c io.h timeout.h
|
io.$(O): io.c io.h timeout.h
|
||||||
luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \
|
luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \
|
||||||
timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \
|
timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \
|
||||||
|
|
111
src/options.c
111
src/options.c
|
@ -10,12 +10,31 @@
|
||||||
#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 +125,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 +155,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 +199,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)
|
||||||
|
@ -179,6 +226,8 @@ int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int opt_set_ip_add_membership(lua_State *L, p_socket ps)
|
int opt_set_ip_add_membership(lua_State *L, p_socket ps)
|
||||||
{
|
{
|
||||||
return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
|
||||||
|
@ -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,31 @@ 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 all interfaces. However, interface= can
|
||||||
|
override it. */
|
||||||
|
if (!lua_isnil(L, -1))
|
||||||
|
{
|
||||||
|
if (!lua_isnumber(L, -1))
|
||||||
|
luaL_argerror(L, 3, "string 'interface' field expected");
|
||||||
|
/* XXX - support non-numeric interface specification? */
|
||||||
|
}
|
||||||
|
val.ipv6mr_interface = lua_tonumber(L, -1);
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
@ -260,3 +349,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));
|
||||||
|
}
|
||||||
|
|
|
@ -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 */
|
||||||
|
@ -42,6 +47,10 @@ int opt_get_linger(lua_State *L, p_socket ps);
|
||||||
int opt_get_reuseaddr(lua_State *L, p_socket ps);
|
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_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);
|
||||||
|
|
148
src/udp.c
148
src/udp.c
|
@ -77,6 +77,11 @@ static t_opt optset[] = {
|
||||||
{"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-unicast-hops", opt_set_ip6_unicast_hops},
|
||||||
|
{"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},
|
{"ipv6-v6only", opt_set_ip6_v6only},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
@ -85,6 +90,10 @@ static t_opt optset[] = {
|
||||||
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},
|
||||||
|
{"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}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -151,39 +160,25 @@ 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);
|
/* unsigned short port = (unsigned short) luaL_checknumber(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(&addr, 0, sizeof(addr));
|
memset(&aihint, 0, sizeof(aihint));
|
||||||
if (!inet_pton(AF_INET, ip, &addr.sin_addr))
|
aihint.ai_family = udp->family;
|
||||||
luaL_argerror(L, 3, "invalid ip address");
|
aihint.ai_socktype = SOCK_DGRAM;
|
||||||
addr.sin_family = AF_INET;
|
if ((err = getaddrinfo(ip, lua_tostring(L, 4), &aihint, &ai)))
|
||||||
addr.sin_port = htons(port);
|
{
|
||||||
timeout_markstart(tm);
|
lua_pushnil(L);
|
||||||
err = socket_sendto(&udp->sock, data, count, &sent,
|
lua_pushstring(L, udp_strerror(err));
|
||||||
(SA *) &addr, sizeof(addr), tm);
|
return 2;
|
||||||
break;
|
}
|
||||||
}
|
timeout_markstart(tm);
|
||||||
case PF_INET6: {
|
err = socket_sendto(&udp->sock, data, count, &sent,
|
||||||
struct sockaddr_in6 addr;
|
ai->ai_addr, ai->ai_addrlen, tm);
|
||||||
memset(&addr, 0, sizeof(addr));
|
freeaddrinfo(ai);
|
||||||
if (!inet_pton(AF_INET6, ip, &addr.sin6_addr))
|
|
||||||
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_pushfstring(L, "unknown family %d", udp->family);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
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));
|
||||||
|
@ -220,69 +215,40 @@ 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);
|
{
|
||||||
char buffer[UDP_DATAGRAMSIZE];
|
p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
|
||||||
size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
|
char buffer[UDP_DATAGRAMSIZE];
|
||||||
int err;
|
size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
|
||||||
p_timeout tm = &udp->tm;
|
int err;
|
||||||
timeout_markstart(tm);
|
p_timeout tm = &udp->tm;
|
||||||
count = MIN(count, sizeof(buffer));
|
struct sockaddr_storage addr;
|
||||||
switch (udp->family) {
|
socklen_t addr_len = sizeof(addr);
|
||||||
case PF_INET: {
|
char addrstr[INET6_ADDRSTRLEN];
|
||||||
struct sockaddr_in addr;
|
char portstr[6];
|
||||||
socklen_t addr_len = sizeof(addr);
|
timeout_markstart(tm);
|
||||||
err = socket_recvfrom(&udp->sock, buffer, count, &got,
|
count = MIN(count, sizeof(buffer));
|
||||||
(SA *) &addr, &addr_len, tm);
|
err = socket_recvfrom(&udp->sock, buffer, count, &got,
|
||||||
/* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
|
(SA *) &addr, &addr_len, tm);
|
||||||
if (err == IO_CLOSED)
|
/* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
|
||||||
err = IO_DONE;
|
if (err == IO_CLOSED)
|
||||||
if (err == IO_DONE) {
|
err = IO_DONE;
|
||||||
char addrstr[INET_ADDRSTRLEN];
|
if (err != IO_DONE) {
|
||||||
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_pushfstring(L, "unknown family %d", udp->family);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_pushstring(L, udp_strerror(err));
|
lua_pushstring(L, udp_strerror(err));
|
||||||
return 2;
|
return 2;
|
||||||
|
}
|
||||||
|
if ((err = getnameinfo((struct sockaddr *)&addr, addr_len,
|
||||||
|
addrstr, INET6_ADDRSTRLEN,
|
||||||
|
portstr, 6,
|
||||||
|
NI_NUMERICHOST | NI_NUMERICSERV))) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, gai_strerror(err));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
lua_pushstring(L, addrstr);
|
||||||
|
lua_pushstring(L, portstr);
|
||||||
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
|
|
27
test/ifacetest.lua
Normal file
27
test/ifacetest.lua
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#!/usr/bin/env lua
|
||||||
|
-- -*-lua-*-
|
||||||
|
--
|
||||||
|
-- $Id: ifacetest.lua $
|
||||||
|
--
|
||||||
|
-- Author: Markus Stenberg <fingon@iki.fi>
|
||||||
|
--
|
||||||
|
-- Copyright (c) 2012 cisco Systems, Inc.
|
||||||
|
--
|
||||||
|
-- Created: Wed Dec 5 09:42:12 2012 mstenber
|
||||||
|
-- Last modified: Wed Dec 5 09:51:17 2012 mstenber
|
||||||
|
-- Edit time: 3 min
|
||||||
|
--
|
||||||
|
|
||||||
|
local socket = require 'socket'
|
||||||
|
local iface = socket.iface
|
||||||
|
|
||||||
|
local a = iface.nameindex()
|
||||||
|
assert(#a > 0)
|
||||||
|
local o = a[1]
|
||||||
|
assert(o.index)
|
||||||
|
assert(o.name)
|
||||||
|
assert(iface.nametoindex(o.name))
|
||||||
|
assert(not iface.nametoindex('gargle'))
|
||||||
|
assert(iface.indextoname(o.index))
|
||||||
|
assert(not iface.indextoname(-123))
|
||||||
|
|
116
test/ipv6mcasttest.lua
Normal file
116
test/ipv6mcasttest.lua
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
#!/usr/bin/env lua
|
||||||
|
-- -*-lua-*-
|
||||||
|
--
|
||||||
|
-- $Id: ipv6mcasttest.lua $
|
||||||
|
--
|
||||||
|
-- Author: Markus Stenberg <mstenber@cisco.com>
|
||||||
|
--
|
||||||
|
-- Copyright (c) 2012 cisco Systems, Inc.
|
||||||
|
--
|
||||||
|
-- Created: Wed Dec 5 09:55:46 2012 mstenber
|
||||||
|
-- Last modified: Wed Dec 5 18:33:09 2012 mstenber
|
||||||
|
-- Edit time: 50 min
|
||||||
|
--
|
||||||
|
|
||||||
|
-- exercise the new IPv6 related functionality:
|
||||||
|
|
||||||
|
-- join/leave IPv6 multicast group, send/receive IPv6 message using
|
||||||
|
-- sendmsg/recvmsg API
|
||||||
|
|
||||||
|
require 'socket'
|
||||||
|
|
||||||
|
local mcast = 'ff02::fb'
|
||||||
|
|
||||||
|
local port = 4242
|
||||||
|
local ifname = 'eth2'
|
||||||
|
--local ifname = 'lo' -- should not work - no multicast on lo in Linux?
|
||||||
|
local testdata = 'foobar'
|
||||||
|
|
||||||
|
function setup_socket(port)
|
||||||
|
local s = socket.udp6()
|
||||||
|
s:setoption('ipv6-v6only', true)
|
||||||
|
if port
|
||||||
|
then
|
||||||
|
s:setoption('reuseaddr', true)
|
||||||
|
--local r, err = s:setsockname('::1', port)
|
||||||
|
local r, err = s:setsockname('*', port)
|
||||||
|
assert(r, 'setsockname failed ' .. tostring(err))
|
||||||
|
--print("getsockname", s:getsockname())
|
||||||
|
end
|
||||||
|
assert(tostring(s):find("udp{unconnected}"))
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
function setup_connected(host, port)
|
||||||
|
local c = setup_socket()
|
||||||
|
assert(host and port)
|
||||||
|
local r, err = c:setpeername(host, port)
|
||||||
|
assert(r, 'setpeername failed ' .. tostring(err))
|
||||||
|
--print('got from setpeername', r, err)
|
||||||
|
--print("getsockname", c:getsockname())
|
||||||
|
assert(tostring(c):find("udp{connected}"), 'not connected', tostring(c))
|
||||||
|
--print("getpeername", c:getpeername())
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
|
||||||
|
function my_setoption(s, k, v)
|
||||||
|
local old = s:getoption(k)
|
||||||
|
print('before set', k, old)
|
||||||
|
local r, err = s:setoption(k, v)
|
||||||
|
assert(r, 'setoption failed ' .. tostring(err))
|
||||||
|
local new = s:getoption(k)
|
||||||
|
print('after set', k, new)
|
||||||
|
assert(v == new)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- specific interface
|
||||||
|
--local ifindex = socket.iface.nametoindex(ifname)
|
||||||
|
|
||||||
|
-- all interfaces
|
||||||
|
local ifindex = nil
|
||||||
|
|
||||||
|
local s = setup_socket(port)
|
||||||
|
|
||||||
|
local mct = {multiaddr=mcast, interface=ifindex}
|
||||||
|
local r, err = s:setoption('ipv6-drop-membership', mct)
|
||||||
|
-- don't check result - it may or may not fail
|
||||||
|
local r, err = s:setoption('ipv6-add-membership', mct)
|
||||||
|
assert(r, 'ipv6-add-membership failed', err)
|
||||||
|
|
||||||
|
--local c = setup_connected(mcast .. '%lo', port)
|
||||||
|
--c:send('foo')
|
||||||
|
local c = setup_socket()
|
||||||
|
|
||||||
|
-- this seems to be the default anyway?
|
||||||
|
my_setoption(c, 'ipv6-unicast-hops', 255)
|
||||||
|
my_setoption(c, 'ipv6-multicast-loop', true)
|
||||||
|
my_setoption(c, 'ipv6-multicast-hops', 255)
|
||||||
|
my_setoption(s, 'ipv6-multicast-hops', 255)
|
||||||
|
|
||||||
|
local r, err = c:sendto(testdata, mcast .. '%' .. ifname, port)
|
||||||
|
assert(r, 'sendto failed')
|
||||||
|
|
||||||
|
-- make sure sending both to mcast address, and link-local
|
||||||
|
-- address on the chosen interface works
|
||||||
|
local r, err = c:sendto(testdata, mcast .. '%' .. ifname, port)
|
||||||
|
assert(r, 'sendto failed (mcast)')
|
||||||
|
|
||||||
|
local data, host, port = s:receivefrom()
|
||||||
|
assert(data)
|
||||||
|
print('recvfrom', host, port)
|
||||||
|
|
||||||
|
-- make sure we can send stuff back to link-local addr as well'
|
||||||
|
local r, err = s:sendto(testdata, host, port)
|
||||||
|
assert(r, 'sendto failed (ll)')
|
||||||
|
|
||||||
|
-- and it is received too
|
||||||
|
local data, host, port = c:receivefrom()
|
||||||
|
assert(data)
|
||||||
|
print('recvfrom', host, port)
|
||||||
|
|
||||||
|
-- now, finally, we can get rid of mcast group
|
||||||
|
local r, err = s:setoption('ipv6-drop-membership', mct)
|
||||||
|
assert(r, 'ipv6-drop-membership failed', err)
|
||||||
|
--local c = setup_connected(port)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue