]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-network.c
network: Add functions to break network into subnets
[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
f3e02bc5 17#include <errno.h>
3b5f4af2
MT
18#include <stdio.h>
19#include <stddef.h>
20#include <stdlib.h>
21#include <string.h>
37317e4f 22#include <syslog.h>
3b5f4af2
MT
23
24#include <loc/libloc.h>
2a30e4de 25#include <loc/database.h>
3b5f4af2 26#include <loc/network.h>
f3e02bc5 27#include <loc/writer.h>
3b5f4af2
MT
28
29int main(int argc, char** argv) {
30 int err;
31
32 struct loc_ctx* ctx;
33 err = loc_new(&ctx);
34 if (err < 0)
35 exit(EXIT_FAILURE);
36
37317e4f
MT
37 // Enable debug logging
38 loc_set_log_priority(ctx, LOG_DEBUG);
39
3b5f4af2
MT
40 struct loc_network_tree* tree;
41 err = loc_network_tree_new(ctx, &tree);
42 if (err) {
43 fprintf(stderr, "Could not create the network tree\n");
44 exit(EXIT_FAILURE);
45 }
46
47 // Create a network
48 struct loc_network* network1;
2b9338ea 49 err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32");
3b5f4af2
MT
50 if (err) {
51 fprintf(stderr, "Could not create the network\n");
52 exit(EXIT_FAILURE);
53 }
54
55 err = loc_network_set_country_code(network1, "XX");
56 if (err) {
57 fprintf(stderr, "Could not set country code\n");
58 exit(EXIT_FAILURE);
59 }
60
61 // Adding network to the tree
62 err = loc_network_tree_add_network(tree, network1);
63 if (err) {
64 fprintf(stderr, "Could not add network to the tree\n");
65 exit(EXIT_FAILURE);
66 }
67
2b9338ea
MT
68 // Check if the first and last addresses are correct
69 char* string = loc_network_format_first_address(network1);
70 if (!string) {
71 fprintf(stderr, "Did get NULL instead of a string for the first address\n");
72 exit(EXIT_FAILURE);
73 }
74
75 if (strcmp(string, "2001:db8::") != 0) {
76 fprintf(stderr, "Got an incorrect first address: %s\n", string);
77 exit(EXIT_FAILURE);
78 }
79
80 string = loc_network_format_last_address(network1);
81 if (!string) {
82 fprintf(stderr, "Did get NULL instead of a string for the last address\n");
83 exit(EXIT_FAILURE);
84 }
85
86 if (strcmp(string, "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff") != 0) {
87 fprintf(stderr, "Got an incorrect last address: %s\n", string);
88 exit(EXIT_FAILURE);
89 }
90
c051bfad
MT
91 struct loc_network* network2;
92 err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48");
93 if (err) {
94 fprintf(stderr, "Could not create the network\n");
95 exit(EXIT_FAILURE);
96 }
97
98 err = loc_network_set_country_code(network2, "XY");
99 if (err) {
100 fprintf(stderr, "Could not set country code\n");
101 exit(EXIT_FAILURE);
102 }
103
104 // Adding network to the tree
105 err = loc_network_tree_add_network(tree, network2);
106 if (err) {
107 fprintf(stderr, "Could not add network to the tree\n");
108 exit(EXIT_FAILURE);
109 }
110
3b5f4af2
MT
111 // Dump the tree
112 err = loc_network_tree_dump(tree);
113 if (err) {
114 fprintf(stderr, "Error dumping tree: %d\n", err);
115 exit(EXIT_FAILURE);
116 }
117
940f9c2b
MT
118 size_t nodes = loc_network_tree_count_nodes(tree);
119 printf("The tree has %zu nodes\n", nodes);
120
850e7516
MT
121 // Check equals function
122 err = loc_network_eq(network1, network1);
123 if (!err) {
124 fprintf(stderr, "Network is not equal with itself\n");
125 exit(EXIT_FAILURE);
126 }
127
128 err = loc_network_eq(network1, network2);
129 if (err) {
130 fprintf(stderr, "Networks equal unexpectedly\n");
131 exit(EXIT_FAILURE);
132 }
133
43554dc4
MT
134 // Check subnet function
135 err = loc_network_is_subnet_of(network1, network2);
136 if (err != 0) {
137 fprintf(stderr, "Subnet check 1 failed: %d\n", err);
138 exit(EXIT_FAILURE);
139 }
140
141 err = loc_network_is_subnet_of(network2, network1);
142 if (err != 1) {
143 fprintf(stderr, "Subnet check 2 failed: %d\n", err);
144 exit(EXIT_FAILURE);
145 }
146
850e7516
MT
147 // Make list of subnets
148 struct loc_network_list* subnets = loc_network_subnets(network1);
149 if (!subnets) {
150 fprintf(stderr, "Could not find subnets of network\n");
151 exit(EXIT_FAILURE);
152 }
153
154 loc_network_list_dump(subnets);
155
156 while (!loc_network_list_empty(subnets)) {
157 struct loc_network* subnet = loc_network_list_pop(subnets);
158 if (!subnet) {
159 fprintf(stderr, "Received an empty subnet\n");
160 exit(EXIT_FAILURE);
161 }
162
163 char* s = loc_network_str(subnet);
164 printf("Received subnet %s\n", s);
165 free(s);
166
167 if (!loc_network_is_subnet_of(subnet, network1)) {
168 fprintf(stderr, "Not a subnet\n");
169 exit(EXIT_FAILURE);
170 }
171
172 loc_network_unref(subnet);
173 }
174
175 loc_network_list_unref(subnets);
176
177 struct loc_network_list* excluded = loc_network_exclude(network1, network2);
178 if (!excluded) {
179 fprintf(stderr, "Could not create excluded list\n");
180 exit(EXIT_FAILURE);
181 }
182
183 loc_network_list_dump(excluded);
184
185 // Reverse it
186 loc_network_list_reverse(excluded);
187 loc_network_list_dump(excluded);
188
189 // Sort them and dump them again
190 loc_network_list_sort(excluded);
191 loc_network_list_dump(excluded);
192
193 loc_network_list_unref(excluded);
194
f3e02bc5
MT
195 // Create a database
196 struct loc_writer* writer;
5ce881d4 197 err = loc_writer_new(ctx, &writer, NULL, NULL);
f3e02bc5
MT
198 if (err < 0)
199 exit(EXIT_FAILURE);
200
c051bfad 201 struct loc_network* network3;
83a5d737 202 err = loc_writer_add_network(writer, &network3, "2001:db8::/64");
f3e02bc5
MT
203 if (err) {
204 fprintf(stderr, "Could not add network\n");
205 exit(EXIT_FAILURE);
206 }
207
208 // Set country code
c051bfad 209 loc_network_set_country_code(network3, "XX");
f3e02bc5 210
83a5d737
MT
211 struct loc_network* network4;
212 err = loc_writer_add_network(writer, &network4, "2001:db8:ffff::/64");
213 if (err) {
214 fprintf(stderr, "Could not add network\n");
215 exit(EXIT_FAILURE);
216 }
217
218 // Set country code
219 loc_network_set_country_code(network4, "XY");
220
9a339aa0
MT
221 // Set ASN
222 loc_network_set_asn(network4, 1024);
223
0f1aedbc
MT
224 // Try adding an invalid network
225 struct loc_network* network;
226 err = loc_writer_add_network(writer, &network, "xxxx:xxxx::/32");
227 if (err != -EINVAL) {
228 fprintf(stderr, "It was possible to add an invalid network (err = %d)\n", err);
229 exit(EXIT_FAILURE);
230 }
231
13ad6e69
MT
232 // Try adding a single address
233 err = loc_writer_add_network(writer, &network, "2001:db8::");
26ab419b
MT
234 if (err) {
235 fprintf(stderr, "It was impossible to add an single IP address (err = %d)\n", err);
13ad6e69
MT
236 exit(EXIT_FAILURE);
237 }
238
6a467e93
MT
239 // Try adding localhost
240 err = loc_writer_add_network(writer, &network, "::1/128");
241 if (err != -EINVAL) {
242 fprintf(stderr, "It was possible to add localhost (::1/128): %d\n", err);
243 exit(EXIT_FAILURE);
244 }
245
6254dca6 246 FILE* f = tmpfile();
f3e02bc5
MT
247 if (!f) {
248 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
249 exit(EXIT_FAILURE);
250 }
251
22c7b98b 252 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
f3e02bc5
MT
253 if (err) {
254 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
255 exit(EXIT_FAILURE);
256 }
f3e02bc5
MT
257 loc_writer_unref(writer);
258
3b5f4af2 259 loc_network_unref(network1);
c051bfad
MT
260 loc_network_unref(network2);
261 loc_network_unref(network3);
83a5d737 262 loc_network_unref(network4);
3b5f4af2 263 loc_network_tree_unref(tree);
2a30e4de
MT
264
265 // And open it again from disk
2a30e4de
MT
266 struct loc_database* db;
267 err = loc_database_new(ctx, &db, f);
268 if (err) {
269 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
270 exit(EXIT_FAILURE);
271 }
272
e477e813 273 // Lookup an address in the subnet
2a30e4de
MT
274 err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
275 if (err) {
e477e813 276 fprintf(stderr, "Could not look up 2001:db8::\n");
2a30e4de
MT
277 exit(EXIT_FAILURE);
278 }
279 loc_network_unref(network1);
280
e477e813 281 // Lookup an address outside the subnet
2a30e4de 282 err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
e477e813
MT
283 if (err == 0) {
284 fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
2a30e4de
MT
285 exit(EXIT_FAILURE);
286 }
287 loc_network_unref(network1);
288
3b5f4af2 289 loc_unref(ctx);
6254dca6 290 fclose(f);
3b5f4af2
MT
291
292 return EXIT_SUCCESS;
293}