You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.9 KiB

  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
  5. ---
  6. src/if.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. src/if.h | 27 ++++++++++++++
  8. src/luasocket.c | 2 +
  9. src/makefile | 2 +
  10. src/options.c | 9 +++++
  11. 5 files changed, 153 insertions(+)
  12. create mode 100644 src/if.c
  13. create mode 100644 src/if.h
  14. diff --git a/src/if.c b/src/if.c
  15. new file mode 100644
  16. index 0000000..db231aa
  17. --- /dev/null
  18. +++ b/src/if.c
  19. @@ -0,0 +1,117 @@
  20. +/*
  21. + * $Id: if.c $
  22. + *
  23. + * Author: Markus Stenberg <fingon@iki.fi>
  24. + *
  25. + * Copyright (c) 2012 cisco Systems, Inc.
  26. + *
  27. + * Created: Tue Dec 4 14:50:34 2012 mstenber
  28. + * Last modified: Wed Dec 5 18:51:08 2012 mstenber
  29. + * Edit time: 24 min
  30. + *
  31. + */
  32. +
  33. +#include <sys/types.h>
  34. +#include <sys/socket.h>
  35. +#include <net/if.h>
  36. +
  37. +#include "if.h"
  38. +
  39. +#include "lauxlib.h"
  40. +
  41. +static int if_global_indextoname(lua_State *L);
  42. +static int if_global_nametoindex(lua_State *L);
  43. +static int if_global_nameindex(lua_State *L);
  44. +
  45. +static luaL_Reg func[] = {
  46. + { "indextoname", if_global_indextoname},
  47. + { "nametoindex", if_global_nametoindex},
  48. + { "nameindex", if_global_nameindex},
  49. + { NULL, NULL}
  50. +};
  51. +
  52. +int if_open(lua_State *L)
  53. +{
  54. + lua_pushstring(L, "iface");
  55. + lua_newtable(L);
  56. +#if LUA_VERSION_NUM < 503
  57. + luaL_openlib(L, NULL, func, 0);
  58. +#else
  59. + luaL_setfuncs(L, func, 0);
  60. +#endif
  61. + lua_settable(L, -3);
  62. + return 0;
  63. +}
  64. +
  65. +int if_global_indextoname(lua_State *L)
  66. +{
  67. + unsigned int ifnumber;
  68. + const char *name;
  69. + char buf[IF_NAMESIZE+1];
  70. +
  71. + if (!lua_isnumber(L, 1))
  72. + {
  73. + lua_pushnil(L);
  74. + lua_pushstring(L, "indextoname expects only number argument");
  75. + return 2;
  76. + }
  77. + ifnumber = lua_tonumber(L, 1);
  78. + if (!(name = if_indextoname(ifnumber, buf)))
  79. + {
  80. + lua_pushnil(L);
  81. + lua_pushstring(L, "nonexistent interface");
  82. + return 2;
  83. + }
  84. + lua_pushstring(L, name);
  85. + return 1;
  86. +}
  87. +
  88. +int if_global_nametoindex(lua_State *L)
  89. +{
  90. + unsigned int ifnumber;
  91. + if (!lua_isstring(L, 1))
  92. + {
  93. + lua_pushnil(L);
  94. + lua_pushstring(L, "nametoindex expects only string argument");
  95. + return 2;
  96. + }
  97. + if (!(ifnumber = if_nametoindex(lua_tostring(L, 1))))
  98. + {
  99. + lua_pushnil(L);
  100. + lua_pushstring(L, "nonexistent interface");
  101. + return 2;
  102. + }
  103. + lua_pushnumber(L, ifnumber);
  104. + return 1;
  105. +}
  106. +
  107. +int if_global_nameindex(lua_State *L)
  108. +{
  109. + struct if_nameindex *ni, *oni;
  110. + int i = 1;
  111. + oni = ni = if_nameindex();
  112. + lua_newtable(L);
  113. + while (ni && ni->if_index && *(ni->if_name))
  114. + {
  115. + /* at result[i], we store.. */
  116. + lua_pushnumber(L, i);
  117. +
  118. + /* new table with two items - index, name*/
  119. + lua_newtable(L);
  120. + lua_pushstring(L, "index");
  121. + lua_pushnumber(L, ni->if_index);
  122. + lua_settable(L, -3);
  123. +
  124. + lua_pushstring(L, "name");
  125. + lua_pushstring(L, ni->if_name);
  126. + lua_settable(L, -3);
  127. +
  128. + /* Then, actually store it */
  129. + lua_settable(L, -3);
  130. +
  131. + i++;
  132. + ni++;
  133. + }
  134. + if_freenameindex(oni);
  135. + return 1;
  136. +}
  137. diff --git a/src/if.h b/src/if.h
  138. new file mode 100644
  139. index 0000000..dc7faf8
  140. --- /dev/null
  141. +++ b/src/if.h
  142. @@ -0,0 +1,27 @@
  143. +/*
  144. + * $Id: if.h $
  145. + *
  146. + * Author: Markus Stenberg <fingon@iki.fi>
  147. + *
  148. + * Copyright (c) 2012 cisco Systems, Inc.
  149. + *
  150. + * Created: Tue Dec 4 14:37:24 2012 mstenber
  151. + * Last modified: Tue Dec 4 14:51:43 2012 mstenber
  152. + * Edit time: 7 min
  153. + *
  154. + */
  155. +
  156. +/* This module provides Lua wrapping for the advanced socket API
  157. + * defined in RFC3542, or mainly, the access to the system's interface
  158. + * list. It is necessary for use of recvmsg/sendmsg.
  159. + *
  160. + * TODO - Do something clever with Windows?
  161. + */
  162. +#ifndef IF_H
  163. +#define IF_H
  164. +
  165. +#include "lua.h"
  166. +
  167. +int if_open(lua_State *L);
  168. +
  169. +#endif /* IF_H */
  170. diff --git a/src/luasocket.c b/src/luasocket.c
  171. index e6ee747..85d41a6 100644
  172. --- a/src/luasocket.c
  173. +++ b/src/luasocket.c
  174. @@ -21,6 +21,7 @@
  175. #include "tcp.h"
  176. #include "udp.h"
  177. #include "select.h"
  178. +#include "if.h"
  179. /*-------------------------------------------------------------------------*\
  180. * Internal function prototypes
  181. @@ -41,6 +42,7 @@ static const luaL_Reg mod[] = {
  182. {"tcp", tcp_open},
  183. {"udp", udp_open},
  184. {"select", select_open},
  185. + {"iface", if_open},
  186. {NULL, NULL}
  187. };
  188. diff --git a/src/makefile b/src/makefile
  189. index 8d3521e..09d4882 100644
  190. --- a/src/makefile
  191. +++ b/src/makefile
  192. @@ -303,6 +303,7 @@ SOCKET_OBJS= \
  193. compat.$(O) \
  194. options.$(O) \
  195. inet.$(O) \
  196. + if.$(O) \
  197. $(SOCKET) \
  198. except.$(O) \
  199. select.$(O) \
  200. @@ -440,6 +441,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
  204. +if.$(O): if.c if.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
  210. --- a/src/options.c
  211. +++ b/src/options.c
  212. @@ -7,7 +7,10 @@
  213. #include "options.h"
  214. #include "inet.h"
  215. #include <string.h>
  216. -
  217. +#include <sys/types.h>
  218. +#include <sys/socket.h>
  219. +#include <net/if.h>
  220. +
  221. /*=========================================================================*\
  222. * Internal functions prototypes
  223. \*=========================================================================*/
  224. @@ -388,6 +391,12 @@ static int opt_ip6_setmembership(lua_Sta
  225. if (!lua_isnil(L, -1)) {
  226. if (lua_isnumber(L, -1)) {
  227. val.ipv6mr_interface = (unsigned int) lua_tonumber(L, -1);
  228. + } else if (lua_isstring(L, -1)) {
  229. + if (!(val.ipv6mr_interface = if_nametoindex(lua_tostring(L, -1)))) {
  230. + lua_pushnil(L);
  231. + lua_pushstring(L, "nonexistent interface");
  232. + return 2;
  233. + }
  234. } else
  235. luaL_argerror(L, -1, "number 'interface' field expected");
  236. }
  237. --
  238. 1.8.4.rc3