]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/network.c
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>
28 #include <libloc/libloc.h>
29 #include <libloc/compat.h>
30 #include <libloc/country.h>
31 #include <libloc/network.h>
32 #include <libloc/network-list.h>
33 #include <libloc/private.h>
40 struct in6_addr first_address
;
41 struct in6_addr last_address
;
46 enum loc_network_flags flags
;
49 static int valid_prefix(struct in6_addr
* address
, unsigned int prefix
) {
50 // The prefix cannot be larger than 128 bits
54 // For IPv4-mapped addresses the prefix has to be 96 or lager
55 if (IN6_IS_ADDR_V4MAPPED(address
) && prefix
<= 96)
61 LOC_EXPORT
int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
62 struct in6_addr
* address
, unsigned int prefix
) {
63 // Validate the prefix
64 if (valid_prefix(address
, prefix
) != 0) {
65 ERROR(ctx
, "Invalid prefix: %u\n", prefix
);
70 struct loc_network
* n
= calloc(1, sizeof(*n
));
76 n
->ctx
= loc_ref(ctx
);
82 // Convert the prefix into a bitmask
83 struct in6_addr bitmask
= loc_prefix_to_bitmask(n
->prefix
);
85 // Store the first and last address in the network
86 n
->first_address
= loc_address_and(address
, &bitmask
);
87 n
->last_address
= loc_address_or(&n
->first_address
, &bitmask
);
90 n
->family
= loc_address_family(&n
->first_address
);
92 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
97 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
98 const char* address_string
) {
99 struct in6_addr first_address
;
101 unsigned int prefix
= 128;
104 DEBUG(ctx
, "Attempting to parse network %s\n", address_string
);
106 // Make a copy of the string to work on it
107 char* buffer
= strdup(address_string
);
108 address_string
= prefix_string
= buffer
;
110 // Split address and prefix
111 address_string
= strsep(&prefix_string
, "/");
113 DEBUG(ctx
, " Split into address = %s, prefix = %s\n", address_string
, prefix_string
);
116 r
= loc_parse_address(ctx
, address_string
, &first_address
);
118 DEBUG(ctx
, "The address could not be parsed\n");
122 // If a prefix was given, we will try to parse it
124 // Convert prefix to integer
125 prefix
= strtol(prefix_string
, NULL
, 10);
128 DEBUG(ctx
, "The prefix was not parsable: %s\n", prefix_string
);
132 // Map the prefix to IPv6 if needed
133 if (IN6_IS_ADDR_V4MAPPED(&first_address
))
138 // Free temporary buffer
141 // Exit if the parsing was unsuccessful
145 // Create a new network
146 return loc_network_new(ctx
, network
, &first_address
, prefix
);
149 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
155 static void loc_network_free(struct loc_network
* network
) {
156 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
158 loc_unref(network
->ctx
);
162 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
166 if (--network
->refcount
> 0)
169 loc_network_free(network
);
173 static int format_ipv6_address(const struct in6_addr
* address
, char* string
, size_t length
) {
174 const char* ret
= inet_ntop(AF_INET6
, address
, string
, length
);
181 static int format_ipv4_address(const struct in6_addr
* address
, char* string
, size_t length
) {
182 struct in_addr ipv4_address
;
183 ipv4_address
.s_addr
= address
->s6_addr32
[3];
185 const char* ret
= inet_ntop(AF_INET
, &ipv4_address
, string
, length
);
192 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
194 const size_t length
= INET6_ADDRSTRLEN
+ 4;
196 char* string
= malloc(length
);
200 unsigned int prefix
= network
->prefix
;
202 switch (network
->family
) {
204 r
= format_ipv6_address(&network
->first_address
, string
, length
);
208 r
= format_ipv4_address(&network
->first_address
, string
, length
);
218 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
225 sprintf(string
+ strlen(string
), "/%u", prefix
);
230 LOC_EXPORT
int loc_network_address_family(struct loc_network
* network
) {
231 return network
->family
;
234 LOC_EXPORT
unsigned int loc_network_prefix(struct loc_network
* network
) {
235 switch (network
->family
) {
237 return network
->prefix
;
240 return network
->prefix
- 96;
246 static char* loc_network_format_address(struct loc_network
* network
, const struct in6_addr
* address
) {
247 const size_t length
= INET6_ADDRSTRLEN
;
249 char* string
= malloc(length
);
255 switch (network
->family
) {
257 r
= format_ipv6_address(address
, string
, length
);
261 r
= format_ipv4_address(address
, string
, length
);
270 ERROR(network
->ctx
, "Could not format IP address to string: %s\n", strerror(errno
));
279 LOC_EXPORT
const struct in6_addr
* loc_network_get_first_address(struct loc_network
* network
) {
280 return &network
->first_address
;
283 LOC_EXPORT
char* loc_network_format_first_address(struct loc_network
* network
) {
284 return loc_network_format_address(network
, &network
->first_address
);
287 LOC_EXPORT
const struct in6_addr
* loc_network_get_last_address(struct loc_network
* network
) {
288 return &network
->last_address
;
291 LOC_EXPORT
char* loc_network_format_last_address(struct loc_network
* network
) {
292 return loc_network_format_address(network
, &network
->last_address
);
295 LOC_EXPORT
int loc_network_matches_address(struct loc_network
* network
, const struct in6_addr
* address
) {
296 // Address must be larger than the start address
297 if (in6_addr_cmp(&network
->first_address
, address
) > 0)
300 // Address must be smaller than the last address
301 if (in6_addr_cmp(&network
->last_address
, address
) < 0)
304 // The address is inside this network
308 LOC_EXPORT
const char* loc_network_get_country_code(struct loc_network
* network
) {
309 return network
->country_code
;
312 LOC_EXPORT
int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
) {
313 // Set empty country code
314 if (!country_code
|| !*country_code
) {
315 *network
->country_code
= '\0';
319 // Check country code
320 if (!loc_country_code_is_valid(country_code
))
323 loc_country_code_copy(network
->country_code
, country_code
);
328 LOC_EXPORT
int loc_network_matches_country_code(struct loc_network
* network
, const char* country_code
) {
329 // Search for any special flags
330 const int flag
= loc_country_special_code_to_flag(country_code
);
332 // If we found a flag, we will return whether it is set or not
334 return loc_network_has_flag(network
, flag
);
336 // Check country code
337 if (!loc_country_code_is_valid(country_code
))
340 // Check for an exact match
341 return (network
->country_code
[0] == country_code
[0])
342 && (network
->country_code
[1] == country_code
[1]);
345 LOC_EXPORT
uint32_t loc_network_get_asn(struct loc_network
* network
) {
349 LOC_EXPORT
int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
) {
355 LOC_EXPORT
int loc_network_has_flag(struct loc_network
* network
, uint32_t flag
) {
356 return network
->flags
& flag
;
359 LOC_EXPORT
int loc_network_set_flag(struct loc_network
* network
, uint32_t flag
) {
360 network
->flags
|= flag
;
365 LOC_EXPORT
int loc_network_cmp(struct loc_network
* self
, struct loc_network
* other
) {
367 int r
= in6_addr_cmp(&self
->first_address
, &other
->first_address
);
372 if (self
->prefix
> other
->prefix
)
374 else if (self
->prefix
< other
->prefix
)
377 // Both networks are equal
381 LOC_EXPORT
int loc_network_overlaps(struct loc_network
* self
, struct loc_network
* other
) {
382 // Either of the start addresses must be in the other subnet
383 if (loc_network_matches_address(self
, &other
->first_address
))
386 if (loc_network_matches_address(other
, &self
->first_address
))
389 // Or either of the end addresses is in the other subnet
390 if (loc_network_matches_address(self
, &other
->last_address
))
393 if (loc_network_matches_address(other
, &self
->last_address
))
399 LOC_EXPORT
int loc_network_is_subnet(struct loc_network
* self
, struct loc_network
* other
) {
400 // The prefix must be smaller (this avoids the more complex comparisons later)
401 if (self
->prefix
> other
->prefix
)
404 // If the start address of the other network is smaller than this network,
405 // it cannot be a subnet.
406 if (in6_addr_cmp(&self
->first_address
, &other
->first_address
) > 0)
409 // If the end address of the other network is greater than this network,
410 // it cannot be a subnet.
411 if (in6_addr_cmp(&self
->last_address
, &other
->last_address
) < 0)
417 LOC_EXPORT
int loc_network_subnets(struct loc_network
* network
,
418 struct loc_network
** subnet1
, struct loc_network
** subnet2
) {
424 unsigned int prefix
= network
->prefix
+ 1;
426 // Check if the new prefix is valid
427 if (valid_prefix(&network
->first_address
, prefix
))
430 // Create the first half of the network
431 r
= loc_network_new(network
->ctx
, subnet1
, &network
->first_address
, prefix
);
435 // The next subnet starts after the first one
436 struct in6_addr first_address
= address_increment(&(*subnet1
)->last_address
);
438 // Create the second half of the network
439 r
= loc_network_new(network
->ctx
, subnet2
, &first_address
, prefix
);
444 const char* country_code
= loc_network_get_country_code(network
);
446 loc_network_set_country_code(*subnet1
, country_code
);
447 loc_network_set_country_code(*subnet2
, country_code
);
451 uint32_t asn
= loc_network_get_asn(network
);
453 loc_network_set_asn(*subnet1
, asn
);
454 loc_network_set_asn(*subnet2
, asn
);
458 loc_network_set_flag(*subnet1
, network
->flags
);
459 loc_network_set_flag(*subnet2
, network
->flags
);
464 static int __loc_network_exclude(struct loc_network
* network
,
465 struct loc_network
* other
, struct loc_network_list
* list
) {
466 struct loc_network
* subnet1
= NULL
;
467 struct loc_network
* subnet2
= NULL
;
469 int r
= loc_network_subnets(network
, &subnet1
, &subnet2
);
473 if (loc_network_cmp(other
, subnet1
) == 0) {
474 r
= loc_network_list_push(list
, subnet2
);
478 } else if (loc_network_cmp(other
, subnet2
) == 0) {
479 r
= loc_network_list_push(list
, subnet1
);
483 } else if (loc_network_is_subnet(subnet1
, other
)) {
484 r
= loc_network_list_push(list
, subnet2
);
488 r
= __loc_network_exclude(subnet1
, other
, list
);
492 } else if (loc_network_is_subnet(subnet2
, other
)) {
493 r
= loc_network_list_push(list
, subnet1
);
497 r
= __loc_network_exclude(subnet2
, other
, list
);
502 ERROR(network
->ctx
, "We should never get here\n");
509 loc_network_unref(subnet1
);
512 loc_network_unref(subnet2
);
517 static int __loc_network_exclude_to_list(struct loc_network
* self
,
518 struct loc_network
* other
, struct loc_network_list
* list
) {
519 // Other must be a subnet of self
520 if (!loc_network_is_subnet(self
, other
)) {
521 DEBUG(self
->ctx
, "Network %p is not contained in network %p\n", other
, self
);
527 // We cannot perform this operation if both networks equal
528 if (loc_network_cmp(self
, other
) == 0) {
529 DEBUG(self
->ctx
, "Networks %p and %p are equal\n", self
, other
);
535 return __loc_network_exclude(self
, other
, list
);
538 LOC_EXPORT
struct loc_network_list
* loc_network_exclude(
539 struct loc_network
* self
, struct loc_network
* other
) {
540 struct loc_network_list
* list
;
543 char* n1
= loc_network_str(self
);
544 char* n2
= loc_network_str(other
);
546 DEBUG(self
->ctx
, "Returning %s excluding %s...\n", n1
, n2
);
552 // Create a new list with the result
553 int r
= loc_network_list_new(self
->ctx
, &list
);
555 ERROR(self
->ctx
, "Could not create network list: %d\n", r
);
560 r
= __loc_network_exclude_to_list(self
, other
, list
);
562 loc_network_list_unref(list
);
571 LOC_EXPORT
struct loc_network_list
* loc_network_exclude_list(
572 struct loc_network
* network
, struct loc_network_list
* list
) {
573 struct loc_network_list
* to_check
;
575 // Create a new list with all networks to look at
576 int r
= loc_network_list_new(network
->ctx
, &to_check
);
580 struct loc_network
* subnet
= NULL
;
581 struct loc_network_list
* subnets
= NULL
;
583 for (unsigned int i
= 0; i
< loc_network_list_size(list
); i
++) {
584 subnet
= loc_network_list_get(list
, i
);
586 // Find all excluded networks
587 if (!loc_network_list_contains(to_check
, subnet
)) {
588 r
= __loc_network_exclude_to_list(network
, subnet
, to_check
);
590 loc_network_list_unref(to_check
);
591 loc_network_unref(subnet
);
598 loc_network_unref(subnet
);
601 r
= loc_network_list_new(network
->ctx
, &subnets
);
603 loc_network_list_unref(to_check
);
607 off_t smallest_subnet
= 0;
609 while (!loc_network_list_empty(to_check
)) {
610 struct loc_network
* subnet_to_check
= loc_network_list_pop_first(to_check
);
612 // Check whether the subnet to check is part of the input list
613 if (loc_network_list_contains(list
, subnet_to_check
)) {
614 loc_network_unref(subnet_to_check
);
618 // Marks whether this subnet passed all checks
621 for (unsigned int i
= smallest_subnet
; i
< loc_network_list_size(list
); i
++) {
622 subnet
= loc_network_list_get(list
, i
);
624 // Drop this subnet if is a subnet of another subnet
625 if (loc_network_is_subnet(subnet
, subnet_to_check
)) {
627 loc_network_unref(subnet
);
631 // Break it down if it overlaps
632 if (loc_network_overlaps(subnet
, subnet_to_check
)) {
635 __loc_network_exclude_to_list(subnet_to_check
, subnet
, to_check
);
637 loc_network_unref(subnet
);
641 // If the subnet is strictly greater, we do not need to continue the search
642 r
= loc_network_cmp(subnet
, subnet_to_check
);
644 loc_network_unref(subnet
);
647 // If it is strictly smaller, we can continue the search from here next
648 // time because all networks that are to be checked can only be larger
654 loc_network_unref(subnet
);
658 r
= loc_network_list_push(subnets
, subnet_to_check
);
661 loc_network_unref(subnet_to_check
);
664 loc_network_list_unref(to_check
);
669 int loc_network_to_database_v1(struct loc_network
* network
, struct loc_database_network_v1
* dbobj
) {
671 loc_country_code_copy(dbobj
->country_code
, network
->country_code
);
674 dbobj
->asn
= htobe32(network
->asn
);
677 dbobj
->flags
= htobe16(network
->flags
);
682 int loc_network_new_from_database_v1(struct loc_ctx
* ctx
, struct loc_network
** network
,
683 struct in6_addr
* address
, unsigned int prefix
, const struct loc_database_network_v1
* dbobj
) {
684 char country_code
[3] = "\0\0";
686 int r
= loc_network_new(ctx
, network
, address
, prefix
);
688 ERROR(ctx
, "Could not allocate a new network: %s", strerror(-r
));
692 // Import country code
693 loc_country_code_copy(country_code
, dbobj
->country_code
);
695 r
= loc_network_set_country_code(*network
, country_code
);
697 ERROR(ctx
, "Could not set country code: %s\n", country_code
);
702 uint32_t asn
= be32toh(dbobj
->asn
);
703 r
= loc_network_set_asn(*network
, asn
);
705 ERROR(ctx
, "Could not set ASN: %d\n", asn
);
710 int flags
= be16toh(dbobj
->flags
);
711 r
= loc_network_set_flag(*network
, flags
);
713 ERROR(ctx
, "Could not set flags: %d\n", flags
);
720 struct loc_network_tree
{
724 struct loc_network_tree_node
* root
;
727 struct loc_network_tree_node
{
731 struct loc_network_tree_node
* zero
;
732 struct loc_network_tree_node
* one
;
734 struct loc_network
* network
;
737 int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
738 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
742 t
->ctx
= loc_ref(ctx
);
745 // Create the root node
746 int r
= loc_network_tree_node_new(ctx
, &t
->root
);
748 loc_network_tree_unref(t
);
752 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
757 struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
) {
758 return loc_network_tree_node_ref(tree
->root
);
761 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
762 struct loc_network_tree_node
** n
;
769 // If the desired node doesn't exist, yet, we will create it
771 int r
= loc_network_tree_node_new(node
->ctx
, n
);
779 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
, unsigned int prefix
) {
780 struct loc_network_tree_node
* node
= tree
->root
;
782 for (unsigned int i
= 0; i
< prefix
; i
++) {
783 // Check if the ith bit is one or zero
784 node
= loc_network_tree_get_node(node
, in6_addr_get_bit(address
, i
));
790 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
791 int(*filter_callback
)(struct loc_network
* network
, void* data
),
792 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
795 // Finding a network ends the walk here
797 if (filter_callback
) {
798 int f
= filter_callback(node
->network
, data
);
802 // Skip network if filter function returns value greater than zero
807 r
= callback(node
->network
, data
);
812 // Walk down on the left side of the tree first
814 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
819 // Then walk on the other side
821 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
829 int loc_network_tree_walk(struct loc_network_tree
* tree
,
830 int(*filter_callback
)(struct loc_network
* network
, void* data
),
831 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
832 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
835 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
836 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
838 loc_network_tree_node_unref(tree
->root
);
840 loc_unref(tree
->ctx
);
844 struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
845 if (--tree
->refcount
> 0)
848 loc_network_tree_free(tree
);
852 static int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
853 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
855 char* s
= loc_network_str(network
);
859 INFO(network
->ctx
, "%s\n", s
);
865 int loc_network_tree_dump(struct loc_network_tree
* tree
) {
866 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
868 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
871 int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
872 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
874 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
,
875 &network
->first_address
, network
->prefix
);
877 ERROR(tree
->ctx
, "Could not find a node\n");
881 // Check if node has not been set before
883 DEBUG(tree
->ctx
, "There is already a network at this path\n");
887 // Point node to the network
888 node
->network
= loc_network_ref(network
);
893 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
894 size_t* counter
= (size_t*)data
;
896 // Increase the counter for each network
902 size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
905 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);
912 static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node
* node
) {
916 counter
+= __loc_network_tree_count_nodes(node
->zero
);
919 counter
+= __loc_network_tree_count_nodes(node
->one
);
924 size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
) {
925 return __loc_network_tree_count_nodes(tree
->root
);
928 int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
) {
929 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
933 n
->ctx
= loc_ref(ctx
);
936 n
->zero
= n
->one
= NULL
;
938 DEBUG(n
->ctx
, "Network node allocated at %p\n", n
);
943 struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
) {
950 static void loc_network_tree_node_free(struct loc_network_tree_node
* node
) {
951 DEBUG(node
->ctx
, "Releasing network node at %p\n", node
);
954 loc_network_unref(node
->network
);
957 loc_network_tree_node_unref(node
->zero
);
960 loc_network_tree_node_unref(node
->one
);
962 loc_unref(node
->ctx
);
966 struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
) {
970 if (--node
->refcount
> 0)
973 loc_network_tree_node_free(node
);
977 struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
) {
986 return loc_network_tree_node_ref(node
);
989 int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
) {
990 return (!!node
->network
);
993 struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
) {
994 return loc_network_ref(node
->network
);