1 From 96fdf07acf78ecfc9be76a8b0591f38fe6f1a875 Mon Sep 17 00:00:00 2001
2 From: Steven Barth <steven@midlink.org>
3 Date: Sat, 9 Nov 2013 12:01:42 +0100
4 Subject: [PATCH] Add interface resolving
7 src/if.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 src/if.h | 27 ++++++++++++++
11 src/options.c | 9 +++++
12 5 files changed, 153 insertions(+)
13 create mode 100644 src/if.c
14 create mode 100644 src/if.h
22 + * Author: Markus Stenberg <fingon@iki.fi>
24 + * Copyright (c) 2012 cisco Systems, Inc.
26 + * Created: Tue Dec 4 14:50:34 2012 mstenber
27 + * Last modified: Wed Dec 5 18:51:08 2012 mstenber
32 +#include <sys/types.h>
33 +#include <sys/socket.h>
40 +static int if_global_indextoname(lua_State *L);
41 +static int if_global_nametoindex(lua_State *L);
42 +static int if_global_nameindex(lua_State *L);
44 +static luaL_Reg func[] = {
45 + { "indextoname", if_global_indextoname},
46 + { "nametoindex", if_global_nametoindex},
47 + { "nameindex", if_global_nameindex},
51 +int if_open(lua_State *L)
53 + lua_pushstring(L, "iface");
55 +#if LUA_VERSION_NUM < 503
56 + luaL_openlib(L, NULL, func, 0);
58 + luaL_setfuncs(L, func, 0);
60 + lua_settable(L, -3);
64 +int if_global_indextoname(lua_State *L)
66 + unsigned int ifnumber;
68 + char buf[IF_NAMESIZE+1];
70 + if (!lua_isnumber(L, 1))
73 + lua_pushstring(L, "indextoname expects only number argument");
76 + ifnumber = lua_tonumber(L, 1);
77 + if (!(name = if_indextoname(ifnumber, buf)))
80 + lua_pushstring(L, "nonexistent interface");
83 + lua_pushstring(L, name);
87 +int if_global_nametoindex(lua_State *L)
89 + unsigned int ifnumber;
90 + if (!lua_isstring(L, 1))
93 + lua_pushstring(L, "nametoindex expects only string argument");
96 + if (!(ifnumber = if_nametoindex(lua_tostring(L, 1))))
99 + lua_pushstring(L, "nonexistent interface");
102 + lua_pushnumber(L, ifnumber);
106 +int if_global_nameindex(lua_State *L)
108 + struct if_nameindex *ni, *oni;
110 + oni = ni = if_nameindex();
112 + while (ni && ni->if_index && *(ni->if_name))
114 + /* at result[i], we store.. */
115 + lua_pushnumber(L, i);
117 + /* new table with two items - index, name*/
119 + lua_pushstring(L, "index");
120 + lua_pushnumber(L, ni->if_index);
121 + lua_settable(L, -3);
123 + lua_pushstring(L, "name");
124 + lua_pushstring(L, ni->if_name);
125 + lua_settable(L, -3);
127 + /* Then, actually store it */
128 + lua_settable(L, -3);
133 + if_freenameindex(oni);
142 + * Author: Markus Stenberg <fingon@iki.fi>
144 + * Copyright (c) 2012 cisco Systems, Inc.
146 + * Created: Tue Dec 4 14:37:24 2012 mstenber
147 + * Last modified: Tue Dec 4 14:51:43 2012 mstenber
152 +/* This module provides Lua wrapping for the advanced socket API
153 + * defined in RFC3542, or mainly, the access to the system's interface
154 + * list. It is necessary for use of recvmsg/sendmsg.
156 + * TODO - Do something clever with Windows?
163 +int if_open(lua_State *L);
166 --- a/src/luasocket.c
167 +++ b/src/luasocket.c
174 /*-------------------------------------------------------------------------*\
175 * Internal function prototypes
176 @@ -41,6 +42,7 @@ static const luaL_Reg mod[] = {
179 {"select", select_open},
180 + {"iface", if_open},
186 @@ -303,6 +303,7 @@ SOCKET_OBJS= \
194 @@ -440,6 +441,7 @@ auxiliar.$(O): auxiliar.c auxiliar.h
195 buffer.$(O): buffer.c buffer.h io.h timeout.h
196 except.$(O): except.c except.h
197 inet.$(O): inet.c inet.h socket.h io.h timeout.h usocket.h
199 io.$(O): io.c io.h timeout.h
200 luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \
201 timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \
209 +#include <sys/types.h>
210 +#include <sys/socket.h>
213 /*=========================================================================*\
214 * Internal functions prototypes
215 \*=========================================================================*/
216 @@ -414,6 +417,12 @@ static int opt_ip6_setmembership(lua_Sta
217 if (!lua_isnil(L, -1)) {
218 if (lua_isnumber(L, -1)) {
219 val.ipv6mr_interface = (unsigned int) lua_tonumber(L, -1);
220 + } else if (lua_isstring(L, -1)) {
221 + if (!(val.ipv6mr_interface = if_nametoindex(lua_tostring(L, -1)))) {
223 + lua_pushstring(L, "nonexistent interface");
227 luaL_argerror(L, -1, "number 'interface' field expected");