]>
Commit | Line | Data |
---|---|---|
a5db3e49 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 | ||
17 | #include <errno.h> | |
18 | #include <stdlib.h> | |
19 | #include <string.h> | |
37317e4f | 20 | #include <syslog.h> |
a5db3e49 MT |
21 | |
22 | #include <loc/libloc.h> | |
9fc7f001 | 23 | #include <loc/database.h> |
c182393f | 24 | #include <loc/writer.h> |
a5db3e49 | 25 | |
8f3e2a06 | 26 | #define TEST_AS_COUNT 5000 |
a5db3e49 MT |
27 | |
28 | int main(int argc, char** argv) { | |
29 | int err; | |
30 | ||
31 | struct loc_ctx* ctx; | |
32 | err = loc_new(&ctx); | |
33 | if (err < 0) | |
34 | exit(EXIT_FAILURE); | |
35 | ||
37317e4f MT |
36 | // Enable debug logging |
37 | loc_set_log_priority(ctx, LOG_DEBUG); | |
38 | ||
a5db3e49 | 39 | // Create a database |
c182393f | 40 | struct loc_writer* writer; |
5ce881d4 | 41 | err = loc_writer_new(ctx, &writer, NULL, NULL); |
a5db3e49 MT |
42 | if (err < 0) |
43 | exit(EXIT_FAILURE); | |
44 | ||
45 | char name[256]; | |
46 | for (unsigned int i = 1; i <= TEST_AS_COUNT; i++) { | |
c182393f | 47 | struct loc_as* as; |
0e974d4b | 48 | loc_writer_add_as(writer, &as, i); |
a5db3e49 MT |
49 | |
50 | sprintf(name, "Test AS%u", i); | |
51 | loc_as_set_name(as, name); | |
52 | ||
53 | loc_as_unref(as); | |
54 | } | |
55 | ||
6254dca6 | 56 | FILE* f = tmpfile(); |
a5db3e49 MT |
57 | if (!f) { |
58 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
59 | exit(EXIT_FAILURE); | |
60 | } | |
61 | ||
22c7b98b | 62 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); |
a5db3e49 MT |
63 | if (err) { |
64 | fprintf(stderr, "Could not write database: %s\n", strerror(-err)); | |
65 | exit(EXIT_FAILURE); | |
66 | } | |
a5db3e49 | 67 | |
c182393f | 68 | loc_writer_unref(writer); |
a5db3e49 MT |
69 | |
70 | // And open it again from disk | |
c182393f MT |
71 | struct loc_database* db; |
72 | err = loc_database_new(ctx, &db, f); | |
a5db3e49 MT |
73 | if (err) { |
74 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
75 | exit(EXIT_FAILURE); | |
76 | } | |
77 | ||
78 | size_t as_count = loc_database_count_as(db); | |
79 | if (as_count != TEST_AS_COUNT) { | |
80 | fprintf(stderr, "Could not read all ASes\n"); | |
81 | exit(EXIT_FAILURE); | |
82 | } | |
83 | ||
c182393f | 84 | struct loc_as* as; |
8f3e2a06 MT |
85 | for (unsigned int i = 1; i <= 10; i++) { |
86 | err = loc_database_get_as(db, &as, i); | |
87 | if (err) { | |
88 | fprintf(stderr, "Could not find AS%d\n", i); | |
89 | exit(EXIT_FAILURE); | |
90 | } | |
91 | ||
92 | loc_as_unref(as); | |
c182393f MT |
93 | } |
94 | ||
d3d8ede6 MT |
95 | // Enumerator |
96 | ||
97 | struct loc_database_enumerator* enumerator; | |
ccc7ab4e | 98 | err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_ASES); |
d3d8ede6 MT |
99 | if (err) { |
100 | fprintf(stderr, "Could not create a database enumerator\n"); | |
101 | exit(EXIT_FAILURE); | |
102 | } | |
103 | ||
104 | loc_database_enumerator_set_string(enumerator, "10"); | |
105 | ||
8b6197b8 MT |
106 | err = loc_database_enumerator_next_as(enumerator, &as); |
107 | if (err) { | |
108 | fprintf(stderr, "Could not enumerate next AS\n"); | |
109 | exit(EXIT_FAILURE); | |
110 | } | |
111 | ||
d3d8ede6 MT |
112 | while (as) { |
113 | printf("Found AS%d: %s\n", loc_as_get_number(as), loc_as_get_name(as)); | |
114 | ||
8b6197b8 MT |
115 | err = loc_database_enumerator_next_as(enumerator, &as); |
116 | if (err) { | |
117 | fprintf(stderr, "Could not enumerate next AS\n"); | |
118 | exit(EXIT_FAILURE); | |
119 | } | |
d3d8ede6 MT |
120 | } |
121 | ||
122 | loc_database_enumerator_unref(enumerator); | |
a5db3e49 MT |
123 | loc_database_unref(db); |
124 | loc_unref(ctx); | |
6254dca6 | 125 | fclose(f); |
a5db3e49 MT |
126 | |
127 | return EXIT_SUCCESS; | |
128 | } |