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/address.h>
41 #include <libloc/as.h>
42 #include <libloc/as-list.h>
43 #include <libloc/compat.h>
44 #include <libloc/country.h>
45 #include <libloc/country-list.h>
46 #include <libloc/database.h>
47 #include <libloc/format.h>
48 #include <libloc/network.h>
49 #include <libloc/network-list.h>
50 #include <libloc/private.h>
51 #include <libloc/stringpool.h>
59 enum loc_database_version version
;
67 size_t signature1_length
;
69 size_t signature2_length
;
71 // ASes in the database
72 struct loc_database_as_v1
* as_v1
;
76 struct loc_database_network_node_v1
* network_nodes_v1
;
77 size_t network_nodes_count
;
80 struct loc_database_network_v1
* networks_v1
;
81 size_t networks_count
;
84 struct loc_database_country_v1
* countries_v1
;
85 size_t countries_count
;
87 struct loc_stringpool
* pool
;
90 #define MAX_STACK_DEPTH 256
92 struct loc_node_stack
{
94 int i
; // Is this node 0 or 1?
98 struct loc_database_enumerator
{
100 struct loc_database
* db
;
101 enum loc_database_enumerator_mode mode
;
106 struct loc_country_list
* countries
;
107 struct loc_as_list
* asns
;
108 enum loc_network_flags flags
;
114 // Index of the AS we are looking at
115 unsigned int as_index
;
117 // Index of the country we are looking at
118 unsigned int country_index
;
121 struct in6_addr network_address
;
122 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
123 int network_stack_depth
;
124 unsigned int* networks_visited
;
126 // For subnet search and bogons
127 struct loc_network_list
* stack
;
130 struct in6_addr gap6_start
;
131 struct in6_addr gap4_start
;
134 static int loc_database_read_magic(struct loc_database
* db
) {
135 struct loc_database_magic magic
;
138 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
140 // Check if we have been able to read enough data
141 if (bytes_read
< sizeof(magic
)) {
142 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
143 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
147 // Compare magic bytes
148 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
149 DEBUG(db
->ctx
, "Magic value matches\n");
152 db
->version
= magic
.version
;
157 ERROR(db
->ctx
, "Unrecognized file type\n");
163 static int loc_database_read_as_section_v1(struct loc_database
* db
,
164 const struct loc_database_header_v1
* header
) {
165 off_t as_offset
= be32toh(header
->as_offset
);
166 size_t as_length
= be32toh(header
->as_length
);
168 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset
, as_length
);
171 db
->as_v1
= mmap(NULL
, as_length
, PROT_READ
,
172 MAP_SHARED
, fileno(db
->f
), as_offset
);
174 if (db
->as_v1
== MAP_FAILED
)
178 db
->as_count
= as_length
/ sizeof(*db
->as_v1
);
180 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
185 static int loc_database_read_network_nodes_section_v1(struct loc_database
* db
,
186 const struct loc_database_header_v1
* header
) {
187 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
188 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
190 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
191 (intmax_t)network_nodes_offset
, network_nodes_length
);
193 if (network_nodes_length
> 0) {
194 db
->network_nodes_v1
= mmap(NULL
, network_nodes_length
, PROT_READ
,
195 MAP_SHARED
, fileno(db
->f
), network_nodes_offset
);
197 if (db
->network_nodes_v1
== MAP_FAILED
)
201 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v1
);
203 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
208 static int loc_database_read_networks_section_v1(struct loc_database
* db
,
209 const struct loc_database_header_v1
* header
) {
210 off_t networks_offset
= be32toh(header
->network_data_offset
);
211 size_t networks_length
= be32toh(header
->network_data_length
);
213 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
214 (intmax_t)networks_offset
, networks_length
);
216 if (networks_length
> 0) {
217 db
->networks_v1
= mmap(NULL
, networks_length
, PROT_READ
,
218 MAP_SHARED
, fileno(db
->f
), networks_offset
);
220 if (db
->networks_v1
== MAP_FAILED
)
224 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v1
);
226 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
231 static int loc_database_read_countries_section_v1(struct loc_database
* db
,
232 const struct loc_database_header_v1
* header
) {
233 off_t countries_offset
= be32toh(header
->countries_offset
);
234 size_t countries_length
= be32toh(header
->countries_length
);
236 DEBUG(db
->ctx
, "Reading countries section from %jd (%zu bytes)\n",
237 (intmax_t)countries_offset
, countries_length
);
239 if (countries_length
> 0) {
240 db
->countries_v1
= mmap(NULL
, countries_length
, PROT_READ
,
241 MAP_SHARED
, fileno(db
->f
), countries_offset
);
243 if (db
->countries_v1
== MAP_FAILED
)
247 db
->countries_count
= countries_length
/ sizeof(*db
->countries_v1
);
249 INFO(db
->ctx
, "Read %zu countries from the database\n",
250 db
->countries_count
);
255 static int loc_database_read_signature(struct loc_database
* db
,
256 char** dst
, char* src
, size_t length
) {
257 // Check for a plausible signature length
258 if (length
> LOC_SIGNATURE_MAX_LENGTH
) {
259 ERROR(db
->ctx
, "Signature too long: %zu\n", length
);
263 DEBUG(db
->ctx
, "Reading signature of %zu bytes\n", length
);
266 *dst
= malloc(length
);
271 memcpy(*dst
, src
, length
);
276 static int loc_database_read_header_v1(struct loc_database
* db
) {
277 struct loc_database_header_v1 header
;
281 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
283 if (size
< sizeof(header
)) {
284 ERROR(db
->ctx
, "Could not read enough data for header\n");
289 db
->created_at
= be64toh(header
.created_at
);
290 db
->vendor
= be32toh(header
.vendor
);
291 db
->description
= be32toh(header
.description
);
292 db
->license
= be32toh(header
.license
);
294 db
->signature1_length
= be16toh(header
.signature1_length
);
295 db
->signature2_length
= be16toh(header
.signature2_length
);
298 if (db
->signature1_length
) {
299 r
= loc_database_read_signature(db
, &db
->signature1
,
300 header
.signature1
, db
->signature1_length
);
305 if (db
->signature2_length
) {
306 r
= loc_database_read_signature(db
, &db
->signature2
,
307 header
.signature2
, db
->signature2_length
);
313 off_t pool_offset
= be32toh(header
.pool_offset
);
314 size_t pool_length
= be32toh(header
.pool_length
);
316 r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
317 db
->f
, pool_length
, pool_offset
);
322 r
= loc_database_read_as_section_v1(db
, &header
);
327 r
= loc_database_read_network_nodes_section_v1(db
, &header
);
332 r
= loc_database_read_networks_section_v1(db
, &header
);
337 r
= loc_database_read_countries_section_v1(db
, &header
);
344 static int loc_database_read_header(struct loc_database
* db
) {
345 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
347 switch (db
->version
) {
348 case LOC_DATABASE_VERSION_1
:
349 return loc_database_read_header_v1(db
);
352 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
357 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
358 clock_t start
= clock();
362 // Clone file descriptor
365 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
369 // Reopen the file so that we can keep our own file handle
370 db
->f
= fdopen(fd
, "r");
372 ERROR(db
->ctx
, "Could not re-open database file\n");
376 // Rewind to the start of the file
380 int r
= loc_database_read_magic(db
);
385 r
= loc_database_read_header(db
);
389 clock_t end
= clock();
391 INFO(db
->ctx
, "Opened database in %.4fms\n",
392 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
397 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
398 // Fail on invalid file handle
402 struct loc_database
* db
= calloc(1, sizeof(*db
));
407 db
->ctx
= loc_ref(ctx
);
410 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
412 int r
= loc_database_read(db
, f
);
414 loc_database_unref(db
);
423 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
429 static void loc_database_free(struct loc_database
* db
) {
432 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
436 r
= munmap(db
->as_v1
, db
->as_count
* sizeof(*db
->as_v1
));
438 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
441 // Remove mapped network sections
442 if (db
->networks_v1
) {
443 r
= munmap(db
->networks_v1
, db
->networks_count
* sizeof(*db
->networks_v1
));
445 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
448 // Remove mapped network nodes section
449 if (db
->network_nodes_v1
) {
450 r
= munmap(db
->network_nodes_v1
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v1
));
452 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
455 // Remove mapped countries section
456 if (db
->countries_v1
) {
457 r
= munmap(db
->countries_v1
, db
->countries_count
* sizeof(*db
->countries_v1
));
459 ERROR(db
->ctx
, "Could not unmap countries section: %s\n", strerror(errno
));
463 loc_stringpool_unref(db
->pool
);
467 free(db
->signature1
);
469 free(db
->signature2
);
471 // Close database file
479 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
480 if (--db
->refcount
> 0)
483 loc_database_free(db
);
487 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
488 // Cannot do this when no signature is available
489 if (!db
->signature1
&& !db
->signature2
) {
490 DEBUG(db
->ctx
, "No signature available to verify\n");
494 // Start the stopwatch
495 clock_t start
= clock();
498 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
500 char* error
= ERR_error_string(ERR_get_error(), NULL
);
501 ERROR(db
->ctx
, "Could not parse public key: %s\n", error
);
508 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
510 // Initialise hash function
511 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
513 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
514 ERR_error_string(ERR_get_error(), NULL
));
520 // Reset file to start
524 struct loc_database_magic magic
;
525 fread(&magic
, 1, sizeof(magic
), db
->f
);
527 hexdump(db
->ctx
, &magic
, sizeof(magic
));
529 // Feed magic into the hash
530 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
532 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
539 struct loc_database_header_v1 header_v1
;
542 switch (db
->version
) {
543 case LOC_DATABASE_VERSION_1
:
544 bytes_read
= fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
545 if (bytes_read
< sizeof(header_v1
)) {
546 ERROR(db
->ctx
, "Could not read header\n");
553 memset(header_v1
.signature1
, '\0', sizeof(header_v1
.signature1
));
554 header_v1
.signature1_length
= 0;
555 memset(header_v1
.signature2
, '\0', sizeof(header_v1
.signature2
));
556 header_v1
.signature2_length
= 0;
558 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
560 // Feed header into the hash
561 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
563 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
571 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
577 // Walk through the file in chunks of 64kB
578 char buffer
[64 * 1024];
580 while (!feof(db
->f
)) {
581 bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
583 hexdump(db
->ctx
, buffer
, bytes_read
);
585 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
587 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
594 // Check first signature
595 if (db
->signature1
) {
596 hexdump(db
->ctx
, db
->signature1
, db
->signature1_length
);
598 r
= EVP_DigestVerifyFinal(mdctx
,
599 (unsigned char*)db
->signature1
, db
->signature1_length
);
602 DEBUG(db
->ctx
, "The first signature is invalid\n");
605 DEBUG(db
->ctx
, "The first signature is valid\n");
608 ERROR(db
->ctx
, "Error verifying the first signature: %s\n",
609 ERR_error_string(ERR_get_error(), NULL
));
614 // Check second signature only when the first one was invalid
615 if (r
&& db
->signature2
) {
616 hexdump(db
->ctx
, db
->signature2
, db
->signature2_length
);
618 r
= EVP_DigestVerifyFinal(mdctx
,
619 (unsigned char*)db
->signature2
, db
->signature2_length
);
622 DEBUG(db
->ctx
, "The second signature is invalid\n");
625 DEBUG(db
->ctx
, "The second signature is valid\n");
628 ERROR(db
->ctx
, "Error verifying the second signature: %s\n",
629 ERR_error_string(ERR_get_error(), NULL
));
634 clock_t end
= clock();
635 INFO(db
->ctx
, "Signature checked in %.4fms\n",
636 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
640 EVP_MD_CTX_free(mdctx
);
646 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
647 return db
->created_at
;
650 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
651 return loc_stringpool_get(db
->pool
, db
->vendor
);
654 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
655 return loc_stringpool_get(db
->pool
, db
->description
);
658 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
659 return loc_stringpool_get(db
->pool
, db
->license
);
662 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
666 // Returns the AS at position pos
667 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
668 if ((size_t)pos
>= db
->as_count
)
671 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
674 switch (db
->version
) {
675 case LOC_DATABASE_VERSION_1
:
676 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, db
->as_v1
+ pos
);
684 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
690 // Performs a binary search to find the AS in the list
691 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
693 off_t hi
= db
->as_count
- 1;
697 clock_t start
= clock();
701 off_t i
= (lo
+ hi
) / 2;
703 // Fetch AS in the middle between lo and hi
704 int r
= loc_database_fetch_as(db
, as
, i
);
708 // Check if this is a match
709 uint32_t as_number
= loc_as_get_number(*as
);
710 if (as_number
== number
) {
712 clock_t end
= clock();
714 // Log how fast this has been
715 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
716 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
722 // If it wasn't, we release the AS and
723 // adjust our search pointers
726 if (as_number
< number
) {
738 // Returns the network at position pos
739 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
740 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
741 if ((size_t)pos
>= db
->networks_count
) {
742 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
743 (intmax_t)pos
, (intmax_t)db
->networks_count
);
748 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
751 switch (db
->version
) {
752 case LOC_DATABASE_VERSION_1
:
753 r
= loc_network_new_from_database_v1(db
->ctx
, network
,
754 address
, prefix
, db
->networks_v1
+ pos
);
763 char* string
= loc_network_str(*network
);
764 DEBUG(db
->ctx
, "Got network %s\n", string
);
772 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
773 return (node
->network
!= htobe32(0xffffffff));
776 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
777 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
778 const struct loc_database_network_node_v1
* node
) {
779 off_t network_index
= be32toh(node
->network
);
781 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node
- db
->network_nodes_v1
), (intmax_t)network_index
);
784 int r
= loc_database_fetch_network(db
, network
,
785 network_address
, prefix
, network_index
);
787 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", (intmax_t)network_index
);
791 // Check if the given IP address is inside the network
792 if (!loc_network_matches_address(*network
, address
)) {
793 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
795 loc_network_unref(*network
);
800 // A network was found and the IP address matches
804 // Searches for an exact match along the path
805 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
806 struct loc_network
** network
, struct in6_addr
* network_address
,
807 const struct loc_database_network_node_v1
* node
, unsigned int level
) {
812 int bit
= loc_address_get_bit(address
, level
);
813 loc_address_set_bit(network_address
, level
, bit
);
816 node_index
= be32toh(node
->zero
);
818 node_index
= be32toh(node
->one
);
820 // If the node index is zero, the tree ends here
821 // and we cannot descend any further
822 if (node_index
> 0) {
824 if ((size_t)node_index
>= db
->network_nodes_count
)
827 // Move on to the next node
828 r
= __loc_database_lookup(db
, address
, network
, network_address
,
829 db
->network_nodes_v1
+ node_index
, level
+ 1);
831 // End here if a result was found
839 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
841 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
844 // If this node has a leaf, we will check if it matches
845 if (__loc_database_node_is_leaf(node
)) {
846 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
854 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
855 const struct in6_addr
* address
, struct loc_network
** network
) {
856 struct in6_addr network_address
;
857 memset(&network_address
, 0, sizeof(network_address
));
863 clock_t start
= clock();
866 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
867 db
->network_nodes_v1
, 0);
870 clock_t end
= clock();
872 // Log how fast this has been
873 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
874 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
880 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
881 const char* string
, struct loc_network
** network
) {
882 struct in6_addr address
;
884 int r
= loc_parse_address(db
->ctx
, string
, &address
);
888 return loc_database_lookup(db
, &address
, network
);
891 // Returns the country at position pos
892 static int loc_database_fetch_country(struct loc_database
* db
,
893 struct loc_country
** country
, off_t pos
) {
894 if ((size_t)pos
>= db
->countries_count
)
897 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
900 switch (db
->version
) {
901 case LOC_DATABASE_VERSION_1
:
902 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, db
->countries_v1
+ pos
);
910 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
916 // Performs a binary search to find the country in the list
917 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
918 struct loc_country
** country
, const char* code
) {
920 off_t hi
= db
->countries_count
- 1;
924 clock_t start
= clock();
928 off_t i
= (lo
+ hi
) / 2;
930 // Fetch country in the middle between lo and hi
931 int r
= loc_database_fetch_country(db
, country
, i
);
935 // Check if this is a match
936 const char* cc
= loc_country_get_code(*country
);
937 int result
= strcmp(code
, cc
);
941 clock_t end
= clock();
943 // Log how fast this has been
944 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
945 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
951 // If it wasn't, we release the country and
952 // adjust our search pointers
953 loc_country_unref(*country
);
969 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
970 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
972 // Release all references
973 loc_database_unref(enumerator
->db
);
974 loc_unref(enumerator
->ctx
);
976 if (enumerator
->string
)
977 free(enumerator
->string
);
979 if (enumerator
->countries
)
980 loc_country_list_unref(enumerator
->countries
);
982 if (enumerator
->asns
)
983 loc_as_list_unref(enumerator
->asns
);
985 // Free network search
986 free(enumerator
->networks_visited
);
988 // Free subnet/bogons stack
989 if (enumerator
->stack
)
990 loc_network_list_unref(enumerator
->stack
);
995 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
996 struct loc_database
* db
, enum loc_database_enumerator_mode mode
, int flags
) {
997 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
1001 // Reference context
1002 e
->ctx
= loc_ref(db
->ctx
);
1003 e
->db
= loc_database_ref(db
);
1008 e
->flatten
= (flags
& LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
1010 // Initialise graph search
1011 e
->network_stack_depth
= 1;
1012 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
1015 int r
= loc_network_list_new(e
->ctx
, &e
->stack
);
1017 loc_database_enumerator_free(e
);
1021 // Initialize bogon search
1022 loc_address_reset(&e
->gap6_start
, AF_INET6
);
1023 loc_address_reset(&e
->gap4_start
, AF_INET
);
1025 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
1031 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
1032 enumerator
->refcount
++;
1037 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
1041 if (--enumerator
->refcount
> 0)
1044 loc_database_enumerator_free(enumerator
);
1048 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
1049 enumerator
->string
= strdup(string
);
1051 // Make the string lowercase
1052 for (char *p
= enumerator
->string
; *p
; p
++)
1058 LOC_EXPORT
struct loc_country_list
* loc_database_enumerator_get_countries(
1059 struct loc_database_enumerator
* enumerator
) {
1060 if (!enumerator
->countries
)
1063 return loc_country_list_ref(enumerator
->countries
);
1066 LOC_EXPORT
int loc_database_enumerator_set_countries(
1067 struct loc_database_enumerator
* enumerator
, struct loc_country_list
* countries
) {
1068 if (enumerator
->countries
)
1069 loc_country_list_unref(enumerator
->countries
);
1071 enumerator
->countries
= loc_country_list_ref(countries
);
1076 LOC_EXPORT
struct loc_as_list
* loc_database_enumerator_get_asns(
1077 struct loc_database_enumerator
* enumerator
) {
1078 if (!enumerator
->asns
)
1081 return loc_as_list_ref(enumerator
->asns
);
1084 LOC_EXPORT
int loc_database_enumerator_set_asns(
1085 struct loc_database_enumerator
* enumerator
, struct loc_as_list
* asns
) {
1086 if (enumerator
->asns
)
1087 loc_as_list_unref(enumerator
->asns
);
1089 enumerator
->asns
= loc_as_list_ref(asns
);
1094 LOC_EXPORT
int loc_database_enumerator_set_flag(
1095 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
1096 enumerator
->flags
|= flag
;
1101 LOC_EXPORT
int loc_database_enumerator_set_family(
1102 struct loc_database_enumerator
* enumerator
, int family
) {
1103 enumerator
->family
= family
;
1108 LOC_EXPORT
int loc_database_enumerator_next_as(
1109 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
1112 // Do not do anything if not in AS mode
1113 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
1116 struct loc_database
* db
= enumerator
->db
;
1118 while (enumerator
->as_index
< db
->as_count
) {
1119 // Fetch the next AS
1120 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1124 r
= loc_as_match_string(*as
, enumerator
->string
);
1126 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1127 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1138 enumerator
->as_index
= 0;
1140 // We have searched through all of them
1144 static int loc_database_enumerator_stack_push_node(
1145 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1146 // Do not add empty nodes
1150 // Check if there is any space left on the stack
1151 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1152 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1156 // Increase stack size
1157 int s
= ++e
->network_stack_depth
;
1159 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1161 e
->network_stack
[s
].offset
= offset
;
1162 e
->network_stack
[s
].i
= i
;
1163 e
->network_stack
[s
].depth
= depth
;
1168 static int loc_database_enumerator_match_network(
1169 struct loc_database_enumerator
* enumerator
, struct loc_network
* network
) {
1170 // If family is set, it must match
1171 if (enumerator
->family
&& loc_network_address_family(network
) != enumerator
->family
) {
1172 DEBUG(enumerator
->ctx
, "Filtered network %p because of family not matching\n", network
);
1176 // Match if no filter criteria is configured
1177 if (!enumerator
->countries
&& !enumerator
->asns
&& !enumerator
->flags
)
1180 // Check if the country code matches
1181 if (enumerator
->countries
&& !loc_country_list_empty(enumerator
->countries
)) {
1182 const char* country_code
= loc_network_get_country_code(network
);
1184 if (loc_country_list_contains_code(enumerator
->countries
, country_code
)) {
1185 DEBUG(enumerator
->ctx
, "Matched network %p because of its country code\n", network
);
1190 // Check if the ASN matches
1191 if (enumerator
->asns
&& !loc_as_list_empty(enumerator
->asns
)) {
1192 uint32_t asn
= loc_network_get_asn(network
);
1194 if (loc_as_list_contains_number(enumerator
->asns
, asn
)) {
1195 DEBUG(enumerator
->ctx
, "Matched network %p because of its ASN\n", network
);
1200 // Check if flags match
1201 if (enumerator
->flags
&& loc_network_has_flag(network
, enumerator
->flags
)) {
1202 DEBUG(enumerator
->ctx
, "Matched network %p because of its flags\n", network
);
1210 static int __loc_database_enumerator_next_network(
1211 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
, int filter
) {
1212 // Return top element from the stack
1214 *network
= loc_network_list_pop_first(enumerator
->stack
);
1220 // Return everything if filter isn't enabled, or only return matches
1221 if (!filter
|| loc_database_enumerator_match_network(enumerator
, *network
))
1224 // Throw away anything that doesn't match
1225 loc_network_unref(*network
);
1229 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1230 enumerator
->network_stack_depth
);
1233 while (enumerator
->network_stack_depth
> 0) {
1234 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1236 // Get object from top of the stack
1237 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1239 // Remove the node from the stack if we have already visited it
1240 if (enumerator
->networks_visited
[node
->offset
]) {
1241 enumerator
->network_stack_depth
--;
1245 // Mark the bits on the path correctly
1246 loc_address_set_bit(&enumerator
->network_address
,
1247 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1249 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1250 enumerator
->networks_visited
[node
->offset
]++;
1252 // Pop node from top of the stack
1253 struct loc_database_network_node_v1
* n
=
1254 enumerator
->db
->network_nodes_v1
+ node
->offset
;
1256 // Add edges to stack
1257 int r
= loc_database_enumerator_stack_push_node(enumerator
,
1258 be32toh(n
->one
), 1, node
->depth
+ 1);
1263 r
= loc_database_enumerator_stack_push_node(enumerator
,
1264 be32toh(n
->zero
), 0, node
->depth
+ 1);
1269 // Check if this node is a leaf and has a network object
1270 if (__loc_database_node_is_leaf(n
)) {
1271 off_t network_index
= be32toh(n
->network
);
1273 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1275 // Fetch the network object
1276 r
= loc_database_fetch_network(enumerator
->db
, network
,
1277 &enumerator
->network_address
, node
->depth
, network_index
);
1279 // Break on any errors
1283 // Return all networks when the filter is disabled, or check for match
1284 if (!filter
|| loc_database_enumerator_match_network(enumerator
, *network
))
1287 // Does not seem to be a match, so we cleanup and move on
1288 loc_network_unref(*network
);
1293 // Reached the end of the search
1297 static int __loc_database_enumerator_next_network_flattened(
1298 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1299 // Fetch the next network
1300 int r
= __loc_database_enumerator_next_network(enumerator
, network
, 1);
1304 // End if we could not read another network
1308 struct loc_network
* subnet
= NULL
;
1309 struct loc_network_list
* subnets
;
1311 // Create a list with all subnets
1312 r
= loc_network_list_new(enumerator
->ctx
, &subnets
);
1316 // Search all subnets from the database
1318 // Fetch the next network in line
1319 r
= __loc_database_enumerator_next_network(enumerator
, &subnet
, 0);
1321 loc_network_unref(subnet
);
1322 loc_network_list_unref(subnets
);
1327 // End if we did not receive another subnet
1331 // Collect all subnets in a list
1332 if (loc_network_is_subnet(*network
, subnet
)) {
1333 r
= loc_network_list_push(subnets
, subnet
);
1335 loc_network_unref(subnet
);
1336 loc_network_list_unref(subnets
);
1341 loc_network_unref(subnet
);
1345 // If this is not a subnet, we push it back onto the stack and break
1346 r
= loc_network_list_push(enumerator
->stack
, subnet
);
1348 loc_network_unref(subnet
);
1349 loc_network_list_unref(subnets
);
1354 loc_network_unref(subnet
);
1358 DEBUG(enumerator
->ctx
, "Found %zu subnet(s)\n", loc_network_list_size(subnets
));
1360 // We can abort here if the network has no subnets
1361 if (loc_network_list_empty(subnets
)) {
1362 loc_network_list_unref(subnets
);
1367 // If the network has any subnets, we will break it into smaller parts
1368 // without the subnets.
1369 struct loc_network_list
* excluded
= loc_network_exclude_list(*network
, subnets
);
1371 loc_network_list_unref(subnets
);
1375 // Merge subnets onto the stack
1376 r
= loc_network_list_merge(enumerator
->stack
, subnets
);
1378 loc_network_list_unref(subnets
);
1379 loc_network_list_unref(excluded
);
1384 // Push excluded list onto the stack
1385 r
= loc_network_list_merge(enumerator
->stack
, excluded
);
1387 loc_network_list_unref(subnets
);
1388 loc_network_list_unref(excluded
);
1393 loc_network_list_unref(subnets
);
1394 loc_network_list_unref(excluded
);
1396 // Drop the network and restart the whole process again to pick the next network
1397 loc_network_unref(*network
);
1399 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1403 This function finds all bogons (i.e. gaps) between the input networks
1405 static int __loc_database_enumerator_next_bogon(
1406 struct loc_database_enumerator
* enumerator
, struct loc_network
** bogon
) {
1409 // Return top element from the stack
1411 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1421 struct loc_network
* network
= NULL
;
1422 struct in6_addr
* gap_start
= NULL
;
1423 struct in6_addr gap_end
= IN6ADDR_ANY_INIT
;
1426 r
= __loc_database_enumerator_next_network(enumerator
, &network
, 1);
1430 // We have read the last network
1434 const char* country_code
= loc_network_get_country_code(network
);
1437 Skip anything that does not have a country code
1439 Even if a network is part of the routing table, and the database provides
1440 an ASN, this does not mean that this is a legitimate announcement.
1442 if (country_code
&& !*country_code
) {
1443 loc_network_unref(network
);
1447 // Determine the network family
1448 int family
= loc_network_address_family(network
);
1452 gap_start
= &enumerator
->gap6_start
;
1456 gap_start
= &enumerator
->gap4_start
;
1460 ERROR(enumerator
->ctx
, "Unsupported network family %d\n", family
);
1465 // Search where the gap could end
1466 gap_end
= address_decrement(loc_network_get_first_address(network
));
1469 if (loc_address_cmp(gap_start
, &gap_end
) < 0) {
1470 r
= loc_network_list_summarize(enumerator
->ctx
,
1471 gap_start
, &gap_end
, &enumerator
->stack
);
1473 loc_network_unref(network
);
1478 // The gap now starts after this network
1479 const struct in6_addr
* network_end
= loc_network_get_last_address(network
);
1480 (*gap_start
) = address_increment(network_end
);
1482 loc_network_unref(network
);
1484 // Try to return something
1485 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1494 if (!loc_address_all_zeroes(&enumerator
->gap6_start
)) {
1495 r
= loc_address_reset_last(&gap_end
, AF_INET6
);
1499 if (loc_address_cmp(&enumerator
->gap6_start
, &gap_end
) < 0) {
1500 r
= loc_network_list_summarize(enumerator
->ctx
,
1501 &enumerator
->gap6_start
, &gap_end
, &enumerator
->stack
);
1507 loc_address_reset(&enumerator
->gap6_start
, AF_INET6
);
1510 if (!loc_address_all_zeroes(&enumerator
->gap4_start
)) {
1511 r
= loc_address_reset_last(&gap_end
, AF_INET
);
1515 if (loc_address_cmp(&enumerator
->gap4_start
, &gap_end
) < 0) {
1516 r
= loc_network_list_summarize(enumerator
->ctx
,
1517 &enumerator
->gap4_start
, &gap_end
, &enumerator
->stack
);
1523 loc_address_reset(&enumerator
->gap4_start
, AF_INET
);
1526 // Try to return something
1527 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1532 LOC_EXPORT
int loc_database_enumerator_next_network(
1533 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1534 switch (enumerator
->mode
) {
1535 case LOC_DB_ENUMERATE_NETWORKS
:
1537 if (enumerator
->flatten
)
1538 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1540 return __loc_database_enumerator_next_network(enumerator
, network
, 1);
1542 case LOC_DB_ENUMERATE_BOGONS
:
1543 return __loc_database_enumerator_next_bogon(enumerator
, network
);
1550 LOC_EXPORT
int loc_database_enumerator_next_country(
1551 struct loc_database_enumerator
* enumerator
, struct loc_country
** country
) {
1554 // Do not do anything if not in country mode
1555 if (enumerator
->mode
!= LOC_DB_ENUMERATE_COUNTRIES
)
1558 struct loc_database
* db
= enumerator
->db
;
1560 while (enumerator
->country_index
< db
->countries_count
) {
1561 // Fetch the next country
1562 int r
= loc_database_fetch_country(db
, country
, enumerator
->country_index
++);
1566 // We do not filter here, so it always is a match
1571 enumerator
->country_index
= 0;
1573 // We have searched through all of them