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_search_as(DatabaseObject
* self
, PyObject
* args
) {
211 const char* string
= NULL
;
213 if (!PyArg_ParseTuple(args
, "s", &string
))
216 struct loc_database_enumerator
* enumerator
;
218 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_ASES
);
220 PyErr_SetFromErrno(PyExc_SystemError
);
224 // Search string we are searching for
225 loc_database_enumerator_set_string(enumerator
, string
);
227 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
228 loc_database_enumerator_unref(enumerator
);
233 static PyObject
* Database_search_networks(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
234 char* kwlist
[] = { "country_code", "asn", "flags", "family", NULL
};
235 const char* country_code
= NULL
;
236 unsigned int asn
= 0;
240 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siii", kwlist
, &country_code
, &asn
, &flags
, &family
))
243 struct loc_database_enumerator
* enumerator
;
244 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_NETWORKS
);
246 PyErr_SetFromErrno(PyExc_SystemError
);
250 // Set country code we are searching for
252 r
= loc_database_enumerator_set_country_code(enumerator
, country_code
);
255 PyErr_SetFromErrno(PyExc_SystemError
);
260 // Set the ASN we are searching for
262 r
= loc_database_enumerator_set_asn(enumerator
, asn
);
265 PyErr_SetFromErrno(PyExc_SystemError
);
270 // Set the flags we are searching for
272 r
= loc_database_enumerator_set_flag(enumerator
, flags
);
275 PyErr_SetFromErrno(PyExc_SystemError
);
280 // Set the family we are searching for
282 r
= loc_database_enumerator_set_family(enumerator
, family
);
285 PyErr_SetFromErrno(PyExc_SystemError
);
290 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
291 loc_database_enumerator_unref(enumerator
);
296 static struct PyMethodDef Database_methods
[] = {
299 (PyCFunction
)Database_get_as
,
305 (PyCFunction
)Database_get_country
,
311 (PyCFunction
)Database_lookup
,
317 (PyCFunction
)Database_search_as
,
323 (PyCFunction
)Database_search_networks
,
324 METH_VARARGS
|METH_KEYWORDS
,
329 (PyCFunction
)Database_verify
,
336 static struct PyGetSetDef Database_getsetters
[] = {
339 (getter
)Database_get_created_at
,
346 (getter
)Database_get_description
,
353 (getter
)Database_get_license
,
360 (getter
)Database_get_vendor
,
368 PyTypeObject DatabaseType
= {
369 PyVarObject_HEAD_INIT(NULL
, 0)
370 .tp_name
= "location.Database",
371 .tp_basicsize
= sizeof(DatabaseObject
),
372 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
373 .tp_new
= Database_new
,
374 .tp_dealloc
= (destructor
)Database_dealloc
,
375 .tp_init
= (initproc
)Database_init
,
376 .tp_doc
= "Database object",
377 .tp_methods
= Database_methods
,
378 .tp_getset
= Database_getsetters
,
379 .tp_repr
= (reprfunc
)Database_repr
,
382 static PyObject
* DatabaseEnumerator_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
383 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
385 return (PyObject
*)self
;
388 static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject
* self
) {
389 loc_database_enumerator_unref(self
->enumerator
);
391 Py_TYPE(self
)->tp_free((PyObject
* )self
);
394 static PyObject
* DatabaseEnumerator_next(DatabaseEnumeratorObject
* self
) {
395 struct loc_network
* network
= NULL
;
397 // Enumerate all networks
398 int r
= loc_database_enumerator_next_network(self
->enumerator
, &network
);
400 PyErr_SetFromErrno(PyExc_ValueError
);
404 // A network was found
406 PyObject
* obj
= new_network(&NetworkType
, network
);
407 loc_network_unref(network
);
412 // Enumerate all ASes
413 struct loc_as
* as
= NULL
;
415 r
= loc_database_enumerator_next_as(self
->enumerator
, &as
);
417 PyErr_SetFromErrno(PyExc_ValueError
);
422 PyObject
* obj
= new_as(&ASType
, as
);
428 // Nothing found, that means the end
429 PyErr_SetNone(PyExc_StopIteration
);
433 PyTypeObject DatabaseEnumeratorType
= {
434 PyVarObject_HEAD_INIT(NULL
, 0)
435 .tp_name
= "location.DatabaseEnumerator",
436 .tp_basicsize
= sizeof(DatabaseEnumeratorObject
),
437 .tp_flags
= Py_TPFLAGS_DEFAULT
,
438 .tp_alloc
= PyType_GenericAlloc
,
439 .tp_new
= DatabaseEnumerator_new
,
440 .tp_dealloc
= (destructor
)DatabaseEnumerator_dealloc
,
441 .tp_iter
= PyObject_SelfIter
,
442 .tp_iternext
= (iternextfunc
)DatabaseEnumerator_next
,