2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
17 #include <arpa/inet.h>
20 #include <netinet/in.h>
27 #include <sys/types.h>
35 #include <loc/libloc.h>
37 #include <loc/compat.h>
38 #include <loc/country.h>
39 #include <loc/database.h>
40 #include <loc/format.h>
41 #include <loc/network.h>
42 #include <loc/private.h>
43 #include <loc/stringpool.h>
55 // ASes in the database
56 struct loc_database_as_v0
* as_v0
;
60 struct loc_database_network_node_v0
* network_nodes_v0
;
61 size_t network_nodes_count
;
64 struct loc_database_network_v0
* networks_v0
;
65 size_t networks_count
;
68 struct loc_database_country_v0
* countries_v0
;
69 size_t countries_count
;
71 struct loc_stringpool
* pool
;
74 #define MAX_STACK_DEPTH 256
76 struct loc_node_stack
{
78 int i
; // Is this node 0 or 1?
82 struct loc_database_enumerator
{
84 struct loc_database
* db
;
85 enum loc_database_enumerator_mode mode
;
92 enum loc_network_flags flags
;
94 // Index of the AS we are looking at
95 unsigned int as_index
;
98 struct in6_addr network_address
;
99 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
100 int network_stack_depth
;
101 unsigned int* networks_visited
;
104 static int loc_database_read_magic(struct loc_database
* db
, FILE* f
) {
105 struct loc_database_magic magic
;
108 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), f
);
110 // Check if we have been able to read enough data
111 if (bytes_read
< sizeof(magic
)) {
112 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
113 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
117 // Compare magic bytes
118 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
119 DEBUG(db
->ctx
, "Magic value matches\n");
122 db
->version
= be16toh(magic
.version
);
123 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
128 ERROR(db
->ctx
, "Database format is not compatible\n");
134 static int loc_database_read_as_section_v0(struct loc_database
* db
,
135 FILE* f
, const struct loc_database_header_v0
* header
) {
136 off_t as_offset
= be32toh(header
->as_offset
);
137 size_t as_length
= be32toh(header
->as_length
);
139 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
142 db
->as_v0
= mmap(NULL
, as_length
, PROT_READ
,
143 MAP_SHARED
, fileno(f
), as_offset
);
145 if (db
->as_v0
== MAP_FAILED
)
149 db
->as_count
= as_length
/ sizeof(*db
->as_v0
);
151 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
156 static int loc_database_read_network_nodes_section_v0(struct loc_database
* db
,
157 FILE* f
, const struct loc_database_header_v0
* header
) {
158 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
159 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
161 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
162 (intmax_t)network_nodes_offset
, network_nodes_length
);
164 if (network_nodes_length
> 0) {
165 db
->network_nodes_v0
= mmap(NULL
, network_nodes_length
, PROT_READ
,
166 MAP_SHARED
, fileno(f
), network_nodes_offset
);
168 if (db
->network_nodes_v0
== MAP_FAILED
)
172 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v0
);
174 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
179 static int loc_database_read_networks_section_v0(struct loc_database
* db
,
180 FILE* f
, const struct loc_database_header_v0
* header
) {
181 off_t networks_offset
= be32toh(header
->network_data_offset
);
182 size_t networks_length
= be32toh(header
->network_data_length
);
184 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
185 (intmax_t)networks_offset
, networks_length
);
187 if (networks_length
> 0) {
188 db
->networks_v0
= mmap(NULL
, networks_length
, PROT_READ
,
189 MAP_SHARED
, fileno(f
), networks_offset
);
191 if (db
->networks_v0
== MAP_FAILED
)
195 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v0
);
197 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
202 static int loc_database_read_countries_section_v0(struct loc_database
* db
,
203 FILE* f
, const struct loc_database_header_v0
* header
) {
204 off_t countries_offset
= be32toh(header
->countries_offset
);
205 size_t countries_length
= be32toh(header
->countries_length
);
207 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
208 (intmax_t)countries_offset
, countries_length
);
210 if (countries_length
> 0) {
211 db
->countries_v0
= mmap(NULL
, countries_length
, PROT_READ
,
212 MAP_SHARED
, fileno(f
), countries_offset
);
214 if (db
->countries_v0
== MAP_FAILED
)
218 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v0
);
220 INFO(db
->ctx
, "Read %zu countries from the database\n",
221 db
->countries_count
);
226 static int loc_database_read_header_v0(struct loc_database
* db
, FILE* f
) {
227 struct loc_database_header_v0 header
;
230 size_t size
= fread(&header
, 1, sizeof(header
), f
);
232 if (size
< sizeof(header
)) {
233 ERROR(db
->ctx
, "Could not read enough data for header\n");
238 db
->created_at
= be64toh(header
.created_at
);
239 db
->vendor
= be32toh(header
.vendor
);
240 db
->description
= be32toh(header
.description
);
241 db
->license
= be32toh(header
.license
);
244 off_t pool_offset
= be32toh(header
.pool_offset
);
245 size_t pool_length
= be32toh(header
.pool_length
);
247 int r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
248 f
, pool_length
, pool_offset
);
253 r
= loc_database_read_as_section_v0(db
, f
, &header
);
258 r
= loc_database_read_network_nodes_section_v0(db
, f
, &header
);
263 r
= loc_database_read_networks_section_v0(db
, f
, &header
);
268 r
= loc_database_read_countries_section_v0(db
, f
, &header
);
275 static int loc_database_read_header(struct loc_database
* db
, FILE* f
) {
276 switch (db
->version
) {
278 return loc_database_read_header_v0(db
, f
);
281 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
286 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
287 clock_t start
= clock();
290 int r
= loc_database_read_magic(db
, f
);
295 r
= loc_database_read_header(db
, f
);
299 clock_t end
= clock();
301 INFO(db
->ctx
, "Opened database in %.4fms\n",
302 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
307 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
308 // Fail on invalid file handle
312 struct loc_database
* db
= calloc(1, sizeof(*db
));
317 db
->ctx
= loc_ref(ctx
);
320 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
322 int r
= loc_database_read(db
, f
);
324 loc_database_unref(db
);
333 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
339 static void loc_database_free(struct loc_database
* db
) {
342 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
346 r
= munmap(db
->as_v0
, db
->as_count
* sizeof(*db
->as_v0
));
348 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
351 // Remove mapped network sections
352 if (db
->networks_v0
) {
353 r
= munmap(db
->networks_v0
, db
->networks_count
* sizeof(*db
->networks_v0
));
355 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
358 // Remove mapped network nodes section
359 if (db
->network_nodes_v0
) {
360 r
= munmap(db
->network_nodes_v0
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v0
));
362 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
365 loc_stringpool_unref(db
->pool
);
371 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
372 if (--db
->refcount
> 0)
375 loc_database_free(db
);
379 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
380 return db
->created_at
;
383 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
384 return loc_stringpool_get(db
->pool
, db
->vendor
);
387 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
388 return loc_stringpool_get(db
->pool
, db
->description
);
391 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
392 return loc_stringpool_get(db
->pool
, db
->license
);
395 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
399 // Returns the AS at position pos
400 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
401 if ((size_t)pos
>= db
->as_count
)
404 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
407 switch (db
->version
) {
409 r
= loc_as_new_from_database_v0(db
->ctx
, db
->pool
, as
, db
->as_v0
+ pos
);
417 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
423 // Performs a binary search to find the AS in the list
424 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
426 off_t hi
= db
->as_count
- 1;
429 clock_t start
= clock();
432 off_t i
= (lo
+ hi
) / 2;
434 // Fetch AS in the middle between lo and hi
435 int r
= loc_database_fetch_as(db
, as
, i
);
439 // Check if this is a match
440 uint32_t as_number
= loc_as_get_number(*as
);
441 if (as_number
== number
) {
442 clock_t end
= clock();
444 // Log how fast this has been
445 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
446 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
451 // If it wasn't, we release the AS and
452 // adjust our search pointers
455 if (as_number
< number
) {
467 // Returns the network at position pos
468 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
469 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
470 if ((size_t)pos
>= db
->networks_count
) {
471 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
472 (intmax_t)pos
, (intmax_t)db
->networks_count
);
477 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
480 switch (db
->version
) {
482 r
= loc_network_new_from_database_v0(db
->ctx
, network
,
483 address
, prefix
, db
->networks_v0
+ pos
);
491 char* string
= loc_network_str(*network
);
492 DEBUG(db
->ctx
, "Got network %s\n", string
);
499 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v0
* node
) {
500 return (node
->network
!= htobe32(0xffffffff));
503 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
504 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
505 const struct loc_database_network_node_v0
* node
) {
506 off_t network_index
= be32toh(node
->network
);
508 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v0
), (intmax_t)network_index
);
511 int r
= loc_database_fetch_network(db
, network
,
512 network_address
, prefix
, network_index
);
514 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
518 // Check if the given IP address is inside the network
519 r
= loc_network_match_address(*network
, address
);
521 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
523 loc_network_unref(*network
);
528 // A network was found and the IP address matches
532 // Searches for an exact match along the path
533 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
534 struct loc_network
** network
, struct in6_addr
* network_address
,
535 const struct loc_database_network_node_v0
* node
, unsigned int level
) {
540 int bit
= in6_addr_get_bit(address
, level
);
541 in6_addr_set_bit(network_address
, level
, bit
);
544 node_index
= be32toh(node
->zero
);
546 node_index
= be32toh(node
->one
);
548 // If the node index is zero, the tree ends here
549 // and we cannot descend any further
550 if (node_index
> 0) {
552 if ((size_t)node_index
>= db
->network_nodes_count
)
555 // Move on to the next node
556 r
= __loc_database_lookup(db
, address
, network
, network_address
,
557 db
->network_nodes_v0
+ node_index
, level
+ 1);
559 // End here if a result was found
567 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
569 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
572 // If this node has a leaf, we will check if it matches
573 if (__loc_database_node_is_leaf(node
)) {
574 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
582 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
583 struct in6_addr
* address
, struct loc_network
** network
) {
584 struct in6_addr network_address
;
585 memset(&network_address
, 0, sizeof(network_address
));
590 clock_t start
= clock();
592 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
593 db
->network_nodes_v0
, 0);
595 clock_t end
= clock();
597 // Log how fast this has been
598 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
599 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
604 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
605 const char* string
, struct loc_network
** network
) {
606 struct in6_addr address
;
608 int r
= loc_parse_address(db
->ctx
, string
, &address
);
612 return loc_database_lookup(db
, &address
, network
);
615 // Returns the country at position pos
616 static int loc_database_fetch_country(struct loc_database
* db
,
617 struct loc_country
** country
, off_t pos
) {
618 if ((size_t)pos
>= db
->countries_count
)
621 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
624 switch (db
->version
) {
626 r
= loc_country_new_from_database_v0(db
->ctx
, db
->pool
, country
, db
->countries_v0
+ pos
);
634 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
640 // Performs a binary search to find the country in the list
641 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
642 struct loc_country
** country
, const char* code
) {
644 off_t hi
= db
->countries_count
- 1;
647 clock_t start
= clock();
650 off_t i
= (lo
+ hi
) / 2;
652 // Fetch country in the middle between lo and hi
653 int r
= loc_database_fetch_country(db
, country
, i
);
657 // Check if this is a match
658 const char* cc
= loc_country_get_code(*country
);
659 int result
= strcmp(code
, cc
);
662 clock_t end
= clock();
664 // Log how fast this has been
665 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
666 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
671 // If it wasn't, we release the country and
672 // adjust our search pointers
673 loc_country_unref(*country
);
689 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
690 struct loc_database
* db
, enum loc_database_enumerator_mode mode
) {
691 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
696 e
->ctx
= loc_ref(db
->ctx
);
697 e
->db
= loc_database_ref(db
);
701 // Initialise graph search
702 //e->network_stack[++e->network_stack_depth] = 0;
703 e
->network_stack_depth
= 1;
704 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
706 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
712 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
713 enumerator
->refcount
++;
718 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
719 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
721 // Release all references
722 loc_database_unref(enumerator
->db
);
723 loc_unref(enumerator
->ctx
);
725 if (enumerator
->string
)
726 free(enumerator
->string
);
728 // Free network search
729 free(enumerator
->networks_visited
);
734 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
738 if (--enumerator
->refcount
> 0)
741 loc_database_enumerator_free(enumerator
);
745 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
746 enumerator
->string
= strdup(string
);
748 // Make the string lowercase
749 for (char *p
= enumerator
->string
; *p
; p
++)
755 LOC_EXPORT
int loc_database_enumerator_set_country_code(struct loc_database_enumerator
* enumerator
, const char* country_code
) {
756 // Set empty country code
757 if (!country_code
|| !*country_code
) {
758 *enumerator
->country_code
= '\0';
762 // Treat A1, A2, A3 as special country codes,
763 // but perform search for flags instead
764 if (strcmp(country_code
, "A1") == 0) {
765 return loc_database_enumerator_set_flag(enumerator
,
766 LOC_NETWORK_FLAG_ANONYMOUS_PROXY
);
767 } else if (strcmp(country_code
, "A2") == 0) {
768 return loc_database_enumerator_set_flag(enumerator
,
769 LOC_NETWORK_FLAG_SATELLITE_PROVIDER
);
770 } else if (strcmp(country_code
, "A3") == 0) {
771 return loc_database_enumerator_set_flag(enumerator
,
772 LOC_NETWORK_FLAG_ANYCAST
);
775 // Country codes must be two characters
776 if (!loc_country_code_is_valid(country_code
))
779 for (unsigned int i
= 0; i
< 3; i
++) {
780 enumerator
->country_code
[i
] = country_code
[i
];
786 LOC_EXPORT
int loc_database_enumerator_set_asn(
787 struct loc_database_enumerator
* enumerator
, unsigned int asn
) {
788 enumerator
->asn
= asn
;
793 LOC_EXPORT
int loc_database_enumerator_set_flag(
794 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
795 enumerator
->flags
|= flag
;
800 LOC_EXPORT
int loc_database_enumerator_next_as(
801 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
804 // Do not do anything if not in AS mode
805 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
808 struct loc_database
* db
= enumerator
->db
;
810 while (enumerator
->as_index
< db
->as_count
) {
812 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
816 r
= loc_as_match_string(*as
, enumerator
->string
);
818 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
819 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
830 enumerator
->as_index
= 0;
832 // We have searched through all of them
836 static int loc_database_enumerator_stack_push_node(
837 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
838 // Do not add empty nodes
842 // Check if there is any space left on the stack
843 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
844 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
848 // Increase stack size
849 int s
= ++e
->network_stack_depth
;
851 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
853 e
->network_stack
[s
].offset
= offset
;
854 e
->network_stack
[s
].i
= i
;
855 e
->network_stack
[s
].depth
= depth
;
860 LOC_EXPORT
int loc_database_enumerator_next_network(
861 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
865 // Do not do anything if not in network mode
866 if (enumerator
->mode
!= LOC_DB_ENUMERATE_NETWORKS
)
871 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
872 enumerator
->network_stack_depth
);
875 while (enumerator
->network_stack_depth
> 0) {
876 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
878 // Get object from top of the stack
879 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
881 // Remove the node from the stack if we have already visited it
882 if (enumerator
->networks_visited
[node
->offset
]) {
883 enumerator
->network_stack_depth
--;
887 // Mark the bits on the path correctly
888 in6_addr_set_bit(&enumerator
->network_address
,
889 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
891 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
892 enumerator
->networks_visited
[node
->offset
]++;
894 // Pop node from top of the stack
895 struct loc_database_network_node_v0
* n
=
896 enumerator
->db
->network_nodes_v0
+ node
->offset
;
898 // Add edges to stack
899 r
= loc_database_enumerator_stack_push_node(enumerator
,
900 be32toh(n
->one
), 1, node
->depth
+ 1);
905 r
= loc_database_enumerator_stack_push_node(enumerator
,
906 be32toh(n
->zero
), 0, node
->depth
+ 1);
911 // Check if this node is a leaf and has a network object
912 if (__loc_database_node_is_leaf(n
)) {
913 off_t network_index
= be32toh(n
->network
);
915 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
917 // Fetch the network object
918 r
= loc_database_fetch_network(enumerator
->db
, network
,
919 &enumerator
->network_address
, node
->depth
, network_index
);
921 // Break on any errors
925 // Check if we are interested in this network
927 // Skip if the country code does not match
928 if (*enumerator
->country_code
&&
929 !loc_network_match_country_code(*network
, enumerator
->country_code
)) {
930 loc_network_unref(*network
);
936 // Skip if the ASN does not match
937 if (enumerator
->asn
&&
938 !loc_network_match_asn(*network
, enumerator
->asn
)) {
939 loc_network_unref(*network
);
945 // Skip if flags do not match
946 if (enumerator
->flags
&&
947 !loc_network_match_flag(*network
, enumerator
->flags
)) {
948 loc_network_unref(*network
);
956 // Reached the end of the search
958 // Mark all nodes as non-visited
959 for (unsigned int i
= 0; i
< enumerator
->db
->network_nodes_count
; i
++)
960 enumerator
->networks_visited
[i
] = 0;