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/as-list.h>
42 #include <loc/compat.h>
43 #include <loc/country.h>
44 #include <loc/country-list.h>
45 #include <loc/database.h>
46 #include <loc/format.h>
47 #include <loc/network.h>
48 #include <loc/private.h>
49 #include <loc/stringpool.h>
57 enum loc_database_version version
;
65 size_t signature1_length
;
67 size_t signature2_length
;
69 // ASes in the database
70 struct loc_database_as_v1
* as_v1
;
74 struct loc_database_network_node_v1
* network_nodes_v1
;
75 size_t network_nodes_count
;
78 struct loc_database_network_v1
* networks_v1
;
79 size_t networks_count
;
82 struct loc_database_country_v1
* countries_v1
;
83 size_t countries_count
;
85 struct loc_stringpool
* pool
;
88 #define MAX_STACK_DEPTH 256
90 struct loc_node_stack
{
92 int i
; // Is this node 0 or 1?
96 struct loc_database_enumerator
{
98 struct loc_database
* db
;
99 enum loc_database_enumerator_mode mode
;
104 struct loc_country_list
* countries
;
105 struct loc_as_list
* asns
;
106 enum loc_network_flags flags
;
112 // Index of the AS we are looking at
113 unsigned int as_index
;
115 // Index of the country we are looking at
116 unsigned int country_index
;
119 struct in6_addr network_address
;
120 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
121 int network_stack_depth
;
122 unsigned int* networks_visited
;
125 struct loc_network_list
* stack
;
128 static int loc_database_read_magic(struct loc_database
* db
) {
129 struct loc_database_magic magic
;
132 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
134 // Check if we have been able to read enough data
135 if (bytes_read
< sizeof(magic
)) {
136 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
137 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
141 // Compare magic bytes
142 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
143 DEBUG(db
->ctx
, "Magic value matches\n");
146 db
->version
= magic
.version
;
151 ERROR(db
->ctx
, "Unrecognized file type\n");
157 static int loc_database_read_as_section_v1(struct loc_database
* db
,
158 const struct loc_database_header_v1
* header
) {
159 off_t as_offset
= be32toh(header
->as_offset
);
160 size_t as_length
= be32toh(header
->as_length
);
162 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
165 db
->as_v1
= mmap(NULL
, as_length
, PROT_READ
,
166 MAP_SHARED
, fileno(db
->f
), as_offset
);
168 if (db
->as_v1
== MAP_FAILED
)
172 db
->as_count
= as_length
/ sizeof(*db
->as_v1
);
174 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
179 static int loc_database_read_network_nodes_section_v1(struct loc_database
* db
,
180 const struct loc_database_header_v1
* header
) {
181 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
182 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
184 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
185 (intmax_t)network_nodes_offset
, network_nodes_length
);
187 if (network_nodes_length
> 0) {
188 db
->network_nodes_v1
= mmap(NULL
, network_nodes_length
, PROT_READ
,
189 MAP_SHARED
, fileno(db
->f
), network_nodes_offset
);
191 if (db
->network_nodes_v1
== MAP_FAILED
)
195 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v1
);
197 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
202 static int loc_database_read_networks_section_v1(struct loc_database
* db
,
203 const struct loc_database_header_v1
* header
) {
204 off_t networks_offset
= be32toh(header
->network_data_offset
);
205 size_t networks_length
= be32toh(header
->network_data_length
);
207 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
208 (intmax_t)networks_offset
, networks_length
);
210 if (networks_length
> 0) {
211 db
->networks_v1
= mmap(NULL
, networks_length
, PROT_READ
,
212 MAP_SHARED
, fileno(db
->f
), networks_offset
);
214 if (db
->networks_v1
== MAP_FAILED
)
218 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v1
);
220 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
225 static int loc_database_read_countries_section_v1(struct loc_database
* db
,
226 const struct loc_database_header_v1
* header
) {
227 off_t countries_offset
= be32toh(header
->countries_offset
);
228 size_t countries_length
= be32toh(header
->countries_length
);
230 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
231 (intmax_t)countries_offset
, countries_length
);
233 if (countries_length
> 0) {
234 db
->countries_v1
= mmap(NULL
, countries_length
, PROT_READ
,
235 MAP_SHARED
, fileno(db
->f
), countries_offset
);
237 if (db
->countries_v1
== MAP_FAILED
)
241 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v1
);
243 INFO(db
->ctx
, "Read %zu countries from the database\n",
244 db
->countries_count
);
249 static int loc_database_read_signature(struct loc_database
* db
,
250 char** dst
, char* src
, size_t length
) {
251 // Check for a plausible signature length
252 if (length
> LOC_SIGNATURE_MAX_LENGTH
) {
253 ERROR(db
->ctx
, "Signature too long: %zu\n", length
);
257 DEBUG(db
->ctx
, "Reading signature of %zu bytes\n", length
);
260 *dst
= malloc(length
);
265 memcpy(*dst
, src
, length
);
270 static int loc_database_read_header_v1(struct loc_database
* db
) {
271 struct loc_database_header_v1 header
;
275 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
277 if (size
< sizeof(header
)) {
278 ERROR(db
->ctx
, "Could not read enough data for header\n");
283 db
->created_at
= be64toh(header
.created_at
);
284 db
->vendor
= be32toh(header
.vendor
);
285 db
->description
= be32toh(header
.description
);
286 db
->license
= be32toh(header
.license
);
288 db
->signature1_length
= be16toh(header
.signature1_length
);
289 db
->signature2_length
= be16toh(header
.signature2_length
);
292 if (db
->signature1_length
) {
293 r
= loc_database_read_signature(db
, &db
->signature1
,
294 header
.signature1
, db
->signature1_length
);
299 if (db
->signature2_length
) {
300 r
= loc_database_read_signature(db
, &db
->signature2
,
301 header
.signature2
, db
->signature2_length
);
307 off_t pool_offset
= be32toh(header
.pool_offset
);
308 size_t pool_length
= be32toh(header
.pool_length
);
310 r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
311 db
->f
, pool_length
, pool_offset
);
316 r
= loc_database_read_as_section_v1(db
, &header
);
321 r
= loc_database_read_network_nodes_section_v1(db
, &header
);
326 r
= loc_database_read_networks_section_v1(db
, &header
);
331 r
= loc_database_read_countries_section_v1(db
, &header
);
338 static int loc_database_read_header(struct loc_database
* db
) {
339 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
341 switch (db
->version
) {
342 case LOC_DATABASE_VERSION_1
:
343 return loc_database_read_header_v1(db
);
346 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
351 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
352 clock_t start
= clock();
356 // Clone file descriptor
359 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
363 // Reopen the file so that we can keep our own file handle
364 db
->f
= fdopen(fd
, "r");
366 ERROR(db
->ctx
, "Could not re-open database file\n");
370 // Rewind to the start of the file
374 int r
= loc_database_read_magic(db
);
379 r
= loc_database_read_header(db
);
383 clock_t end
= clock();
385 INFO(db
->ctx
, "Opened database in %.4fms\n",
386 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
391 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
392 // Fail on invalid file handle
396 struct loc_database
* db
= calloc(1, sizeof(*db
));
401 db
->ctx
= loc_ref(ctx
);
404 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
406 int r
= loc_database_read(db
, f
);
408 loc_database_unref(db
);
417 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
423 static void loc_database_free(struct loc_database
* db
) {
426 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
430 r
= munmap(db
->as_v1
, db
->as_count
* sizeof(*db
->as_v1
));
432 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
435 // Remove mapped network sections
436 if (db
->networks_v1
) {
437 r
= munmap(db
->networks_v1
, db
->networks_count
* sizeof(*db
->networks_v1
));
439 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
442 // Remove mapped network nodes section
443 if (db
->network_nodes_v1
) {
444 r
= munmap(db
->network_nodes_v1
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v1
));
446 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
449 // Remove mapped countries section
450 if (db
->countries_v1
) {
451 r
= munmap(db
->countries_v1
, db
->countries_count
* sizeof(*db
->countries_v1
));
453 ERROR(db
->ctx
, "Could not unmap countries section: %s\n", strerror(errno
));
457 loc_stringpool_unref(db
->pool
);
461 free(db
->signature1
);
463 free(db
->signature2
);
465 // Close database file
473 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
474 if (--db
->refcount
> 0)
477 loc_database_free(db
);
481 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
482 // Cannot do this when no signature is available
483 if (!db
->signature1
&& !db
->signature2
) {
484 DEBUG(db
->ctx
, "No signature available to verify\n");
488 // Start the stopwatch
489 clock_t start
= clock();
492 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
494 char* error
= ERR_error_string(ERR_get_error(), NULL
);
495 ERROR(db
->ctx
, "Could not parse public key: %s\n", error
);
502 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
504 // Initialise hash function
505 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
507 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
508 ERR_error_string(ERR_get_error(), NULL
));
514 // Reset file to start
518 struct loc_database_magic magic
;
519 fread(&magic
, 1, sizeof(magic
), db
->f
);
521 hexdump(db
->ctx
, &magic
, sizeof(magic
));
523 // Feed magic into the hash
524 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
526 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
533 struct loc_database_header_v1 header_v1
;
536 switch (db
->version
) {
537 case LOC_DATABASE_VERSION_1
:
538 bytes_read
= fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
539 if (bytes_read
< sizeof(header_v1
)) {
540 ERROR(db
->ctx
, "Could not read header\n");
547 memset(header_v1
.signature1
, '\0', sizeof(header_v1
.signature1
));
548 header_v1
.signature1_length
= 0;
549 memset(header_v1
.signature2
, '\0', sizeof(header_v1
.signature2
));
550 header_v1
.signature2_length
= 0;
552 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
554 // Feed header into the hash
555 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
557 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
565 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
571 // Walk through the file in chunks of 64kB
572 char buffer
[64 * 1024];
574 while (!feof(db
->f
)) {
575 bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
577 hexdump(db
->ctx
, buffer
, bytes_read
);
579 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
581 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
588 // Check first signature
589 if (db
->signature1
) {
590 hexdump(db
->ctx
, db
->signature1
, db
->signature1_length
);
592 r
= EVP_DigestVerifyFinal(mdctx
,
593 (unsigned char*)db
->signature1
, db
->signature1_length
);
596 DEBUG(db
->ctx
, "The first signature is invalid\n");
599 DEBUG(db
->ctx
, "The first signature is valid\n");
602 ERROR(db
->ctx
, "Error verifying the first signature: %s\n",
603 ERR_error_string(ERR_get_error(), NULL
));
608 // Check second signature only when the first one was invalid
609 if (r
&& db
->signature2
) {
610 hexdump(db
->ctx
, db
->signature2
, db
->signature2_length
);
612 r
= EVP_DigestVerifyFinal(mdctx
,
613 (unsigned char*)db
->signature2
, db
->signature2_length
);
616 DEBUG(db
->ctx
, "The second signature is invalid\n");
619 DEBUG(db
->ctx
, "The second signature is valid\n");
622 ERROR(db
->ctx
, "Error verifying the second signature: %s\n",
623 ERR_error_string(ERR_get_error(), NULL
));
628 clock_t end
= clock();
629 INFO(db
->ctx
, "Signature checked in %.4fms\n",
630 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
634 EVP_MD_CTX_free(mdctx
);
640 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
641 return db
->created_at
;
644 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
645 return loc_stringpool_get(db
->pool
, db
->vendor
);
648 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
649 return loc_stringpool_get(db
->pool
, db
->description
);
652 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
653 return loc_stringpool_get(db
->pool
, db
->license
);
656 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
660 // Returns the AS at position pos
661 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
662 if ((size_t)pos
>= db
->as_count
)
665 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
668 switch (db
->version
) {
669 case LOC_DATABASE_VERSION_1
:
670 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, db
->as_v1
+ pos
);
678 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
684 // Performs a binary search to find the AS in the list
685 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
687 off_t hi
= db
->as_count
- 1;
691 clock_t start
= clock();
695 off_t i
= (lo
+ hi
) / 2;
697 // Fetch AS in the middle between lo and hi
698 int r
= loc_database_fetch_as(db
, as
, i
);
702 // Check if this is a match
703 uint32_t as_number
= loc_as_get_number(*as
);
704 if (as_number
== number
) {
706 clock_t end
= clock();
708 // Log how fast this has been
709 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
710 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
716 // If it wasn't, we release the AS and
717 // adjust our search pointers
720 if (as_number
< number
) {
732 // Returns the network at position pos
733 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
734 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
735 if ((size_t)pos
>= db
->networks_count
) {
736 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
737 (intmax_t)pos
, (intmax_t)db
->networks_count
);
742 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
745 switch (db
->version
) {
746 case LOC_DATABASE_VERSION_1
:
747 r
= loc_network_new_from_database_v1(db
->ctx
, network
,
748 address
, prefix
, db
->networks_v1
+ pos
);
757 char* string
= loc_network_str(*network
);
758 DEBUG(db
->ctx
, "Got network %s\n", string
);
766 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
767 return (node
->network
!= htobe32(0xffffffff));
770 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
771 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
772 const struct loc_database_network_node_v1
* node
) {
773 off_t network_index
= be32toh(node
->network
);
775 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v1
), (intmax_t)network_index
);
778 int r
= loc_database_fetch_network(db
, network
,
779 network_address
, prefix
, network_index
);
781 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
785 // Check if the given IP address is inside the network
786 if (!loc_network_match_address(*network
, address
)) {
787 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
789 loc_network_unref(*network
);
794 // A network was found and the IP address matches
798 // Searches for an exact match along the path
799 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
800 struct loc_network
** network
, struct in6_addr
* network_address
,
801 const struct loc_database_network_node_v1
* node
, unsigned int level
) {
806 int bit
= in6_addr_get_bit(address
, level
);
807 in6_addr_set_bit(network_address
, level
, bit
);
810 node_index
= be32toh(node
->zero
);
812 node_index
= be32toh(node
->one
);
814 // If the node index is zero, the tree ends here
815 // and we cannot descend any further
816 if (node_index
> 0) {
818 if ((size_t)node_index
>= db
->network_nodes_count
)
821 // Move on to the next node
822 r
= __loc_database_lookup(db
, address
, network
, network_address
,
823 db
->network_nodes_v1
+ node_index
, level
+ 1);
825 // End here if a result was found
833 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
835 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
838 // If this node has a leaf, we will check if it matches
839 if (__loc_database_node_is_leaf(node
)) {
840 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
848 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
849 struct in6_addr
* address
, struct loc_network
** network
) {
850 struct in6_addr network_address
;
851 memset(&network_address
, 0, sizeof(network_address
));
857 clock_t start
= clock();
860 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
861 db
->network_nodes_v1
, 0);
864 clock_t end
= clock();
866 // Log how fast this has been
867 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
868 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
874 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
875 const char* string
, struct loc_network
** network
) {
876 struct in6_addr address
;
878 int r
= loc_parse_address(db
->ctx
, string
, &address
);
882 return loc_database_lookup(db
, &address
, network
);
885 // Returns the country at position pos
886 static int loc_database_fetch_country(struct loc_database
* db
,
887 struct loc_country
** country
, off_t pos
) {
888 if ((size_t)pos
>= db
->countries_count
)
891 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
894 switch (db
->version
) {
895 case LOC_DATABASE_VERSION_1
:
896 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, db
->countries_v1
+ pos
);
904 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
910 // Performs a binary search to find the country in the list
911 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
912 struct loc_country
** country
, const char* code
) {
914 off_t hi
= db
->countries_count
- 1;
918 clock_t start
= clock();
922 off_t i
= (lo
+ hi
) / 2;
924 // Fetch country in the middle between lo and hi
925 int r
= loc_database_fetch_country(db
, country
, i
);
929 // Check if this is a match
930 const char* cc
= loc_country_get_code(*country
);
931 int result
= strcmp(code
, cc
);
935 clock_t end
= clock();
937 // Log how fast this has been
938 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
939 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
945 // If it wasn't, we release the country and
946 // adjust our search pointers
947 loc_country_unref(*country
);
963 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
964 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
966 // Release all references
967 loc_database_unref(enumerator
->db
);
968 loc_unref(enumerator
->ctx
);
970 if (enumerator
->string
)
971 free(enumerator
->string
);
973 if (enumerator
->countries
)
974 loc_country_list_unref(enumerator
->countries
);
976 if (enumerator
->asns
)
977 loc_as_list_unref(enumerator
->asns
);
979 // Free network search
980 free(enumerator
->networks_visited
);
983 if (enumerator
->stack
)
984 loc_network_list_unref(enumerator
->stack
);
989 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
990 struct loc_database
* db
, enum loc_database_enumerator_mode mode
, int flags
) {
991 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
996 e
->ctx
= loc_ref(db
->ctx
);
997 e
->db
= loc_database_ref(db
);
1002 e
->flatten
= (flags
& LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
1004 // Initialise graph search
1005 e
->network_stack_depth
= 1;
1006 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
1009 int r
= loc_network_list_new(e
->ctx
, &e
->stack
);
1011 loc_database_enumerator_free(e
);
1015 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
1021 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
1022 enumerator
->refcount
++;
1027 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
1031 if (--enumerator
->refcount
> 0)
1034 loc_database_enumerator_free(enumerator
);
1038 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
1039 enumerator
->string
= strdup(string
);
1041 // Make the string lowercase
1042 for (char *p
= enumerator
->string
; *p
; p
++)
1048 LOC_EXPORT
struct loc_country_list
* loc_database_enumerator_get_countries(
1049 struct loc_database_enumerator
* enumerator
) {
1050 if (!enumerator
->countries
)
1053 return loc_country_list_ref(enumerator
->countries
);
1056 LOC_EXPORT
int loc_database_enumerator_set_countries(
1057 struct loc_database_enumerator
* enumerator
, struct loc_country_list
* countries
) {
1058 if (enumerator
->countries
)
1059 loc_country_list_unref(enumerator
->countries
);
1061 enumerator
->countries
= loc_country_list_ref(countries
);
1066 LOC_EXPORT
struct loc_as_list
* loc_database_enumerator_get_asns(
1067 struct loc_database_enumerator
* enumerator
) {
1068 if (!enumerator
->asns
)
1071 return loc_as_list_ref(enumerator
->asns
);
1074 LOC_EXPORT
int loc_database_enumerator_set_asns(
1075 struct loc_database_enumerator
* enumerator
, struct loc_as_list
* asns
) {
1076 if (enumerator
->asns
)
1077 loc_as_list_unref(enumerator
->asns
);
1079 enumerator
->asns
= loc_as_list_ref(asns
);
1084 LOC_EXPORT
int loc_database_enumerator_set_flag(
1085 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
1086 enumerator
->flags
|= flag
;
1091 LOC_EXPORT
int loc_database_enumerator_set_family(
1092 struct loc_database_enumerator
* enumerator
, int family
) {
1093 enumerator
->family
= family
;
1098 LOC_EXPORT
int loc_database_enumerator_next_as(
1099 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
1102 // Do not do anything if not in AS mode
1103 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
1106 struct loc_database
* db
= enumerator
->db
;
1108 while (enumerator
->as_index
< db
->as_count
) {
1109 // Fetch the next AS
1110 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1114 r
= loc_as_match_string(*as
, enumerator
->string
);
1116 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1117 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1128 enumerator
->as_index
= 0;
1130 // We have searched through all of them
1134 static int loc_database_enumerator_stack_push_node(
1135 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1136 // Do not add empty nodes
1140 // Check if there is any space left on the stack
1141 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1142 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1146 // Increase stack size
1147 int s
= ++e
->network_stack_depth
;
1149 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1151 e
->network_stack
[s
].offset
= offset
;
1152 e
->network_stack
[s
].i
= i
;
1153 e
->network_stack
[s
].depth
= depth
;
1158 static int loc_database_enumerator_filter_network(
1159 struct loc_database_enumerator
* enumerator
, struct loc_network
* network
) {
1160 // Skip if the family does not match
1161 if (enumerator
->family
&& loc_network_address_family(network
) != enumerator
->family
) {
1162 DEBUG(enumerator
->ctx
, "Filtered network %p because of family not matching\n", network
);
1166 // Skip if the country code does not match
1167 if (enumerator
->countries
&& !loc_country_list_empty(enumerator
->countries
)) {
1168 const char* country_code
= loc_network_get_country_code(network
);
1170 if (!loc_country_list_contains_code(enumerator
->countries
, country_code
)) {
1171 DEBUG(enumerator
->ctx
, "Filtered network %p because of country code not matching\n", network
);
1176 // Skip if the ASN does not match
1177 if (enumerator
->asns
&& !loc_as_list_empty(enumerator
->asns
)) {
1178 uint32_t asn
= loc_network_get_asn(network
);
1180 if (!loc_as_list_contains_number(enumerator
->asns
, asn
)) {
1181 DEBUG(enumerator
->ctx
, "Filtered network %p because of ASN not matching\n", network
);
1186 // Skip if flags do not match
1187 if (enumerator
->flags
&& !loc_network_match_flag(network
, enumerator
->flags
)) {
1188 DEBUG(enumerator
->ctx
, "Filtered network %p because of flags not matching\n", network
);
1196 static int __loc_database_enumerator_next_network(
1197 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
, int filter
) {
1198 // Return top element from the stack
1200 *network
= loc_network_list_pop_first(enumerator
->stack
);
1206 // Throw away any networks by filter
1207 if (filter
&& loc_database_enumerator_filter_network(enumerator
, *network
)) {
1208 loc_network_unref(*network
);
1217 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1218 enumerator
->network_stack_depth
);
1221 while (enumerator
->network_stack_depth
> 0) {
1222 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1224 // Get object from top of the stack
1225 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1227 // Remove the node from the stack if we have already visited it
1228 if (enumerator
->networks_visited
[node
->offset
]) {
1229 enumerator
->network_stack_depth
--;
1233 // Mark the bits on the path correctly
1234 in6_addr_set_bit(&enumerator
->network_address
,
1235 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1237 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1238 enumerator
->networks_visited
[node
->offset
]++;
1240 // Pop node from top of the stack
1241 struct loc_database_network_node_v1
* n
=
1242 enumerator
->db
->network_nodes_v1
+ node
->offset
;
1244 // Add edges to stack
1245 int r
= loc_database_enumerator_stack_push_node(enumerator
,
1246 be32toh(n
->one
), 1, node
->depth
+ 1);
1251 r
= loc_database_enumerator_stack_push_node(enumerator
,
1252 be32toh(n
->zero
), 0, node
->depth
+ 1);
1257 // Check if this node is a leaf and has a network object
1258 if (__loc_database_node_is_leaf(n
)) {
1259 off_t network_index
= be32toh(n
->network
);
1261 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1263 // Fetch the network object
1264 r
= loc_database_fetch_network(enumerator
->db
, network
,
1265 &enumerator
->network_address
, node
->depth
, network_index
);
1267 // Break on any errors
1271 // Return all networks when the filter is disabled
1275 // Check if we are interested in this network
1276 if (loc_database_enumerator_filter_network(enumerator
, *network
)) {
1277 loc_network_unref(*network
);
1287 // Reached the end of the search
1291 static int __loc_database_enumerator_next_network_flattened(
1292 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1293 // Fetch the next network
1294 int r
= __loc_database_enumerator_next_network(enumerator
, network
, 1);
1298 // End if we could not read another network
1302 struct loc_network
* subnet
= NULL
;
1303 struct loc_network_list
* subnets
;
1305 // Create a list with all subnets
1306 r
= loc_network_list_new(enumerator
->ctx
, &subnets
);
1310 // Search all subnets from the database
1312 // Fetch the next network in line
1313 r
= __loc_database_enumerator_next_network(enumerator
, &subnet
, 0);
1315 loc_network_unref(subnet
);
1316 loc_network_list_unref(subnets
);
1321 // End if we did not receive another subnet
1325 // Collect all subnets in a list
1326 if (loc_network_is_subnet(*network
, subnet
)) {
1327 r
= loc_network_list_push(subnets
, subnet
);
1329 loc_network_unref(subnet
);
1330 loc_network_list_unref(subnets
);
1335 loc_network_unref(subnet
);
1339 // If this is not a subnet, we push it back onto the stack and break
1340 r
= loc_network_list_push(enumerator
->stack
, subnet
);
1342 loc_network_unref(subnet
);
1343 loc_network_list_unref(subnets
);
1348 loc_network_unref(subnet
);
1352 DEBUG(enumerator
->ctx
, "Found %zu subnet(s)\n", loc_network_list_size(subnets
));
1354 // We can abort here if the network has no subnets
1355 if (loc_network_list_empty(subnets
)) {
1356 loc_network_list_unref(subnets
);
1361 // If the network has any subnets, we will break it into smaller parts
1362 // without the subnets.
1363 struct loc_network_list
* excluded
= loc_network_exclude_list(*network
, subnets
);
1365 loc_network_list_unref(subnets
);
1369 // Merge subnets onto the stack
1370 r
= loc_network_list_merge(enumerator
->stack
, subnets
);
1372 loc_network_list_unref(subnets
);
1373 loc_network_list_unref(excluded
);
1378 // Push excluded list onto the stack
1379 r
= loc_network_list_merge(enumerator
->stack
, excluded
);
1381 loc_network_list_unref(subnets
);
1382 loc_network_list_unref(excluded
);
1387 loc_network_list_unref(subnets
);
1388 loc_network_list_unref(excluded
);
1390 // Drop the network and restart the whole process again to pick the next network
1391 loc_network_unref(*network
);
1393 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1396 LOC_EXPORT
int loc_database_enumerator_next_network(
1397 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1398 // Do not do anything if not in network mode
1399 if (enumerator
->mode
!= LOC_DB_ENUMERATE_NETWORKS
)
1403 if (enumerator
->flatten
)
1404 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1406 return __loc_database_enumerator_next_network(enumerator
, network
, 1);
1409 LOC_EXPORT
int loc_database_enumerator_next_country(
1410 struct loc_database_enumerator
* enumerator
, struct loc_country
** country
) {
1413 // Do not do anything if not in country mode
1414 if (enumerator
->mode
!= LOC_DB_ENUMERATE_COUNTRIES
)
1417 struct loc_database
* db
= enumerator
->db
;
1419 while (enumerator
->country_index
< db
->countries_count
) {
1420 // Fetch the next country
1421 int r
= loc_database_fetch_country(db
, country
, enumerator
->country_index
++);
1425 // We do not filter here, so it always is a match
1430 enumerator
->country_index
= 0;
1432 // We have searched through all of them