2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
19 #include <libloc/libloc.h>
20 #include <libloc/as.h>
21 #include <libloc/as-list.h>
22 #include <libloc/database.h>
24 #include "locationmodule.h"
30 static PyObject
* Database_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
31 DatabaseObject
* self
= (DatabaseObject
*)type
->tp_alloc(type
, 0);
33 return (PyObject
*)self
;
36 static void Database_dealloc(DatabaseObject
* self
) {
38 loc_database_unref(self
->db
);
43 Py_TYPE(self
)->tp_free((PyObject
* )self
);
46 static int Database_init(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
47 const char* path
= NULL
;
51 if (!PyArg_ParseTuple(args
, "s", &path
))
55 self
->path
= strdup(path
);
59 // Open the file for reading
60 f
= fopen(self
->path
, "r");
65 int r
= loc_database_new(loc_ctx
, &self
->db
, f
);
76 PyErr_SetFromErrno(PyExc_OSError
);
80 static PyObject
* Database_repr(DatabaseObject
* self
) {
81 return PyUnicode_FromFormat("<Database %s>", self
->path
);
84 static PyObject
* Database_verify(DatabaseObject
* self
, PyObject
* args
) {
85 PyObject
* public_key
= NULL
;
89 if (!PyArg_ParseTuple(args
, "O", &public_key
))
93 int fd
= PyObject_AsFileDescriptor(public_key
);
97 // Re-open file descriptor
100 PyErr_SetFromErrno(PyExc_IOError
);
104 int r
= loc_database_verify(self
->db
, f
);
112 static PyObject
* Database_get_description(DatabaseObject
* self
) {
113 const char* description
= loc_database_get_description(self
->db
);
117 return PyUnicode_FromString(description
);
120 static PyObject
* Database_get_vendor(DatabaseObject
* self
) {
121 const char* vendor
= loc_database_get_vendor(self
->db
);
125 return PyUnicode_FromString(vendor
);
128 static PyObject
* Database_get_license(DatabaseObject
* self
) {
129 const char* license
= loc_database_get_license(self
->db
);
133 return PyUnicode_FromString(license
);
136 static PyObject
* Database_get_created_at(DatabaseObject
* self
) {
137 time_t created_at
= loc_database_created_at(self
->db
);
139 return PyLong_FromLong(created_at
);
142 static PyObject
* Database_get_as(DatabaseObject
* self
, PyObject
* args
) {
143 struct loc_as
* as
= NULL
;
146 if (!PyArg_ParseTuple(args
, "i", &number
))
149 // Try to retrieve the AS
150 int r
= loc_database_get_as(self
->db
, &as
, number
);
154 PyObject
* obj
= new_as(&ASType
, as
);
168 static PyObject
* Database_get_country(DatabaseObject
* self
, PyObject
* args
) {
169 const char* country_code
= NULL
;
171 if (!PyArg_ParseTuple(args
, "s", &country_code
))
174 struct loc_country
* country
;
175 int r
= loc_database_get_country(self
->db
, &country
, country_code
);
180 PyObject
* obj
= new_country(&CountryType
, country
);
181 loc_country_unref(country
);
186 static PyObject
* Database_lookup(DatabaseObject
* self
, PyObject
* args
) {
187 struct loc_network
* network
= NULL
;
188 const char* address
= NULL
;
190 if (!PyArg_ParseTuple(args
, "s", &address
))
193 // Try to retrieve a matching network
194 int r
= loc_database_lookup_from_string(self
->db
, address
, &network
);
198 PyObject
* obj
= new_network(&NetworkType
, network
);
199 loc_network_unref(network
);
211 PyErr_Format(PyExc_ValueError
, "Invalid IP address: %s", address
);
214 PyErr_SetFromErrno(PyExc_OSError
);
220 static PyObject
* new_database_enumerator(PyTypeObject
* type
, struct loc_database_enumerator
* enumerator
) {
221 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
223 self
->enumerator
= loc_database_enumerator_ref(enumerator
);
226 return (PyObject
*)self
;
229 static PyObject
* Database_iterate_all(DatabaseObject
* self
,
230 enum loc_database_enumerator_mode what
, int family
, int flags
) {
231 struct loc_database_enumerator
* enumerator
;
233 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, what
, flags
);
235 PyErr_SetFromErrno(PyExc_SystemError
);
241 loc_database_enumerator_set_family(enumerator
, family
);
243 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
244 loc_database_enumerator_unref(enumerator
);
249 static PyObject
* Database_ases(DatabaseObject
* self
) {
250 return Database_iterate_all(self
, LOC_DB_ENUMERATE_ASES
, AF_UNSPEC
, 0);
253 static PyObject
* Database_search_as(DatabaseObject
* self
, PyObject
* args
) {
254 const char* string
= NULL
;
256 if (!PyArg_ParseTuple(args
, "s", &string
))
259 struct loc_database_enumerator
* enumerator
;
261 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_ASES
, 0);
263 PyErr_SetFromErrno(PyExc_SystemError
);
267 // Search string we are searching for
268 loc_database_enumerator_set_string(enumerator
, string
);
270 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
271 loc_database_enumerator_unref(enumerator
);
276 static PyObject
* Database_networks(DatabaseObject
* self
) {
277 return Database_iterate_all(self
, LOC_DB_ENUMERATE_NETWORKS
, AF_UNSPEC
, 0);
280 static PyObject
* Database_networks_flattened(DatabaseObject
*self
) {
281 return Database_iterate_all(self
, LOC_DB_ENUMERATE_NETWORKS
, AF_UNSPEC
,
282 LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
285 static PyObject
* Database_search_networks(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
286 char* kwlist
[] = { "country_codes", "asns", "flags", "family", "flatten", NULL
};
287 PyObject
* country_codes
= NULL
;
288 PyObject
* asn_list
= NULL
;
293 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O!O!iip", kwlist
,
294 &PyList_Type
, &country_codes
, &PyList_Type
, &asn_list
, &flags
, &family
, &flatten
))
297 struct loc_database_enumerator
* enumerator
;
298 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_NETWORKS
,
299 (flatten
) ? LOC_DB_ENUMERATOR_FLAGS_FLATTEN
: 0);
301 PyErr_SetFromErrno(PyExc_SystemError
);
305 // Set country code we are searching for
307 struct loc_country_list
* countries
;
308 r
= loc_country_list_new(loc_ctx
, &countries
);
310 PyErr_SetString(PyExc_SystemError
, "Could not create country list");
314 for (int i
= 0; i
< PyList_Size(country_codes
); i
++) {
315 PyObject
* item
= PyList_GetItem(country_codes
, i
);
317 if (!PyUnicode_Check(item
)) {
318 PyErr_SetString(PyExc_TypeError
, "Country codes must be strings");
319 loc_country_list_unref(countries
);
323 const char* country_code
= PyUnicode_AsUTF8(item
);
325 struct loc_country
* country
;
326 r
= loc_country_new(loc_ctx
, &country
, country_code
);
329 PyErr_Format(PyExc_ValueError
, "Invalid country code: %s", country_code
);
331 PyErr_SetString(PyExc_SystemError
, "Could not create country");
334 loc_country_list_unref(countries
);
338 // Append it to the list
339 r
= loc_country_list_append(countries
, country
);
341 PyErr_SetString(PyExc_SystemError
, "Could not append country to the list");
343 loc_country_list_unref(countries
);
344 loc_country_unref(country
);
348 loc_country_unref(country
);
351 r
= loc_database_enumerator_set_countries(enumerator
, countries
);
353 PyErr_SetFromErrno(PyExc_SystemError
);
355 loc_country_list_unref(countries
);
359 loc_country_list_unref(countries
);
362 // Set the ASN we are searching for
364 struct loc_as_list
* asns
;
365 r
= loc_as_list_new(loc_ctx
, &asns
);
367 PyErr_SetFromErrno(PyExc_OSError
);
371 for (int i
= 0; i
< PyList_Size(asn_list
); i
++) {
372 PyObject
* item
= PyList_GetItem(asn_list
, i
);
374 if (!PyLong_Check(item
)) {
375 PyErr_SetString(PyExc_TypeError
, "ASNs must be numbers");
377 loc_as_list_unref(asns
);
381 unsigned long number
= PyLong_AsLong(item
);
384 r
= loc_as_new(loc_ctx
, &as
, number
);
386 PyErr_SetFromErrno(PyExc_OSError
);
388 loc_as_list_unref(asns
);
393 r
= loc_as_list_append(asns
, as
);
395 PyErr_SetFromErrno(PyExc_OSError
);
397 loc_as_list_unref(asns
);
405 r
= loc_database_enumerator_set_asns(enumerator
, asns
);
407 PyErr_SetFromErrno(PyExc_OSError
);
409 loc_as_list_unref(asns
);
413 loc_as_list_unref(asns
);
416 // Set the flags we are searching for
418 r
= loc_database_enumerator_set_flag(enumerator
, flags
);
421 PyErr_SetFromErrno(PyExc_OSError
);
426 // Set the family we are searching for
428 r
= loc_database_enumerator_set_family(enumerator
, family
);
431 PyErr_SetFromErrno(PyExc_OSError
);
436 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
437 loc_database_enumerator_unref(enumerator
);
442 static PyObject
* Database_countries(DatabaseObject
* self
) {
443 return Database_iterate_all(self
, LOC_DB_ENUMERATE_COUNTRIES
, AF_UNSPEC
, 0);
446 static PyObject
* Database_list_bogons(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
447 char* kwlist
[] = { "family", NULL
};
448 int family
= AF_UNSPEC
;
451 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|i", kwlist
, &family
))
454 return Database_iterate_all(self
, LOC_DB_ENUMERATE_BOGONS
, family
, 0);
457 static struct PyMethodDef Database_methods
[] = {
460 (PyCFunction
)Database_get_as
,
466 (PyCFunction
)Database_get_country
,
472 (PyCFunction
)Database_list_bogons
,
473 METH_VARARGS
|METH_KEYWORDS
,
478 (PyCFunction
)Database_lookup
,
484 (PyCFunction
)Database_search_as
,
490 (PyCFunction
)Database_search_networks
,
491 METH_VARARGS
|METH_KEYWORDS
,
496 (PyCFunction
)Database_verify
,
503 static struct PyGetSetDef Database_getsetters
[] = {
506 (getter
)Database_ases
,
513 (getter
)Database_countries
,
520 (getter
)Database_get_created_at
,
527 (getter
)Database_get_description
,
534 (getter
)Database_get_license
,
541 (getter
)Database_networks
,
547 "networks_flattened",
548 (getter
)Database_networks_flattened
,
555 (getter
)Database_get_vendor
,
563 PyTypeObject DatabaseType
= {
564 PyVarObject_HEAD_INIT(NULL
, 0)
565 .tp_name
= "location.Database",
566 .tp_basicsize
= sizeof(DatabaseObject
),
567 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
568 .tp_new
= Database_new
,
569 .tp_dealloc
= (destructor
)Database_dealloc
,
570 .tp_init
= (initproc
)Database_init
,
571 .tp_doc
= "Database object",
572 .tp_methods
= Database_methods
,
573 .tp_getset
= Database_getsetters
,
574 .tp_repr
= (reprfunc
)Database_repr
,
577 static PyObject
* DatabaseEnumerator_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
578 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
580 return (PyObject
*)self
;
583 static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject
* self
) {
584 loc_database_enumerator_unref(self
->enumerator
);
586 Py_TYPE(self
)->tp_free((PyObject
* )self
);
589 static PyObject
* DatabaseEnumerator_next(DatabaseEnumeratorObject
* self
) {
590 struct loc_network
* network
= NULL
;
592 // Enumerate all networks
593 int r
= loc_database_enumerator_next_network(self
->enumerator
, &network
);
595 PyErr_SetFromErrno(PyExc_ValueError
);
599 // A network was found
601 PyObject
* obj
= new_network(&NetworkType
, network
);
602 loc_network_unref(network
);
607 // Enumerate all ASes
608 struct loc_as
* as
= NULL
;
610 r
= loc_database_enumerator_next_as(self
->enumerator
, &as
);
612 PyErr_SetFromErrno(PyExc_ValueError
);
617 PyObject
* obj
= new_as(&ASType
, as
);
623 // Enumerate all countries
624 struct loc_country
* country
= NULL
;
626 r
= loc_database_enumerator_next_country(self
->enumerator
, &country
);
628 PyErr_SetFromErrno(PyExc_ValueError
);
633 PyObject
* obj
= new_country(&CountryType
, country
);
634 loc_country_unref(country
);
639 // Nothing found, that means the end
640 PyErr_SetNone(PyExc_StopIteration
);
644 PyTypeObject DatabaseEnumeratorType
= {
645 PyVarObject_HEAD_INIT(NULL
, 0)
646 .tp_name
= "location.DatabaseEnumerator",
647 .tp_basicsize
= sizeof(DatabaseEnumeratorObject
),
648 .tp_flags
= Py_TPFLAGS_DEFAULT
,
649 .tp_alloc
= PyType_GenericAlloc
,
650 .tp_new
= DatabaseEnumerator_new
,
651 .tp_dealloc
= (destructor
)DatabaseEnumerator_dealloc
,
652 .tp_iter
= PyObject_SelfIter
,
653 .tp_iternext
= (iternextfunc
)DatabaseEnumerator_next
,