]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/locationmodule.c
46d9f83417416e05f8b10012574c9eaa6ca3a620
[people/ms/libloc.git] / src / python / locationmodule.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017-2021 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
17 #include <Python.h>
18 #include <syslog.h>
19
20 #include <libloc/format.h>
21 #include <libloc/resolv.h>
22
23 #include "locationmodule.h"
24 #include "as.h"
25 #include "country.h"
26 #include "database.h"
27 #include "network.h"
28 #include "writer.h"
29
30 /* Declare global context */
31 struct loc_ctx* loc_ctx;
32
33 PyMODINIT_FUNC PyInit__location(void);
34
35 static void location_free(void) {
36 // Release context
37 if (loc_ctx)
38 loc_unref(loc_ctx);
39 }
40
41 static 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
52 static PyObject* discover_latest_version(PyObject* m, PyObject* args) {
53 unsigned int version = LOC_DATABASE_VERSION_LATEST;
54
55 if (!PyArg_ParseTuple(args, "|i", &version))
56 return NULL;
57
58 time_t t = 0;
59
60 int r = loc_discover_latest_version(loc_ctx, version, &t);
61 if (r)
62 Py_RETURN_NONE;
63
64 return PyLong_FromUnsignedLong(t);
65 }
66
67 static 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
79 static PyMethodDef location_module_methods[] = {
80 {
81 "country_code_is_valid",
82 (PyCFunction)country_code_is_valid,
83 METH_VARARGS,
84 NULL,
85 },
86 {
87 "discover_latest_version",
88 (PyCFunction)discover_latest_version,
89 METH_VARARGS,
90 NULL,
91 },
92 {
93 "set_log_level",
94 (PyCFunction)set_log_level,
95 METH_VARARGS,
96 NULL,
97 },
98 { NULL },
99 };
100
101 static struct PyModuleDef location_module = {
102 .m_base = PyModuleDef_HEAD_INIT,
103 .m_name = "_location",
104 .m_size = -1,
105 .m_doc = "Python module for libloc",
106 .m_methods = location_module_methods,
107 .m_free = (freefunc)location_free,
108 };
109
110 PyMODINIT_FUNC PyInit__location(void) {
111 // Initialise loc context
112 int r = loc_new(&loc_ctx);
113 if (r)
114 return NULL;
115
116 PyObject* m = PyModule_Create(&location_module);
117 if (!m)
118 return NULL;
119
120 // Version
121 if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION))
122 return NULL;
123
124 // AS
125 if (PyType_Ready(&ASType) < 0)
126 return NULL;
127
128 Py_INCREF(&ASType);
129 PyModule_AddObject(m, "AS", (PyObject *)&ASType);
130
131 // Country
132 if (PyType_Ready(&CountryType) < 0)
133 return NULL;
134
135 Py_INCREF(&CountryType);
136 PyModule_AddObject(m, "Country", (PyObject *)&CountryType);
137
138 // Database
139 if (PyType_Ready(&DatabaseType) < 0)
140 return NULL;
141
142 Py_INCREF(&DatabaseType);
143 PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType);
144
145 // Database Enumerator
146 if (PyType_Ready(&DatabaseEnumeratorType) < 0)
147 return NULL;
148
149 Py_INCREF(&DatabaseEnumeratorType);
150 PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType);
151
152 // Network
153 if (PyType_Ready(&NetworkType) < 0)
154 return NULL;
155
156 Py_INCREF(&NetworkType);
157 PyModule_AddObject(m, "Network", (PyObject *)&NetworkType);
158
159 // Writer
160 if (PyType_Ready(&WriterType) < 0)
161 return NULL;
162
163 Py_INCREF(&WriterType);
164 PyModule_AddObject(m, "Writer", (PyObject *)&WriterType);
165
166 // Add flags
167 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY))
168 return NULL;
169
170 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER))
171 return NULL;
172
173 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST))
174 return NULL;
175
176 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_DROP", LOC_NETWORK_FLAG_DROP))
177 return NULL;
178
179 // Add latest database version
180 if (PyModule_AddIntConstant(m, "DATABASE_VERSION_LATEST", LOC_DATABASE_VERSION_LATEST))
181 return NULL;
182
183 return m;
184 }