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 program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
17 #include <arpa/inet.h>
25 #include <libloc/libloc.h>
26 #include <libloc/address.h>
27 #include <libloc/database.h>
28 #include <libloc/network.h>
29 #include <libloc/private.h>
30 #include <libloc/writer.h>
32 int main(int argc
, char** argv
) {
40 // Enable debug logging
41 loc_set_log_priority(ctx
, LOG_DEBUG
);
44 struct loc_network_tree
* tree
;
45 err
= loc_network_tree_new(ctx
, &tree
);
47 fprintf(stderr
, "Could not create the network tree\n");
52 struct in6_addr address
;
53 err
= inet_pton(AF_INET6
, "2001:db8::1", &address
);
55 fprintf(stderr
, "Could not parse IP address\n");
60 struct loc_network
* network1
;
61 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::1/32");
63 fprintf(stderr
, "Could not create the network\n");
67 err
= loc_network_set_country_code(network1
, "DE");
69 fprintf(stderr
, "Could not set country code\n");
74 // Adding network to the tree
75 err
= loc_network_tree_add_network(tree
, network1
);
77 fprintf(stderr
, "Could not add network to the tree\n");
82 // Check if the first and last addresses are correct
83 const char* string
= loc_network_format_first_address(network1
);
85 fprintf(stderr
, "Did get NULL instead of a string for the first address\n");
89 if (strcmp(string
, "2001:db8::") != 0) {
90 fprintf(stderr
, "Got an incorrect first address: %s\n", string
);
94 string
= loc_network_format_last_address(network1
);
96 fprintf(stderr
, "Did get NULL instead of a string for the last address\n");
100 if (strcmp(string
, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
101 fprintf(stderr
, "Got an incorrect last address: %s\n", string
);
105 err
= loc_network_matches_address(network1
, &address
);
107 fprintf(stderr
, "Network1 does not match address\n");
111 struct loc_network
* network2
;
112 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
114 fprintf(stderr
, "Could not create the network\n");
118 err
= loc_network_set_country_code(network2
, "DE");
120 fprintf(stderr
, "Could not set country code\n");
125 // Adding network to the tree
126 err
= loc_network_tree_add_network(tree
, network2
);
128 fprintf(stderr
, "Could not add network to the tree\n");
133 err
= loc_network_tree_dump(tree
);
135 fprintf(stderr
, "Error dumping tree: %d\n", err
);
139 size_t nodes
= loc_network_tree_count_nodes(tree
);
140 printf("The tree has %zu nodes\n", nodes
);
143 // Check equals function
144 err
= loc_network_cmp(network1
, network1
);
146 fprintf(stderr
, "Network is not equal with itself\n");
150 err
= loc_network_cmp(network1
, network2
);
152 fprintf(stderr
, "Networks equal unexpectedly\n");
156 // Check subnet function
157 err
= loc_network_is_subnet(network1
, network2
);
159 fprintf(stderr
, "Subnet check 1 failed: %d\n", err
);
163 err
= loc_network_is_subnet(network2
, network1
);
165 fprintf(stderr
, "Subnet check 2 failed: %d\n", err
);
170 struct loc_network
* subnet1
= NULL
;
171 struct loc_network
* subnet2
= NULL
;
173 err
= loc_network_subnets(network1
, &subnet1
, &subnet2
);
174 if (err
|| !subnet1
|| !subnet2
) {
175 fprintf(stderr
, "Could not find subnets of network: %d\n", err
);
179 const char* s
= loc_network_str(subnet1
);
180 printf("Received subnet1 = %s\n", s
);
182 s
= loc_network_str(subnet2
);
183 printf("Received subnet2 = %s\n", s
);
185 if (!loc_network_is_subnet(network1
, subnet1
)) {
186 fprintf(stderr
, "Subnet1 is not a subnet\n");
190 if (!loc_network_is_subnet(network1
, subnet2
)) {
191 fprintf(stderr
, "Subnet2 is not a subnet\n");
195 if (!loc_network_overlaps(network1
, subnet1
)) {
196 fprintf(stderr
, "Network1 does not seem to contain subnet1\n");
200 if (!loc_network_overlaps(network1
, subnet2
)) {
201 fprintf(stderr
, "Network1 does not seem to contain subnet2\n");
205 loc_network_unref(subnet1
);
206 loc_network_unref(subnet2
);
208 struct loc_network_list
* excluded
= loc_network_exclude(network1
, network2
);
210 fprintf(stderr
, "Could not create excluded list\n");
214 loc_network_list_dump(excluded
);
215 loc_network_list_unref(excluded
);
218 struct loc_writer
* writer
;
219 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
223 struct loc_network
* network3
;
224 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
226 fprintf(stderr
, "Could not add network\n");
231 loc_network_set_country_code(network3
, "XX");
233 struct loc_network
* network4
;
234 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
236 fprintf(stderr
, "Could not add network\n");
241 loc_network_set_country_code(network4
, "XY");
244 loc_network_set_asn(network4
, 1024);
246 // Try adding an invalid network
247 struct loc_network
* network
;
248 err
= loc_writer_add_network(writer
, &network
, "xxxx:xxxx::/32");
250 fprintf(stderr
, "It was possible to add an invalid network (err = %d)\n", err
);
254 // Try adding a single address
255 err
= loc_writer_add_network(writer
, &network
, "2001:db8::");
257 fprintf(stderr
, "It was impossible to add an single IP address (err = %d)\n", err
);
263 fprintf(stderr
, "Could not open file for writing: %m\n");
267 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
269 fprintf(stderr
, "Could not write database: %m\n");
272 loc_writer_unref(writer
);
274 loc_network_unref(network1
);
275 loc_network_unref(network2
);
276 loc_network_unref(network3
);
277 loc_network_unref(network4
);
280 loc_network_tree_unref(tree
);
283 // And open it again from disk
284 struct loc_database
* db
;
285 err
= loc_database_new(ctx
, &db
, f
);
287 fprintf(stderr
, "Could not open database: %m\n");
291 // Lookup an address in the subnet
292 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
294 fprintf(stderr
, "Could not look up 2001:db8::\n");
297 loc_network_unref(network1
);
299 // Lookup an address outside the subnet
300 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
302 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
306 const struct bit_length_test
{
308 unsigned int bit_length
;
309 } bit_length_tests
[] = {
311 { "2001::/128", 126 },
312 { "1.0.0.0/32", 25 },
314 { "255.255.255.255/32", 32 },
318 for (const struct bit_length_test
* t
= bit_length_tests
; t
->network
; t
++) {
319 err
= loc_network_new_from_string(ctx
, &network1
, t
->network
);
321 fprintf(stderr
, "Could not create network %s: %m\n", t
->network
);
325 const struct in6_addr
* addr
= loc_network_get_first_address(network1
);
327 unsigned int bit_length
= loc_address_bit_length(addr
);
329 if (bit_length
!= t
->bit_length
) {
330 printf("Bit length of %s didn't match: %u != %u\n",
331 t
->network
, t
->bit_length
, bit_length
);
332 loc_network_unref(network1
);
336 loc_network_unref(network1
);