]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/network.c
python: Show country code that was invalid
[people/ms/libloc.git] / src / python / network.c
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 PyObject* 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
36 static 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
42 static 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
49 static 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
65 static 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
74 static PyObject* Network_get_country_code(NetworkObject* self) {
75 const char* country_code = loc_network_get_country_code(self->network);
76
77 return PyUnicode_FromString(country_code);
78 }
79
80 static int Network_set_country_code(NetworkObject* self, PyObject* value) {
81 const char* country_code = PyUnicode_AsUTF8(value);
82
83 int r = loc_network_set_country_code(self->network, country_code);
84 if (r) {
85 if (r == -EINVAL)
86 PyErr_Format(PyExc_ValueError,
87 "Invalid country code: %s", country_code);
88
89 return -1;
90 }
91
92 return 0;
93 }
94
95 static PyObject* Network_get_asn(NetworkObject* self) {
96 uint32_t asn = loc_network_get_asn(self->network);
97
98 if (asn)
99 return PyLong_FromLong(asn);
100
101 Py_RETURN_NONE;
102 }
103
104 static int Network_set_asn(NetworkObject* self, PyObject* value) {
105 long int asn = PyLong_AsLong(value);
106
107 // Check if the ASN is within the valid range
108 if (asn <= 0 || asn > UINT32_MAX) {
109 PyErr_Format(PyExc_ValueError, "Invalid ASN %ld", asn);
110 return -1;
111 }
112
113 int r = loc_network_set_asn(self->network, asn);
114 if (r)
115 return -1;
116
117 return 0;
118 }
119
120 static struct PyGetSetDef Network_getsetters[] = {
121 {
122 "asn",
123 (getter)Network_get_asn,
124 (setter)Network_set_asn,
125 NULL,
126 NULL,
127 },
128 {
129 "country_code",
130 (getter)Network_get_country_code,
131 (setter)Network_set_country_code,
132 NULL,
133 NULL,
134 },
135 { NULL },
136 };
137
138 PyTypeObject NetworkType = {
139 PyVarObject_HEAD_INIT(NULL, 0)
140 tp_name: "location.Network",
141 tp_basicsize: sizeof(NetworkObject),
142 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
143 tp_new: Network_new,
144 tp_dealloc: (destructor)Network_dealloc,
145 tp_init: (initproc)Network_init,
146 tp_doc: "Network object",
147 tp_getset: Network_getsetters,
148 tp_repr: (reprfunc)Network_repr,
149 };