]> git.ipfire.org Git - location/libloc.git/blame - src/database.c
database: Restart flatten algorithm from the top when a network was dropped
[location/libloc.git] / src / database.c
CommitLineData
2601e83e
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 library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library 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 GNU
14 Lesser General Public License for more details.
15*/
16
2a30e4de 17#include <arpa/inet.h>
d3d8ede6 18#include <ctype.h>
2601e83e 19#include <errno.h>
10778041 20#include <netinet/in.h>
2601e83e
MT
21#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
c182393f 26#include <sys/mman.h>
2601e83e 27#include <sys/types.h>
96ea74a5 28#include <time.h>
3f35869a 29#include <unistd.h>
2601e83e 30
42f3ccd7
MT
31#ifdef HAVE_ENDIAN_H
32# include <endian.h>
33#endif
34
726f9984 35#include <openssl/err.h>
b1720435 36#include <openssl/evp.h>
726f9984 37#include <openssl/pem.h>
b1720435 38
2601e83e 39#include <loc/libloc.h>
9fc7f001 40#include <loc/as.h>
84a2f0c2 41#include <loc/as-list.h>
42f3ccd7 42#include <loc/compat.h>
ec684c1a 43#include <loc/country.h>
e646a8f3 44#include <loc/country-list.h>
9fc7f001 45#include <loc/database.h>
a5db3e49 46#include <loc/format.h>
10778041 47#include <loc/network.h>
9fc7f001
MT
48#include <loc/private.h>
49#include <loc/stringpool.h>
2601e83e
MT
50
51struct loc_database {
52 struct loc_ctx* ctx;
53 int refcount;
54
b1720435
MT
55 FILE* f;
56
22c7b98b 57 enum loc_database_version version;
96ea74a5 58 time_t created_at;
2601e83e
MT
59 off_t vendor;
60 off_t description;
4bf49d00 61 off_t license;
2601e83e 62
5ce881d4
MT
63 // Signatures
64 char* signature1;
65 size_t signature1_length;
66 char* signature2;
67 size_t signature2_length;
726f9984 68
a5db3e49 69 // ASes in the database
b904896a 70 struct loc_database_as_v1* as_v1;
a5db3e49
MT
71 size_t as_count;
72
f66b7b09 73 // Network tree
b904896a 74 struct loc_database_network_node_v1* network_nodes_v1;
f66b7b09
MT
75 size_t network_nodes_count;
76
a735a563 77 // Networks
b904896a 78 struct loc_database_network_v1* networks_v1;
a735a563
MT
79 size_t networks_count;
80
ec684c1a 81 // Countries
b904896a 82 struct loc_database_country_v1* countries_v1;
ec684c1a
MT
83 size_t countries_count;
84
2601e83e
MT
85 struct loc_stringpool* pool;
86};
87
e3f696c1
MT
88#define MAX_STACK_DEPTH 256
89
90struct loc_node_stack {
91 off_t offset;
92 int i; // Is this node 0 or 1?
93 int depth;
94};
95
7e13db74
MT
96struct loc_database_enumerator {
97 struct loc_ctx* ctx;
98 struct loc_database* db;
ccc7ab4e 99 enum loc_database_enumerator_mode mode;
7e13db74 100 int refcount;
d3d8ede6
MT
101
102 // Search string
103 char* string;
e646a8f3 104 struct loc_country_list* countries;
84a2f0c2 105 struct loc_as_list* asns;
9268db5a 106 enum loc_network_flags flags;
44e5ef71 107 int family;
d3d8ede6 108
681ff05c
MT
109 // Flatten output?
110 int flatten;
111
d3d8ede6
MT
112 // Index of the AS we are looking at
113 unsigned int as_index;
e3f696c1 114
fa9a3663
MT
115 // Index of the country we are looking at
116 unsigned int country_index;
117
e3f696c1
MT
118 // Network state
119 struct in6_addr network_address;
120 struct loc_node_stack network_stack[MAX_STACK_DEPTH];
121 int network_stack_depth;
122 unsigned int* networks_visited;
d87fd7a3
MT
123
124 // For subnet search
125 struct loc_network_list* stack;
7e13db74
MT
126};
127
b1720435 128static int loc_database_read_magic(struct loc_database* db) {
2601e83e
MT
129 struct loc_database_magic magic;
130
131 // Read from file
b1720435 132 size_t bytes_read = fread(&magic, 1, sizeof(magic), db->f);
2601e83e
MT
133
134 // Check if we have been able to read enough data
135 if (bytes_read < sizeof(magic)) {
136 ERROR(db->ctx, "Could not read enough data to validate magic bytes\n");
137 DEBUG(db->ctx, "Read %zu bytes, but needed %zu\n", bytes_read, sizeof(magic));
138 return -ENOMSG;
139 }
140
141 // Compare magic bytes
142 if (memcmp(LOC_DATABASE_MAGIC, magic.magic, strlen(LOC_DATABASE_MAGIC)) == 0) {
143 DEBUG(db->ctx, "Magic value matches\n");
144
145 // Parse version
22c7b98b 146 db->version = magic.version;
2601e83e
MT
147
148 return 0;
149 }
150
22c7b98b 151 ERROR(db->ctx, "Unrecognized file type\n");
2601e83e
MT
152
153 // Return an error
154 return 1;
155}
156
b904896a
MT
157static int loc_database_read_as_section_v1(struct loc_database* db,
158 const struct loc_database_header_v1* header) {
edb4ba7c
MT
159 off_t as_offset = be32toh(header->as_offset);
160 size_t as_length = be32toh(header->as_length);
161
5c57de03 162 DEBUG(db->ctx, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset, as_length);
a5db3e49 163
c182393f 164 if (as_length > 0) {
b904896a 165 db->as_v1 = mmap(NULL, as_length, PROT_READ,
b1720435 166 MAP_SHARED, fileno(db->f), as_offset);
a5db3e49 167
b904896a 168 if (db->as_v1 == MAP_FAILED)
c182393f 169 return -errno;
a5db3e49
MT
170 }
171
b904896a 172 db->as_count = as_length / sizeof(*db->as_v1);
c182393f 173
a5db3e49
MT
174 INFO(db->ctx, "Read %zu ASes from the database\n", db->as_count);
175
176 return 0;
177}
178
b904896a
MT
179static int loc_database_read_network_nodes_section_v1(struct loc_database* db,
180 const struct loc_database_header_v1* header) {
edb4ba7c
MT
181 off_t network_nodes_offset = be32toh(header->network_tree_offset);
182 size_t network_nodes_length = be32toh(header->network_tree_length);
183
f66b7b09 184 DEBUG(db->ctx, "Reading network nodes section from %jd (%zu bytes)\n",
5c57de03 185 (intmax_t)network_nodes_offset, network_nodes_length);
f66b7b09
MT
186
187 if (network_nodes_length > 0) {
b904896a 188 db->network_nodes_v1 = mmap(NULL, network_nodes_length, PROT_READ,
b1720435 189 MAP_SHARED, fileno(db->f), network_nodes_offset);
f66b7b09 190
b904896a 191 if (db->network_nodes_v1 == MAP_FAILED)
f66b7b09
MT
192 return -errno;
193 }
194
b904896a 195 db->network_nodes_count = network_nodes_length / sizeof(*db->network_nodes_v1);
f66b7b09
MT
196
197 INFO(db->ctx, "Read %zu network nodes from the database\n", db->network_nodes_count);
198
199 return 0;
200}
201
b904896a
MT
202static int loc_database_read_networks_section_v1(struct loc_database* db,
203 const struct loc_database_header_v1* header) {
a735a563
MT
204 off_t networks_offset = be32toh(header->network_data_offset);
205 size_t networks_length = be32toh(header->network_data_length);
206
207 DEBUG(db->ctx, "Reading networks section from %jd (%zu bytes)\n",
5c57de03 208 (intmax_t)networks_offset, networks_length);
a735a563
MT
209
210 if (networks_length > 0) {
b904896a 211 db->networks_v1 = mmap(NULL, networks_length, PROT_READ,
b1720435 212 MAP_SHARED, fileno(db->f), networks_offset);
a735a563 213
b904896a 214 if (db->networks_v1 == MAP_FAILED)
a735a563
MT
215 return -errno;
216 }
217
b904896a 218 db->networks_count = networks_length / sizeof(*db->networks_v1);
a735a563
MT
219
220 INFO(db->ctx, "Read %zu networks from the database\n", db->networks_count);
221
222 return 0;
223}
224
b904896a
MT
225static int loc_database_read_countries_section_v1(struct loc_database* db,
226 const struct loc_database_header_v1* header) {
ec684c1a
MT
227 off_t countries_offset = be32toh(header->countries_offset);
228 size_t countries_length = be32toh(header->countries_length);
229
230 DEBUG(db->ctx, "Reading countries section from %jd (%zu bytes)\n",
2e2325a9 231 (intmax_t)countries_offset, countries_length);
ec684c1a
MT
232
233 if (countries_length > 0) {
b904896a 234 db->countries_v1 = mmap(NULL, countries_length, PROT_READ,
b1720435 235 MAP_SHARED, fileno(db->f), countries_offset);
ec684c1a 236
b904896a 237 if (db->countries_v1 == MAP_FAILED)
ec684c1a
MT
238 return -errno;
239 }
240
b904896a 241 db->countries_count = countries_length / sizeof(*db->countries_v1);
ec684c1a
MT
242
243 INFO(db->ctx, "Read %zu countries from the database\n",
244 db->countries_count);
245
246 return 0;
247}
248
5ce881d4
MT
249static int loc_database_read_signature(struct loc_database* db,
250 char** dst, char* src, size_t length) {
251 // Check for a plausible signature length
252 if (length > LOC_SIGNATURE_MAX_LENGTH) {
36b9fd75 253 ERROR(db->ctx, "Signature too long: %zu\n", length);
5ce881d4
MT
254 return -EINVAL;
255 }
256
36b9fd75 257 DEBUG(db->ctx, "Reading signature of %zu bytes\n", length);
5ce881d4
MT
258
259 // Allocate space
260 *dst = malloc(length);
261 if (!*dst)
262 return -ENOMEM;
263
264 // Copy payload
265 memcpy(*dst, src, length);
266
267 return 0;
268}
269
b904896a
MT
270static int loc_database_read_header_v1(struct loc_database* db) {
271 struct loc_database_header_v1 header;
5ce881d4 272 int r;
2601e83e
MT
273
274 // Read from file
b1720435 275 size_t size = fread(&header, 1, sizeof(header), db->f);
2601e83e
MT
276
277 if (size < sizeof(header)) {
278 ERROR(db->ctx, "Could not read enough data for header\n");
279 return -ENOMSG;
280 }
281
282 // Copy over data
96ea74a5 283 db->created_at = be64toh(header.created_at);
0676cd80
MT
284 db->vendor = be32toh(header.vendor);
285 db->description = be32toh(header.description);
4bf49d00 286 db->license = be32toh(header.license);
2601e83e 287
c7db968a
MT
288 db->signature1_length = be16toh(header.signature1_length);
289 db->signature2_length = be16toh(header.signature2_length);
726f9984 290
5ce881d4
MT
291 // Read signatures
292 if (db->signature1_length) {
293 r = loc_database_read_signature(db, &db->signature1,
294 header.signature1, db->signature1_length);
295 if (r)
296 return r;
297 }
726f9984 298
5ce881d4
MT
299 if (db->signature2_length) {
300 r = loc_database_read_signature(db, &db->signature2,
301 header.signature2, db->signature2_length);
302 if (r)
303 return r;
726f9984
MT
304 }
305
2601e83e 306 // Open pool
0676cd80
MT
307 off_t pool_offset = be32toh(header.pool_offset);
308 size_t pool_length = be32toh(header.pool_length);
2601e83e 309
5ce881d4 310 r = loc_stringpool_open(db->ctx, &db->pool,
b1720435 311 db->f, pool_length, pool_offset);
2601e83e
MT
312 if (r)
313 return r;
314
a5db3e49 315 // AS section
b904896a 316 r = loc_database_read_as_section_v1(db, &header);
a5db3e49
MT
317 if (r)
318 return r;
319
f66b7b09 320 // Network Nodes
b904896a 321 r = loc_database_read_network_nodes_section_v1(db, &header);
f66b7b09
MT
322 if (r)
323 return r;
324
a735a563 325 // Networks
b904896a 326 r = loc_database_read_networks_section_v1(db, &header);
a735a563
MT
327 if (r)
328 return r;
329
ec684c1a 330 // countries
b904896a 331 r = loc_database_read_countries_section_v1(db, &header);
ec684c1a
MT
332 if (r)
333 return r;
334
2601e83e
MT
335 return 0;
336}
337
b1720435 338static int loc_database_read_header(struct loc_database* db) {
22c7b98b
MT
339 DEBUG(db->ctx, "Database version is %u\n", db->version);
340
2601e83e 341 switch (db->version) {
22c7b98b 342 case LOC_DATABASE_VERSION_1:
b904896a 343 return loc_database_read_header_v1(db);
2601e83e
MT
344
345 default:
346 ERROR(db->ctx, "Incompatible database version: %u\n", db->version);
347 return 1;
348 }
349}
350
a7431f1a 351static int loc_database_read(struct loc_database* db, FILE* f) {
02879100
MT
352 clock_t start = clock();
353
b1720435
MT
354 int fd = fileno(f);
355
356 // Clone file descriptor
357 fd = dup(fd);
358 if (!fd) {
359 ERROR(db->ctx, "Could not duplicate file descriptor\n");
360 return -1;
361 }
362
363 // Reopen the file so that we can keep our own file handle
364 db->f = fdopen(fd, "r");
365 if (!db->f) {
366 ERROR(db->ctx, "Could not re-open database file\n");
367 return -1;
368 }
369
370 // Rewind to the start of the file
371 rewind(db->f);
372
02879100 373 // Read magic bytes
b1720435 374 int r = loc_database_read_magic(db);
02879100
MT
375 if (r)
376 return r;
377
378 // Read the header
b1720435 379 r = loc_database_read_header(db);
02879100
MT
380 if (r)
381 return r;
382
383 clock_t end = clock();
384
e16c943b
MT
385 INFO(db->ctx, "Opened database in %.4fms\n",
386 (double)(end - start) / CLOCKS_PER_SEC * 1000);
02879100
MT
387
388 return 0;
389}
390
c182393f 391LOC_EXPORT int loc_database_new(struct loc_ctx* ctx, struct loc_database** database, FILE* f) {
a7431f1a
MT
392 // Fail on invalid file handle
393 if (!f)
394 return -EINVAL;
395
c182393f
MT
396 struct loc_database* db = calloc(1, sizeof(*db));
397 if (!db)
398 return -ENOMEM;
399
400 // Reference context
401 db->ctx = loc_ref(ctx);
402 db->refcount = 1;
403
404 DEBUG(db->ctx, "Database object allocated at %p\n", db);
405
a7431f1a 406 int r = loc_database_read(db, f);
02879100
MT
407 if (r) {
408 loc_database_unref(db);
2601e83e 409 return r;
02879100 410 }
2601e83e 411
c182393f
MT
412 *database = db;
413
2601e83e 414 return 0;
2601e83e
MT
415}
416
c182393f
MT
417LOC_EXPORT struct loc_database* loc_database_ref(struct loc_database* db) {
418 db->refcount++;
419
420 return db;
8f5b676a
MT
421}
422
c182393f 423static void loc_database_free(struct loc_database* db) {
f10ebc2d
MT
424 int r;
425
c182393f 426 DEBUG(db->ctx, "Releasing database %p\n", db);
c34e76f1 427
c182393f 428 // Removing all ASes
b904896a
MT
429 if (db->as_v1) {
430 r = munmap(db->as_v1, db->as_count * sizeof(*db->as_v1));
c182393f
MT
431 if (r)
432 ERROR(db->ctx, "Could not unmap AS section: %s\n", strerror(errno));
433 }
c34e76f1 434
f10ebc2d 435 // Remove mapped network sections
b904896a
MT
436 if (db->networks_v1) {
437 r = munmap(db->networks_v1, db->networks_count * sizeof(*db->networks_v1));
f10ebc2d
MT
438 if (r)
439 ERROR(db->ctx, "Could not unmap networks section: %s\n", strerror(errno));
440 }
441
442 // Remove mapped network nodes section
b904896a
MT
443 if (db->network_nodes_v1) {
444 r = munmap(db->network_nodes_v1, db->network_nodes_count * sizeof(*db->network_nodes_v1));
f10ebc2d
MT
445 if (r)
446 ERROR(db->ctx, "Could not unmap network nodes section: %s\n", strerror(errno));
447 }
448
414d8958
MT
449 if (db->pool)
450 loc_stringpool_unref(db->pool);
c34e76f1 451
726f9984 452 // Free signature
5ce881d4
MT
453 if (db->signature1)
454 free(db->signature1);
455 if (db->signature2)
456 free(db->signature2);
726f9984 457
b1720435
MT
458 // Close database file
459 if (db->f)
460 fclose(db->f);
461
c182393f
MT
462 loc_unref(db->ctx);
463 free(db);
c34e76f1
MT
464}
465
c182393f
MT
466LOC_EXPORT struct loc_database* loc_database_unref(struct loc_database* db) {
467 if (--db->refcount > 0)
468 return NULL;
78ace4ed 469
c182393f
MT
470 loc_database_free(db);
471 return NULL;
472}
78ace4ed 473
726f9984
MT
474LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
475 // Cannot do this when no signature is available
5ce881d4 476 if (!db->signature1 && !db->signature2) {
726f9984
MT
477 DEBUG(db->ctx, "No signature available to verify\n");
478 return 1;
479 }
480
c81205a5
MT
481 // Start the stopwatch
482 clock_t start = clock();
483
726f9984
MT
484 // Load public key
485 EVP_PKEY* pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL);
486 if (!pkey) {
487 char* error = ERR_error_string(ERR_get_error(), NULL);
488 ERROR(db->ctx, "Could not parse public key: %s\n", error);
489
490 return -1;
491 }
492
b1720435
MT
493 int r = 0;
494
495 EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
496
b1720435 497 // Initialise hash function
e7f4b2ce
MT
498 r = EVP_DigestVerifyInit(mdctx, NULL, NULL, NULL, pkey);
499 if (r != 1) {
500 ERROR(db->ctx, "Error initializing signature validation: %s\n",
501 ERR_error_string(ERR_get_error(), NULL));
502 r = 1;
503
504 goto CLEANUP;
505 }
b1720435
MT
506
507 // Reset file to start
508 rewind(db->f);
509
510 // Read magic
511 struct loc_database_magic magic;
512 fread(&magic, 1, sizeof(magic), db->f);
513
a0cff45d
MT
514 hexdump(db->ctx, &magic, sizeof(magic));
515
b1720435 516 // Feed magic into the hash
e7f4b2ce
MT
517 r = EVP_DigestVerifyUpdate(mdctx, &magic, sizeof(magic));
518 if (r != 1) {
519 ERROR(db->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
520 r = 1;
521
522 goto CLEANUP;
523 }
b1720435
MT
524
525 // Read the header
b904896a 526 struct loc_database_header_v1 header_v1;
54f0649f 527 size_t bytes_read;
b1720435
MT
528
529 switch (db->version) {
22c7b98b 530 case LOC_DATABASE_VERSION_1:
54f0649f
MT
531 bytes_read = fread(&header_v1, 1, sizeof(header_v1), db->f);
532 if (bytes_read < sizeof(header_v1)) {
533 ERROR(db->ctx, "Could not read header\n");
534 r = 1;
535
536 goto CLEANUP;
537 }
b1720435 538
5ce881d4
MT
539 // Clear signatures
540 memset(header_v1.signature1, '\0', sizeof(header_v1.signature1));
541 header_v1.signature1_length = 0;
542 memset(header_v1.signature2, '\0', sizeof(header_v1.signature2));
543 header_v1.signature2_length = 0;
b1720435 544
b904896a 545 hexdump(db->ctx, &header_v1, sizeof(header_v1));
a0cff45d 546
b1720435 547 // Feed header into the hash
b904896a 548 r = EVP_DigestVerifyUpdate(mdctx, &header_v1, sizeof(header_v1));
e7f4b2ce
MT
549 if (r != 1) {
550 ERROR(db->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
551 r = 1;
552
553 goto CLEANUP;
554 }
b1720435
MT
555 break;
556
557 default:
558 ERROR(db->ctx, "Cannot compute hash for database with format %d\n",
559 db->version);
560 r = -EINVAL;
561 goto CLEANUP;
562 }
563
726f9984
MT
564 // Walk through the file in chunks of 64kB
565 char buffer[64 * 1024];
b1720435
MT
566
567 while (!feof(db->f)) {
54f0649f 568 bytes_read = fread(buffer, 1, sizeof(buffer), db->f);
b1720435 569
a0cff45d
MT
570 hexdump(db->ctx, buffer, bytes_read);
571
e7f4b2ce
MT
572 r = EVP_DigestVerifyUpdate(mdctx, buffer, bytes_read);
573 if (r != 1) {
574 ERROR(db->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
575 r = 1;
576
577 goto CLEANUP;
578 }
b1720435
MT
579 }
580
5ce881d4
MT
581 // Check first signature
582 if (db->signature1) {
583 hexdump(db->ctx, db->signature1, db->signature1_length);
726f9984 584
5ce881d4
MT
585 r = EVP_DigestVerifyFinal(mdctx,
586 (unsigned char*)db->signature1, db->signature1_length);
587
588 if (r == 0) {
589 DEBUG(db->ctx, "The first signature is invalid\n");
590 r = 1;
591 } else if (r == 1) {
592 DEBUG(db->ctx, "The first signature is valid\n");
593 r = 0;
594 } else {
595 ERROR(db->ctx, "Error verifying the first signature: %s\n",
596 ERR_error_string(ERR_get_error(), NULL));
597 r = -1;
598 }
b1720435
MT
599 }
600
5ce881d4
MT
601 // Check second signature only when the first one was invalid
602 if (r && db->signature2) {
603 hexdump(db->ctx, db->signature2, db->signature2_length);
604
605 r = EVP_DigestVerifyFinal(mdctx,
606 (unsigned char*)db->signature2, db->signature2_length);
607
608 if (r == 0) {
609 DEBUG(db->ctx, "The second signature is invalid\n");
610 r = 1;
611 } else if (r == 1) {
612 DEBUG(db->ctx, "The second signature is valid\n");
613 r = 0;
614 } else {
615 ERROR(db->ctx, "Error verifying the second signature: %s\n",
616 ERR_error_string(ERR_get_error(), NULL));
617 r = -1;
618 }
619 }
257626f5 620
c81205a5 621 clock_t end = clock();
6661692f 622 INFO(db->ctx, "Signature checked in %.4fms\n",
c81205a5
MT
623 (double)(end - start) / CLOCKS_PER_SEC * 1000);
624
b1720435
MT
625CLEANUP:
626 // Cleanup
627 EVP_MD_CTX_free(mdctx);
726f9984 628 EVP_PKEY_free(pkey);
b1720435
MT
629
630 return r;
631}
632
c182393f
MT
633LOC_EXPORT time_t loc_database_created_at(struct loc_database* db) {
634 return db->created_at;
635}
78ace4ed 636
c182393f
MT
637LOC_EXPORT const char* loc_database_get_vendor(struct loc_database* db) {
638 return loc_stringpool_get(db->pool, db->vendor);
639}
78ace4ed 640
c182393f
MT
641LOC_EXPORT const char* loc_database_get_description(struct loc_database* db) {
642 return loc_stringpool_get(db->pool, db->description);
643}
78ace4ed 644
4bf49d00
MT
645LOC_EXPORT const char* loc_database_get_license(struct loc_database* db) {
646 return loc_stringpool_get(db->pool, db->license);
647}
648
c182393f
MT
649LOC_EXPORT size_t loc_database_count_as(struct loc_database* db) {
650 return db->as_count;
78ace4ed
MT
651}
652
c182393f
MT
653// Returns the AS at position pos
654static int loc_database_fetch_as(struct loc_database* db, struct loc_as** as, off_t pos) {
655 if ((size_t)pos >= db->as_count)
656 return -EINVAL;
2601e83e 657
5c57de03 658 DEBUG(db->ctx, "Fetching AS at position %jd\n", (intmax_t)pos);
2601e83e
MT
659
660 int r;
c182393f 661 switch (db->version) {
22c7b98b 662 case LOC_DATABASE_VERSION_1:
b904896a 663 r = loc_as_new_from_database_v1(db->ctx, db->pool, as, db->as_v1 + pos);
c182393f 664 break;
2601e83e 665
c182393f
MT
666 default:
667 return -1;
668 }
2601e83e 669
c182393f
MT
670 if (r == 0) {
671 DEBUG(db->ctx, "Got AS%u\n", loc_as_get_number(*as));
2601e83e 672 }
2601e83e 673
c182393f
MT
674 return r;
675}
c34e76f1 676
c182393f
MT
677// Performs a binary search to find the AS in the list
678LOC_EXPORT int loc_database_get_as(struct loc_database* db, struct loc_as** as, uint32_t number) {
679 off_t lo = 0;
680 off_t hi = db->as_count - 1;
c34e76f1 681
6661692f 682#ifdef ENABLE_DEBUG
8f3e2a06
MT
683 // Save start time
684 clock_t start = clock();
6661692f 685#endif
8f3e2a06 686
c182393f
MT
687 while (lo <= hi) {
688 off_t i = (lo + hi) / 2;
8f5b676a 689
c182393f
MT
690 // Fetch AS in the middle between lo and hi
691 int r = loc_database_fetch_as(db, as, i);
692 if (r)
693 return r;
a5db3e49 694
c182393f
MT
695 // Check if this is a match
696 uint32_t as_number = loc_as_get_number(*as);
8f3e2a06 697 if (as_number == number) {
6661692f 698#ifdef ENABLE_DEBUG
8f3e2a06
MT
699 clock_t end = clock();
700
701 // Log how fast this has been
e16c943b
MT
702 DEBUG(db->ctx, "Found AS%u in %.4fms\n", as_number,
703 (double)(end - start) / CLOCKS_PER_SEC * 1000);
6661692f 704#endif
8f3e2a06 705
c182393f 706 return 0;
8f3e2a06 707 }
c182393f
MT
708
709 // If it wasn't, we release the AS and
710 // adjust our search pointers
711 loc_as_unref(*as);
712
713 if (as_number < number) {
714 lo = i + 1;
715 } else
716 hi = i - 1;
717 }
2601e83e 718
c182393f
MT
719 // Nothing found
720 *as = NULL;
2601e83e 721
8f3e2a06 722 return 1;
2601e83e 723}
10778041
MT
724
725// Returns the network at position pos
39a55353
MT
726static int loc_database_fetch_network(struct loc_database* db, struct loc_network** network,
727 struct in6_addr* address, unsigned int prefix, off_t pos) {
9b9e5faf
MT
728 if ((size_t)pos >= db->networks_count) {
729 DEBUG(db->ctx, "Network ID out of range: %jd/%jd\n",
730 (intmax_t)pos, (intmax_t)db->networks_count);
10778041 731 return -EINVAL;
9b9e5faf
MT
732 }
733
10778041 734
5c57de03 735 DEBUG(db->ctx, "Fetching network at position %jd\n", (intmax_t)pos);
10778041
MT
736
737 int r;
738 switch (db->version) {
22c7b98b 739 case LOC_DATABASE_VERSION_1:
b904896a
MT
740 r = loc_network_new_from_database_v1(db->ctx, network,
741 address, prefix, db->networks_v1 + pos);
10778041
MT
742 break;
743
744 default:
745 return -1;
746 }
747
6661692f 748#ifdef ENABLE_DEBUG
10778041
MT
749 if (r == 0) {
750 char* string = loc_network_str(*network);
751 DEBUG(db->ctx, "Got network %s\n", string);
752 free(string);
753 }
6661692f 754#endif
10778041
MT
755
756 return r;
757}
2a30e4de 758
b904896a 759static int __loc_database_node_is_leaf(const struct loc_database_network_node_v1* node) {
39a55353 760 return (node->network != htobe32(0xffffffff));
025ef489
MT
761}
762
763static int __loc_database_lookup_handle_leaf(struct loc_database* db, const struct in6_addr* address,
39a55353 764 struct loc_network** network, struct in6_addr* network_address, unsigned int prefix,
b904896a 765 const struct loc_database_network_node_v1* node) {
39a55353
MT
766 off_t network_index = be32toh(node->network);
767
b904896a 768 DEBUG(db->ctx, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node - db->network_nodes_v1), (intmax_t)network_index);
2a30e4de
MT
769
770 // Fetch the network
771 int r = loc_database_fetch_network(db, network,
39a55353 772 network_address, prefix, network_index);
e85e2b0b 773 if (r) {
5c57de03 774 ERROR(db->ctx, "Could not fetch network %jd from database\n", (intmax_t)network_index);
2a30e4de 775 return r;
e85e2b0b 776 }
39a55353 777
2a30e4de 778 // Check if the given IP address is inside the network
fc692a58 779 if (!loc_network_match_address(*network, address)) {
2a30e4de
MT
780 DEBUG(db->ctx, "Searched address is not part of the network\n");
781
782 loc_network_unref(*network);
783 *network = NULL;
784 return 1;
785 }
786
787 // A network was found and the IP address matches
788 return 0;
789}
790
2a30e4de
MT
791// Searches for an exact match along the path
792static int __loc_database_lookup(struct loc_database* db, const struct in6_addr* address,
793 struct loc_network** network, struct in6_addr* network_address,
b904896a 794 const struct loc_database_network_node_v1* node, unsigned int level) {
025ef489 795 int r;
2a30e4de
MT
796 off_t node_index;
797
798 // Follow the path
799 int bit = in6_addr_get_bit(address, level);
800 in6_addr_set_bit(network_address, level, bit);
801
802 if (bit == 0)
803 node_index = be32toh(node->zero);
804 else
805 node_index = be32toh(node->one);
806
9086d2b1
MT
807 // If the node index is zero, the tree ends here
808 // and we cannot descend any further
809 if (node_index > 0) {
810 // Check boundaries
811 if ((size_t)node_index >= db->network_nodes_count)
812 return -EINVAL;
2a30e4de 813
9086d2b1
MT
814 // Move on to the next node
815 r = __loc_database_lookup(db, address, network, network_address,
b904896a 816 db->network_nodes_v1 + node_index, level + 1);
2a30e4de 817
9086d2b1
MT
818 // End here if a result was found
819 if (r == 0)
820 return r;
2a30e4de 821
9086d2b1
MT
822 // Raise any errors
823 else if (r < 0)
824 return r;
ec1d9681
MT
825
826 DEBUG(db->ctx, "No match found below level %u\n", level);
827 } else {
828 DEBUG(db->ctx, "Tree ended at level %u\n", level);
9086d2b1 829 }
2a30e4de 830
9086d2b1
MT
831 // If this node has a leaf, we will check if it matches
832 if (__loc_database_node_is_leaf(node)) {
833 r = __loc_database_lookup_handle_leaf(db, address, network, network_address, level, node);
834 if (r <= 0)
835 return r;
836 }
2a30e4de 837
ec1d9681 838 return 1;
2a30e4de
MT
839}
840
841LOC_EXPORT int loc_database_lookup(struct loc_database* db,
842 struct in6_addr* address, struct loc_network** network) {
843 struct in6_addr network_address;
844 memset(&network_address, 0, sizeof(network_address));
845
846 *network = NULL;
847
6661692f 848#ifdef ENABLE_DEBUG
2a30e4de
MT
849 // Save start time
850 clock_t start = clock();
6661692f 851#endif
2a30e4de
MT
852
853 int r = __loc_database_lookup(db, address, network, &network_address,
b904896a 854 db->network_nodes_v1, 0);
2a30e4de 855
6661692f 856#ifdef ENABLE_DEBUG
2a30e4de
MT
857 clock_t end = clock();
858
859 // Log how fast this has been
e16c943b
MT
860 DEBUG(db->ctx, "Executed network search in %.4fms\n",
861 (double)(end - start) / CLOCKS_PER_SEC * 1000);
6661692f 862#endif
2a30e4de
MT
863
864 return r;
865}
866
867LOC_EXPORT int loc_database_lookup_from_string(struct loc_database* db,
868 const char* string, struct loc_network** network) {
869 struct in6_addr address;
870
871 int r = loc_parse_address(db->ctx, string, &address);
872 if (r)
873 return r;
874
875 return loc_database_lookup(db, &address, network);
876}
7e13db74 877
ec684c1a
MT
878// Returns the country at position pos
879static int loc_database_fetch_country(struct loc_database* db,
880 struct loc_country** country, off_t pos) {
881 if ((size_t)pos >= db->countries_count)
882 return -EINVAL;
883
2e2325a9 884 DEBUG(db->ctx, "Fetching country at position %jd\n", (intmax_t)pos);
ec684c1a
MT
885
886 int r;
887 switch (db->version) {
22c7b98b 888 case LOC_DATABASE_VERSION_1:
b904896a 889 r = loc_country_new_from_database_v1(db->ctx, db->pool, country, db->countries_v1 + pos);
ec684c1a
MT
890 break;
891
892 default:
893 return -1;
894 }
895
896 if (r == 0) {
897 DEBUG(db->ctx, "Got country %s\n", loc_country_get_code(*country));
898 }
899
900 return r;
901}
902
903// Performs a binary search to find the country in the list
904LOC_EXPORT int loc_database_get_country(struct loc_database* db,
905 struct loc_country** country, const char* code) {
906 off_t lo = 0;
907 off_t hi = db->countries_count - 1;
908
6661692f 909#ifdef ENABLE_DEBUG
ec684c1a
MT
910 // Save start time
911 clock_t start = clock();
6661692f 912#endif
ec684c1a
MT
913
914 while (lo <= hi) {
915 off_t i = (lo + hi) / 2;
916
917 // Fetch country in the middle between lo and hi
918 int r = loc_database_fetch_country(db, country, i);
919 if (r)
920 return r;
921
922 // Check if this is a match
923 const char* cc = loc_country_get_code(*country);
924 int result = strcmp(code, cc);
925
926 if (result == 0) {
6661692f 927#ifdef ENABLE_DEBUG
ec684c1a
MT
928 clock_t end = clock();
929
930 // Log how fast this has been
931 DEBUG(db->ctx, "Found country %s in %.4fms\n", cc,
932 (double)(end - start) / CLOCKS_PER_SEC * 1000);
6661692f 933#endif
ec684c1a
MT
934
935 return 0;
936 }
937
938 // If it wasn't, we release the country and
939 // adjust our search pointers
940 loc_country_unref(*country);
941
191830da 942 if (result > 0) {
ec684c1a
MT
943 lo = i + 1;
944 } else
945 hi = i - 1;
946 }
947
948 // Nothing found
949 *country = NULL;
950
951 return 1;
952}
953
7e13db74
MT
954// Enumerator
955
d87fd7a3
MT
956static void loc_database_enumerator_free(struct loc_database_enumerator* enumerator) {
957 DEBUG(enumerator->ctx, "Releasing database enumerator %p\n", enumerator);
958
959 // Release all references
960 loc_database_unref(enumerator->db);
961 loc_unref(enumerator->ctx);
962
963 if (enumerator->string)
964 free(enumerator->string);
965
5470d06c
MT
966 if (enumerator->countries)
967 loc_country_list_unref(enumerator->countries);
968
969 if (enumerator->asns)
970 loc_as_list_unref(enumerator->asns);
971
d87fd7a3
MT
972 // Free network search
973 free(enumerator->networks_visited);
974
975 // Free subnet stack
976 if (enumerator->stack)
977 loc_network_list_unref(enumerator->stack);
978
979 free(enumerator);
980}
981
ccc7ab4e 982LOC_EXPORT int loc_database_enumerator_new(struct loc_database_enumerator** enumerator,
681ff05c 983 struct loc_database* db, enum loc_database_enumerator_mode mode, int flags) {
7e13db74
MT
984 struct loc_database_enumerator* e = calloc(1, sizeof(*e));
985 if (!e)
986 return -ENOMEM;
987
988 // Reference context
989 e->ctx = loc_ref(db->ctx);
990 e->db = loc_database_ref(db);
ccc7ab4e 991 e->mode = mode;
7e13db74
MT
992 e->refcount = 1;
993
681ff05c
MT
994 // Flatten output?
995 e->flatten = (flags & LOC_DB_ENUMERATOR_FLAGS_FLATTEN);
996
e3f696c1 997 // Initialise graph search
e3f696c1
MT
998 e->network_stack_depth = 1;
999 e->networks_visited = calloc(db->network_nodes_count, sizeof(*e->networks_visited));
1000
d87fd7a3
MT
1001 // Allocate stack
1002 int r = loc_network_list_new(e->ctx, &e->stack);
1003 if (r) {
1004 loc_database_enumerator_free(e);
1005 return r;
1006 }
1007
7e13db74
MT
1008 DEBUG(e->ctx, "Database enumerator object allocated at %p\n", e);
1009
1010 *enumerator = e;
1011 return 0;
1012}
1013
1014LOC_EXPORT struct loc_database_enumerator* loc_database_enumerator_ref(struct loc_database_enumerator* enumerator) {
1015 enumerator->refcount++;
1016
1017 return enumerator;
1018}
1019
7e13db74
MT
1020LOC_EXPORT struct loc_database_enumerator* loc_database_enumerator_unref(struct loc_database_enumerator* enumerator) {
1021 if (!enumerator)
1022 return NULL;
1023
1024 if (--enumerator->refcount > 0)
1025 return enumerator;
1026
1027 loc_database_enumerator_free(enumerator);
1028 return NULL;
1029}
d3d8ede6
MT
1030
1031LOC_EXPORT int loc_database_enumerator_set_string(struct loc_database_enumerator* enumerator, const char* string) {
1032 enumerator->string = strdup(string);
1033
1034 // Make the string lowercase
1035 for (char *p = enumerator->string; *p; p++)
1036 *p = tolower(*p);
1037
1038 return 0;
1039}
1040
e646a8f3
MT
1041LOC_EXPORT struct loc_country_list* loc_database_enumerator_get_countries(
1042 struct loc_database_enumerator* enumerator) {
1043 if (!enumerator->countries)
1044 return NULL;
35bb3a32 1045
e646a8f3
MT
1046 return loc_country_list_ref(enumerator->countries);
1047}
4ef1761f 1048
e646a8f3
MT
1049LOC_EXPORT int loc_database_enumerator_set_countries(
1050 struct loc_database_enumerator* enumerator, struct loc_country_list* countries) {
1051 if (enumerator->countries)
1052 loc_country_list_unref(enumerator->countries);
57146963 1053
e646a8f3 1054 enumerator->countries = loc_country_list_ref(countries);
35bb3a32
MT
1055
1056 return 0;
1057}
1058
84a2f0c2
MT
1059LOC_EXPORT struct loc_as_list* loc_database_enumerator_get_asns(
1060 struct loc_database_enumerator* enumerator) {
1061 if (!enumerator->asns)
1062 return NULL;
1063
1064 return loc_as_list_ref(enumerator->asns);
1065}
1066
1067LOC_EXPORT int loc_database_enumerator_set_asns(
1068 struct loc_database_enumerator* enumerator, struct loc_as_list* asns) {
1069 if (enumerator->asns)
1070 loc_as_list_unref(enumerator->asns);
1071
1072 enumerator->asns = loc_as_list_ref(asns);
82910b95
MT
1073
1074 return 0;
1075}
1076
9268db5a
MT
1077LOC_EXPORT int loc_database_enumerator_set_flag(
1078 struct loc_database_enumerator* enumerator, enum loc_network_flags flag) {
1079 enumerator->flags |= flag;
1080
1081 return 0;
1082}
1083
44e5ef71
MT
1084LOC_EXPORT int loc_database_enumerator_set_family(
1085 struct loc_database_enumerator* enumerator, int family) {
1086 enumerator->family = family;
1087
1088 return 0;
1089}
1090
15f79e2d
MT
1091LOC_EXPORT int loc_database_enumerator_next_as(
1092 struct loc_database_enumerator* enumerator, struct loc_as** as) {
1093 *as = NULL;
1094
ccc7ab4e
MT
1095 // Do not do anything if not in AS mode
1096 if (enumerator->mode != LOC_DB_ENUMERATE_ASES)
15f79e2d 1097 return 0;
ccc7ab4e 1098
d3d8ede6 1099 struct loc_database* db = enumerator->db;
d3d8ede6
MT
1100
1101 while (enumerator->as_index < db->as_count) {
1102 // Fetch the next AS
15f79e2d 1103 int r = loc_database_fetch_as(db, as, enumerator->as_index++);
d3d8ede6 1104 if (r)
15f79e2d 1105 return r;
d3d8ede6 1106
15f79e2d 1107 r = loc_as_match_string(*as, enumerator->string);
273948cf 1108 if (r == 1) {
d3d8ede6 1109 DEBUG(enumerator->ctx, "AS%d (%s) matches %s\n",
15f79e2d 1110 loc_as_get_number(*as), loc_as_get_name(*as), enumerator->string);
d3d8ede6 1111
15f79e2d 1112 return 0;
d3d8ede6
MT
1113 }
1114
1115 // No match
15f79e2d 1116 loc_as_unref(*as);
74f218f0 1117 *as = NULL;
d3d8ede6
MT
1118 }
1119
1120 // Reset the index
1121 enumerator->as_index = 0;
1122
1123 // We have searched through all of them
15f79e2d 1124 return 0;
d3d8ede6 1125}
e3f696c1
MT
1126
1127static int loc_database_enumerator_stack_push_node(
1128 struct loc_database_enumerator* e, off_t offset, int i, int depth) {
1129 // Do not add empty nodes
1130 if (!offset)
1131 return 0;
1132
1133 // Check if there is any space left on the stack
1134 if (e->network_stack_depth >= MAX_STACK_DEPTH) {
1135 ERROR(e->ctx, "Maximum stack size reached: %d\n", e->network_stack_depth);
1136 return -1;
1137 }
1138
1139 // Increase stack size
1140 int s = ++e->network_stack_depth;
1141
2e2325a9 1142 DEBUG(e->ctx, "Added node %jd to stack (%d)\n", (intmax_t)offset, depth);
e3f696c1
MT
1143
1144 e->network_stack[s].offset = offset;
1145 e->network_stack[s].i = i;
1146 e->network_stack[s].depth = depth;
1147
1148 return 0;
1149}
1150
69248038
MT
1151static int loc_database_enumerator_filter_network(
1152 struct loc_database_enumerator* enumerator, struct loc_network* network) {
1153 // Skip if the family does not match
e0e96878
MT
1154 if (enumerator->family && loc_network_address_family(network) != enumerator->family) {
1155 DEBUG(enumerator->ctx, "Filtered network %p because of family not matching\n", network);
69248038 1156 return 1;
e0e96878 1157 }
69248038
MT
1158
1159 // Skip if the country code does not match
e0e96878
MT
1160 if (enumerator->countries && !loc_country_list_empty(enumerator->countries)) {
1161 const char* country_code = loc_network_get_country_code(network);
50120b99 1162
e0e96878
MT
1163 if (!loc_country_list_contains_code(enumerator->countries, country_code)) {
1164 DEBUG(enumerator->ctx, "Filtered network %p because of country code not matching\n", network);
1165 return 1;
50120b99
MT
1166 }
1167 }
69248038
MT
1168
1169 // Skip if the ASN does not match
e0e96878
MT
1170 if (enumerator->asns && !loc_as_list_empty(enumerator->asns)) {
1171 uint32_t asn = loc_network_get_asn(network);
c1a36c94 1172
e0e96878
MT
1173 if (!loc_as_list_contains_number(enumerator->asns, asn)) {
1174 DEBUG(enumerator->ctx, "Filtered network %p because of ASN not matching\n", network);
1175 return 1;
c1a36c94
MT
1176 }
1177 }
69248038
MT
1178
1179 // Skip if flags do not match
e0e96878
MT
1180 if (enumerator->flags && !loc_network_match_flag(network, enumerator->flags)) {
1181 DEBUG(enumerator->ctx, "Filtered network %p because of flags not matching\n", network);
69248038 1182 return 1;
e0e96878 1183 }
69248038
MT
1184
1185 // Do not filter
1186 return 0;
1187}
1188
d87fd7a3
MT
1189static int __loc_database_enumerator_next_network(
1190 struct loc_database_enumerator* enumerator, struct loc_network** network, int filter) {
1191 // Return top element from the stack
2113e71b 1192 while (1) {
04cbd2bf 1193 *network = loc_network_list_pop_first(enumerator->stack);
2113e71b
MT
1194
1195 // Stack is empty
1196 if (!*network)
1197 break;
1198
1199 // Throw away any networks by filter
1200 if (filter && loc_database_enumerator_filter_network(enumerator, *network)) {
1201 loc_network_unref(*network);
1202 *network = NULL;
1203 continue;
1204 }
1205
1206 // Return result
15f79e2d 1207 return 0;
2113e71b 1208 }
15f79e2d 1209
15f79e2d
MT
1210 DEBUG(enumerator->ctx, "Called with a stack of %u nodes\n",
1211 enumerator->network_stack_depth);
e3f696c1
MT
1212
1213 // Perform DFS
15f79e2d
MT
1214 while (enumerator->network_stack_depth > 0) {
1215 DEBUG(enumerator->ctx, "Stack depth: %u\n", enumerator->network_stack_depth);
e3f696c1
MT
1216
1217 // Get object from top of the stack
15f79e2d 1218 struct loc_node_stack* node = &enumerator->network_stack[enumerator->network_stack_depth];
e3f696c1
MT
1219
1220 // Remove the node from the stack if we have already visited it
15f79e2d
MT
1221 if (enumerator->networks_visited[node->offset]) {
1222 enumerator->network_stack_depth--;
e3f696c1
MT
1223 continue;
1224 }
1225
74fb733a 1226 // Mark the bits on the path correctly
15f79e2d 1227 in6_addr_set_bit(&enumerator->network_address,
e3f696c1
MT
1228 (node->depth > 0) ? node->depth - 1 : 0, node->i);
1229
2e2325a9 1230 DEBUG(enumerator->ctx, "Looking at node %jd\n", (intmax_t)node->offset);
15f79e2d 1231 enumerator->networks_visited[node->offset]++;
e3f696c1
MT
1232
1233 // Pop node from top of the stack
b904896a
MT
1234 struct loc_database_network_node_v1* n =
1235 enumerator->db->network_nodes_v1 + node->offset;
e3f696c1
MT
1236
1237 // Add edges to stack
d87fd7a3 1238 int r = loc_database_enumerator_stack_push_node(enumerator,
e3f696c1
MT
1239 be32toh(n->one), 1, node->depth + 1);
1240
1241 if (r)
1242 return r;
1243
15f79e2d 1244 r = loc_database_enumerator_stack_push_node(enumerator,
e3f696c1
MT
1245 be32toh(n->zero), 0, node->depth + 1);
1246
1247 if (r)
1248 return r;
1249
1250 // Check if this node is a leaf and has a network object
1251 if (__loc_database_node_is_leaf(n)) {
1252 off_t network_index = be32toh(n->network);
1253
2e2325a9 1254 DEBUG(enumerator->ctx, "Node has a network at %jd\n", (intmax_t)network_index);
e3f696c1
MT
1255
1256 // Fetch the network object
15f79e2d
MT
1257 r = loc_database_fetch_network(enumerator->db, network,
1258 &enumerator->network_address, node->depth, network_index);
e3f696c1
MT
1259
1260 // Break on any errors
1261 if (r)
1262 return r;
1263
d87fd7a3
MT
1264 // Return all networks when the filter is disabled
1265 if (!filter)
1266 return 0;
1267
e3f696c1 1268 // Check if we are interested in this network
69248038 1269 if (loc_database_enumerator_filter_network(enumerator, *network)) {
9268db5a
MT
1270 loc_network_unref(*network);
1271 *network = NULL;
864dd22e
MT
1272
1273 continue;
9268db5a
MT
1274 }
1275
e3f696c1
MT
1276 return 0;
1277 }
1278 }
1279
1280 // Reached the end of the search
d87fd7a3
MT
1281 return 0;
1282}
fe483cdc 1283
d87fd7a3
MT
1284static int __loc_database_enumerator_next_network_flattened(
1285 struct loc_database_enumerator* enumerator, struct loc_network** network) {
1286 // Fetch the next network
1287 int r = __loc_database_enumerator_next_network(enumerator, network, 1);
1288 if (r)
1289 return r;
e3f696c1 1290
d87fd7a3
MT
1291 // End if we could not read another network
1292 if (!*network)
1293 return 0;
1294
1295 struct loc_network* subnet = NULL;
1296 struct loc_network_list* subnets;
1297
1298 // Create a list with all subnets
1299 r = loc_network_list_new(enumerator->ctx, &subnets);
1300 if (r)
1301 return r;
1302
1303 // Search all subnets from the database
1304 while (1) {
1305 // Fetch the next network in line
1306 r = __loc_database_enumerator_next_network(enumerator, &subnet, 0);
ed0f53df
MT
1307 if (r) {
1308 loc_network_unref(subnet);
1309 loc_network_list_unref(subnets);
1310
1311 return r;
1312 }
d87fd7a3
MT
1313
1314 // End if we did not receive another subnet
1315 if (!subnet)
1316 break;
1317
1318 // Collect all subnets in a list
1319 if (loc_network_is_subnet(*network, subnet)) {
1320 r = loc_network_list_push(subnets, subnet);
ed0f53df
MT
1321 if (r) {
1322 loc_network_unref(subnet);
1323 loc_network_list_unref(subnets);
1324
1325 return r;
1326 }
d87fd7a3
MT
1327
1328 loc_network_unref(subnet);
1329 continue;
1330 }
1331
1332 // If this is not a subnet, we push it back onto the stack and break
1333 r = loc_network_list_push(enumerator->stack, subnet);
ed0f53df
MT
1334 if (r) {
1335 loc_network_unref(subnet);
1336 loc_network_list_unref(subnets);
1337
1338 return r;
1339 }
d87fd7a3
MT
1340
1341 loc_network_unref(subnet);
1342 break;
1343 }
1344
1345 DEBUG(enumerator->ctx, "Found %zu subnet(s)\n", loc_network_list_size(subnets));
1346
1347 // We can abort here if the network has no subnets
1348 if (loc_network_list_empty(subnets)) {
1349 loc_network_list_unref(subnets);
1350
1351 return 0;
1352 }
1353
1354 // If the network has any subnets, we will break it into smaller parts
1355 // without the subnets.
1356 struct loc_network_list* excluded = loc_network_exclude_list(*network, subnets);
ed0f53df
MT
1357 if (!excluded) {
1358 loc_network_list_unref(subnets);
1359 return -1;
1360 }
1361
5dacb45a
MT
1362 // Merge subnets onto the stack
1363 r = loc_network_list_merge(enumerator->stack, subnets);
ed0f53df
MT
1364 if (r) {
1365 loc_network_list_unref(subnets);
1366 loc_network_list_unref(excluded);
1367
1368 return r;
d87fd7a3
MT
1369 }
1370
5dacb45a
MT
1371 // Push excluded list onto the stack
1372 r = loc_network_list_merge(enumerator->stack, excluded);
1373 if (r) {
1374 loc_network_list_unref(subnets);
1375 loc_network_list_unref(excluded);
d87fd7a3 1376
5dacb45a
MT
1377 return r;
1378 }
d87fd7a3 1379
d87fd7a3 1380 loc_network_list_unref(subnets);
5dacb45a
MT
1381 loc_network_list_unref(excluded);
1382
6bd9cd76 1383 // Drop the network and restart the whole process again to pick the next network
5dacb45a 1384 loc_network_unref(*network);
d87fd7a3 1385
6bd9cd76 1386 return __loc_database_enumerator_next_network_flattened(enumerator, network);
d87fd7a3
MT
1387}
1388
1389LOC_EXPORT int loc_database_enumerator_next_network(
1390 struct loc_database_enumerator* enumerator, struct loc_network** network) {
1391 // Do not do anything if not in network mode
1392 if (enumerator->mode != LOC_DB_ENUMERATE_NETWORKS)
1393 return 0;
1394
1395 // Flatten output?
1396 if (enumerator->flatten)
1397 return __loc_database_enumerator_next_network_flattened(enumerator, network);
1398
1399 return __loc_database_enumerator_next_network(enumerator, network, 1);
e3f696c1 1400}
fa9a3663
MT
1401
1402LOC_EXPORT int loc_database_enumerator_next_country(
1403 struct loc_database_enumerator* enumerator, struct loc_country** country) {
1404 *country = NULL;
1405
1406 // Do not do anything if not in country mode
1407 if (enumerator->mode != LOC_DB_ENUMERATE_COUNTRIES)
1408 return 0;
1409
1410 struct loc_database* db = enumerator->db;
1411
1412 while (enumerator->country_index < db->countries_count) {
1413 // Fetch the next country
1414 int r = loc_database_fetch_country(db, country, enumerator->country_index++);
1415 if (r)
1416 return r;
1417
1418 // We do not filter here, so it always is a match
1419 return 0;
1420 }
1421
1422 // Reset the index
1423 enumerator->country_index = 0;
1424
1425 // We have searched through all of them
1426 return 0;
1427}