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.
21 #include <loc/libloc.h>
22 #include <loc/network.h>
24 #include "locationmodule.h"
27 static PyObject
* PyList_FromNetworkList(struct loc_network_list
* networks
) {
28 PyObject
* list
= PyList_New(0);
32 while (!loc_network_list_empty(networks
)) {
33 struct loc_network
* network
= loc_network_list_pop(networks
);
35 PyObject
* n
= new_network(&NetworkType
, network
);
36 PyList_Append(list
, n
);
38 loc_network_unref(network
);
45 PyObject
* new_network(PyTypeObject
* type
, struct loc_network
* network
) {
46 NetworkObject
* self
= (NetworkObject
*)type
->tp_alloc(type
, 0);
48 self
->network
= loc_network_ref(network
);
51 return (PyObject
*)self
;
54 static PyObject
* Network_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
55 NetworkObject
* self
= (NetworkObject
*)type
->tp_alloc(type
, 0);
57 return (PyObject
*)self
;
60 static void Network_dealloc(NetworkObject
* self
) {
62 loc_network_unref(self
->network
);
64 Py_TYPE(self
)->tp_free((PyObject
* )self
);
67 static int Network_init(NetworkObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
68 const char* network
= NULL
;
70 if (!PyArg_ParseTuple(args
, "s", &network
))
74 int r
= loc_network_new_from_string(loc_ctx
, &self
->network
, network
);
76 PyErr_Format(PyExc_ValueError
, "Invalid network: %s", network
);
83 static PyObject
* Network_repr(NetworkObject
* self
) {
84 char* network
= loc_network_str(self
->network
);
86 PyObject
* obj
= PyUnicode_FromFormat("<location.Network %s>", network
);
92 static PyObject
* Network_str(NetworkObject
* self
) {
93 char* network
= loc_network_str(self
->network
);
95 PyObject
* obj
= PyUnicode_FromString(network
);
101 static PyObject
* Network_get_country_code(NetworkObject
* self
) {
102 const char* country_code
= loc_network_get_country_code(self
->network
);
104 return PyUnicode_FromString(country_code
);
107 static int Network_set_country_code(NetworkObject
* self
, PyObject
* value
) {
108 const char* country_code
= PyUnicode_AsUTF8(value
);
110 int r
= loc_network_set_country_code(self
->network
, country_code
);
113 PyErr_Format(PyExc_ValueError
,
114 "Invalid country code: %s", country_code
);
122 static PyObject
* Network_get_asn(NetworkObject
* self
) {
123 uint32_t asn
= loc_network_get_asn(self
->network
);
126 return PyLong_FromLong(asn
);
131 static int Network_set_asn(NetworkObject
* self
, PyObject
* value
) {
132 long int asn
= PyLong_AsLong(value
);
134 // Check if the ASN is within the valid range
135 if (asn
<= 0 || asn
> UINT32_MAX
) {
136 PyErr_Format(PyExc_ValueError
, "Invalid ASN %ld", asn
);
140 int r
= loc_network_set_asn(self
->network
, asn
);
147 static PyObject
* Network_has_flag(NetworkObject
* self
, PyObject
* args
) {
148 enum loc_network_flags flag
= 0;
150 if (!PyArg_ParseTuple(args
, "i", &flag
))
153 if (loc_network_has_flag(self
->network
, flag
))
159 static PyObject
* Network_set_flag(NetworkObject
* self
, PyObject
* args
) {
160 enum loc_network_flags flag
= 0;
162 if (!PyArg_ParseTuple(args
, "i", &flag
))
165 int r
= loc_network_set_flag(self
->network
, flag
);
168 // What exception to throw here?
175 static PyObject
* Network_exclude(NetworkObject
* self
, PyObject
* args
) {
176 NetworkObject
* other
= NULL
;
178 if (!PyArg_ParseTuple(args
, "O!", &NetworkType
, &other
))
181 struct loc_network_list
* list
= loc_network_exclude(self
->network
, other
->network
);
183 // Convert to Python objects
184 PyObject
* obj
= PyList_FromNetworkList(list
);
185 loc_network_list_unref(list
);
190 static PyObject
* Network_is_subnet_of(NetworkObject
* self
, PyObject
* args
) {
191 NetworkObject
* other
= NULL
;
193 if (!PyArg_ParseTuple(args
, "O!", &NetworkType
, &other
))
196 if (loc_network_is_subnet_of(self
->network
, other
->network
))
202 static PyObject
* Network_get_family(NetworkObject
* self
) {
203 int family
= loc_network_address_family(self
->network
);
205 return PyLong_FromLong(family
);
208 static PyObject
* Network_get_first_address(NetworkObject
* self
) {
209 char* address
= loc_network_format_first_address(self
->network
);
211 PyObject
* obj
= PyUnicode_FromString(address
);
217 static PyObject
* Network_get_last_address(NetworkObject
* self
) {
218 char* address
= loc_network_format_last_address(self
->network
);
220 PyObject
* obj
= PyUnicode_FromString(address
);
226 static struct PyMethodDef Network_methods
[] = {
229 (PyCFunction
)Network_exclude
,
235 (PyCFunction
)Network_has_flag
,
241 (PyCFunction
)Network_is_subnet_of
,
247 (PyCFunction
)Network_set_flag
,
254 static struct PyGetSetDef Network_getsetters
[] = {
257 (getter
)Network_get_asn
,
258 (setter
)Network_set_asn
,
264 (getter
)Network_get_country_code
,
265 (setter
)Network_set_country_code
,
271 (getter
)Network_get_family
,
278 (getter
)Network_get_first_address
,
285 (getter
)Network_get_last_address
,
293 PyTypeObject NetworkType
= {
294 PyVarObject_HEAD_INIT(NULL
, 0)
295 .tp_name
= "location.Network",
296 .tp_basicsize
= sizeof(NetworkObject
),
297 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
298 .tp_new
= Network_new
,
299 .tp_dealloc
= (destructor
)Network_dealloc
,
300 .tp_init
= (initproc
)Network_init
,
301 .tp_doc
= "Network object",
302 .tp_methods
= Network_methods
,
303 .tp_getset
= Network_getsetters
,
304 .tp_repr
= (reprfunc
)Network_repr
,
305 .tp_str
= (reprfunc
)Network_str
,