]>
Commit | Line | Data |
---|---|---|
3b5f4af2 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 IPFire Development Team <info@ipfire.org> | |
5 | ||
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. | |
10 | ||
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. | |
15 | */ | |
16 | ||
fc692a58 | 17 | #include <arpa/inet.h> |
f3e02bc5 | 18 | #include <errno.h> |
3b5f4af2 MT |
19 | #include <stdio.h> |
20 | #include <stddef.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
37317e4f | 23 | #include <syslog.h> |
3b5f4af2 | 24 | |
c12a1385 MT |
25 | #include <libloc/libloc.h> |
26 | #include <libloc/database.h> | |
27 | #include <libloc/network.h> | |
f69e0c8b | 28 | #include <libloc/private.h> |
c12a1385 | 29 | #include <libloc/writer.h> |
3b5f4af2 MT |
30 | |
31 | int main(int argc, char** argv) { | |
32 | int err; | |
33 | ||
34 | struct loc_ctx* ctx; | |
35 | err = loc_new(&ctx); | |
36 | if (err < 0) | |
37 | exit(EXIT_FAILURE); | |
38 | ||
37317e4f MT |
39 | // Enable debug logging |
40 | loc_set_log_priority(ctx, LOG_DEBUG); | |
41 | ||
7933f5bf | 42 | #if 0 |
3b5f4af2 MT |
43 | struct loc_network_tree* tree; |
44 | err = loc_network_tree_new(ctx, &tree); | |
45 | if (err) { | |
46 | fprintf(stderr, "Could not create the network tree\n"); | |
47 | exit(EXIT_FAILURE); | |
48 | } | |
7933f5bf | 49 | #endif |
3b5f4af2 | 50 | |
fc692a58 MT |
51 | struct in6_addr address; |
52 | err = inet_pton(AF_INET6, "2001:db8::1", &address); | |
53 | if (err != 1) { | |
54 | fprintf(stderr, "Could not parse IP address\n"); | |
55 | exit(EXIT_FAILURE); | |
56 | } | |
57 | ||
3b5f4af2 MT |
58 | // Create a network |
59 | struct loc_network* network1; | |
2b9338ea | 60 | err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32"); |
3b5f4af2 MT |
61 | if (err) { |
62 | fprintf(stderr, "Could not create the network\n"); | |
63 | exit(EXIT_FAILURE); | |
64 | } | |
65 | ||
e8ebd079 | 66 | err = loc_network_set_country_code(network1, "DE"); |
3b5f4af2 MT |
67 | if (err) { |
68 | fprintf(stderr, "Could not set country code\n"); | |
69 | exit(EXIT_FAILURE); | |
70 | } | |
71 | ||
7933f5bf | 72 | #if 0 |
3b5f4af2 MT |
73 | // Adding network to the tree |
74 | err = loc_network_tree_add_network(tree, network1); | |
75 | if (err) { | |
76 | fprintf(stderr, "Could not add network to the tree\n"); | |
77 | exit(EXIT_FAILURE); | |
78 | } | |
7933f5bf | 79 | #endif |
3b5f4af2 | 80 | |
2b9338ea MT |
81 | // Check if the first and last addresses are correct |
82 | char* string = loc_network_format_first_address(network1); | |
83 | if (!string) { | |
84 | fprintf(stderr, "Did get NULL instead of a string for the first address\n"); | |
85 | exit(EXIT_FAILURE); | |
86 | } | |
87 | ||
88 | if (strcmp(string, "2001:db8::") != 0) { | |
89 | fprintf(stderr, "Got an incorrect first address: %s\n", string); | |
90 | exit(EXIT_FAILURE); | |
91 | } | |
92 | ||
93 | string = loc_network_format_last_address(network1); | |
94 | if (!string) { | |
95 | fprintf(stderr, "Did get NULL instead of a string for the last address\n"); | |
96 | exit(EXIT_FAILURE); | |
97 | } | |
98 | ||
99 | if (strcmp(string, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) { | |
100 | fprintf(stderr, "Got an incorrect last address: %s\n", string); | |
101 | exit(EXIT_FAILURE); | |
102 | } | |
103 | ||
0258d3c9 | 104 | err = loc_network_matches_address(network1, &address); |
fc692a58 MT |
105 | if (!err) { |
106 | fprintf(stderr, "Network1 does not match address\n"); | |
107 | exit(EXIT_FAILURE); | |
108 | } | |
109 | ||
c051bfad MT |
110 | struct loc_network* network2; |
111 | err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48"); | |
112 | if (err) { | |
113 | fprintf(stderr, "Could not create the network\n"); | |
114 | exit(EXIT_FAILURE); | |
115 | } | |
116 | ||
e8ebd079 | 117 | err = loc_network_set_country_code(network2, "DE"); |
c051bfad MT |
118 | if (err) { |
119 | fprintf(stderr, "Could not set country code\n"); | |
120 | exit(EXIT_FAILURE); | |
121 | } | |
122 | ||
7933f5bf | 123 | #if 0 |
c051bfad MT |
124 | // Adding network to the tree |
125 | err = loc_network_tree_add_network(tree, network2); | |
126 | if (err) { | |
127 | fprintf(stderr, "Could not add network to the tree\n"); | |
128 | exit(EXIT_FAILURE); | |
129 | } | |
130 | ||
3b5f4af2 MT |
131 | // Dump the tree |
132 | err = loc_network_tree_dump(tree); | |
133 | if (err) { | |
134 | fprintf(stderr, "Error dumping tree: %d\n", err); | |
135 | exit(EXIT_FAILURE); | |
136 | } | |
137 | ||
940f9c2b MT |
138 | size_t nodes = loc_network_tree_count_nodes(tree); |
139 | printf("The tree has %zu nodes\n", nodes); | |
7933f5bf | 140 | #endif |
940f9c2b | 141 | |
850e7516 | 142 | // Check equals function |
da101d55 MT |
143 | err = loc_network_cmp(network1, network1); |
144 | if (err) { | |
850e7516 MT |
145 | fprintf(stderr, "Network is not equal with itself\n"); |
146 | exit(EXIT_FAILURE); | |
147 | } | |
148 | ||
da101d55 MT |
149 | err = loc_network_cmp(network1, network2); |
150 | if (!err) { | |
850e7516 MT |
151 | fprintf(stderr, "Networks equal unexpectedly\n"); |
152 | exit(EXIT_FAILURE); | |
153 | } | |
154 | ||
43554dc4 | 155 | // Check subnet function |
a5967330 MT |
156 | err = loc_network_is_subnet(network1, network2); |
157 | if (!err) { | |
43554dc4 MT |
158 | fprintf(stderr, "Subnet check 1 failed: %d\n", err); |
159 | exit(EXIT_FAILURE); | |
160 | } | |
161 | ||
a5967330 MT |
162 | err = loc_network_is_subnet(network2, network1); |
163 | if (err) { | |
43554dc4 MT |
164 | fprintf(stderr, "Subnet check 2 failed: %d\n", err); |
165 | exit(EXIT_FAILURE); | |
166 | } | |
167 | ||
a5967330 MT |
168 | // Make subnets |
169 | struct loc_network* subnet1 = NULL; | |
170 | struct loc_network* subnet2 = NULL; | |
171 | ||
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); | |
850e7516 MT |
175 | exit(EXIT_FAILURE); |
176 | } | |
177 | ||
a5967330 MT |
178 | char* s = loc_network_str(subnet1); |
179 | printf("Received subnet1 = %s\n", s); | |
180 | free(s); | |
850e7516 | 181 | |
a5967330 MT |
182 | s = loc_network_str(subnet2); |
183 | printf("Received subnet2 = %s\n", s); | |
184 | free(s); | |
850e7516 | 185 | |
a5967330 MT |
186 | if (!loc_network_is_subnet(network1, subnet1)) { |
187 | fprintf(stderr, "Subnet1 is not a subnet\n"); | |
188 | exit(EXIT_FAILURE); | |
189 | } | |
850e7516 | 190 | |
a5967330 MT |
191 | if (!loc_network_is_subnet(network1, subnet2)) { |
192 | fprintf(stderr, "Subnet2 is not a subnet\n"); | |
193 | exit(EXIT_FAILURE); | |
850e7516 MT |
194 | } |
195 | ||
82fa4c92 MT |
196 | if (!loc_network_overlaps(network1, subnet1)) { |
197 | fprintf(stderr, "Network1 does not seem to contain subnet1\n"); | |
198 | exit(EXIT_FAILURE); | |
199 | } | |
200 | ||
201 | if (!loc_network_overlaps(network1, subnet2)) { | |
202 | fprintf(stderr, "Network1 does not seem to contain subnet2\n"); | |
203 | exit(EXIT_FAILURE); | |
204 | } | |
205 | ||
a5967330 MT |
206 | loc_network_unref(subnet1); |
207 | loc_network_unref(subnet2); | |
850e7516 MT |
208 | |
209 | struct loc_network_list* excluded = loc_network_exclude(network1, network2); | |
210 | if (!excluded) { | |
211 | fprintf(stderr, "Could not create excluded list\n"); | |
212 | exit(EXIT_FAILURE); | |
213 | } | |
214 | ||
215 | loc_network_list_dump(excluded); | |
850e7516 MT |
216 | loc_network_list_unref(excluded); |
217 | ||
f3e02bc5 MT |
218 | // Create a database |
219 | struct loc_writer* writer; | |
5ce881d4 | 220 | err = loc_writer_new(ctx, &writer, NULL, NULL); |
f3e02bc5 MT |
221 | if (err < 0) |
222 | exit(EXIT_FAILURE); | |
223 | ||
c051bfad | 224 | struct loc_network* network3; |
83a5d737 | 225 | err = loc_writer_add_network(writer, &network3, "2001:db8::/64"); |
f3e02bc5 MT |
226 | if (err) { |
227 | fprintf(stderr, "Could not add network\n"); | |
228 | exit(EXIT_FAILURE); | |
229 | } | |
230 | ||
231 | // Set country code | |
c051bfad | 232 | loc_network_set_country_code(network3, "XX"); |
f3e02bc5 | 233 | |
83a5d737 MT |
234 | struct loc_network* network4; |
235 | err = loc_writer_add_network(writer, &network4, "2001:db8:ffff::/64"); | |
236 | if (err) { | |
237 | fprintf(stderr, "Could not add network\n"); | |
238 | exit(EXIT_FAILURE); | |
239 | } | |
240 | ||
241 | // Set country code | |
242 | loc_network_set_country_code(network4, "XY"); | |
243 | ||
9a339aa0 MT |
244 | // Set ASN |
245 | loc_network_set_asn(network4, 1024); | |
246 | ||
0f1aedbc MT |
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); | |
252 | exit(EXIT_FAILURE); | |
253 | } | |
254 | ||
13ad6e69 MT |
255 | // Try adding a single address |
256 | err = loc_writer_add_network(writer, &network, "2001:db8::"); | |
26ab419b MT |
257 | if (err) { |
258 | fprintf(stderr, "It was impossible to add an single IP address (err = %d)\n", err); | |
13ad6e69 MT |
259 | exit(EXIT_FAILURE); |
260 | } | |
261 | ||
6a467e93 MT |
262 | // Try adding localhost |
263 | err = loc_writer_add_network(writer, &network, "::1/128"); | |
264 | if (err != -EINVAL) { | |
265 | fprintf(stderr, "It was possible to add localhost (::1/128): %d\n", err); | |
266 | exit(EXIT_FAILURE); | |
267 | } | |
268 | ||
6254dca6 | 269 | FILE* f = tmpfile(); |
f3e02bc5 MT |
270 | if (!f) { |
271 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
272 | exit(EXIT_FAILURE); | |
273 | } | |
274 | ||
22c7b98b | 275 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); |
f3e02bc5 MT |
276 | if (err) { |
277 | fprintf(stderr, "Could not write database: %s\n", strerror(-err)); | |
278 | exit(EXIT_FAILURE); | |
279 | } | |
f3e02bc5 MT |
280 | loc_writer_unref(writer); |
281 | ||
3b5f4af2 | 282 | loc_network_unref(network1); |
c051bfad MT |
283 | loc_network_unref(network2); |
284 | loc_network_unref(network3); | |
83a5d737 | 285 | loc_network_unref(network4); |
7933f5bf MT |
286 | |
287 | #if 0 | |
3b5f4af2 | 288 | loc_network_tree_unref(tree); |
7933f5bf | 289 | #endif |
2a30e4de MT |
290 | |
291 | // And open it again from disk | |
2a30e4de MT |
292 | struct loc_database* db; |
293 | err = loc_database_new(ctx, &db, f); | |
294 | if (err) { | |
295 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
296 | exit(EXIT_FAILURE); | |
297 | } | |
298 | ||
e477e813 | 299 | // Lookup an address in the subnet |
2a30e4de MT |
300 | err = loc_database_lookup_from_string(db, "2001:db8::", &network1); |
301 | if (err) { | |
e477e813 | 302 | fprintf(stderr, "Could not look up 2001:db8::\n"); |
2a30e4de MT |
303 | exit(EXIT_FAILURE); |
304 | } | |
305 | loc_network_unref(network1); | |
306 | ||
e477e813 | 307 | // Lookup an address outside the subnet |
2a30e4de | 308 | err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1); |
e477e813 MT |
309 | if (err == 0) { |
310 | fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n"); | |
2a30e4de MT |
311 | exit(EXIT_FAILURE); |
312 | } | |
313 | loc_network_unref(network1); | |
314 | ||
f69e0c8b MT |
315 | const struct bit_length_test { |
316 | const char* network; | |
317 | unsigned int bit_length; | |
318 | } bit_length_tests[] = { | |
319 | { "::/0", 0 }, | |
320 | { "2001::/128", 126 }, | |
321 | { NULL, 0, }, | |
322 | }; | |
323 | ||
324 | for (const struct bit_length_test* t = bit_length_tests; t->network; t++) { | |
325 | err = loc_network_new_from_string(ctx, &network1, t->network); | |
326 | if (err) { | |
327 | fprintf(stderr, "Could not create network %s: %m\n", t->network); | |
328 | exit(EXIT_FAILURE); | |
329 | } | |
330 | ||
331 | const struct in6_addr* addr = loc_network_get_first_address(network1); | |
332 | ||
333 | unsigned int bit_length = loc_address_bit_length(addr); | |
334 | ||
335 | if (bit_length != t->bit_length) { | |
336 | printf("Bit length of %s didn't match: %u != %u\n", | |
337 | t->network, t->bit_length, bit_length); | |
338 | loc_network_unref(network1); | |
339 | exit(EXIT_FAILURE); | |
340 | } | |
341 | ||
342 | loc_network_unref(network1); | |
343 | } | |
344 | ||
3b5f4af2 | 345 | loc_unref(ctx); |
6254dca6 | 346 | fclose(f); |
3b5f4af2 MT |
347 | |
348 | return EXIT_SUCCESS; | |
349 | } |