From 71ff3e69d69d66854cf3f4943d1a7bf51780d939 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 29 Dec 2017 20:39:04 +0000 Subject: [PATCH] network: Add asn and remove reference to struct loc_as Signed-off-by: Michael Tremer --- src/libloc.sym | 2 ++ src/loc/network.h | 3 +++ src/network.c | 24 +++++++++++++----------- src/python/network.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/libloc.sym b/src/libloc.sym index 7218e7b..4026939 100644 --- a/src/libloc.sym +++ b/src/libloc.sym @@ -21,9 +21,11 @@ global: loc_database_unref; # Network + loc_network_get_asn; loc_network_get_country_code; loc_network_new; loc_network_new_from_string; + loc_network_set_asn; loc_network_set_country_code; loc_network_str; loc_network_unref; diff --git a/src/loc/network.h b/src/loc/network.h index daf97d4..32e3091 100644 --- a/src/loc/network.h +++ b/src/loc/network.h @@ -34,6 +34,9 @@ char* loc_network_str(struct loc_network* network); const char* loc_network_get_country_code(struct loc_network* network); int loc_network_set_country_code(struct loc_network* network, const char* country_code); +uint32_t loc_network_get_asn(struct loc_network* network); +int loc_network_set_asn(struct loc_network* network, uint32_t asn); + int loc_network_to_database_v0(struct loc_network* network, struct loc_database_network_v0* dbobj); struct loc_network_tree; diff --git a/src/network.c b/src/network.c index 8617fd5..9d6819c 100644 --- a/src/network.c +++ b/src/network.c @@ -18,11 +18,11 @@ #include #include #include +#include #include #include #include -#include #include #include @@ -34,8 +34,7 @@ struct loc_network { unsigned int prefix; char country_code[3]; - - struct loc_as* as; + uint32_t asn; }; LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network, @@ -116,9 +115,6 @@ LOC_EXPORT struct loc_network* loc_network_ref(struct loc_network* network) { static void loc_network_free(struct loc_network* network) { DEBUG(network->ctx, "Releasing network at %p\n", network); - if (network->as) - loc_as_unref(network->as); - loc_unref(network->ctx); free(network); } @@ -168,6 +164,16 @@ LOC_EXPORT int loc_network_set_country_code(struct loc_network* network, const c return 0; } +LOC_EXPORT uint32_t loc_network_get_asn(struct loc_network* network) { + return network->asn; +} + +LOC_EXPORT int loc_network_set_asn(struct loc_network* network, uint32_t asn) { + network->asn = asn; + + return 0; +} + LOC_EXPORT int loc_network_to_database_v0(struct loc_network* network, struct loc_database_network_v0* dbobj) { dbobj->prefix = htobe16(network->prefix); @@ -177,11 +183,7 @@ LOC_EXPORT int loc_network_to_database_v0(struct loc_network* network, struct lo } // Add ASN - uint32_t asn = 0; - if (network->as) { - asn = loc_as_get_number(network->as); - } - dbobj->asn = htobe32(asn); + dbobj->asn = htobe32(network->asn); return 0; } diff --git a/src/python/network.c b/src/python/network.c index 702faa8..ae7b298 100644 --- a/src/python/network.c +++ b/src/python/network.c @@ -82,7 +82,39 @@ static int Network_set_country_code(NetworkObject* self, PyObject* value) { 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, -- 2.39.2