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