]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-network.c
network: Implement bit length function for IPv4
[people/ms/libloc.git] / src / test-network.c
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/database.h>
27 #include <libloc/network.h>
28 #include <libloc/private.h>
29 #include <libloc/writer.h>
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
39 // Enable debug logging
40 loc_set_log_priority(ctx, LOG_DEBUG);
41
42 #if 0
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 }
49 #endif
50
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
58 // Create a network
59 struct loc_network* network1;
60 err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32");
61 if (err) {
62 fprintf(stderr, "Could not create the network\n");
63 exit(EXIT_FAILURE);
64 }
65
66 err = loc_network_set_country_code(network1, "DE");
67 if (err) {
68 fprintf(stderr, "Could not set country code\n");
69 exit(EXIT_FAILURE);
70 }
71
72 #if 0
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 }
79 #endif
80
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
104 err = loc_network_matches_address(network1, &address);
105 if (!err) {
106 fprintf(stderr, "Network1 does not match address\n");
107 exit(EXIT_FAILURE);
108 }
109
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
117 err = loc_network_set_country_code(network2, "DE");
118 if (err) {
119 fprintf(stderr, "Could not set country code\n");
120 exit(EXIT_FAILURE);
121 }
122
123 #if 0
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
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
138 size_t nodes = loc_network_tree_count_nodes(tree);
139 printf("The tree has %zu nodes\n", nodes);
140 #endif
141
142 // Check equals function
143 err = loc_network_cmp(network1, network1);
144 if (err) {
145 fprintf(stderr, "Network is not equal with itself\n");
146 exit(EXIT_FAILURE);
147 }
148
149 err = loc_network_cmp(network1, network2);
150 if (!err) {
151 fprintf(stderr, "Networks equal unexpectedly\n");
152 exit(EXIT_FAILURE);
153 }
154
155 // Check subnet function
156 err = loc_network_is_subnet(network1, network2);
157 if (!err) {
158 fprintf(stderr, "Subnet check 1 failed: %d\n", err);
159 exit(EXIT_FAILURE);
160 }
161
162 err = loc_network_is_subnet(network2, network1);
163 if (err) {
164 fprintf(stderr, "Subnet check 2 failed: %d\n", err);
165 exit(EXIT_FAILURE);
166 }
167
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);
175 exit(EXIT_FAILURE);
176 }
177
178 char* s = loc_network_str(subnet1);
179 printf("Received subnet1 = %s\n", s);
180 free(s);
181
182 s = loc_network_str(subnet2);
183 printf("Received subnet2 = %s\n", s);
184 free(s);
185
186 if (!loc_network_is_subnet(network1, subnet1)) {
187 fprintf(stderr, "Subnet1 is not a subnet\n");
188 exit(EXIT_FAILURE);
189 }
190
191 if (!loc_network_is_subnet(network1, subnet2)) {
192 fprintf(stderr, "Subnet2 is not a subnet\n");
193 exit(EXIT_FAILURE);
194 }
195
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
206 loc_network_unref(subnet1);
207 loc_network_unref(subnet2);
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);
216 loc_network_list_unref(excluded);
217
218 // Create a database
219 struct loc_writer* writer;
220 err = loc_writer_new(ctx, &writer, NULL, NULL);
221 if (err < 0)
222 exit(EXIT_FAILURE);
223
224 struct loc_network* network3;
225 err = loc_writer_add_network(writer, &network3, "2001:db8::/64");
226 if (err) {
227 fprintf(stderr, "Could not add network\n");
228 exit(EXIT_FAILURE);
229 }
230
231 // Set country code
232 loc_network_set_country_code(network3, "XX");
233
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
244 // Set ASN
245 loc_network_set_asn(network4, 1024);
246
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
255 // Try adding a single address
256 err = loc_writer_add_network(writer, &network, "2001:db8::");
257 if (err) {
258 fprintf(stderr, "It was impossible to add an single IP address (err = %d)\n", err);
259 exit(EXIT_FAILURE);
260 }
261
262 FILE* f = tmpfile();
263 if (!f) {
264 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
265 exit(EXIT_FAILURE);
266 }
267
268 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
269 if (err) {
270 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
271 exit(EXIT_FAILURE);
272 }
273 loc_writer_unref(writer);
274
275 loc_network_unref(network1);
276 loc_network_unref(network2);
277 loc_network_unref(network3);
278 loc_network_unref(network4);
279
280 #if 0
281 loc_network_tree_unref(tree);
282 #endif
283
284 // And open it again from disk
285 struct loc_database* db;
286 err = loc_database_new(ctx, &db, f);
287 if (err) {
288 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
289 exit(EXIT_FAILURE);
290 }
291
292 // Lookup an address in the subnet
293 err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
294 if (err) {
295 fprintf(stderr, "Could not look up 2001:db8::\n");
296 exit(EXIT_FAILURE);
297 }
298 loc_network_unref(network1);
299
300 // Lookup an address outside the subnet
301 err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
302 if (err == 0) {
303 fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
304 exit(EXIT_FAILURE);
305 }
306 loc_network_unref(network1);
307
308 const struct bit_length_test {
309 const char* network;
310 unsigned int bit_length;
311 } bit_length_tests[] = {
312 { "::/0", 0 },
313 { "2001::/128", 126 },
314 { "1.0.0.0/32", 25 },
315 { "255.255.255.255/32", 32 },
316 { NULL, 0, },
317 };
318
319 for (const struct bit_length_test* t = bit_length_tests; t->network; t++) {
320 err = loc_network_new_from_string(ctx, &network1, t->network);
321 if (err) {
322 fprintf(stderr, "Could not create network %s: %m\n", t->network);
323 exit(EXIT_FAILURE);
324 }
325
326 const struct in6_addr* addr = loc_network_get_first_address(network1);
327
328 unsigned int bit_length = loc_address_bit_length(addr);
329
330 if (bit_length != t->bit_length) {
331 printf("Bit length of %s didn't match: %u != %u\n",
332 t->network, t->bit_length, bit_length);
333 loc_network_unref(network1);
334 exit(EXIT_FAILURE);
335 }
336
337 loc_network_unref(network1);
338 }
339
340 loc_unref(ctx);
341 fclose(f);
342
343 return EXIT_SUCCESS;
344 }