]>
Commit | Line | Data |
---|---|---|
ec684c1a MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2019 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 <stdio.h> | |
19 | #include <stddef.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
37317e4f | 22 | #include <syslog.h> |
ec684c1a | 23 | |
c12a1385 MT |
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> | |
ec684c1a MT |
29 | |
30 | int main(int argc, char** argv) { | |
31 | struct loc_country* country; | |
3eb1eed6 | 32 | int flag; |
ec684c1a MT |
33 | int err; |
34 | ||
57146963 | 35 | // Check some valid country codes |
e8ebd079 MT |
36 | if (!loc_country_code_is_valid("DE")) { |
37 | fprintf(stderr, "Valid country code detected as invalid: %s\n", "DE"); | |
57146963 MT |
38 | exit(EXIT_FAILURE); |
39 | } | |
40 | ||
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"); | |
44 | exit(EXIT_FAILURE); | |
45 | } | |
46 | ||
3eb1eed6 MT |
47 | // Test special country codes |
48 | flag = loc_country_special_code_to_flag("XX"); | |
49 | if (flag) { | |
50 | fprintf(stderr, "Unexpectedly received a flag for XX: %d\n", flag); | |
51 | exit(EXIT_FAILURE); | |
52 | } | |
53 | ||
54 | // A1 | |
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); | |
58 | exit(EXIT_FAILURE); | |
59 | } | |
60 | ||
61 | // A2 | |
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); | |
65 | exit(EXIT_FAILURE); | |
66 | } | |
67 | ||
68 | // A3 | |
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); | |
72 | exit(EXIT_FAILURE); | |
73 | } | |
74 | ||
75 | // XD | |
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); | |
79 | exit(EXIT_FAILURE); | |
80 | } | |
81 | ||
82 | // NULL input | |
83 | flag = loc_country_special_code_to_flag(NULL); | |
84 | if (flag >= 0) { | |
85 | fprintf(stderr, "loc_country_special_code_to_flag didn't throw an error for NULL\n"); | |
86 | exit(EXIT_FAILURE); | |
87 | } | |
88 | ||
ec684c1a MT |
89 | struct loc_ctx* ctx; |
90 | err = loc_new(&ctx); | |
91 | if (err < 0) | |
92 | exit(EXIT_FAILURE); | |
93 | ||
37317e4f MT |
94 | // Enable debug logging |
95 | loc_set_log_priority(ctx, LOG_DEBUG); | |
96 | ||
ec684c1a MT |
97 | // Create a database |
98 | struct loc_writer* writer; | |
5ce881d4 | 99 | err = loc_writer_new(ctx, &writer, NULL, NULL); |
ec684c1a MT |
100 | if (err < 0) |
101 | exit(EXIT_FAILURE); | |
102 | ||
103 | // Create a country | |
e8ebd079 | 104 | err = loc_writer_add_country(writer, &country, "DE"); |
ec684c1a MT |
105 | if (err) { |
106 | fprintf(stderr, "Could not create country\n"); | |
107 | exit(EXIT_FAILURE); | |
108 | } | |
109 | ||
110 | // Set name & continent | |
111 | loc_country_set_name(country, "Testistan"); | |
57146963 | 112 | loc_country_set_continent_code(country, "YY"); |
ec684c1a MT |
113 | |
114 | // Free country | |
115 | loc_country_unref(country); | |
116 | ||
8bbddd9a MT |
117 | // Add another country |
118 | err = loc_writer_add_country(writer, &country, "YY"); | |
119 | if (err) { | |
120 | fprintf(stderr, "Could not create country: YY\n"); | |
121 | exit(EXIT_FAILURE); | |
122 | } | |
123 | loc_country_unref(country); | |
124 | ||
6254dca6 | 125 | FILE* f = tmpfile(); |
ec684c1a | 126 | if (!f) { |
6cd02f1d | 127 | fprintf(stderr, "Could not open file for writing: %m\n"); |
ec684c1a MT |
128 | exit(EXIT_FAILURE); |
129 | } | |
130 | ||
22c7b98b | 131 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); |
ec684c1a | 132 | if (err) { |
198e382c | 133 | fprintf(stderr, "Could not write database: %m\n"); |
ec684c1a MT |
134 | exit(EXIT_FAILURE); |
135 | } | |
ec684c1a MT |
136 | loc_writer_unref(writer); |
137 | ||
138 | // And open it again from disk | |
ec684c1a MT |
139 | struct loc_database* db; |
140 | err = loc_database_new(ctx, &db, f); | |
141 | if (err) { | |
198e382c | 142 | fprintf(stderr, "Could not open database: %m\n"); |
ec684c1a MT |
143 | exit(EXIT_FAILURE); |
144 | } | |
145 | ||
146 | // Lookup an address in the subnet | |
8bbddd9a | 147 | err = loc_database_get_country(db, &country, "YY"); |
766577bb | 148 | if (err || !country) { |
8bbddd9a | 149 | fprintf(stderr, "Could not find country: YY\n"); |
ec684c1a MT |
150 | exit(EXIT_FAILURE); |
151 | } | |
152 | loc_country_unref(country); | |
153 | ||
0b27a567 MT |
154 | struct loc_network* network = NULL; |
155 | ||
156 | // Create a test network | |
157 | err = loc_network_new_from_string(ctx, &network, "2001:db8::/64"); | |
158 | if (err) { | |
159 | fprintf(stderr, "Could not create network: %m\n"); | |
160 | exit(EXIT_FAILURE); | |
161 | } | |
162 | ||
163 | // Set country code & flag | |
164 | loc_network_set_country_code(network, "YY"); | |
165 | loc_network_set_flag(network, LOC_NETWORK_FLAG_ANONYMOUS_PROXY); | |
166 | ||
167 | // Check if this network matches its own country code | |
0258d3c9 | 168 | err = loc_network_matches_country_code(network, "YY"); |
0b27a567 MT |
169 | if (!err) { |
170 | fprintf(stderr, "Network does not match its own country code\n"); | |
171 | exit(EXIT_FAILURE); | |
172 | } | |
173 | ||
174 | // Check if this network matches the special country code | |
0258d3c9 | 175 | err = loc_network_matches_country_code(network, "A1"); |
0b27a567 MT |
176 | if (!err) { |
177 | fprintf(stderr, "Network does not match the special country code A1\n"); | |
178 | exit(EXIT_FAILURE); | |
179 | } | |
180 | ||
181 | // Check if this network does not match another special country code | |
0258d3c9 | 182 | err = loc_network_matches_country_code(network, "A2"); |
0b27a567 MT |
183 | if (err) { |
184 | fprintf(stderr, "Network matches another special country code A2\n"); | |
185 | exit(EXIT_FAILURE); | |
186 | } | |
187 | ||
188 | loc_network_unref(network); | |
189 | ||
ec684c1a MT |
190 | loc_database_unref(db); |
191 | loc_unref(ctx); | |
6254dca6 | 192 | fclose(f); |
ec684c1a MT |
193 | |
194 | return EXIT_SUCCESS; | |
195 | } |