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 static int test_reverse_pointers(struct loc_ctx
* ctx
) {
38 { "::1/128", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa." },
39 { "2001:db8::/32", "*.8.b.d.0.1.0.0.2.ip6.arpa." },
42 { "10.0.0.0/32", "0.0.0.10.in-addr.arpa." },
43 { "10.0.0.0/24", "*.0.0.10.in-addr.arpa." },
44 { "10.0.0.0/16", "*.0.10.in-addr.arpa." },
45 { "10.0.0.0/8", "*.10.in-addr.arpa." },
46 { "10.0.0.0/0", "*.in-addr.arpa." },
47 { "10.0.0.0/1", NULL
, },
51 struct loc_network
* network
= NULL
;
55 for (const struct test
* test
= tests
; test
->network
; test
++) {
56 // Create a new network
57 r
= loc_network_new_from_string(ctx
, &network
, test
->network
);
61 // Fetch the reverse pointer
62 rp
= loc_network_reverse_pointer(network
, NULL
);
64 // No RP expected and got none
68 // Got a result when expecting none
69 else if (!test
->rp
&& rp
) {
70 fprintf(stderr
, "Got an RP for %s when expecting none\n", test
->network
);
73 // Got nothing when expecting a result
74 } else if (test
->rp
&& !rp
) {
75 fprintf(stderr
, "Got no RP for %s when expecting one\n", test
->network
);
79 } else if (strcmp(test
->rp
, rp
) != 0) {
80 fprintf(stderr
, "Got an unexpected RP for %s: Got %s, expected %s\n",
81 test
->network
, rp
, test
->rp
);
85 loc_network_unref(network
);
93 int main(int argc
, char** argv
) {
101 // Enable debug logging
102 loc_set_log_priority(ctx
, LOG_DEBUG
);
105 struct loc_network_tree
* tree
;
106 err
= loc_network_tree_new(ctx
, &tree
);
108 fprintf(stderr
, "Could not create the network tree\n");
113 struct in6_addr address
;
114 err
= inet_pton(AF_INET6
, "2001:db8::1", &address
);
116 fprintf(stderr
, "Could not parse IP address\n");
121 struct loc_network
* network1
;
122 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::1/32");
124 fprintf(stderr
, "Could not create the network\n");
128 err
= loc_network_set_country_code(network1
, "DE");
130 fprintf(stderr
, "Could not set country code\n");
135 // Adding network to the tree
136 err
= loc_network_tree_add_network(tree
, network1
);
138 fprintf(stderr
, "Could not add network to the tree\n");
143 // Check if the first and last addresses are correct
144 const char* string
= loc_network_format_first_address(network1
);
146 fprintf(stderr
, "Did get NULL instead of a string for the first address\n");
150 if (strcmp(string
, "2001:db8::") != 0) {
151 fprintf(stderr
, "Got an incorrect first address: %s\n", string
);
155 string
= loc_network_format_last_address(network1
);
157 fprintf(stderr
, "Did get NULL instead of a string for the last address\n");
161 if (strcmp(string
, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
162 fprintf(stderr
, "Got an incorrect last address: %s\n", string
);
166 err
= loc_network_matches_address(network1
, &address
);
168 fprintf(stderr
, "Network1 does not match address\n");
172 struct loc_network
* network2
;
173 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
175 fprintf(stderr
, "Could not create the network\n");
179 err
= loc_network_set_country_code(network2
, "DE");
181 fprintf(stderr
, "Could not set country code\n");
186 // Adding network to the tree
187 err
= loc_network_tree_add_network(tree
, network2
);
189 fprintf(stderr
, "Could not add network to the tree\n");
194 err
= loc_network_tree_dump(tree
);
196 fprintf(stderr
, "Error dumping tree: %d\n", err
);
200 size_t nodes
= loc_network_tree_count_nodes(tree
);
201 printf("The tree has %zu nodes\n", nodes
);
204 // Check equals function
205 err
= loc_network_cmp(network1
, network1
);
207 fprintf(stderr
, "Network is not equal with itself\n");
211 err
= loc_network_cmp(network1
, network2
);
213 fprintf(stderr
, "Networks equal unexpectedly\n");
217 // Check subnet function
218 err
= loc_network_is_subnet(network1
, network2
);
220 fprintf(stderr
, "Subnet check 1 failed: %d\n", err
);
224 err
= loc_network_is_subnet(network2
, network1
);
226 fprintf(stderr
, "Subnet check 2 failed: %d\n", err
);
231 struct loc_network
* subnet1
= NULL
;
232 struct loc_network
* subnet2
= NULL
;
234 err
= loc_network_subnets(network1
, &subnet1
, &subnet2
);
235 if (err
|| !subnet1
|| !subnet2
) {
236 fprintf(stderr
, "Could not find subnets of network: %d\n", err
);
240 const char* s
= loc_network_str(subnet1
);
241 printf("Received subnet1 = %s\n", s
);
243 s
= loc_network_str(subnet2
);
244 printf("Received subnet2 = %s\n", s
);
246 if (!loc_network_is_subnet(network1
, subnet1
)) {
247 fprintf(stderr
, "Subnet1 is not a subnet\n");
251 if (!loc_network_is_subnet(network1
, subnet2
)) {
252 fprintf(stderr
, "Subnet2 is not a subnet\n");
256 if (!loc_network_overlaps(network1
, subnet1
)) {
257 fprintf(stderr
, "Network1 does not seem to contain subnet1\n");
261 if (!loc_network_overlaps(network1
, subnet2
)) {
262 fprintf(stderr
, "Network1 does not seem to contain subnet2\n");
266 loc_network_unref(subnet1
);
267 loc_network_unref(subnet2
);
269 struct loc_network_list
* excluded
= loc_network_exclude(network1
, network2
);
271 fprintf(stderr
, "Could not create excluded list\n");
275 loc_network_list_dump(excluded
);
276 loc_network_list_unref(excluded
);
279 struct loc_writer
* writer
;
280 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
284 struct loc_network
* network3
;
285 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
287 fprintf(stderr
, "Could not add network\n");
292 loc_network_set_country_code(network3
, "XX");
294 struct loc_network
* network4
;
295 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
297 fprintf(stderr
, "Could not add network\n");
302 loc_network_set_country_code(network4
, "XY");
305 loc_network_set_asn(network4
, 1024);
307 // Try adding an invalid network
308 struct loc_network
* network
;
309 err
= loc_writer_add_network(writer
, &network
, "xxxx:xxxx::/32");
311 fprintf(stderr
, "It was possible to add an invalid network (err = %d)\n", err
);
315 // Try adding a single address
316 err
= loc_writer_add_network(writer
, &network
, "2001:db8::");
318 fprintf(stderr
, "It was impossible to add an single IP address (err = %d)\n", err
);
324 fprintf(stderr
, "Could not open file for writing: %m\n");
328 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
330 fprintf(stderr
, "Could not write database: %m\n");
333 loc_writer_unref(writer
);
335 loc_network_unref(network1
);
336 loc_network_unref(network2
);
337 loc_network_unref(network3
);
338 loc_network_unref(network4
);
341 loc_network_tree_unref(tree
);
344 // And open it again from disk
345 struct loc_database
* db
;
346 err
= loc_database_new(ctx
, &db
, f
);
348 fprintf(stderr
, "Could not open database: %m\n");
352 // Lookup an address in the subnet
353 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
355 fprintf(stderr
, "Could not look up 2001:db8::\n");
358 loc_network_unref(network1
);
360 // Lookup an address outside the subnet
361 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
363 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
367 const struct bit_length_test
{
369 unsigned int bit_length
;
370 } bit_length_tests
[] = {
372 { "2001::/128", 16 },
374 { "0.0.0.1/32", 32 },
375 { "255.255.255.255/32", 32 },
379 for (const struct bit_length_test
* t
= bit_length_tests
; t
->network
; t
++) {
380 err
= loc_network_new_from_string(ctx
, &network1
, t
->network
);
382 fprintf(stderr
, "Could not create network %s: %m\n", t
->network
);
386 const struct in6_addr
* addr
= loc_network_get_first_address(network1
);
388 unsigned int bit_length
= loc_address_bit_length(addr
);
390 if (bit_length
!= t
->bit_length
) {
391 printf("Bit length of %s didn't match: %u != %u\n",
392 t
->network
, t
->bit_length
, bit_length
);
393 loc_network_unref(network1
);
397 loc_network_unref(network1
);
400 // Test reverse pointers
401 err
= test_reverse_pointers(ctx
);