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.
22 #include <libloc/libloc.h>
23 #include <libloc/network.h>
24 #include <libloc/network-list.h>
26 #include "locationmodule.h"
29 static PyObject
* PyList_FromNetworkList(struct loc_network_list
* networks
) {
30 PyObject
* list
= PyList_New(0);
34 while (!loc_network_list_empty(networks
)) {
35 struct loc_network
* network
= loc_network_list_pop(networks
);
37 PyObject
* n
= new_network(&NetworkType
, network
);
38 PyList_Append(list
, n
);
40 loc_network_unref(network
);
47 PyObject
* new_network(PyTypeObject
* type
, struct loc_network
* network
) {
48 NetworkObject
* self
= (NetworkObject
*)type
->tp_alloc(type
, 0);
50 self
->network
= loc_network_ref(network
);
53 return (PyObject
*)self
;
56 static PyObject
* Network_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
57 NetworkObject
* self
= (NetworkObject
*)type
->tp_alloc(type
, 0);
59 return (PyObject
*)self
;
62 static void Network_dealloc(NetworkObject
* self
) {
64 loc_network_unref(self
->network
);
66 Py_TYPE(self
)->tp_free((PyObject
* )self
);
69 static int Network_init(NetworkObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
70 const char* network
= NULL
;
72 if (!PyArg_ParseTuple(args
, "s", &network
))
76 int r
= loc_network_new_from_string(loc_ctx
, &self
->network
, network
);
78 PyErr_Format(PyExc_ValueError
, "Invalid network: %s", network
);
85 static PyObject
* Network_repr(NetworkObject
* self
) {
86 const char* network
= loc_network_str(self
->network
);
88 return PyUnicode_FromFormat("<location.Network %s>", network
);
91 static PyObject
* Network_str(NetworkObject
* self
) {
92 const char* network
= loc_network_str(self
->network
);
94 return PyUnicode_FromString(network
);
97 static PyObject
* Network_get_country_code(NetworkObject
* self
) {
98 const char* country_code
= loc_network_get_country_code(self
->network
);
100 return PyUnicode_FromString(country_code
);
103 static int Network_set_country_code(NetworkObject
* self
, PyObject
* value
) {
104 const char* country_code
= PyUnicode_AsUTF8(value
);
106 int r
= loc_network_set_country_code(self
->network
, country_code
);
109 PyErr_Format(PyExc_ValueError
,
110 "Invalid country code: %s", country_code
);
118 static PyObject
* Network_get_asn(NetworkObject
* self
) {
119 uint32_t asn
= loc_network_get_asn(self
->network
);
122 return PyLong_FromLong(asn
);
127 static int Network_set_asn(NetworkObject
* self
, PyObject
* value
) {
128 long int asn
= PyLong_AsLong(value
);
130 // Check if the ASN is within the valid range
132 PyErr_Format(PyExc_ValueError
, "Invalid ASN %ld", asn
);
136 #if (__WORDSIZE > 32)
137 // Check whether the input was longer than 32 bit
138 if (asn
> UINT32_MAX
) {
139 PyErr_Format(PyExc_ValueError
, "Invalid ASN %ld", asn
);
144 int r
= loc_network_set_asn(self
->network
, asn
);
151 static PyObject
* Network_has_flag(NetworkObject
* self
, PyObject
* args
) {
152 enum loc_network_flags flag
= 0;
154 if (!PyArg_ParseTuple(args
, "i", &flag
))
157 if (loc_network_has_flag(self
->network
, flag
))
163 static PyObject
* Network_set_flag(NetworkObject
* self
, PyObject
* args
) {
164 enum loc_network_flags flag
= 0;
166 if (!PyArg_ParseTuple(args
, "i", &flag
))
169 int r
= loc_network_set_flag(self
->network
, flag
);
172 // What exception to throw here?
179 static PyObject
* Network_exclude(NetworkObject
* self
, PyObject
* args
) {
180 NetworkObject
* other
= NULL
;
182 if (!PyArg_ParseTuple(args
, "O!", &NetworkType
, &other
))
185 struct loc_network_list
* list
= loc_network_exclude(self
->network
, other
->network
);
187 // Convert to Python objects
188 PyObject
* obj
= PyList_FromNetworkList(list
);
189 loc_network_list_unref(list
);
194 static PyObject
* Network_is_subnet_of(NetworkObject
* self
, PyObject
* args
) {
195 NetworkObject
* other
= NULL
;
197 if (!PyArg_ParseTuple(args
, "O!", &NetworkType
, &other
))
200 if (loc_network_is_subnet(other
->network
, self
->network
))
206 static PyObject
* Network_get_family(NetworkObject
* self
) {
207 int family
= loc_network_address_family(self
->network
);
209 return PyLong_FromLong(family
);
212 static PyObject
* Network_get_first_address(NetworkObject
* self
) {
213 const char* address
= loc_network_format_first_address(self
->network
);
215 return PyUnicode_FromString(address
);
218 static PyObject
* PyBytes_FromAddress(const struct in6_addr
* address6
) {
219 struct in_addr address4
;
221 // Convert IPv4 addresses to struct in_addr
222 if (IN6_IS_ADDR_V4MAPPED(address6
)) {
223 address4
.s_addr
= address6
->s6_addr32
[3];
225 return PyBytes_FromStringAndSize((const char*)&address4
, sizeof(address4
));
228 // Return IPv6 addresses as they are
229 return PyBytes_FromStringAndSize((const char*)address6
, sizeof(*address6
));
232 static PyObject
* Network_get__first_address(NetworkObject
* self
) {
233 const struct in6_addr
* address
= loc_network_get_first_address(self
->network
);
235 return PyBytes_FromAddress(address
);
238 static PyObject
* Network_get_last_address(NetworkObject
* self
) {
239 const char* address
= loc_network_format_last_address(self
->network
);
241 return PyUnicode_FromString(address
);
244 static PyObject
* Network_get__last_address(NetworkObject
* self
) {
245 const struct in6_addr
* address
= loc_network_get_last_address(self
->network
);
247 return PyBytes_FromAddress(address
);
250 static struct PyMethodDef Network_methods
[] = {
253 (PyCFunction
)Network_exclude
,
259 (PyCFunction
)Network_has_flag
,
265 (PyCFunction
)Network_is_subnet_of
,
271 (PyCFunction
)Network_set_flag
,
278 static struct PyGetSetDef Network_getsetters
[] = {
281 (getter
)Network_get_asn
,
282 (setter
)Network_set_asn
,
288 (getter
)Network_get_country_code
,
289 (setter
)Network_set_country_code
,
295 (getter
)Network_get_family
,
302 (getter
)Network_get_first_address
,
309 (getter
)Network_get__first_address
,
316 (getter
)Network_get_last_address
,
323 (getter
)Network_get__last_address
,
331 PyTypeObject NetworkType
= {
332 PyVarObject_HEAD_INIT(NULL
, 0)
333 .tp_name
= "location.Network",
334 .tp_basicsize
= sizeof(NetworkObject
),
335 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
336 .tp_new
= Network_new
,
337 .tp_dealloc
= (destructor
)Network_dealloc
,
338 .tp_init
= (initproc
)Network_init
,
339 .tp_doc
= "Network object",
340 .tp_methods
= Network_methods
,
341 .tp_getset
= Network_getsetters
,
342 .tp_repr
= (reprfunc
)Network_repr
,
343 .tp_str
= (reprfunc
)Network_str
,