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