1
0

0001-Add-interface-support.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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,113 @@
  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. + luaL_openlib(L, NULL, func, 0);
  57. + lua_settable(L, -3);
  58. + return 0;
  59. +}
  60. +
  61. +int if_global_indextoname(lua_State *L)
  62. +{
  63. + unsigned int ifnumber;
  64. + const char *name;
  65. + char buf[IF_NAMESIZE+1];
  66. +
  67. + if (!lua_isnumber(L, 1))
  68. + {
  69. + lua_pushnil(L);
  70. + lua_pushstring(L, "indextoname expects only number argument");
  71. + return 2;
  72. + }
  73. + ifnumber = lua_tonumber(L, 1);
  74. + if (!(name = if_indextoname(ifnumber, buf)))
  75. + {
  76. + lua_pushnil(L);
  77. + lua_pushstring(L, "nonexistent interface");
  78. + return 2;
  79. + }
  80. + lua_pushstring(L, name);
  81. + return 1;
  82. +}
  83. +
  84. +int if_global_nametoindex(lua_State *L)
  85. +{
  86. + unsigned int ifnumber;
  87. + if (!lua_isstring(L, 1))
  88. + {
  89. + lua_pushnil(L);
  90. + lua_pushstring(L, "nametoindex expects only string argument");
  91. + return 2;
  92. + }
  93. + if (!(ifnumber = if_nametoindex(lua_tostring(L, 1))))
  94. + {
  95. + lua_pushnil(L);
  96. + lua_pushstring(L, "nonexistent interface");
  97. + return 2;
  98. + }
  99. + lua_pushnumber(L, ifnumber);
  100. + return 1;
  101. +}
  102. +
  103. +int if_global_nameindex(lua_State *L)
  104. +{
  105. + struct if_nameindex *ni, *oni;
  106. + int i = 1;
  107. + oni = ni = if_nameindex();
  108. + lua_newtable(L);
  109. + while (ni && ni->if_index && *(ni->if_name))
  110. + {
  111. + /* at result[i], we store.. */
  112. + lua_pushnumber(L, i);
  113. +
  114. + /* new table with two items - index, name*/
  115. + lua_newtable(L);
  116. + lua_pushstring(L, "index");
  117. + lua_pushnumber(L, ni->if_index);
  118. + lua_settable(L, -3);
  119. +
  120. + lua_pushstring(L, "name");
  121. + lua_pushstring(L, ni->if_name);
  122. + lua_settable(L, -3);
  123. +
  124. + /* Then, actually store it */
  125. + lua_settable(L, -3);
  126. +
  127. + i++;
  128. + ni++;
  129. + }
  130. + if_freenameindex(oni);
  131. + return 1;
  132. +}
  133. diff --git a/src/if.h b/src/if.h
  134. new file mode 100644
  135. index 0000000..dc7faf8
  136. --- /dev/null
  137. +++ b/src/if.h
  138. @@ -0,0 +1,27 @@
  139. +/*
  140. + * $Id: if.h $
  141. + *
  142. + * Author: Markus Stenberg <fingon@iki.fi>
  143. + *
  144. + * Copyright (c) 2012 cisco Systems, Inc.
  145. + *
  146. + * Created: Tue Dec 4 14:37:24 2012 mstenber
  147. + * Last modified: Tue Dec 4 14:51:43 2012 mstenber
  148. + * Edit time: 7 min
  149. + *
  150. + */
  151. +
  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.
  155. + *
  156. + * TODO - Do something clever with Windows?
  157. + */
  158. +#ifndef IF_H
  159. +#define IF_H
  160. +
  161. +#include "lua.h"
  162. +
  163. +int if_open(lua_State *L);
  164. +
  165. +#endif /* IF_H */
  166. diff --git a/src/luasocket.c b/src/luasocket.c
  167. index e6ee747..85d41a6 100644
  168. --- a/src/luasocket.c
  169. +++ b/src/luasocket.c
  170. @@ -31,6 +31,7 @@
  171. #include "tcp.h"
  172. #include "udp.h"
  173. #include "select.h"
  174. +#include "if.h"
  175. /*-------------------------------------------------------------------------*\
  176. * Internal function prototypes
  177. @@ -51,6 +52,7 @@ static const luaL_Reg mod[] = {
  178. {"tcp", tcp_open},
  179. {"udp", udp_open},
  180. {"select", select_open},
  181. + {"iface", if_open},
  182. {NULL, NULL}
  183. };
  184. diff --git a/src/makefile b/src/makefile
  185. index 8d3521e..09d4882 100644
  186. --- a/src/makefile
  187. +++ b/src/makefile
  188. @@ -262,6 +262,7 @@ SOCKET_OBJS= \
  189. auxiliar.$(O) \
  190. options.$(O) \
  191. inet.$(O) \
  192. + if.$(O) \
  193. $(SOCKET) \
  194. except.$(O) \
  195. select.$(O) \
  196. @@ -387,6 +388,7 @@ auxiliar.$(O): auxiliar.c auxiliar.h
  197. buffer.$(O): buffer.c buffer.h io.h timeout.h
  198. except.$(O): except.c except.h
  199. inet.$(O): inet.c inet.h socket.h io.h timeout.h usocket.h
  200. +if.$(O): if.c if.h
  201. io.$(O): io.c io.h timeout.h
  202. luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \
  203. timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \
  204. diff --git a/src/options.c b/src/options.c
  205. index 8ac2a14..1c73e6f 100644
  206. --- a/src/options.c
  207. +++ b/src/options.c
  208. @@ -3,6 +3,9 @@
  209. * LuaSocket toolkit
  210. \*=========================================================================*/
  211. #include <string.h>
  212. +#include <sys/types.h>
  213. +#include <sys/socket.h>
  214. +#include <net/if.h>
  215. #include "lauxlib.h"
  216. @@ -285,6 +288,12 @@ static int opt_ip6_setmembership(lua_State *L, p_socket ps, int level, int name)
  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)))) {
  222. + lua_pushnil(L);
  223. + lua_pushstring(L, "nonexistent interface");
  224. + return 2;
  225. + }
  226. } else
  227. luaL_argerror(L, -1, "number 'interface' field expected");
  228. }
  229. --
  230. 1.8.4.rc3