Added if_* POSIX API wrappers under socket.iface.

This commit is contained in:
Markus Stenberg 2012-12-05 09:54:34 +02:00
parent a402222464
commit 2107b9a650
5 changed files with 180 additions and 10 deletions

112
src/if.c Normal file
View file

@ -0,0 +1,112 @@
/*
* $Id: if.c $
*
* Author: Markus Stenberg <fingon@iki.fi>
*
* Copyright (c) 2012 Markus Stenberg
* All rights reserved
*
* Created: Tue Dec 4 14:50:34 2012 mstenber
* Last modified: Wed Dec 5 09:50:59 2012 mstenber
* Edit time: 22 min
*
*/
#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
View 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 */

View file

@ -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}
}; };

View file

@ -183,6 +183,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) \
@ -301,6 +302,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 \

27
test/ifacetest.lua Normal file
View 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))