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
;
82 // Index of the AS we are looking at
83 unsigned int as_index
;
86 struct in6_addr network_address
;
87 struct loc_node_stack network_stack
[MAX_STACK_DEPTH
];
88 int network_stack_depth
;
89 unsigned int* networks_visited
;
92 static int loc_database_read_magic(struct loc_database
* db
, FILE* f
) {
93 struct loc_database_magic magic
;
96 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), f
);
98 // Check if we have been able to read enough data
99 if (bytes_read
< sizeof(magic
)) {
100 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
101 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
105 // Compare magic bytes
106 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
107 DEBUG(db
->ctx
, "Magic value matches\n");
110 db
->version
= be16toh(magic
.version
);
111 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
116 ERROR(db
->ctx
, "Database format is not compatible\n");
122 static int loc_database_read_as_section_v0(struct loc_database
* db
,
123 FILE* f
, const struct loc_database_header_v0
* header
) {
124 off_t as_offset
= be32toh(header
->as_offset
);
125 size_t as_length
= be32toh(header
->as_length
);
127 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", as_offset
, as_length
);
130 db
->as_v0
= mmap(NULL
, as_length
, PROT_READ
,
131 MAP_SHARED
, fileno(f
), as_offset
);
133 if (db
->as_v0
== MAP_FAILED
)
137 db
->as_count
= as_length
/ sizeof(*db
->as_v0
);
139 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
144 static int loc_database_read_network_nodes_section_v0(struct loc_database
* db
,
145 FILE* f
, const struct loc_database_header_v0
* header
) {
146 off_t network_nodes_offset
= be32toh(header
->network_tree_offset
);
147 size_t network_nodes_length
= be32toh(header
->network_tree_length
);
149 DEBUG(db
->ctx
, "Reading network nodes section from %jd (%zu bytes)\n",
150 network_nodes_offset
, network_nodes_length
);
152 if (network_nodes_length
> 0) {
153 db
->network_nodes_v0
= mmap(NULL
, network_nodes_length
, PROT_READ
,
154 MAP_SHARED
, fileno(f
), network_nodes_offset
);
156 if (db
->network_nodes_v0
== MAP_FAILED
)
160 db
->network_nodes_count
= network_nodes_length
/ sizeof(*db
->network_nodes_v0
);
162 INFO(db
->ctx
, "Read %zu network nodes from the database\n", db
->network_nodes_count
);
167 static int loc_database_read_networks_section_v0(struct loc_database
* db
,
168 FILE* f
, const struct loc_database_header_v0
* header
) {
169 off_t networks_offset
= be32toh(header
->network_data_offset
);
170 size_t networks_length
= be32toh(header
->network_data_length
);
172 DEBUG(db
->ctx
, "Reading networks section from %jd (%zu bytes)\n",
173 networks_offset
, networks_length
);
175 if (networks_length
> 0) {
176 db
->networks_v0
= mmap(NULL
, networks_length
, PROT_READ
,
177 MAP_SHARED
, fileno(f
), networks_offset
);
179 if (db
->networks_v0
== MAP_FAILED
)
183 db
->networks_count
= networks_length
/ sizeof(*db
->networks_v0
);
185 INFO(db
->ctx
, "Read %zu networks from the database\n", db
->networks_count
);
190 static int loc_database_read_header_v0(struct loc_database
* db
, FILE* f
) {
191 struct loc_database_header_v0 header
;
194 size_t size
= fread(&header
, 1, sizeof(header
), f
);
196 if (size
< sizeof(header
)) {
197 ERROR(db
->ctx
, "Could not read enough data for header\n");
202 db
->created_at
= be64toh(header
.created_at
);
203 db
->vendor
= be32toh(header
.vendor
);
204 db
->description
= be32toh(header
.description
);
205 db
->license
= be32toh(header
.license
);
208 off_t pool_offset
= be32toh(header
.pool_offset
);
209 size_t pool_length
= be32toh(header
.pool_length
);
211 int r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
212 f
, pool_length
, pool_offset
);
217 r
= loc_database_read_as_section_v0(db
, f
, &header
);
222 r
= loc_database_read_network_nodes_section_v0(db
, f
, &header
);
227 r
= loc_database_read_networks_section_v0(db
, f
, &header
);
234 static int loc_database_read_header(struct loc_database
* db
, FILE* f
) {
235 switch (db
->version
) {
237 return loc_database_read_header_v0(db
, f
);
240 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
245 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
246 clock_t start
= clock();
249 int r
= loc_database_read_magic(db
, f
);
254 r
= loc_database_read_header(db
, f
);
258 clock_t end
= clock();
260 INFO(db
->ctx
, "Opened database in %.8fs\n",
261 (double)(end
- start
) / CLOCKS_PER_SEC
);
266 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
267 // Fail on invalid file handle
271 struct loc_database
* db
= calloc(1, sizeof(*db
));
276 db
->ctx
= loc_ref(ctx
);
279 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
281 int r
= loc_database_read(db
, f
);
283 loc_database_unref(db
);
292 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
298 static void loc_database_free(struct loc_database
* db
) {
301 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
305 r
= munmap(db
->as_v0
, db
->as_count
* sizeof(*db
->as_v0
));
307 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
310 // Remove mapped network sections
311 if (db
->networks_v0
) {
312 r
= munmap(db
->networks_v0
, db
->networks_count
* sizeof(*db
->networks_v0
));
314 ERROR(db
->ctx
, "Could not unmap networks section: %s\n", strerror(errno
));
317 // Remove mapped network nodes section
318 if (db
->network_nodes_v0
) {
319 r
= munmap(db
->network_nodes_v0
, db
->network_nodes_count
* sizeof(*db
->network_nodes_v0
));
321 ERROR(db
->ctx
, "Could not unmap network nodes section: %s\n", strerror(errno
));
324 loc_stringpool_unref(db
->pool
);
330 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
331 if (--db
->refcount
> 0)
334 loc_database_free(db
);
338 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
339 return db
->created_at
;
342 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
343 return loc_stringpool_get(db
->pool
, db
->vendor
);
346 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
347 return loc_stringpool_get(db
->pool
, db
->description
);
350 LOC_EXPORT
const char* loc_database_get_license(struct loc_database
* db
) {
351 return loc_stringpool_get(db
->pool
, db
->license
);
354 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
358 // Returns the AS at position pos
359 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
360 if ((size_t)pos
>= db
->as_count
)
363 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", pos
);
366 switch (db
->version
) {
368 r
= loc_as_new_from_database_v0(db
->ctx
, db
->pool
, as
, db
->as_v0
+ pos
);
376 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
382 // Performs a binary search to find the AS in the list
383 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
385 off_t hi
= db
->as_count
- 1;
388 clock_t start
= clock();
391 off_t i
= (lo
+ hi
) / 2;
393 // Fetch AS in the middle between lo and hi
394 int r
= loc_database_fetch_as(db
, as
, i
);
398 // Check if this is a match
399 uint32_t as_number
= loc_as_get_number(*as
);
400 if (as_number
== number
) {
401 clock_t end
= clock();
403 // Log how fast this has been
404 DEBUG(db
->ctx
, "Found AS%u in %.8fs\n", as_number
,
405 (double)(end
- start
) / CLOCKS_PER_SEC
);
410 // If it wasn't, we release the AS and
411 // adjust our search pointers
414 if (as_number
< number
) {
426 // Returns the network at position pos
427 static int loc_database_fetch_network(struct loc_database
* db
, struct loc_network
** network
,
428 struct in6_addr
* address
, unsigned int prefix
, off_t pos
) {
429 if ((size_t)pos
>= db
->networks_count
)
432 DEBUG(db
->ctx
, "Fetching network at position %jd\n", pos
);
435 switch (db
->version
) {
437 r
= loc_network_new_from_database_v0(db
->ctx
, network
,
438 address
, prefix
, db
->networks_v0
+ pos
);
446 char* string
= loc_network_str(*network
);
447 DEBUG(db
->ctx
, "Got network %s\n", string
);
454 static int __loc_database_node_is_leaf(const struct loc_database_network_node_v0
* node
) {
455 return (node
->network
!= htobe32(0xffffffff));
458 static int __loc_database_lookup_handle_leaf(struct loc_database
* db
, const struct in6_addr
* address
,
459 struct loc_network
** network
, struct in6_addr
* network_address
, unsigned int prefix
,
460 const struct loc_database_network_node_v0
* node
) {
461 off_t network_index
= be32toh(node
->network
);
463 DEBUG(db
->ctx
, "Handling leaf node at %jd (%jd)\n", node
- db
->network_nodes_v0
, network_index
);
466 int r
= loc_database_fetch_network(db
, network
,
467 network_address
, prefix
, network_index
);
469 ERROR(db
->ctx
, "Could not fetch network %jd from database\n", network_index
);
473 // Check if the given IP address is inside the network
474 r
= loc_network_match_address(*network
, address
);
476 DEBUG(db
->ctx
, "Searched address is not part of the network\n");
478 loc_network_unref(*network
);
483 // A network was found and the IP address matches
487 // Searches for an exact match along the path
488 static int __loc_database_lookup(struct loc_database
* db
, const struct in6_addr
* address
,
489 struct loc_network
** network
, struct in6_addr
* network_address
,
490 const struct loc_database_network_node_v0
* node
, unsigned int level
) {
495 int bit
= in6_addr_get_bit(address
, level
);
496 in6_addr_set_bit(network_address
, level
, bit
);
499 node_index
= be32toh(node
->zero
);
501 node_index
= be32toh(node
->one
);
503 // If the node index is zero, the tree ends here
504 // and we cannot descend any further
505 if (node_index
> 0) {
507 if ((size_t)node_index
>= db
->network_nodes_count
)
510 // Move on to the next node
511 r
= __loc_database_lookup(db
, address
, network
, network_address
,
512 db
->network_nodes_v0
+ node_index
, level
+ 1);
514 // End here if a result was found
522 DEBUG(db
->ctx
, "No match found below level %u\n", level
);
524 DEBUG(db
->ctx
, "Tree ended at level %u\n", level
);
527 // If this node has a leaf, we will check if it matches
528 if (__loc_database_node_is_leaf(node
)) {
529 r
= __loc_database_lookup_handle_leaf(db
, address
, network
, network_address
, level
, node
);
537 LOC_EXPORT
int loc_database_lookup(struct loc_database
* db
,
538 struct in6_addr
* address
, struct loc_network
** network
) {
539 struct in6_addr network_address
;
540 memset(&network_address
, 0, sizeof(network_address
));
545 clock_t start
= clock();
547 int r
= __loc_database_lookup(db
, address
, network
, &network_address
,
548 db
->network_nodes_v0
, 0);
550 clock_t end
= clock();
552 // Log how fast this has been
553 DEBUG(db
->ctx
, "Executed network search in %.8fs\n",
554 (double)(end
- start
) / CLOCKS_PER_SEC
);
559 LOC_EXPORT
int loc_database_lookup_from_string(struct loc_database
* db
,
560 const char* string
, struct loc_network
** network
) {
561 struct in6_addr address
;
563 int r
= loc_parse_address(db
->ctx
, string
, &address
);
567 return loc_database_lookup(db
, &address
, network
);
572 LOC_EXPORT
int loc_database_enumerator_new(struct loc_database_enumerator
** enumerator
, struct loc_database
* db
) {
573 struct loc_database_enumerator
* e
= calloc(1, sizeof(*e
));
578 e
->ctx
= loc_ref(db
->ctx
);
579 e
->db
= loc_database_ref(db
);
582 // Initialise graph search
583 //e->network_stack[++e->network_stack_depth] = 0;
584 e
->network_stack_depth
= 1;
585 e
->networks_visited
= calloc(db
->network_nodes_count
, sizeof(*e
->networks_visited
));
587 DEBUG(e
->ctx
, "Database enumerator object allocated at %p\n", e
);
593 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_ref(struct loc_database_enumerator
* enumerator
) {
594 enumerator
->refcount
++;
599 static void loc_database_enumerator_free(struct loc_database_enumerator
* enumerator
) {
600 DEBUG(enumerator
->ctx
, "Releasing database enumerator %p\n", enumerator
);
602 // Release all references
603 loc_database_unref(enumerator
->db
);
604 loc_unref(enumerator
->ctx
);
606 if (enumerator
->string
)
607 free(enumerator
->string
);
609 // Free network search
610 free(enumerator
->networks_visited
);
615 LOC_EXPORT
struct loc_database_enumerator
* loc_database_enumerator_unref(struct loc_database_enumerator
* enumerator
) {
619 if (--enumerator
->refcount
> 0)
622 loc_database_enumerator_free(enumerator
);
626 LOC_EXPORT
int loc_database_enumerator_set_string(struct loc_database_enumerator
* enumerator
, const char* string
) {
627 enumerator
->string
= strdup(string
);
629 // Make the string lowercase
630 for (char *p
= enumerator
->string
; *p
; p
++)
636 LOC_EXPORT
int loc_database_enumerator_set_country_code(struct loc_database_enumerator
* enumerator
, const char* country_code
) {
637 // Set empty country code
638 if (!country_code
|| !*country_code
) {
639 *enumerator
->country_code
= '\0';
643 // Country codes must be two characters
644 if (strlen(country_code
) != 2)
647 for (unsigned int i
= 0; i
< 3; i
++) {
648 enumerator
->country_code
[i
] = country_code
[i
];
654 LOC_EXPORT
struct loc_as
* loc_database_enumerator_next_as(struct loc_database_enumerator
* enumerator
) {
655 struct loc_database
* db
= enumerator
->db
;
658 while (enumerator
->as_index
< db
->as_count
) {
660 int r
= loc_database_fetch_as(db
, &as
, enumerator
->as_index
++);
664 r
= loc_as_match_string(as
, enumerator
->string
);
666 DEBUG(enumerator
->ctx
, "AS%d (%s) matches %s\n",
667 loc_as_get_number(as
), loc_as_get_name(as
), enumerator
->string
);
677 enumerator
->as_index
= 0;
679 // We have searched through all of them
683 static int loc_database_enumerator_stack_push_node(
684 struct loc_database_enumerator
* e
, off_t offset
, int i
, int depth
) {
685 // Do not add empty nodes
689 // Check if there is any space left on the stack
690 if (e
->network_stack_depth
>= MAX_STACK_DEPTH
) {
691 ERROR(e
->ctx
, "Maximum stack size reached: %d\n", e
->network_stack_depth
);
695 // Increase stack size
696 int s
= ++e
->network_stack_depth
;
698 DEBUG(e
->ctx
, "Added node %jd to stack (%d)\n", offset
, depth
);
700 e
->network_stack
[s
].offset
= offset
;
701 e
->network_stack
[s
].i
= i
;
702 e
->network_stack
[s
].depth
= depth
;
707 static int loc_database_enumerator_network_depth_first_search(
708 struct loc_database_enumerator
* e
, struct loc_network
** network
) {
713 DEBUG(e
->ctx
, "Called with a stack of %u nodes\n", e
->network_stack_depth
);
716 while (e
->network_stack_depth
> 0) {
717 DEBUG(e
->ctx
, "Stack depth: %u\n", e
->network_stack_depth
);
719 // Get object from top of the stack
720 struct loc_node_stack
* node
= &e
->network_stack
[e
->network_stack_depth
];
722 // Remove the node from the stack if we have already visited it
723 if (e
->networks_visited
[node
->offset
]) {
724 e
->network_stack_depth
--;
728 in6_addr_set_bit(&e
->network_address
,
729 (node
->depth
> 0) ? node
->depth
- 1 : 0, node
->i
);
731 //for (unsigned int i = stack->depth + 1; i < 128; i++)
732 // in6_addr_set_bit(&e->network_address, i, 0);
734 DEBUG(e
->ctx
, "Looking at node %jd\n", node
->offset
);
735 e
->networks_visited
[node
->offset
]++;
737 // Pop node from top of the stack
738 struct loc_database_network_node_v0
* n
=
739 e
->db
->network_nodes_v0
+ node
->offset
;
741 // Add edges to stack
742 r
= loc_database_enumerator_stack_push_node(e
,
743 be32toh(n
->one
), 1, node
->depth
+ 1);
748 r
= loc_database_enumerator_stack_push_node(e
,
749 be32toh(n
->zero
), 0, node
->depth
+ 1);
754 // Check if this node is a leaf and has a network object
755 if (__loc_database_node_is_leaf(n
)) {
756 off_t network_index
= be32toh(n
->network
);
758 DEBUG(e
->ctx
, "Node has a network at %jd\n", network_index
);
760 // Fetch the network object
761 r
= loc_database_fetch_network(e
->db
, network
,
762 &e
->network_address
, node
->depth
, network_index
);
764 // Break on any errors
768 // Check if we are interested in this network
770 // Skip if the country code does not match
771 if (e
->country_code
&& !loc_network_match_country_code(*network
, e
->country_code
)) {
772 loc_network_unref(*network
);
780 // Reached the end of the search
786 LOC_EXPORT
struct loc_network
* loc_database_enumerator_next_network(
787 struct loc_database_enumerator
* enumerator
) {
788 struct loc_network
* network
= NULL
;
790 int r
= loc_database_enumerator_network_depth_first_search(enumerator
, &network
);