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
) {
75 int r
= loc_database_verify(self
->db
);
83 static PyObject
* Database_get_description(DatabaseObject
* self
) {
84 const char* description
= loc_database_get_description(self
->db
);
86 return PyUnicode_FromString(description
);
89 static PyObject
* Database_get_vendor(DatabaseObject
* self
) {
90 const char* vendor
= loc_database_get_vendor(self
->db
);
92 return PyUnicode_FromString(vendor
);
95 static PyObject
* Database_get_license(DatabaseObject
* self
) {
96 const char* license
= loc_database_get_license(self
->db
);
98 return PyUnicode_FromString(license
);
101 static PyObject
* Database_get_created_at(DatabaseObject
* self
) {
102 time_t created_at
= loc_database_created_at(self
->db
);
104 return PyLong_FromLong(created_at
);
107 static PyObject
* Database_get_as(DatabaseObject
* self
, PyObject
* args
) {
108 struct loc_as
* as
= NULL
;
111 if (!PyArg_ParseTuple(args
, "i", &number
))
114 // Try to retrieve the AS
115 int r
= loc_database_get_as(self
->db
, &as
, number
);
119 PyObject
* obj
= new_as(&ASType
, as
);
133 static PyObject
* Database_get_country(DatabaseObject
* self
, PyObject
* args
) {
134 const char* country_code
= NULL
;
136 if (!PyArg_ParseTuple(args
, "s", &country_code
))
139 struct loc_country
* country
;
140 int r
= loc_database_get_country(self
->db
, &country
, country_code
);
145 PyObject
* obj
= new_country(&CountryType
, country
);
146 loc_country_unref(country
);
151 static PyObject
* Database_lookup(DatabaseObject
* self
, PyObject
* args
) {
152 struct loc_network
* network
= NULL
;
153 const char* address
= NULL
;
155 if (!PyArg_ParseTuple(args
, "s", &address
))
158 // Try to retrieve a matching network
159 int r
= loc_database_lookup_from_string(self
->db
, address
, &network
);
163 PyObject
* obj
= new_network(&NetworkType
, network
);
164 loc_network_unref(network
);
173 } else if (r
== -EINVAL
) {
174 PyErr_Format(PyExc_ValueError
, "Invalid IP address: %s", address
);
182 static PyObject
* new_database_enumerator(PyTypeObject
* type
, struct loc_database_enumerator
* enumerator
) {
183 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
185 self
->enumerator
= loc_database_enumerator_ref(enumerator
);
188 return (PyObject
*)self
;
191 static PyObject
* Database_search_as(DatabaseObject
* self
, PyObject
* args
) {
192 const char* string
= NULL
;
194 if (!PyArg_ParseTuple(args
, "s", &string
))
197 struct loc_database_enumerator
* enumerator
;
199 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_ASES
);
201 PyErr_SetFromErrno(PyExc_SystemError
);
205 // Search string we are searching for
206 loc_database_enumerator_set_string(enumerator
, string
);
208 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
209 loc_database_enumerator_unref(enumerator
);
214 static PyObject
* Database_search_networks(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
215 char* kwlist
[] = { "country_code", "asn", "flags", NULL
};
216 const char* country_code
= NULL
;
217 unsigned int asn
= 0;
220 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|sii", kwlist
, &country_code
, &asn
, &flags
))
223 struct loc_database_enumerator
* enumerator
;
224 int r
= loc_database_enumerator_new(&enumerator
, self
->db
, LOC_DB_ENUMERATE_NETWORKS
);
226 PyErr_SetFromErrno(PyExc_SystemError
);
230 // Set country code we are searching for
232 r
= loc_database_enumerator_set_country_code(enumerator
, country_code
);
235 PyErr_SetFromErrno(PyExc_SystemError
);
240 // Set the ASN we are searching for
242 r
= loc_database_enumerator_set_asn(enumerator
, asn
);
245 PyErr_SetFromErrno(PyExc_SystemError
);
250 // Set the flags we are searching for
252 r
= loc_database_enumerator_set_flag(enumerator
, flags
);
255 PyErr_SetFromErrno(PyExc_SystemError
);
260 PyObject
* obj
= new_database_enumerator(&DatabaseEnumeratorType
, enumerator
);
261 loc_database_enumerator_unref(enumerator
);
266 static struct PyMethodDef Database_methods
[] = {
269 (PyCFunction
)Database_get_as
,
275 (PyCFunction
)Database_get_country
,
281 (PyCFunction
)Database_lookup
,
287 (PyCFunction
)Database_search_as
,
293 (PyCFunction
)Database_search_networks
,
294 METH_VARARGS
|METH_KEYWORDS
,
299 (PyCFunction
)Database_verify
,
306 static struct PyGetSetDef Database_getsetters
[] = {
309 (getter
)Database_get_created_at
,
316 (getter
)Database_get_description
,
323 (getter
)Database_get_license
,
330 (getter
)Database_get_vendor
,
338 PyTypeObject DatabaseType
= {
339 PyVarObject_HEAD_INIT(NULL
, 0)
340 .tp_name
= "location.Database",
341 .tp_basicsize
= sizeof(DatabaseObject
),
342 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
343 .tp_new
= Database_new
,
344 .tp_dealloc
= (destructor
)Database_dealloc
,
345 .tp_init
= (initproc
)Database_init
,
346 .tp_doc
= "Database object",
347 .tp_methods
= Database_methods
,
348 .tp_getset
= Database_getsetters
,
349 .tp_repr
= (reprfunc
)Database_repr
,
352 static PyObject
* DatabaseEnumerator_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
353 DatabaseEnumeratorObject
* self
= (DatabaseEnumeratorObject
*)type
->tp_alloc(type
, 0);
355 return (PyObject
*)self
;
358 static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject
* self
) {
359 loc_database_enumerator_unref(self
->enumerator
);
361 Py_TYPE(self
)->tp_free((PyObject
* )self
);
364 static PyObject
* DatabaseEnumerator_next(DatabaseEnumeratorObject
* self
) {
365 struct loc_network
* network
= NULL
;
367 // Enumerate all networks
368 int r
= loc_database_enumerator_next_network(self
->enumerator
, &network
);
370 PyErr_SetFromErrno(PyExc_ValueError
);
374 // A network was found
376 PyObject
* obj
= new_network(&NetworkType
, network
);
377 loc_network_unref(network
);
382 // Enumerate all ASes
383 struct loc_as
* as
= NULL
;
385 r
= loc_database_enumerator_next_as(self
->enumerator
, &as
);
387 PyErr_SetFromErrno(PyExc_ValueError
);
392 PyObject
* obj
= new_as(&ASType
, as
);
398 // Nothing found, that means the end
399 PyErr_SetNone(PyExc_StopIteration
);
403 PyTypeObject DatabaseEnumeratorType
= {
404 PyVarObject_HEAD_INIT(NULL
, 0)
405 .tp_name
= "location.DatabaseEnumerator",
406 .tp_basicsize
= sizeof(DatabaseEnumeratorObject
),
407 .tp_flags
= Py_TPFLAGS_DEFAULT
,
408 .tp_alloc
= PyType_GenericAlloc
,
409 .tp_new
= DatabaseEnumerator_new
,
410 .tp_dealloc
= (destructor
)DatabaseEnumerator_dealloc
,
411 .tp_iter
= PyObject_SelfIter
,
412 .tp_iternext
= (iternextfunc
)DatabaseEnumerator_next
,