]>
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>
24 #include <loc/libloc.h>
25 #include <loc/network.h>
27 #include "libloc-private.h"
34 struct in6_addr start_address
;
42 LOC_EXPORT
int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
43 struct in6_addr start_address
, unsigned int prefix
) {
44 // Address cannot be unspecified
45 if (IN6_IS_ADDR_UNSPECIFIED(&start_address
)) {
46 DEBUG(ctx
, "Start address is unspecified\n");
50 // Address cannot be loopback
51 if (IN6_IS_ADDR_LOOPBACK(&start_address
)) {
52 DEBUG(ctx
, "Start address is loopback address\n");
56 // Address cannot be link-local
57 if (IN6_IS_ADDR_LINKLOCAL(&start_address
)) {
58 DEBUG(ctx
, "Start address cannot be link-local\n");
62 // Address cannot be site-local
63 if (IN6_IS_ADDR_SITELOCAL(&start_address
)) {
64 DEBUG(ctx
, "Start address cannot be site-local\n");
68 struct loc_network
* n
= calloc(1, sizeof(*n
));
72 n
->ctx
= loc_ref(ctx
);
75 n
->start_address
= start_address
;
78 DEBUG(n
->ctx
, "Network allocated at %p\n", n
);
83 LOC_EXPORT
int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
84 const char* address_string
) {
85 struct in6_addr start_address
;
88 // Make a copy of the string to work on it
89 char* buffer
= strdup(address_string
);
90 address_string
= prefix_string
= buffer
;
92 // Split address and prefix
93 address_string
= strsep(&prefix_string
, "/");
95 // Convert prefix to integer
96 unsigned int prefix
= strtol(prefix_string
, NULL
, 10);
99 int r
= inet_pton(AF_INET6
, address_string
, &start_address
);
101 // Free temporary buffer
105 r
= loc_network_new(ctx
, network
, start_address
, prefix
);
111 LOC_EXPORT
struct loc_network
* loc_network_ref(struct loc_network
* network
) {
117 static void loc_network_free(struct loc_network
* network
) {
118 DEBUG(network
->ctx
, "Releasing network at %p\n", network
);
121 loc_as_unref(network
->as
);
123 loc_unref(network
->ctx
);
127 LOC_EXPORT
struct loc_network
* loc_network_unref(struct loc_network
* network
) {
128 if (--network
->refcount
> 0)
131 loc_network_free(network
);
135 LOC_EXPORT
char* loc_network_str(struct loc_network
* network
) {
136 const size_t l
= INET6_ADDRSTRLEN
+ 3;
138 char* string
= malloc(l
);
142 const char* ret
= inet_ntop(AF_INET6
, &network
->start_address
, string
, l
);
144 ERROR(network
->ctx
, "Could not convert network to string: %s\n", strerror(errno
));
151 sprintf(string
+ strlen(string
), "/%u", network
->prefix
);
156 LOC_EXPORT
const char* loc_network_get_country_code(struct loc_network
* network
) {
157 return network
->country_code
;
160 LOC_EXPORT
int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
) {
161 // Country codes must be two characters
162 if (strlen(country_code
) != 2)
165 for (unsigned int i
= 0; i
< 3; i
++) {
166 network
->country_code
[i
] = country_code
[i
];
172 LOC_EXPORT
int loc_network_to_database_v0(struct loc_network
* network
, struct loc_database_network_v0
* dbobj
) {
173 dbobj
->prefix
= htobe16(network
->prefix
);
176 for (unsigned int i
= 0; i
< 2; i
++) {
177 dbobj
->country_code
[i
] = network
->country_code
? network
->country_code
[i
] : '\0';
183 asn
= loc_as_get_number(network
->as
);
185 dbobj
->asn
= htobe32(asn
);
190 struct loc_network_tree
{
194 struct loc_network_tree_node
* root
;
197 struct loc_network_tree_node
{
198 struct loc_network_tree_node
* zero
;
199 struct loc_network_tree_node
* one
;
201 struct loc_network
* network
;
204 LOC_EXPORT
int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
) {
205 struct loc_network_tree
* t
= calloc(1, sizeof(*t
));
209 t
->ctx
= loc_ref(ctx
);
212 // Create the root node
213 t
->root
= calloc(1, sizeof(*t
->root
));
215 DEBUG(t
->ctx
, "Network tree allocated at %p\n", t
);
220 static int loc_network_tree_node_new(struct loc_network_tree_node
** node
) {
221 struct loc_network_tree_node
* n
= calloc(1, sizeof(*n
));
225 n
->zero
= n
->one
= NULL
;
231 static struct loc_network_tree_node
* loc_network_tree_get_node(struct loc_network_tree_node
* node
, int path
) {
232 struct loc_network_tree_node
** n
;
239 // If the desired node doesn't exist, yet, we will create it
241 int r
= loc_network_tree_node_new(n
);
249 static struct loc_network_tree_node
* loc_network_tree_get_path(struct loc_network_tree
* tree
, const struct in6_addr
* address
) {
250 struct loc_network_tree_node
* node
= tree
->root
;
252 for (unsigned int i
= 127; i
> 0; i
--) {
253 // Check if the ith bit is one or zero
254 node
= loc_network_tree_get_node(node
, ((address
->s6_addr32
[i
/ 32] & (1 << (i
% 32))) == 0));
260 static int __loc_network_tree_walk(struct loc_ctx
* ctx
, struct loc_network_tree_node
* node
,
261 int(*filter_callback
)(struct loc_network
* network
, void* data
),
262 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
265 // Finding a network ends the walk here
267 if (filter_callback
) {
268 int f
= filter_callback(node
->network
, data
);
272 // Skip network if filter function returns value greater than zero
277 r
= callback(node
->network
, data
);
282 // Walk down on the left side of the tree first
284 r
= __loc_network_tree_walk(ctx
, node
->zero
, filter_callback
, callback
, data
);
289 // Then walk on the other side
291 r
= __loc_network_tree_walk(ctx
, node
->one
, filter_callback
, callback
, data
);
299 LOC_EXPORT
int loc_network_tree_walk(struct loc_network_tree
* tree
,
300 int(*filter_callback
)(struct loc_network
* network
, void* data
),
301 int(*callback
)(struct loc_network
* network
, void* data
), void* data
) {
302 return __loc_network_tree_walk(tree
->ctx
, tree
->root
, filter_callback
, callback
, data
);
305 static void loc_network_tree_free_subtree(struct loc_network_tree_node
* node
) {
307 loc_network_unref(node
->network
);
310 loc_network_tree_free_subtree(node
->zero
);
313 loc_network_tree_free_subtree(node
->one
);
318 static void loc_network_tree_free(struct loc_network_tree
* tree
) {
319 DEBUG(tree
->ctx
, "Releasing network tree at %p\n", tree
);
321 loc_network_tree_free_subtree(tree
->root
);
323 loc_unref(tree
->ctx
);
327 LOC_EXPORT
struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
) {
328 if (--tree
->refcount
> 0)
331 loc_network_tree_free(tree
);
335 int __loc_network_tree_dump(struct loc_network
* network
, void* data
) {
336 DEBUG(network
->ctx
, "Dumping network at %p\n", network
);
338 char* s
= loc_network_str(network
);
342 INFO(network
->ctx
, "%s\n", s
);
348 LOC_EXPORT
int loc_network_tree_dump(struct loc_network_tree
* tree
) {
349 DEBUG(tree
->ctx
, "Dumping network tree at %p\n", tree
);
351 return loc_network_tree_walk(tree
, NULL
, __loc_network_tree_dump
, NULL
);
354 LOC_EXPORT
int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
) {
355 DEBUG(tree
->ctx
, "Adding network %p to tree %p\n", network
, tree
);
357 struct loc_network_tree_node
* node
= loc_network_tree_get_path(tree
, &network
->start_address
);
359 ERROR(tree
->ctx
, "Could not find a node\n");
363 // Check if node has not been set before
365 DEBUG(tree
->ctx
, "There is already a network at this path\n");
369 // Point node to the network
370 node
->network
= loc_network_ref(network
);
375 static int __loc_network_tree_count(struct loc_network
* network
, void* data
) {
376 size_t* counter
= (size_t*)data
;
378 // Increase the counter for each network
384 LOC_EXPORT
size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
) {
387 int r
= loc_network_tree_walk(tree
, NULL
, __loc_network_tree_count
, &counter
);