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 static int loc_database_read_magic(struct loc_database
* db
) {
124 struct loc_database_magic magic
;
127 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
129 // Check if we have been able to read enough data
130 if (bytes_read
< sizeof(magic
)) {
131 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
132 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
136 // Compare magic bytes
137 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
138 DEBUG(db
->ctx
, "Magic value matches\n");
141 db
->version
= magic
.version
;
146 ERROR(db
->ctx
, "Unrecognized file type\n");
152 static int loc_database_read_as_section_v1(struct loc_database
* db
,
153 const struct loc_database_header_v1
* header
) {
154 off_t as_offset
= be32toh(header
->as_offset
);
155 size_t as_length
= be32toh(header
->as_length
);
157 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
160 db
->as_v1
= mmap(NULL
, as_length
, PROT_READ
,
161 MAP_SHARED
, fileno(db
->f
), as_offset
);
163 if (db
->as_v1
== MAP_FAILED
)
167 db
->as_count
= as_length
/ sizeof(*db
->as_v1
);
169 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
174 static int loc_database_read_network_nodes_section_v1(struct loc_database
* db
,
175 const struct loc_database_header_v1
* header
) {
176 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
177 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
179 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
180 (intmax_t)network_nodes_offset
, network_nodes_length
);
182 if (network_nodes_length
> 0) {
183 db
->network_nodes_v1
= mmap(NULL
, network_nodes_length
, PROT_READ
,
184 MAP_SHARED
, fileno(db
->f
), network_nodes_offset
);
186 if (db
->network_nodes_v1
== MAP_FAILED
)
190 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v1
);
192 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
197 static int loc_database_read_networks_section_v1(struct loc_database
* db
,
198 const struct loc_database_header_v1
* header
) {
199 off_t networks_offset
= be32toh(header
->network_data_offset
);
200 size_t networks_length
= be32toh(header
->network_data_length
);
202 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
203 (intmax_t)networks_offset
, networks_length
);
205 if (networks_length
> 0) {
206 db
->networks_v1
= mmap(NULL
, networks_length
, PROT_READ
,
207 MAP_SHARED
, fileno(db
->f
), networks_offset
);
209 if (db
->networks_v1
== MAP_FAILED
)
213 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v1
);
215 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
220 static int loc_database_read_countries_section_v1(struct loc_database
* db
,
221 const struct loc_database_header_v1
* header
) {
222 off_t countries_offset
= be32toh(header
->countries_offset
);
223 size_t countries_length
= be32toh(header
->countries_length
);
225 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
226 (intmax_t)countries_offset
, countries_length
);
228 if (countries_length
> 0) {
229 db
->countries_v1
= mmap(NULL
, countries_length
, PROT_READ
,
230 MAP_SHARED
, fileno(db
->f
), countries_offset
);
232 if (db
->countries_v1
== MAP_FAILED
)
236 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v1
);
238 INFO(db
->ctx
, "Read %zu countries from the database\n",
239 db
->countries_count
);
244 static int loc_database_read_signature(struct loc_database
* db
,
245 char** dst
, char* src
, size_t length
) {
246 // Check for a plausible signature length
247 if (length
> LOC_SIGNATURE_MAX_LENGTH
) {
248 ERROR(db
->ctx
, "Signature too long: %ld\n", length
);
252 DEBUG(db
->ctx
, "Reading signature of %ld bytes\n", length
);
255 *dst
= malloc(length
);
260 memcpy(*dst
, src
, length
);
265 static int loc_database_read_header_v1(struct loc_database
* db
) {
266 struct loc_database_header_v1 header
;
270 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
272 if (size
< sizeof(header
)) {
273 ERROR(db
->ctx
, "Could not read enough data for header\n");
278 db
->created_at
= be64toh(header
.created_at
);
279 db
->vendor
= be32toh(header
.vendor
);
280 db
->description
= be32toh(header
.description
);
281 db
->license
= be32toh(header
.license
);
283 db
->signature1_length
= be16toh(header
.signature1_length
);
284 db
->signature2_length
= be16toh(header
.signature2_length
);
287 if (db
->signature1_length
) {
288 r
= loc_database_read_signature(db
, &db
->signature1
,
289 header
.signature1
, db
->signature1_length
);
294 if (db
->signature2_length
) {
295 r
= loc_database_read_signature(db
, &db
->signature2
,
296 header
.signature2
, db
->signature2_length
);
302 off_t pool_offset
= be32toh(header
.pool_offset
);
303 size_t pool_length
= be32toh(header
.pool_length
);
305 r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
306 db
->f
, pool_length
, pool_offset
);
311 r
= loc_database_read_as_section_v1(db
, &header
);
316 r
= loc_database_read_network_nodes_section_v1(db
, &header
);
321 r
= loc_database_read_networks_section_v1(db
, &header
);
326 r
= loc_database_read_countries_section_v1(db
, &header
);
333 static int loc_database_read_header(struct loc_database
* db
) {
334 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
336 switch (db
->version
) {
337 case LOC_DATABASE_VERSION_1
:
338 return loc_database_read_header_v1(db
);
341 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
346 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
347 clock_t start
= clock();
351 // Clone file descriptor
354 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
358 // Reopen the file so that we can keep our own file handle
359 db
->f
= fdopen(fd
, "r");
361 ERROR(db
->ctx
, "Could not re-open database file\n");
365 // Rewind to the start of the file
369 int r
= loc_database_read_magic(db
);
374 r
= loc_database_read_header(db
);
378 clock_t end
= clock();
380 INFO(db
->ctx
, "Opened database in %.4fms\n",
381 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
386 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
387 // Fail on invalid file handle
391 struct loc_database
* db
= calloc(1, sizeof(*db
));
396 db
->ctx
= loc_ref(ctx
);
399 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
401 int r
= loc_database_read(db
, f
);
403 loc_database_unref(db
);
412 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
418 static void loc_database_free(struct loc_database
* db
) {
421 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
425 r
= munmap(db
->as_v1
, db
->as_count
* sizeof(*db
->as_v1
));
427 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
430 // Remove mapped network sections
431 if (db
->networks_v1
) {
432 r
= munmap(db
->networks_v1
, db
->networks_count
* sizeof(*db
->networks_v1
));
434 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
437 // Remove mapped network nodes section
438 if (db
->network_nodes_v1
) {
439 r
= munmap(db
->network_nodes_v1
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v1
));
441 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
445 loc_stringpool_unref(db
->pool
);
449 free(db
->signature1
);
451 free(db
->signature2
);
453 // Close database file
461 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
462 if (--db
->refcount
> 0)
465 loc_database_free(db
);
469 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
470 // Cannot do this when no signature is available
471 if (!db
->signature1
&& !db
->signature2
) {
472 DEBUG(db
->ctx
, "No signature available to verify\n");
476 // Start the stopwatch
477 clock_t start
= clock();
480 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
482 char* error
= ERR_error_string(ERR_get_error(), NULL
);
483 ERROR(db
->ctx
, "Could not parse public key: %s\n", error
);
490 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
492 // Initialise hash function
493 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
495 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
496 ERR_error_string(ERR_get_error(), NULL
));
502 // Reset file to start
506 struct loc_database_magic magic
;
507 fread(&magic
, 1, sizeof(magic
), db
->f
);
509 hexdump(db
->ctx
, &magic
, sizeof(magic
));
511 // Feed magic into the hash
512 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
514 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
521 struct loc_database_header_v1 header_v1
;
524 switch (db
->version
) {
525 case LOC_DATABASE_VERSION_1
:
526 bytes_read
= fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
527 if (bytes_read
< sizeof(header_v1
)) {
528 ERROR(db
->ctx
, "Could not read header\n");
535 memset(header_v1
.signature1
, '\0', sizeof(header_v1
.signature1
));
536 header_v1
.signature1_length
= 0;
537 memset(header_v1
.signature2
, '\0', sizeof(header_v1
.signature2
));
538 header_v1
.signature2_length
= 0;
540 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
542 // Feed header into the hash
543 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
545 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
553 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
559 // Walk through the file in chunks of 64kB
560 char buffer
[64 * 1024];
562 while (!feof(db
->f
)) {
563 bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
565 hexdump(db
->ctx
, buffer
, bytes_read
);
567 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
569 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
576 // Check first signature
577 if (db
->signature1
) {
578 hexdump(db
->ctx
, db
->signature1
, db
->signature1_length
);
580 r
= EVP_DigestVerifyFinal(mdctx
,
581 (unsigned char*)db
->signature1
, db
->signature1_length
);
584 DEBUG(db
->ctx
, "The first signature is invalid\n");
587 DEBUG(db
->ctx
, "The first signature is valid\n");
590 ERROR(db
->ctx
, "Error verifying the first signature: %s\n",
591 ERR_error_string(ERR_get_error(), NULL
));
596 // Check second signature only when the first one was invalid
597 if (r
&& db
->signature2
) {
598 hexdump(db
->ctx
, db
->signature2
, db
->signature2_length
);
600 r
= EVP_DigestVerifyFinal(mdctx
,
601 (unsigned char*)db
->signature2
, db
->signature2_length
);
604 DEBUG(db
->ctx
, "The second signature is invalid\n");
607 DEBUG(db
->ctx
, "The second signature is valid\n");
610 ERROR(db
->ctx
, "Error verifying the second signature: %s\n",
611 ERR_error_string(ERR_get_error(), NULL
));
616 clock_t end
= clock();
617 DEBUG(db
->ctx
, "Signature checked in %.4fms\n",
618 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
622 EVP_MD_CTX_free(mdctx
);
628 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
629 return db
->created_at
;
632 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
633 return loc_stringpool_get(db
->pool
, db
->vendor
);
636 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
637 return loc_stringpool_get(db
->pool
, db
->description
);
640 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
641 return loc_stringpool_get(db
->pool
, db
->license
);
644 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
648 // Returns the AS at position pos
649 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
650 if ((size_t)pos
>= db
->as_count
)
653 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
656 switch (db
->version
) {
657 case LOC_DATABASE_VERSION_1
:
658 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, db
->as_v1
+ pos
);
666 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
672 // Performs a binary search to find the AS in the list
673 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
675 off_t hi
= db
->as_count
- 1;
678 clock_t start
= clock();
681 off_t i
= (lo
+ hi
) / 2;
683 // Fetch AS in the middle between lo and hi
684 int r
= loc_database_fetch_as(db
, as
, i
);
688 // Check if this is a match
689 uint32_t as_number
= loc_as_get_number(*as
);
690 if (as_number
== number
) {
691 clock_t end
= clock();
693 // Log how fast this has been
694 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
695 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
700 // If it wasn't, we release the AS and
701 // adjust our search pointers
704 if (as_number
< number
) {
716 // Returns the network at position pos
717 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
718 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
719 if ((size_t)pos
>= db
->networks_count
) {
720 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
721 (intmax_t)pos
, (intmax_t)db
->networks_count
);
726 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
729 switch (db
->version
) {
730 case LOC_DATABASE_VERSION_1
:
731 r
= loc_network_new_from_database_v1(db
->ctx
, network
,
732 address
, prefix
, db
->networks_v1
+ pos
);
740 char* string
= loc_network_str(*network
);
741 DEBUG(db
->ctx
, "Got network %s\n", string
);
748 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
749 return (node
->network
!= htobe32(0xffffffff));
752 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
753 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
754 const struct loc_database_network_node_v1
* node
) {
755 off_t network_index
= be32toh(node
->network
);
757 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v1
), (intmax_t)network_index
);
760 int r
= loc_database_fetch_network(db
, network
,
761 network_address
, prefix
, network_index
);
763 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
767 // Check if the given IP address is inside the network
768 r
= loc_network_match_address(*network
, address
);
770 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
772 loc_network_unref(*network
);
777 // A network was found and the IP address matches
781 // Searches for an exact match along the path
782 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
783 struct loc_network
** network
, struct in6_addr
* network_address
,
784 const struct loc_database_network_node_v1
* node
, unsigned int level
) {
789 int bit
= in6_addr_get_bit(address
, level
);
790 in6_addr_set_bit(network_address
, level
, bit
);
793 node_index
= be32toh(node
->zero
);
795 node_index
= be32toh(node
->one
);
797 // If the node index is zero, the tree ends here
798 // and we cannot descend any further
799 if (node_index
> 0) {
801 if ((size_t)node_index
>= db
->network_nodes_count
)
804 // Move on to the next node
805 r
= __loc_database_lookup(db
, address
, network
, network_address
,
806 db
->network_nodes_v1
+ node_index
, level
+ 1);
808 // End here if a result was found
816 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
818 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
821 // If this node has a leaf, we will check if it matches
822 if (__loc_database_node_is_leaf(node
)) {
823 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
831 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
832 struct in6_addr
* address
, struct loc_network
** network
) {
833 struct in6_addr network_address
;
834 memset(&network_address
, 0, sizeof(network_address
));
839 clock_t start
= clock();
841 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
842 db
->network_nodes_v1
, 0);
844 clock_t end
= clock();
846 // Log how fast this has been
847 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
848 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
853 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
854 const char* string
, struct loc_network
** network
) {
855 struct in6_addr address
;
857 int r
= loc_parse_address(db
->ctx
, string
, &address
);
861 return loc_database_lookup(db
, &address
, network
);
864 // Returns the country at position pos
865 static int loc_database_fetch_country(struct loc_database
* db
,
866 struct loc_country
** country
, off_t pos
) {
867 if ((size_t)pos
>= db
->countries_count
)
870 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
873 switch (db
->version
) {
874 case LOC_DATABASE_VERSION_1
:
875 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, db
->countries_v1
+ pos
);
883 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
889 // Performs a binary search to find the country in the list
890 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
891 struct loc_country
** country
, const char* code
) {
893 off_t hi
= db
->countries_count
- 1;
896 clock_t start
= clock();
899 off_t i
= (lo
+ hi
) / 2;
901 // Fetch country in the middle between lo and hi
902 int r
= loc_database_fetch_country(db
, country
, i
);
906 // Check if this is a match
907 const char* cc
= loc_country_get_code(*country
);
908 int result
= strcmp(code
, cc
);
911 clock_t end
= clock();
913 // Log how fast this has been
914 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
915 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
920 // If it wasn't, we release the country and
921 // adjust our search pointers
922 loc_country_unref(*country
);
938 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
939 struct loc_database
* db
, enum loc_database_enumerator_mode mode
, int flags
) {
940 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
945 e
->ctx
= loc_ref(db
->ctx
);
946 e
->db
= loc_database_ref(db
);
951 e
->flatten
= (flags
& LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
953 // Initialise graph search
954 //e->network_stack[++e->network_stack_depth] = 0;
955 e
->network_stack_depth
= 1;
956 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
958 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
964 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
965 enumerator
->refcount
++;
970 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
971 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
973 // Release all references
974 loc_database_unref(enumerator
->db
);
975 loc_unref(enumerator
->ctx
);
977 if (enumerator
->string
)
978 free(enumerator
->string
);
980 // Free network search
981 free(enumerator
->networks_visited
);
986 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
990 if (--enumerator
->refcount
> 0)
993 loc_database_enumerator_free(enumerator
);
997 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
998 enumerator
->string
= strdup(string
);
1000 // Make the string lowercase
1001 for (char *p
= enumerator
->string
; *p
; p
++)
1007 LOC_EXPORT
int loc_database_enumerator_set_country_code(struct loc_database_enumerator
* enumerator
, const char* country_code
) {
1008 // Set empty country code
1009 if (!country_code
|| !*country_code
) {
1010 *enumerator
->country_code
= '\0';
1014 // Treat A1, A2, A3 as special country codes,
1015 // but perform search for flags instead
1016 if (strcmp(country_code
, "A1") == 0) {
1017 return loc_database_enumerator_set_flag(enumerator
,
1018 LOC_NETWORK_FLAG_ANONYMOUS_PROXY
);
1019 } else if (strcmp(country_code
, "A2") == 0) {
1020 return loc_database_enumerator_set_flag(enumerator
,
1021 LOC_NETWORK_FLAG_SATELLITE_PROVIDER
);
1022 } else if (strcmp(country_code
, "A3") == 0) {
1023 return loc_database_enumerator_set_flag(enumerator
,
1024 LOC_NETWORK_FLAG_ANYCAST
);
1027 // Country codes must be two characters
1028 if (!loc_country_code_is_valid(country_code
))
1031 for (unsigned int i
= 0; i
< 3; i
++) {
1032 enumerator
->country_code
[i
] = country_code
[i
];
1038 LOC_EXPORT
int loc_database_enumerator_set_asn(
1039 struct loc_database_enumerator
* enumerator
, unsigned int asn
) {
1040 enumerator
->asn
= asn
;
1045 LOC_EXPORT
int loc_database_enumerator_set_flag(
1046 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
1047 enumerator
->flags
|= flag
;
1052 LOC_EXPORT
int loc_database_enumerator_set_family(
1053 struct loc_database_enumerator
* enumerator
, int family
) {
1054 enumerator
->family
= family
;
1059 LOC_EXPORT
int loc_database_enumerator_next_as(
1060 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
1063 // Do not do anything if not in AS mode
1064 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
1067 struct loc_database
* db
= enumerator
->db
;
1069 while (enumerator
->as_index
< db
->as_count
) {
1070 // Fetch the next AS
1071 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1075 r
= loc_as_match_string(*as
, enumerator
->string
);
1077 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1078 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1089 enumerator
->as_index
= 0;
1091 // We have searched through all of them
1095 static int loc_database_enumerator_stack_push_node(
1096 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1097 // Do not add empty nodes
1101 // Check if there is any space left on the stack
1102 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1103 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1107 // Increase stack size
1108 int s
= ++e
->network_stack_depth
;
1110 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1112 e
->network_stack
[s
].offset
= offset
;
1113 e
->network_stack
[s
].i
= i
;
1114 e
->network_stack
[s
].depth
= depth
;
1119 LOC_EXPORT
int loc_database_enumerator_next_network(
1120 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1124 // Do not do anything if not in network mode
1125 if (enumerator
->mode
!= LOC_DB_ENUMERATE_NETWORKS
)
1130 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1131 enumerator
->network_stack_depth
);
1134 while (enumerator
->network_stack_depth
> 0) {
1135 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1137 // Get object from top of the stack
1138 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1140 // Remove the node from the stack if we have already visited it
1141 if (enumerator
->networks_visited
[node
->offset
]) {
1142 enumerator
->network_stack_depth
--;
1146 // Mark the bits on the path correctly
1147 in6_addr_set_bit(&enumerator
->network_address
,
1148 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1150 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1151 enumerator
->networks_visited
[node
->offset
]++;
1153 // Pop node from top of the stack
1154 struct loc_database_network_node_v1
* n
=
1155 enumerator
->db
->network_nodes_v1
+ node
->offset
;
1157 // Add edges to stack
1158 r
= loc_database_enumerator_stack_push_node(enumerator
,
1159 be32toh(n
->one
), 1, node
->depth
+ 1);
1164 r
= loc_database_enumerator_stack_push_node(enumerator
,
1165 be32toh(n
->zero
), 0, node
->depth
+ 1);
1170 // Check if this node is a leaf and has a network object
1171 if (__loc_database_node_is_leaf(n
)) {
1172 off_t network_index
= be32toh(n
->network
);
1174 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1176 // Fetch the network object
1177 r
= loc_database_fetch_network(enumerator
->db
, network
,
1178 &enumerator
->network_address
, node
->depth
, network_index
);
1180 // Break on any errors
1184 // Check if we are interested in this network
1186 // Skip if the family does not match
1187 if (enumerator
->family
&& loc_network_address_family(*network
) != enumerator
->family
) {
1188 loc_network_unref(*network
);
1194 // Skip if the country code does not match
1195 if (*enumerator
->country_code
&&
1196 !loc_network_match_country_code(*network
, enumerator
->country_code
)) {
1197 loc_network_unref(*network
);
1203 // Skip if the ASN does not match
1204 if (enumerator
->asn
&&
1205 !loc_network_match_asn(*network
, enumerator
->asn
)) {
1206 loc_network_unref(*network
);
1212 // Skip if flags do not match
1213 if (enumerator
->flags
&&
1214 !loc_network_match_flag(*network
, enumerator
->flags
)) {
1215 loc_network_unref(*network
);
1225 // Reached the end of the search
1227 // Mark all nodes as non-visited
1228 for (unsigned int i
= 0; i
< enumerator
->db
->network_nodes_count
; i
++)
1229 enumerator
->networks_visited
[i
] = 0;
1234 LOC_EXPORT
int loc_database_enumerator_next_country(
1235 struct loc_database_enumerator
* enumerator
, struct loc_country
** country
) {
1238 // Do not do anything if not in country mode
1239 if (enumerator
->mode
!= LOC_DB_ENUMERATE_COUNTRIES
)
1242 struct loc_database
* db
= enumerator
->db
;
1244 while (enumerator
->country_index
< db
->countries_count
) {
1245 // Fetch the next country
1246 int r
= loc_database_fetch_country(db
, country
, enumerator
->country_index
++);
1250 // We do not filter here, so it always is a match
1255 enumerator
->country_index
= 0;
1257 // We have searched through all of them