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.
24 #include <loc/libloc.h>
25 #include <loc/database.h>
26 #include <loc/network.h>
27 #include <loc/writer.h>
29 int main(int argc
, char** argv
) {
37 // Enable debug logging
38 loc_set_log_priority(ctx
, LOG_DEBUG
);
41 struct loc_network_tree
* tree
;
42 err
= loc_network_tree_new(ctx
, &tree
);
44 fprintf(stderr
, "Could not create the network tree\n");
50 struct loc_network
* network1
;
51 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::1/32");
53 fprintf(stderr
, "Could not create the network\n");
57 err
= loc_network_set_country_code(network1
, "XX");
59 fprintf(stderr
, "Could not set country code\n");
64 // Adding network to the tree
65 err
= loc_network_tree_add_network(tree
, network1
);
67 fprintf(stderr
, "Could not add network to the tree\n");
72 // Check if the first and last addresses are correct
73 char* string
= loc_network_format_first_address(network1
);
75 fprintf(stderr
, "Did get NULL instead of a string for the first address\n");
79 if (strcmp(string
, "2001:db8::") != 0) {
80 fprintf(stderr
, "Got an incorrect first address: %s\n", string
);
84 string
= loc_network_format_last_address(network1
);
86 fprintf(stderr
, "Did get NULL instead of a string for the last address\n");
90 if (strcmp(string
, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
91 fprintf(stderr
, "Got an incorrect last address: %s\n", string
);
95 struct loc_network
* network2
;
96 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
98 fprintf(stderr
, "Could not create the network\n");
102 err
= loc_network_set_country_code(network2
, "XY");
104 fprintf(stderr
, "Could not set country code\n");
109 // Adding network to the tree
110 err
= loc_network_tree_add_network(tree
, network2
);
112 fprintf(stderr
, "Could not add network to the tree\n");
117 err
= loc_network_tree_dump(tree
);
119 fprintf(stderr
, "Error dumping tree: %d\n", err
);
123 size_t nodes
= loc_network_tree_count_nodes(tree
);
124 printf("The tree has %zu nodes\n", nodes
);
127 // Check equals function
128 err
= loc_network_eq(network1
, network1
);
130 fprintf(stderr
, "Network is not equal with itself\n");
134 err
= loc_network_eq(network1
, network2
);
136 fprintf(stderr
, "Networks equal unexpectedly\n");
140 // Check subnet function
141 err
= loc_network_is_subnet(network1
, network2
);
143 fprintf(stderr
, "Subnet check 1 failed: %d\n", err
);
147 err
= loc_network_is_subnet(network2
, network1
);
149 fprintf(stderr
, "Subnet check 2 failed: %d\n", err
);
154 struct loc_network
* subnet1
= NULL
;
155 struct loc_network
* subnet2
= NULL
;
157 err
= loc_network_subnets(network1
, &subnet1
, &subnet2
);
158 if (err
|| !subnet1
|| !subnet2
) {
159 fprintf(stderr
, "Could not find subnets of network: %d\n", err
);
163 char* s
= loc_network_str(subnet1
);
164 printf("Received subnet1 = %s\n", s
);
167 s
= loc_network_str(subnet2
);
168 printf("Received subnet2 = %s\n", s
);
171 if (!loc_network_is_subnet(network1
, subnet1
)) {
172 fprintf(stderr
, "Subnet1 is not a subnet\n");
176 if (!loc_network_is_subnet(network1
, subnet2
)) {
177 fprintf(stderr
, "Subnet2 is not a subnet\n");
181 loc_network_unref(subnet1
);
182 loc_network_unref(subnet2
);
184 struct loc_network_list
* excluded
= loc_network_exclude(network1
, network2
);
186 fprintf(stderr
, "Could not create excluded list\n");
190 loc_network_list_dump(excluded
);
193 loc_network_list_reverse(excluded
);
194 loc_network_list_dump(excluded
);
196 // Sort them and dump them again
197 loc_network_list_sort(excluded
);
198 loc_network_list_dump(excluded
);
200 loc_network_list_unref(excluded
);
203 struct loc_writer
* writer
;
204 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
208 struct loc_network
* network3
;
209 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
211 fprintf(stderr
, "Could not add network\n");
216 loc_network_set_country_code(network3
, "XX");
218 struct loc_network
* network4
;
219 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
221 fprintf(stderr
, "Could not add network\n");
226 loc_network_set_country_code(network4
, "XY");
229 loc_network_set_asn(network4
, 1024);
231 // Try adding an invalid network
232 struct loc_network
* network
;
233 err
= loc_writer_add_network(writer
, &network
, "xxxx:xxxx::/32");
234 if (err
!= -EINVAL
) {
235 fprintf(stderr
, "It was possible to add an invalid network (err = %d)\n", err
);
239 // Try adding a single address
240 err
= loc_writer_add_network(writer
, &network
, "2001:db8::");
242 fprintf(stderr
, "It was impossible to add an single IP address (err = %d)\n", err
);
246 // Try adding localhost
247 err
= loc_writer_add_network(writer
, &network
, "::1/128");
248 if (err
!= -EINVAL
) {
249 fprintf(stderr
, "It was possible to add localhost (::1/128): %d\n", err
);
255 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
259 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
261 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
264 loc_writer_unref(writer
);
266 loc_network_unref(network1
);
267 loc_network_unref(network2
);
268 loc_network_unref(network3
);
269 loc_network_unref(network4
);
272 loc_network_tree_unref(tree
);
275 // And open it again from disk
276 struct loc_database
* db
;
277 err
= loc_database_new(ctx
, &db
, f
);
279 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
283 // Lookup an address in the subnet
284 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
286 fprintf(stderr
, "Could not look up 2001:db8::\n");
289 loc_network_unref(network1
);
291 // Lookup an address outside the subnet
292 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
294 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
297 loc_network_unref(network1
);