]>
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>
25 #include <loc/libloc.h>
26 #include <loc/network.h>
27 #include <loc/private.h>
33 struct in6_addr start_address
;
40 static int valid_prefix(struct in6_addr
* address
, unsigned int prefix
) {
41 // The prefix cannot be larger than 128 bits
45 // And the prefix cannot be zero
49 // For IPv4-mapped addresses the prefix has to be 96 or lager
50 if (IN6_IS_ADDR_V4MAPPED(address
) && prefix
<= 96)
56 static struct in6_addr
prefix_to_bitmask(unsigned int prefix
) {
57 struct in6_addr bitmask
;
59 for (unsigned int i
= 0; i
< 16; i
++)
60 bitmask
.s6_addr
[i
] = 0;
62 for (unsigned int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
64 bitmask
.s6_addr
[j
] = 0xff;
66 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
72 static struct in6_addr
make_start_address(struct in6_addr
* address
, unsigned int prefix
) {
74 struct in6_addr bitmask
= prefix_to_bitmask(prefix
);
76 // Perform bitwise AND
77 for (unsigned int i
= 0; i
< 4; i
++)
78 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] & bitmask
.s6_addr32
[i
];
83 LOC_EXPORT
int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
84 struct in6_addr
* address
, unsigned int prefix
) {
85 // Address cannot be unspecified
86 if (IN6_IS_ADDR_UNSPECIFIED(&address
)) {
87 DEBUG(ctx
, "Start address is unspecified\n");
91 // Address cannot be loopback
92 if (IN6_IS_ADDR_LOOPBACK(&address
)) {
93 DEBUG(ctx
, "Start address is loopback address\n");
97 // Address cannot be link-local
98 if (IN6_IS_ADDR_LINKLOCAL(&address
)) {
99 DEBUG(ctx
, "Start address cannot be link-local\n");
103 // Address cannot be site-local
104 if (IN6_IS_ADDR_SITELOCAL(&address
)) {
105 DEBUG(ctx
, "Start address cannot be site-local\n");
109 // Validate the prefix
110 if (valid_prefix(address
, prefix
) != 0) {
111 DEBUG(ctx
, "Invalid prefix: %u\n", prefix
);
115 struct loc_network
* n
= calloc(1, sizeof(*n
));
119 n
->ctx
= loc_ref(ctx
);
122 // Store the first address in the network
123 n
->start_address
= make_start_address(address
, prefix
);
126 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
131 static int loc_network_address_family(struct loc_network
* network
) {
132 if (IN6_IS_ADDR_V4MAPPED(&network
->start_address
))
138 static int parse_address(struct loc_ctx
* ctx
, const char* string
, struct in6_addr
* address
) {
139 DEBUG(ctx
, "Paring IP address %s\n", string
);
141 // Try parsing this as an IPv6 address
142 int r
= inet_pton(AF_INET6
, string
, address
);
144 // If inet_pton returns one it has been successful
146 DEBUG(ctx
, "%s is an IPv6 address\n", string
);
150 // Try parsing this as an IPv4 address
151 struct in_addr ipv4_address
;
152 r
= inet_pton(AF_INET
, string
, &ipv4_address
);
154 DEBUG(ctx
, "%s is an IPv4 address\n", string
);
156 // Convert to IPv6-mapped address
157 address
->s6_addr32
[0] = htonl(0x0000);
158 address
->s6_addr32
[1] = htonl(0x0000);
159 address
->s6_addr32
[2] = htonl(0xffff);
160 address
->s6_addr32
[3] = ipv4_address
.s_addr
;
165 DEBUG(ctx
, "%s is not an valid IP address\n", string
);
169 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
170 const char* address_string
) {
171 struct in6_addr start_address
;
172 unsigned int prefix
= 0;
176 // Make a copy of the string to work on it
177 char* buffer
= strdup(address_string
);
178 address_string
= prefix_string
= buffer
;
180 // Split address and prefix
181 address_string
= strsep(&prefix_string
, "/");
183 // Did we find a prefix?
185 // Convert prefix to integer
186 prefix
= strtol(prefix_string
, NULL
, 10);
190 r
= parse_address(ctx
, address_string
, &start_address
);
192 // Map the prefix to IPv6 if needed
193 if (IN6_IS_ADDR_V4MAPPED(&start_address
))
198 // Free temporary buffer
202 r
= loc_network_new(ctx
, network
, &start_address
, prefix
);
208 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
214 static void loc_network_free(struct loc_network
* network
) {
215 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
217 loc_unref(network
->ctx
);
221 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
222 if (--network
->refcount
> 0)
225 loc_network_free(network
);
229 static int format_ipv6_address(struct loc_network
* network
, char* string
, size_t length
) {
230 const char* ret
= inet_ntop(AF_INET6
, &network
->start_address
, string
, length
);
237 static int format_ipv4_address(struct loc_network
* network
, char* string
, size_t length
) {
238 struct in_addr ipv4_address
;
239 ipv4_address
.s_addr
= network
->start_address
.s6_addr32
[3];
241 const char* ret
= inet_ntop(AF_INET
, &ipv4_address
, string
, length
);
248 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
250 const size_t length
= INET6_ADDRSTRLEN
+ 4;
252 char* string
= malloc(length
);
256 unsigned int prefix
= network
->prefix
;
258 int family
= loc_network_address_family(network
);
261 r
= format_ipv6_address(network
, string
, length
);
265 r
= format_ipv4_address(network
, string
, length
);
275 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
282 sprintf(string
+ strlen(string
), "/%u", prefix
);
287 LOC_EXPORT
const char* loc_network_get_country_code(struct loc_network
* network
) {
288 return network
->country_code
;
291 LOC_EXPORT
int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
) {
292 // Country codes must be two characters
293 if (strlen(country_code
) != 2)
296 for (unsigned int i
= 0; i
< 3; i
++) {
297 network
->country_code
[i
] = country_code
[i
];
303 LOC_EXPORT
uint32_t loc_network_get_asn(struct loc_network
* network
) {
307 LOC_EXPORT
int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
) {
313 LOC_EXPORT
int loc_network_to_database_v0(struct loc_network
* network
, struct loc_database_network_v0
* dbobj
) {
314 dbobj
->prefix
= network
->prefix
;
317 for (unsigned int i
= 0; i
< 2; i
++) {
318 dbobj
->country_code
[i
] = network
->country_code
? network
->country_code
[i
] : '\0';
322 dbobj
->asn
= htobe32(network
->asn
);
327 LOC_EXPORT
int loc_network_new_from_database_v0(struct loc_ctx
* ctx
, struct loc_network
** network
,
328 struct in6_addr
* address
, const struct loc_database_network_v0
* dbobj
) {
329 int r
= loc_network_new(ctx
, network
, address
, dbobj
->prefix
);
333 // Import country code
334 char country_code
[3];
335 for (unsigned int i
= 0; i
< 2; i
++) {
336 country_code
[i
] = dbobj
->country_code
[i
];
338 country_code
[2] = '\0';
340 r
= loc_network_set_country_code(*network
, country_code
);
345 r
= loc_network_set_asn(*network
, be32toh(dbobj
->asn
));
352 struct loc_network_tree
{
356 struct loc_network_tree_node
* root
;
359 struct loc_network_tree_node
{
363 struct loc_network_tree_node
* zero
;
364 struct loc_network_tree_node
* one
;
366 struct loc_network
* network
;
369 LOC_EXPORT
int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
370 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
374 t
->ctx
= loc_ref(ctx
);
377 // Create the root node
378 int r
= loc_network_tree_node_new(ctx
, &t
->root
);
380 loc_network_tree_unref(t
);
384 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
389 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
) {
390 return loc_network_tree_node_ref(tree
->root
);
393 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
394 struct loc_network_tree_node
** n
;
401 // If the desired node doesn't exist, yet, we will create it
403 int r
= loc_network_tree_node_new(node
->ctx
, n
);
411 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
) {
412 struct loc_network_tree_node
* node
= tree
->root
;
414 for (unsigned int i
= 127; i
> 0; i
--) {
415 // Check if the ith bit is one or zero
416 node
= loc_network_tree_get_node(node
, ((address
->s6_addr32
[i
/ 32] & (1 << (i
% 32))) == 0));
422 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
423 int(*filter_callback
)(struct loc_network
* network
, void* data
),
424 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
427 // Finding a network ends the walk here
429 if (filter_callback
) {
430 int f
= filter_callback(node
->network
, data
);
434 // Skip network if filter function returns value greater than zero
439 r
= callback(node
->network
, data
);
444 // Walk down on the left side of the tree first
446 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
451 // Then walk on the other side
453 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
461 LOC_EXPORT
int loc_network_tree_walk(struct loc_network_tree
* tree
,
462 int(*filter_callback
)(struct loc_network
* network
, void* data
),
463 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
464 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
467 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
468 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
470 loc_network_tree_node_unref(tree
->root
);
472 loc_unref(tree
->ctx
);
476 LOC_EXPORT
struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
477 if (--tree
->refcount
> 0)
480 loc_network_tree_free(tree
);
484 static int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
485 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
487 char* s
= loc_network_str(network
);
491 INFO(network
->ctx
, "%s\n", s
);
497 LOC_EXPORT
int loc_network_tree_dump(struct loc_network_tree
* tree
) {
498 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
500 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
503 LOC_EXPORT
int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
504 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
506 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
, &network
->start_address
);
508 ERROR(tree
->ctx
, "Could not find a node\n");
512 // Check if node has not been set before
514 DEBUG(tree
->ctx
, "There is already a network at this path\n");
518 // Point node to the network
519 node
->network
= loc_network_ref(network
);
524 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
525 size_t* counter
= (size_t*)data
;
527 // Increase the counter for each network
533 LOC_EXPORT
size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
536 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);
543 static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node
* node
) {
547 counter
+= __loc_network_tree_count_nodes(node
->zero
);
550 counter
+= __loc_network_tree_count_nodes(node
->one
);
555 LOC_EXPORT
size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
) {
556 return __loc_network_tree_count_nodes(tree
->root
);
559 LOC_EXPORT
int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
) {
560 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
564 n
->ctx
= loc_ref(ctx
);
567 n
->zero
= n
->one
= NULL
;
569 DEBUG(n
->ctx
, "Network node allocated at %p\n", n
);
574 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
) {
581 static void loc_network_tree_node_free(struct loc_network_tree_node
* node
) {
582 DEBUG(node
->ctx
, "Releasing network node at %p\n", node
);
585 loc_network_unref(node
->network
);
588 loc_network_tree_node_unref(node
->zero
);
591 loc_network_tree_node_unref(node
->one
);
593 loc_unref(node
->ctx
);
597 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
) {
601 if (--node
->refcount
> 0)
604 loc_network_tree_node_free(node
);
608 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
) {
617 return loc_network_tree_node_ref(node
);
620 LOC_EXPORT
int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
) {
621 return (!!node
->network
);
624 LOC_EXPORT
struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
) {
625 return loc_network_ref(node
->network
);