1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | From 96fdf07acf78ecfc9be76a8b0591f38fe6f1a875 Mon Sep 17 00:00:00 2001 From: Steven Barth <steven@midlink.org> Date: Sat, 9 Nov 2013 12:01:42 +0100 Subject: [PATCH] Add interface resolving --- src/if.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/if.h | 27 ++++++++++++++ src/luasocket.c | 2 + src/makefile | 2 + src/options.c | 9 +++++ 5 files changed, 153 insertions(+) create mode 100644 src/if.c create mode 100644 src/if.h diff --git a/src/if.c b/src/if.c new file mode 100644 index 0000000..db231aa --- /dev/null +++ b/src/if.c @@ -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; +} diff --git a/src/if.h b/src/if.h new file mode 100644 index 0000000..dc7faf8 --- /dev/null +++ b/src/if.h @@ -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 */ diff --git a/src/luasocket.c b/src/luasocket.c index e6ee747..85d41a6 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -31,6 +31,7 @@ #include "tcp.h" #include "udp.h" #include "select.h" +#include "if.h" /*-------------------------------------------------------------------------*\ * Internal function prototypes @@ -51,6 +52,7 @@ static const luaL_Reg mod[] = { {"tcp", tcp_open}, {"udp", udp_open}, {"select", select_open}, + {"iface", if_open}, {NULL, NULL} }; diff --git a/src/makefile b/src/makefile index 8d3521e..09d4882 100644 --- a/src/makefile +++ b/src/makefile @@ -262,6 +262,7 @@ SOCKET_OBJS= \ auxiliar.$(O) \ options.$(O) \ inet.$(O) \ + if.$(O) \ $(SOCKET) \ except.$(O) \ select.$(O) \ @@ -387,6 +388,7 @@ auxiliar.$(O): auxiliar.c auxiliar.h buffer.$(O): buffer.c buffer.h io.h timeout.h except.$(O): except.c except.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 luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \ timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \ diff --git a/src/options.c b/src/options.c index 8ac2a14..1c73e6f 100644 --- a/src/options.c +++ b/src/options.c @@ -3,6 +3,9 @@ * LuaSocket toolkit \*=========================================================================*/ #include <string.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <net/if.h> #include "lauxlib.h" @@ -285,6 +288,12 @@ static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name) if (!lua_isnil(L, -1)) { if (lua_isnumber(L, -1)) { val.ipv6mr_interface = (unsigned int) 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 'interface' field expected"); } -- 1.8.4.rc3 |