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/country-list.h>
44 #include <loc/database.h>
45 #include <loc/format.h>
46 #include <loc/network.h>
47 #include <loc/private.h>
48 #include <loc/stringpool.h>
56 enum loc_database_version version
;
64 size_t signature1_length
;
66 size_t signature2_length
;
68 // ASes in the database
69 struct loc_database_as_v1
* as_v1
;
73 struct loc_database_network_node_v1
* network_nodes_v1
;
74 size_t network_nodes_count
;
77 struct loc_database_network_v1
* networks_v1
;
78 size_t networks_count
;
81 struct loc_database_country_v1
* countries_v1
;
82 size_t countries_count
;
84 struct loc_stringpool
* pool
;
87 #define MAX_STACK_DEPTH 256
89 struct loc_node_stack
{
91 int i
; // Is this node 0 or 1?
95 struct loc_database_enumerator
{
97 struct loc_database
* db
;
98 enum loc_database_enumerator_mode mode
;
103 struct loc_country_list
* countries
;
105 enum loc_network_flags flags
;
111 // Index of the AS we are looking at
112 unsigned int as_index
;
114 // Index of the country we are looking at
115 unsigned int country_index
;
118 struct in6_addr network_address
;
119 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
120 int network_stack_depth
;
121 unsigned int* networks_visited
;
124 struct loc_network_list
* stack
;
127 static int loc_database_read_magic(struct loc_database
* db
) {
128 struct loc_database_magic magic
;
131 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
133 // Check if we have been able to read enough data
134 if (bytes_read
< sizeof(magic
)) {
135 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
136 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
140 // Compare magic bytes
141 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
142 DEBUG(db
->ctx
, "Magic value matches\n");
145 db
->version
= magic
.version
;
150 ERROR(db
->ctx
, "Unrecognized file type\n");
156 static int loc_database_read_as_section_v1(struct loc_database
* db
,
157 const struct loc_database_header_v1
* header
) {
158 off_t as_offset
= be32toh(header
->as_offset
);
159 size_t as_length
= be32toh(header
->as_length
);
161 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
164 db
->as_v1
= mmap(NULL
, as_length
, PROT_READ
,
165 MAP_SHARED
, fileno(db
->f
), as_offset
);
167 if (db
->as_v1
== MAP_FAILED
)
171 db
->as_count
= as_length
/ sizeof(*db
->as_v1
);
173 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
178 static int loc_database_read_network_nodes_section_v1(struct loc_database
* db
,
179 const struct loc_database_header_v1
* header
) {
180 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
181 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
183 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
184 (intmax_t)network_nodes_offset
, network_nodes_length
);
186 if (network_nodes_length
> 0) {
187 db
->network_nodes_v1
= mmap(NULL
, network_nodes_length
, PROT_READ
,
188 MAP_SHARED
, fileno(db
->f
), network_nodes_offset
);
190 if (db
->network_nodes_v1
== MAP_FAILED
)
194 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v1
);
196 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
201 static int loc_database_read_networks_section_v1(struct loc_database
* db
,
202 const struct loc_database_header_v1
* header
) {
203 off_t networks_offset
= be32toh(header
->network_data_offset
);
204 size_t networks_length
= be32toh(header
->network_data_length
);
206 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
207 (intmax_t)networks_offset
, networks_length
);
209 if (networks_length
> 0) {
210 db
->networks_v1
= mmap(NULL
, networks_length
, PROT_READ
,
211 MAP_SHARED
, fileno(db
->f
), networks_offset
);
213 if (db
->networks_v1
== MAP_FAILED
)
217 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v1
);
219 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
224 static int loc_database_read_countries_section_v1(struct loc_database
* db
,
225 const struct loc_database_header_v1
* header
) {
226 off_t countries_offset
= be32toh(header
->countries_offset
);
227 size_t countries_length
= be32toh(header
->countries_length
);
229 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
230 (intmax_t)countries_offset
, countries_length
);
232 if (countries_length
> 0) {
233 db
->countries_v1
= mmap(NULL
, countries_length
, PROT_READ
,
234 MAP_SHARED
, fileno(db
->f
), countries_offset
);
236 if (db
->countries_v1
== MAP_FAILED
)
240 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v1
);
242 INFO(db
->ctx
, "Read %zu countries from the database\n",
243 db
->countries_count
);
248 static int loc_database_read_signature(struct loc_database
* db
,
249 char** dst
, char* src
, size_t length
) {
250 // Check for a plausible signature length
251 if (length
> LOC_SIGNATURE_MAX_LENGTH
) {
252 ERROR(db
->ctx
, "Signature too long: %ld\n", length
);
256 DEBUG(db
->ctx
, "Reading signature of %ld bytes\n", length
);
259 *dst
= malloc(length
);
264 memcpy(*dst
, src
, length
);
269 static int loc_database_read_header_v1(struct loc_database
* db
) {
270 struct loc_database_header_v1 header
;
274 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
276 if (size
< sizeof(header
)) {
277 ERROR(db
->ctx
, "Could not read enough data for header\n");
282 db
->created_at
= be64toh(header
.created_at
);
283 db
->vendor
= be32toh(header
.vendor
);
284 db
->description
= be32toh(header
.description
);
285 db
->license
= be32toh(header
.license
);
287 db
->signature1_length
= be16toh(header
.signature1_length
);
288 db
->signature2_length
= be16toh(header
.signature2_length
);
291 if (db
->signature1_length
) {
292 r
= loc_database_read_signature(db
, &db
->signature1
,
293 header
.signature1
, db
->signature1_length
);
298 if (db
->signature2_length
) {
299 r
= loc_database_read_signature(db
, &db
->signature2
,
300 header
.signature2
, db
->signature2_length
);
306 off_t pool_offset
= be32toh(header
.pool_offset
);
307 size_t pool_length
= be32toh(header
.pool_length
);
309 r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
310 db
->f
, pool_length
, pool_offset
);
315 r
= loc_database_read_as_section_v1(db
, &header
);
320 r
= loc_database_read_network_nodes_section_v1(db
, &header
);
325 r
= loc_database_read_networks_section_v1(db
, &header
);
330 r
= loc_database_read_countries_section_v1(db
, &header
);
337 static int loc_database_read_header(struct loc_database
* db
) {
338 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
340 switch (db
->version
) {
341 case LOC_DATABASE_VERSION_1
:
342 return loc_database_read_header_v1(db
);
345 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
350 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
351 clock_t start
= clock();
355 // Clone file descriptor
358 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
362 // Reopen the file so that we can keep our own file handle
363 db
->f
= fdopen(fd
, "r");
365 ERROR(db
->ctx
, "Could not re-open database file\n");
369 // Rewind to the start of the file
373 int r
= loc_database_read_magic(db
);
378 r
= loc_database_read_header(db
);
382 clock_t end
= clock();
384 INFO(db
->ctx
, "Opened database in %.4fms\n",
385 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
390 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
391 // Fail on invalid file handle
395 struct loc_database
* db
= calloc(1, sizeof(*db
));
400 db
->ctx
= loc_ref(ctx
);
403 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
405 int r
= loc_database_read(db
, f
);
407 loc_database_unref(db
);
416 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
422 static void loc_database_free(struct loc_database
* db
) {
425 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
429 r
= munmap(db
->as_v1
, db
->as_count
* sizeof(*db
->as_v1
));
431 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
434 // Remove mapped network sections
435 if (db
->networks_v1
) {
436 r
= munmap(db
->networks_v1
, db
->networks_count
* sizeof(*db
->networks_v1
));
438 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
441 // Remove mapped network nodes section
442 if (db
->network_nodes_v1
) {
443 r
= munmap(db
->network_nodes_v1
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v1
));
445 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
449 loc_stringpool_unref(db
->pool
);
453 free(db
->signature1
);
455 free(db
->signature2
);
457 // Close database file
465 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
466 if (--db
->refcount
> 0)
469 loc_database_free(db
);
473 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
474 // Cannot do this when no signature is available
475 if (!db
->signature1
&& !db
->signature2
) {
476 DEBUG(db
->ctx
, "No signature available to verify\n");
480 // Start the stopwatch
481 clock_t start
= clock();
484 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
486 char* error
= ERR_error_string(ERR_get_error(), NULL
);
487 ERROR(db
->ctx
, "Could not parse public key: %s\n", error
);
494 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
496 // Initialise hash function
497 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
499 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
500 ERR_error_string(ERR_get_error(), NULL
));
506 // Reset file to start
510 struct loc_database_magic magic
;
511 fread(&magic
, 1, sizeof(magic
), db
->f
);
513 hexdump(db
->ctx
, &magic
, sizeof(magic
));
515 // Feed magic into the hash
516 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
518 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
525 struct loc_database_header_v1 header_v1
;
528 switch (db
->version
) {
529 case LOC_DATABASE_VERSION_1
:
530 bytes_read
= fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
531 if (bytes_read
< sizeof(header_v1
)) {
532 ERROR(db
->ctx
, "Could not read header\n");
539 memset(header_v1
.signature1
, '\0', sizeof(header_v1
.signature1
));
540 header_v1
.signature1_length
= 0;
541 memset(header_v1
.signature2
, '\0', sizeof(header_v1
.signature2
));
542 header_v1
.signature2_length
= 0;
544 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
546 // Feed header into the hash
547 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
549 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
557 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
563 // Walk through the file in chunks of 64kB
564 char buffer
[64 * 1024];
566 while (!feof(db
->f
)) {
567 bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
569 hexdump(db
->ctx
, buffer
, bytes_read
);
571 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
573 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
580 // Check first signature
581 if (db
->signature1
) {
582 hexdump(db
->ctx
, db
->signature1
, db
->signature1_length
);
584 r
= EVP_DigestVerifyFinal(mdctx
,
585 (unsigned char*)db
->signature1
, db
->signature1_length
);
588 DEBUG(db
->ctx
, "The first signature is invalid\n");
591 DEBUG(db
->ctx
, "The first signature is valid\n");
594 ERROR(db
->ctx
, "Error verifying the first signature: %s\n",
595 ERR_error_string(ERR_get_error(), NULL
));
600 // Check second signature only when the first one was invalid
601 if (r
&& db
->signature2
) {
602 hexdump(db
->ctx
, db
->signature2
, db
->signature2_length
);
604 r
= EVP_DigestVerifyFinal(mdctx
,
605 (unsigned char*)db
->signature2
, db
->signature2_length
);
608 DEBUG(db
->ctx
, "The second signature is invalid\n");
611 DEBUG(db
->ctx
, "The second signature is valid\n");
614 ERROR(db
->ctx
, "Error verifying the second signature: %s\n",
615 ERR_error_string(ERR_get_error(), NULL
));
620 clock_t end
= clock();
621 DEBUG(db
->ctx
, "Signature checked in %.4fms\n",
622 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
626 EVP_MD_CTX_free(mdctx
);
632 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
633 return db
->created_at
;
636 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
637 return loc_stringpool_get(db
->pool
, db
->vendor
);
640 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
641 return loc_stringpool_get(db
->pool
, db
->description
);
644 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
645 return loc_stringpool_get(db
->pool
, db
->license
);
648 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
652 // Returns the AS at position pos
653 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
654 if ((size_t)pos
>= db
->as_count
)
657 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
660 switch (db
->version
) {
661 case LOC_DATABASE_VERSION_1
:
662 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, db
->as_v1
+ pos
);
670 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
676 // Performs a binary search to find the AS in the list
677 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
679 off_t hi
= db
->as_count
- 1;
682 clock_t start
= clock();
685 off_t i
= (lo
+ hi
) / 2;
687 // Fetch AS in the middle between lo and hi
688 int r
= loc_database_fetch_as(db
, as
, i
);
692 // Check if this is a match
693 uint32_t as_number
= loc_as_get_number(*as
);
694 if (as_number
== number
) {
695 clock_t end
= clock();
697 // Log how fast this has been
698 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
699 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
704 // If it wasn't, we release the AS and
705 // adjust our search pointers
708 if (as_number
< number
) {
720 // Returns the network at position pos
721 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
722 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
723 if ((size_t)pos
>= db
->networks_count
) {
724 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
725 (intmax_t)pos
, (intmax_t)db
->networks_count
);
730 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
733 switch (db
->version
) {
734 case LOC_DATABASE_VERSION_1
:
735 r
= loc_network_new_from_database_v1(db
->ctx
, network
,
736 address
, prefix
, db
->networks_v1
+ pos
);
744 char* string
= loc_network_str(*network
);
745 DEBUG(db
->ctx
, "Got network %s\n", string
);
752 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
753 return (node
->network
!= htobe32(0xffffffff));
756 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
757 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
758 const struct loc_database_network_node_v1
* node
) {
759 off_t network_index
= be32toh(node
->network
);
761 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v1
), (intmax_t)network_index
);
764 int r
= loc_database_fetch_network(db
, network
,
765 network_address
, prefix
, network_index
);
767 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
771 // Check if the given IP address is inside the network
772 r
= loc_network_match_address(*network
, address
);
774 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
776 loc_network_unref(*network
);
781 // A network was found and the IP address matches
785 // Searches for an exact match along the path
786 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
787 struct loc_network
** network
, struct in6_addr
* network_address
,
788 const struct loc_database_network_node_v1
* node
, unsigned int level
) {
793 int bit
= in6_addr_get_bit(address
, level
);
794 in6_addr_set_bit(network_address
, level
, bit
);
797 node_index
= be32toh(node
->zero
);
799 node_index
= be32toh(node
->one
);
801 // If the node index is zero, the tree ends here
802 // and we cannot descend any further
803 if (node_index
> 0) {
805 if ((size_t)node_index
>= db
->network_nodes_count
)
808 // Move on to the next node
809 r
= __loc_database_lookup(db
, address
, network
, network_address
,
810 db
->network_nodes_v1
+ node_index
, level
+ 1);
812 // End here if a result was found
820 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
822 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
825 // If this node has a leaf, we will check if it matches
826 if (__loc_database_node_is_leaf(node
)) {
827 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
835 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
836 struct in6_addr
* address
, struct loc_network
** network
) {
837 struct in6_addr network_address
;
838 memset(&network_address
, 0, sizeof(network_address
));
843 clock_t start
= clock();
845 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
846 db
->network_nodes_v1
, 0);
848 clock_t end
= clock();
850 // Log how fast this has been
851 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
852 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
857 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
858 const char* string
, struct loc_network
** network
) {
859 struct in6_addr address
;
861 int r
= loc_parse_address(db
->ctx
, string
, &address
);
865 return loc_database_lookup(db
, &address
, network
);
868 // Returns the country at position pos
869 static int loc_database_fetch_country(struct loc_database
* db
,
870 struct loc_country
** country
, off_t pos
) {
871 if ((size_t)pos
>= db
->countries_count
)
874 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
877 switch (db
->version
) {
878 case LOC_DATABASE_VERSION_1
:
879 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, db
->countries_v1
+ pos
);
887 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
893 // Performs a binary search to find the country in the list
894 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
895 struct loc_country
** country
, const char* code
) {
897 off_t hi
= db
->countries_count
- 1;
900 clock_t start
= clock();
903 off_t i
= (lo
+ hi
) / 2;
905 // Fetch country in the middle between lo and hi
906 int r
= loc_database_fetch_country(db
, country
, i
);
910 // Check if this is a match
911 const char* cc
= loc_country_get_code(*country
);
912 int result
= strcmp(code
, cc
);
915 clock_t end
= clock();
917 // Log how fast this has been
918 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
919 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
924 // If it wasn't, we release the country and
925 // adjust our search pointers
926 loc_country_unref(*country
);
942 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
943 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
945 // Release all references
946 loc_database_unref(enumerator
->db
);
947 loc_unref(enumerator
->ctx
);
949 if (enumerator
->string
)
950 free(enumerator
->string
);
952 // Free network search
953 free(enumerator
->networks_visited
);
956 if (enumerator
->stack
)
957 loc_network_list_unref(enumerator
->stack
);
962 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
963 struct loc_database
* db
, enum loc_database_enumerator_mode mode
, int flags
) {
964 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
969 e
->ctx
= loc_ref(db
->ctx
);
970 e
->db
= loc_database_ref(db
);
975 e
->flatten
= (flags
& LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
977 // Initialise graph search
978 e
->network_stack_depth
= 1;
979 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
982 int r
= loc_network_list_new(e
->ctx
, &e
->stack
);
984 loc_database_enumerator_free(e
);
988 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
994 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
995 enumerator
->refcount
++;
1000 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
1004 if (--enumerator
->refcount
> 0)
1007 loc_database_enumerator_free(enumerator
);
1011 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
1012 enumerator
->string
= strdup(string
);
1014 // Make the string lowercase
1015 for (char *p
= enumerator
->string
; *p
; p
++)
1021 LOC_EXPORT
struct loc_country_list
* loc_database_enumerator_get_countries(
1022 struct loc_database_enumerator
* enumerator
) {
1023 if (!enumerator
->countries
)
1026 return loc_country_list_ref(enumerator
->countries
);
1029 LOC_EXPORT
int loc_database_enumerator_set_countries(
1030 struct loc_database_enumerator
* enumerator
, struct loc_country_list
* countries
) {
1031 if (enumerator
->countries
)
1032 loc_country_list_unref(enumerator
->countries
);
1034 enumerator
->countries
= loc_country_list_ref(countries
);
1039 LOC_EXPORT
int loc_database_enumerator_set_asn(
1040 struct loc_database_enumerator
* enumerator
, unsigned int asn
) {
1041 enumerator
->asn
= asn
;
1046 LOC_EXPORT
int loc_database_enumerator_set_flag(
1047 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
1048 enumerator
->flags
|= flag
;
1053 LOC_EXPORT
int loc_database_enumerator_set_family(
1054 struct loc_database_enumerator
* enumerator
, int family
) {
1055 enumerator
->family
= family
;
1060 LOC_EXPORT
int loc_database_enumerator_next_as(
1061 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
1064 // Do not do anything if not in AS mode
1065 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
1068 struct loc_database
* db
= enumerator
->db
;
1070 while (enumerator
->as_index
< db
->as_count
) {
1071 // Fetch the next AS
1072 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1076 r
= loc_as_match_string(*as
, enumerator
->string
);
1078 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1079 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1090 enumerator
->as_index
= 0;
1092 // We have searched through all of them
1096 static int loc_database_enumerator_stack_push_node(
1097 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1098 // Do not add empty nodes
1102 // Check if there is any space left on the stack
1103 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1104 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1108 // Increase stack size
1109 int s
= ++e
->network_stack_depth
;
1111 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1113 e
->network_stack
[s
].offset
= offset
;
1114 e
->network_stack
[s
].i
= i
;
1115 e
->network_stack
[s
].depth
= depth
;
1120 static int loc_network_match_countries(struct loc_network
* network
, struct loc_country_list
* countries
) {
1121 const char* country_code
= loc_network_get_country_code(network
);
1123 return loc_country_list_contains_code(countries
, country_code
);
1126 static int loc_database_enumerator_filter_network(
1127 struct loc_database_enumerator
* enumerator
, struct loc_network
* network
) {
1128 // Skip if the family does not match
1129 if (enumerator
->family
&& loc_network_address_family(network
) != enumerator
->family
)
1132 // Skip if the country code does not match
1133 if (enumerator
->countries
&& !loc_network_match_countries(network
, enumerator
->countries
))
1136 // Skip if the ASN does not match
1137 if (enumerator
->asn
&&
1138 !loc_network_match_asn(network
, enumerator
->asn
))
1141 // Skip if flags do not match
1142 if (enumerator
->flags
&&
1143 !loc_network_match_flag(network
, enumerator
->flags
))
1150 static int __loc_database_enumerator_next_network(
1151 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
, int filter
) {
1152 // Return top element from the stack
1154 *network
= loc_network_list_pop(enumerator
->stack
);
1160 // Throw away any networks by filter
1161 if (filter
&& loc_database_enumerator_filter_network(enumerator
, *network
)) {
1162 loc_network_unref(*network
);
1171 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1172 enumerator
->network_stack_depth
);
1175 while (enumerator
->network_stack_depth
> 0) {
1176 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1178 // Get object from top of the stack
1179 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1181 // Remove the node from the stack if we have already visited it
1182 if (enumerator
->networks_visited
[node
->offset
]) {
1183 enumerator
->network_stack_depth
--;
1187 // Mark the bits on the path correctly
1188 in6_addr_set_bit(&enumerator
->network_address
,
1189 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1191 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1192 enumerator
->networks_visited
[node
->offset
]++;
1194 // Pop node from top of the stack
1195 struct loc_database_network_node_v1
* n
=
1196 enumerator
->db
->network_nodes_v1
+ node
->offset
;
1198 // Add edges to stack
1199 int r
= loc_database_enumerator_stack_push_node(enumerator
,
1200 be32toh(n
->one
), 1, node
->depth
+ 1);
1205 r
= loc_database_enumerator_stack_push_node(enumerator
,
1206 be32toh(n
->zero
), 0, node
->depth
+ 1);
1211 // Check if this node is a leaf and has a network object
1212 if (__loc_database_node_is_leaf(n
)) {
1213 off_t network_index
= be32toh(n
->network
);
1215 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1217 // Fetch the network object
1218 r
= loc_database_fetch_network(enumerator
->db
, network
,
1219 &enumerator
->network_address
, node
->depth
, network_index
);
1221 // Break on any errors
1225 // Return all networks when the filter is disabled
1229 // Check if we are interested in this network
1230 if (loc_database_enumerator_filter_network(enumerator
, *network
)) {
1231 loc_network_unref(*network
);
1241 // Reached the end of the search
1245 static int __loc_database_enumerator_next_network_flattened(
1246 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1247 // Fetch the next network
1248 int r
= __loc_database_enumerator_next_network(enumerator
, network
, 1);
1252 // End if we could not read another network
1256 struct loc_network
* subnet
= NULL
;
1257 struct loc_network_list
* subnets
;
1259 // Create a list with all subnets
1260 r
= loc_network_list_new(enumerator
->ctx
, &subnets
);
1264 // Search all subnets from the database
1266 // Fetch the next network in line
1267 r
= __loc_database_enumerator_next_network(enumerator
, &subnet
, 0);
1271 // End if we did not receive another subnet
1275 // Collect all subnets in a list
1276 if (loc_network_is_subnet(*network
, subnet
)) {
1277 r
= loc_network_list_push(subnets
, subnet
);
1281 loc_network_unref(subnet
);
1285 // If this is not a subnet, we push it back onto the stack and break
1286 r
= loc_network_list_push(enumerator
->stack
, subnet
);
1290 loc_network_unref(subnet
);
1294 DEBUG(enumerator
->ctx
, "Found %zu subnet(s)\n", loc_network_list_size(subnets
));
1296 // We can abort here if the network has no subnets
1297 if (loc_network_list_empty(subnets
)) {
1298 loc_network_list_unref(subnets
);
1303 // If the network has any subnets, we will break it into smaller parts
1304 // without the subnets.
1305 struct loc_network_list
* excluded
= loc_network_exclude_list(*network
, subnets
);
1306 if (!excluded
|| loc_network_list_empty(excluded
)) {
1311 // Replace network with the first one
1312 loc_network_unref(*network
);
1314 *network
= loc_network_list_pop_first(excluded
);
1316 // Push the rest onto the stack
1317 loc_network_list_reverse(excluded
);
1318 loc_network_list_merge(enumerator
->stack
, excluded
);
1320 loc_network_list_unref(excluded
);
1324 loc_network_unref(subnet
);
1326 loc_network_list_unref(subnets
);
1331 LOC_EXPORT
int loc_database_enumerator_next_network(
1332 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1333 // Do not do anything if not in network mode
1334 if (enumerator
->mode
!= LOC_DB_ENUMERATE_NETWORKS
)
1338 if (enumerator
->flatten
)
1339 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1341 return __loc_database_enumerator_next_network(enumerator
, network
, 1);
1344 LOC_EXPORT
int loc_database_enumerator_next_country(
1345 struct loc_database_enumerator
* enumerator
, struct loc_country
** country
) {
1348 // Do not do anything if not in country mode
1349 if (enumerator
->mode
!= LOC_DB_ENUMERATE_COUNTRIES
)
1352 struct loc_database
* db
= enumerator
->db
;
1354 while (enumerator
->country_index
< db
->countries_count
) {
1355 // Fetch the next country
1356 int r
= loc_database_fetch_country(db
, country
, enumerator
->country_index
++);
1360 // We do not filter here, so it always is a match
1365 enumerator
->country_index
= 0;
1367 // We have searched through all of them