]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
Change how errors are handled by iterators
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 3 Oct 2019 18:22:39 +0000 (18:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 3 Oct 2019 18:22:39 +0000 (18:22 +0000)
Instead of returning a pointer, we are returning an
exit code and a pointer is being set to the object.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/loc/database.h
src/python/database.c

index 7f5a58a165c953f5d783e4deb8090ac777d9d3a0..f98647979d9dd8938fbd8287a1d3379226c216e3 100644 (file)
@@ -662,37 +662,39 @@ LOC_EXPORT int loc_database_enumerator_set_asn(
        return 0;
 }
 
        return 0;
 }
 
-LOC_EXPORT struct loc_as* loc_database_enumerator_next_as(struct loc_database_enumerator* enumerator) {
+LOC_EXPORT int loc_database_enumerator_next_as(
+               struct loc_database_enumerator* enumerator, struct loc_as** as) {
+       *as = NULL;
+
        // Do not do anything if not in AS mode
        if (enumerator->mode != LOC_DB_ENUMERATE_ASES)
        // Do not do anything if not in AS mode
        if (enumerator->mode != LOC_DB_ENUMERATE_ASES)
-               return NULL;
+               return 0;
 
        struct loc_database* db = enumerator->db;
 
        struct loc_database* db = enumerator->db;
-       struct loc_as* as;
 
        while (enumerator->as_index < db->as_count) {
                // Fetch the next AS
 
        while (enumerator->as_index < db->as_count) {
                // Fetch the next AS
-               int r = loc_database_fetch_as(db, &as, enumerator->as_index++);
+               int r = loc_database_fetch_as(db, as, enumerator->as_index++);
                if (r)
                if (r)
-                       return NULL;
+                       return r;
 
 
-               r = loc_as_match_string(as, enumerator->string);
+               r = loc_as_match_string(*as, enumerator->string);
                if (r == 1) {
                        DEBUG(enumerator->ctx, "AS%d (%s) matches %s\n",
                if (r == 1) {
                        DEBUG(enumerator->ctx, "AS%d (%s) matches %s\n",
-                               loc_as_get_number(as), loc_as_get_name(as), enumerator->string);
+                               loc_as_get_number(*as), loc_as_get_name(*as), enumerator->string);
 
 
-                       return as;
+                       return 0;
                }
 
                // No match
                }
 
                // No match
-               loc_as_unref(as);
+               loc_as_unref(*as);
        }
 
        // Reset the index
        enumerator->as_index = 0;
 
        // We have searched through all of them
        }
 
        // Reset the index
        enumerator->as_index = 0;
 
        // We have searched through all of them
-       return NULL;
+       return 0;
 }
 
 static int loc_database_enumerator_stack_push_node(
 }
 
 static int loc_database_enumerator_stack_push_node(
@@ -719,46 +721,52 @@ static int loc_database_enumerator_stack_push_node(
        return 0;
 }
 
        return 0;
 }
 
-static int loc_database_enumerator_network_depth_first_search(
-               struct loc_database_enumerator* e, struct loc_network** network) {
+LOC_EXPORT int loc_database_enumerator_next_network(
+               struct loc_database_enumerator* enumerator, struct loc_network** network) {
        // Reset network
        *network = NULL;
        // Reset network
        *network = NULL;
+
+       // Do not do anything if not in network mode
+       if (enumerator->mode != LOC_DB_ENUMERATE_NETWORKS)
+               return 0;
+
        int r;
 
        int r;
 
-       DEBUG(e->ctx, "Called with a stack of %u nodes\n", e->network_stack_depth);
+       DEBUG(enumerator->ctx, "Called with a stack of %u nodes\n",
+               enumerator->network_stack_depth);
 
        // Perform DFS
 
        // Perform DFS
-       while (e->network_stack_depth > 0) {
-               DEBUG(e->ctx, "Stack depth: %u\n", e->network_stack_depth);
+       while (enumerator->network_stack_depth > 0) {
+               DEBUG(enumerator->ctx, "Stack depth: %u\n", enumerator->network_stack_depth);
 
                // Get object from top of the stack
 
                // Get object from top of the stack
-               struct loc_node_stack* node = &e->network_stack[e->network_stack_depth];
+               struct loc_node_stack* node = &enumerator->network_stack[enumerator->network_stack_depth];
 
                // Remove the node from the stack if we have already visited it
 
                // Remove the node from the stack if we have already visited it
-               if (e->networks_visited[node->offset]) {
-                       e->network_stack_depth--;
+               if (enumerator->networks_visited[node->offset]) {
+                       enumerator->network_stack_depth--;
                        continue;
                }
 
                // Mark the bits on the path correctly
                        continue;
                }
 
                // Mark the bits on the path correctly
-               in6_addr_set_bit(&e->network_address,
+               in6_addr_set_bit(&enumerator->network_address,
                        (node->depth > 0) ? node->depth - 1 : 0, node->i);
 
                        (node->depth > 0) ? node->depth - 1 : 0, node->i);
 
-               DEBUG(e->ctx, "Looking at node %jd\n", node->offset);
-               e->networks_visited[node->offset]++;
+               DEBUG(enumerator->ctx, "Looking at node %jd\n", node->offset);
+               enumerator->networks_visited[node->offset]++;
 
                // Pop node from top of the stack
                struct loc_database_network_node_v0* n =
 
                // Pop node from top of the stack
                struct loc_database_network_node_v0* n =
-                       e->db->network_nodes_v0 + node->offset;
+                       enumerator->db->network_nodes_v0 + node->offset;
 
                // Add edges to stack
 
                // Add edges to stack
-               r = loc_database_enumerator_stack_push_node(e,
+               r = loc_database_enumerator_stack_push_node(enumerator,
                        be32toh(n->one), 1, node->depth + 1);
 
                if (r)
                        return r;
 
                        be32toh(n->one), 1, node->depth + 1);
 
                if (r)
                        return r;
 
-               r = loc_database_enumerator_stack_push_node(e,
+               r = loc_database_enumerator_stack_push_node(enumerator,
                        be32toh(n->zero), 0, node->depth + 1);
 
                if (r)
                        be32toh(n->zero), 0, node->depth + 1);
 
                if (r)
@@ -768,11 +776,11 @@ static int loc_database_enumerator_network_depth_first_search(
                if (__loc_database_node_is_leaf(n)) {
                        off_t network_index = be32toh(n->network);
 
                if (__loc_database_node_is_leaf(n)) {
                        off_t network_index = be32toh(n->network);
 
-                       DEBUG(e->ctx, "Node has a network at %jd\n", network_index);
+                       DEBUG(enumerator->ctx, "Node has a network at %jd\n", network_index);
 
                        // Fetch the network object
 
                        // Fetch the network object
-                       r = loc_database_fetch_network(e->db, network,
-                               &e->network_address, node->depth, network_index);
+                       r = loc_database_fetch_network(enumerator->db, network,
+                               &enumerator->network_address, node->depth, network_index);
 
                        // Break on any errors
                        if (r)
 
                        // Break on any errors
                        if (r)
@@ -781,14 +789,20 @@ static int loc_database_enumerator_network_depth_first_search(
                        // Check if we are interested in this network
 
                        // Skip if the country code does not match
                        // Check if we are interested in this network
 
                        // Skip if the country code does not match
-                       if (e->country_code && !loc_network_match_country_code(*network, e->country_code)) {
+                       if (enumerator->country_code &&
+                                       !loc_network_match_country_code(*network, enumerator->country_code)) {
                                loc_network_unref(*network);
                                loc_network_unref(*network);
+                               *network = NULL;
+
                                continue;
                        }
 
                        // Skip if the ASN does not match
                                continue;
                        }
 
                        // Skip if the ASN does not match
-                       if (e->asn && !loc_network_match_asn(*network, e->asn)) {
+                       if (enumerator->asn &&
+                                       !loc_network_match_asn(*network, enumerator->asn)) {
                                loc_network_unref(*network);
                                loc_network_unref(*network);
+                               *network = NULL;
+
                                continue;
                        }
 
                                continue;
                        }
 
@@ -799,24 +813,8 @@ static int loc_database_enumerator_network_depth_first_search(
        // Reached the end of the search
 
        // Mark all nodes as non-visited
        // Reached the end of the search
 
        // Mark all nodes as non-visited
-       for (unsigned int i = 0; i < e->db->network_nodes_count; i++)
-               e->networks_visited[i] = 0;
+       for (unsigned int i = 0; i < enumerator->db->network_nodes_count; i++)
+               enumerator->networks_visited[i] = 0;
 
        return 0;
 }
 
        return 0;
 }
-
-LOC_EXPORT struct loc_network* loc_database_enumerator_next_network(
-               struct loc_database_enumerator* enumerator) {
-       // Do not do anything if not in network mode
-       if (enumerator->mode != LOC_DB_ENUMERATE_NETWORKS)
-               return NULL;
-
-       struct loc_network* network = NULL;
-
-       int r = loc_database_enumerator_network_depth_first_search(enumerator, &network);
-       if (r) {
-               return NULL;
-       }
-
-       return network;
-}
index 97650bdcd750b1dae1d13aec0be7bcce6089d1b0..3b3208a4f24ec373cb6bcb5ace055f16a1cec29d 100644 (file)
@@ -57,7 +57,9 @@ struct loc_database_enumerator* loc_database_enumerator_unref(struct loc_databas
 int loc_database_enumerator_set_string(struct loc_database_enumerator* enumerator, const char* string);
 int loc_database_enumerator_set_country_code(struct loc_database_enumerator* enumerator, const char* country_code);
 int loc_database_enumerator_set_asn(struct loc_database_enumerator* enumerator, unsigned int asn);
 int loc_database_enumerator_set_string(struct loc_database_enumerator* enumerator, const char* string);
 int loc_database_enumerator_set_country_code(struct loc_database_enumerator* enumerator, const char* country_code);
 int loc_database_enumerator_set_asn(struct loc_database_enumerator* enumerator, unsigned int asn);
-struct loc_as* loc_database_enumerator_next_as(struct loc_database_enumerator* enumerator);
-struct loc_network* loc_database_enumerator_next_network(struct loc_database_enumerator* enumerator);
+int loc_database_enumerator_next_as(
+       struct loc_database_enumerator* enumerator, struct loc_as** as);
+int loc_database_enumerator_next_network(
+       struct loc_database_enumerator* enumerator, struct loc_network** network);
 
 #endif
 
 #endif
index 954b5aceae975760e596c56c0ed945a7686119fa..48023b07c482d6755751a8f393acdef2984e68bf 100644 (file)
@@ -311,8 +311,15 @@ static void DatabaseEnumerator_dealloc(DatabaseEnumeratorObject* self) {
 }
 
 static PyObject* DatabaseEnumerator_next(DatabaseEnumeratorObject* self) {
 }
 
 static PyObject* DatabaseEnumerator_next(DatabaseEnumeratorObject* self) {
+       struct loc_network* network = NULL;
+
        // Enumerate all networks
        // Enumerate all networks
-       struct loc_network* network = loc_database_enumerator_next_network(self->enumerator);
+       int r = loc_database_enumerator_next_network(self->enumerator, &network);
+       if (r) {
+               return NULL;
+       }
+
+       // A network was found
        if (network) {
                PyObject* obj = new_network(&NetworkType, network);
                loc_network_unref(network);
        if (network) {
                PyObject* obj = new_network(&NetworkType, network);
                loc_network_unref(network);
@@ -321,7 +328,13 @@ static PyObject* DatabaseEnumerator_next(DatabaseEnumeratorObject* self) {
        }
 
        // Enumerate all ASes
        }
 
        // Enumerate all ASes
-       struct loc_as* as = loc_database_enumerator_next_as(self->enumerator);
+       struct loc_as* as = NULL;
+
+       r = loc_database_enumerator_next_as(self->enumerator, &as);
+       if (r) {
+               return NULL;
+       }
+
        if (as) {
                PyObject* obj = new_as(&ASType, as);
                loc_as_unref(as);
        if (as) {
                PyObject* obj = new_as(&ASType, as);
                loc_as_unref(as);