]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lua-iputils.cc
Standardize license text in all PDNS files
[thirdparty/pdns.git] / pdns / lua-iputils.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #ifdef HAVE_LUA
26 extern "C" {
27 #include <lua.h>
28 #include <lauxlib.h>
29 }
30 #include <iostream>
31 #include "iputils.hh"
32 #include <string>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <stdlib.h>
36 #include <arpa/inet.h>
37 #include <string.h>
38 #include <netdb.h>
39 #include "namespaces.hh"
40 #undef L
41
42 #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
43 /*
44 ** Adapted from Lua 5.2.0
45 */
46 static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
47 luaL_checkstack(L, nup+1, "too many upvalues");
48 for (; l->name != NULL; l++) { /* fill the table with given functions */
49 int i;
50 lua_pushstring(L, l->name);
51 for (i = 0; i < nup; i++) /* copy upvalues to the top */
52 lua_pushvalue(L, -(nup+1));
53 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
54 lua_settable(L, -(nup + 3));
55 }
56 lua_pop(L, nup); /* remove upvalues */
57 }
58 #endif
59
60
61 /////////////////////////////////
62
63 static int l_new_ca(lua_State* L)
64 {
65 ComboAddress* ca=(ComboAddress*)lua_newuserdata(L, sizeof(ComboAddress));
66 memset(ca, 0, sizeof(ComboAddress));
67 *ca=ComboAddress(luaL_checkstring(L, 1));
68 luaL_getmetatable(L, "iputils.ca");
69 lua_setmetatable(L, -2);
70 return 1;
71 }
72
73 static int l_ca_tostring(lua_State* L)
74 {
75 ComboAddress* ca = (ComboAddress*)luaL_checkudata(L, 1, "iputils.ca");
76
77 string ret=ca->toString();
78 lua_pushstring(L, ret.c_str());
79 return 1;
80 }
81
82 static int l_ca_tostringWithPort(lua_State* L)
83 {
84 ComboAddress* ca = (ComboAddress*)luaL_checkudata(L, 1, "iputils.ca");
85
86 string ret=ca->toStringWithPort();
87 lua_pushstring(L, ret.c_str());
88 return 1;
89 }
90
91 static int l_ca_equal(lua_State* L)
92 {
93 ComboAddress* ca1 = (ComboAddress*)luaL_checkudata(L, 1, "iputils.ca");
94 ComboAddress* ca2 = (ComboAddress*)luaL_checkudata(L, 2, "iputils.ca");
95 lua_pushboolean(L, *ca1==*ca2);
96 return 1;
97 }
98
99 static const struct luaL_Reg iputils_ca_methods[]={
100 {"tostring", l_ca_tostring},
101 {"__tostring", l_ca_tostring},
102 {"__eq", l_ca_equal},
103 {"tostringwithport", l_ca_tostringWithPort},
104 {NULL, NULL}
105 };
106
107
108 /////////////////////////////
109
110 typedef set<ComboAddress, ComboAddress::addressOnlyLessThan> ourset_t;
111
112 static int l_ipset_index(lua_State* L)
113 {
114 ourset_t *ourset = (ourset_t*)luaL_checkudata(L, 1, "iputils.ipset");
115 ComboAddress* ca1 = (ComboAddress*)luaL_checkudata(L, 2, "iputils.ca");
116 if(ourset->count(*ca1)) {
117 lua_pushboolean(L, 1);
118 return 1;
119 }
120
121 return 0;
122 }
123
124 static int l_ipset_newindex(lua_State* L)
125 {
126 ourset_t*ourset = (ourset_t*)luaL_checkudata(L, 1, "iputils.ipset");
127 ComboAddress* ca1 = (ComboAddress*)luaL_checkudata(L, 2, "iputils.ca");
128 ourset->insert(*ca1);
129 return 0;
130 }
131
132 static int l_newipset(lua_State* L)
133 {
134 new(lua_newuserdata(L, sizeof(ourset_t))) ourset_t();
135 luaL_getmetatable(L, "iputils.ipset");
136 lua_setmetatable(L, -2);
137 return 1;
138 }
139
140 static int l_ipset_gc(lua_State* L)
141 {
142 ourset_t*ourset = (ourset_t*)luaL_checkudata(L, 1, "iputils.ipset");
143 ourset->~ourset_t();
144 return 0;
145 }
146
147 static const struct luaL_Reg ipset_methods[]={
148 {"__index", l_ipset_index},
149 {"__newindex", l_ipset_newindex},
150 {"__gc", l_ipset_gc},
151 {NULL, NULL}
152 };
153
154 ////////////////////////////////////////////////////
155
156
157 static int l_netmask_tostring(lua_State* L)
158 {
159 Netmask* nm = (Netmask*)luaL_checkudata(L, 1, "iputils.netmask");
160 string ret=nm->toString();
161 lua_pushstring(L, ret.c_str());
162 return 1;
163 }
164
165 static int l_new_netmask(lua_State* L)
166 {
167 /*Netmask* nm=*/ new(lua_newuserdata(L, sizeof(Netmask))) Netmask(luaL_checkstring(L, 1));
168 luaL_getmetatable(L, "iputils.netmask");
169 lua_setmetatable(L, -2);
170 return 1;
171 }
172
173 static int l_netmask_match(lua_State* L)
174 {
175 Netmask* nm=(Netmask*)luaL_checkudata(L, 1, "iputils.netmask");
176 ComboAddress* ca1 = (ComboAddress*)luaL_checkudata(L, 2, "iputils.ca");
177 lua_pushboolean(L, nm->match(*ca1));
178 return 1;
179 }
180
181 static int l_netmask_gc(lua_State* L)
182 {
183 Netmask* nm = (Netmask*)luaL_checkudata(L, 1, "iputils.netmask");
184 nm->~Netmask();
185 return 0;
186 }
187
188 static const struct luaL_Reg iputils_netmask_methods[]={
189 {"__tostring", l_netmask_tostring},
190 {"tostring", l_netmask_tostring},
191 {"match", l_netmask_match},
192 {"__gc", l_netmask_gc},
193 {NULL, NULL}
194 };
195
196
197 //////////////////////
198
199 static int l_nmgroup_tostring(lua_State* L)
200 {
201 NetmaskGroup* nmg = (NetmaskGroup*)luaL_checkudata(L, 1, "iputils.nmgroup");
202
203 string ret=nmg->toString();
204 lua_pushstring(L, ret.c_str());
205 return 1;
206 }
207
208 static int l_new_nmgroup(lua_State* L)
209 {
210 /*NetmaskGroup*nmg= */ new(lua_newuserdata(L, sizeof(NetmaskGroup))) NetmaskGroup();
211 luaL_getmetatable(L, "iputils.nmgroup");
212 lua_setmetatable(L, -2);
213 return 1;
214 }
215
216 static int l_nmgroup_match(lua_State* L)
217 {
218 NetmaskGroup* nm=(NetmaskGroup*)luaL_checkudata(L, 1, "iputils.nmgroup");
219 ComboAddress* ca1 = (ComboAddress*)luaL_checkudata(L, 2, "iputils.ca");
220 lua_pushboolean(L, nm->match(*ca1));
221 return 1;
222 }
223
224 static int l_nmgroup_add(lua_State* L)
225 {
226 NetmaskGroup* nm=(NetmaskGroup*)luaL_checkudata(L, 1, "iputils.nmgroup");
227 nm->addMask(luaL_checkstring(L, 2));
228 return 0;
229 }
230
231
232 static int l_nmgroup_gc(lua_State* L)
233 {
234 NetmaskGroup* nm = (NetmaskGroup*)luaL_checkudata(L, 1, "iputils.nmgroup");
235 nm->~NetmaskGroup();
236 return 0;
237 }
238
239 static const struct luaL_Reg iputils_nmgroup_methods[]={
240 {"__tostring", l_nmgroup_tostring},
241 {"tostring", l_nmgroup_tostring},
242 {"match", l_nmgroup_match},
243 {"add", l_nmgroup_add},
244 {"__gc", l_nmgroup_gc},
245 {NULL, NULL}
246 };
247
248 ////////////
249
250 static const struct luaL_Reg iputils[]={
251 {"newca", l_new_ca},
252 {"newipset", l_newipset},
253 {"newnm", l_new_netmask},
254 {"newnmgroup", l_new_nmgroup},
255 {NULL, NULL}
256 };
257
258
259 extern "C" int luaopen_iputils(lua_State* L)
260 {
261 luaL_newmetatable(L, "iputils.ca");
262 lua_pushvalue(L, -1);
263 lua_setfield(L, -2, "__index");
264 luaL_setfuncs(L, iputils_ca_methods, 0);
265
266 luaL_newmetatable(L, "iputils.ipset");
267 lua_pushvalue(L, -1);
268 lua_setfield(L, -2, "__index");
269 luaL_setfuncs(L, ipset_methods, 0);
270
271 luaL_newmetatable(L, "iputils.netmask");
272 lua_pushvalue(L, -1);
273 lua_setfield(L, -2, "__index");
274 luaL_setfuncs(L, iputils_netmask_methods, 0);
275
276 luaL_newmetatable(L, "iputils.nmgroup");
277 lua_pushvalue(L, -1);
278 lua_setfield(L, -2, "__index");
279 luaL_setfuncs(L, iputils_nmgroup_methods, 0);
280
281 #if LUA_VERSION_NUM < 502
282 luaL_register(L, "iputils", iputils);
283 #else
284 luaL_newlib(L, iputils);
285 #endif
286 return 1;
287 }
288
289 #endif