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"
27 static PyObject
* Database_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
28 DatabaseObject
* self
= (DatabaseObject
*)type
->tp_alloc(type
, 0);
30 return (PyObject
*)self
;
33 static void Database_dealloc(DatabaseObject
* self
) {
35 loc_database_unref(self
->db
);
37 Py_TYPE(self
)->tp_free((PyObject
* )self
);
40 static int Database_init(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
41 const char* path
= NULL
;
43 if (!PyArg_ParseTuple(args
, "s", &path
))
46 // Open the file for reading
47 FILE* f
= fopen(path
, "r");
52 int r
= loc_database_new(loc_ctx
, &self
->db
, f
);
55 // Return on any errors
62 static PyObject
* Database_get_description(DatabaseObject
* self
) {
63 const char* description
= loc_database_get_description(self
->db
);
65 return PyUnicode_FromString(description
);
68 static PyObject
* Database_get_vendor(DatabaseObject
* self
) {
69 const char* vendor
= loc_database_get_vendor(self
->db
);
71 return PyUnicode_FromString(vendor
);
74 static PyObject
* Database_get_created_at(DatabaseObject
* self
) {
75 time_t created_at
= loc_database_created_at(self
->db
);
77 return PyLong_FromLong(created_at
);
80 static PyObject
* Database_get_as(DatabaseObject
* self
, PyObject
* args
) {
81 struct loc_as
* as
= NULL
;
84 if (!PyArg_ParseTuple(args
, "i", &number
))
87 // Try to retrieve the AS
88 int r
= loc_database_get_as(self
->db
, &as
, number
);
92 PyObject
* obj
= new_as(&ASType
, as
);
106 static PyObject
* Database_lookup(DatabaseObject
* self
, PyObject
* args
) {
107 struct loc_network
* network
= NULL
;
108 const char* address
= NULL
;
110 if (!PyArg_ParseTuple(args
, "s", &address
))
113 // Try to retrieve a matching network
114 int r
= loc_database_lookup_from_string(self
->db
, address
, &network
);
118 PyObject
* obj
= new_network(&NetworkType
, network
);
119 loc_network_unref(network
);
132 static struct PyMethodDef Database_methods
[] = {
135 (PyCFunction
)Database_get_as
,
141 (PyCFunction
)Database_lookup
,
148 static struct PyGetSetDef Database_getsetters
[] = {
151 (getter
)Database_get_created_at
,
158 (getter
)Database_get_description
,
165 (getter
)Database_get_vendor
,
173 PyTypeObject DatabaseType
= {
174 PyVarObject_HEAD_INIT(NULL
, 0)
175 tp_name
: "location.Database",
176 tp_basicsize
: sizeof(DatabaseObject
),
177 tp_flags
: Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
178 tp_new
: Database_new
,
179 tp_dealloc
: (destructor
)Database_dealloc
,
180 tp_init
: (initproc
)Database_init
,
181 tp_doc
: "Database object",
182 tp_methods
: Database_methods
,
183 tp_getset
: Database_getsetters
,