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 <openssl/err.h>
36 #include <openssl/evp.h>
37 #include <openssl/pem.h>
39 #include <loc/libloc.h>
41 #include <loc/compat.h>
42 #include <loc/country.h>
43 #include <loc/database.h>
44 #include <loc/format.h>
45 #include <loc/network.h>
46 #include <loc/private.h>
47 #include <loc/stringpool.h>
62 size_t signature_length
;
64 // ASes in the database
65 struct loc_database_as_v1
* as_v1
;
69 struct loc_database_network_node_v1
* network_nodes_v1
;
70 size_t network_nodes_count
;
73 struct loc_database_network_v1
* networks_v1
;
74 size_t networks_count
;
77 struct loc_database_country_v1
* countries_v1
;
78 size_t countries_count
;
80 struct loc_stringpool
* pool
;
83 #define MAX_STACK_DEPTH 256
85 struct loc_node_stack
{
87 int i
; // Is this node 0 or 1?
91 struct loc_database_enumerator
{
93 struct loc_database
* db
;
94 enum loc_database_enumerator_mode mode
;
101 enum loc_network_flags flags
;
104 // Index of the AS we are looking at
105 unsigned int as_index
;
108 struct in6_addr network_address
;
109 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
110 int network_stack_depth
;
111 unsigned int* networks_visited
;
114 static int loc_database_read_magic(struct loc_database
* db
) {
115 struct loc_database_magic magic
;
118 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
120 // Check if we have been able to read enough data
121 if (bytes_read
< sizeof(magic
)) {
122 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
123 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
127 // Compare magic bytes
128 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
129 DEBUG(db
->ctx
, "Magic value matches\n");
132 db
->version
= be16toh(magic
.version
);
133 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
138 ERROR(db
->ctx
, "Database format is not compatible\n");
144 static int loc_database_read_as_section_v1(struct loc_database
* db
,
145 const struct loc_database_header_v1
* header
) {
146 off_t as_offset
= be32toh(header
->as_offset
);
147 size_t as_length
= be32toh(header
->as_length
);
149 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
152 db
->as_v1
= mmap(NULL
, as_length
, PROT_READ
,
153 MAP_SHARED
, fileno(db
->f
), as_offset
);
155 if (db
->as_v1
== MAP_FAILED
)
159 db
->as_count
= as_length
/ sizeof(*db
->as_v1
);
161 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
166 static int loc_database_read_network_nodes_section_v1(struct loc_database
* db
,
167 const struct loc_database_header_v1
* header
) {
168 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
169 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
171 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
172 (intmax_t)network_nodes_offset
, network_nodes_length
);
174 if (network_nodes_length
> 0) {
175 db
->network_nodes_v1
= mmap(NULL
, network_nodes_length
, PROT_READ
,
176 MAP_SHARED
, fileno(db
->f
), network_nodes_offset
);
178 if (db
->network_nodes_v1
== MAP_FAILED
)
182 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v1
);
184 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
189 static int loc_database_read_networks_section_v1(struct loc_database
* db
,
190 const struct loc_database_header_v1
* header
) {
191 off_t networks_offset
= be32toh(header
->network_data_offset
);
192 size_t networks_length
= be32toh(header
->network_data_length
);
194 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
195 (intmax_t)networks_offset
, networks_length
);
197 if (networks_length
> 0) {
198 db
->networks_v1
= mmap(NULL
, networks_length
, PROT_READ
,
199 MAP_SHARED
, fileno(db
->f
), networks_offset
);
201 if (db
->networks_v1
== MAP_FAILED
)
205 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v1
);
207 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
212 static int loc_database_read_countries_section_v1(struct loc_database
* db
,
213 const struct loc_database_header_v1
* header
) {
214 off_t countries_offset
= be32toh(header
->countries_offset
);
215 size_t countries_length
= be32toh(header
->countries_length
);
217 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
218 (intmax_t)countries_offset
, countries_length
);
220 if (countries_length
> 0) {
221 db
->countries_v1
= mmap(NULL
, countries_length
, PROT_READ
,
222 MAP_SHARED
, fileno(db
->f
), countries_offset
);
224 if (db
->countries_v1
== MAP_FAILED
)
228 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v1
);
230 INFO(db
->ctx
, "Read %zu countries from the database\n",
231 db
->countries_count
);
236 static int loc_database_read_header_v1(struct loc_database
* db
) {
237 struct loc_database_header_v1 header
;
240 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
242 if (size
< sizeof(header
)) {
243 ERROR(db
->ctx
, "Could not read enough data for header\n");
248 db
->created_at
= be64toh(header
.created_at
);
249 db
->vendor
= be32toh(header
.vendor
);
250 db
->description
= be32toh(header
.description
);
251 db
->license
= be32toh(header
.license
);
254 db
->signature_length
= be32toh(header
.signature_length
);
255 if (db
->signature_length
) {
256 // Check for a plausible signature length
257 if (db
->signature_length
> LOC_SIGNATURE_MAX_LENGTH
) {
258 ERROR(db
->ctx
, "Signature too long: %ld\n", db
->signature_length
);
262 DEBUG(db
->ctx
, "Reading signature of %ld bytes\n",
263 db
->signature_length
);
265 db
->signature
= malloc(db
->signature_length
);
266 for (unsigned int i
= 0; i
< db
->signature_length
; i
++)
267 db
->signature
[i
] = header
.signature
[i
];
271 off_t pool_offset
= be32toh(header
.pool_offset
);
272 size_t pool_length
= be32toh(header
.pool_length
);
274 int r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
275 db
->f
, pool_length
, pool_offset
);
280 r
= loc_database_read_as_section_v1(db
, &header
);
285 r
= loc_database_read_network_nodes_section_v1(db
, &header
);
290 r
= loc_database_read_networks_section_v1(db
, &header
);
295 r
= loc_database_read_countries_section_v1(db
, &header
);
302 static int loc_database_read_header(struct loc_database
* db
) {
303 switch (db
->version
) {
305 return loc_database_read_header_v1(db
);
308 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
313 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
314 clock_t start
= clock();
318 // Clone file descriptor
321 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
325 // Reopen the file so that we can keep our own file handle
326 db
->f
= fdopen(fd
, "r");
328 ERROR(db
->ctx
, "Could not re-open database file\n");
332 // Rewind to the start of the file
336 int r
= loc_database_read_magic(db
);
341 r
= loc_database_read_header(db
);
345 clock_t end
= clock();
347 INFO(db
->ctx
, "Opened database in %.4fms\n",
348 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
353 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
354 // Fail on invalid file handle
358 struct loc_database
* db
= calloc(1, sizeof(*db
));
363 db
->ctx
= loc_ref(ctx
);
366 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
368 int r
= loc_database_read(db
, f
);
370 loc_database_unref(db
);
379 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
385 static void loc_database_free(struct loc_database
* db
) {
388 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
392 r
= munmap(db
->as_v1
, db
->as_count
* sizeof(*db
->as_v1
));
394 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
397 // Remove mapped network sections
398 if (db
->networks_v1
) {
399 r
= munmap(db
->networks_v1
, db
->networks_count
* sizeof(*db
->networks_v1
));
401 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
404 // Remove mapped network nodes section
405 if (db
->network_nodes_v1
) {
406 r
= munmap(db
->network_nodes_v1
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v1
));
408 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
411 loc_stringpool_unref(db
->pool
);
417 // Close database file
425 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
426 if (--db
->refcount
> 0)
429 loc_database_free(db
);
433 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
434 // Cannot do this when no signature is available
435 if (!db
->signature
) {
436 DEBUG(db
->ctx
, "No signature available to verify\n");
440 // Start the stopwatch
441 clock_t start
= clock();
444 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
446 char* error
= ERR_error_string(ERR_get_error(), NULL
);
447 ERROR(db
->ctx
, "Could not parse public key: %s\n", error
);
454 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
456 // Initialise hash function
457 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
459 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
460 ERR_error_string(ERR_get_error(), NULL
));
466 // Reset file to start
470 struct loc_database_magic magic
;
471 fread(&magic
, 1, sizeof(magic
), db
->f
);
473 hexdump(db
->ctx
, &magic
, sizeof(magic
));
475 // Feed magic into the hash
476 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
478 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
485 struct loc_database_header_v1 header_v1
;
487 switch (db
->version
) {
489 fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
492 for (unsigned int i
= 0; i
< sizeof(header_v1
.signature
); i
++) {
493 header_v1
.signature
[i
] = '\0';
496 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
498 // Feed header into the hash
499 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
501 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
509 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
515 // Walk through the file in chunks of 64kB
516 char buffer
[64 * 1024];
518 while (!feof(db
->f
)) {
519 size_t bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
521 hexdump(db
->ctx
, buffer
, bytes_read
);
523 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
525 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
533 r
= EVP_DigestVerifyFinal(mdctx
,
534 (unsigned char*)db
->signature
, db
->signature_length
);
537 DEBUG(db
->ctx
, "The signature is invalid\n");
540 DEBUG(db
->ctx
, "The signature is valid\n");
543 ERROR(db
->ctx
, "Error verifying the signature: %s\n",
544 ERR_error_string(ERR_get_error(), NULL
));
549 hexdump(db
->ctx
, db
->signature
, db
->signature_length
);
551 clock_t end
= clock();
552 DEBUG(db
->ctx
, "Signature checked in %.4fms\n",
553 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
557 EVP_MD_CTX_free(mdctx
);
563 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
564 return db
->created_at
;
567 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
568 return loc_stringpool_get(db
->pool
, db
->vendor
);
571 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
572 return loc_stringpool_get(db
->pool
, db
->description
);
575 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
576 return loc_stringpool_get(db
->pool
, db
->license
);
579 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
583 // Returns the AS at position pos
584 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
585 if ((size_t)pos
>= db
->as_count
)
588 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
591 switch (db
->version
) {
593 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, db
->as_v1
+ pos
);
601 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
607 // Performs a binary search to find the AS in the list
608 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
610 off_t hi
= db
->as_count
- 1;
613 clock_t start
= clock();
616 off_t i
= (lo
+ hi
) / 2;
618 // Fetch AS in the middle between lo and hi
619 int r
= loc_database_fetch_as(db
, as
, i
);
623 // Check if this is a match
624 uint32_t as_number
= loc_as_get_number(*as
);
625 if (as_number
== number
) {
626 clock_t end
= clock();
628 // Log how fast this has been
629 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
630 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
635 // If it wasn't, we release the AS and
636 // adjust our search pointers
639 if (as_number
< number
) {
651 // Returns the network at position pos
652 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
653 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
654 if ((size_t)pos
>= db
->networks_count
) {
655 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
656 (intmax_t)pos
, (intmax_t)db
->networks_count
);
661 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
664 switch (db
->version
) {
666 r
= loc_network_new_from_database_v1(db
->ctx
, network
,
667 address
, prefix
, db
->networks_v1
+ pos
);
675 char* string
= loc_network_str(*network
);
676 DEBUG(db
->ctx
, "Got network %s\n", string
);
683 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
684 return (node
->network
!= htobe32(0xffffffff));
687 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
688 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
689 const struct loc_database_network_node_v1
* node
) {
690 off_t network_index
= be32toh(node
->network
);
692 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v1
), (intmax_t)network_index
);
695 int r
= loc_database_fetch_network(db
, network
,
696 network_address
, prefix
, network_index
);
698 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
702 // Check if the given IP address is inside the network
703 r
= loc_network_match_address(*network
, address
);
705 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
707 loc_network_unref(*network
);
712 // A network was found and the IP address matches
716 // Searches for an exact match along the path
717 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
718 struct loc_network
** network
, struct in6_addr
* network_address
,
719 const struct loc_database_network_node_v1
* node
, unsigned int level
) {
724 int bit
= in6_addr_get_bit(address
, level
);
725 in6_addr_set_bit(network_address
, level
, bit
);
728 node_index
= be32toh(node
->zero
);
730 node_index
= be32toh(node
->one
);
732 // If the node index is zero, the tree ends here
733 // and we cannot descend any further
734 if (node_index
> 0) {
736 if ((size_t)node_index
>= db
->network_nodes_count
)
739 // Move on to the next node
740 r
= __loc_database_lookup(db
, address
, network
, network_address
,
741 db
->network_nodes_v1
+ node_index
, level
+ 1);
743 // End here if a result was found
751 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
753 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
756 // If this node has a leaf, we will check if it matches
757 if (__loc_database_node_is_leaf(node
)) {
758 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
766 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
767 struct in6_addr
* address
, struct loc_network
** network
) {
768 struct in6_addr network_address
;
769 memset(&network_address
, 0, sizeof(network_address
));
774 clock_t start
= clock();
776 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
777 db
->network_nodes_v1
, 0);
779 clock_t end
= clock();
781 // Log how fast this has been
782 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
783 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
788 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
789 const char* string
, struct loc_network
** network
) {
790 struct in6_addr address
;
792 int r
= loc_parse_address(db
->ctx
, string
, &address
);
796 return loc_database_lookup(db
, &address
, network
);
799 // Returns the country at position pos
800 static int loc_database_fetch_country(struct loc_database
* db
,
801 struct loc_country
** country
, off_t pos
) {
802 if ((size_t)pos
>= db
->countries_count
)
805 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
808 switch (db
->version
) {
810 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, db
->countries_v1
+ pos
);
818 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
824 // Performs a binary search to find the country in the list
825 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
826 struct loc_country
** country
, const char* code
) {
828 off_t hi
= db
->countries_count
- 1;
831 clock_t start
= clock();
834 off_t i
= (lo
+ hi
) / 2;
836 // Fetch country in the middle between lo and hi
837 int r
= loc_database_fetch_country(db
, country
, i
);
841 // Check if this is a match
842 const char* cc
= loc_country_get_code(*country
);
843 int result
= strcmp(code
, cc
);
846 clock_t end
= clock();
848 // Log how fast this has been
849 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
850 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
855 // If it wasn't, we release the country and
856 // adjust our search pointers
857 loc_country_unref(*country
);
873 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
874 struct loc_database
* db
, enum loc_database_enumerator_mode mode
) {
875 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
880 e
->ctx
= loc_ref(db
->ctx
);
881 e
->db
= loc_database_ref(db
);
885 // Initialise graph search
886 //e->network_stack[++e->network_stack_depth] = 0;
887 e
->network_stack_depth
= 1;
888 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
890 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
896 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
897 enumerator
->refcount
++;
902 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
903 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
905 // Release all references
906 loc_database_unref(enumerator
->db
);
907 loc_unref(enumerator
->ctx
);
909 if (enumerator
->string
)
910 free(enumerator
->string
);
912 // Free network search
913 free(enumerator
->networks_visited
);
918 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
922 if (--enumerator
->refcount
> 0)
925 loc_database_enumerator_free(enumerator
);
929 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
930 enumerator
->string
= strdup(string
);
932 // Make the string lowercase
933 for (char *p
= enumerator
->string
; *p
; p
++)
939 LOC_EXPORT
int loc_database_enumerator_set_country_code(struct loc_database_enumerator
* enumerator
, const char* country_code
) {
940 // Set empty country code
941 if (!country_code
|| !*country_code
) {
942 *enumerator
->country_code
= '\0';
946 // Treat A1, A2, A3 as special country codes,
947 // but perform search for flags instead
948 if (strcmp(country_code
, "A1") == 0) {
949 return loc_database_enumerator_set_flag(enumerator
,
950 LOC_NETWORK_FLAG_ANONYMOUS_PROXY
);
951 } else if (strcmp(country_code
, "A2") == 0) {
952 return loc_database_enumerator_set_flag(enumerator
,
953 LOC_NETWORK_FLAG_SATELLITE_PROVIDER
);
954 } else if (strcmp(country_code
, "A3") == 0) {
955 return loc_database_enumerator_set_flag(enumerator
,
956 LOC_NETWORK_FLAG_ANYCAST
);
959 // Country codes must be two characters
960 if (!loc_country_code_is_valid(country_code
))
963 for (unsigned int i
= 0; i
< 3; i
++) {
964 enumerator
->country_code
[i
] = country_code
[i
];
970 LOC_EXPORT
int loc_database_enumerator_set_asn(
971 struct loc_database_enumerator
* enumerator
, unsigned int asn
) {
972 enumerator
->asn
= asn
;
977 LOC_EXPORT
int loc_database_enumerator_set_flag(
978 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
979 enumerator
->flags
|= flag
;
984 LOC_EXPORT
int loc_database_enumerator_set_family(
985 struct loc_database_enumerator
* enumerator
, int family
) {
986 enumerator
->family
= family
;
991 LOC_EXPORT
int loc_database_enumerator_next_as(
992 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
995 // Do not do anything if not in AS mode
996 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
999 struct loc_database
* db
= enumerator
->db
;
1001 while (enumerator
->as_index
< db
->as_count
) {
1002 // Fetch the next AS
1003 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1007 r
= loc_as_match_string(*as
, enumerator
->string
);
1009 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1010 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1021 enumerator
->as_index
= 0;
1023 // We have searched through all of them
1027 static int loc_database_enumerator_stack_push_node(
1028 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1029 // Do not add empty nodes
1033 // Check if there is any space left on the stack
1034 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1035 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1039 // Increase stack size
1040 int s
= ++e
->network_stack_depth
;
1042 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1044 e
->network_stack
[s
].offset
= offset
;
1045 e
->network_stack
[s
].i
= i
;
1046 e
->network_stack
[s
].depth
= depth
;
1051 LOC_EXPORT
int loc_database_enumerator_next_network(
1052 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1056 // Do not do anything if not in network mode
1057 if (enumerator
->mode
!= LOC_DB_ENUMERATE_NETWORKS
)
1062 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1063 enumerator
->network_stack_depth
);
1066 while (enumerator
->network_stack_depth
> 0) {
1067 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1069 // Get object from top of the stack
1070 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1072 // Remove the node from the stack if we have already visited it
1073 if (enumerator
->networks_visited
[node
->offset
]) {
1074 enumerator
->network_stack_depth
--;
1078 // Mark the bits on the path correctly
1079 in6_addr_set_bit(&enumerator
->network_address
,
1080 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1082 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1083 enumerator
->networks_visited
[node
->offset
]++;
1085 // Pop node from top of the stack
1086 struct loc_database_network_node_v1
* n
=
1087 enumerator
->db
->network_nodes_v1
+ node
->offset
;
1089 // Add edges to stack
1090 r
= loc_database_enumerator_stack_push_node(enumerator
,
1091 be32toh(n
->one
), 1, node
->depth
+ 1);
1096 r
= loc_database_enumerator_stack_push_node(enumerator
,
1097 be32toh(n
->zero
), 0, node
->depth
+ 1);
1102 // Check if this node is a leaf and has a network object
1103 if (__loc_database_node_is_leaf(n
)) {
1104 off_t network_index
= be32toh(n
->network
);
1106 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1108 // Fetch the network object
1109 r
= loc_database_fetch_network(enumerator
->db
, network
,
1110 &enumerator
->network_address
, node
->depth
, network_index
);
1112 // Break on any errors
1116 // Check if we are interested in this network
1118 // Skip if the family does not match
1119 if (enumerator
->family
&& loc_network_address_family(*network
) != enumerator
->family
) {
1120 loc_network_unref(*network
);
1126 // Skip if the country code does not match
1127 if (*enumerator
->country_code
&&
1128 !loc_network_match_country_code(*network
, enumerator
->country_code
)) {
1129 loc_network_unref(*network
);
1135 // Skip if the ASN does not match
1136 if (enumerator
->asn
&&
1137 !loc_network_match_asn(*network
, enumerator
->asn
)) {
1138 loc_network_unref(*network
);
1144 // Skip if flags do not match
1145 if (enumerator
->flags
&&
1146 !loc_network_match_flag(*network
, enumerator
->flags
)) {
1147 loc_network_unref(*network
);
1155 // Reached the end of the search
1157 // Mark all nodes as non-visited
1158 for (unsigned int i
= 0; i
< enumerator
->db
->network_nodes_count
; i
++)
1159 enumerator
->networks_visited
[i
] = 0;