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 <loc/libloc.h>
20 #include <loc/database.h>
22 #include "locationmodule.h"
28 static PyObject
* Database_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
29 DatabaseObject
* self
= (DatabaseObject
*)type
->tp_alloc(type
, 0);
31 return (PyObject
*)self
;
34 static void Database_dealloc(DatabaseObject
* self
) {
36 loc_database_unref(self
->db
);
41 Py_TYPE(self
)->tp_free((PyObject
* )self
);
44 static int Database_init(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
45 const char* path
= NULL
;
47 if (!PyArg_ParseTuple(args
, "s", &path
))
50 self
->path
= strdup(path
);
52 // Open the file for reading
53 FILE* f
= fopen(self
->path
, "r");
55 PyErr_SetFromErrno(PyExc_IOError
);
60 int r
= loc_database_new(loc_ctx
, &self
->db
, f
);
63 // Return on any errors
70 static PyObject
* Database_repr(DatabaseObject
* self
) {
71 return PyUnicode_FromFormat("<Database %s>", self
->path
);
74 static PyObject
* Database_verify(DatabaseObject
* self
, PyObject
* args
) {
75 PyObject
* public_key
= NULL
;
79 if (!PyArg_ParseTuple(args
, "O", &public_key
))
83 int fd
= PyObject_AsFileDescriptor(public_key
);
87 // Re-open file descriptor
90 PyErr_SetFromErrno(PyExc_IOError
);
94 int r
= loc_database_verify(self
->db
, f
);
102 static PyObject
* Database_get_description(DatabaseObject
* self
) {
103 const char* description
= loc_database_get_description(self
->db
);
105 return PyUnicode_FromString(description
);
108 static PyObject
* Database_get_vendor(DatabaseObject
* self
) {
109 const char* vendor
= loc_database_get_vendor(self
->db
);
111 return PyUnicode_FromString(vendor
);
114 static PyObject
* Database_get_license(DatabaseObject
* self
) {
115 const char* license
= loc_database_get_license(self
->db
);
117 return PyUnicode_FromString(license
);
120 static PyObject
* Database_get_created_at(DatabaseObject
* self
) {
121 time_t created_at
= loc_database_created_at(self
->db
);
123 return PyLong_FromLong(created_at
);
126 static PyObject
* Database_get_as(DatabaseObject
* self
, PyObject
* args
) {
127 struct loc_as
* as
= NULL
;
130 if (!PyArg_ParseTuple(args
, "i", &number
))
133 // Try to retrieve the AS
134 int r
= loc_database_get_as(self
->db
, &as
, number
);
138 PyObject
* obj
= new_as(&ASType
, as
);
152 static PyObject
* Database_get_country(DatabaseObject
* self
, PyObject
* args
) {
153 const char* country_code
= NULL
;
155 if (!PyArg_ParseTuple(args
, "s", &country_code
))
158 struct loc_country
* country
;
159 int r
= loc_database_get_country(self
->db
, &country
, country_code
);
164 PyObject
* obj
= new_country(&CountryType
, country
);
165 loc_country_unref(country
);
170 static PyObject
* Database_lookup(DatabaseObject
* self
, PyObject
* args
) {
171 struct loc_network
* network
= NULL
;
172 const char* address
= NULL
;
174 if (!PyArg_ParseTuple(args
, "s", &address
))
177 // Try to retrieve a matching network
178 int r
= loc_database_lookup_from_string(self
->db
, address
, &network
);
182 PyObject
* obj
= new_network(&NetworkType
, network
);
183 loc_network_unref(network
);
192 } else if (r
== -EINVAL
) {
193 PyErr_Format(PyExc_ValueError
, "Invalid IP address: %s", address
);
201 static PyObject
* new_database_enumerator(PyTypeObject
* type
, struct loc_database_enumerator
* enumerator
) {
202 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
204 self
->enumerator
= loc_database_enumerator_ref(enumerator
);
207 return (PyObject
*)self
;
210 static PyObject
* Database_iterate_all(DatabaseObject
* self
, enum loc_database_enumerator_mode what
, int flags
) {
211 struct loc_database_enumerator
* enumerator
;
213 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, what
, flags
);
215 PyErr_SetFromErrno(PyExc_SystemError
);
219 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
220 loc_database_enumerator_unref(enumerator
);
225 static PyObject
* Database_ases(DatabaseObject
* self
) {
226 return Database_iterate_all(self
, LOC_DB_ENUMERATE_ASES
, 0);
229 static PyObject
* Database_search_as(DatabaseObject
* self
, PyObject
* args
) {
230 const char* string
= NULL
;
232 if (!PyArg_ParseTuple(args
, "s", &string
))
235 struct loc_database_enumerator
* enumerator
;
237 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_ASES
, 0);
239 PyErr_SetFromErrno(PyExc_SystemError
);
243 // Search string we are searching for
244 loc_database_enumerator_set_string(enumerator
, string
);
246 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
247 loc_database_enumerator_unref(enumerator
);
252 static PyObject
* Database_networks(DatabaseObject
* self
) {
253 return Database_iterate_all(self
, LOC_DB_ENUMERATE_NETWORKS
, 0);
256 static PyObject
* Database_networks_flattened(DatabaseObject
*self
) {
257 return Database_iterate_all(self
, LOC_DB_ENUMERATE_NETWORKS
, LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
260 static PyObject
* Database_search_networks(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
261 char* kwlist
[] = { "country_code", "asn", "flags", "family", NULL
};
262 const char* country_code
= NULL
;
263 unsigned int asn
= 0;
267 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siii", kwlist
, &country_code
, &asn
, &flags
, &family
))
270 struct loc_database_enumerator
* enumerator
;
271 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_NETWORKS
, 0);
273 PyErr_SetFromErrno(PyExc_SystemError
);
277 // Set country code we are searching for
279 r
= loc_database_enumerator_set_country_code(enumerator
, country_code
);
282 PyErr_SetFromErrno(PyExc_SystemError
);
287 // Set the ASN we are searching for
289 r
= loc_database_enumerator_set_asn(enumerator
, asn
);
292 PyErr_SetFromErrno(PyExc_SystemError
);
297 // Set the flags we are searching for
299 r
= loc_database_enumerator_set_flag(enumerator
, flags
);
302 PyErr_SetFromErrno(PyExc_SystemError
);
307 // Set the family we are searching for
309 r
= loc_database_enumerator_set_family(enumerator
, family
);
312 PyErr_SetFromErrno(PyExc_SystemError
);
317 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
318 loc_database_enumerator_unref(enumerator
);
323 static PyObject
* Database_countries(DatabaseObject
* self
) {
324 return Database_iterate_all(self
, LOC_DB_ENUMERATE_COUNTRIES
, 0);
327 static struct PyMethodDef Database_methods
[] = {
330 (PyCFunction
)Database_get_as
,
336 (PyCFunction
)Database_get_country
,
342 (PyCFunction
)Database_lookup
,
348 (PyCFunction
)Database_search_as
,
354 (PyCFunction
)Database_search_networks
,
355 METH_VARARGS
|METH_KEYWORDS
,
360 (PyCFunction
)Database_verify
,
367 static struct PyGetSetDef Database_getsetters
[] = {
370 (getter
)Database_ases
,
377 (getter
)Database_countries
,
384 (getter
)Database_get_created_at
,
391 (getter
)Database_get_description
,
398 (getter
)Database_get_license
,
405 (getter
)Database_networks
,
411 "networks_flattened",
412 (getter
)Database_networks_flattened
,
419 (getter
)Database_get_vendor
,
427 PyTypeObject DatabaseType
= {
428 PyVarObject_HEAD_INIT(NULL
, 0)
429 .tp_name
= "location.Database",
430 .tp_basicsize
= sizeof(DatabaseObject
),
431 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
432 .tp_new
= Database_new
,
433 .tp_dealloc
= (destructor
)Database_dealloc
,
434 .tp_init
= (initproc
)Database_init
,
435 .tp_doc
= "Database object",
436 .tp_methods
= Database_methods
,
437 .tp_getset
= Database_getsetters
,
438 .tp_repr
= (reprfunc
)Database_repr
,
441 static PyObject
* DatabaseEnumerator_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
442 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
444 return (PyObject
*)self
;
447 static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject
* self
) {
448 loc_database_enumerator_unref(self
->enumerator
);
450 Py_TYPE(self
)->tp_free((PyObject
* )self
);
453 static PyObject
* DatabaseEnumerator_next(DatabaseEnumeratorObject
* self
) {
454 struct loc_network
* network
= NULL
;
456 // Enumerate all networks
457 int r
= loc_database_enumerator_next_network(self
->enumerator
, &network
);
459 PyErr_SetFromErrno(PyExc_ValueError
);
463 // A network was found
465 PyObject
* obj
= new_network(&NetworkType
, network
);
466 loc_network_unref(network
);
471 // Enumerate all ASes
472 struct loc_as
* as
= NULL
;
474 r
= loc_database_enumerator_next_as(self
->enumerator
, &as
);
476 PyErr_SetFromErrno(PyExc_ValueError
);
481 PyObject
* obj
= new_as(&ASType
, as
);
487 // Enumerate all countries
488 struct loc_country
* country
= NULL
;
490 r
= loc_database_enumerator_next_country(self
->enumerator
, &country
);
492 PyErr_SetFromErrno(PyExc_ValueError
);
497 PyObject
* obj
= new_country(&CountryType
, country
);
498 loc_country_unref(country
);
503 // Nothing found, that means the end
504 PyErr_SetNone(PyExc_StopIteration
);
508 PyTypeObject DatabaseEnumeratorType
= {
509 PyVarObject_HEAD_INIT(NULL
, 0)
510 .tp_name
= "location.DatabaseEnumerator",
511 .tp_basicsize
= sizeof(DatabaseEnumeratorObject
),
512 .tp_flags
= Py_TPFLAGS_DEFAULT
,
513 .tp_alloc
= PyType_GenericAlloc
,
514 .tp_new
= DatabaseEnumerator_new
,
515 .tp_dealloc
= (destructor
)DatabaseEnumerator_dealloc
,
516 .tp_iter
= PyObject_SelfIter
,
517 .tp_iternext
= (iternextfunc
)DatabaseEnumerator_next
,