]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/locationmodule.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / python / locationmodule.c
CommitLineData
18e2b14c
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
e17e804e 4 Copyright (C) 2017-2021 IPFire Development Team <info@ipfire.org>
18e2b14c
MT
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
17#include <Python.h>
c941dd31 18#include <syslog.h>
18e2b14c 19
c12a1385
MT
20#include <libloc/format.h>
21#include <libloc/resolv.h>
f4fef543 22
62ffafdd 23#include "locationmodule.h"
fadebc89 24#include "as.h"
af208e26 25#include "country.h"
9cdf6c53 26#include "database.h"
39967361 27#include "network.h"
d688e569 28#include "writer.h"
9cdf6c53 29
51173f45
MT
30/* Declare global context */
31struct loc_ctx* loc_ctx;
32
7c13a4e6 33PyMODINIT_FUNC PyInit__location(void);
18e2b14c 34
62ffafdd
MT
35static void location_free(void) {
36 // Release context
37 if (loc_ctx)
38 loc_unref(loc_ctx);
39}
40
c941dd31
MT
41static PyObject* set_log_level(PyObject* m, PyObject* args) {
42 int priority = LOG_INFO;
43
44 if (!PyArg_ParseTuple(args, "i", &priority))
45 return NULL;
46
47 loc_set_log_priority(loc_ctx, priority);
48
49 Py_RETURN_NONE;
50}
51
f4fef543 52static PyObject* discover_latest_version(PyObject* m, PyObject* args) {
a6f1e346 53 unsigned int version = LOC_DATABASE_VERSION_LATEST;
f4fef543 54
a6f1e346 55 if (!PyArg_ParseTuple(args, "|i", &version))
f4fef543
MT
56 return NULL;
57
58 time_t t = 0;
59
3ee36b5e
MT
60 int r = loc_discover_latest_version(loc_ctx, version, &t);
61 if (r)
62 Py_RETURN_NONE;
f4fef543
MT
63
64 return PyLong_FromUnsignedLong(t);
65}
66
0f0829ef
MT
67static PyObject* country_code_is_valid(PyObject* m, PyObject* args) {
68 const char* country_code = NULL;
69
70 if (!PyArg_ParseTuple(args, "s", &country_code))
71 return NULL;
72
73 if (loc_country_code_is_valid(country_code))
74 Py_RETURN_TRUE;
75
76 Py_RETURN_FALSE;
77}
78
18e2b14c 79static PyMethodDef location_module_methods[] = {
0f0829ef
MT
80 {
81 "country_code_is_valid",
82 (PyCFunction)country_code_is_valid,
83 METH_VARARGS,
84 NULL,
85 },
f4fef543
MT
86 {
87 "discover_latest_version",
88 (PyCFunction)discover_latest_version,
89 METH_VARARGS,
90 NULL,
91 },
c941dd31
MT
92 {
93 "set_log_level",
94 (PyCFunction)set_log_level,
95 METH_VARARGS,
96 NULL,
97 },
18e2b14c
MT
98 { NULL },
99};
100
101static struct PyModuleDef location_module = {
102 .m_base = PyModuleDef_HEAD_INIT,
7c13a4e6 103 .m_name = "_location",
18e2b14c
MT
104 .m_size = -1,
105 .m_doc = "Python module for libloc",
106 .m_methods = location_module_methods,
62ffafdd 107 .m_free = (freefunc)location_free,
18e2b14c
MT
108};
109
7c13a4e6 110PyMODINIT_FUNC PyInit__location(void) {
62ffafdd
MT
111 // Initialise loc context
112 int r = loc_new(&loc_ctx);
113 if (r)
114 return NULL;
115
18e2b14c
MT
116 PyObject* m = PyModule_Create(&location_module);
117 if (!m)
118 return NULL;
119
5653ad71
MT
120 // Version
121 if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION))
122 return NULL;
123
a0e8d454
MT
124 // Default Database Path
125 if (PyModule_AddStringConstant(m, "DATABASE_PATH", LIBLOC_DEFAULT_DATABASE_PATH))
126 return NULL;
127
fadebc89
MT
128 // AS
129 if (PyType_Ready(&ASType) < 0)
130 return NULL;
131
132 Py_INCREF(&ASType);
133 PyModule_AddObject(m, "AS", (PyObject *)&ASType);
134
af208e26
MT
135 // Country
136 if (PyType_Ready(&CountryType) < 0)
137 return NULL;
138
139 Py_INCREF(&CountryType);
140 PyModule_AddObject(m, "Country", (PyObject *)&CountryType);
141
9cdf6c53
MT
142 // Database
143 if (PyType_Ready(&DatabaseType) < 0)
144 return NULL;
145
146 Py_INCREF(&DatabaseType);
147 PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType);
148
afb426df
MT
149 // Database Enumerator
150 if (PyType_Ready(&DatabaseEnumeratorType) < 0)
151 return NULL;
152
153 Py_INCREF(&DatabaseEnumeratorType);
b4923a31 154 PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType);
afb426df 155
39967361
MT
156 // Network
157 if (PyType_Ready(&NetworkType) < 0)
158 return NULL;
159
160 Py_INCREF(&NetworkType);
161 PyModule_AddObject(m, "Network", (PyObject *)&NetworkType);
162
d688e569
MT
163 // Writer
164 if (PyType_Ready(&WriterType) < 0)
165 return NULL;
166
167 Py_INCREF(&WriterType);
168 PyModule_AddObject(m, "Writer", (PyObject *)&WriterType);
169
24ca7992 170 // Add flags
c1467f55 171 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY))
24ca7992
MT
172 return NULL;
173
c1467f55 174 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER))
24ca7992
MT
175 return NULL;
176
c1467f55 177 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST))
24ca7992
MT
178 return NULL;
179
e17e804e
PM
180 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_DROP", LOC_NETWORK_FLAG_DROP))
181 return NULL;
182
3ee36b5e
MT
183 // Add latest database version
184 if (PyModule_AddIntConstant(m, "DATABASE_VERSION_LATEST", LOC_DATABASE_VERSION_LATEST))
185 return NULL;
186
18e2b14c
MT
187 return m;
188}