]>
Commit | Line | Data |
---|---|---|
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 | ||
27 | static PyObject* Network_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
28 | NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0); | |
29 | ||
30 | return (PyObject*)self; | |
31 | } | |
32 | ||
33 | static void Network_dealloc(NetworkObject* self) { | |
34 | if (self->network) | |
35 | loc_network_unref(self->network); | |
36 | ||
37 | Py_TYPE(self)->tp_free((PyObject* )self); | |
38 | } | |
39 | ||
40 | static int Network_init(NetworkObject* self, PyObject* args, PyObject* kwargs) { | |
41 | const char* network = NULL; | |
42 | ||
43 | if (!PyArg_ParseTuple(args, "s", &network)) | |
44 | return -1; | |
45 | ||
46 | // Load the Network | |
47 | int r = loc_network_new_from_string(loc_ctx, &self->network, network); | |
48 | if (r) { | |
49 | PyErr_Format(PyExc_ValueError, "Invalid network: %s", network); | |
50 | return -1; | |
51 | } | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | static PyObject* Network_repr(NetworkObject* self) { | |
57 | char* network = loc_network_str(self->network); | |
58 | ||
59 | PyObject* obj = PyUnicode_FromFormat("<location.Network %s>", network); | |
60 | free(network); | |
61 | ||
62 | return obj; | |
63 | } | |
64 | ||
65 | static PyObject* Network_get_country_code(NetworkObject* self) { | |
66 | const char* country_code = loc_network_get_country_code(self->network); | |
67 | ||
68 | return PyUnicode_FromString(country_code); | |
69 | } | |
70 | ||
71 | static int Network_set_country_code(NetworkObject* self, PyObject* value) { | |
72 | const char* country_code = PyUnicode_AsUTF8(value); | |
73 | ||
74 | int r = loc_network_set_country_code(self->network, country_code); | |
75 | if (r) { | |
76 | if (r == -EINVAL) | |
77 | PyErr_SetString(PyExc_ValueError, "Invalid country code"); | |
78 | ||
79 | return -1; | |
80 | } | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
71ff3e69 MT |
85 | static PyObject* Network_get_asn(NetworkObject* self) { |
86 | uint32_t asn = loc_network_get_asn(self->network); | |
87 | ||
88 | if (asn) | |
89 | return PyLong_FromLong(asn); | |
90 | ||
91 | Py_RETURN_NONE; | |
92 | } | |
93 | ||
94 | static int Network_set_asn(NetworkObject* self, PyObject* value) { | |
95 | long int asn = PyLong_AsLong(value); | |
96 | ||
97 | // Check if the ASN is within the valid range | |
98 | if (asn <= 0 || asn > UINT32_MAX) { | |
99 | PyErr_Format(PyExc_ValueError, "Invalid ASN %ld", asn); | |
100 | return -1; | |
101 | } | |
102 | ||
103 | int r = loc_network_set_asn(self->network, asn); | |
104 | if (r) | |
105 | return -1; | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
39967361 | 110 | static struct PyGetSetDef Network_getsetters[] = { |
71ff3e69 MT |
111 | { |
112 | "asn", | |
113 | (getter)Network_get_asn, | |
114 | (setter)Network_set_asn, | |
115 | NULL, | |
116 | NULL, | |
117 | }, | |
39967361 MT |
118 | { |
119 | "country_code", | |
120 | (getter)Network_get_country_code, | |
121 | (setter)Network_set_country_code, | |
122 | NULL, | |
123 | NULL, | |
124 | }, | |
125 | { NULL }, | |
126 | }; | |
127 | ||
128 | PyTypeObject NetworkType = { | |
129 | PyVarObject_HEAD_INIT(NULL, 0) | |
130 | tp_name: "location.Network", | |
131 | tp_basicsize: sizeof(NetworkObject), | |
132 | tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, | |
133 | tp_new: Network_new, | |
134 | tp_dealloc: (destructor)Network_dealloc, | |
135 | tp_init: (initproc)Network_init, | |
136 | tp_doc: "Network object", | |
137 | tp_getset: Network_getsetters, | |
138 | tp_repr: (reprfunc)Network_repr, | |
139 | }; |