]>
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 <loc/libloc.h>
29 #include <loc/compat.h>
30 #include <loc/country.h>
31 #include <loc/network.h>
32 #include <loc/private.h>
39 struct in6_addr first_address
;
40 struct in6_addr last_address
;
45 enum loc_network_flags flags
;
48 static int valid_prefix(struct in6_addr
* address
, unsigned int prefix
) {
49 // The prefix cannot be larger than 128 bits
53 // And the prefix cannot be zero
57 // For IPv4-mapped addresses the prefix has to be 96 or lager
58 if (IN6_IS_ADDR_V4MAPPED(address
) && prefix
<= 96)
64 static struct in6_addr
prefix_to_bitmask(unsigned int prefix
) {
65 struct in6_addr bitmask
;
67 for (unsigned int i
= 0; i
< 16; i
++)
68 bitmask
.s6_addr
[i
] = 0;
70 for (int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
72 bitmask
.s6_addr
[j
] = 0xff;
74 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
80 static struct in6_addr
make_first_address(const struct in6_addr
* address
, const struct in6_addr
* bitmask
) {
83 // Perform bitwise AND
84 for (unsigned int i
= 0; i
< 4; i
++)
85 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] & bitmask
->s6_addr32
[i
];
90 static struct in6_addr
make_last_address(const struct in6_addr
* address
, const struct in6_addr
* bitmask
) {
94 for (unsigned int i
= 0; i
< 4; i
++)
95 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] | ~bitmask
->s6_addr32
[i
];
100 LOC_EXPORT
int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
101 struct in6_addr
* address
, unsigned int prefix
) {
102 // Address cannot be unspecified
103 if (IN6_IS_ADDR_UNSPECIFIED(address
)) {
104 DEBUG(ctx
, "Start address is unspecified\n");
108 // Address cannot be loopback
109 if (IN6_IS_ADDR_LOOPBACK(address
)) {
110 DEBUG(ctx
, "Start address is loopback address\n");
114 // Address cannot be link-local
115 if (IN6_IS_ADDR_LINKLOCAL(address
)) {
116 DEBUG(ctx
, "Start address cannot be link-local\n");
120 // Address cannot be site-local
121 if (IN6_IS_ADDR_SITELOCAL(address
)) {
122 DEBUG(ctx
, "Start address cannot be site-local\n");
126 // Validate the prefix
127 if (valid_prefix(address
, prefix
) != 0) {
128 DEBUG(ctx
, "Invalid prefix: %u\n", prefix
);
132 struct loc_network
* n
= calloc(1, sizeof(*n
));
136 n
->ctx
= loc_ref(ctx
);
142 // Convert the prefix into a bitmask
143 struct in6_addr bitmask
= prefix_to_bitmask(n
->prefix
);
145 // Store the first and last address in the network
146 n
->first_address
= make_first_address(address
, &bitmask
);
147 n
->last_address
= make_last_address(&n
->first_address
, &bitmask
);
150 if (IN6_IS_ADDR_V4MAPPED(&n
->first_address
))
153 n
->family
= AF_INET6
;
155 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
160 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
161 const char* address_string
) {
162 struct in6_addr first_address
;
163 unsigned int prefix
= 0;
167 // Make a copy of the string to work on it
168 char* buffer
= strdup(address_string
);
169 address_string
= prefix_string
= buffer
;
171 // Split address and prefix
172 address_string
= strsep(&prefix_string
, "/");
174 // Did we find a prefix?
176 // Convert prefix to integer
177 prefix
= strtol(prefix_string
, NULL
, 10);
181 r
= loc_parse_address(ctx
, address_string
, &first_address
);
183 // Map the prefix to IPv6 if needed
184 if (IN6_IS_ADDR_V4MAPPED(&first_address
))
189 // Free temporary buffer
193 r
= loc_network_new(ctx
, network
, &first_address
, prefix
);
199 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
205 static void loc_network_free(struct loc_network
* network
) {
206 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
208 loc_unref(network
->ctx
);
212 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
216 if (--network
->refcount
> 0)
219 loc_network_free(network
);
223 static int format_ipv6_address(const struct in6_addr
* address
, char* string
, size_t length
) {
224 const char* ret
= inet_ntop(AF_INET6
, address
, string
, length
);
231 static int format_ipv4_address(const struct in6_addr
* address
, char* string
, size_t length
) {
232 struct in_addr ipv4_address
;
233 ipv4_address
.s_addr
= address
->s6_addr32
[3];
235 const char* ret
= inet_ntop(AF_INET
, &ipv4_address
, string
, length
);
242 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
244 const size_t length
= INET6_ADDRSTRLEN
+ 4;
246 char* string
= malloc(length
);
250 unsigned int prefix
= network
->prefix
;
252 switch (network
->family
) {
254 r
= format_ipv6_address(&network
->first_address
, string
, length
);
258 r
= format_ipv4_address(&network
->first_address
, string
, length
);
268 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
275 sprintf(string
+ strlen(string
), "/%u", prefix
);
280 LOC_EXPORT
int loc_network_address_family(struct loc_network
* network
) {
281 return network
->family
;
284 static char* loc_network_format_address(struct loc_network
* network
, const struct in6_addr
* address
) {
285 const size_t length
= INET6_ADDRSTRLEN
;
287 char* string
= malloc(length
);
293 switch (network
->family
) {
295 r
= format_ipv6_address(address
, string
, length
);
299 r
= format_ipv4_address(address
, string
, length
);
308 ERROR(network
->ctx
, "Could not format IP address to string: %s\n", strerror(errno
));
317 LOC_EXPORT
char* loc_network_format_first_address(struct loc_network
* network
) {
318 return loc_network_format_address(network
, &network
->first_address
);
321 LOC_EXPORT
char* loc_network_format_last_address(struct loc_network
* network
) {
322 return loc_network_format_address(network
, &network
->last_address
);
325 LOC_EXPORT
int loc_network_match_address(struct loc_network
* network
, const struct in6_addr
* address
) {
326 // Address must be larger than the start address
327 if (in6_addr_cmp(&network
->first_address
, address
) > 0)
330 // Address must be smaller than the last address
331 if (in6_addr_cmp(&network
->last_address
, address
) < 0)
334 // The address is inside this network
338 LOC_EXPORT
const char* loc_network_get_country_code(struct loc_network
* network
) {
339 return network
->country_code
;
342 LOC_EXPORT
int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
) {
343 // Set empty country code
344 if (!country_code
|| !*country_code
) {
345 *network
->country_code
= '\0';
349 // Check country code
350 if (!loc_country_code_is_valid(country_code
))
353 loc_country_code_copy(network
->country_code
, country_code
);
358 LOC_EXPORT
int loc_network_match_country_code(struct loc_network
* network
, const char* country_code
) {
359 // Check country code
360 if (!loc_country_code_is_valid(country_code
))
363 return (network
->country_code
[0] == country_code
[0])
364 && (network
->country_code
[1] == country_code
[1]);
367 LOC_EXPORT
uint32_t loc_network_get_asn(struct loc_network
* network
) {
371 LOC_EXPORT
int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
) {
377 LOC_EXPORT
int loc_network_match_asn(struct loc_network
* network
, uint32_t asn
) {
378 return network
->asn
== asn
;
381 LOC_EXPORT
int loc_network_has_flag(struct loc_network
* network
, uint32_t flag
) {
382 return network
->flags
& flag
;
385 LOC_EXPORT
int loc_network_set_flag(struct loc_network
* network
, uint32_t flag
) {
386 network
->flags
|= flag
;
391 LOC_EXPORT
int loc_network_match_flag(struct loc_network
* network
, uint32_t flag
) {
392 return loc_network_has_flag(network
, flag
);
395 LOC_EXPORT
int loc_network_is_subnet_of(struct loc_network
* self
, struct loc_network
* other
) {
396 // If the start address of the other network is smaller than this network,
397 // it cannot be a subnet.
398 if (in6_addr_cmp(&self
->first_address
, &other
->first_address
) < 0)
401 // If the end address of the other network is greater than this network,
402 // it cannot be a subnet.
403 if (in6_addr_cmp(&self
->last_address
, &other
->last_address
) > 0)
409 LOC_EXPORT
int loc_network_to_database_v1(struct loc_network
* network
, struct loc_database_network_v1
* dbobj
) {
411 loc_country_code_copy(dbobj
->country_code
, network
->country_code
);
414 dbobj
->asn
= htobe32(network
->asn
);
417 dbobj
->flags
= htobe16(network
->flags
);
422 LOC_EXPORT
int loc_network_new_from_database_v1(struct loc_ctx
* ctx
, struct loc_network
** network
,
423 struct in6_addr
* address
, unsigned int prefix
, const struct loc_database_network_v1
* dbobj
) {
424 char country_code
[3] = "\0\0";
426 int r
= loc_network_new(ctx
, network
, address
, prefix
);
428 ERROR(ctx
, "Could not allocate a new network: %s", strerror(-r
));
432 // Import country code
433 loc_country_code_copy(country_code
, dbobj
->country_code
);
435 r
= loc_network_set_country_code(*network
, country_code
);
437 ERROR(ctx
, "Could not set country code: %s\n", country_code
);
442 uint32_t asn
= be32toh(dbobj
->asn
);
443 r
= loc_network_set_asn(*network
, asn
);
445 ERROR(ctx
, "Could not set ASN: %d\n", asn
);
450 int flags
= be16toh(dbobj
->flags
);
451 r
= loc_network_set_flag(*network
, flags
);
453 ERROR(ctx
, "Could not set flags: %d\n", flags
);
460 struct loc_network_tree
{
464 struct loc_network_tree_node
* root
;
467 struct loc_network_tree_node
{
471 struct loc_network_tree_node
* zero
;
472 struct loc_network_tree_node
* one
;
474 struct loc_network
* network
;
477 LOC_EXPORT
int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
478 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
482 t
->ctx
= loc_ref(ctx
);
485 // Create the root node
486 int r
= loc_network_tree_node_new(ctx
, &t
->root
);
488 loc_network_tree_unref(t
);
492 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
497 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
) {
498 return loc_network_tree_node_ref(tree
->root
);
501 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
502 struct loc_network_tree_node
** n
;
509 // If the desired node doesn't exist, yet, we will create it
511 int r
= loc_network_tree_node_new(node
->ctx
, n
);
519 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
, unsigned int prefix
) {
520 struct loc_network_tree_node
* node
= tree
->root
;
522 for (unsigned int i
= 0; i
< prefix
; i
++) {
523 // Check if the ith bit is one or zero
524 node
= loc_network_tree_get_node(node
, in6_addr_get_bit(address
, i
));
530 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
531 int(*filter_callback
)(struct loc_network
* network
, void* data
),
532 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
535 // Finding a network ends the walk here
537 if (filter_callback
) {
538 int f
= filter_callback(node
->network
, data
);
542 // Skip network if filter function returns value greater than zero
547 r
= callback(node
->network
, data
);
552 // Walk down on the left side of the tree first
554 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
559 // Then walk on the other side
561 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
569 LOC_EXPORT
int loc_network_tree_walk(struct loc_network_tree
* tree
,
570 int(*filter_callback
)(struct loc_network
* network
, void* data
),
571 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
572 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
575 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
576 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
578 loc_network_tree_node_unref(tree
->root
);
580 loc_unref(tree
->ctx
);
584 LOC_EXPORT
struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
585 if (--tree
->refcount
> 0)
588 loc_network_tree_free(tree
);
592 static int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
593 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
595 char* s
= loc_network_str(network
);
599 INFO(network
->ctx
, "%s\n", s
);
605 LOC_EXPORT
int loc_network_tree_dump(struct loc_network_tree
* tree
) {
606 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
608 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
611 LOC_EXPORT
int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
612 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
614 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
,
615 &network
->first_address
, network
->prefix
);
617 ERROR(tree
->ctx
, "Could not find a node\n");
621 // Check if node has not been set before
623 DEBUG(tree
->ctx
, "There is already a network at this path\n");
627 // Point node to the network
628 node
->network
= loc_network_ref(network
);
633 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
634 size_t* counter
= (size_t*)data
;
636 // Increase the counter for each network
642 LOC_EXPORT
size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
645 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);
652 static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node
* node
) {
656 counter
+= __loc_network_tree_count_nodes(node
->zero
);
659 counter
+= __loc_network_tree_count_nodes(node
->one
);
664 LOC_EXPORT
size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
) {
665 return __loc_network_tree_count_nodes(tree
->root
);
668 LOC_EXPORT
int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
) {
669 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
673 n
->ctx
= loc_ref(ctx
);
676 n
->zero
= n
->one
= NULL
;
678 DEBUG(n
->ctx
, "Network node allocated at %p\n", n
);
683 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
) {
690 static void loc_network_tree_node_free(struct loc_network_tree_node
* node
) {
691 DEBUG(node
->ctx
, "Releasing network node at %p\n", node
);
694 loc_network_unref(node
->network
);
697 loc_network_tree_node_unref(node
->zero
);
700 loc_network_tree_node_unref(node
->one
);
702 loc_unref(node
->ctx
);
706 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
) {
710 if (--node
->refcount
> 0)
713 loc_network_tree_node_free(node
);
717 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
) {
726 return loc_network_tree_node_ref(node
);
729 LOC_EXPORT
int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
) {
730 return (!!node
->network
);
733 LOC_EXPORT
struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
) {
734 return loc_network_ref(node
->network
);