#include "locationmodule.h"
#include "network.h"
+PyObject* new_network(PyTypeObject* type, struct loc_network* network) {
+ NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
+ if (self) {
+ self->network = loc_network_ref(network);
+ }
+
+ return (PyObject*)self;
+}
+
static PyObject* Network_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
return obj;
}
+static PyObject* Network_str(NetworkObject* self) {
+ char* network = loc_network_str(self->network);
+
+ PyObject* obj = PyUnicode_FromString(network);
+ free(network);
+
+ return obj;
+}
+
static PyObject* Network_get_country_code(NetworkObject* self) {
const char* country_code = loc_network_get_country_code(self->network);
int r = loc_network_set_country_code(self->network, country_code);
if (r) {
if (r == -EINVAL)
- PyErr_SetString(PyExc_ValueError, "Invalid country code");
+ PyErr_Format(PyExc_ValueError,
+ "Invalid country code: %s", country_code);
+
+ return -1;
+ }
+
+ return 0;
+}
+
+static PyObject* Network_get_asn(NetworkObject* self) {
+ uint32_t asn = loc_network_get_asn(self->network);
+
+ if (asn)
+ return PyLong_FromLong(asn);
+
+ Py_RETURN_NONE;
+}
+static int Network_set_asn(NetworkObject* self, PyObject* value) {
+ long int asn = PyLong_AsLong(value);
+
+ // Check if the ASN is within the valid range
+ if (asn <= 0 || asn > UINT32_MAX) {
+ PyErr_Format(PyExc_ValueError, "Invalid ASN %ld", asn);
return -1;
}
+ int r = loc_network_set_asn(self->network, asn);
+ if (r)
+ return -1;
+
return 0;
}
static struct PyGetSetDef Network_getsetters[] = {
+ {
+ "asn",
+ (getter)Network_get_asn,
+ (setter)Network_set_asn,
+ NULL,
+ NULL,
+ },
{
"country_code",
(getter)Network_get_country_code,
PyTypeObject NetworkType = {
PyVarObject_HEAD_INIT(NULL, 0)
- tp_name: "location.Network",
- tp_basicsize: sizeof(NetworkObject),
- tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
- tp_new: Network_new,
- tp_dealloc: (destructor)Network_dealloc,
- tp_init: (initproc)Network_init,
- tp_doc: "Network object",
- tp_getset: Network_getsetters,
- tp_repr: (reprfunc)Network_repr,
+ .tp_name = "location.Network",
+ .tp_basicsize = sizeof(NetworkObject),
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_new = Network_new,
+ .tp_dealloc = (destructor)Network_dealloc,
+ .tp_init = (initproc)Network_init,
+ .tp_doc = "Network object",
+ .tp_getset = Network_getsetters,
+ .tp_repr = (reprfunc)Network_repr,
+ .tp_str = (reprfunc)Network_str,
};