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 PyObject
* new_network(PyTypeObject
* type
, struct loc_network
* network
) {
28 NetworkObject
* self
= (NetworkObject
*)type
->tp_alloc(type
, 0);
30 self
->network
= loc_network_ref(network
);
33 return (PyObject
*)self
;
36 static PyObject
* Network_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
37 NetworkObject
* self
= (NetworkObject
*)type
->tp_alloc(type
, 0);
39 return (PyObject
*)self
;
42 static void Network_dealloc(NetworkObject
* self
) {
44 loc_network_unref(self
->network
);
46 Py_TYPE(self
)->tp_free((PyObject
* )self
);
49 static int Network_init(NetworkObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
50 const char* network
= NULL
;
52 if (!PyArg_ParseTuple(args
, "s", &network
))
56 int r
= loc_network_new_from_string(loc_ctx
, &self
->network
, network
);
58 PyErr_Format(PyExc_ValueError
, "Invalid network: %s", network
);
65 static PyObject
* Network_repr(NetworkObject
* self
) {
66 char* network
= loc_network_str(self
->network
);
68 PyObject
* obj
= PyUnicode_FromFormat("<location.Network %s>", network
);
74 static PyObject
* Network_str(NetworkObject
* self
) {
75 char* network
= loc_network_str(self
->network
);
77 PyObject
* obj
= PyUnicode_FromString(network
);
83 static PyObject
* Network_get_country_code(NetworkObject
* self
) {
84 const char* country_code
= loc_network_get_country_code(self
->network
);
86 return PyUnicode_FromString(country_code
);
89 static int Network_set_country_code(NetworkObject
* self
, PyObject
* value
) {
90 const char* country_code
= PyUnicode_AsUTF8(value
);
92 int r
= loc_network_set_country_code(self
->network
, country_code
);
95 PyErr_Format(PyExc_ValueError
,
96 "Invalid country code: %s", country_code
);
104 static PyObject
* Network_get_asn(NetworkObject
* self
) {
105 uint32_t asn
= loc_network_get_asn(self
->network
);
108 return PyLong_FromLong(asn
);
113 static int Network_set_asn(NetworkObject
* self
, PyObject
* value
) {
114 long int asn
= PyLong_AsLong(value
);
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
);
122 int r
= loc_network_set_asn(self
->network
, asn
);
129 static PyObject
* Network_has_flag(NetworkObject
* self
, PyObject
* args
) {
130 enum loc_network_flags flag
= 0;
132 if (!PyArg_ParseTuple(args
, "i", &flag
))
135 if (loc_network_has_flag(self
->network
, flag
))
141 static PyObject
* Network_set_flag(NetworkObject
* self
, PyObject
* args
) {
142 enum loc_network_flags flag
= 0;
144 if (!PyArg_ParseTuple(args
, "i", &flag
))
147 int r
= loc_network_set_flag(self
->network
, flag
);
150 // What exception to throw here?
157 static struct PyMethodDef Network_methods
[] = {
160 (PyCFunction
)Network_has_flag
,
166 (PyCFunction
)Network_set_flag
,
173 static struct PyGetSetDef Network_getsetters
[] = {
176 (getter
)Network_get_asn
,
177 (setter
)Network_set_asn
,
183 (getter
)Network_get_country_code
,
184 (setter
)Network_set_country_code
,
191 PyTypeObject NetworkType
= {
192 PyVarObject_HEAD_INIT(NULL
, 0)
193 .tp_name
= "location.Network",
194 .tp_basicsize
= sizeof(NetworkObject
),
195 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
196 .tp_new
= Network_new
,
197 .tp_dealloc
= (destructor
)Network_dealloc
,
198 .tp_init
= (initproc
)Network_init
,
199 .tp_doc
= "Network object",
200 .tp_methods
= Network_methods
,
201 .tp_getset
= Network_getsetters
,
202 .tp_repr
= (reprfunc
)Network_repr
,
203 .tp_str
= (reprfunc
)Network_str
,