luasocket: update to 3.1.0
[feed/packages.git] / lang / luasocket / patches / 0001-Add-interface-support.patch
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 ---
7 src/if.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 src/if.h | 27 ++++++++++++++
9 src/luasocket.c | 2 +
10 src/makefile | 2 +
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
15
16 --- /dev/null
17 +++ b/src/if.c
18 @@ -0,0 +1,117 @@
19 +/*
20 + * $Id: if.c $
21 + *
22 + * Author: Markus Stenberg <fingon@iki.fi>
23 + *
24 + * Copyright (c) 2012 cisco Systems, Inc.
25 + *
26 + * Created: Tue Dec 4 14:50:34 2012 mstenber
27 + * Last modified: Wed Dec 5 18:51:08 2012 mstenber
28 + * Edit time: 24 min
29 + *
30 + */
31 +
32 +#include <sys/types.h>
33 +#include <sys/socket.h>
34 +#include <net/if.h>
35 +
36 +#include "if.h"
37 +
38 +#include "lauxlib.h"
39 +
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);
43 +
44 +static luaL_Reg func[] = {
45 + { "indextoname", if_global_indextoname},
46 + { "nametoindex", if_global_nametoindex},
47 + { "nameindex", if_global_nameindex},
48 + { NULL, NULL}
49 +};
50 +
51 +int if_open(lua_State *L)
52 +{
53 + lua_pushstring(L, "iface");
54 + lua_newtable(L);
55 +#if LUA_VERSION_NUM < 503
56 + luaL_openlib(L, NULL, func, 0);
57 +#else
58 + luaL_setfuncs(L, func, 0);
59 +#endif
60 + lua_settable(L, -3);
61 + return 0;
62 +}
63 +
64 +int if_global_indextoname(lua_State *L)
65 +{
66 + unsigned int ifnumber;
67 + const char *name;
68 + char buf[IF_NAMESIZE+1];
69 +
70 + if (!lua_isnumber(L, 1))
71 + {
72 + lua_pushnil(L);
73 + lua_pushstring(L, "indextoname expects only number argument");
74 + return 2;
75 + }
76 + ifnumber = lua_tonumber(L, 1);
77 + if (!(name = if_indextoname(ifnumber, buf)))
78 + {
79 + lua_pushnil(L);
80 + lua_pushstring(L, "nonexistent interface");
81 + return 2;
82 + }
83 + lua_pushstring(L, name);
84 + return 1;
85 +}
86 +
87 +int if_global_nametoindex(lua_State *L)
88 +{
89 + unsigned int ifnumber;
90 + if (!lua_isstring(L, 1))
91 + {
92 + lua_pushnil(L);
93 + lua_pushstring(L, "nametoindex expects only string argument");
94 + return 2;
95 + }
96 + if (!(ifnumber = if_nametoindex(lua_tostring(L, 1))))
97 + {
98 + lua_pushnil(L);
99 + lua_pushstring(L, "nonexistent interface");
100 + return 2;
101 + }
102 + lua_pushnumber(L, ifnumber);
103 + return 1;
104 +}
105 +
106 +int if_global_nameindex(lua_State *L)
107 +{
108 + struct if_nameindex *ni, *oni;
109 + int i = 1;
110 + oni = ni = if_nameindex();
111 + lua_newtable(L);
112 + while (ni && ni->if_index && *(ni->if_name))
113 + {
114 + /* at result[i], we store.. */
115 + lua_pushnumber(L, i);
116 +
117 + /* new table with two items - index, name*/
118 + lua_newtable(L);
119 + lua_pushstring(L, "index");
120 + lua_pushnumber(L, ni->if_index);
121 + lua_settable(L, -3);
122 +
123 + lua_pushstring(L, "name");
124 + lua_pushstring(L, ni->if_name);
125 + lua_settable(L, -3);
126 +
127 + /* Then, actually store it */
128 + lua_settable(L, -3);
129 +
130 + i++;
131 + ni++;
132 + }
133 + if_freenameindex(oni);
134 + return 1;
135 +}
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 --- a/src/luasocket.c
167 +++ b/src/luasocket.c
168 @@ -21,6 +21,7 @@
169 #include "tcp.h"
170 #include "udp.h"
171 #include "select.h"
172 +#include "if.h"
173
174 /*-------------------------------------------------------------------------*\
175 * Internal function prototypes
176 @@ -41,6 +42,7 @@ static const luaL_Reg mod[] = {
177 {"tcp", tcp_open},
178 {"udp", udp_open},
179 {"select", select_open},
180 + {"iface", if_open},
181 {NULL, NULL}
182 };
183
184 --- a/src/makefile
185 +++ b/src/makefile
186 @@ -303,6 +303,7 @@ SOCKET_OBJS= \
187 compat.$(O) \
188 options.$(O) \
189 inet.$(O) \
190 + if.$(O) \
191 $(SOCKET) \
192 except.$(O) \
193 select.$(O) \
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
198 +if.$(O): if.c if.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 \
202 --- a/src/options.c
203 +++ b/src/options.c
204 @@ -7,7 +7,10 @@
205 #include "options.h"
206 #include "inet.h"
207 #include <string.h>
208 -
209 +#include <sys/types.h>
210 +#include <sys/socket.h>
211 +#include <net/if.h>
212 +
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)))) {
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 }