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/database.h>
27 #include <libloc/network.h>
28 #include <libloc/private.h>
29 #include <libloc/writer.h>
31 int main(int argc
, char** argv
) {
39 // Enable debug logging
40 loc_set_log_priority(ctx
, LOG_DEBUG
);
43 struct loc_network_tree
* tree
;
44 err
= loc_network_tree_new(ctx
, &tree
);
46 fprintf(stderr
, "Could not create the network tree\n");
51 struct in6_addr address
;
52 err
= inet_pton(AF_INET6
, "2001:db8::1", &address
);
54 fprintf(stderr
, "Could not parse IP address\n");
59 struct loc_network
* network1
;
60 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::1/32");
62 fprintf(stderr
, "Could not create the network\n");
66 err
= loc_network_set_country_code(network1
, "DE");
68 fprintf(stderr
, "Could not set country code\n");
73 // Adding network to the tree
74 err
= loc_network_tree_add_network(tree
, network1
);
76 fprintf(stderr
, "Could not add network to the tree\n");
81 // Check if the first and last addresses are correct
82 char* string
= loc_network_format_first_address(network1
);
84 fprintf(stderr
, "Did get NULL instead of a string for the first address\n");
88 if (strcmp(string
, "2001:db8::") != 0) {
89 fprintf(stderr
, "Got an incorrect first address: %s\n", string
);
93 string
= loc_network_format_last_address(network1
);
95 fprintf(stderr
, "Did get NULL instead of a string for the last address\n");
99 if (strcmp(string
, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
100 fprintf(stderr
, "Got an incorrect last address: %s\n", string
);
104 err
= loc_network_matches_address(network1
, &address
);
106 fprintf(stderr
, "Network1 does not match address\n");
110 struct loc_network
* network2
;
111 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
113 fprintf(stderr
, "Could not create the network\n");
117 err
= loc_network_set_country_code(network2
, "DE");
119 fprintf(stderr
, "Could not set country code\n");
124 // Adding network to the tree
125 err
= loc_network_tree_add_network(tree
, network2
);
127 fprintf(stderr
, "Could not add network to the tree\n");
132 err
= loc_network_tree_dump(tree
);
134 fprintf(stderr
, "Error dumping tree: %d\n", err
);
138 size_t nodes
= loc_network_tree_count_nodes(tree
);
139 printf("The tree has %zu nodes\n", nodes
);
142 // Check equals function
143 err
= loc_network_cmp(network1
, network1
);
145 fprintf(stderr
, "Network is not equal with itself\n");
149 err
= loc_network_cmp(network1
, network2
);
151 fprintf(stderr
, "Networks equal unexpectedly\n");
155 // Check subnet function
156 err
= loc_network_is_subnet(network1
, network2
);
158 fprintf(stderr
, "Subnet check 1 failed: %d\n", err
);
162 err
= loc_network_is_subnet(network2
, network1
);
164 fprintf(stderr
, "Subnet check 2 failed: %d\n", err
);
169 struct loc_network
* subnet1
= NULL
;
170 struct loc_network
* subnet2
= NULL
;
172 err
= loc_network_subnets(network1
, &subnet1
, &subnet2
);
173 if (err
|| !subnet1
|| !subnet2
) {
174 fprintf(stderr
, "Could not find subnets of network: %d\n", err
);
178 char* s
= loc_network_str(subnet1
);
179 printf("Received subnet1 = %s\n", s
);
182 s
= loc_network_str(subnet2
);
183 printf("Received subnet2 = %s\n", s
);
186 if (!loc_network_is_subnet(network1
, subnet1
)) {
187 fprintf(stderr
, "Subnet1 is not a subnet\n");
191 if (!loc_network_is_subnet(network1
, subnet2
)) {
192 fprintf(stderr
, "Subnet2 is not a subnet\n");
196 if (!loc_network_overlaps(network1
, subnet1
)) {
197 fprintf(stderr
, "Network1 does not seem to contain subnet1\n");
201 if (!loc_network_overlaps(network1
, subnet2
)) {
202 fprintf(stderr
, "Network1 does not seem to contain subnet2\n");
206 loc_network_unref(subnet1
);
207 loc_network_unref(subnet2
);
209 struct loc_network_list
* excluded
= loc_network_exclude(network1
, network2
);
211 fprintf(stderr
, "Could not create excluded list\n");
215 loc_network_list_dump(excluded
);
216 loc_network_list_unref(excluded
);
219 struct loc_writer
* writer
;
220 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
224 struct loc_network
* network3
;
225 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
227 fprintf(stderr
, "Could not add network\n");
232 loc_network_set_country_code(network3
, "XX");
234 struct loc_network
* network4
;
235 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
237 fprintf(stderr
, "Could not add network\n");
242 loc_network_set_country_code(network4
, "XY");
245 loc_network_set_asn(network4
, 1024);
247 // Try adding an invalid network
248 struct loc_network
* network
;
249 err
= loc_writer_add_network(writer
, &network
, "xxxx:xxxx::/32");
250 if (err
!= -EINVAL
) {
251 fprintf(stderr
, "It was possible to add an invalid network (err = %d)\n", err
);
255 // Try adding a single address
256 err
= loc_writer_add_network(writer
, &network
, "2001:db8::");
258 fprintf(stderr
, "It was impossible to add an single IP address (err = %d)\n", err
);
264 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
268 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
270 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
273 loc_writer_unref(writer
);
275 loc_network_unref(network1
);
276 loc_network_unref(network2
);
277 loc_network_unref(network3
);
278 loc_network_unref(network4
);
281 loc_network_tree_unref(tree
);
284 // And open it again from disk
285 struct loc_database
* db
;
286 err
= loc_database_new(ctx
, &db
, f
);
288 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
292 // Lookup an address in the subnet
293 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
295 fprintf(stderr
, "Could not look up 2001:db8::\n");
298 loc_network_unref(network1
);
300 // Lookup an address outside the subnet
301 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
303 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
306 loc_network_unref(network1
);
308 const struct bit_length_test
{
310 unsigned int bit_length
;
311 } bit_length_tests
[] = {
313 { "2001::/128", 126 },
317 for (const struct bit_length_test
* t
= bit_length_tests
; t
->network
; t
++) {
318 err
= loc_network_new_from_string(ctx
, &network1
, t
->network
);
320 fprintf(stderr
, "Could not create network %s: %m\n", t
->network
);
324 const struct in6_addr
* addr
= loc_network_get_first_address(network1
);
326 unsigned int bit_length
= loc_address_bit_length(addr
);
328 if (bit_length
!= t
->bit_length
) {
329 printf("Bit length of %s didn't match: %u != %u\n",
330 t
->network
, t
->bit_length
, bit_length
);
331 loc_network_unref(network1
);
335 loc_network_unref(network1
);