]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-network.c
location: Implement listing bogons
[people/ms/libloc.git] / src / test-network.c
CommitLineData
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
MT
24
25#include <loc/libloc.h>
2a30e4de 26#include <loc/database.h>
3b5f4af2 27#include <loc/network.h>
f3e02bc5 28#include <loc/writer.h>
3b5f4af2
MT
29
30int 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
37317e4f
MT
38 // Enable debug logging
39 loc_set_log_priority(ctx, LOG_DEBUG);
40
7933f5bf 41#if 0
3b5f4af2
MT
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 }
7933f5bf 48#endif
3b5f4af2 49
fc692a58
MT
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
3b5f4af2
MT
57 // Create a network
58 struct loc_network* network1;
2b9338ea 59 err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32");
3b5f4af2
MT
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
7933f5bf 71#if 0
3b5f4af2
MT
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 }
7933f5bf 78#endif
3b5f4af2 79
2b9338ea
MT
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
fc692a58
MT
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
c051bfad
MT
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
7933f5bf 122#if 0
c051bfad
MT
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
3b5f4af2
MT
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
940f9c2b
MT
137 size_t nodes = loc_network_tree_count_nodes(tree);
138 printf("The tree has %zu nodes\n", nodes);
7933f5bf 139#endif
940f9c2b 140
850e7516 141 // Check equals function
da101d55
MT
142 err = loc_network_cmp(network1, network1);
143 if (err) {
850e7516
MT
144 fprintf(stderr, "Network is not equal with itself\n");
145 exit(EXIT_FAILURE);
146 }
147
da101d55
MT
148 err = loc_network_cmp(network1, network2);
149 if (!err) {
850e7516
MT
150 fprintf(stderr, "Networks equal unexpectedly\n");
151 exit(EXIT_FAILURE);
152 }
153
43554dc4 154 // Check subnet function
a5967330
MT
155 err = loc_network_is_subnet(network1, network2);
156 if (!err) {
43554dc4
MT
157 fprintf(stderr, "Subnet check 1 failed: %d\n", err);
158 exit(EXIT_FAILURE);
159 }
160
a5967330
MT
161 err = loc_network_is_subnet(network2, network1);
162 if (err) {
43554dc4
MT
163 fprintf(stderr, "Subnet check 2 failed: %d\n", err);
164 exit(EXIT_FAILURE);
165 }
166
a5967330
MT
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);
850e7516
MT
174 exit(EXIT_FAILURE);
175 }
176
a5967330
MT
177 char* s = loc_network_str(subnet1);
178 printf("Received subnet1 = %s\n", s);
179 free(s);
850e7516 180
a5967330
MT
181 s = loc_network_str(subnet2);
182 printf("Received subnet2 = %s\n", s);
183 free(s);
850e7516 184
a5967330
MT
185 if (!loc_network_is_subnet(network1, subnet1)) {
186 fprintf(stderr, "Subnet1 is not a subnet\n");
187 exit(EXIT_FAILURE);
188 }
850e7516 189
a5967330
MT
190 if (!loc_network_is_subnet(network1, subnet2)) {
191 fprintf(stderr, "Subnet2 is not a subnet\n");
192 exit(EXIT_FAILURE);
850e7516
MT
193 }
194
82fa4c92
MT
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
a5967330
MT
205 loc_network_unref(subnet1);
206 loc_network_unref(subnet2);
850e7516
MT
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);
850e7516
MT
215 loc_network_list_unref(excluded);
216
f3e02bc5
MT
217 // Create a database
218 struct loc_writer* writer;
5ce881d4 219 err = loc_writer_new(ctx, &writer, NULL, NULL);
f3e02bc5
MT
220 if (err < 0)
221 exit(EXIT_FAILURE);
222
c051bfad 223 struct loc_network* network3;
83a5d737 224 err = loc_writer_add_network(writer, &network3, "2001:db8::/64");
f3e02bc5
MT
225 if (err) {
226 fprintf(stderr, "Could not add network\n");
227 exit(EXIT_FAILURE);
228 }
229
230 // Set country code
c051bfad 231 loc_network_set_country_code(network3, "XX");
f3e02bc5 232
83a5d737
MT
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
9a339aa0
MT
243 // Set ASN
244 loc_network_set_asn(network4, 1024);
245
0f1aedbc
MT
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
13ad6e69
MT
254 // Try adding a single address
255 err = loc_writer_add_network(writer, &network, "2001:db8::");
26ab419b
MT
256 if (err) {
257 fprintf(stderr, "It was impossible to add an single IP address (err = %d)\n", err);
13ad6e69
MT
258 exit(EXIT_FAILURE);
259 }
260
6a467e93
MT
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
6254dca6 268 FILE* f = tmpfile();
f3e02bc5
MT
269 if (!f) {
270 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
271 exit(EXIT_FAILURE);
272 }
273
22c7b98b 274 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
f3e02bc5
MT
275 if (err) {
276 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
277 exit(EXIT_FAILURE);
278 }
f3e02bc5
MT
279 loc_writer_unref(writer);
280
3b5f4af2 281 loc_network_unref(network1);
c051bfad
MT
282 loc_network_unref(network2);
283 loc_network_unref(network3);
83a5d737 284 loc_network_unref(network4);
7933f5bf
MT
285
286#if 0
3b5f4af2 287 loc_network_tree_unref(tree);
7933f5bf 288#endif
2a30e4de
MT
289
290 // And open it again from disk
2a30e4de
MT
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
e477e813 298 // Lookup an address in the subnet
2a30e4de
MT
299 err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
300 if (err) {
e477e813 301 fprintf(stderr, "Could not look up 2001:db8::\n");
2a30e4de
MT
302 exit(EXIT_FAILURE);
303 }
304 loc_network_unref(network1);
305
e477e813 306 // Lookup an address outside the subnet
2a30e4de 307 err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
e477e813
MT
308 if (err == 0) {
309 fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
2a30e4de
MT
310 exit(EXIT_FAILURE);
311 }
312 loc_network_unref(network1);
313
3b5f4af2 314 loc_unref(ctx);
6254dca6 315 fclose(f);
3b5f4af2
MT
316
317 return EXIT_SUCCESS;
318}