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 <libloc/libloc.h>
40 #include <libloc/as.h>
41 #include <libloc/as-list.h>
42 #include <libloc/compat.h>
43 #include <libloc/country.h>
44 #include <libloc/country-list.h>
45 #include <libloc/database.h>
46 #include <libloc/format.h>
47 #include <libloc/network.h>
48 #include <libloc/network-list.h>
49 #include <libloc/private.h>
50 #include <libloc/stringpool.h>
58 enum loc_database_version version
;
66 size_t signature1_length
;
68 size_t signature2_length
;
70 // ASes in the database
71 struct loc_database_as_v1
* as_v1
;
75 struct loc_database_network_node_v1
* network_nodes_v1
;
76 size_t network_nodes_count
;
79 struct loc_database_network_v1
* networks_v1
;
80 size_t networks_count
;
83 struct loc_database_country_v1
* countries_v1
;
84 size_t countries_count
;
86 struct loc_stringpool
* pool
;
89 #define MAX_STACK_DEPTH 256
91 struct loc_node_stack
{
93 int i
; // Is this node 0 or 1?
97 struct loc_database_enumerator
{
99 struct loc_database
* db
;
100 enum loc_database_enumerator_mode mode
;
105 struct loc_country_list
* countries
;
106 struct loc_as_list
* asns
;
107 enum loc_network_flags flags
;
113 // Index of the AS we are looking at
114 unsigned int as_index
;
116 // Index of the country we are looking at
117 unsigned int country_index
;
120 struct in6_addr network_address
;
121 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
122 int network_stack_depth
;
123 unsigned int* networks_visited
;
125 // For subnet search and bogons
126 struct loc_network_list
* stack
;
127 struct loc_network
* last_network
;
130 static int loc_database_read_magic(struct loc_database
* db
) {
131 struct loc_database_magic magic
;
134 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
136 // Check if we have been able to read enough data
137 if (bytes_read
< sizeof(magic
)) {
138 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
139 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
143 // Compare magic bytes
144 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
145 DEBUG(db
->ctx
, "Magic value matches\n");
148 db
->version
= magic
.version
;
153 ERROR(db
->ctx
, "Unrecognized file type\n");
159 static int loc_database_read_as_section_v1(struct loc_database
* db
,
160 const struct loc_database_header_v1
* header
) {
161 off_t as_offset
= be32toh(header
->as_offset
);
162 size_t as_length
= be32toh(header
->as_length
);
164 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
167 db
->as_v1
= mmap(NULL
, as_length
, PROT_READ
,
168 MAP_SHARED
, fileno(db
->f
), as_offset
);
170 if (db
->as_v1
== MAP_FAILED
)
174 db
->as_count
= as_length
/ sizeof(*db
->as_v1
);
176 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
181 static int loc_database_read_network_nodes_section_v1(struct loc_database
* db
,
182 const struct loc_database_header_v1
* header
) {
183 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
184 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
186 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
187 (intmax_t)network_nodes_offset
, network_nodes_length
);
189 if (network_nodes_length
> 0) {
190 db
->network_nodes_v1
= mmap(NULL
, network_nodes_length
, PROT_READ
,
191 MAP_SHARED
, fileno(db
->f
), network_nodes_offset
);
193 if (db
->network_nodes_v1
== MAP_FAILED
)
197 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v1
);
199 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
204 static int loc_database_read_networks_section_v1(struct loc_database
* db
,
205 const struct loc_database_header_v1
* header
) {
206 off_t networks_offset
= be32toh(header
->network_data_offset
);
207 size_t networks_length
= be32toh(header
->network_data_length
);
209 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
210 (intmax_t)networks_offset
, networks_length
);
212 if (networks_length
> 0) {
213 db
->networks_v1
= mmap(NULL
, networks_length
, PROT_READ
,
214 MAP_SHARED
, fileno(db
->f
), networks_offset
);
216 if (db
->networks_v1
== MAP_FAILED
)
220 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v1
);
222 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
227 static int loc_database_read_countries_section_v1(struct loc_database
* db
,
228 const struct loc_database_header_v1
* header
) {
229 off_t countries_offset
= be32toh(header
->countries_offset
);
230 size_t countries_length
= be32toh(header
->countries_length
);
232 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
233 (intmax_t)countries_offset
, countries_length
);
235 if (countries_length
> 0) {
236 db
->countries_v1
= mmap(NULL
, countries_length
, PROT_READ
,
237 MAP_SHARED
, fileno(db
->f
), countries_offset
);
239 if (db
->countries_v1
== MAP_FAILED
)
243 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v1
);
245 INFO(db
->ctx
, "Read %zu countries from the database\n",
246 db
->countries_count
);
251 static int loc_database_read_signature(struct loc_database
* db
,
252 char** dst
, char* src
, size_t length
) {
253 // Check for a plausible signature length
254 if (length
> LOC_SIGNATURE_MAX_LENGTH
) {
255 ERROR(db
->ctx
, "Signature too long: %zu\n", length
);
259 DEBUG(db
->ctx
, "Reading signature of %zu bytes\n", length
);
262 *dst
= malloc(length
);
267 memcpy(*dst
, src
, length
);
272 static int loc_database_read_header_v1(struct loc_database
* db
) {
273 struct loc_database_header_v1 header
;
277 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
279 if (size
< sizeof(header
)) {
280 ERROR(db
->ctx
, "Could not read enough data for header\n");
285 db
->created_at
= be64toh(header
.created_at
);
286 db
->vendor
= be32toh(header
.vendor
);
287 db
->description
= be32toh(header
.description
);
288 db
->license
= be32toh(header
.license
);
290 db
->signature1_length
= be16toh(header
.signature1_length
);
291 db
->signature2_length
= be16toh(header
.signature2_length
);
294 if (db
->signature1_length
) {
295 r
= loc_database_read_signature(db
, &db
->signature1
,
296 header
.signature1
, db
->signature1_length
);
301 if (db
->signature2_length
) {
302 r
= loc_database_read_signature(db
, &db
->signature2
,
303 header
.signature2
, db
->signature2_length
);
309 off_t pool_offset
= be32toh(header
.pool_offset
);
310 size_t pool_length
= be32toh(header
.pool_length
);
312 r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
313 db
->f
, pool_length
, pool_offset
);
318 r
= loc_database_read_as_section_v1(db
, &header
);
323 r
= loc_database_read_network_nodes_section_v1(db
, &header
);
328 r
= loc_database_read_networks_section_v1(db
, &header
);
333 r
= loc_database_read_countries_section_v1(db
, &header
);
340 static int loc_database_read_header(struct loc_database
* db
) {
341 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
343 switch (db
->version
) {
344 case LOC_DATABASE_VERSION_1
:
345 return loc_database_read_header_v1(db
);
348 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
353 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
354 clock_t start
= clock();
358 // Clone file descriptor
361 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
365 // Reopen the file so that we can keep our own file handle
366 db
->f
= fdopen(fd
, "r");
368 ERROR(db
->ctx
, "Could not re-open database file\n");
372 // Rewind to the start of the file
376 int r
= loc_database_read_magic(db
);
381 r
= loc_database_read_header(db
);
385 clock_t end
= clock();
387 INFO(db
->ctx
, "Opened database in %.4fms\n",
388 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
393 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
394 // Fail on invalid file handle
398 struct loc_database
* db
= calloc(1, sizeof(*db
));
403 db
->ctx
= loc_ref(ctx
);
406 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
408 int r
= loc_database_read(db
, f
);
410 loc_database_unref(db
);
419 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
425 static void loc_database_free(struct loc_database
* db
) {
428 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
432 r
= munmap(db
->as_v1
, db
->as_count
* sizeof(*db
->as_v1
));
434 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
437 // Remove mapped network sections
438 if (db
->networks_v1
) {
439 r
= munmap(db
->networks_v1
, db
->networks_count
* sizeof(*db
->networks_v1
));
441 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
444 // Remove mapped network nodes section
445 if (db
->network_nodes_v1
) {
446 r
= munmap(db
->network_nodes_v1
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v1
));
448 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
451 // Remove mapped countries section
452 if (db
->countries_v1
) {
453 r
= munmap(db
->countries_v1
, db
->countries_count
* sizeof(*db
->countries_v1
));
455 ERROR(db
->ctx
, "Could not unmap countries section: %s\n", strerror(errno
));
459 loc_stringpool_unref(db
->pool
);
463 free(db
->signature1
);
465 free(db
->signature2
);
467 // Close database file
475 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
476 if (--db
->refcount
> 0)
479 loc_database_free(db
);
483 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
484 // Cannot do this when no signature is available
485 if (!db
->signature1
&& !db
->signature2
) {
486 DEBUG(db
->ctx
, "No signature available to verify\n");
490 // Start the stopwatch
491 clock_t start
= clock();
494 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
496 char* error
= ERR_error_string(ERR_get_error(), NULL
);
497 ERROR(db
->ctx
, "Could not parse public key: %s\n", error
);
504 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
506 // Initialise hash function
507 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
509 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
510 ERR_error_string(ERR_get_error(), NULL
));
516 // Reset file to start
520 struct loc_database_magic magic
;
521 fread(&magic
, 1, sizeof(magic
), db
->f
);
523 hexdump(db
->ctx
, &magic
, sizeof(magic
));
525 // Feed magic into the hash
526 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
528 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
535 struct loc_database_header_v1 header_v1
;
538 switch (db
->version
) {
539 case LOC_DATABASE_VERSION_1
:
540 bytes_read
= fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
541 if (bytes_read
< sizeof(header_v1
)) {
542 ERROR(db
->ctx
, "Could not read header\n");
549 memset(header_v1
.signature1
, '\0', sizeof(header_v1
.signature1
));
550 header_v1
.signature1_length
= 0;
551 memset(header_v1
.signature2
, '\0', sizeof(header_v1
.signature2
));
552 header_v1
.signature2_length
= 0;
554 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
556 // Feed header into the hash
557 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
559 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
567 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
573 // Walk through the file in chunks of 64kB
574 char buffer
[64 * 1024];
576 while (!feof(db
->f
)) {
577 bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
579 hexdump(db
->ctx
, buffer
, bytes_read
);
581 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
583 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
590 // Check first signature
591 if (db
->signature1
) {
592 hexdump(db
->ctx
, db
->signature1
, db
->signature1_length
);
594 r
= EVP_DigestVerifyFinal(mdctx
,
595 (unsigned char*)db
->signature1
, db
->signature1_length
);
598 DEBUG(db
->ctx
, "The first signature is invalid\n");
601 DEBUG(db
->ctx
, "The first signature is valid\n");
604 ERROR(db
->ctx
, "Error verifying the first signature: %s\n",
605 ERR_error_string(ERR_get_error(), NULL
));
610 // Check second signature only when the first one was invalid
611 if (r
&& db
->signature2
) {
612 hexdump(db
->ctx
, db
->signature2
, db
->signature2_length
);
614 r
= EVP_DigestVerifyFinal(mdctx
,
615 (unsigned char*)db
->signature2
, db
->signature2_length
);
618 DEBUG(db
->ctx
, "The second signature is invalid\n");
621 DEBUG(db
->ctx
, "The second signature is valid\n");
624 ERROR(db
->ctx
, "Error verifying the second signature: %s\n",
625 ERR_error_string(ERR_get_error(), NULL
));
630 clock_t end
= clock();
631 INFO(db
->ctx
, "Signature checked in %.4fms\n",
632 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
636 EVP_MD_CTX_free(mdctx
);
642 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
643 return db
->created_at
;
646 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
647 return loc_stringpool_get(db
->pool
, db
->vendor
);
650 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
651 return loc_stringpool_get(db
->pool
, db
->description
);
654 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
655 return loc_stringpool_get(db
->pool
, db
->license
);
658 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
662 // Returns the AS at position pos
663 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
664 if ((size_t)pos
>= db
->as_count
)
667 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
670 switch (db
->version
) {
671 case LOC_DATABASE_VERSION_1
:
672 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, db
->as_v1
+ pos
);
680 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
686 // Performs a binary search to find the AS in the list
687 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
689 off_t hi
= db
->as_count
- 1;
693 clock_t start
= clock();
697 off_t i
= (lo
+ hi
) / 2;
699 // Fetch AS in the middle between lo and hi
700 int r
= loc_database_fetch_as(db
, as
, i
);
704 // Check if this is a match
705 uint32_t as_number
= loc_as_get_number(*as
);
706 if (as_number
== number
) {
708 clock_t end
= clock();
710 // Log how fast this has been
711 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
712 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
718 // If it wasn't, we release the AS and
719 // adjust our search pointers
722 if (as_number
< number
) {
734 // Returns the network at position pos
735 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
736 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
737 if ((size_t)pos
>= db
->networks_count
) {
738 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
739 (intmax_t)pos
, (intmax_t)db
->networks_count
);
744 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
747 switch (db
->version
) {
748 case LOC_DATABASE_VERSION_1
:
749 r
= loc_network_new_from_database_v1(db
->ctx
, network
,
750 address
, prefix
, db
->networks_v1
+ pos
);
759 char* string
= loc_network_str(*network
);
760 DEBUG(db
->ctx
, "Got network %s\n", string
);
768 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
769 return (node
->network
!= htobe32(0xffffffff));
772 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
773 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
774 const struct loc_database_network_node_v1
* node
) {
775 off_t network_index
= be32toh(node
->network
);
777 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v1
), (intmax_t)network_index
);
780 int r
= loc_database_fetch_network(db
, network
,
781 network_address
, prefix
, network_index
);
783 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
787 // Check if the given IP address is inside the network
788 if (!loc_network_matches_address(*network
, address
)) {
789 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
791 loc_network_unref(*network
);
796 // A network was found and the IP address matches
800 // Searches for an exact match along the path
801 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
802 struct loc_network
** network
, struct in6_addr
* network_address
,
803 const struct loc_database_network_node_v1
* node
, unsigned int level
) {
808 int bit
= in6_addr_get_bit(address
, level
);
809 in6_addr_set_bit(network_address
, level
, bit
);
812 node_index
= be32toh(node
->zero
);
814 node_index
= be32toh(node
->one
);
816 // If the node index is zero, the tree ends here
817 // and we cannot descend any further
818 if (node_index
> 0) {
820 if ((size_t)node_index
>= db
->network_nodes_count
)
823 // Move on to the next node
824 r
= __loc_database_lookup(db
, address
, network
, network_address
,
825 db
->network_nodes_v1
+ node_index
, level
+ 1);
827 // End here if a result was found
835 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
837 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
840 // If this node has a leaf, we will check if it matches
841 if (__loc_database_node_is_leaf(node
)) {
842 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
850 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
851 const struct in6_addr
* address
, struct loc_network
** network
) {
852 struct in6_addr network_address
;
853 memset(&network_address
, 0, sizeof(network_address
));
859 clock_t start
= clock();
862 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
863 db
->network_nodes_v1
, 0);
866 clock_t end
= clock();
868 // Log how fast this has been
869 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
870 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
876 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
877 const char* string
, struct loc_network
** network
) {
878 struct in6_addr address
;
880 int r
= loc_parse_address(db
->ctx
, string
, &address
);
884 return loc_database_lookup(db
, &address
, network
);
887 // Returns the country at position pos
888 static int loc_database_fetch_country(struct loc_database
* db
,
889 struct loc_country
** country
, off_t pos
) {
890 if ((size_t)pos
>= db
->countries_count
)
893 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
896 switch (db
->version
) {
897 case LOC_DATABASE_VERSION_1
:
898 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, db
->countries_v1
+ pos
);
906 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
912 // Performs a binary search to find the country in the list
913 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
914 struct loc_country
** country
, const char* code
) {
916 off_t hi
= db
->countries_count
- 1;
920 clock_t start
= clock();
924 off_t i
= (lo
+ hi
) / 2;
926 // Fetch country in the middle between lo and hi
927 int r
= loc_database_fetch_country(db
, country
, i
);
931 // Check if this is a match
932 const char* cc
= loc_country_get_code(*country
);
933 int result
= strcmp(code
, cc
);
937 clock_t end
= clock();
939 // Log how fast this has been
940 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
941 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
947 // If it wasn't, we release the country and
948 // adjust our search pointers
949 loc_country_unref(*country
);
965 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
966 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
968 // Release all references
969 loc_database_unref(enumerator
->db
);
970 loc_unref(enumerator
->ctx
);
972 if (enumerator
->string
)
973 free(enumerator
->string
);
975 if (enumerator
->countries
)
976 loc_country_list_unref(enumerator
->countries
);
978 if (enumerator
->asns
)
979 loc_as_list_unref(enumerator
->asns
);
981 // Free network search
982 free(enumerator
->networks_visited
);
984 // Free subnet/bogons stack
985 if (enumerator
->stack
)
986 loc_network_list_unref(enumerator
->stack
);
988 if (enumerator
->last_network
)
989 loc_network_unref(enumerator
->last_network
);
994 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
995 struct loc_database
* db
, enum loc_database_enumerator_mode mode
, int flags
) {
996 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
1000 // Reference context
1001 e
->ctx
= loc_ref(db
->ctx
);
1002 e
->db
= loc_database_ref(db
);
1007 e
->flatten
= (flags
& LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
1009 // Initialise graph search
1010 e
->network_stack_depth
= 1;
1011 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
1014 int r
= loc_network_list_new(e
->ctx
, &e
->stack
);
1016 loc_database_enumerator_free(e
);
1020 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
1026 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
1027 enumerator
->refcount
++;
1032 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
1036 if (--enumerator
->refcount
> 0)
1039 loc_database_enumerator_free(enumerator
);
1043 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
1044 enumerator
->string
= strdup(string
);
1046 // Make the string lowercase
1047 for (char *p
= enumerator
->string
; *p
; p
++)
1053 LOC_EXPORT
struct loc_country_list
* loc_database_enumerator_get_countries(
1054 struct loc_database_enumerator
* enumerator
) {
1055 if (!enumerator
->countries
)
1058 return loc_country_list_ref(enumerator
->countries
);
1061 LOC_EXPORT
int loc_database_enumerator_set_countries(
1062 struct loc_database_enumerator
* enumerator
, struct loc_country_list
* countries
) {
1063 if (enumerator
->countries
)
1064 loc_country_list_unref(enumerator
->countries
);
1066 enumerator
->countries
= loc_country_list_ref(countries
);
1071 LOC_EXPORT
struct loc_as_list
* loc_database_enumerator_get_asns(
1072 struct loc_database_enumerator
* enumerator
) {
1073 if (!enumerator
->asns
)
1076 return loc_as_list_ref(enumerator
->asns
);
1079 LOC_EXPORT
int loc_database_enumerator_set_asns(
1080 struct loc_database_enumerator
* enumerator
, struct loc_as_list
* asns
) {
1081 if (enumerator
->asns
)
1082 loc_as_list_unref(enumerator
->asns
);
1084 enumerator
->asns
= loc_as_list_ref(asns
);
1089 LOC_EXPORT
int loc_database_enumerator_set_flag(
1090 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
1091 enumerator
->flags
|= flag
;
1096 LOC_EXPORT
int loc_database_enumerator_set_family(
1097 struct loc_database_enumerator
* enumerator
, int family
) {
1098 enumerator
->family
= family
;
1103 LOC_EXPORT
int loc_database_enumerator_next_as(
1104 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
1107 // Do not do anything if not in AS mode
1108 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
1111 struct loc_database
* db
= enumerator
->db
;
1113 while (enumerator
->as_index
< db
->as_count
) {
1114 // Fetch the next AS
1115 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1119 r
= loc_as_match_string(*as
, enumerator
->string
);
1121 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1122 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1133 enumerator
->as_index
= 0;
1135 // We have searched through all of them
1139 static int loc_database_enumerator_stack_push_node(
1140 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1141 // Do not add empty nodes
1145 // Check if there is any space left on the stack
1146 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1147 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1151 // Increase stack size
1152 int s
= ++e
->network_stack_depth
;
1154 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1156 e
->network_stack
[s
].offset
= offset
;
1157 e
->network_stack
[s
].i
= i
;
1158 e
->network_stack
[s
].depth
= depth
;
1163 static int loc_database_enumerator_match_network(
1164 struct loc_database_enumerator
* enumerator
, struct loc_network
* network
) {
1165 // If family is set, it must match
1166 if (enumerator
->family
&& loc_network_address_family(network
) != enumerator
->family
) {
1167 DEBUG(enumerator
->ctx
, "Filtered network %p because of family not matching\n", network
);
1171 // Match if no filter criteria is configured
1172 if (!enumerator
->countries
&& !enumerator
->asns
&& !enumerator
->flags
)
1175 // Check if the country code matches
1176 if (enumerator
->countries
&& !loc_country_list_empty(enumerator
->countries
)) {
1177 const char* country_code
= loc_network_get_country_code(network
);
1179 if (loc_country_list_contains_code(enumerator
->countries
, country_code
)) {
1180 DEBUG(enumerator
->ctx
, "Matched network %p because of its country code\n", network
);
1185 // Check if the ASN matches
1186 if (enumerator
->asns
&& !loc_as_list_empty(enumerator
->asns
)) {
1187 uint32_t asn
= loc_network_get_asn(network
);
1189 if (loc_as_list_contains_number(enumerator
->asns
, asn
)) {
1190 DEBUG(enumerator
->ctx
, "Matched network %p because of its ASN\n", network
);
1195 // Check if flags match
1196 if (enumerator
->flags
&& loc_network_has_flag(network
, enumerator
->flags
)) {
1197 DEBUG(enumerator
->ctx
, "Matched network %p because of its flags\n", network
);
1205 static int __loc_database_enumerator_next_network(
1206 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
, int filter
) {
1207 // Return top element from the stack
1209 *network
= loc_network_list_pop_first(enumerator
->stack
);
1215 // Return everything if filter isn't enabled, or only return matches
1216 if (!filter
|| loc_database_enumerator_match_network(enumerator
, *network
))
1219 // Throw away anything that doesn't match
1220 loc_network_unref(*network
);
1224 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1225 enumerator
->network_stack_depth
);
1228 while (enumerator
->network_stack_depth
> 0) {
1229 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1231 // Get object from top of the stack
1232 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1234 // Remove the node from the stack if we have already visited it
1235 if (enumerator
->networks_visited
[node
->offset
]) {
1236 enumerator
->network_stack_depth
--;
1240 // Mark the bits on the path correctly
1241 in6_addr_set_bit(&enumerator
->network_address
,
1242 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1244 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1245 enumerator
->networks_visited
[node
->offset
]++;
1247 // Pop node from top of the stack
1248 struct loc_database_network_node_v1
* n
=
1249 enumerator
->db
->network_nodes_v1
+ node
->offset
;
1251 // Add edges to stack
1252 int r
= loc_database_enumerator_stack_push_node(enumerator
,
1253 be32toh(n
->one
), 1, node
->depth
+ 1);
1258 r
= loc_database_enumerator_stack_push_node(enumerator
,
1259 be32toh(n
->zero
), 0, node
->depth
+ 1);
1264 // Check if this node is a leaf and has a network object
1265 if (__loc_database_node_is_leaf(n
)) {
1266 off_t network_index
= be32toh(n
->network
);
1268 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1270 // Fetch the network object
1271 r
= loc_database_fetch_network(enumerator
->db
, network
,
1272 &enumerator
->network_address
, node
->depth
, network_index
);
1274 // Break on any errors
1278 // Return all networks when the filter is disabled, or check for match
1279 if (!filter
|| loc_database_enumerator_match_network(enumerator
, *network
))
1282 // Does not seem to be a match, so we cleanup and move on
1283 loc_network_unref(*network
);
1288 // Reached the end of the search
1292 static int __loc_database_enumerator_next_network_flattened(
1293 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1294 // Fetch the next network
1295 int r
= __loc_database_enumerator_next_network(enumerator
, network
, 1);
1299 // End if we could not read another network
1303 struct loc_network
* subnet
= NULL
;
1304 struct loc_network_list
* subnets
;
1306 // Create a list with all subnets
1307 r
= loc_network_list_new(enumerator
->ctx
, &subnets
);
1311 // Search all subnets from the database
1313 // Fetch the next network in line
1314 r
= __loc_database_enumerator_next_network(enumerator
, &subnet
, 0);
1316 loc_network_unref(subnet
);
1317 loc_network_list_unref(subnets
);
1322 // End if we did not receive another subnet
1326 // Collect all subnets in a list
1327 if (loc_network_is_subnet(*network
, subnet
)) {
1328 r
= loc_network_list_push(subnets
, subnet
);
1330 loc_network_unref(subnet
);
1331 loc_network_list_unref(subnets
);
1336 loc_network_unref(subnet
);
1340 // If this is not a subnet, we push it back onto the stack and break
1341 r
= loc_network_list_push(enumerator
->stack
, subnet
);
1343 loc_network_unref(subnet
);
1344 loc_network_list_unref(subnets
);
1349 loc_network_unref(subnet
);
1353 DEBUG(enumerator
->ctx
, "Found %zu subnet(s)\n", loc_network_list_size(subnets
));
1355 // We can abort here if the network has no subnets
1356 if (loc_network_list_empty(subnets
)) {
1357 loc_network_list_unref(subnets
);
1362 // If the network has any subnets, we will break it into smaller parts
1363 // without the subnets.
1364 struct loc_network_list
* excluded
= loc_network_exclude_list(*network
, subnets
);
1366 loc_network_list_unref(subnets
);
1370 // Merge subnets onto the stack
1371 r
= loc_network_list_merge(enumerator
->stack
, subnets
);
1373 loc_network_list_unref(subnets
);
1374 loc_network_list_unref(excluded
);
1379 // Push excluded list onto the stack
1380 r
= loc_network_list_merge(enumerator
->stack
, excluded
);
1382 loc_network_list_unref(subnets
);
1383 loc_network_list_unref(excluded
);
1388 loc_network_list_unref(subnets
);
1389 loc_network_list_unref(excluded
);
1391 // Drop the network and restart the whole process again to pick the next network
1392 loc_network_unref(*network
);
1394 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1398 This function finds all bogons (i.e. gaps) between the input networks
1400 static int __loc_database_enumerator_find_bogons(struct loc_ctx
* ctx
,
1401 struct loc_network
* first
, struct loc_network
* second
, struct loc_network_list
* bogons
) {
1402 // We do not need to check if first < second because the graph is always ordered
1404 // The last address of the first network + 1 is the start of the gap
1405 struct in6_addr first_address
= address_increment(loc_network_get_last_address(first
));
1407 // The first address of the second network - 1 is the end of the gap
1408 struct in6_addr last_address
= address_decrement(loc_network_get_first_address(second
));
1410 // If first address is now greater than last address, there is no gap
1411 if (in6_addr_cmp(&first_address
, &last_address
) >= 0)
1414 return loc_network_list_summarize(ctx
, &first_address
, &last_address
, &bogons
);
1417 static int __loc_database_enumerator_next_bogon(
1418 struct loc_database_enumerator
* enumerator
, struct loc_network
** bogon
) {
1421 // Return top element from the stack
1423 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1433 struct loc_network
* network
= NULL
;
1436 r
= __loc_database_enumerator_next_network(enumerator
, &network
, 1);
1443 if (enumerator
->last_network
&& loc_network_address_family(enumerator
->last_network
) == loc_network_address_family(network
)) {
1444 r
= __loc_database_enumerator_find_bogons(enumerator
->ctx
,
1445 enumerator
->last_network
, network
, enumerator
->stack
);
1447 loc_network_unref(network
);
1452 // Remember network for next iteration
1453 enumerator
->last_network
= loc_network_ref(network
);
1454 loc_network_unref(network
);
1456 // Try to return something
1457 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1466 LOC_EXPORT
int loc_database_enumerator_next_network(
1467 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1468 switch (enumerator
->mode
) {
1469 case LOC_DB_ENUMERATE_NETWORKS
:
1471 if (enumerator
->flatten
)
1472 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1474 return __loc_database_enumerator_next_network(enumerator
, network
, 1);
1476 case LOC_DB_ENUMERATE_BOGONS
:
1477 return __loc_database_enumerator_next_bogon(enumerator
, network
);
1484 LOC_EXPORT
int loc_database_enumerator_next_country(
1485 struct loc_database_enumerator
* enumerator
, struct loc_country
** country
) {
1488 // Do not do anything if not in country mode
1489 if (enumerator
->mode
!= LOC_DB_ENUMERATE_COUNTRIES
)
1492 struct loc_database
* db
= enumerator
->db
;
1494 while (enumerator
->country_index
< db
->countries_count
) {
1495 // Fetch the next country
1496 int r
= loc_database_fetch_country(db
, country
, enumerator
->country_index
++);
1500 // We do not filter here, so it always is a match
1505 enumerator
->country_index
= 0;
1507 // We have searched through all of them