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>
53 struct loc_database_objects
{
66 enum loc_database_version version
;
74 size_t signature1_length
;
76 size_t signature2_length
;
78 struct loc_stringpool
* pool
;
80 // ASes in the database
81 struct loc_database_objects as_objects
;
84 struct loc_database_objects network_node_objects
;
87 struct loc_database_objects network_objects
;
90 struct loc_database_objects country_objects
;
93 #define MAX_STACK_DEPTH 256
95 struct loc_node_stack
{
97 int i
; // Is this node 0 or 1?
101 struct loc_database_enumerator
{
103 struct loc_database
* db
;
104 enum loc_database_enumerator_mode mode
;
109 struct loc_country_list
* countries
;
110 struct loc_as_list
* asns
;
111 enum loc_network_flags flags
;
117 // Index of the AS we are looking at
118 unsigned int as_index
;
120 // Index of the country we are looking at
121 unsigned int country_index
;
124 struct in6_addr network_address
;
125 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
126 int network_stack_depth
;
127 unsigned int* networks_visited
;
129 // For subnet search and bogons
130 struct loc_network_list
* stack
;
131 struct loc_network_list
* subnets
;
134 struct in6_addr gap6_start
;
135 struct in6_addr gap4_start
;
138 #define loc_database_read_object(db, buffer, objects, pos) \
139 __loc_database_read_object(db, buffer, sizeof(*buffer), objects, pos)
142 Reads an object into memory (used when mmap() isn't available)
144 static void* __loc_database_read_object(struct loc_database
* db
, void* buffer
,
145 const size_t length
, const struct loc_database_objects
* objects
, const off_t pos
) {
147 const off_t offset
= pos
* length
;
149 // Map the object first if possible
151 return (uint8_t*)objects
->p
+ offset
;
153 // Otherwise fall back and read the object into the buffer
154 const int fd
= fileno(db
->f
);
157 ssize_t bytes_read
= pread(fd
, buffer
, length
, objects
->offset
+ offset
);
159 // Check if we could read what we wanted
160 if (bytes_read
< (ssize_t
)length
) {
161 ERROR(db
->ctx
, "Error reading object from database: %m\n");
169 static int loc_database_read_magic(struct loc_database
* db
) {
170 struct loc_database_magic magic
;
173 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
175 // Check if we have been able to read enough data
176 if (bytes_read
< sizeof(magic
)) {
177 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
178 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
182 // Compare magic bytes
183 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
184 DEBUG(db
->ctx
, "Magic value matches\n");
187 db
->version
= magic
.version
;
193 ERROR(db
->ctx
, "Unrecognized file type\n");
201 Maps arbitrary objects from the database into memory.
203 static int loc_database_map_objects(struct loc_database
* db
,
204 struct loc_database_objects
* objects
, const size_t size
,
205 off_t offset
, size_t length
) {
209 objects
->offset
= offset
;
210 objects
->length
= length
;
211 objects
->count
= objects
->length
/ size
;
213 // End if there is nothing to map
214 if (!objects
->length
)
217 DEBUG(db
->ctx
, "Mapping %zu object(s) from %jd (%zu bytes)\n",
218 objects
->count
, (intmax_t)objects
->offset
, objects
->length
);
220 // mmap() objects into memory
221 void* p
= mmap(NULL
, objects
->length
, PROT_READ
, MAP_PRIVATE
,
222 fileno(db
->f
), objects
->offset
);
224 if (p
== MAP_FAILED
) {
225 // Ignore if the database wasn't page aligned for this architecture
226 // and fall back to use pread().
227 if (errno
== EINVAL
) {
228 DEBUG(db
->ctx
, "Falling back to pread()\n");
232 ERROR(db
->ctx
, "mmap() failed: %m\n");
242 static int loc_database_unmap_objects(struct loc_database
* db
,
243 struct loc_database_objects
* objects
) {
246 // Nothing to do if nothing was mapped
250 // Call munmap() to free everything
251 r
= munmap(objects
->p
, objects
->length
);
253 ERROR(db
->ctx
, "Could not unmap objects: %m\n");
260 static int loc_database_read_signature(struct loc_database
* db
,
261 char** dst
, char* src
, size_t length
) {
262 // Check for a plausible signature length
263 if (length
> LOC_SIGNATURE_MAX_LENGTH
) {
264 ERROR(db
->ctx
, "Signature too long: %zu\n", length
);
268 DEBUG(db
->ctx
, "Reading signature of %zu bytes\n", length
);
271 *dst
= malloc(length
);
276 memcpy(*dst
, src
, length
);
281 static int loc_database_read_header_v1(struct loc_database
* db
) {
282 struct loc_database_header_v1 header
;
286 size_t size
= fread(&header
, 1, sizeof(header
), db
->f
);
288 if (size
< sizeof(header
)) {
289 ERROR(db
->ctx
, "Could not read enough data for header\n");
295 db
->created_at
= be64toh(header
.created_at
);
296 db
->vendor
= be32toh(header
.vendor
);
297 db
->description
= be32toh(header
.description
);
298 db
->license
= be32toh(header
.license
);
300 db
->signature1_length
= be16toh(header
.signature1_length
);
301 db
->signature2_length
= be16toh(header
.signature2_length
);
304 if (db
->signature1_length
) {
305 r
= loc_database_read_signature(db
, &db
->signature1
,
306 header
.signature1
, db
->signature1_length
);
311 if (db
->signature2_length
) {
312 r
= loc_database_read_signature(db
, &db
->signature2
,
313 header
.signature2
, db
->signature2_length
);
318 // Open the stringpool
319 r
= loc_stringpool_open(db
->ctx
, &db
->pool
, db
->f
,
320 be32toh(header
.pool_length
), be32toh(header
.pool_offset
));
325 r
= loc_database_map_objects(db
, &db
->as_objects
,
326 sizeof(struct loc_database_as_v1
),
327 be32toh(header
.as_offset
),
328 be32toh(header
.as_length
));
333 r
= loc_database_map_objects(db
, &db
->network_node_objects
,
334 sizeof(struct loc_database_network_node_v1
),
335 be32toh(header
.network_tree_offset
),
336 be32toh(header
.network_tree_length
));
341 r
= loc_database_map_objects(db
, &db
->network_objects
,
342 sizeof(struct loc_database_network_v1
),
343 be32toh(header
.network_data_offset
),
344 be32toh(header
.network_data_length
));
349 r
= loc_database_map_objects(db
, &db
->country_objects
,
350 sizeof(struct loc_database_country_v1
),
351 be32toh(header
.countries_offset
),
352 be32toh(header
.countries_length
));
359 static int loc_database_read_header(struct loc_database
* db
) {
360 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
362 switch (db
->version
) {
363 case LOC_DATABASE_VERSION_1
:
364 return loc_database_read_header_v1(db
);
367 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
372 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
373 clock_t start
= clock();
377 // Clone file descriptor
380 ERROR(db
->ctx
, "Could not duplicate file descriptor\n");
384 // Reopen the file so that we can keep our own file handle
385 db
->f
= fdopen(fd
, "r");
387 ERROR(db
->ctx
, "Could not re-open database file\n");
391 // Rewind to the start of the file
395 int r
= loc_database_read_magic(db
);
400 r
= loc_database_read_header(db
);
404 clock_t end
= clock();
406 INFO(db
->ctx
, "Opened database in %.4fms\n",
407 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
412 static void loc_database_free(struct loc_database
* db
) {
413 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
416 loc_database_unmap_objects(db
, &db
->as_objects
);
418 // Remove mapped network sections
419 loc_database_unmap_objects(db
, &db
->network_objects
);
421 // Remove mapped network nodes section
422 loc_database_unmap_objects(db
, &db
->network_node_objects
);
424 // Remove mapped countries section
425 loc_database_unmap_objects(db
, &db
->country_objects
);
427 // Free the stringpool
429 loc_stringpool_unref(db
->pool
);
433 free(db
->signature1
);
435 free(db
->signature2
);
437 // Close database file
445 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
446 struct loc_database
* db
= NULL
;
449 // Fail on invalid file handle
455 // Allocate the database object
456 db
= calloc(1, sizeof(*db
));
461 db
->ctx
= loc_ref(ctx
);
464 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
466 // Try to open the database
467 r
= loc_database_read(db
, f
);
476 loc_database_free(db
);
481 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
487 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
488 if (--db
->refcount
> 0)
491 loc_database_free(db
);
495 LOC_EXPORT
int loc_database_verify(struct loc_database
* db
, FILE* f
) {
496 size_t bytes_read
= 0;
498 // Cannot do this when no signature is available
499 if (!db
->signature1
&& !db
->signature2
) {
500 DEBUG(db
->ctx
, "No signature available to verify\n");
504 // Start the stopwatch
505 clock_t start
= clock();
508 EVP_PKEY
* pkey
= PEM_read_PUBKEY(f
, NULL
, NULL
, NULL
);
510 ERROR(db
->ctx
, "Could not parse public key: %s\n",
511 ERR_error_string(ERR_get_error(), NULL
));
518 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
520 // Initialise hash function
521 r
= EVP_DigestVerifyInit(mdctx
, NULL
, NULL
, NULL
, pkey
);
523 ERROR(db
->ctx
, "Error initializing signature validation: %s\n",
524 ERR_error_string(ERR_get_error(), NULL
));
530 // Reset file to start
534 struct loc_database_magic magic
;
535 bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->f
);
536 if (bytes_read
< sizeof(magic
)) {
537 ERROR(db
->ctx
, "Could not read header: %m\n");
542 hexdump(db
->ctx
, &magic
, sizeof(magic
));
544 // Feed magic into the hash
545 r
= EVP_DigestVerifyUpdate(mdctx
, &magic
, sizeof(magic
));
547 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
554 struct loc_database_header_v1 header_v1
;
556 switch (db
->version
) {
557 case LOC_DATABASE_VERSION_1
:
558 bytes_read
= fread(&header_v1
, 1, sizeof(header_v1
), db
->f
);
559 if (bytes_read
< sizeof(header_v1
)) {
560 ERROR(db
->ctx
, "Could not read header\n");
567 memset(header_v1
.signature1
, '\0', sizeof(header_v1
.signature1
));
568 header_v1
.signature1_length
= 0;
569 memset(header_v1
.signature2
, '\0', sizeof(header_v1
.signature2
));
570 header_v1
.signature2_length
= 0;
572 hexdump(db
->ctx
, &header_v1
, sizeof(header_v1
));
574 // Feed header into the hash
575 r
= EVP_DigestVerifyUpdate(mdctx
, &header_v1
, sizeof(header_v1
));
577 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
585 ERROR(db
->ctx
, "Cannot compute hash for database with format %d\n",
591 // Walk through the file in chunks of 64kB
592 char buffer
[64 * 1024];
594 while (!feof(db
->f
)) {
595 bytes_read
= fread(buffer
, 1, sizeof(buffer
), db
->f
);
597 hexdump(db
->ctx
, buffer
, bytes_read
);
599 r
= EVP_DigestVerifyUpdate(mdctx
, buffer
, bytes_read
);
601 ERROR(db
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
608 // Check first signature
609 if (db
->signature1
) {
610 hexdump(db
->ctx
, db
->signature1
, db
->signature1_length
);
612 r
= EVP_DigestVerifyFinal(mdctx
,
613 (unsigned char*)db
->signature1
, db
->signature1_length
);
616 DEBUG(db
->ctx
, "The first signature is invalid\n");
619 DEBUG(db
->ctx
, "The first signature is valid\n");
622 ERROR(db
->ctx
, "Error verifying the first signature: %s\n",
623 ERR_error_string(ERR_get_error(), NULL
));
628 // Check second signature only when the first one was invalid
629 if (r
&& db
->signature2
) {
630 hexdump(db
->ctx
, db
->signature2
, db
->signature2_length
);
632 r
= EVP_DigestVerifyFinal(mdctx
,
633 (unsigned char*)db
->signature2
, db
->signature2_length
);
636 DEBUG(db
->ctx
, "The second signature is invalid\n");
639 DEBUG(db
->ctx
, "The second signature is valid\n");
642 ERROR(db
->ctx
, "Error verifying the second signature: %s\n",
643 ERR_error_string(ERR_get_error(), NULL
));
648 clock_t end
= clock();
649 INFO(db
->ctx
, "Signature checked in %.4fms\n",
650 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
654 EVP_MD_CTX_free(mdctx
);
660 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
661 return db
->created_at
;
664 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
665 return loc_stringpool_get(db
->pool
, db
->vendor
);
668 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
669 return loc_stringpool_get(db
->pool
, db
->description
);
672 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
673 return loc_stringpool_get(db
->pool
, db
->license
);
676 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
677 return db
->as_objects
.count
;
680 // Returns the AS at position pos
681 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
682 struct loc_database_as_v1 as_v1
;
683 struct loc_database_as_v1
* p_v1
;
686 if ((size_t)pos
>= db
->as_objects
.count
) {
691 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", (intmax_t)pos
);
693 switch (db
->version
) {
694 case LOC_DATABASE_VERSION_1
:
696 p_v1
= loc_database_read_object(db
, &as_v1
, &db
->as_objects
, pos
);
700 r
= loc_as_new_from_database_v1(db
->ctx
, db
->pool
, as
, p_v1
);
709 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
714 // Performs a binary search to find the AS in the list
715 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
717 off_t hi
= db
->as_objects
.count
- 1;
721 clock_t start
= clock();
725 off_t i
= (lo
+ hi
) / 2;
727 // Fetch AS in the middle between lo and hi
728 int r
= loc_database_fetch_as(db
, as
, i
);
732 // Check if this is a match
733 uint32_t as_number
= loc_as_get_number(*as
);
734 if (as_number
== number
) {
736 clock_t end
= clock();
738 // Log how fast this has been
739 DEBUG(db
->ctx
, "Found AS%u in %.4fms\n", as_number
,
740 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
746 // If it wasn't, we release the AS and
747 // adjust our search pointers
750 if (as_number
< number
) {
762 // Returns the network at position pos
763 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
764 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
765 struct loc_database_network_v1 network_v1
;
766 struct loc_database_network_v1
* p_v1
;
769 if ((size_t)pos
>= db
->network_objects
.count
) {
770 DEBUG(db
->ctx
, "Network ID out of range: %jd/%jd\n",
771 (intmax_t)pos
, (intmax_t)db
->network_objects
.count
);
776 DEBUG(db
->ctx
, "Fetching network at position %jd\n", (intmax_t)pos
);
778 switch (db
->version
) {
779 case LOC_DATABASE_VERSION_1
:
781 p_v1
= loc_database_read_object(db
, &network_v1
, &db
->network_objects
, pos
);
785 r
= loc_network_new_from_database_v1(db
->ctx
, network
, address
, prefix
, p_v1
);
794 DEBUG(db
->ctx
, "Got network %s\n", loc_network_str(*network
));
799 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1
* node
) {
800 return (node
->network
!= htobe32(0xffffffff));
803 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
804 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
805 const struct loc_database_network_node_v1
* node
) {
806 off_t network_index
= be32toh(node
->network
);
808 DEBUG(db
->ctx
, "Handling leaf node at %jd\n", (intmax_t)network_index
);
811 int r
= loc_database_fetch_network(db
, network
, network_address
, prefix
, network_index
);
813 ERROR(db
->ctx
, "Could not fetch network %jd from database: %m\n",
814 (intmax_t)network_index
);
818 // Check if the given IP address is inside the network
819 if (!loc_network_matches_address(*network
, address
)) {
820 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
822 loc_network_unref(*network
);
827 // A network was found and the IP address matches
831 // Searches for an exact match along the path
832 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
833 struct loc_network
** network
, struct in6_addr
* network_address
,
834 off_t node_index
, unsigned int level
) {
835 struct loc_database_network_node_v1 node_v1
;
836 struct loc_database_network_node_v1
* p_v1
;
840 // Fetch the next node
841 p_v1
= loc_database_read_object(db
, &node_v1
, &db
->network_node_objects
, node_index
);
846 int bit
= loc_address_get_bit(address
, level
);
847 loc_address_set_bit(network_address
, level
, bit
);
850 node_index
= be32toh(p_v1
->zero
);
852 node_index
= be32toh(p_v1
->one
);
854 // If the node index is zero, the tree ends here
855 // and we cannot descend any further
856 if (node_index
> 0) {
858 if ((size_t)node_index
>= db
->network_node_objects
.count
) {
863 // Move on to the next node
864 r
= __loc_database_lookup(db
, address
, network
, network_address
, node_index
, level
+ 1);
866 // End here if a result was found
874 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
876 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
879 // If this node has a leaf, we will check if it matches
880 if (__loc_database_node_is_leaf(p_v1
)) {
881 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, p_v1
);
889 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
890 const struct in6_addr
* address
, struct loc_network
** network
) {
891 struct in6_addr network_address
;
892 memset(&network_address
, 0, sizeof(network_address
));
898 clock_t start
= clock();
901 int r
= __loc_database_lookup(db
, address
, network
, &network_address
, 0, 0);
904 clock_t end
= clock();
906 // Log how fast this has been
907 DEBUG(db
->ctx
, "Executed network search in %.4fms\n",
908 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
914 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
915 const char* string
, struct loc_network
** network
) {
916 struct in6_addr address
;
918 int r
= loc_address_parse(&address
, NULL
, string
);
922 return loc_database_lookup(db
, &address
, network
);
925 // Returns the country at position pos
926 static int loc_database_fetch_country(struct loc_database
* db
,
927 struct loc_country
** country
, off_t pos
) {
928 struct loc_database_country_v1 country_v1
;
929 struct loc_database_country_v1
* p_v1
;
932 // Check if the country is within range
933 if ((size_t)pos
>= db
->country_objects
.count
) {
938 DEBUG(db
->ctx
, "Fetching country at position %jd\n", (intmax_t)pos
);
940 switch (db
->version
) {
941 case LOC_DATABASE_VERSION_1
:
943 p_v1
= loc_database_read_object(db
, &country_v1
, &db
->country_objects
, pos
);
947 r
= loc_country_new_from_database_v1(db
->ctx
, db
->pool
, country
, p_v1
);
956 DEBUG(db
->ctx
, "Got country %s\n", loc_country_get_code(*country
));
961 // Performs a binary search to find the country in the list
962 LOC_EXPORT
int loc_database_get_country(struct loc_database
* db
,
963 struct loc_country
** country
, const char* code
) {
965 off_t hi
= db
->country_objects
.count
- 1;
967 // Check if the country code is valid
968 if (!loc_country_code_is_valid(code
)) {
975 clock_t start
= clock();
979 off_t i
= (lo
+ hi
) / 2;
981 // Fetch country in the middle between lo and hi
982 int r
= loc_database_fetch_country(db
, country
, i
);
986 // Check if this is a match
987 const char* cc
= loc_country_get_code(*country
);
988 int result
= strcmp(code
, cc
);
992 clock_t end
= clock();
994 // Log how fast this has been
995 DEBUG(db
->ctx
, "Found country %s in %.4fms\n", cc
,
996 (double)(end
- start
) / CLOCKS_PER_SEC
* 1000);
1002 // If it wasn't, we release the country and
1003 // adjust our search pointers
1004 loc_country_unref(*country
);
1020 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
1021 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
1023 // Release all references
1024 loc_database_unref(enumerator
->db
);
1025 loc_unref(enumerator
->ctx
);
1027 if (enumerator
->string
)
1028 free(enumerator
->string
);
1030 if (enumerator
->countries
)
1031 loc_country_list_unref(enumerator
->countries
);
1033 if (enumerator
->asns
)
1034 loc_as_list_unref(enumerator
->asns
);
1036 // Free network search
1037 if (enumerator
->networks_visited
)
1038 free(enumerator
->networks_visited
);
1040 // Free subnet/bogons stack
1041 if (enumerator
->stack
)
1042 loc_network_list_unref(enumerator
->stack
);
1044 if (enumerator
->subnets
)
1045 loc_network_list_unref(enumerator
->subnets
);
1050 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
1051 struct loc_database
* db
, enum loc_database_enumerator_mode mode
, int flags
) {
1054 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
1059 // Reference context
1060 e
->ctx
= loc_ref(db
->ctx
);
1061 e
->db
= loc_database_ref(db
);
1066 e
->flatten
= (flags
& LOC_DB_ENUMERATOR_FLAGS_FLATTEN
);
1068 // Initialise graph search
1069 e
->network_stack_depth
= 1;
1070 e
->networks_visited
= calloc(db
->network_node_objects
.count
, sizeof(*e
->networks_visited
));
1071 printf("COUNT = %zu, P = %p\n", db
->network_node_objects
.count
, e
->networks_visited
);
1072 if (!e
->networks_visited
) {
1073 ERROR(db
->ctx
, "Could not allocated visited networks: %m\n");
1079 r
= loc_network_list_new(e
->ctx
, &e
->stack
);
1083 // Initialize bogon search
1084 loc_address_reset(&e
->gap6_start
, AF_INET6
);
1085 loc_address_reset(&e
->gap4_start
, AF_INET
);
1087 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
1094 loc_database_enumerator_free(e
);
1099 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
1100 enumerator
->refcount
++;
1105 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
1109 if (--enumerator
->refcount
> 0)
1112 loc_database_enumerator_free(enumerator
);
1116 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
1117 enumerator
->string
= strdup(string
);
1119 // Make the string lowercase
1120 for (char *p
= enumerator
->string
; *p
; p
++)
1126 LOC_EXPORT
struct loc_country_list
* loc_database_enumerator_get_countries(
1127 struct loc_database_enumerator
* enumerator
) {
1128 if (!enumerator
->countries
)
1131 return loc_country_list_ref(enumerator
->countries
);
1134 LOC_EXPORT
int loc_database_enumerator_set_countries(
1135 struct loc_database_enumerator
* enumerator
, struct loc_country_list
* countries
) {
1136 if (enumerator
->countries
)
1137 loc_country_list_unref(enumerator
->countries
);
1139 enumerator
->countries
= loc_country_list_ref(countries
);
1144 LOC_EXPORT
struct loc_as_list
* loc_database_enumerator_get_asns(
1145 struct loc_database_enumerator
* enumerator
) {
1146 if (!enumerator
->asns
)
1149 return loc_as_list_ref(enumerator
->asns
);
1152 LOC_EXPORT
int loc_database_enumerator_set_asns(
1153 struct loc_database_enumerator
* enumerator
, struct loc_as_list
* asns
) {
1154 if (enumerator
->asns
)
1155 loc_as_list_unref(enumerator
->asns
);
1157 enumerator
->asns
= loc_as_list_ref(asns
);
1162 LOC_EXPORT
int loc_database_enumerator_set_flag(
1163 struct loc_database_enumerator
* enumerator
, enum loc_network_flags flag
) {
1164 enumerator
->flags
|= flag
;
1169 LOC_EXPORT
int loc_database_enumerator_set_family(
1170 struct loc_database_enumerator
* enumerator
, int family
) {
1171 enumerator
->family
= family
;
1176 LOC_EXPORT
int loc_database_enumerator_next_as(
1177 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
1180 // Do not do anything if not in AS mode
1181 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
1184 struct loc_database
* db
= enumerator
->db
;
1186 while (enumerator
->as_index
< db
->as_objects
.count
) {
1187 // Fetch the next AS
1188 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
1192 r
= loc_as_match_string(*as
, enumerator
->string
);
1194 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
1195 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
1206 enumerator
->as_index
= 0;
1208 // We have searched through all of them
1212 static int loc_database_enumerator_stack_push_node(
1213 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
1214 // Do not add empty nodes
1218 // Check if there is any space left on the stack
1219 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
1220 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
1224 // Check if the node is in range
1225 if (offset
>= (off_t
)e
->db
->network_node_objects
.count
) {
1226 ERROR(e
->ctx
, "Trying to add invalid node with offset %jd/%zu\n",
1227 offset
, e
->db
->network_node_objects
.count
);
1232 // Increase stack size
1233 int s
= ++e
->network_stack_depth
;
1235 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", (intmax_t)offset
, depth
);
1237 e
->network_stack
[s
].offset
= offset
;
1238 e
->network_stack
[s
].i
= i
;
1239 e
->network_stack
[s
].depth
= depth
;
1244 static int loc_database_enumerator_match_network(
1245 struct loc_database_enumerator
* enumerator
, struct loc_network
* network
) {
1246 // If family is set, it must match
1247 if (enumerator
->family
&& loc_network_address_family(network
) != enumerator
->family
) {
1248 DEBUG(enumerator
->ctx
, "Filtered network %p because of family not matching\n", network
);
1252 // Match if no filter criteria is configured
1253 if (!enumerator
->countries
&& !enumerator
->asns
&& !enumerator
->flags
)
1256 // Check if the country code matches
1257 if (enumerator
->countries
&& !loc_country_list_empty(enumerator
->countries
)) {
1258 const char* country_code
= loc_network_get_country_code(network
);
1260 if (loc_country_list_contains_code(enumerator
->countries
, country_code
)) {
1261 DEBUG(enumerator
->ctx
, "Matched network %p because of its country code\n", network
);
1266 // Check if the ASN matches
1267 if (enumerator
->asns
&& !loc_as_list_empty(enumerator
->asns
)) {
1268 uint32_t asn
= loc_network_get_asn(network
);
1270 if (loc_as_list_contains_number(enumerator
->asns
, asn
)) {
1271 DEBUG(enumerator
->ctx
, "Matched network %p because of its ASN\n", network
);
1276 // Check if flags match
1277 if (enumerator
->flags
&& loc_network_has_flag(network
, enumerator
->flags
)) {
1278 DEBUG(enumerator
->ctx
, "Matched network %p because of its flags\n", network
);
1286 static int __loc_database_enumerator_next_network(
1287 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
, int filter
) {
1288 struct loc_database_network_node_v1 node_v1
;
1290 // Return top element from the stack
1292 *network
= loc_network_list_pop_first(enumerator
->stack
);
1298 // Return everything if filter isn't enabled, or only return matches
1299 if (!filter
|| loc_database_enumerator_match_network(enumerator
, *network
))
1302 // Throw away anything that doesn't match
1303 loc_network_unref(*network
);
1307 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
1308 enumerator
->network_stack_depth
);
1311 while (enumerator
->network_stack_depth
> 0) {
1312 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
1314 // Get object from top of the stack
1315 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
1317 DEBUG(enumerator
->ctx
, " Got node: %jd\n", node
->offset
);
1319 // Remove the node from the stack if we have already visited it
1320 if (enumerator
->networks_visited
[node
->offset
]) {
1321 enumerator
->network_stack_depth
--;
1325 // Mark the bits on the path correctly
1326 loc_address_set_bit(&enumerator
->network_address
,
1327 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
1329 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", (intmax_t)node
->offset
);
1330 enumerator
->networks_visited
[node
->offset
]++;
1332 // Pop node from top of the stack
1333 struct loc_database_network_node_v1
* n
= loc_database_read_object(enumerator
->db
,
1334 &node_v1
, &enumerator
->db
->network_node_objects
, node
->offset
);
1338 // Add edges to stack
1339 int r
= loc_database_enumerator_stack_push_node(enumerator
,
1340 be32toh(n
->one
), 1, node
->depth
+ 1);
1344 r
= loc_database_enumerator_stack_push_node(enumerator
,
1345 be32toh(n
->zero
), 0, node
->depth
+ 1);
1349 // Check if this node is a leaf and has a network object
1350 if (__loc_database_node_is_leaf(n
)) {
1351 off_t network_index
= be32toh(n
->network
);
1353 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", (intmax_t)network_index
);
1355 // Fetch the network object
1356 r
= loc_database_fetch_network(enumerator
->db
, network
,
1357 &enumerator
->network_address
, node
->depth
, network_index
);
1359 // Break on any errors
1363 // Return all networks when the filter is disabled, or check for match
1364 if (!filter
|| loc_database_enumerator_match_network(enumerator
, *network
))
1367 // Does not seem to be a match, so we cleanup and move on
1368 loc_network_unref(*network
);
1373 // Reached the end of the search
1377 static int __loc_database_enumerator_next_network_flattened(
1378 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1379 // Fetch the next network
1380 int r
= __loc_database_enumerator_next_network(enumerator
, network
, 1);
1384 // End if we could not read another network
1388 struct loc_network
* subnet
= NULL
;
1390 // Create a list with all subnets
1391 if (!enumerator
->subnets
) {
1392 r
= loc_network_list_new(enumerator
->ctx
, &enumerator
->subnets
);
1397 // Search all subnets from the database
1399 // Fetch the next network in line
1400 r
= __loc_database_enumerator_next_network(enumerator
, &subnet
, 0);
1402 loc_network_unref(subnet
);
1403 loc_network_list_clear(enumerator
->subnets
);
1408 // End if we did not receive another subnet
1412 // Collect all subnets in a list
1413 if (loc_network_is_subnet(*network
, subnet
)) {
1414 r
= loc_network_list_push(enumerator
->subnets
, subnet
);
1416 loc_network_unref(subnet
);
1417 loc_network_list_clear(enumerator
->subnets
);
1422 loc_network_unref(subnet
);
1426 // If this is not a subnet, we push it back onto the stack and break
1427 r
= loc_network_list_push(enumerator
->stack
, subnet
);
1429 loc_network_unref(subnet
);
1430 loc_network_list_clear(enumerator
->subnets
);
1435 loc_network_unref(subnet
);
1439 DEBUG(enumerator
->ctx
, "Found %zu subnet(s)\n",
1440 loc_network_list_size(enumerator
->subnets
));
1442 // We can abort here if the network has no subnets
1443 if (loc_network_list_empty(enumerator
->subnets
)) {
1444 loc_network_list_clear(enumerator
->subnets
);
1449 // If the network has any subnets, we will break it into smaller parts
1450 // without the subnets.
1451 struct loc_network_list
* excluded
= loc_network_exclude_list(*network
, enumerator
->subnets
);
1453 loc_network_list_clear(enumerator
->subnets
);
1457 // Merge subnets onto the stack
1458 r
= loc_network_list_merge(enumerator
->stack
, enumerator
->subnets
);
1460 loc_network_list_clear(enumerator
->subnets
);
1461 loc_network_list_unref(excluded
);
1466 // Push excluded list onto the stack
1467 r
= loc_network_list_merge(enumerator
->stack
, excluded
);
1469 loc_network_list_clear(enumerator
->subnets
);
1470 loc_network_list_unref(excluded
);
1475 loc_network_list_clear(enumerator
->subnets
);
1476 loc_network_list_unref(excluded
);
1478 // Drop the network and restart the whole process again to pick the next network
1479 loc_network_unref(*network
);
1481 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1485 This function finds all bogons (i.e. gaps) between the input networks
1487 static int __loc_database_enumerator_next_bogon(
1488 struct loc_database_enumerator
* enumerator
, struct loc_network
** bogon
) {
1491 // Return top element from the stack
1493 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1503 struct loc_network
* network
= NULL
;
1504 struct in6_addr
* gap_start
= NULL
;
1505 struct in6_addr gap_end
= IN6ADDR_ANY_INIT
;
1508 r
= __loc_database_enumerator_next_network(enumerator
, &network
, 1);
1512 // We have read the last network
1516 const char* country_code
= loc_network_get_country_code(network
);
1519 Skip anything that does not have a country code
1521 Even if a network is part of the routing table, and the database provides
1522 an ASN, this does not mean that this is a legitimate announcement.
1524 if (country_code
&& !*country_code
) {
1525 loc_network_unref(network
);
1529 // Determine the network family
1530 int family
= loc_network_address_family(network
);
1534 gap_start
= &enumerator
->gap6_start
;
1538 gap_start
= &enumerator
->gap4_start
;
1542 ERROR(enumerator
->ctx
, "Unsupported network family %d\n", family
);
1547 const struct in6_addr
* first_address
= loc_network_get_first_address(network
);
1548 const struct in6_addr
* last_address
= loc_network_get_last_address(network
);
1550 // Skip if this network is a subnet of a former one
1551 if (loc_address_cmp(gap_start
, last_address
) >= 0) {
1552 loc_network_unref(network
);
1556 // Search where the gap could end
1557 gap_end
= *first_address
;
1558 loc_address_decrement(&gap_end
);
1561 if (loc_address_cmp(gap_start
, &gap_end
) <= 0) {
1562 r
= loc_network_list_summarize(enumerator
->ctx
,
1563 gap_start
, &gap_end
, &enumerator
->stack
);
1565 loc_network_unref(network
);
1570 // The gap now starts after this network
1571 *gap_start
= *last_address
;
1572 loc_address_increment(gap_start
);
1574 loc_network_unref(network
);
1576 // Try to return something
1577 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1586 if (!loc_address_all_zeroes(&enumerator
->gap6_start
)) {
1587 r
= loc_address_reset_last(&gap_end
, AF_INET6
);
1591 if (loc_address_cmp(&enumerator
->gap6_start
, &gap_end
) <= 0) {
1592 r
= loc_network_list_summarize(enumerator
->ctx
,
1593 &enumerator
->gap6_start
, &gap_end
, &enumerator
->stack
);
1599 loc_address_reset(&enumerator
->gap6_start
, AF_INET6
);
1602 if (!loc_address_all_zeroes(&enumerator
->gap4_start
)) {
1603 r
= loc_address_reset_last(&gap_end
, AF_INET
);
1607 if (loc_address_cmp(&enumerator
->gap4_start
, &gap_end
) <= 0) {
1608 r
= loc_network_list_summarize(enumerator
->ctx
,
1609 &enumerator
->gap4_start
, &gap_end
, &enumerator
->stack
);
1615 loc_address_reset(&enumerator
->gap4_start
, AF_INET
);
1618 // Try to return something
1619 *bogon
= loc_network_list_pop_first(enumerator
->stack
);
1624 LOC_EXPORT
int loc_database_enumerator_next_network(
1625 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
1626 switch (enumerator
->mode
) {
1627 case LOC_DB_ENUMERATE_NETWORKS
:
1629 if (enumerator
->flatten
)
1630 return __loc_database_enumerator_next_network_flattened(enumerator
, network
);
1632 return __loc_database_enumerator_next_network(enumerator
, network
, 1);
1634 case LOC_DB_ENUMERATE_BOGONS
:
1635 return __loc_database_enumerator_next_bogon(enumerator
, network
);
1642 LOC_EXPORT
int loc_database_enumerator_next_country(
1643 struct loc_database_enumerator
* enumerator
, struct loc_country
** country
) {
1646 // Do not do anything if not in country mode
1647 if (enumerator
->mode
!= LOC_DB_ENUMERATE_COUNTRIES
)
1650 struct loc_database
* db
= enumerator
->db
;
1652 while (enumerator
->country_index
< db
->country_objects
.count
) {
1653 // Fetch the next country
1654 int r
= loc_database_fetch_country(db
, country
, enumerator
->country_index
++);
1658 // We do not filter here, so it always is a match
1663 enumerator
->country_index
= 0;
1665 // We have searched through all of them