]>
Commit | Line | Data |
---|---|---|
9cdf6c53 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 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 | ||
9fc7f001 MT |
19 | #include <loc/libloc.h> |
20 | #include <loc/database.h> | |
21 | ||
1da9cd39 | 22 | #include "locationmodule.h" |
86ca7ef7 | 23 | #include "as.h" |
9cdf6c53 | 24 | #include "database.h" |
31edab76 | 25 | #include "network.h" |
9cdf6c53 MT |
26 | |
27 | static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
9cdf6c53 | 28 | DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0); |
9cdf6c53 MT |
29 | |
30 | return (PyObject*)self; | |
31 | } | |
32 | ||
33 | static void Database_dealloc(DatabaseObject* self) { | |
34 | if (self->db) | |
35 | loc_database_unref(self->db); | |
36 | ||
6fd96715 MT |
37 | if (self->path) |
38 | free(self->path); | |
39 | ||
9cdf6c53 MT |
40 | Py_TYPE(self)->tp_free((PyObject* )self); |
41 | } | |
42 | ||
43 | static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) { | |
44 | const char* path = NULL; | |
45 | ||
46 | if (!PyArg_ParseTuple(args, "s", &path)) | |
47 | return -1; | |
48 | ||
6fd96715 MT |
49 | self->path = strdup(path); |
50 | ||
9cdf6c53 | 51 | // Open the file for reading |
6fd96715 | 52 | FILE* f = fopen(self->path, "r"); |
9cdf6c53 MT |
53 | if (!f) |
54 | return -1; | |
55 | ||
56 | // Load the database | |
38e07ee0 | 57 | int r = loc_database_new(loc_ctx, &self->db, f); |
9cdf6c53 MT |
58 | fclose(f); |
59 | ||
60 | // Return on any errors | |
61 | if (r) | |
62 | return -1; | |
63 | ||
64 | return 0; | |
65 | } | |
66 | ||
6fd96715 MT |
67 | static PyObject* Database_repr(DatabaseObject* self) { |
68 | return PyUnicode_FromFormat("<Database %s>", self->path); | |
69 | } | |
70 | ||
d99b0256 MT |
71 | static PyObject* Database_get_description(DatabaseObject* self) { |
72 | const char* description = loc_database_get_description(self->db); | |
73 | ||
74 | return PyUnicode_FromString(description); | |
75 | } | |
76 | ||
77 | static PyObject* Database_get_vendor(DatabaseObject* self) { | |
78 | const char* vendor = loc_database_get_vendor(self->db); | |
79 | ||
80 | return PyUnicode_FromString(vendor); | |
81 | } | |
82 | ||
4bf49d00 MT |
83 | static PyObject* Database_get_license(DatabaseObject* self) { |
84 | const char* license = loc_database_get_license(self->db); | |
85 | ||
86 | return PyUnicode_FromString(license); | |
87 | } | |
88 | ||
53524b2d MT |
89 | static PyObject* Database_get_created_at(DatabaseObject* self) { |
90 | time_t created_at = loc_database_created_at(self->db); | |
91 | ||
92 | return PyLong_FromLong(created_at); | |
93 | } | |
94 | ||
86ca7ef7 MT |
95 | static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) { |
96 | struct loc_as* as = NULL; | |
97 | uint32_t number = 0; | |
98 | ||
99 | if (!PyArg_ParseTuple(args, "i", &number)) | |
100 | return NULL; | |
101 | ||
102 | // Try to retrieve the AS | |
103 | int r = loc_database_get_as(self->db, &as, number); | |
86ca7ef7 | 104 | |
4a0a0f7e MT |
105 | // We got an AS |
106 | if (r == 0) { | |
86ca7ef7 MT |
107 | PyObject* obj = new_as(&ASType, as); |
108 | loc_as_unref(as); | |
109 | ||
110 | return obj; | |
86ca7ef7 MT |
111 | |
112 | // Nothing found | |
4a0a0f7e MT |
113 | } else if (r == 1) { |
114 | Py_RETURN_NONE; | |
115 | } | |
116 | ||
117 | // Unexpected error | |
118 | return NULL; | |
86ca7ef7 MT |
119 | } |
120 | ||
31edab76 MT |
121 | static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) { |
122 | struct loc_network* network = NULL; | |
123 | const char* address = NULL; | |
124 | ||
125 | if (!PyArg_ParseTuple(args, "s", &address)) | |
126 | return NULL; | |
127 | ||
128 | // Try to retrieve a matching network | |
129 | int r = loc_database_lookup_from_string(self->db, address, &network); | |
130 | ||
131 | // We got a network | |
132 | if (r == 0) { | |
133 | PyObject* obj = new_network(&NetworkType, network); | |
134 | loc_network_unref(network); | |
135 | ||
136 | return obj; | |
137 | ||
138 | // Nothing found | |
139 | } else if (r == 1) { | |
140 | Py_RETURN_NONE; | |
927e82f2 MT |
141 | |
142 | // Invalid input | |
143 | } else if (r == -EINVAL) { | |
144 | PyErr_Format(PyExc_ValueError, "Invalid IP address: %s", address); | |
145 | return NULL; | |
31edab76 MT |
146 | } |
147 | ||
148 | // Unexpected error | |
149 | return NULL; | |
150 | } | |
151 | ||
afb426df MT |
152 | static PyObject* new_database_enumerator(PyTypeObject* type, struct loc_database_enumerator* enumerator) { |
153 | DatabaseEnumeratorObject* self = (DatabaseEnumeratorObject*)type->tp_alloc(type, 0); | |
154 | if (self) { | |
155 | self->enumerator = loc_database_enumerator_ref(enumerator); | |
156 | } | |
157 | ||
158 | return (PyObject*)self; | |
159 | } | |
160 | ||
161 | static PyObject* Database_search_as(DatabaseObject* self, PyObject* args) { | |
162 | const char* string = NULL; | |
163 | ||
164 | if (!PyArg_ParseTuple(args, "s", &string)) | |
165 | return NULL; | |
166 | ||
167 | struct loc_database_enumerator* enumerator; | |
168 | ||
169 | int r = loc_database_enumerator_new(&enumerator, self->db); | |
170 | if (r) { | |
171 | PyErr_SetFromErrno(PyExc_SystemError); | |
172 | return NULL; | |
173 | } | |
174 | ||
175 | // Search string we are searching for | |
176 | loc_database_enumerator_set_string(enumerator, string); | |
177 | ||
178 | PyObject* obj = new_database_enumerator(&DatabaseEnumeratorType, enumerator); | |
179 | loc_database_enumerator_unref(enumerator); | |
180 | ||
181 | return obj; | |
182 | } | |
183 | ||
9cdf6c53 | 184 | static struct PyMethodDef Database_methods[] = { |
86ca7ef7 MT |
185 | { |
186 | "get_as", | |
187 | (PyCFunction)Database_get_as, | |
188 | METH_VARARGS, | |
189 | NULL, | |
190 | }, | |
31edab76 MT |
191 | { |
192 | "lookup", | |
193 | (PyCFunction)Database_lookup, | |
194 | METH_VARARGS, | |
195 | NULL, | |
196 | }, | |
afb426df MT |
197 | { |
198 | "search_as", | |
199 | (PyCFunction)Database_search_as, | |
200 | METH_VARARGS, | |
201 | NULL, | |
202 | }, | |
9cdf6c53 MT |
203 | { NULL }, |
204 | }; | |
205 | ||
d99b0256 | 206 | static struct PyGetSetDef Database_getsetters[] = { |
53524b2d MT |
207 | { |
208 | "created_at", | |
209 | (getter)Database_get_created_at, | |
210 | NULL, | |
211 | NULL, | |
212 | NULL, | |
213 | }, | |
d99b0256 MT |
214 | { |
215 | "description", | |
216 | (getter)Database_get_description, | |
217 | NULL, | |
218 | NULL, | |
219 | NULL, | |
220 | }, | |
4bf49d00 MT |
221 | { |
222 | "license", | |
223 | (getter)Database_get_license, | |
224 | NULL, | |
225 | NULL, | |
226 | NULL, | |
227 | }, | |
d99b0256 MT |
228 | { |
229 | "vendor", | |
230 | (getter)Database_get_vendor, | |
231 | NULL, | |
232 | NULL, | |
53524b2d | 233 | NULL, |
d99b0256 MT |
234 | }, |
235 | { NULL }, | |
236 | }; | |
237 | ||
9cdf6c53 MT |
238 | PyTypeObject DatabaseType = { |
239 | PyVarObject_HEAD_INIT(NULL, 0) | |
240 | tp_name: "location.Database", | |
241 | tp_basicsize: sizeof(DatabaseObject), | |
242 | tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, | |
243 | tp_new: Database_new, | |
244 | tp_dealloc: (destructor)Database_dealloc, | |
245 | tp_init: (initproc)Database_init, | |
246 | tp_doc: "Database object", | |
247 | tp_methods: Database_methods, | |
d99b0256 | 248 | tp_getset: Database_getsetters, |
6fd96715 | 249 | tp_repr: (reprfunc)Database_repr, |
9cdf6c53 | 250 | }; |
afb426df MT |
251 | |
252 | static PyObject* DatabaseEnumerator_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
253 | DatabaseEnumeratorObject* self = (DatabaseEnumeratorObject*)type->tp_alloc(type, 0); | |
254 | ||
255 | return (PyObject*)self; | |
256 | } | |
257 | ||
258 | static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject* self) { | |
259 | loc_database_enumerator_unref(self->enumerator); | |
260 | ||
261 | Py_TYPE(self)->tp_free((PyObject* )self); | |
262 | } | |
263 | ||
264 | static PyObject* DatabaseEnumerator_next(DatabaseEnumeratorObject* self) { | |
265 | struct loc_as* as = loc_database_enumerator_next_as(self->enumerator); | |
266 | if (as) { | |
267 | PyObject* obj = new_as(&ASType, as); | |
268 | loc_as_unref(as); | |
269 | ||
270 | return obj; | |
271 | } | |
272 | ||
273 | // Nothing found, that means the end | |
274 | PyErr_SetNone(PyExc_StopIteration); | |
275 | return NULL; | |
276 | } | |
277 | ||
278 | PyTypeObject DatabaseEnumeratorType = { | |
279 | PyVarObject_HEAD_INIT(NULL, 0) | |
280 | tp_name: "location.DatabaseEnumerator", | |
281 | tp_basicsize: sizeof(DatabaseEnumeratorObject), | |
282 | tp_flags: Py_TPFLAGS_DEFAULT, | |
283 | tp_alloc: PyType_GenericAlloc, | |
284 | tp_new: DatabaseEnumerator_new, | |
285 | tp_dealloc: (destructor)DatabaseEnumerator_dealloc, | |
286 | tp_iter: PyObject_SelfIter, | |
287 | tp_iternext: (iternextfunc)DatabaseEnumerator_next, | |
288 | }; |