]>
git.ipfire.org Git - location/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>
38 struct in6_addr start_address
;
43 enum loc_network_flags flags
;
46 static int valid_prefix(struct in6_addr
* address
, unsigned int prefix
) {
47 // The prefix cannot be larger than 128 bits
51 // And the prefix cannot be zero
55 // For IPv4-mapped addresses the prefix has to be 96 or lager
56 if (IN6_IS_ADDR_V4MAPPED(address
) && prefix
<= 96)
62 static struct in6_addr
prefix_to_bitmask(unsigned int prefix
) {
63 struct in6_addr bitmask
;
65 for (unsigned int i
= 0; i
< 16; i
++)
66 bitmask
.s6_addr
[i
] = 0;
68 for (int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
70 bitmask
.s6_addr
[j
] = 0xff;
72 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
78 static struct in6_addr
make_start_address(const struct in6_addr
* address
, unsigned int prefix
) {
80 struct in6_addr bitmask
= prefix_to_bitmask(prefix
);
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
, unsigned int prefix
) {
91 struct in6_addr bitmask
= prefix_to_bitmask(prefix
);
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
);
139 // Store the first address in the network
140 n
->start_address
= make_start_address(address
, prefix
);
143 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
148 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
149 const char* address_string
) {
150 struct in6_addr start_address
;
151 unsigned int prefix
= 0;
155 // Make a copy of the string to work on it
156 char* buffer
= strdup(address_string
);
157 address_string
= prefix_string
= buffer
;
159 // Split address and prefix
160 address_string
= strsep(&prefix_string
, "/");
162 // Did we find a prefix?
164 // Convert prefix to integer
165 prefix
= strtol(prefix_string
, NULL
, 10);
169 r
= loc_parse_address(ctx
, address_string
, &start_address
);
171 // Map the prefix to IPv6 if needed
172 if (IN6_IS_ADDR_V4MAPPED(&start_address
))
177 // Free temporary buffer
181 r
= loc_network_new(ctx
, network
, &start_address
, prefix
);
187 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
193 static void loc_network_free(struct loc_network
* network
) {
194 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
196 loc_unref(network
->ctx
);
200 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
204 if (--network
->refcount
> 0)
207 loc_network_free(network
);
211 static int format_ipv6_address(const struct in6_addr
* address
, char* string
, size_t length
) {
212 const char* ret
= inet_ntop(AF_INET6
, address
, string
, length
);
219 static int format_ipv4_address(const struct in6_addr
* address
, char* string
, size_t length
) {
220 struct in_addr ipv4_address
;
221 ipv4_address
.s_addr
= address
->s6_addr32
[3];
223 const char* ret
= inet_ntop(AF_INET
, &ipv4_address
, string
, length
);
230 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
232 const size_t length
= INET6_ADDRSTRLEN
+ 4;
234 char* string
= malloc(length
);
238 unsigned int prefix
= network
->prefix
;
240 int family
= loc_network_address_family(network
);
243 r
= format_ipv6_address(&network
->start_address
, string
, length
);
247 r
= format_ipv4_address(&network
->start_address
, string
, length
);
257 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
264 sprintf(string
+ strlen(string
), "/%u", prefix
);
269 LOC_EXPORT
int loc_network_address_family(struct loc_network
* network
) {
270 if (IN6_IS_ADDR_V4MAPPED(&network
->start_address
))
276 LOC_EXPORT
int loc_network_match_address(struct loc_network
* network
, const struct in6_addr
* address
) {
277 // Address must be larger than the start address
278 if (in6_addr_cmp(&network
->start_address
, address
) > 0)
281 // Determine the last address in this network
282 struct in6_addr last_address
= make_last_address(&network
->start_address
, network
->prefix
);
284 // Address must be smaller than the last address
285 if (in6_addr_cmp(address
, &last_address
) > 0)
288 // The address is inside this network
292 LOC_EXPORT
const char* loc_network_get_country_code(struct loc_network
* network
) {
293 return network
->country_code
;
296 LOC_EXPORT
int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
) {
297 // Set empty country code
298 if (!country_code
|| !*country_code
) {
299 *network
->country_code
= '\0';
303 // Check country code
304 if (!loc_country_code_is_valid(country_code
))
307 loc_country_code_copy(network
->country_code
, country_code
);
312 LOC_EXPORT
int loc_network_match_country_code(struct loc_network
* network
, const char* country_code
) {
313 // Check country code
314 if (!loc_country_code_is_valid(country_code
))
317 return (network
->country_code
[0] == country_code
[0])
318 && (network
->country_code
[1] == country_code
[1]);
321 LOC_EXPORT
uint32_t loc_network_get_asn(struct loc_network
* network
) {
325 LOC_EXPORT
int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
) {
331 LOC_EXPORT
int loc_network_match_asn(struct loc_network
* network
, uint32_t asn
) {
332 return network
->asn
== asn
;
335 LOC_EXPORT
int loc_network_has_flag(struct loc_network
* network
, uint32_t flag
) {
336 return network
->flags
& flag
;
339 LOC_EXPORT
int loc_network_set_flag(struct loc_network
* network
, uint32_t flag
) {
340 network
->flags
|= flag
;
345 LOC_EXPORT
int loc_network_match_flag(struct loc_network
* network
, uint32_t flag
) {
346 return loc_network_has_flag(network
, flag
);
349 LOC_EXPORT
int loc_network_to_database_v0(struct loc_network
* network
, struct loc_database_network_v0
* dbobj
) {
351 loc_country_code_copy(dbobj
->country_code
, network
->country_code
);
354 dbobj
->asn
= htobe32(network
->asn
);
357 dbobj
->flags
= htobe16(network
->flags
);
362 LOC_EXPORT
int loc_network_new_from_database_v0(struct loc_ctx
* ctx
, struct loc_network
** network
,
363 struct in6_addr
* address
, unsigned int prefix
, const struct loc_database_network_v0
* dbobj
) {
364 char country_code
[3] = "\0\0";
366 int r
= loc_network_new(ctx
, network
, address
, prefix
);
368 ERROR(ctx
, "Could not allocate a new network: %s", strerror(-r
));
372 // Import country code
373 loc_country_code_copy(country_code
, dbobj
->country_code
);
375 r
= loc_network_set_country_code(*network
, country_code
);
377 ERROR(ctx
, "Could not set country code: %s\n", country_code
);
382 uint32_t asn
= be32toh(dbobj
->asn
);
383 r
= loc_network_set_asn(*network
, asn
);
385 ERROR(ctx
, "Could not set ASN: %d\n", asn
);
390 int flags
= be16toh(dbobj
->flags
);
391 r
= loc_network_set_flag(*network
, flags
);
393 ERROR(ctx
, "Could not set flags: %d\n", flags
);
400 struct loc_network_tree
{
404 struct loc_network_tree_node
* root
;
407 struct loc_network_tree_node
{
411 struct loc_network_tree_node
* zero
;
412 struct loc_network_tree_node
* one
;
414 struct loc_network
* network
;
417 LOC_EXPORT
int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
418 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
422 t
->ctx
= loc_ref(ctx
);
425 // Create the root node
426 int r
= loc_network_tree_node_new(ctx
, &t
->root
);
428 loc_network_tree_unref(t
);
432 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
437 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
) {
438 return loc_network_tree_node_ref(tree
->root
);
441 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
442 struct loc_network_tree_node
** n
;
449 // If the desired node doesn't exist, yet, we will create it
451 int r
= loc_network_tree_node_new(node
->ctx
, n
);
459 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
, unsigned int prefix
) {
460 struct loc_network_tree_node
* node
= tree
->root
;
462 for (unsigned int i
= 0; i
< prefix
; i
++) {
463 // Check if the ith bit is one or zero
464 node
= loc_network_tree_get_node(node
, in6_addr_get_bit(address
, i
));
470 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
471 int(*filter_callback
)(struct loc_network
* network
, void* data
),
472 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
475 // Finding a network ends the walk here
477 if (filter_callback
) {
478 int f
= filter_callback(node
->network
, data
);
482 // Skip network if filter function returns value greater than zero
487 r
= callback(node
->network
, data
);
492 // Walk down on the left side of the tree first
494 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
499 // Then walk on the other side
501 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
509 LOC_EXPORT
int loc_network_tree_walk(struct loc_network_tree
* tree
,
510 int(*filter_callback
)(struct loc_network
* network
, void* data
),
511 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
512 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
515 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
516 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
518 loc_network_tree_node_unref(tree
->root
);
520 loc_unref(tree
->ctx
);
524 LOC_EXPORT
struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
525 if (--tree
->refcount
> 0)
528 loc_network_tree_free(tree
);
532 static int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
533 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
535 char* s
= loc_network_str(network
);
539 INFO(network
->ctx
, "%s\n", s
);
545 LOC_EXPORT
int loc_network_tree_dump(struct loc_network_tree
* tree
) {
546 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
548 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
551 LOC_EXPORT
int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
552 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
554 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
,
555 &network
->start_address
, network
->prefix
);
557 ERROR(tree
->ctx
, "Could not find a node\n");
561 // Check if node has not been set before
563 DEBUG(tree
->ctx
, "There is already a network at this path\n");
567 // Point node to the network
568 node
->network
= loc_network_ref(network
);
573 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
574 size_t* counter
= (size_t*)data
;
576 // Increase the counter for each network
582 LOC_EXPORT
size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
585 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);
592 static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node
* node
) {
596 counter
+= __loc_network_tree_count_nodes(node
->zero
);
599 counter
+= __loc_network_tree_count_nodes(node
->one
);
604 LOC_EXPORT
size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
) {
605 return __loc_network_tree_count_nodes(tree
->root
);
608 LOC_EXPORT
int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
) {
609 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
613 n
->ctx
= loc_ref(ctx
);
616 n
->zero
= n
->one
= NULL
;
618 DEBUG(n
->ctx
, "Network node allocated at %p\n", n
);
623 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
) {
630 static void loc_network_tree_node_free(struct loc_network_tree_node
* node
) {
631 DEBUG(node
->ctx
, "Releasing network node at %p\n", node
);
634 loc_network_unref(node
->network
);
637 loc_network_tree_node_unref(node
->zero
);
640 loc_network_tree_node_unref(node
->one
);
642 loc_unref(node
->ctx
);
646 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
) {
650 if (--node
->refcount
> 0)
653 loc_network_tree_node_free(node
);
657 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
) {
666 return loc_network_tree_node_ref(node
);
669 LOC_EXPORT
int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
) {
670 return (!!node
->network
);
673 LOC_EXPORT
struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
) {
674 return loc_network_ref(node
->network
);