]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/network.c
0a22b7632b5f89a7841726a3543f60ed1e860924
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>
25 #include <loc/libloc.h>
26 #include <loc/country.h>
27 #include <loc/network.h>
28 #include <loc/private.h>
34 struct in6_addr start_address
;
39 enum loc_network_flags flags
;
42 static int valid_prefix(struct in6_addr
* address
, unsigned int prefix
) {
43 // The prefix cannot be larger than 128 bits
47 // And the prefix cannot be zero
51 // For IPv4-mapped addresses the prefix has to be 96 or lager
52 if (IN6_IS_ADDR_V4MAPPED(address
) && prefix
<= 96)
58 static struct in6_addr
prefix_to_bitmask(unsigned int prefix
) {
59 struct in6_addr bitmask
;
61 for (unsigned int i
= 0; i
< 16; i
++)
62 bitmask
.s6_addr
[i
] = 0;
64 for (int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
66 bitmask
.s6_addr
[j
] = 0xff;
68 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
74 static struct in6_addr
make_start_address(const struct in6_addr
* address
, unsigned int prefix
) {
76 struct in6_addr bitmask
= prefix_to_bitmask(prefix
);
78 // Perform bitwise AND
79 for (unsigned int i
= 0; i
< 4; i
++)
80 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] & bitmask
.s6_addr32
[i
];
85 static struct in6_addr
make_last_address(const struct in6_addr
* address
, unsigned int prefix
) {
87 struct in6_addr bitmask
= prefix_to_bitmask(prefix
);
90 for (unsigned int i
= 0; i
< 4; i
++)
91 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] | ~bitmask
.s6_addr32
[i
];
96 LOC_EXPORT
int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
97 struct in6_addr
* address
, unsigned int prefix
) {
98 // Address cannot be unspecified
99 if (IN6_IS_ADDR_UNSPECIFIED(address
)) {
100 DEBUG(ctx
, "Start address is unspecified\n");
104 // Address cannot be loopback
105 if (IN6_IS_ADDR_LOOPBACK(address
)) {
106 DEBUG(ctx
, "Start address is loopback address\n");
110 // Address cannot be link-local
111 if (IN6_IS_ADDR_LINKLOCAL(address
)) {
112 DEBUG(ctx
, "Start address cannot be link-local\n");
116 // Address cannot be site-local
117 if (IN6_IS_ADDR_SITELOCAL(address
)) {
118 DEBUG(ctx
, "Start address cannot be site-local\n");
122 // Validate the prefix
123 if (valid_prefix(address
, prefix
) != 0) {
124 DEBUG(ctx
, "Invalid prefix: %u\n", prefix
);
128 struct loc_network
* n
= calloc(1, sizeof(*n
));
132 n
->ctx
= loc_ref(ctx
);
135 // Store the first address in the network
136 n
->start_address
= make_start_address(address
, prefix
);
139 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
144 static int loc_network_address_family(struct loc_network
* network
) {
145 if (IN6_IS_ADDR_V4MAPPED(&network
->start_address
))
151 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
152 const char* address_string
) {
153 struct in6_addr start_address
;
154 unsigned int prefix
= 0;
158 // Make a copy of the string to work on it
159 char* buffer
= strdup(address_string
);
160 address_string
= prefix_string
= buffer
;
162 // Split address and prefix
163 address_string
= strsep(&prefix_string
, "/");
165 // Did we find a prefix?
167 // Convert prefix to integer
168 prefix
= strtol(prefix_string
, NULL
, 10);
172 r
= loc_parse_address(ctx
, address_string
, &start_address
);
174 // Map the prefix to IPv6 if needed
175 if (IN6_IS_ADDR_V4MAPPED(&start_address
))
180 // Free temporary buffer
184 r
= loc_network_new(ctx
, network
, &start_address
, prefix
);
190 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
196 static void loc_network_free(struct loc_network
* network
) {
197 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
199 loc_unref(network
->ctx
);
203 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
207 if (--network
->refcount
> 0)
210 loc_network_free(network
);
214 static int format_ipv6_address(const struct in6_addr
* address
, char* string
, size_t length
) {
215 const char* ret
= inet_ntop(AF_INET6
, address
, string
, length
);
222 static int format_ipv4_address(const struct in6_addr
* address
, char* string
, size_t length
) {
223 struct in_addr ipv4_address
;
224 ipv4_address
.s_addr
= address
->s6_addr32
[3];
226 const char* ret
= inet_ntop(AF_INET
, &ipv4_address
, string
, length
);
233 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
235 const size_t length
= INET6_ADDRSTRLEN
+ 4;
237 char* string
= malloc(length
);
241 unsigned int prefix
= network
->prefix
;
243 int family
= loc_network_address_family(network
);
246 r
= format_ipv6_address(&network
->start_address
, string
, length
);
250 r
= format_ipv4_address(&network
->start_address
, string
, length
);
260 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
267 sprintf(string
+ strlen(string
), "/%u", prefix
);
272 LOC_EXPORT
int loc_network_match_address(struct loc_network
* network
, const struct in6_addr
* address
) {
273 // Address must be larger than the start address
274 if (in6_addr_cmp(&network
->start_address
, address
) > 0)
277 // Determine the last address in this network
278 struct in6_addr last_address
= make_last_address(&network
->start_address
, network
->prefix
);
280 // Address must be smaller than the last address
281 if (in6_addr_cmp(address
, &last_address
) > 0)
284 // The address is inside this network
288 LOC_EXPORT
const char* loc_network_get_country_code(struct loc_network
* network
) {
289 return network
->country_code
;
292 LOC_EXPORT
int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
) {
293 // Set empty country code
294 if (!country_code
|| !*country_code
) {
295 *network
->country_code
= '\0';
299 // Check country code
300 if (!loc_country_code_is_valid(country_code
))
303 loc_country_code_copy(network
->country_code
, country_code
);
308 LOC_EXPORT
int loc_network_match_country_code(struct loc_network
* network
, const char* country_code
) {
309 // Check country code
310 if (!loc_country_code_is_valid(country_code
))
313 return (network
->country_code
[0] == country_code
[0])
314 && (network
->country_code
[1] == country_code
[1]);
317 LOC_EXPORT
uint32_t loc_network_get_asn(struct loc_network
* network
) {
321 LOC_EXPORT
int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
) {
327 LOC_EXPORT
int loc_network_match_asn(struct loc_network
* network
, uint32_t asn
) {
328 return network
->asn
== asn
;
331 LOC_EXPORT
int loc_network_has_flag(struct loc_network
* network
, uint32_t flag
) {
332 return network
->flags
& flag
;
335 LOC_EXPORT
int loc_network_set_flag(struct loc_network
* network
, uint32_t flag
) {
336 network
->flags
|= flag
;
341 LOC_EXPORT
int loc_network_match_flag(struct loc_network
* network
, uint32_t flag
) {
342 return loc_network_has_flag(network
, flag
);
345 LOC_EXPORT
int loc_network_to_database_v0(struct loc_network
* network
, struct loc_database_network_v0
* dbobj
) {
347 loc_country_code_copy(dbobj
->country_code
, network
->country_code
);
350 dbobj
->asn
= htobe32(network
->asn
);
353 dbobj
->flags
= htobe16(network
->flags
);
358 LOC_EXPORT
int loc_network_new_from_database_v0(struct loc_ctx
* ctx
, struct loc_network
** network
,
359 struct in6_addr
* address
, unsigned int prefix
, const struct loc_database_network_v0
* dbobj
) {
360 char country_code
[3];
362 int r
= loc_network_new(ctx
, network
, address
, prefix
);
366 // Import country code
367 loc_country_code_copy(country_code
, dbobj
->country_code
);
369 r
= loc_network_set_country_code(*network
, country_code
);
374 r
= loc_network_set_asn(*network
, be32toh(dbobj
->asn
));
379 r
= loc_network_set_flag(*network
, be16toh(dbobj
->flags
));
386 struct loc_network_tree
{
390 struct loc_network_tree_node
* root
;
393 struct loc_network_tree_node
{
397 struct loc_network_tree_node
* zero
;
398 struct loc_network_tree_node
* one
;
400 struct loc_network
* network
;
403 LOC_EXPORT
int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
404 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
408 t
->ctx
= loc_ref(ctx
);
411 // Create the root node
412 int r
= loc_network_tree_node_new(ctx
, &t
->root
);
414 loc_network_tree_unref(t
);
418 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
423 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
) {
424 return loc_network_tree_node_ref(tree
->root
);
427 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
428 struct loc_network_tree_node
** n
;
435 // If the desired node doesn't exist, yet, we will create it
437 int r
= loc_network_tree_node_new(node
->ctx
, n
);
445 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
, unsigned int prefix
) {
446 struct loc_network_tree_node
* node
= tree
->root
;
448 for (unsigned int i
= 0; i
< prefix
; i
++) {
449 // Check if the ith bit is one or zero
450 node
= loc_network_tree_get_node(node
, in6_addr_get_bit(address
, i
));
456 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
457 int(*filter_callback
)(struct loc_network
* network
, void* data
),
458 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
461 // Finding a network ends the walk here
463 if (filter_callback
) {
464 int f
= filter_callback(node
->network
, data
);
468 // Skip network if filter function returns value greater than zero
473 r
= callback(node
->network
, data
);
478 // Walk down on the left side of the tree first
480 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
485 // Then walk on the other side
487 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
495 LOC_EXPORT
int loc_network_tree_walk(struct loc_network_tree
* tree
,
496 int(*filter_callback
)(struct loc_network
* network
, void* data
),
497 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
498 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
501 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
502 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
504 loc_network_tree_node_unref(tree
->root
);
506 loc_unref(tree
->ctx
);
510 LOC_EXPORT
struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
511 if (--tree
->refcount
> 0)
514 loc_network_tree_free(tree
);
518 static int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
519 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
521 char* s
= loc_network_str(network
);
525 INFO(network
->ctx
, "%s\n", s
);
531 LOC_EXPORT
int loc_network_tree_dump(struct loc_network_tree
* tree
) {
532 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
534 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
537 LOC_EXPORT
int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
538 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
540 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
,
541 &network
->start_address
, network
->prefix
);
543 ERROR(tree
->ctx
, "Could not find a node\n");
547 // Check if node has not been set before
549 DEBUG(tree
->ctx
, "There is already a network at this path\n");
553 // Point node to the network
554 node
->network
= loc_network_ref(network
);
559 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
560 size_t* counter
= (size_t*)data
;
562 // Increase the counter for each network
568 LOC_EXPORT
size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
571 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);
578 static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node
* node
) {
582 counter
+= __loc_network_tree_count_nodes(node
->zero
);
585 counter
+= __loc_network_tree_count_nodes(node
->one
);
590 LOC_EXPORT
size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
) {
591 return __loc_network_tree_count_nodes(tree
->root
);
594 LOC_EXPORT
int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
) {
595 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
599 n
->ctx
= loc_ref(ctx
);
602 n
->zero
= n
->one
= NULL
;
604 DEBUG(n
->ctx
, "Network node allocated at %p\n", n
);
609 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
) {
616 static void loc_network_tree_node_free(struct loc_network_tree_node
* node
) {
617 DEBUG(node
->ctx
, "Releasing network node at %p\n", node
);
620 loc_network_unref(node
->network
);
623 loc_network_tree_node_unref(node
->zero
);
626 loc_network_tree_node_unref(node
->one
);
628 loc_unref(node
->ctx
);
632 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
) {
636 if (--node
->refcount
> 0)
639 loc_network_tree_node_free(node
);
643 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
) {
652 return loc_network_tree_node_ref(node
);
655 LOC_EXPORT
int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
) {
656 return (!!node
->network
);
659 LOC_EXPORT
struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
) {
660 return loc_network_ref(node
->network
);