2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2024 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
24 #include <libloc/network.h>
30 typedef struct network
{
31 struct loc_network
* network
;
34 static Network
* luaL_checknetwork(lua_State
* L
, int i
) {
35 void* userdata
= luaL_checkudata(L
, i
, "location.Network");
37 // Throw an error if the argument doesn't match
38 luaL_argcheck(L
, userdata
, i
, "Network expected");
40 return (Network
*)userdata
;
43 int create_network(lua_State
* L
, struct loc_network
* network
) {
44 // Allocate a new object
45 Network
* self
= (Network
*)lua_newuserdata(L
, sizeof(*self
));
48 luaL_setmetatable(L
, "location.Network");
51 self
->network
= loc_network_ref(network
);
56 static int Network_new(lua_State
* L
) {
57 struct loc_network
* network
= NULL
;
62 n
= luaL_checkstring(L
, 1);
65 r
= loc_network_new_from_string(ctx
, &network
, n
);
67 return luaL_error(L
, "Could not create network %s: %s\n", n
, strerror(errno
));
70 r
= create_network(L
, network
);
71 loc_network_unref(network
);
76 static int Network_gc(lua_State
* L
) {
77 Network
* self
= luaL_checknetwork(L
, 1);
81 loc_network_unref(self
->network
);
88 static int Network_tostring(lua_State
* L
) {
89 Network
* self
= luaL_checknetwork(L
, 1);
91 // Push string representation of the network
92 lua_pushstring(L
, loc_network_str(self
->network
));
99 static int Network_get_asn(lua_State
* L
) {
100 Network
* self
= luaL_checknetwork(L
, 1);
102 uint32_t asn
= loc_network_get_asn(self
->network
);
106 lua_pushnumber(L
, asn
);
115 static int Network_get_family(lua_State
* L
) {
116 Network
* self
= luaL_checknetwork(L
, 1);
119 lua_pushnumber(L
, loc_network_address_family(self
->network
));
126 static int Network_get_country_code(lua_State
* L
) {
127 Network
* self
= luaL_checknetwork(L
, 1);
129 const char* country_code
= loc_network_get_country_code(self
->network
);
132 if (country_code
&& *country_code
)
133 lua_pushstring(L
, country_code
);
142 static int Network_has_flag(lua_State
* L
) {
143 Network
* self
= luaL_checknetwork(L
, 1);
146 int flag
= luaL_checknumber(L
, 2);
149 lua_pushboolean(L
, loc_network_has_flag(self
->network
, flag
));
156 static int Network_subnets(lua_State
* L
) {
157 struct loc_network
* subnet1
= NULL
;
158 struct loc_network
* subnet2
= NULL
;
161 Network
* self
= luaL_checknetwork(L
, 1);
164 r
= loc_network_subnets(self
->network
, &subnet1
, &subnet2
);
166 return luaL_error(L
, "Could not create subnets of %s: %s\n",
167 loc_network_str(self
->network
), strerror(errno
));
169 // Create a new table
170 lua_createtable(L
, 2, 0);
172 // Create the networks & push them onto the table
173 create_network(L
, subnet1
);
174 loc_network_unref(subnet1
);
175 lua_rawseti(L
, -2, 1);
177 create_network(L
, subnet2
);
178 loc_network_unref(subnet2
);
179 lua_rawseti(L
, -2, 2);
186 static int Network_reverse_pointer(lua_State
* L
) {
189 Network
* self
= luaL_checknetwork(L
, 1);
192 const char* suffix
= luaL_optstring(L
, 2, NULL
);
194 // Make the reverse pointer
195 rp
= loc_network_reverse_pointer(self
->network
, suffix
);
203 return luaL_error(L
, "Could not create reverse pointer: %s\n", strerror(errno
));
207 // Return the response
208 lua_pushstring(L
, rp
);
214 static const struct luaL_Reg Network_functions
[] = {
215 { "new", Network_new
},
216 { "get_asn", Network_get_asn
},
217 { "get_country_code", Network_get_country_code
},
218 { "get_family", Network_get_family
},
219 { "has_flag", Network_has_flag
},
220 { "reverse_pointer", Network_reverse_pointer
},
221 { "subnets", Network_subnets
},
222 { "__gc", Network_gc
},
223 { "__tostring", Network_tostring
},
227 int register_network(lua_State
* L
) {
228 return register_class(L
, "location.Network", Network_functions
);