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 struct loc_country
* country
= NULL
;
170 const char* country_code
= NULL
;
172 if (!PyArg_ParseTuple(args
, "s", &country_code
))
176 int r
= loc_database_get_country(self
->db
, &country
, country_code
);
180 PyErr_SetString(PyExc_ValueError
, "Invalid country code");
184 PyErr_SetFromErrno(PyExc_OSError
);
195 PyObject
* obj
= new_country(&CountryType
, country
);
196 loc_country_unref(country
);
201 static PyObject
* Database_lookup(DatabaseObject
* self
, PyObject
* args
) {
202 struct loc_network
* network
= NULL
;
203 const char* address
= NULL
;
205 if (!PyArg_ParseTuple(args
, "s", &address
))
208 // Try to retrieve a matching network
209 int r
= loc_database_lookup_from_string(self
->db
, address
, &network
);
213 PyObject
* obj
= new_network(&NetworkType
, network
);
214 loc_network_unref(network
);
226 PyErr_Format(PyExc_ValueError
, "Invalid IP address: %s", address
);
229 PyErr_SetFromErrno(PyExc_OSError
);
235 static PyObject
* new_database_enumerator(PyTypeObject
* type
, struct loc_database_enumerator
* enumerator
) {
236 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
238 self
->enumerator
= loc_database_enumerator_ref(enumerator
);
241 return (PyObject
*)self
;
244 static PyObject
* Database_iterate_all(DatabaseObject
* self
,
245 enum loc_database_enumerator_mode what
, int family
, int flags
) {
246 struct loc_database_enumerator
* enumerator
;
248 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, what
, flags
);
250 PyErr_SetFromErrno(PyExc_SystemError
);
256 loc_database_enumerator_set_family(enumerator
, family
);
258 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
259 loc_database_enumerator_unref(enumerator
);
264 static PyObject
* Database_ases(DatabaseObject
* self
) {
265 return Database_iterate_all(self
, LOC_DB_ENUMERATE_ASES
, AF_UNSPEC
, 0);
268 static PyObject
* Database_search_as(DatabaseObject
* self
, PyObject
* args
) {
269 const char* string
= NULL
;
271 if (!PyArg_ParseTuple(args
, "s", &string
))
274 struct loc_database_enumerator
* enumerator
;
276 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_ASES
, 0);
278 PyErr_SetFromErrno(PyExc_SystemError
);
282 // Search string we are searching for
283 loc_database_enumerator_set_string(enumerator
, string
);
285 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
286 loc_database_enumerator_unref(enumerator
);
291 static PyObject
* Database_networks(DatabaseObject
* self
) {
292 return Database_iterate_all(self
, LOC_DB_ENUMERATE_NETWORKS
, AF_UNSPEC
, 0);
295 static PyObject
* Database_networks_flattened(DatabaseObject
*self
) {
296 return Database_iterate_all(self
, LOC_DB_ENUMERATE_NETWORKS
, AF_UNSPEC
,
297 LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
300 static PyObject
* Database_search_networks(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
301 char* kwlist
[] = { "country_codes", "asns", "flags", "family", "flatten", NULL
};
302 PyObject
* country_codes
= NULL
;
303 PyObject
* asn_list
= NULL
;
308 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O!O!iip", kwlist
,
309 &PyList_Type
, &country_codes
, &PyList_Type
, &asn_list
, &flags
, &family
, &flatten
))
312 struct loc_database_enumerator
* enumerator
;
313 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_NETWORKS
,
314 (flatten
) ? LOC_DB_ENUMERATOR_FLAGS_FLATTEN
: 0);
316 PyErr_SetFromErrno(PyExc_SystemError
);
320 // Set country code we are searching for
322 struct loc_country_list
* countries
;
323 r
= loc_country_list_new(loc_ctx
, &countries
);
325 PyErr_SetString(PyExc_SystemError
, "Could not create country list");
329 for (int i
= 0; i
< PyList_Size(country_codes
); i
++) {
330 PyObject
* item
= PyList_GetItem(country_codes
, i
);
332 if (!PyUnicode_Check(item
)) {
333 PyErr_SetString(PyExc_TypeError
, "Country codes must be strings");
334 loc_country_list_unref(countries
);
338 const char* country_code
= PyUnicode_AsUTF8(item
);
340 struct loc_country
* country
;
341 r
= loc_country_new(loc_ctx
, &country
, country_code
);
344 PyErr_Format(PyExc_ValueError
, "Invalid country code: %s", country_code
);
346 PyErr_SetString(PyExc_SystemError
, "Could not create country");
349 loc_country_list_unref(countries
);
353 // Append it to the list
354 r
= loc_country_list_append(countries
, country
);
356 PyErr_SetString(PyExc_SystemError
, "Could not append country to the list");
358 loc_country_list_unref(countries
);
359 loc_country_unref(country
);
363 loc_country_unref(country
);
366 r
= loc_database_enumerator_set_countries(enumerator
, countries
);
368 PyErr_SetFromErrno(PyExc_SystemError
);
370 loc_country_list_unref(countries
);
374 loc_country_list_unref(countries
);
377 // Set the ASN we are searching for
379 struct loc_as_list
* asns
;
380 r
= loc_as_list_new(loc_ctx
, &asns
);
382 PyErr_SetFromErrno(PyExc_OSError
);
386 for (int i
= 0; i
< PyList_Size(asn_list
); i
++) {
387 PyObject
* item
= PyList_GetItem(asn_list
, i
);
389 if (!PyLong_Check(item
)) {
390 PyErr_SetString(PyExc_TypeError
, "ASNs must be numbers");
392 loc_as_list_unref(asns
);
396 unsigned long number
= PyLong_AsLong(item
);
399 r
= loc_as_new(loc_ctx
, &as
, number
);
401 PyErr_SetFromErrno(PyExc_OSError
);
403 loc_as_list_unref(asns
);
408 r
= loc_as_list_append(asns
, as
);
410 PyErr_SetFromErrno(PyExc_OSError
);
412 loc_as_list_unref(asns
);
420 r
= loc_database_enumerator_set_asns(enumerator
, asns
);
422 PyErr_SetFromErrno(PyExc_OSError
);
424 loc_as_list_unref(asns
);
428 loc_as_list_unref(asns
);
431 // Set the flags we are searching for
433 r
= loc_database_enumerator_set_flag(enumerator
, flags
);
436 PyErr_SetFromErrno(PyExc_OSError
);
441 // Set the family we are searching for
443 r
= loc_database_enumerator_set_family(enumerator
, family
);
446 PyErr_SetFromErrno(PyExc_OSError
);
451 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
452 loc_database_enumerator_unref(enumerator
);
457 static PyObject
* Database_countries(DatabaseObject
* self
) {
458 return Database_iterate_all(self
, LOC_DB_ENUMERATE_COUNTRIES
, AF_UNSPEC
, 0);
461 static PyObject
* Database_list_bogons(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
462 char* kwlist
[] = { "family", NULL
};
463 int family
= AF_UNSPEC
;
466 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|i", kwlist
, &family
))
469 return Database_iterate_all(self
, LOC_DB_ENUMERATE_BOGONS
, family
, 0);
472 static struct PyMethodDef Database_methods
[] = {
475 (PyCFunction
)Database_get_as
,
481 (PyCFunction
)Database_get_country
,
487 (PyCFunction
)Database_list_bogons
,
488 METH_VARARGS
|METH_KEYWORDS
,
493 (PyCFunction
)Database_lookup
,
499 (PyCFunction
)Database_search_as
,
505 (PyCFunction
)Database_search_networks
,
506 METH_VARARGS
|METH_KEYWORDS
,
511 (PyCFunction
)Database_verify
,
518 static struct PyGetSetDef Database_getsetters
[] = {
521 (getter
)Database_ases
,
528 (getter
)Database_countries
,
535 (getter
)Database_get_created_at
,
542 (getter
)Database_get_description
,
549 (getter
)Database_get_license
,
556 (getter
)Database_networks
,
562 "networks_flattened",
563 (getter
)Database_networks_flattened
,
570 (getter
)Database_get_vendor
,
578 PyTypeObject DatabaseType
= {
579 PyVarObject_HEAD_INIT(NULL
, 0)
580 .tp_name
= "location.Database",
581 .tp_basicsize
= sizeof(DatabaseObject
),
582 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
583 .tp_new
= Database_new
,
584 .tp_dealloc
= (destructor
)Database_dealloc
,
585 .tp_init
= (initproc
)Database_init
,
586 .tp_doc
= "Database object",
587 .tp_methods
= Database_methods
,
588 .tp_getset
= Database_getsetters
,
589 .tp_repr
= (reprfunc
)Database_repr
,
592 static PyObject
* DatabaseEnumerator_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
593 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
595 return (PyObject
*)self
;
598 static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject
* self
) {
599 loc_database_enumerator_unref(self
->enumerator
);
601 Py_TYPE(self
)->tp_free((PyObject
* )self
);
604 static PyObject
* DatabaseEnumerator_next(DatabaseEnumeratorObject
* self
) {
605 struct loc_network
* network
= NULL
;
607 // Enumerate all networks
608 int r
= loc_database_enumerator_next_network(self
->enumerator
, &network
);
610 PyErr_SetFromErrno(PyExc_ValueError
);
614 // A network was found
616 PyObject
* obj
= new_network(&NetworkType
, network
);
617 loc_network_unref(network
);
622 // Enumerate all ASes
623 struct loc_as
* as
= NULL
;
625 r
= loc_database_enumerator_next_as(self
->enumerator
, &as
);
627 PyErr_SetFromErrno(PyExc_ValueError
);
632 PyObject
* obj
= new_as(&ASType
, as
);
638 // Enumerate all countries
639 struct loc_country
* country
= NULL
;
641 r
= loc_database_enumerator_next_country(self
->enumerator
, &country
);
643 PyErr_SetFromErrno(PyExc_ValueError
);
648 PyObject
* obj
= new_country(&CountryType
, country
);
649 loc_country_unref(country
);
654 // Nothing found, that means the end
655 PyErr_SetNone(PyExc_StopIteration
);
659 PyTypeObject DatabaseEnumeratorType
= {
660 PyVarObject_HEAD_INIT(NULL
, 0)
661 .tp_name
= "location.DatabaseEnumerator",
662 .tp_basicsize
= sizeof(DatabaseEnumeratorObject
),
663 .tp_flags
= Py_TPFLAGS_DEFAULT
,
664 .tp_alloc
= PyType_GenericAlloc
,
665 .tp_new
= DatabaseEnumerator_new
,
666 .tp_dealloc
= (destructor
)DatabaseEnumerator_dealloc
,
667 .tp_iter
= PyObject_SelfIter
,
668 .tp_iternext
= (iternextfunc
)DatabaseEnumerator_next
,