if (!c)
return -ENOMEM;
diff --git a/src/database.c b/src/database.c
-index fa1dad0..7100298 100644
+index fa1dad0..4b8bf1d 100644
--- a/src/database.c
+++ b/src/database.c
@@ -38,8 +38,10 @@
be32toh(n->one), 1, node->depth + 1);
if (r)
-@@ -1175,56 +1261,145 @@ LOC_EXPORT int loc_database_enumerator_next_network(
+@@ -1175,54 +1261,142 @@ LOC_EXPORT int loc_database_enumerator_next_network(
if (r)
return r;
- return 0;
+ loc_network_unref(subnet);
+ continue;
- }
++ }
+
+ // If this is not a subnet, we push it back onto the stack and break
+ r = loc_network_list_push(enumerator->stack, subnet);
+ loc_network_list_unref(subnets);
+
+ return r;
-+ }
+ }
+
+ loc_network_unref(subnet);
+ break;
- // Reached the end of the search
+ DEBUG(enumerator->ctx, "Found %zu subnet(s)\n", loc_network_list_size(subnets));
-+
+
+- // Mark all nodes as non-visited
+- for (unsigned int i = 0; i < enumerator->db->network_nodes_count; i++)
+- enumerator->networks_visited[i] = 0;
+ // We can abort here if the network has no subnets
+ if (loc_network_list_empty(subnets)) {
+ loc_network_list_unref(subnets);
-+
+
+- return 0;
+ return 0;
+ }
-
-- // Mark all nodes as non-visited
-- for (unsigned int i = 0; i < enumerator->db->network_nodes_count; i++)
-- enumerator->networks_visited[i] = 0;
++
+ // If the network has any subnets, we will break it into smaller parts
+ // without the subnets.
+ struct loc_network_list* excluded = loc_network_exclude_list(*network, subnets);
+ loc_network_list_unref(subnets);
+ loc_network_list_unref(excluded);
+
-+ // Replace network with the first one from the stack
++ // Drop the network and restart the whole process again to pick the next network
+ loc_network_unref(*network);
-+ *network = loc_network_list_pop_first(enumerator->stack);
-
- return 0;
- }
-
++
++ return __loc_database_enumerator_next_network_flattened(enumerator, network);
++}
++
+LOC_EXPORT int loc_database_enumerator_next_network(
+ struct loc_database_enumerator* enumerator, struct loc_network** network) {
+ // Do not do anything if not in network mode
+ return __loc_database_enumerator_next_network_flattened(enumerator, network);
+
+ return __loc_database_enumerator_next_network(enumerator, network, 1);
-+}
-+
+ }
+
LOC_EXPORT int loc_database_enumerator_next_country(
- struct loc_database_enumerator* enumerator, struct loc_country** country) {
- *country = NULL;
diff --git a/src/libloc.sym b/src/libloc.sym
index b8296eb..ee333f1 100644
--- a/src/libloc.sym