]> git.ipfire.org Git - location/libloc.git/blame - src/lua/location.c
lua: Add compatibility function to compile with Lua >= 5.1
[location/libloc.git] / src / lua / location.c
CommitLineData
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"
031bae06 29#include "compat.h"
0ddfebb3 30#include "country.h"
7eaabd10 31#include "database.h"
e914e3ca 32#include "network.h"
6ae775f9 33
f51350bb
MT
34struct loc_ctx* ctx = NULL;
35
03b9c3a4
MT
36static int version(lua_State* L) {
37 lua_pushstring(L, PACKAGE_VERSION);
38 return 1;
39}
40
41static const struct luaL_Reg location_functions[] = {
42 { "version", version },
43 { NULL, NULL },
44};
45
6ae775f9 46int luaopen_location(lua_State* L) {
f51350bb
MT
47 int r;
48
49 // Initialize the context
50 r = loc_new(&ctx);
51 if (r)
52 return luaL_error(L,
53 "Could not initialize location context: %s\n", strerror(errno));
54
55 // Register functions
03b9c3a4
MT
56 luaL_newlib(L, location_functions);
57
67675dc3
MT
58 // Register AS type
59 register_as(L);
60
61 lua_setfield(L, -2, "AS");
62
0ddfebb3
MT
63 // Register Country type
64 register_country(L);
65
66 lua_setfield(L, -2, "Country");
67
7eaabd10
MT
68 // Register Database type
69 register_database(L);
70
71 lua_setfield(L, -2, "Database");
72
e914e3ca
MT
73 // Register Network type
74 register_network(L);
75
76 lua_setfield(L, -2, "Network");
77
726553e6
MT
78 // Set DATABASE_PATH
79 lua_pushstring(L, LIBLOC_DEFAULT_DATABASE_PATH);
80 lua_setfield(L, -2, "DATABASE_PATH");
81
db80a494
MT
82 // Add flags
83 lua_pushnumber(L, LOC_NETWORK_FLAG_ANONYMOUS_PROXY);
84 lua_setfield(L, -2, "NETWORK_FLAG_ANONYMOUS_PROXY");
85
86 lua_pushnumber(L, LOC_NETWORK_FLAG_SATELLITE_PROVIDER);
87 lua_setfield(L, -2, "NETWORK_FLAG_SATELLITE_PROVIDER");
88
89 lua_pushnumber(L, LOC_NETWORK_FLAG_ANYCAST);
90 lua_setfield(L, -2, "NETWORK_FLAG_ANYCAST");
91
92 lua_pushnumber(L, LOC_NETWORK_FLAG_DROP);
93 lua_setfield(L, -2, "NETWORK_FLAG_DROP");
94
03b9c3a4 95 return 1;
6ae775f9 96}