]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-network.c
importer: Drop EDROP as it has been merged into DROP
[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 <loc/libloc.h>
26 #include <loc/database.h>
27 #include <loc/network.h>
28 #include <loc/writer.h>
29
30 int main(int argc, char** argv) {
31 int err;
32
33 struct loc_ctx* ctx;
34 err = loc_new(&ctx);
35 if (err < 0)
36 exit(EXIT_FAILURE);
37
38 // Enable debug logging
39 loc_set_log_priority(ctx, LOG_DEBUG);
40
41 #if 0
42 struct loc_network_tree* tree;
43 err = loc_network_tree_new(ctx, &tree);
44 if (err) {
45 fprintf(stderr, "Could not create the network tree\n");
46 exit(EXIT_FAILURE);
47 }
48 #endif
49
50 struct in6_addr address;
51 err = inet_pton(AF_INET6, "2001:db8::1", &address);
52 if (err != 1) {
53 fprintf(stderr, "Could not parse IP address\n");
54 exit(EXIT_FAILURE);
55 }
56
57 // Create a network
58 struct loc_network* network1;
59 err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32");
60 if (err) {
61 fprintf(stderr, "Could not create the network\n");
62 exit(EXIT_FAILURE);
63 }
64
65 err = loc_network_set_country_code(network1, "XX");
66 if (err) {
67 fprintf(stderr, "Could not set country code\n");
68 exit(EXIT_FAILURE);
69 }
70
71 #if 0
72 // Adding network to the tree
73 err = loc_network_tree_add_network(tree, network1);
74 if (err) {
75 fprintf(stderr, "Could not add network to the tree\n");
76 exit(EXIT_FAILURE);
77 }
78 #endif
79
80 // Check if the first and last addresses are correct
81 char* string = loc_network_format_first_address(network1);
82 if (!string) {
83 fprintf(stderr, "Did get NULL instead of a string for the first address\n");
84 exit(EXIT_FAILURE);
85 }
86
87 if (strcmp(string, "2001:db8::") != 0) {
88 fprintf(stderr, "Got an incorrect first address: %s\n", string);
89 exit(EXIT_FAILURE);
90 }
91
92 string = loc_network_format_last_address(network1);
93 if (!string) {
94 fprintf(stderr, "Did get NULL instead of a string for the last address\n");
95 exit(EXIT_FAILURE);
96 }
97
98 if (strcmp(string, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
99 fprintf(stderr, "Got an incorrect last address: %s\n", string);
100 exit(EXIT_FAILURE);
101 }
102
103 err = loc_network_match_address(network1, &address);
104 if (!err) {
105 fprintf(stderr, "Network1 does not match address\n");
106 exit(EXIT_FAILURE);
107 }
108
109 struct loc_network* network2;
110 err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48");
111 if (err) {
112 fprintf(stderr, "Could not create the network\n");
113 exit(EXIT_FAILURE);
114 }
115
116 err = loc_network_set_country_code(network2, "XY");
117 if (err) {
118 fprintf(stderr, "Could not set country code\n");
119 exit(EXIT_FAILURE);
120 }
121
122 #if 0
123 // Adding network to the tree
124 err = loc_network_tree_add_network(tree, network2);
125 if (err) {
126 fprintf(stderr, "Could not add network to the tree\n");
127 exit(EXIT_FAILURE);
128 }
129
130 // Dump the tree
131 err = loc_network_tree_dump(tree);
132 if (err) {
133 fprintf(stderr, "Error dumping tree: %d\n", err);
134 exit(EXIT_FAILURE);
135 }
136
137 size_t nodes = loc_network_tree_count_nodes(tree);
138 printf("The tree has %zu nodes\n", nodes);
139 #endif
140
141 // Check equals function
142 err = loc_network_cmp(network1, network1);
143 if (err) {
144 fprintf(stderr, "Network is not equal with itself\n");
145 exit(EXIT_FAILURE);
146 }
147
148 err = loc_network_cmp(network1, network2);
149 if (!err) {
150 fprintf(stderr, "Networks equal unexpectedly\n");
151 exit(EXIT_FAILURE);
152 }
153
154 // Check subnet function
155 err = loc_network_is_subnet(network1, network2);
156 if (!err) {
157 fprintf(stderr, "Subnet check 1 failed: %d\n", err);
158 exit(EXIT_FAILURE);
159 }
160
161 err = loc_network_is_subnet(network2, network1);
162 if (err) {
163 fprintf(stderr, "Subnet check 2 failed: %d\n", err);
164 exit(EXIT_FAILURE);
165 }
166
167 // Make subnets
168 struct loc_network* subnet1 = NULL;
169 struct loc_network* subnet2 = NULL;
170
171 err = loc_network_subnets(network1, &subnet1, &subnet2);
172 if (err || !subnet1 || !subnet2) {
173 fprintf(stderr, "Could not find subnets of network: %d\n", err);
174 exit(EXIT_FAILURE);
175 }
176
177 char* s = loc_network_str(subnet1);
178 printf("Received subnet1 = %s\n", s);
179 free(s);
180
181 s = loc_network_str(subnet2);
182 printf("Received subnet2 = %s\n", s);
183 free(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 != -EINVAL) {
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 // Try adding localhost
262 err = loc_writer_add_network(writer, &network, "::1/128");
263 if (err != -EINVAL) {
264 fprintf(stderr, "It was possible to add localhost (::1/128): %d\n", err);
265 exit(EXIT_FAILURE);
266 }
267
268 FILE* f = tmpfile();
269 if (!f) {
270 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
271 exit(EXIT_FAILURE);
272 }
273
274 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
275 if (err) {
276 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
277 exit(EXIT_FAILURE);
278 }
279 loc_writer_unref(writer);
280
281 loc_network_unref(network1);
282 loc_network_unref(network2);
283 loc_network_unref(network3);
284 loc_network_unref(network4);
285
286 #if 0
287 loc_network_tree_unref(tree);
288 #endif
289
290 // And open it again from disk
291 struct loc_database* db;
292 err = loc_database_new(ctx, &db, f);
293 if (err) {
294 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
295 exit(EXIT_FAILURE);
296 }
297
298 // Lookup an address in the subnet
299 err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
300 if (err) {
301 fprintf(stderr, "Could not look up 2001:db8::\n");
302 exit(EXIT_FAILURE);
303 }
304 loc_network_unref(network1);
305
306 // Lookup an address outside the subnet
307 err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
308 if (err == 0) {
309 fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
310 exit(EXIT_FAILURE);
311 }
312 loc_network_unref(network1);
313
314 loc_unref(ctx);
315 fclose(f);
316
317 return EXIT_SUCCESS;
318 }