2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2019 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.
24 #include <loc/libloc.h>
25 #include <loc/country.h>
26 #include <loc/database.h>
27 #include <loc/network.h>
28 #include <loc/writer.h>
30 int main(int argc
, char** argv
) {
31 struct loc_country
* country
;
34 // Check some valid country codes
35 if (!loc_country_code_is_valid("XX")) {
36 fprintf(stderr
, "Valid country code detected as invalid: %s\n", "XX");
40 // Check some invalid country codes
41 if (loc_country_code_is_valid("X1")) {
42 fprintf(stderr
, "Invalid country code detected as valid: %s\n", "X1");
51 // Enable debug logging
52 loc_set_log_priority(ctx
, LOG_DEBUG
);
55 struct loc_writer
* writer
;
56 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
61 err
= loc_writer_add_country(writer
, &country
, "XX");
63 fprintf(stderr
, "Could not create country\n");
67 // Set name & continent
68 loc_country_set_name(country
, "Testistan");
69 loc_country_set_continent_code(country
, "YY");
72 loc_country_unref(country
);
74 // Add another country
75 err
= loc_writer_add_country(writer
, &country
, "YY");
77 fprintf(stderr
, "Could not create country: YY\n");
80 loc_country_unref(country
);
84 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
88 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
90 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
93 loc_writer_unref(writer
);
95 // And open it again from disk
96 struct loc_database
* db
;
97 err
= loc_database_new(ctx
, &db
, f
);
99 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
103 // Lookup an address in the subnet
104 err
= loc_database_get_country(db
, &country
, "YY");
106 fprintf(stderr
, "Could not find country: YY\n");
109 loc_country_unref(country
);
111 loc_database_unref(db
);