]>
Commit | Line | Data |
---|---|---|
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> | |
20 | #include <syslog.h> | |
21 | ||
22 | #include <libloc/libloc.h> | |
23 | #include <libloc/database.h> | |
24 | #include <libloc/writer.h> | |
25 | ||
26 | #define TEST_AS_COUNT 5000 | |
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 | ||
36 | // Enable debug logging | |
37 | loc_set_log_priority(ctx, LOG_DEBUG); | |
38 | ||
39 | // Create a database | |
40 | struct loc_writer* writer; | |
41 | err = loc_writer_new(ctx, &writer, NULL, NULL); | |
42 | if (err < 0) | |
43 | exit(EXIT_FAILURE); | |
44 | ||
45 | char name[256]; | |
46 | for (unsigned int i = 1; i <= TEST_AS_COUNT; i++) { | |
47 | struct loc_as* as; | |
48 | loc_writer_add_as(writer, &as, i); | |
49 | ||
50 | sprintf(name, "Test AS%u", i); | |
51 | loc_as_set_name(as, name); | |
52 | ||
53 | loc_as_unref(as); | |
54 | } | |
55 | ||
56 | FILE* f = tmpfile(); | |
57 | if (!f) { | |
58 | fprintf(stderr, "Could not open file for writing: %m\n"); | |
59 | exit(EXIT_FAILURE); | |
60 | } | |
61 | ||
62 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); | |
63 | if (err) { | |
64 | fprintf(stderr, "Could not write database: %m\n"); | |
65 | exit(EXIT_FAILURE); | |
66 | } | |
67 | ||
68 | loc_writer_unref(writer); | |
69 | ||
70 | // And open it again from disk | |
71 | struct loc_database* db; | |
72 | err = loc_database_new(ctx, &db, f); | |
73 | if (err) { | |
74 | fprintf(stderr, "Could not open database: %m\n"); | |
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 | ||
84 | struct loc_as* as; | |
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); | |
93 | } | |
94 | ||
95 | // Enumerator | |
96 | ||
97 | struct loc_database_enumerator* enumerator; | |
98 | err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_ASES, 0); | |
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 | ||
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 | ||
112 | while (as) { | |
113 | printf("Found AS%d: %s\n", loc_as_get_number(as), loc_as_get_name(as)); | |
114 | ||
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 | } | |
120 | } | |
121 | ||
122 | loc_database_enumerator_unref(enumerator); | |
123 | loc_database_unref(db); | |
124 | loc_unref(ctx); | |
125 | fclose(f); | |
126 | ||
127 | return EXIT_SUCCESS; | |
128 | } |