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
) {
211 struct loc_database_enumerator
* enumerator
;
213 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, what
);
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
);
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
);
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
);
256 static PyObject
* Database_search_networks(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
257 char* kwlist
[] = { "country_code", "asn", "flags", "family", NULL
};
258 const char* country_code
= NULL
;
259 unsigned int asn
= 0;
263 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siii", kwlist
, &country_code
, &asn
, &flags
, &family
))
266 struct loc_database_enumerator
* enumerator
;
267 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_NETWORKS
);
269 PyErr_SetFromErrno(PyExc_SystemError
);
273 // Set country code we are searching for
275 r
= loc_database_enumerator_set_country_code(enumerator
, country_code
);
278 PyErr_SetFromErrno(PyExc_SystemError
);
283 // Set the ASN we are searching for
285 r
= loc_database_enumerator_set_asn(enumerator
, asn
);
288 PyErr_SetFromErrno(PyExc_SystemError
);
293 // Set the flags we are searching for
295 r
= loc_database_enumerator_set_flag(enumerator
, flags
);
298 PyErr_SetFromErrno(PyExc_SystemError
);
303 // Set the family we are searching for
305 r
= loc_database_enumerator_set_family(enumerator
, family
);
308 PyErr_SetFromErrno(PyExc_SystemError
);
313 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
314 loc_database_enumerator_unref(enumerator
);
319 static struct PyMethodDef Database_methods
[] = {
322 (PyCFunction
)Database_get_as
,
328 (PyCFunction
)Database_get_country
,
334 (PyCFunction
)Database_lookup
,
340 (PyCFunction
)Database_search_as
,
346 (PyCFunction
)Database_search_networks
,
347 METH_VARARGS
|METH_KEYWORDS
,
352 (PyCFunction
)Database_verify
,
359 static struct PyGetSetDef Database_getsetters
[] = {
362 (getter
)Database_ases
,
369 (getter
)Database_get_created_at
,
376 (getter
)Database_get_description
,
383 (getter
)Database_get_license
,
390 (getter
)Database_networks
,
397 (getter
)Database_get_vendor
,
405 PyTypeObject DatabaseType
= {
406 PyVarObject_HEAD_INIT(NULL
, 0)
407 .tp_name
= "location.Database",
408 .tp_basicsize
= sizeof(DatabaseObject
),
409 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
410 .tp_new
= Database_new
,
411 .tp_dealloc
= (destructor
)Database_dealloc
,
412 .tp_init
= (initproc
)Database_init
,
413 .tp_doc
= "Database object",
414 .tp_methods
= Database_methods
,
415 .tp_getset
= Database_getsetters
,
416 .tp_repr
= (reprfunc
)Database_repr
,
419 static PyObject
* DatabaseEnumerator_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
420 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
422 return (PyObject
*)self
;
425 static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject
* self
) {
426 loc_database_enumerator_unref(self
->enumerator
);
428 Py_TYPE(self
)->tp_free((PyObject
* )self
);
431 static PyObject
* DatabaseEnumerator_next(DatabaseEnumeratorObject
* self
) {
432 struct loc_network
* network
= NULL
;
434 // Enumerate all networks
435 int r
= loc_database_enumerator_next_network(self
->enumerator
, &network
);
437 PyErr_SetFromErrno(PyExc_ValueError
);
441 // A network was found
443 PyObject
* obj
= new_network(&NetworkType
, network
);
444 loc_network_unref(network
);
449 // Enumerate all ASes
450 struct loc_as
* as
= NULL
;
452 r
= loc_database_enumerator_next_as(self
->enumerator
, &as
);
454 PyErr_SetFromErrno(PyExc_ValueError
);
459 PyObject
* obj
= new_as(&ASType
, as
);
465 // Nothing found, that means the end
466 PyErr_SetNone(PyExc_StopIteration
);
470 PyTypeObject DatabaseEnumeratorType
= {
471 PyVarObject_HEAD_INIT(NULL
, 0)
472 .tp_name
= "location.DatabaseEnumerator",
473 .tp_basicsize
= sizeof(DatabaseEnumeratorObject
),
474 .tp_flags
= Py_TPFLAGS_DEFAULT
,
475 .tp_alloc
= PyType_GenericAlloc
,
476 .tp_new
= DatabaseEnumerator_new
,
477 .tp_dealloc
= (destructor
)DatabaseEnumerator_dealloc
,
478 .tp_iter
= PyObject_SelfIter
,
479 .tp_iternext
= (iternextfunc
)DatabaseEnumerator_next
,