From: Michael Tremer Date: Tue, 16 Dec 2025 16:40:34 +0000 (+0000) Subject: python: network: Add a new property "subnets" to fetch any subnets X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=82bd0c679577f8ad9a13ee4e6dac2a7151e49ca2;p=location%2Flibloc.git python: network: Add a new property "subnets" to fetch any subnets Signed-off-by: Michael Tremer --- diff --git a/src/python/network.c b/src/python/network.c index 637037c..084a3b0 100644 --- a/src/python/network.c +++ b/src/python/network.c @@ -305,6 +305,85 @@ static PyObject* Network_reverse_pointer(NetworkObject* self, PyObject* args, Py return ret; } +static PyObject* Network_get_subnets(NetworkObject* self) { + struct loc_network* subnet1 = NULL; + struct loc_network* subnet2 = NULL; + PyObject* result = NULL; + PyObject* s1 = NULL; + PyObject* s2 = NULL; + int r; + + // Make subnets + r = loc_network_subnets(self->network, &subnet1, &subnet2); + if (r < 0) { + PyErr_SetFromErrno(PyExc_OSError); + goto ERROR; + } + + // Determine the length of the tuple + Py_ssize_t l = 0; + + if (subnet1) + l++; + + if (subnet2) + l++; + + // Create a new result tuple + result = PyTuple_New(l); + if (!result) + goto ERROR; + + Py_ssize_t pos = 0; + + // Convert the subnets into Python objects + if (subnet1) { + s1 = new_network(&NetworkType, subnet1); + if (!s1) + goto ERROR; + + // Append it to the list + PyTuple_SET_ITEM(result, pos++, s1); + Py_INCREF(s1); + } + + if (subnet2) { + s2 = new_network(&NetworkType, subnet2); + if (!s2) + goto ERROR; + + // Append it to the list + PyTuple_SET_ITEM(result, pos++, s2); + Py_INCREF(s2); + } + + // Cleanup + if (subnet1) + loc_network_unref(subnet1); + if (subnet2) + loc_network_unref(subnet2); + if (s1) + Py_DECREF(s1); + if (s2) + Py_DECREF(s2); + + return result; + +ERROR: + if (subnet1) + loc_network_unref(subnet1); + if (subnet2) + loc_network_unref(subnet2); + if (result) + Py_DECREF(result); + if (s1) + Py_DECREF(s1); + if (s2) + Py_DECREF(s2); + + return NULL; +} + static struct PyMethodDef Network_methods[] = { { "exclude", @@ -389,6 +468,13 @@ static struct PyGetSetDef Network_getsetters[] = { NULL, NULL, }, + { + "subnets", + (getter)Network_get_subnets, + NULL, + NULL, + NULL, + }, { NULL }, };