2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
22 #include <libloc/libloc.h>
23 #include <libloc/database.h>
24 #include <libloc/writer.h>
26 #define TEST_AS_COUNT 5000
28 int main(int argc
, char** argv
) {
36 // Enable debug logging
37 loc_set_log_priority(ctx
, LOG_DEBUG
);
40 struct loc_writer
* writer
;
41 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
46 for (unsigned int i
= 1; i
<= TEST_AS_COUNT
; i
++) {
48 loc_writer_add_as(writer
, &as
, i
);
50 sprintf(name
, "Test AS%u", i
);
51 loc_as_set_name(as
, name
);
58 fprintf(stderr
, "Could not open file for writing: %m\n");
62 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
64 fprintf(stderr
, "Could not write database: %m\n");
68 loc_writer_unref(writer
);
70 // And open it again from disk
71 struct loc_database
* db
;
72 err
= loc_database_new(ctx
, &db
, f
);
74 fprintf(stderr
, "Could not open database: %m\n");
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");
85 for (unsigned int i
= 1; i
<= 10; i
++) {
86 err
= loc_database_get_as(db
, &as
, i
);
88 fprintf(stderr
, "Could not find AS%d\n", i
);
97 struct loc_database_enumerator
* enumerator
;
98 err
= loc_database_enumerator_new(&enumerator
, db
, LOC_DB_ENUMERATE_ASES
, 0);
100 fprintf(stderr
, "Could not create a database enumerator\n");
104 loc_database_enumerator_set_string(enumerator
, "10");
106 err
= loc_database_enumerator_next_as(enumerator
, &as
);
108 fprintf(stderr
, "Could not enumerate next AS\n");
113 printf("Found AS%d: %s\n", loc_as_get_number(as
), loc_as_get_name(as
));
115 err
= loc_database_enumerator_next_as(enumerator
, &as
);
117 fprintf(stderr
, "Could not enumerate next AS\n");
122 loc_database_enumerator_unref(enumerator
);
123 loc_database_unref(db
);