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 <libloc/libloc.h>
25 #include <libloc/country.h>
26 #include <libloc/database.h>
27 #include <libloc/network.h>
28 #include <libloc/writer.h>
30 int main(int argc
, char** argv
) {
31 struct loc_country
* country
;
35 // Check some valid country codes
36 if (!loc_country_code_is_valid("DE")) {
37 fprintf(stderr
, "Valid country code detected as invalid: %s\n", "DE");
41 // Check some invalid country codes
42 if (loc_country_code_is_valid("X1")) {
43 fprintf(stderr
, "Invalid country code detected as valid: %s\n", "X1");
47 // Test special country codes
48 flag
= loc_country_special_code_to_flag("XX");
50 fprintf(stderr
, "Unexpectedly received a flag for XX: %d\n", flag
);
55 flag
= loc_country_special_code_to_flag("A1");
56 if (flag
!= LOC_NETWORK_FLAG_ANONYMOUS_PROXY
) {
57 fprintf(stderr
, "Got a wrong flag for A1: %d\n", flag
);
62 flag
= loc_country_special_code_to_flag("A2");
63 if (flag
!= LOC_NETWORK_FLAG_SATELLITE_PROVIDER
) {
64 fprintf(stderr
, "Got a wrong flag for A2: %d\n", flag
);
69 flag
= loc_country_special_code_to_flag("A3");
70 if (flag
!= LOC_NETWORK_FLAG_ANYCAST
) {
71 fprintf(stderr
, "Got a wrong flag for A3: %d\n", flag
);
76 flag
= loc_country_special_code_to_flag("XD");
77 if (flag
!= LOC_NETWORK_FLAG_DROP
) {
78 fprintf(stderr
, "Got a wrong flag for XD: %d\n", flag
);
83 flag
= loc_country_special_code_to_flag(NULL
);
85 fprintf(stderr
, "loc_country_special_code_to_flag didn't throw an error for NULL\n");
94 // Enable debug logging
95 loc_set_log_priority(ctx
, LOG_DEBUG
);
98 struct loc_writer
* writer
;
99 err
= loc_writer_new(ctx
, &writer
, NULL
, NULL
);
104 err
= loc_writer_add_country(writer
, &country
, "DE");
106 fprintf(stderr
, "Could not create country\n");
110 // Set name & continent
111 loc_country_set_name(country
, "Testistan");
112 loc_country_set_continent_code(country
, "YY");
115 loc_country_unref(country
);
117 // Add another country
118 err
= loc_writer_add_country(writer
, &country
, "YY");
120 fprintf(stderr
, "Could not create country: YY\n");
123 loc_country_unref(country
);
127 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
131 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
133 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
136 loc_writer_unref(writer
);
138 // And open it again from disk
139 struct loc_database
* db
;
140 err
= loc_database_new(ctx
, &db
, f
);
142 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
146 // Lookup an address in the subnet
147 err
= loc_database_get_country(db
, &country
, "YY");
149 fprintf(stderr
, "Could not find country: YY\n");
152 loc_country_unref(country
);
154 struct loc_network
* network
= NULL
;
156 // Create a test network
157 err
= loc_network_new_from_string(ctx
, &network
, "2001:db8::/64");
159 fprintf(stderr
, "Could not create network: %m\n");
163 // Set country code & flag
164 loc_network_set_country_code(network
, "YY");
165 loc_network_set_flag(network
, LOC_NETWORK_FLAG_ANONYMOUS_PROXY
);
167 // Check if this network matches its own country code
168 err
= loc_network_match_country_code(network
, "YY");
170 fprintf(stderr
, "Network does not match its own country code\n");
174 // Check if this network matches the special country code
175 err
= loc_network_match_country_code(network
, "A1");
177 fprintf(stderr
, "Network does not match the special country code A1\n");
181 // Check if this network does not match another special country code
182 err
= loc_network_match_country_code(network
, "A2");
184 fprintf(stderr
, "Network matches another special country code A2\n");
188 loc_network_unref(network
);
190 loc_database_unref(db
);