]>
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
;
38 enum loc_network_flags flags
;
41 static int valid_prefix(struct in6_addr
* address
, unsigned int prefix
) {
42 // The prefix cannot be larger than 128 bits
46 // And the prefix cannot be zero
50 // For IPv4-mapped addresses the prefix has to be 96 or lager
51 if (IN6_IS_ADDR_V4MAPPED(address
) && prefix
<= 96)
57 static struct in6_addr
prefix_to_bitmask(unsigned int prefix
) {
58 struct in6_addr bitmask
;
60 for (unsigned int i
= 0; i
< 16; i
++)
61 bitmask
.s6_addr
[i
] = 0;
63 for (int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
65 bitmask
.s6_addr
[j
] = 0xff;
67 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
73 static struct in6_addr
make_start_address(const struct in6_addr
* address
, unsigned int prefix
) {
75 struct in6_addr bitmask
= prefix_to_bitmask(prefix
);
77 // Perform bitwise AND
78 for (unsigned int i
= 0; i
< 4; i
++)
79 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] & bitmask
.s6_addr32
[i
];
84 static struct in6_addr
make_last_address(const struct in6_addr
* address
, unsigned int prefix
) {
86 struct in6_addr bitmask
= prefix_to_bitmask(prefix
);
89 for (unsigned int i
= 0; i
< 4; i
++)
90 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] | ~bitmask
.s6_addr32
[i
];
95 LOC_EXPORT
int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
96 struct in6_addr
* address
, unsigned int prefix
) {
97 // Address cannot be unspecified
98 if (IN6_IS_ADDR_UNSPECIFIED(address
)) {
99 DEBUG(ctx
, "Start address is unspecified\n");
103 // Address cannot be loopback
104 if (IN6_IS_ADDR_LOOPBACK(address
)) {
105 DEBUG(ctx
, "Start address is loopback address\n");
109 // Address cannot be link-local
110 if (IN6_IS_ADDR_LINKLOCAL(address
)) {
111 DEBUG(ctx
, "Start address cannot be link-local\n");
115 // Address cannot be site-local
116 if (IN6_IS_ADDR_SITELOCAL(address
)) {
117 DEBUG(ctx
, "Start address cannot be site-local\n");
121 // Validate the prefix
122 if (valid_prefix(address
, prefix
) != 0) {
123 DEBUG(ctx
, "Invalid prefix: %u\n", prefix
);
127 struct loc_network
* n
= calloc(1, sizeof(*n
));
131 n
->ctx
= loc_ref(ctx
);
134 // Store the first address in the network
135 n
->start_address
= make_start_address(address
, prefix
);
138 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
143 static int loc_network_address_family(struct loc_network
* network
) {
144 if (IN6_IS_ADDR_V4MAPPED(&network
->start_address
))
150 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
151 const char* address_string
) {
152 struct in6_addr start_address
;
153 unsigned int prefix
= 0;
157 // Make a copy of the string to work on it
158 char* buffer
= strdup(address_string
);
159 address_string
= prefix_string
= buffer
;
161 // Split address and prefix
162 address_string
= strsep(&prefix_string
, "/");
164 // Did we find a prefix?
166 // Convert prefix to integer
167 prefix
= strtol(prefix_string
, NULL
, 10);
171 r
= loc_parse_address(ctx
, address_string
, &start_address
);
173 // Map the prefix to IPv6 if needed
174 if (IN6_IS_ADDR_V4MAPPED(&start_address
))
179 // Free temporary buffer
183 r
= loc_network_new(ctx
, network
, &start_address
, prefix
);
189 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
195 static void loc_network_free(struct loc_network
* network
) {
196 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
198 loc_unref(network
->ctx
);
202 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
206 if (--network
->refcount
> 0)
209 loc_network_free(network
);
213 static int format_ipv6_address(const struct in6_addr
* address
, char* string
, size_t length
) {
214 const char* ret
= inet_ntop(AF_INET6
, address
, string
, length
);
221 static int format_ipv4_address(const struct in6_addr
* address
, char* string
, size_t length
) {
222 struct in_addr ipv4_address
;
223 ipv4_address
.s_addr
= address
->s6_addr32
[3];
225 const char* ret
= inet_ntop(AF_INET
, &ipv4_address
, string
, length
);
232 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
234 const size_t length
= INET6_ADDRSTRLEN
+ 4;
236 char* string
= malloc(length
);
240 unsigned int prefix
= network
->prefix
;
242 int family
= loc_network_address_family(network
);
245 r
= format_ipv6_address(&network
->start_address
, string
, length
);
249 r
= format_ipv4_address(&network
->start_address
, string
, length
);
259 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
266 sprintf(string
+ strlen(string
), "/%u", prefix
);
271 LOC_EXPORT
int loc_network_match_address(struct loc_network
* network
, const struct in6_addr
* address
) {
272 // Address must be larger than the start address
273 if (in6_addr_cmp(&network
->start_address
, address
) > 0)
276 // Determine the last address in this network
277 struct in6_addr last_address
= make_last_address(&network
->start_address
, network
->prefix
);
279 // Address must be smaller than the last address
280 if (in6_addr_cmp(address
, &last_address
) > 0)
283 // The address is inside this network
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 // Set empty country code
293 if (!country_code
|| !*country_code
) {
294 *network
->country_code
= '\0';
298 // Country codes must be two characters
299 if (strlen(country_code
) != 2)
302 for (unsigned int i
= 0; i
< 3; i
++) {
303 network
->country_code
[i
] = country_code
[i
];
309 LOC_EXPORT
int loc_network_match_country_code(struct loc_network
* network
, const char* country_code
) {
310 // Country codes must be two characters
311 if (strlen(country_code
) != 2)
314 return (network
->country_code
[0] == country_code
[0])
315 && (network
->country_code
[1] == country_code
[1]);
318 LOC_EXPORT
uint32_t loc_network_get_asn(struct loc_network
* network
) {
322 LOC_EXPORT
int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
) {
328 LOC_EXPORT
int loc_network_match_asn(struct loc_network
* network
, uint32_t asn
) {
329 return network
->asn
== asn
;
332 LOC_EXPORT
int loc_network_has_flag(struct loc_network
* network
, uint32_t flag
) {
333 return network
->flags
& flag
;
336 LOC_EXPORT
int loc_network_set_flag(struct loc_network
* network
, uint32_t flag
) {
337 network
->flags
|= flag
;
342 LOC_EXPORT
int loc_network_match_flag(struct loc_network
* network
, uint32_t flag
) {
343 return loc_network_has_flag(network
, flag
);
346 LOC_EXPORT
int loc_network_to_database_v0(struct loc_network
* network
, struct loc_database_network_v0
* dbobj
) {
348 for (unsigned int i
= 0; i
< 2; i
++) {
349 dbobj
->country_code
[i
] = network
->country_code
[i
] ? network
->country_code
[i
] : '\0';
353 dbobj
->asn
= htobe32(network
->asn
);
356 dbobj
->flags
= htobe32(network
->flags
);
361 LOC_EXPORT
int loc_network_new_from_database_v0(struct loc_ctx
* ctx
, struct loc_network
** network
,
362 struct in6_addr
* address
, unsigned int prefix
, const struct loc_database_network_v0
* dbobj
) {
363 int r
= loc_network_new(ctx
, network
, address
, prefix
);
367 // Import country code
368 char country_code
[3];
369 for (unsigned int i
= 0; i
< 2; i
++) {
370 country_code
[i
] = dbobj
->country_code
[i
];
372 country_code
[2] = '\0';
374 r
= loc_network_set_country_code(*network
, country_code
);
379 r
= loc_network_set_asn(*network
, be32toh(dbobj
->asn
));
384 r
= loc_network_set_flag(*network
, be32toh(dbobj
->flags
));
391 struct loc_network_tree
{
395 struct loc_network_tree_node
* root
;
398 struct loc_network_tree_node
{
402 struct loc_network_tree_node
* zero
;
403 struct loc_network_tree_node
* one
;
405 struct loc_network
* network
;
408 LOC_EXPORT
int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
409 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
413 t
->ctx
= loc_ref(ctx
);
416 // Create the root node
417 int r
= loc_network_tree_node_new(ctx
, &t
->root
);
419 loc_network_tree_unref(t
);
423 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
428 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
) {
429 return loc_network_tree_node_ref(tree
->root
);
432 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
433 struct loc_network_tree_node
** n
;
440 // If the desired node doesn't exist, yet, we will create it
442 int r
= loc_network_tree_node_new(node
->ctx
, n
);
450 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
, unsigned int prefix
) {
451 struct loc_network_tree_node
* node
= tree
->root
;
453 for (unsigned int i
= 0; i
< prefix
; i
++) {
454 // Check if the ith bit is one or zero
455 node
= loc_network_tree_get_node(node
, in6_addr_get_bit(address
, i
));
461 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
462 int(*filter_callback
)(struct loc_network
* network
, void* data
),
463 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
466 // Finding a network ends the walk here
468 if (filter_callback
) {
469 int f
= filter_callback(node
->network
, data
);
473 // Skip network if filter function returns value greater than zero
478 r
= callback(node
->network
, data
);
483 // Walk down on the left side of the tree first
485 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
490 // Then walk on the other side
492 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
500 LOC_EXPORT
int loc_network_tree_walk(struct loc_network_tree
* tree
,
501 int(*filter_callback
)(struct loc_network
* network
, void* data
),
502 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
503 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
506 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
507 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
509 loc_network_tree_node_unref(tree
->root
);
511 loc_unref(tree
->ctx
);
515 LOC_EXPORT
struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
516 if (--tree
->refcount
> 0)
519 loc_network_tree_free(tree
);
523 static int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
524 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
526 char* s
= loc_network_str(network
);
530 INFO(network
->ctx
, "%s\n", s
);
536 LOC_EXPORT
int loc_network_tree_dump(struct loc_network_tree
* tree
) {
537 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
539 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
542 LOC_EXPORT
int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
543 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
545 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
,
546 &network
->start_address
, network
->prefix
);
548 ERROR(tree
->ctx
, "Could not find a node\n");
552 // Check if node has not been set before
554 DEBUG(tree
->ctx
, "There is already a network at this path\n");
558 // Point node to the network
559 node
->network
= loc_network_ref(network
);
564 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
565 size_t* counter
= (size_t*)data
;
567 // Increase the counter for each network
573 LOC_EXPORT
size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
576 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);
583 static size_t __loc_network_tree_count_nodes(struct loc_network_tree_node
* node
) {
587 counter
+= __loc_network_tree_count_nodes(node
->zero
);
590 counter
+= __loc_network_tree_count_nodes(node
->one
);
595 LOC_EXPORT
size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
) {
596 return __loc_network_tree_count_nodes(tree
->root
);
599 LOC_EXPORT
int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
) {
600 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
604 n
->ctx
= loc_ref(ctx
);
607 n
->zero
= n
->one
= NULL
;
609 DEBUG(n
->ctx
, "Network node allocated at %p\n", n
);
614 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
) {
621 static void loc_network_tree_node_free(struct loc_network_tree_node
* node
) {
622 DEBUG(node
->ctx
, "Releasing network node at %p\n", node
);
625 loc_network_unref(node
->network
);
628 loc_network_tree_node_unref(node
->zero
);
631 loc_network_tree_node_unref(node
->one
);
633 loc_unref(node
->ctx
);
637 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
) {
641 if (--node
->refcount
> 0)
644 loc_network_tree_node_free(node
);
648 LOC_EXPORT
struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
) {
657 return loc_network_tree_node_ref(node
);
660 LOC_EXPORT
int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
) {
661 return (!!node
->network
);
664 LOC_EXPORT
struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
) {
665 return loc_network_ref(node
->network
);