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
16 diff --git a/src/if.c b/src/if.c
18 index 0000000..db231aa
25 + * Author: Markus Stenberg <fingon@iki.fi>
27 + * Copyright (c) 2012 cisco Systems, Inc.
29 + * Created: Tue Dec 4 14:50:34 2012 mstenber
30 + * Last modified: Wed Dec 5 18:51:08 2012 mstenber
35 +#include <sys/types.h>
36 +#include <sys/socket.h>
43 +static int if_global_indextoname(lua_State *L);
44 +static int if_global_nametoindex(lua_State *L);
45 +static int if_global_nameindex(lua_State *L);
47 +static luaL_Reg func[] = {
48 + { "indextoname", if_global_indextoname},
49 + { "nametoindex", if_global_nametoindex},
50 + { "nameindex", if_global_nameindex},
54 +int if_open(lua_State *L)
56 + lua_pushstring(L, "iface");
58 + luaL_openlib(L, NULL, func, 0);
59 + lua_settable(L, -3);
63 +int if_global_indextoname(lua_State *L)
65 + unsigned int ifnumber;
67 + char buf[IF_NAMESIZE+1];
69 + if (!lua_isnumber(L, 1))
72 + lua_pushstring(L, "indextoname expects only number argument");
75 + ifnumber = lua_tonumber(L, 1);
76 + if (!(name = if_indextoname(ifnumber, buf)))
79 + lua_pushstring(L, "nonexistent interface");
82 + lua_pushstring(L, name);
86 +int if_global_nametoindex(lua_State *L)
88 + unsigned int ifnumber;
89 + if (!lua_isstring(L, 1))
92 + lua_pushstring(L, "nametoindex expects only string argument");
95 + if (!(ifnumber = if_nametoindex(lua_tostring(L, 1))))
98 + lua_pushstring(L, "nonexistent interface");
101 + lua_pushnumber(L, ifnumber);
105 +int if_global_nameindex(lua_State *L)
107 + struct if_nameindex *ni, *oni;
109 + oni = ni = if_nameindex();
111 + while (ni && ni->if_index && *(ni->if_name))
113 + /* at result[i], we store.. */
114 + lua_pushnumber(L, i);
116 + /* new table with two items - index, name*/
118 + lua_pushstring(L, "index");
119 + lua_pushnumber(L, ni->if_index);
120 + lua_settable(L, -3);
122 + lua_pushstring(L, "name");
123 + lua_pushstring(L, ni->if_name);
124 + lua_settable(L, -3);
126 + /* Then, actually store it */
127 + lua_settable(L, -3);
132 + if_freenameindex(oni);
135 diff --git a/src/if.h b/src/if.h
137 index 0000000..dc7faf8
144 + * Author: Markus Stenberg <fingon@iki.fi>
146 + * Copyright (c) 2012 cisco Systems, Inc.
148 + * Created: Tue Dec 4 14:37:24 2012 mstenber
149 + * Last modified: Tue Dec 4 14:51:43 2012 mstenber
154 +/* This module provides Lua wrapping for the advanced socket API
155 + * defined in RFC3542, or mainly, the access to the system's interface
156 + * list. It is necessary for use of recvmsg/sendmsg.
158 + * TODO - Do something clever with Windows?
165 +int if_open(lua_State *L);
168 diff --git a/src/luasocket.c b/src/luasocket.c
169 index e6ee747..85d41a6 100644
170 --- a/src/luasocket.c
171 +++ b/src/luasocket.c
178 /*-------------------------------------------------------------------------*\
179 * Internal function prototypes
180 @@ -51,6 +52,7 @@ static const luaL_Reg mod[] = {
183 {"select", select_open},
184 + {"iface", if_open},
188 diff --git a/src/makefile b/src/makefile
189 index 8d3521e..09d4882 100644
192 @@ -262,6 +262,7 @@ SOCKET_OBJS= \
200 @@ -387,6 +388,7 @@ auxiliar.$(O): auxiliar.c auxiliar.h
201 buffer.$(O): buffer.c buffer.h io.h timeout.h
202 except.$(O): except.c except.h
203 inet.$(O): inet.c inet.h socket.h io.h timeout.h usocket.h
205 io.$(O): io.c io.h timeout.h
206 luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \
207 timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \
208 diff --git a/src/options.c b/src/options.c
209 index 8ac2a14..1c73e6f 100644
214 \*=========================================================================*/
216 +#include <sys/types.h>
217 +#include <sys/socket.h>
222 @@ -285,6 +288,12 @@ static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name)
223 if (!lua_isnil(L, -1)) {
224 if (lua_isnumber(L, -1)) {
225 val.ipv6mr_interface = (unsigned int) lua_tonumber(L, -1);
226 + } else if (lua_isstring(L, -1)) {
227 + if (!(val.ipv6mr_interface = if_nametoindex(lua_tostring(L, -1)))) {
229 + lua_pushstring(L, "nonexistent interface");
233 luaL_argerror(L, -1, "number 'interface' field expected");