]> git.ipfire.org Git - location/libloc.git/blame - src/python/network.c
network: Reduce debugging output
[location/libloc.git] / src / python / network.c
CommitLineData
39967361
MT
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 <errno.h>
20
21#include <loc/libloc.h>
22#include <loc/network.h>
23
24#include "locationmodule.h"
25#include "network.h"
26
31edab76
MT
27PyObject* new_network(PyTypeObject* type, struct loc_network* network) {
28 NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
29 if (self) {
30 self->network = loc_network_ref(network);
31 }
32
33 return (PyObject*)self;
34}
35
39967361
MT
36static PyObject* Network_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
37 NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
38
39 return (PyObject*)self;
40}
41
42static void Network_dealloc(NetworkObject* self) {
43 if (self->network)
44 loc_network_unref(self->network);
45
46 Py_TYPE(self)->tp_free((PyObject* )self);
47}
48
49static int Network_init(NetworkObject* self, PyObject* args, PyObject* kwargs) {
50 const char* network = NULL;
51
52 if (!PyArg_ParseTuple(args, "s", &network))
53 return -1;
54
55 // Load the Network
56 int r = loc_network_new_from_string(loc_ctx, &self->network, network);
57 if (r) {
58 PyErr_Format(PyExc_ValueError, "Invalid network: %s", network);
59 return -1;
60 }
61
62 return 0;
63}
64
65static PyObject* Network_repr(NetworkObject* self) {
66 char* network = loc_network_str(self->network);
67
68 PyObject* obj = PyUnicode_FromFormat("<location.Network %s>", network);
69 free(network);
70
71 return obj;
72}
73
5118a4b8
MT
74static PyObject* Network_str(NetworkObject* self) {
75 char* network = loc_network_str(self->network);
76
77 PyObject* obj = PyUnicode_FromString(network);
78 free(network);
79
80 return obj;
81}
82
39967361
MT
83static PyObject* Network_get_country_code(NetworkObject* self) {
84 const char* country_code = loc_network_get_country_code(self->network);
85
86 return PyUnicode_FromString(country_code);
87}
88
89static int Network_set_country_code(NetworkObject* self, PyObject* value) {
90 const char* country_code = PyUnicode_AsUTF8(value);
91
92 int r = loc_network_set_country_code(self->network, country_code);
93 if (r) {
94 if (r == -EINVAL)
35f5acdc
MT
95 PyErr_Format(PyExc_ValueError,
96 "Invalid country code: %s", country_code);
39967361
MT
97
98 return -1;
99 }
100
101 return 0;
102}
103
71ff3e69
MT
104static PyObject* Network_get_asn(NetworkObject* self) {
105 uint32_t asn = loc_network_get_asn(self->network);
106
107 if (asn)
108 return PyLong_FromLong(asn);
109
110 Py_RETURN_NONE;
111}
112
113static int Network_set_asn(NetworkObject* self, PyObject* value) {
114 long int asn = PyLong_AsLong(value);
115
116 // Check if the ASN is within the valid range
117 if (asn <= 0 || asn > UINT32_MAX) {
118 PyErr_Format(PyExc_ValueError, "Invalid ASN %ld", asn);
119 return -1;
120 }
121
122 int r = loc_network_set_asn(self->network, asn);
123 if (r)
124 return -1;
125
126 return 0;
127}
128
24ca7992
MT
129static PyObject* Network_has_flag(NetworkObject* self, PyObject* args) {
130 enum loc_network_flags flag = 0;
131
132 if (!PyArg_ParseTuple(args, "i", &flag))
133 return NULL;
134
135 if (loc_network_has_flag(self->network, flag))
136 Py_RETURN_TRUE;
137
138 Py_RETURN_FALSE;
139}
140
141static PyObject* Network_set_flag(NetworkObject* self, PyObject* args) {
142 enum loc_network_flags flag = 0;
143
144 if (!PyArg_ParseTuple(args, "i", &flag))
145 return NULL;
146
147 int r = loc_network_set_flag(self->network, flag);
148
149 if (r) {
150 // What exception to throw here?
151 return NULL;
152 }
153
154 Py_RETURN_NONE;
155}
156
43554dc4
MT
157static PyObject* Network_is_subnet_of(NetworkObject* self, PyObject* args) {
158 NetworkObject* other = NULL;
159
160 if (!PyArg_ParseTuple(args, "O!", &NetworkType, &other))
161 return NULL;
162
163 if (loc_network_is_subnet_of(self->network, other->network))
164 Py_RETURN_TRUE;
165
166 Py_RETURN_FALSE;
167}
168
2b9338ea
MT
169static PyObject* Network_get_family(NetworkObject* self) {
170 int family = loc_network_address_family(self->network);
171
172 return PyLong_FromLong(family);
173}
174
175static PyObject* Network_get_first_address(NetworkObject* self) {
176 char* address = loc_network_format_first_address(self->network);
177
178 PyObject* obj = PyUnicode_FromString(address);
179 free(address);
180
181 return obj;
182}
183
184static PyObject* Network_get_last_address(NetworkObject* self) {
185 char* address = loc_network_format_last_address(self->network);
186
187 PyObject* obj = PyUnicode_FromString(address);
188 free(address);
189
190 return obj;
191}
192
24ca7992
MT
193static struct PyMethodDef Network_methods[] = {
194 {
195 "has_flag",
196 (PyCFunction)Network_has_flag,
197 METH_VARARGS,
198 NULL,
199 },
43554dc4
MT
200 {
201 "is_subnet_of",
202 (PyCFunction)Network_is_subnet_of,
203 METH_VARARGS,
204 NULL,
205 },
24ca7992
MT
206 {
207 "set_flag",
208 (PyCFunction)Network_set_flag,
209 METH_VARARGS,
210 NULL,
211 },
212 { NULL },
213};
214
39967361 215static struct PyGetSetDef Network_getsetters[] = {
71ff3e69
MT
216 {
217 "asn",
218 (getter)Network_get_asn,
219 (setter)Network_set_asn,
220 NULL,
221 NULL,
222 },
39967361
MT
223 {
224 "country_code",
225 (getter)Network_get_country_code,
226 (setter)Network_set_country_code,
227 NULL,
228 NULL,
229 },
2b9338ea
MT
230 {
231 "family",
232 (getter)Network_get_family,
233 NULL,
234 NULL,
235 NULL,
236 },
237 {
238 "first_address",
239 (getter)Network_get_first_address,
240 NULL,
241 NULL,
242 NULL,
243 },
244 {
245 "last_address",
246 (getter)Network_get_last_address,
247 NULL,
248 NULL,
249 NULL,
250 },
39967361
MT
251 { NULL },
252};
253
254PyTypeObject NetworkType = {
255 PyVarObject_HEAD_INIT(NULL, 0)
d42e1dcd
MT
256 .tp_name = "location.Network",
257 .tp_basicsize = sizeof(NetworkObject),
258 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
259 .tp_new = Network_new,
260 .tp_dealloc = (destructor)Network_dealloc,
261 .tp_init = (initproc)Network_init,
262 .tp_doc = "Network object",
24ca7992 263 .tp_methods = Network_methods,
d42e1dcd
MT
264 .tp_getset = Network_getsetters,
265 .tp_repr = (reprfunc)Network_repr,
266 .tp_str = (reprfunc)Network_str,
39967361 267};