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>
21 #include <netinet/in.h>
28 #include <sys/types.h>
32 #include <loc/libloc.h>
34 #include <loc/database.h>
35 #include <loc/format.h>
36 #include <loc/network.h>
37 #include <loc/private.h>
38 #include <loc/stringpool.h>
50 // ASes in the database
51 struct loc_database_as_v0
* as_v0
;
55 struct loc_database_network_node_v0
* network_nodes_v0
;
56 size_t network_nodes_count
;
59 struct loc_database_network_v0
* networks_v0
;
60 size_t networks_count
;
62 struct loc_stringpool
* pool
;
65 #define MAX_STACK_DEPTH 256
67 struct loc_node_stack
{
69 int i
; // Is this node 0 or 1?
73 struct loc_database_enumerator
{
75 struct loc_database
* db
;
76 enum loc_database_enumerator_mode mode
;
84 // Index of the AS we are looking at
85 unsigned int as_index
;
88 struct in6_addr network_address
;
89 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
90 int network_stack_depth
;
91 unsigned int* networks_visited
;
94 static int loc_database_read_magic(struct loc_database
* db
, FILE* f
) {
95 struct loc_database_magic magic
;
98 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), f
);
100 // Check if we have been able to read enough data
101 if (bytes_read
< sizeof(magic
)) {
102 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
103 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
107 // Compare magic bytes
108 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
109 DEBUG(db
->ctx
, "Magic value matches\n");
112 db
->version
= be16toh(magic
.version
);
113 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
118 ERROR(db
->ctx
, "Database format is not compatible\n");
124 static int loc_database_read_as_section_v0(struct loc_database
* db
,
125 FILE* f
, const struct loc_database_header_v0
* header
) {
126 off_t as_offset
= be32toh(header
->as_offset
);
127 size_t as_length
= be32toh(header
->as_length
);
129 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", as_offset
, as_length
);
132 db
->as_v0
= mmap(NULL
, as_length
, PROT_READ
,
133 MAP_SHARED
, fileno(f
), as_offset
);
135 if (db
->as_v0
== MAP_FAILED
)
139 db
->as_count
= as_length
/ sizeof(*db
->as_v0
);
141 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
146 static int loc_database_read_network_nodes_section_v0(struct loc_database
* db
,
147 FILE* f
, const struct loc_database_header_v0
* header
) {
148 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
149 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
151 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
152 network_nodes_offset
, network_nodes_length
);
154 if (network_nodes_length
> 0) {
155 db
->network_nodes_v0
= mmap(NULL
, network_nodes_length
, PROT_READ
,
156 MAP_SHARED
, fileno(f
), network_nodes_offset
);
158 if (db
->network_nodes_v0
== MAP_FAILED
)
162 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v0
);
164 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
169 static int loc_database_read_networks_section_v0(struct loc_database
* db
,
170 FILE* f
, const struct loc_database_header_v0
* header
) {
171 off_t networks_offset
= be32toh(header
->network_data_offset
);
172 size_t networks_length
= be32toh(header
->network_data_length
);
174 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
175 networks_offset
, networks_length
);
177 if (networks_length
> 0) {
178 db
->networks_v0
= mmap(NULL
, networks_length
, PROT_READ
,
179 MAP_SHARED
, fileno(f
), networks_offset
);
181 if (db
->networks_v0
== MAP_FAILED
)
185 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v0
);
187 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
192 static int loc_database_read_header_v0(struct loc_database
* db
, FILE* f
) {
193 struct loc_database_header_v0 header
;
196 size_t size
= fread(&header
, 1, sizeof(header
), f
);
198 if (size
< sizeof(header
)) {
199 ERROR(db
->ctx
, "Could not read enough data for header\n");
204 db
->created_at
= be64toh(header
.created_at
);
205 db
->vendor
= be32toh(header
.vendor
);
206 db
->description
= be32toh(header
.description
);
207 db
->license
= be32toh(header
.license
);
210 off_t pool_offset
= be32toh(header
.pool_offset
);
211 size_t pool_length
= be32toh(header
.pool_length
);
213 int r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
214 f
, pool_length
, pool_offset
);
219 r
= loc_database_read_as_section_v0(db
, f
, &header
);
224 r
= loc_database_read_network_nodes_section_v0(db
, f
, &header
);
229 r
= loc_database_read_networks_section_v0(db
, f
, &header
);
236 static int loc_database_read_header(struct loc_database
* db
, FILE* f
) {
237 switch (db
->version
) {
239 return loc_database_read_header_v0(db
, f
);
242 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
247 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
248 clock_t start
= clock();
251 int r
= loc_database_read_magic(db
, f
);
256 r
= loc_database_read_header(db
, f
);
260 clock_t end
= clock();
262 INFO(db
->ctx
, "Opened database in %.8fs\n",
263 (double)(end
- start
) / CLOCKS_PER_SEC
);
268 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
269 // Fail on invalid file handle
273 struct loc_database
* db
= calloc(1, sizeof(*db
));
278 db
->ctx
= loc_ref(ctx
);
281 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
283 int r
= loc_database_read(db
, f
);
285 loc_database_unref(db
);
294 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
300 static void loc_database_free(struct loc_database
* db
) {
303 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
307 r
= munmap(db
->as_v0
, db
->as_count
* sizeof(*db
->as_v0
));
309 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
312 // Remove mapped network sections
313 if (db
->networks_v0
) {
314 r
= munmap(db
->networks_v0
, db
->networks_count
* sizeof(*db
->networks_v0
));
316 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
319 // Remove mapped network nodes section
320 if (db
->network_nodes_v0
) {
321 r
= munmap(db
->network_nodes_v0
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v0
));
323 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
326 loc_stringpool_unref(db
->pool
);
332 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
333 if (--db
->refcount
> 0)
336 loc_database_free(db
);
340 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
341 return db
->created_at
;
344 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
345 return loc_stringpool_get(db
->pool
, db
->vendor
);
348 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
349 return loc_stringpool_get(db
->pool
, db
->description
);
352 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
353 return loc_stringpool_get(db
->pool
, db
->license
);
356 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
360 // Returns the AS at position pos
361 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
362 if ((size_t)pos
>= db
->as_count
)
365 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", pos
);
368 switch (db
->version
) {
370 r
= loc_as_new_from_database_v0(db
->ctx
, db
->pool
, as
, db
->as_v0
+ pos
);
378 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
384 // Performs a binary search to find the AS in the list
385 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
387 off_t hi
= db
->as_count
- 1;
390 clock_t start
= clock();
393 off_t i
= (lo
+ hi
) / 2;
395 // Fetch AS in the middle between lo and hi
396 int r
= loc_database_fetch_as(db
, as
, i
);
400 // Check if this is a match
401 uint32_t as_number
= loc_as_get_number(*as
);
402 if (as_number
== number
) {
403 clock_t end
= clock();
405 // Log how fast this has been
406 DEBUG(db
->ctx
, "Found AS%u in %.8fs\n", as_number
,
407 (double)(end
- start
) / CLOCKS_PER_SEC
);
412 // If it wasn't, we release the AS and
413 // adjust our search pointers
416 if (as_number
< number
) {
428 // Returns the network at position pos
429 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
430 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
431 if ((size_t)pos
>= db
->networks_count
)
434 DEBUG(db
->ctx
, "Fetching network at position %jd\n", pos
);
437 switch (db
->version
) {
439 r
= loc_network_new_from_database_v0(db
->ctx
, network
,
440 address
, prefix
, db
->networks_v0
+ pos
);
448 char* string
= loc_network_str(*network
);
449 DEBUG(db
->ctx
, "Got network %s\n", string
);
456 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v0
* node
) {
457 return (node
->network
!= htobe32(0xffffffff));
460 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
461 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
462 const struct loc_database_network_node_v0
* node
) {
463 off_t network_index
= be32toh(node
->network
);
465 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", node
- db
->network_nodes_v0
, network_index
);
468 int r
= loc_database_fetch_network(db
, network
,
469 network_address
, prefix
, network_index
);
471 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", network_index
);
475 // Check if the given IP address is inside the network
476 r
= loc_network_match_address(*network
, address
);
478 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
480 loc_network_unref(*network
);
485 // A network was found and the IP address matches
489 // Searches for an exact match along the path
490 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
491 struct loc_network
** network
, struct in6_addr
* network_address
,
492 const struct loc_database_network_node_v0
* node
, unsigned int level
) {
497 int bit
= in6_addr_get_bit(address
, level
);
498 in6_addr_set_bit(network_address
, level
, bit
);
501 node_index
= be32toh(node
->zero
);
503 node_index
= be32toh(node
->one
);
505 // If the node index is zero, the tree ends here
506 // and we cannot descend any further
507 if (node_index
> 0) {
509 if ((size_t)node_index
>= db
->network_nodes_count
)
512 // Move on to the next node
513 r
= __loc_database_lookup(db
, address
, network
, network_address
,
514 db
->network_nodes_v0
+ node_index
, level
+ 1);
516 // End here if a result was found
524 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
526 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
529 // If this node has a leaf, we will check if it matches
530 if (__loc_database_node_is_leaf(node
)) {
531 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
539 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
540 struct in6_addr
* address
, struct loc_network
** network
) {
541 struct in6_addr network_address
;
542 memset(&network_address
, 0, sizeof(network_address
));
547 clock_t start
= clock();
549 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
550 db
->network_nodes_v0
, 0);
552 clock_t end
= clock();
554 // Log how fast this has been
555 DEBUG(db
->ctx
, "Executed network search in %.8fs\n",
556 (double)(end
- start
) / CLOCKS_PER_SEC
);
561 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
562 const char* string
, struct loc_network
** network
) {
563 struct in6_addr address
;
565 int r
= loc_parse_address(db
->ctx
, string
, &address
);
569 return loc_database_lookup(db
, &address
, network
);
574 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
,
575 struct loc_database
* db
, enum loc_database_enumerator_mode mode
) {
576 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
581 e
->ctx
= loc_ref(db
->ctx
);
582 e
->db
= loc_database_ref(db
);
586 // Initialise graph search
587 //e->network_stack[++e->network_stack_depth] = 0;
588 e
->network_stack_depth
= 1;
589 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
591 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
597 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
598 enumerator
->refcount
++;
603 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
604 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
606 // Release all references
607 loc_database_unref(enumerator
->db
);
608 loc_unref(enumerator
->ctx
);
610 if (enumerator
->string
)
611 free(enumerator
->string
);
613 // Free network search
614 free(enumerator
->networks_visited
);
619 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
623 if (--enumerator
->refcount
> 0)
626 loc_database_enumerator_free(enumerator
);
630 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
631 enumerator
->string
= strdup(string
);
633 // Make the string lowercase
634 for (char *p
= enumerator
->string
; *p
; p
++)
640 LOC_EXPORT
int loc_database_enumerator_set_country_code(struct loc_database_enumerator
* enumerator
, const char* country_code
) {
641 // Set empty country code
642 if (!country_code
|| !*country_code
) {
643 *enumerator
->country_code
= '\0';
647 // Country codes must be two characters
648 if (strlen(country_code
) != 2)
651 for (unsigned int i
= 0; i
< 3; i
++) {
652 enumerator
->country_code
[i
] = country_code
[i
];
658 LOC_EXPORT
int loc_database_enumerator_set_asn(
659 struct loc_database_enumerator
* enumerator
, unsigned int asn
) {
660 enumerator
->asn
= asn
;
665 LOC_EXPORT
int loc_database_enumerator_next_as(
666 struct loc_database_enumerator
* enumerator
, struct loc_as
** as
) {
669 // Do not do anything if not in AS mode
670 if (enumerator
->mode
!= LOC_DB_ENUMERATE_ASES
)
673 struct loc_database
* db
= enumerator
->db
;
675 while (enumerator
->as_index
< db
->as_count
) {
677 int r
= loc_database_fetch_as(db
, as
, enumerator
->as_index
++);
681 r
= loc_as_match_string(*as
, enumerator
->string
);
683 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
684 loc_as_get_number(*as
), loc_as_get_name(*as
), enumerator
->string
);
694 enumerator
->as_index
= 0;
696 // We have searched through all of them
700 static int loc_database_enumerator_stack_push_node(
701 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
702 // Do not add empty nodes
706 // Check if there is any space left on the stack
707 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
708 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
712 // Increase stack size
713 int s
= ++e
->network_stack_depth
;
715 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", offset
, depth
);
717 e
->network_stack
[s
].offset
= offset
;
718 e
->network_stack
[s
].i
= i
;
719 e
->network_stack
[s
].depth
= depth
;
724 LOC_EXPORT
int loc_database_enumerator_next_network(
725 struct loc_database_enumerator
* enumerator
, struct loc_network
** network
) {
729 // Do not do anything if not in network mode
730 if (enumerator
->mode
!= LOC_DB_ENUMERATE_NETWORKS
)
735 DEBUG(enumerator
->ctx
, "Called with a stack of %u nodes\n",
736 enumerator
->network_stack_depth
);
739 while (enumerator
->network_stack_depth
> 0) {
740 DEBUG(enumerator
->ctx
, "Stack depth: %u\n", enumerator
->network_stack_depth
);
742 // Get object from top of the stack
743 struct loc_node_stack
* node
= &enumerator
->network_stack
[enumerator
->network_stack_depth
];
745 // Remove the node from the stack if we have already visited it
746 if (enumerator
->networks_visited
[node
->offset
]) {
747 enumerator
->network_stack_depth
--;
751 // Mark the bits on the path correctly
752 in6_addr_set_bit(&enumerator
->network_address
,
753 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
755 DEBUG(enumerator
->ctx
, "Looking at node %jd\n", node
->offset
);
756 enumerator
->networks_visited
[node
->offset
]++;
758 // Pop node from top of the stack
759 struct loc_database_network_node_v0
* n
=
760 enumerator
->db
->network_nodes_v0
+ node
->offset
;
762 // Add edges to stack
763 r
= loc_database_enumerator_stack_push_node(enumerator
,
764 be32toh(n
->one
), 1, node
->depth
+ 1);
769 r
= loc_database_enumerator_stack_push_node(enumerator
,
770 be32toh(n
->zero
), 0, node
->depth
+ 1);
775 // Check if this node is a leaf and has a network object
776 if (__loc_database_node_is_leaf(n
)) {
777 off_t network_index
= be32toh(n
->network
);
779 DEBUG(enumerator
->ctx
, "Node has a network at %jd\n", network_index
);
781 // Fetch the network object
782 r
= loc_database_fetch_network(enumerator
->db
, network
,
783 &enumerator
->network_address
, node
->depth
, network_index
);
785 // Break on any errors
789 // Check if we are interested in this network
791 // Skip if the country code does not match
792 if (enumerator
->country_code
&&
793 !loc_network_match_country_code(*network
, enumerator
->country_code
)) {
794 loc_network_unref(*network
);
800 // Skip if the ASN does not match
801 if (enumerator
->asn
&&
802 !loc_network_match_asn(*network
, enumerator
->asn
)) {
803 loc_network_unref(*network
);
813 // Reached the end of the search
815 // Mark all nodes as non-visited
816 for (unsigned int i
= 0; i
< enumerator
->db
->network_nodes_count
; i
++)
817 enumerator
->networks_visited
[i
] = 0;