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/writer.h>
30 int main(int argc
, char** argv
) {
38 // Enable debug logging
39 loc_set_log_priority(ctx
, LOG_DEBUG
);
42 struct loc_network_tree
* tree
;
43 err
= loc_network_tree_new(ctx
, &tree
);
45 fprintf(stderr
, "Could not create the network tree\n");
50 struct in6_addr address
;
51 err
= inet_pton(AF_INET6
, "2001:db8::1", &address
);
53 fprintf(stderr
, "Could not parse IP address\n");
58 struct loc_network
* network1
;
59 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::1/32");
61 fprintf(stderr
, "Could not create the network\n");
65 err
= loc_network_set_country_code(network1
, "DE");
67 fprintf(stderr
, "Could not set country code\n");
72 // Adding network to the tree
73 err
= loc_network_tree_add_network(tree
, network1
);
75 fprintf(stderr
, "Could not add network to the tree\n");
80 // Check if the first and last addresses are correct
81 char* string
= loc_network_format_first_address(network1
);
83 fprintf(stderr
, "Did get NULL instead of a string for the first address\n");
87 if (strcmp(string
, "2001:db8::") != 0) {
88 fprintf(stderr
, "Got an incorrect first address: %s\n", string
);
92 string
= loc_network_format_last_address(network1
);
94 fprintf(stderr
, "Did get NULL instead of a string for the last address\n");
98 if (strcmp(string
, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
99 fprintf(stderr
, "Got an incorrect last address: %s\n", string
);
103 err
= loc_network_matches_address(network1
, &address
);
105 fprintf(stderr
, "Network1 does not match address\n");
109 struct loc_network
* network2
;
110 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
112 fprintf(stderr
, "Could not create the network\n");
116 err
= loc_network_set_country_code(network2
, "DE");
118 fprintf(stderr
, "Could not set country code\n");
123 // Adding network to the tree
124 err
= loc_network_tree_add_network(tree
, network2
);
126 fprintf(stderr
, "Could not add network to the tree\n");
131 err
= loc_network_tree_dump(tree
);
133 fprintf(stderr
, "Error dumping tree: %d\n", err
);
137 size_t nodes
= loc_network_tree_count_nodes(tree
);
138 printf("The tree has %zu nodes\n", nodes
);
141 // Check equals function
142 err
= loc_network_cmp(network1
, network1
);
144 fprintf(stderr
, "Network is not equal with itself\n");
148 err
= loc_network_cmp(network1
, network2
);
150 fprintf(stderr
, "Networks equal unexpectedly\n");
154 // Check subnet function
155 err
= loc_network_is_subnet(network1
, network2
);
157 fprintf(stderr
, "Subnet check 1 failed: %d\n", err
);
161 err
= loc_network_is_subnet(network2
, network1
);
163 fprintf(stderr
, "Subnet check 2 failed: %d\n", err
);
168 struct loc_network
* subnet1
= NULL
;
169 struct loc_network
* subnet2
= NULL
;
171 err
= loc_network_subnets(network1
, &subnet1
, &subnet2
);
172 if (err
|| !subnet1
|| !subnet2
) {
173 fprintf(stderr
, "Could not find subnets of network: %d\n", err
);
177 char* s
= loc_network_str(subnet1
);
178 printf("Received subnet1 = %s\n", s
);
181 s
= loc_network_str(subnet2
);
182 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");
249 if (err
!= -EINVAL
) {
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
);
261 // Try adding localhost
262 err
= loc_writer_add_network(writer
, &network
, "::1/128");
263 if (err
!= -EINVAL
) {
264 fprintf(stderr
, "It was possible to add localhost (::1/128): %d\n", err
);
270 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
274 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
276 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
279 loc_writer_unref(writer
);
281 loc_network_unref(network1
);
282 loc_network_unref(network2
);
283 loc_network_unref(network3
);
284 loc_network_unref(network4
);
287 loc_network_tree_unref(tree
);
290 // And open it again from disk
291 struct loc_database
* db
;
292 err
= loc_database_new(ctx
, &db
, f
);
294 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
298 // Lookup an address in the subnet
299 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
301 fprintf(stderr
, "Could not look up 2001:db8::\n");
304 loc_network_unref(network1
);
306 // Lookup an address outside the subnet
307 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
309 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
312 loc_network_unref(network1
);