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>
55 enum loc_database_version version
;
63 size_t signature1_length
;
65 size_t signature2_length
;
67 // ASes in the database
68 struct loc_database_as_v1
* as_v1
;
72 struct loc_database_network_node_v1
* network_nodes_v1
;
73 size_t network_nodes_count
;
76 struct loc_database_network_v1
* networks_v1
;
77 size_t networks_count
;
80 struct loc_database_country_v1
* countries_v1
;
81 size_t countries_count
;
83 struct loc_stringpool
* pool
;
86 #define MAX_STACK_DEPTH 256
88 struct loc_node_stack
{
90 int i
; // Is this node 0 or 1?
94 struct loc_database_enumerator
{
96 struct loc_database
* db
;
97 enum loc_database_enumerator_mode mode
;
102 char country_code
[3];
104 enum loc_network_flags flags
;
110 // Index of the AS we are looking at
111 unsigned int as_index
;
113 // Index of the country we are looking at
114 unsigned int country_index
;
117 struct in6_addr network_address
;
118 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
119 int network_stack_depth
;
120 unsigned int* networks_visited
;
123 struct loc_network_list
* stack
;
126 static int loc_database_read_magic(struct loc_database
* db
) {
127 struct loc_database_magic magic
;
130 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
132 // Check if we have been able to read enough data
133 if (bytes_read
< sizeof(magic
)) {
134 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
135 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
139 // Compare magic bytes
140 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
141 DEBUG(db
->ctx
, "Magic value matches\n");
144 db
->version
= magic
.version
;
149 ERROR(db
->ctx
, "Unrecognized file type\n");
155 static int loc_database_read_as_section_v1(struct loc_database
* db
,
156 const struct loc_database_header_v1
* header
) {
157 off_t as_offset
= be32toh(header
->as_offset
);
158 size_t as_length
= be32toh(header
->as_length
);
160 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
163 db
->as_v1
= mmap(NULL
, as_length
, PROT_READ
,
164 MAP_SHARED
, fileno(db
->f
), as_offset
);
166 if (db
->as_v1
== MAP_FAILED
)
170 db
->as_count
= as_length
/ sizeof(*db
->as_v1
);
172 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
177 static int loc_database_read_network_nodes_section_v1(struct loc_database
* db
,
178 const struct loc_database_header_v1
* header
) {
179 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
180 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
182 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
183 (intmax_t)network_nodes_offset
, network_nodes_length
);
185 if (network_nodes_length
> 0) {
186 db
->network_nodes_v1
= mmap(NULL
, network_nodes_length
, PROT_READ
,
187 MAP_SHARED
, fileno(db
->f
), network_nodes_offset
);
189 if (db
->network_nodes_v1
== MAP_FAILED
)
193 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v1
);
195 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
200 static int loc_database_read_networks_section_v1(struct loc_database
* db
,
201 const struct loc_database_header_v1
* header
) {
202 off_t networks_offset
= be32toh(header
->network_data_offset
);
203 size_t networks_length
= be32toh(header
->network_data_length
);
205 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
206 (intmax_t)networks_offset
, networks_length
);
208 if (networks_length
> 0) {
209 db
->networks_v1
= mmap(NULL
, networks_length
, PROT_READ
,
210 MAP_SHARED
, fileno(db
->f
), networks_offset
);
212 if (db
->networks_v1
== MAP_FAILED
)
216 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v1
);
218 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
223 static int loc_database_read_countries_section_v1(struct loc_database
* db
,
224 const struct loc_database_header_v1
* header
) {
225 off_t countries_offset
= be32toh(header
->countries_offset
);
226 size_t countries_length
= be32toh(header
->countries_length
);
228 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
229 (intmax_t)countries_offset
, countries_length
);
231 if (countries_length
> 0) {
232 db
->countries_v1
= mmap(NULL
, countries_length
, PROT_READ
,
233 MAP_SHARED
, fileno(db
->f
), countries_offset
);
235 if (db
->countries_v1
== MAP_FAILED
)
239 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v1
);
241 INFO(db
->ctx
, "Read %zu countries from the database\n",
242 db
->countries_count
);
247 static int loc_database_read_signature(struct loc_database
* db
,
248 char** dst
, char* src
, size_t length
) {
249 // Check for a plausible signature length
250 if (length
> LOC_SIGNATURE_MAX_LENGTH
) {
251 ERROR(db
->ctx
, "Signature too long: %ld\n", length
);
255 DEBUG(db
->ctx
, "Reading signature of %ld bytes\n", length
);
258 *dst
= malloc(length
);
263 memcpy(*dst
, src
, length
);
268 static int loc_database_read_header_v1(struct loc_database
* db
) {
269 struct loc_database_header_v1 header
;
273 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
275 if (size
< sizeof(header
)) {
276 ERROR(db
->ctx
, "Could not read enough data for header\n");
281 db
->created_at
= be64toh(header
.created_at
);
282 db
->vendor
= be32toh(header
.vendor
);
283 db
->description
= be32toh(header
.description
);
284 db
->license
= be32toh(header
.license
);
286 db
->signature1_length
= be16toh(header
.signature1_length
);
287 db
->signature2_length
= be16toh(header
.signature2_length
);
290 if (db
->signature1_length
) {
291 r
= loc_database_read_signature(db
, &db
->signature1
,
292 header
.signature1
, db
->signature1_length
);
297 if (db
->signature2_length
) {
298 r
= loc_database_read_signature(db
, &db
->signature2
,
299 header
.signature2
, db
->signature2_length
);
305 off_t pool_offset
= be32toh(header
.pool_offset
);
306 size_t pool_length
= be32toh(header
.pool_length
);
308 r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
309 db
->f
, pool_length
, pool_offset
);
314 r
= loc_database_read_as_section_v1(db
, &header
);
319 r
= loc_database_read_network_nodes_section_v1(db
, &header
);
324 r
= loc_database_read_networks_section_v1(db
, &header
);
329 r
= loc_database_read_countries_section_v1(db
, &header
);
336 static int loc_database_read_header(struct loc_database
* db
) {
337 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
339 switch (db
->version
) {
340 case LOC_DATABASE_VERSION_1
:
341 return loc_database_read_header_v1(db
);
344 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
349 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
350 clock_t start
= clock();
354 // Clone file descriptor
357 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
361 // Reopen the file so that we can keep our own file handle
362 db
->f
= fdopen(fd
, "r");
364 ERROR(db
->ctx
, "Could not re-open database file\n");
368 // Rewind to the start of the file
372 int r
= loc_database_read_magic(db
);
377 r
= loc_database_read_header(db
);
381 clock_t end
= clock();
383 INFO(db
->ctx
, "Opened database in %.4fms\n",
384 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
389 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
390 // Fail on invalid file handle
394 struct loc_database
* db
= calloc(1, sizeof(*db
));
399 db
->ctx
= loc_ref(ctx
);
402 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
404 int r
= loc_database_read(db
, f
);
406 loc_database_unref(db
);
415 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
421 static void loc_database_free(struct loc_database
* db
) {
424 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
428 r
= munmap(db
->as_v1
, db
->as_count
* sizeof(*db
->as_v1
));
430 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
433 // Remove mapped network sections
434 if (db
->networks_v1
) {
435 r
= munmap(db
->networks_v1
, db
->networks_count
* sizeof(*db
->networks_v1
));
437 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
440 // Remove mapped network nodes section
441 if (db
->network_nodes_v1
) {
442 r
= munmap(db
->network_nodes_v1
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v1
));
444 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
448 loc_stringpool_unref(db
->pool
);
452 free(db
->signature1
);
454 free(db
->signature2
);
456 // Close database file
464 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
465 if (--db
->refcount
> 0)
468 loc_database_free(db
);
472 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
473 // Cannot do this when no signature is available
474 if (!db
->signature1
&& !db
->signature2
) {
475 DEBUG(db
->ctx
, "No signature available to verify\n");
479 // Start the stopwatch
480 clock_t start
= clock();
483 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
485 char* error
= ERR_error_string(ERR_get_error(), NULL
);
486 ERROR(db
->ctx
, "Could not parse public key: %s\n", error
);
493 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
495 // Initialise hash function
496 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
498 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
499 ERR_error_string(ERR_get_error(), NULL
));
505 // Reset file to start
509 struct loc_database_magic magic
;
510 fread(&magic
, 1, sizeof(magic
), db
->f
);
512 hexdump(db
->ctx
, &magic
, sizeof(magic
));
514 // Feed magic into the hash
515 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
517 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
524 struct loc_database_header_v1 header_v1
;
527 switch (db
->version
) {
528 case LOC_DATABASE_VERSION_1
:
529 bytes_read
= fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
530 if (bytes_read
< sizeof(header_v1
)) {
531 ERROR(db
->ctx
, "Could not read header\n");
538 memset(header_v1
.signature1
, '\0', sizeof(header_v1
.signature1
));
539 header_v1
.signature1_length
= 0;
540 memset(header_v1
.signature2
, '\0', sizeof(header_v1
.signature2
));
541 header_v1
.signature2_length
= 0;
543 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
545 // Feed header into the hash
546 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
548 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
556 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
562 // Walk through the file in chunks of 64kB
563 char buffer
[64 * 1024];
565 while (!feof(db
->f
)) {
566 bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
568 hexdump(db
->ctx
, buffer
, bytes_read
);
570 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
572 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
579 // Check first signature
580 if (db
->signature1
) {
581 hexdump(db
->ctx
, db
->signature1
, db
->signature1_length
);
583 r
= EVP_DigestVerifyFinal(mdctx
,
584 (unsigned char*)db
->signature1
, db
->signature1_length
);
587 DEBUG(db
->ctx
, "The first signature is invalid\n");
590 DEBUG(db
->ctx
, "The first signature is valid\n");
593 ERROR(db
->ctx
, "Error verifying the first signature: %s\n",
594 ERR_error_string(ERR_get_error(), NULL
));
599 // Check second signature only when the first one was invalid
600 if (r
&& db
->signature2
) {
601 hexdump(db
->ctx
, db
->signature2
, db
->signature2_length
);
603 r
= EVP_DigestVerifyFinal(mdctx
,
604 (unsigned char*)db
->signature2
, db
->signature2_length
);
607 DEBUG(db
->ctx
, "The second signature is invalid\n");
610 DEBUG(db
->ctx
, "The second signature is valid\n");
613 ERROR(db
->ctx
, "Error verifying the second signature: %s\n",
614 ERR_error_string(ERR_get_error(), NULL
));
619 clock_t end
= clock();
620 DEBUG(db
->ctx
, "Signature checked in %.4fms\n",
621 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
625 EVP_MD_CTX_free(mdctx
);
631 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
632 return db
->created_at
;
635 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
636 return loc_stringpool_get(db
->pool
, db
->vendor
);
639 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
640 return loc_stringpool_get(db
->pool
, db
->description
);
643 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
644 return loc_stringpool_get(db
->pool
, db
->license
);
647 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
651 // Returns the AS at position pos
652 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
653 if ((size_t)pos
>= db
->as_count
)
656 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
659 switch (db
->version
) {
660 case LOC_DATABASE_VERSION_1
:
661 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, db
->as_v1
+ pos
);
669 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
675 // Performs a binary search to find the AS in the list
676 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
678 off_t hi
= db
->as_count
- 1;
681 clock_t start
= clock();
684 off_t i
= (lo
+ hi
) / 2;
686 // Fetch AS in the middle between lo and hi
687 int r
= loc_database_fetch_as(db
, as
, i
);
691 // Check if this is a match
692 uint32_t as_number
= loc_as_get_number(*as
);
693 if (as_number
== number
) {
694 clock_t end
= clock();
696 // Log how fast this has been
697 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
698 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
703 // If it wasn't, we release the AS and
704 // adjust our search pointers
707 if (as_number
< number
) {
719 // Returns the network at position pos
720 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
721 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
722 if ((size_t)pos
>= db
->networks_count
) {
723 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
724 (intmax_t)pos
, (intmax_t)db
->networks_count
);
729 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
732 switch (db
->version
) {
733 case LOC_DATABASE_VERSION_1
:
734 r
= loc_network_new_from_database_v1(db
->ctx
, network
,
735 address
, prefix
, db
->networks_v1
+ pos
);
743 char* string
= loc_network_str(*network
);
744 DEBUG(db
->ctx
, "Got network %s\n", string
);
751 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
752 return (node
->network
!= htobe32(0xffffffff));
755 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
756 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
757 const struct loc_database_network_node_v1
* node
) {
758 off_t network_index
= be32toh(node
->network
);
760 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v1
), (intmax_t)network_index
);
763 int r
= loc_database_fetch_network(db
, network
,
764 network_address
, prefix
, network_index
);
766 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
770 // Check if the given IP address is inside the network
771 r
= loc_network_match_address(*network
, address
);
773 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
775 loc_network_unref(*network
);
780 // A network was found and the IP address matches
784 // Searches for an exact match along the path
785 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
786 struct loc_network
** network
, struct in6_addr
* network_address
,
787 const struct loc_database_network_node_v1
* node
, unsigned int level
) {
792 int bit
= in6_addr_get_bit(address
, level
);
793 in6_addr_set_bit(network_address
, level
, bit
);
796 node_index
= be32toh(node
->zero
);
798 node_index
= be32toh(node
->one
);
800 // If the node index is zero, the tree ends here
801 // and we cannot descend any further
802 if (node_index
> 0) {
804 if ((size_t)node_index
>= db
->network_nodes_count
)
807 // Move on to the next node
808 r
= __loc_database_lookup(db
, address
, network
, network_address
,
809 db
->network_nodes_v1
+ node_index
, level
+ 1);
811 // End here if a result was found
819 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
821 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
824 // If this node has a leaf, we will check if it matches
825 if (__loc_database_node_is_leaf(node
)) {
826 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
834 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
835 struct in6_addr
* address
, struct loc_network
** network
) {
836 struct in6_addr network_address
;
837 memset(&network_address
, 0, sizeof(network_address
));
842 clock_t start
= clock();
844 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
845 db
->network_nodes_v1
, 0);
847 clock_t end
= clock();
849 // Log how fast this has been
850 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
851 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
856 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
857 const char* string
, struct loc_network
** network
) {
858 struct in6_addr address
;
860 int r
= loc_parse_address(db
->ctx
, string
, &address
);
864 return loc_database_lookup(db
, &address
, network
);
867 // Returns the country at position pos
868 static int loc_database_fetch_country(struct loc_database
* db
,
869 struct loc_country
** country
, off_t pos
) {
870 if ((size_t)pos
>= db
->countries_count
)
873 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
876 switch (db
->version
) {
877 case LOC_DATABASE_VERSION_1
:
878 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, db
->countries_v1
+ pos
);
886 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
892 // Performs a binary search to find the country in the list
893 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
894 struct loc_country
** country
, const char* code
) {
896 off_t hi
= db
->countries_count
- 1;
899 clock_t start
= clock();
902 off_t i
= (lo
+ hi
) / 2;
904 // Fetch country in the middle between lo and hi
905 int r
= loc_database_fetch_country(db
, country
, i
);
909 // Check if this is a match
910 const char* cc
= loc_country_get_code(*country
);
911 int result
= strcmp(code
, cc
);
914 clock_t end
= clock();
916 // Log how fast this has been
917 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
918 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
923 // If it wasn't, we release the country and
924 // adjust our search pointers
925 loc_country_unref(*country
);
941 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
942 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
944 // Release all references
945 loc_database_unref(enumerator
->db
);
946 loc_unref(enumerator
->ctx
);
948 if (enumerator
->string
)
949 free(enumerator
->string
);
951 // Free network search
952 free(enumerator
->networks_visited
);
955 if (enumerator
->stack
)
956 loc_network_list_unref(enumerator
->stack
);
961 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
962 struct loc_database
* db
, enum loc_database_enumerator_mode mode
, int flags
) {
963 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
968 e
->ctx
= loc_ref(db
->ctx
);
969 e
->db
= loc_database_ref(db
);
974 e
->flatten
= (flags
& LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
976 // Initialise graph search
977 e
->network_stack_depth
= 1;
978 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
981 int r
= loc_network_list_new(e
->ctx
, &e
->stack
);
983 loc_database_enumerator_free(e
);
987 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
993 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
994 enumerator
->refcount
++;
999 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
1003 if (--enumerator
->refcount
> 0)
1006 loc_database_enumerator_free(enumerator
);
1010 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
1011 enumerator
->string
= strdup(string
);
1013 // Make the string lowercase
1014 for (char *p
= enumerator
->string
; *p
; p
++)
1020 LOC_EXPORT
int loc_database_enumerator_set_country_code(struct loc_database_enumerator
* enumerator
, const char* country_code
) {
1021 // Set empty country code
1022 if (!country_code
|| !*country_code
) {
1023 *enumerator
->country_code
= '\0';
1027 // Treat A1, A2, A3 as special country codes,
1028 // but perform search for flags instead
1029 if (strcmp(country_code
, "A1") == 0) {
1030 return loc_database_enumerator_set_flag(enumerator
,
1031 LOC_NETWORK_FLAG_ANONYMOUS_PROXY
);
1032 } else if (strcmp(country_code
, "A2") == 0) {
1033 return loc_database_enumerator_set_flag(enumerator
,
1034 LOC_NETWORK_FLAG_SATELLITE_PROVIDER
);
1035 } else if (strcmp(country_code
, "A3") == 0) {
1036 return loc_database_enumerator_set_flag(enumerator
,
1037 LOC_NETWORK_FLAG_ANYCAST
);
1040 // Country codes must be two characters
1041 if (!loc_country_code_is_valid(country_code
))
1044 for (unsigned int i
= 0; i
< 3; i
++) {
1045 enumerator
->country_code
[i
] = country_code
[i
];
1051 LOC_EXPORT
int loc_database_enumerator_set_asn(
1052 struct loc_database_enumerator
* enumerator
, unsigned int asn
) {
1053 enumerator
->asn
= asn
;
1058 LOC_EXPORT
int loc_database_enumerator_set_flag(
1059 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
1060 enumerator
->flags
|= flag
;
1065 LOC_EXPORT
int loc_database_enumerator_set_family(
1066 struct loc_database_enumerator
* enumerator
, int family
) {
1067 enumerator
->family
= family
;
1072 LOC_EXPORT
int loc_database_enumerator_next_as(
1073 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
1076 // Do not do anything if not in AS mode
1077 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
1080 struct loc_database
* db
= enumerator
->db
;
1082 while (enumerator
->as_index
< db
->as_count
) {
1083 // Fetch the next AS
1084 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1088 r
= loc_as_match_string(*as
, enumerator
->string
);
1090 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1091 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1102 enumerator
->as_index
= 0;
1104 // We have searched through all of them
1108 static int loc_database_enumerator_stack_push_node(
1109 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1110 // Do not add empty nodes
1114 // Check if there is any space left on the stack
1115 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1116 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1120 // Increase stack size
1121 int s
= ++e
->network_stack_depth
;
1123 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1125 e
->network_stack
[s
].offset
= offset
;
1126 e
->network_stack
[s
].i
= i
;
1127 e
->network_stack
[s
].depth
= depth
;
1132 static int loc_database_enumerator_filter_network(
1133 struct loc_database_enumerator
* enumerator
, struct loc_network
* network
) {
1134 // Skip if the family does not match
1135 if (enumerator
->family
&& loc_network_address_family(network
) != enumerator
->family
)
1138 // Skip if the country code does not match
1139 if (*enumerator
->country_code
&&
1140 !loc_network_match_country_code(network
, enumerator
->country_code
))
1143 // Skip if the ASN does not match
1144 if (enumerator
->asn
&&
1145 !loc_network_match_asn(network
, enumerator
->asn
))
1148 // Skip if flags do not match
1149 if (enumerator
->flags
&&
1150 !loc_network_match_flag(network
, enumerator
->flags
))
1157 static int __loc_database_enumerator_next_network(
1158 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
, int filter
) {
1159 // Return top element from the stack
1161 *network
= loc_network_list_pop(enumerator
->stack
);
1167 // Throw away any networks by filter
1168 if (filter
&& loc_database_enumerator_filter_network(enumerator
, *network
)) {
1169 loc_network_unref(*network
);
1178 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1179 enumerator
->network_stack_depth
);
1182 while (enumerator
->network_stack_depth
> 0) {
1183 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1185 // Get object from top of the stack
1186 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1188 // Remove the node from the stack if we have already visited it
1189 if (enumerator
->networks_visited
[node
->offset
]) {
1190 enumerator
->network_stack_depth
--;
1194 // Mark the bits on the path correctly
1195 in6_addr_set_bit(&enumerator
->network_address
,
1196 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1198 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1199 enumerator
->networks_visited
[node
->offset
]++;
1201 // Pop node from top of the stack
1202 struct loc_database_network_node_v1
* n
=
1203 enumerator
->db
->network_nodes_v1
+ node
->offset
;
1205 // Add edges to stack
1206 int r
= loc_database_enumerator_stack_push_node(enumerator
,
1207 be32toh(n
->one
), 1, node
->depth
+ 1);
1212 r
= loc_database_enumerator_stack_push_node(enumerator
,
1213 be32toh(n
->zero
), 0, node
->depth
+ 1);
1218 // Check if this node is a leaf and has a network object
1219 if (__loc_database_node_is_leaf(n
)) {
1220 off_t network_index
= be32toh(n
->network
);
1222 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1224 // Fetch the network object
1225 r
= loc_database_fetch_network(enumerator
->db
, network
,
1226 &enumerator
->network_address
, node
->depth
, network_index
);
1228 // Break on any errors
1232 // Return all networks when the filter is disabled
1236 // Check if we are interested in this network
1237 if (loc_database_enumerator_filter_network(enumerator
, *network
)) {
1238 loc_network_unref(*network
);
1248 // Reached the end of the search
1252 static int __loc_database_enumerator_next_network_flattened(
1253 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1254 // Fetch the next network
1255 int r
= __loc_database_enumerator_next_network(enumerator
, network
, 1);
1259 // End if we could not read another network
1263 struct loc_network
* subnet
= NULL
;
1264 struct loc_network_list
* subnets
;
1266 // Create a list with all subnets
1267 r
= loc_network_list_new(enumerator
->ctx
, &subnets
);
1271 // Search all subnets from the database
1273 // Fetch the next network in line
1274 r
= __loc_database_enumerator_next_network(enumerator
, &subnet
, 0);
1278 // End if we did not receive another subnet
1282 // Collect all subnets in a list
1283 if (loc_network_is_subnet(*network
, subnet
)) {
1284 r
= loc_network_list_push(subnets
, subnet
);
1288 loc_network_unref(subnet
);
1292 // If this is not a subnet, we push it back onto the stack and break
1293 r
= loc_network_list_push(enumerator
->stack
, subnet
);
1297 loc_network_unref(subnet
);
1301 DEBUG(enumerator
->ctx
, "Found %zu subnet(s)\n", loc_network_list_size(subnets
));
1303 // We can abort here if the network has no subnets
1304 if (loc_network_list_empty(subnets
)) {
1305 loc_network_list_unref(subnets
);
1310 // If the network has any subnets, we will break it into smaller parts
1311 // without the subnets.
1312 struct loc_network_list
* excluded
= loc_network_exclude_list(*network
, subnets
);
1313 if (!excluded
|| loc_network_list_empty(excluded
)) {
1319 loc_network_list_reverse(excluded
);
1321 // Replace network with the first one
1322 loc_network_unref(*network
);
1324 *network
= loc_network_list_pop(excluded
);
1326 // Push the rest onto the stack
1327 loc_network_list_merge(enumerator
->stack
, excluded
);
1329 loc_network_list_unref(excluded
);
1333 loc_network_unref(subnet
);
1335 loc_network_list_unref(subnets
);
1340 LOC_EXPORT
int loc_database_enumerator_next_network(
1341 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1342 // Do not do anything if not in network mode
1343 if (enumerator
->mode
!= LOC_DB_ENUMERATE_NETWORKS
)
1347 if (enumerator
->flatten
)
1348 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1350 return __loc_database_enumerator_next_network(enumerator
, network
, 1);
1353 LOC_EXPORT
int loc_database_enumerator_next_country(
1354 struct loc_database_enumerator
* enumerator
, struct loc_country
** country
) {
1357 // Do not do anything if not in country mode
1358 if (enumerator
->mode
!= LOC_DB_ENUMERATE_COUNTRIES
)
1361 struct loc_database
* db
= enumerator
->db
;
1363 while (enumerator
->country_index
< db
->countries_count
) {
1364 // Fetch the next country
1365 int r
= loc_database_fetch_country(db
, country
, enumerator
->country_index
++);
1369 // We do not filter here, so it always is a match
1374 enumerator
->country_index
= 0;
1376 // We have searched through all of them