2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
33 #include <sys/socket.h>
34 #include <netinet/in.h>
36 #include <arpa/inet.h>
39 #include "namespaces.hh"
42 #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
44 ** Adapted from Lua 5.2.0
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 */
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));
56 lua_pop(L
, nup
); /* remove upvalues */
61 /////////////////////////////////
63 static int l_new_ca(lua_State
* L
)
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);
73 static int l_ca_tostring(lua_State
* L
)
75 ComboAddress
* ca
= (ComboAddress
*)luaL_checkudata(L
, 1, "iputils.ca");
77 string ret
=ca
->toString();
78 lua_pushstring(L
, ret
.c_str());
82 static int l_ca_tostringWithPort(lua_State
* L
)
84 ComboAddress
* ca
= (ComboAddress
*)luaL_checkudata(L
, 1, "iputils.ca");
86 string ret
=ca
->toStringWithPort();
87 lua_pushstring(L
, ret
.c_str());
91 static int l_ca_equal(lua_State
* L
)
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
);
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
},
108 /////////////////////////////
110 typedef set
<ComboAddress
, ComboAddress::addressOnlyLessThan
> ourset_t
;
112 static int l_ipset_index(lua_State
* L
)
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);
124 static int l_ipset_newindex(lua_State
* L
)
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
);
132 static int l_newipset(lua_State
* L
)
134 new(lua_newuserdata(L
, sizeof(ourset_t
))) ourset_t();
135 luaL_getmetatable(L
, "iputils.ipset");
136 lua_setmetatable(L
, -2);
140 static int l_ipset_gc(lua_State
* L
)
142 ourset_t
*ourset
= (ourset_t
*)luaL_checkudata(L
, 1, "iputils.ipset");
147 static const struct luaL_Reg ipset_methods
[]={
148 {"__index", l_ipset_index
},
149 {"__newindex", l_ipset_newindex
},
150 {"__gc", l_ipset_gc
},
154 ////////////////////////////////////////////////////
157 static int l_netmask_tostring(lua_State
* L
)
159 Netmask
* nm
= (Netmask
*)luaL_checkudata(L
, 1, "iputils.netmask");
160 string ret
=nm
->toString();
161 lua_pushstring(L
, ret
.c_str());
165 static int l_new_netmask(lua_State
* L
)
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);
173 static int l_netmask_match(lua_State
* L
)
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
));
181 static int l_netmask_gc(lua_State
* L
)
183 Netmask
* nm
= (Netmask
*)luaL_checkudata(L
, 1, "iputils.netmask");
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
},
197 //////////////////////
199 static int l_nmgroup_tostring(lua_State
* L
)
201 NetmaskGroup
* nmg
= (NetmaskGroup
*)luaL_checkudata(L
, 1, "iputils.nmgroup");
203 string ret
=nmg
->toString();
204 lua_pushstring(L
, ret
.c_str());
208 static int l_new_nmgroup(lua_State
* L
)
210 /*NetmaskGroup*nmg= */ new(lua_newuserdata(L
, sizeof(NetmaskGroup
))) NetmaskGroup();
211 luaL_getmetatable(L
, "iputils.nmgroup");
212 lua_setmetatable(L
, -2);
216 static int l_nmgroup_match(lua_State
* L
)
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
));
224 static int l_nmgroup_add(lua_State
* L
)
226 NetmaskGroup
* nm
=(NetmaskGroup
*)luaL_checkudata(L
, 1, "iputils.nmgroup");
227 nm
->addMask(luaL_checkstring(L
, 2));
232 static int l_nmgroup_gc(lua_State
* L
)
234 NetmaskGroup
* nm
= (NetmaskGroup
*)luaL_checkudata(L
, 1, "iputils.nmgroup");
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
},
250 static const struct luaL_Reg iputils
[]={
252 {"newipset", l_newipset
},
253 {"newnm", l_new_netmask
},
254 {"newnmgroup", l_new_nmgroup
},
259 extern "C" int luaopen_iputils(lua_State
* L
)
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);
266 luaL_newmetatable(L
, "iputils.ipset");
267 lua_pushvalue(L
, -1);
268 lua_setfield(L
, -2, "__index");
269 luaL_setfuncs(L
, ipset_methods
, 0);
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);
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);
281 #if LUA_VERSION_NUM < 502
282 luaL_register(L
, "iputils", iputils
);
284 luaL_newlib(L
, iputils
);