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
);
40 struct loc_network_tree
* tree
;
41 err
= loc_network_tree_new(ctx
, &tree
);
43 fprintf(stderr
, "Could not create the network tree\n");
48 struct loc_network
* network1
;
49 err
= loc_network_new_from_string(ctx
, &network1
, "2001:db8::1/32");
51 fprintf(stderr
, "Could not create the network\n");
55 err
= loc_network_set_country_code(network1
, "XX");
57 fprintf(stderr
, "Could not set country code\n");
61 // Adding network to the tree
62 err
= loc_network_tree_add_network(tree
, network1
);
64 fprintf(stderr
, "Could not add network to the tree\n");
68 // Check if the first and last addresses are correct
69 char* string
= loc_network_format_first_address(network1
);
71 fprintf(stderr
, "Did get NULL instead of a string for the first address\n");
75 if (strcmp(string
, "2001:db8::") != 0) {
76 fprintf(stderr
, "Got an incorrect first address: %s\n", string
);
80 string
= loc_network_format_last_address(network1
);
82 fprintf(stderr
, "Did get NULL instead of a string for the last address\n");
86 if (strcmp(string
, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
87 fprintf(stderr
, "Got an incorrect last address: %s\n", string
);
91 struct loc_network
* network2
;
92 err
= loc_network_new_from_string(ctx
, &network2
, "2001:db8:ffff::/48");
94 fprintf(stderr
, "Could not create the network\n");
98 err
= loc_network_set_country_code(network2
, "XY");
100 fprintf(stderr
, "Could not set country code\n");
104 // Adding network to the tree
105 err
= loc_network_tree_add_network(tree
, network2
);
107 fprintf(stderr
, "Could not add network to the tree\n");
112 err
= loc_network_tree_dump(tree
);
114 fprintf(stderr
, "Error dumping tree: %d\n", err
);
118 size_t nodes
= loc_network_tree_count_nodes(tree
);
119 printf("The tree has %zu nodes\n", nodes
);
121 // Check equals function
122 err
= loc_network_eq(network1
, network1
);
124 fprintf(stderr
, "Network is not equal with itself\n");
128 err
= loc_network_eq(network1
, network2
);
130 fprintf(stderr
, "Networks equal unexpectedly\n");
134 // Check subnet function
135 err
= loc_network_is_subnet_of(network1
, network2
);
137 fprintf(stderr
, "Subnet check 1 failed: %d\n", err
);
141 err
= loc_network_is_subnet_of(network2
, network1
);
143 fprintf(stderr
, "Subnet check 2 failed: %d\n", err
);
147 // Make list of subnets
148 struct loc_network_list
* subnets
= loc_network_subnets(network1
);
150 fprintf(stderr
, "Could not find subnets of network\n");
154 loc_network_list_dump(subnets
);
156 while (!loc_network_list_empty(subnets
)) {
157 struct loc_network
* subnet
= loc_network_list_pop(subnets
);
159 fprintf(stderr
, "Received an empty subnet\n");
163 char* s
= loc_network_str(subnet
);
164 printf("Received subnet %s\n", s
);
167 if (!loc_network_is_subnet_of(subnet
, network1
)) {
168 fprintf(stderr
, "Not a subnet\n");
172 loc_network_unref(subnet
);
175 loc_network_list_unref(subnets
);
177 struct loc_network_list
* excluded
= loc_network_exclude(network1
, network2
);
179 fprintf(stderr
, "Could not create excluded list\n");
183 loc_network_list_dump(excluded
);
186 loc_network_list_reverse(excluded
);
187 loc_network_list_dump(excluded
);
189 // Sort them and dump them again
190 loc_network_list_sort(excluded
);
191 loc_network_list_dump(excluded
);
193 loc_network_list_unref(excluded
);
196 struct loc_writer
* writer
;
197 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
201 struct loc_network
* network3
;
202 err
= loc_writer_add_network(writer
, &network3
, "2001:db8::/64");
204 fprintf(stderr
, "Could not add network\n");
209 loc_network_set_country_code(network3
, "XX");
211 struct loc_network
* network4
;
212 err
= loc_writer_add_network(writer
, &network4
, "2001:db8:ffff::/64");
214 fprintf(stderr
, "Could not add network\n");
219 loc_network_set_country_code(network4
, "XY");
222 loc_network_set_asn(network4
, 1024);
224 // Try adding an invalid network
225 struct loc_network
* network
;
226 err
= loc_writer_add_network(writer
, &network
, "xxxx:xxxx::/32");
227 if (err
!= -EINVAL
) {
228 fprintf(stderr
, "It was possible to add an invalid network (err = %d)\n", err
);
232 // Try adding a single address
233 err
= loc_writer_add_network(writer
, &network
, "2001:db8::");
235 fprintf(stderr
, "It was impossible to add an single IP address (err = %d)\n", err
);
239 // Try adding localhost
240 err
= loc_writer_add_network(writer
, &network
, "::1/128");
241 if (err
!= -EINVAL
) {
242 fprintf(stderr
, "It was possible to add localhost (::1/128): %d\n", err
);
248 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
252 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
254 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
257 loc_writer_unref(writer
);
259 loc_network_unref(network1
);
260 loc_network_unref(network2
);
261 loc_network_unref(network3
);
262 loc_network_unref(network4
);
263 loc_network_tree_unref(tree
);
265 // And open it again from disk
266 struct loc_database
* db
;
267 err
= loc_database_new(ctx
, &db
, f
);
269 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
273 // Lookup an address in the subnet
274 err
= loc_database_lookup_from_string(db
, "2001:db8::", &network1
);
276 fprintf(stderr
, "Could not look up 2001:db8::\n");
279 loc_network_unref(network1
);
281 // Lookup an address outside the subnet
282 err
= loc_database_lookup_from_string(db
, "2001:db8:fffe:1::", &network1
);
284 fprintf(stderr
, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
287 loc_network_unref(network1
);