]>
Commit | Line | Data |
---|---|---|
6ae775f9 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2024 IPFire Development Team <info@ipfire.org> | |
5 | ||
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. | |
10 | ||
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. | |
15 | */ | |
16 | ||
f51350bb MT |
17 | #include <errno.h> |
18 | #include <string.h> | |
19 | ||
6ae775f9 | 20 | #include <lua.h> |
03b9c3a4 MT |
21 | #include <lauxlib.h> |
22 | #include <lualib.h> | |
6ae775f9 | 23 | |
f51350bb | 24 | #include <libloc/libloc.h> |
db80a494 | 25 | #include <libloc/network.h> |
f51350bb | 26 | |
6ae775f9 | 27 | #include "location.h" |
67675dc3 | 28 | #include "as.h" |
0ddfebb3 | 29 | #include "country.h" |
7eaabd10 | 30 | #include "database.h" |
e914e3ca | 31 | #include "network.h" |
6ae775f9 | 32 | |
f51350bb MT |
33 | struct loc_ctx* ctx = NULL; |
34 | ||
03b9c3a4 MT |
35 | static int version(lua_State* L) { |
36 | lua_pushstring(L, PACKAGE_VERSION); | |
37 | return 1; | |
38 | } | |
39 | ||
40 | static const struct luaL_Reg location_functions[] = { | |
41 | { "version", version }, | |
42 | { NULL, NULL }, | |
43 | }; | |
44 | ||
6ae775f9 | 45 | int luaopen_location(lua_State* L) { |
f51350bb MT |
46 | int r; |
47 | ||
48 | // Initialize the context | |
49 | r = loc_new(&ctx); | |
50 | if (r) | |
51 | return luaL_error(L, | |
52 | "Could not initialize location context: %s\n", strerror(errno)); | |
53 | ||
54 | // Register functions | |
03b9c3a4 MT |
55 | luaL_newlib(L, location_functions); |
56 | ||
67675dc3 MT |
57 | // Register AS type |
58 | register_as(L); | |
59 | ||
60 | lua_setfield(L, -2, "AS"); | |
61 | ||
0ddfebb3 MT |
62 | // Register Country type |
63 | register_country(L); | |
64 | ||
65 | lua_setfield(L, -2, "Country"); | |
66 | ||
7eaabd10 MT |
67 | // Register Database type |
68 | register_database(L); | |
69 | ||
70 | lua_setfield(L, -2, "Database"); | |
71 | ||
e914e3ca MT |
72 | // Register Network type |
73 | register_network(L); | |
74 | ||
75 | lua_setfield(L, -2, "Network"); | |
76 | ||
726553e6 MT |
77 | // Set DATABASE_PATH |
78 | lua_pushstring(L, LIBLOC_DEFAULT_DATABASE_PATH); | |
79 | lua_setfield(L, -2, "DATABASE_PATH"); | |
80 | ||
db80a494 MT |
81 | // Add flags |
82 | lua_pushnumber(L, LOC_NETWORK_FLAG_ANONYMOUS_PROXY); | |
83 | lua_setfield(L, -2, "NETWORK_FLAG_ANONYMOUS_PROXY"); | |
84 | ||
85 | lua_pushnumber(L, LOC_NETWORK_FLAG_SATELLITE_PROVIDER); | |
86 | lua_setfield(L, -2, "NETWORK_FLAG_SATELLITE_PROVIDER"); | |
87 | ||
88 | lua_pushnumber(L, LOC_NETWORK_FLAG_ANYCAST); | |
89 | lua_setfield(L, -2, "NETWORK_FLAG_ANYCAST"); | |
90 | ||
91 | lua_pushnumber(L, LOC_NETWORK_FLAG_DROP); | |
92 | lua_setfield(L, -2, "NETWORK_FLAG_DROP"); | |
93 | ||
03b9c3a4 | 94 | return 1; |
6ae775f9 | 95 | } |