]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/network.c
dc976e90018ec0e164360199eba2df8e12a61890
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>
38 struct in6_addr first_address
;
39 struct in6_addr last_address
;
44 enum loc_network_flags flags
;
47 static int valid_prefix(struct in6_addr
* address
, unsigned int prefix
) {
48 // The prefix cannot be larger than 128 bits
52 // And the prefix cannot be zero
56 // For IPv4-mapped addresses the prefix has to be 96 or lager
57 if (IN6_IS_ADDR_V4MAPPED(address
) && prefix
<= 96)
63 static struct in6_addr
prefix_to_bitmask(unsigned int prefix
) {
64 struct in6_addr bitmask
;
66 for (unsigned int i
= 0; i
< 16; i
++)
67 bitmask
.s6_addr
[i
] = 0;
69 for (int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
71 bitmask
.s6_addr
[j
] = 0xff;
73 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
79 static struct in6_addr
make_first_address(const struct in6_addr
* address
, const struct in6_addr
* bitmask
) {
82 // Perform bitwise AND
83 for (unsigned int i
= 0; i
< 4; i
++)
84 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] & bitmask
->s6_addr32
[i
];
89 static struct in6_addr
make_last_address(const struct in6_addr
* address
, const struct in6_addr
* bitmask
) {
93 for (unsigned int i
= 0; i
< 4; i
++)
94 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] | ~bitmask
->s6_addr32
[i
];
99 LOC_EXPORT
int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
100 struct in6_addr
* address
, unsigned int prefix
) {
101 // Address cannot be unspecified
102 if (IN6_IS_ADDR_UNSPECIFIED(address
)) {
103 DEBUG(ctx
, "Start address is unspecified\n");
107 // Address cannot be loopback
108 if (IN6_IS_ADDR_LOOPBACK(address
)) {
109 DEBUG(ctx
, "Start address is loopback address\n");
113 // Address cannot be link-local
114 if (IN6_IS_ADDR_LINKLOCAL(address
)) {
115 DEBUG(ctx
, "Start address cannot be link-local\n");
119 // Address cannot be site-local
120 if (IN6_IS_ADDR_SITELOCAL(address
)) {
121 DEBUG(ctx
, "Start address cannot be site-local\n");
125 // Validate the prefix
126 if (valid_prefix(address
, prefix
) != 0) {
127 DEBUG(ctx
, "Invalid prefix: %u\n", prefix
);
131 struct loc_network
* n
= calloc(1, sizeof(*n
));
135 n
->ctx
= loc_ref(ctx
);
141 // Convert the prefix into a bitmask
142 struct in6_addr bitmask
= prefix_to_bitmask(n
->prefix
);
144 // Store the first and last address in the network
145 n
->first_address
= make_first_address(address
, &bitmask
);
146 n
->last_address
= make_last_address(&n
->first_address
, &bitmask
);
148 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
153 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
154 const char* address_string
) {
155 struct in6_addr first_address
;
156 unsigned int prefix
= 0;
160 // Make a copy of the string to work on it
161 char* buffer
= strdup(address_string
);
162 address_string
= prefix_string
= buffer
;
164 // Split address and prefix
165 address_string
= strsep(&prefix_string
, "/");
167 // Did we find a prefix?
169 // Convert prefix to integer
170 prefix
= strtol(prefix_string
, NULL
, 10);
174 r
= loc_parse_address(ctx
, address_string
, &first_address
);
176 // Map the prefix to IPv6 if needed
177 if (IN6_IS_ADDR_V4MAPPED(&first_address
))
182 // Free temporary buffer
186 r
= loc_network_new(ctx
, network
, &first_address
, prefix
);
192 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
198 static void loc_network_free(struct loc_network
* network
) {
199 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
201 loc_unref(network
->ctx
);
205 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
209 if (--network
->refcount
> 0)
212 loc_network_free(network
);
216 static int format_ipv6_address(const struct in6_addr
* address
, char* string
, size_t length
) {
217 const char* ret
= inet_ntop(AF_INET6
, address
, string
, length
);
224 static int format_ipv4_address(const struct in6_addr
* address
, char* string
, size_t length
) {
225 struct in_addr ipv4_address
;
226 ipv4_address
.s_addr
= address
->s6_addr32
[3];
228 const char* ret
= inet_ntop(AF_INET
, &ipv4_address
, string
, length
);
235 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
237 const size_t length
= INET6_ADDRSTRLEN
+ 4;
239 char* string
= malloc(length
);
243 unsigned int prefix
= network
->prefix
;
245 int family
= loc_network_address_family(network
);
248 r
= format_ipv6_address(&network
->first_address
, string
, length
);
252 r
= format_ipv4_address(&network
->first_address
, string
, length
);
262 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
269 sprintf(string
+ strlen(string
), "/%u", prefix
);
274 LOC_EXPORT
int loc_network_address_family(struct loc_network
* network
) {
275 if (IN6_IS_ADDR_V4MAPPED(&network
->first_address
))
281 static char* loc_network_format_address(struct loc_network
* network
, const struct in6_addr
* address
) {
282 const size_t length
= INET6_ADDRSTRLEN
;
284 char* string
= malloc(length
);
290 switch (loc_network_address_family(network
)) {
292 r
= format_ipv6_address(address
, string
, length
);
296 r
= format_ipv4_address(address
, string
, length
);
305 ERROR(network
->ctx
, "Could not format IP address to string: %s\n", strerror(errno
));
314 LOC_EXPORT
char* loc_network_format_first_address(struct loc_network
* network
) {
315 return loc_network_format_address(network
, &network
->first_address
);
318 LOC_EXPORT
char* loc_network_format_last_address(struct loc_network
* network
) {
319 return loc_network_format_address(network
, &network
->last_address
);
322 LOC_EXPORT
int loc_network_match_address(struct loc_network
* network
, const struct in6_addr
* address
) {
323 // Address must be larger than the start address
324 if (in6_addr_cmp(&network
->first_address
, address
) > 0)
327 // Address must be smaller than the last address
328 if (in6_addr_cmp(&network
->last_address
, address
) < 0)
331 // The address is inside this network
335 LOC_EXPORT
const char* loc_network_get_country_code(struct loc_network
* network
) {
336 return network
->country_code
;
339 LOC_EXPORT
int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
) {
340 // Set empty country code
341 if (!country_code
|| !*country_code
) {
342 *network
->country_code
= '\0';
346 // Check country code
347 if (!loc_country_code_is_valid(country_code
))
350 loc_country_code_copy(network
->country_code
, country_code
);
355 LOC_EXPORT
int loc_network_match_country_code(struct loc_network
* network
, const char* country_code
) {
356 // Check country code
357 if (!loc_country_code_is_valid(country_code
))
360 return (network
->country_code
[0] == country_code
[0])
361 && (network
->country_code
[1] == country_code
[1]);
364 LOC_EXPORT
uint32_t loc_network_get_asn(struct loc_network
* network
) {
368 LOC_EXPORT
int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
) {
374 LOC_EXPORT
int loc_network_match_asn(struct loc_network
* network
, uint32_t asn
) {
375 return network
->asn
== asn
;
378 LOC_EXPORT
int loc_network_has_flag(struct loc_network
* network
, uint32_t flag
) {
379 return network
->flags
& flag
;
382 LOC_EXPORT
int loc_network_set_flag(struct loc_network
* network
, uint32_t flag
) {
383 network
->flags
|= flag
;
388 LOC_EXPORT
int loc_network_match_flag(struct loc_network
* network
, uint32_t flag
) {
389 return loc_network_has_flag(network
, flag
);
392 LOC_EXPORT
int loc_network_is_subnet_of(struct loc_network
* self
, struct loc_network
* other
) {
393 // If the start address of the other network is smaller than this network,
394 // it cannot be a subnet.
395 if (in6_addr_cmp(&self
->first_address
, &other
->first_address
) < 0)
398 // If the end address of the other network is greater than this network,
399 // it cannot be a subnet.
400 if (in6_addr_cmp(&self
->last_address
, &other
->last_address
) > 0)
406 LOC_EXPORT
int loc_network_to_database_v1(struct loc_network
* network
, struct loc_database_network_v1
* dbobj
) {
408 loc_country_code_copy(dbobj
->country_code
, network
->country_code
);
411 dbobj
->asn
= htobe32(network
->asn
);
414 dbobj
->flags
= htobe16(network
->flags
);
419 LOC_EXPORT
int loc_network_new_from_database_v1(struct loc_ctx
* ctx
, struct loc_network
** network
,
420 struct in6_addr
* address
, unsigned int prefix
, const struct loc_database_network_v1
* dbobj
) {
421 char country_code
[3] = "\0\0";
423 int r
= loc_network_new(ctx
, network
, address
, prefix
);
425 ERROR(ctx
, "Could not allocate a new network: %s", strerror(-r
));
429 // Import country code
430 loc_country_code_copy(country_code
, dbobj
->country_code
);
432 r
= loc_network_set_country_code(*network
, country_code
);
434 ERROR(ctx
, "Could not set country code: %s\n", country_code
);
439 uint32_t asn
= be32toh(dbobj
->asn
);
440 r
= loc_network_set_asn(*network
, asn
);
442 ERROR(ctx
, "Could not set ASN: %d\n", asn
);
447 int flags
= be16toh(dbobj
->flags
);
448 r
= loc_network_set_flag(*network
, flags
);
450 ERROR(ctx
, "Could not set flags: %d\n", flags
);
457 struct loc_network_tree
{
461 struct loc_network_tree_node
* root
;
464 struct loc_network_tree_node
{
468 struct loc_network_tree_node
* zero
;
469 struct loc_network_tree_node
* one
;
471 struct loc_network
* network
;
474 LOC_EXPORT
int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
475 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
479 t
->ctx
= loc_ref(ctx
);
482 // Create the root node
483 int r
= loc_network_tree_node_new(ctx
, &t
->root
);
485 loc_network_tree_unref(t
);
489 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
494 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
) {
495 return loc_network_tree_node_ref(tree
->root
);
498 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
499 struct loc_network_tree_node
** n
;
506 // If the desired node doesn't exist, yet, we will create it
508 int r
= loc_network_tree_node_new(node
->ctx
, n
);
516 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
, unsigned int prefix
) {
517 struct loc_network_tree_node
* node
= tree
->root
;
519 for (unsigned int i
= 0; i
< prefix
; i
++) {
520 // Check if the ith bit is one or zero
521 node
= loc_network_tree_get_node(node
, in6_addr_get_bit(address
, i
));
527 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
528 int(*filter_callback
)(struct loc_network
* network
, void* data
),
529 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
532 // Finding a network ends the walk here
534 if (filter_callback
) {
535 int f
= filter_callback(node
->network
, data
);
539 // Skip network if filter function returns value greater than zero
544 r
= callback(node
->network
, data
);
549 // Walk down on the left side of the tree first
551 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
556 // Then walk on the other side
558 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
566 LOC_EXPORT
int loc_network_tree_walk(struct loc_network_tree
* tree
,
567 int(*filter_callback
)(struct loc_network
* network
, void* data
),
568 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
569 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
572 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
573 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
575 loc_network_tree_node_unref(tree
->root
);
577 loc_unref(tree
->ctx
);
581 LOC_EXPORT
struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
582 if (--tree
->refcount
> 0)
585 loc_network_tree_free(tree
);
589 static int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
590 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
592 char* s
= loc_network_str(network
);
596 INFO(network
->ctx
, "%s\n", s
);
602 LOC_EXPORT
int loc_network_tree_dump(struct loc_network_tree
* tree
) {
603 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
605 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
608 LOC_EXPORT
int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
609 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
611 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
,
612 &network
->first_address
, network
->prefix
);
614 ERROR(tree
->ctx
, "Could not find a node\n");
618 // Check if node has not been set before
620 DEBUG(tree
->ctx
, "There is already a network at this path\n");
624 // Point node to the network
625 node
->network
= loc_network_ref(network
);
630 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
631 size_t* counter
= (size_t*)data
;
633 // Increase the counter for each network
639 LOC_EXPORT
size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
642 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);
649 static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node
* node
) {
653 counter
+= __loc_network_tree_count_nodes(node
->zero
);
656 counter
+= __loc_network_tree_count_nodes(node
->one
);
661 LOC_EXPORT
size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
) {
662 return __loc_network_tree_count_nodes(tree
->root
);
665 LOC_EXPORT
int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
) {
666 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
670 n
->ctx
= loc_ref(ctx
);
673 n
->zero
= n
->one
= NULL
;
675 DEBUG(n
->ctx
, "Network node allocated at %p\n", n
);
680 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
) {
687 static void loc_network_tree_node_free(struct loc_network_tree_node
* node
) {
688 DEBUG(node
->ctx
, "Releasing network node at %p\n", node
);
691 loc_network_unref(node
->network
);
694 loc_network_tree_node_unref(node
->zero
);
697 loc_network_tree_node_unref(node
->one
);
699 loc_unref(node
->ctx
);
703 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
) {
707 if (--node
->refcount
> 0)
710 loc_network_tree_node_free(node
);
714 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
) {
723 return loc_network_tree_node_ref(node
);
726 LOC_EXPORT
int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
) {
727 return (!!node
->network
);
730 LOC_EXPORT
struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
) {
731 return loc_network_ref(node
->network
);