]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/database.c
Correctly handle invalid IP addresses
[people/ms/libloc.git] / src / python / database.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
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.
10
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.
15 */
16
17 #include <Python.h>
18
19 #include <loc/libloc.h>
20 #include <loc/database.h>
21
22 #include "locationmodule.h"
23 #include "as.h"
24 #include "database.h"
25 #include "network.h"
26
27 static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
28 DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
29
30 return (PyObject*)self;
31 }
32
33 static void Database_dealloc(DatabaseObject* self) {
34 if (self->db)
35 loc_database_unref(self->db);
36
37 if (self->path)
38 free(self->path);
39
40 Py_TYPE(self)->tp_free((PyObject* )self);
41 }
42
43 static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
44 const char* path = NULL;
45
46 if (!PyArg_ParseTuple(args, "s", &path))
47 return -1;
48
49 self->path = strdup(path);
50
51 // Open the file for reading
52 FILE* f = fopen(self->path, "r");
53 if (!f)
54 return -1;
55
56 // Load the database
57 int r = loc_database_new(loc_ctx, &self->db, f);
58 fclose(f);
59
60 // Return on any errors
61 if (r)
62 return -1;
63
64 return 0;
65 }
66
67 static PyObject* Database_repr(DatabaseObject* self) {
68 return PyUnicode_FromFormat("<Database %s>", self->path);
69 }
70
71 static PyObject* Database_get_description(DatabaseObject* self) {
72 const char* description = loc_database_get_description(self->db);
73
74 return PyUnicode_FromString(description);
75 }
76
77 static PyObject* Database_get_vendor(DatabaseObject* self) {
78 const char* vendor = loc_database_get_vendor(self->db);
79
80 return PyUnicode_FromString(vendor);
81 }
82
83 static PyObject* Database_get_license(DatabaseObject* self) {
84 const char* license = loc_database_get_license(self->db);
85
86 return PyUnicode_FromString(license);
87 }
88
89 static PyObject* Database_get_created_at(DatabaseObject* self) {
90 time_t created_at = loc_database_created_at(self->db);
91
92 return PyLong_FromLong(created_at);
93 }
94
95 static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
96 struct loc_as* as = NULL;
97 uint32_t number = 0;
98
99 if (!PyArg_ParseTuple(args, "i", &number))
100 return NULL;
101
102 // Try to retrieve the AS
103 int r = loc_database_get_as(self->db, &as, number);
104
105 // We got an AS
106 if (r == 0) {
107 PyObject* obj = new_as(&ASType, as);
108 loc_as_unref(as);
109
110 return obj;
111
112 // Nothing found
113 } else if (r == 1) {
114 Py_RETURN_NONE;
115 }
116
117 // Unexpected error
118 return NULL;
119 }
120
121 static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
122 struct loc_network* network = NULL;
123 const char* address = NULL;
124
125 if (!PyArg_ParseTuple(args, "s", &address))
126 return NULL;
127
128 // Try to retrieve a matching network
129 int r = loc_database_lookup_from_string(self->db, address, &network);
130
131 // We got a network
132 if (r == 0) {
133 PyObject* obj = new_network(&NetworkType, network);
134 loc_network_unref(network);
135
136 return obj;
137
138 // Nothing found
139 } else if (r == 1) {
140 Py_RETURN_NONE;
141
142 // Invalid input
143 } else if (r == -EINVAL) {
144 PyErr_Format(PyExc_ValueError, "Invalid IP address: %s", address);
145 return NULL;
146 }
147
148 // Unexpected error
149 return NULL;
150 }
151
152 static struct PyMethodDef Database_methods[] = {
153 {
154 "get_as",
155 (PyCFunction)Database_get_as,
156 METH_VARARGS,
157 NULL,
158 },
159 {
160 "lookup",
161 (PyCFunction)Database_lookup,
162 METH_VARARGS,
163 NULL,
164 },
165 { NULL },
166 };
167
168 static struct PyGetSetDef Database_getsetters[] = {
169 {
170 "created_at",
171 (getter)Database_get_created_at,
172 NULL,
173 NULL,
174 NULL,
175 },
176 {
177 "description",
178 (getter)Database_get_description,
179 NULL,
180 NULL,
181 NULL,
182 },
183 {
184 "license",
185 (getter)Database_get_license,
186 NULL,
187 NULL,
188 NULL,
189 },
190 {
191 "vendor",
192 (getter)Database_get_vendor,
193 NULL,
194 NULL,
195 NULL,
196 },
197 { NULL },
198 };
199
200 PyTypeObject DatabaseType = {
201 PyVarObject_HEAD_INIT(NULL, 0)
202 tp_name: "location.Database",
203 tp_basicsize: sizeof(DatabaseObject),
204 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
205 tp_new: Database_new,
206 tp_dealloc: (destructor)Database_dealloc,
207 tp_init: (initproc)Database_init,
208 tp_doc: "Database object",
209 tp_methods: Database_methods,
210 tp_getset: Database_getsetters,
211 tp_repr: (reprfunc)Database_repr,
212 };