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