diff options
Diffstat (limited to 'src/nat-auto/gnunet-nat-auto.c')
-rw-r--r-- | src/nat-auto/gnunet-nat-auto.c | 288 |
1 files changed, 146 insertions, 142 deletions
diff --git a/src/nat-auto/gnunet-nat-auto.c b/src/nat-auto/gnunet-nat-auto.c index 106d60228..87850509d 100644 --- a/src/nat-auto/gnunet-nat-auto.c +++ b/src/nat-auto/gnunet-nat-auto.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file src/nat/gnunet-nat-auto.c | 22 | * @file src/nat/gnunet-nat-auto.c |
@@ -90,13 +90,13 @@ static uint8_t proto; | |||
90 | * terminate. | 90 | * terminate. |
91 | */ | 91 | */ |
92 | static void | 92 | static void |
93 | test_finished () | 93 | test_finished() |
94 | { | 94 | { |
95 | if (NULL != ah) | 95 | if (NULL != ah) |
96 | return; | 96 | return; |
97 | if (NULL != nt) | 97 | if (NULL != nt) |
98 | return; | 98 | return; |
99 | GNUNET_SCHEDULER_shutdown (); | 99 | GNUNET_SCHEDULER_shutdown(); |
100 | } | 100 | } |
101 | 101 | ||
102 | 102 | ||
@@ -109,16 +109,16 @@ test_finished () | |||
109 | * @param value value of the option | 109 | * @param value value of the option |
110 | */ | 110 | */ |
111 | static void | 111 | static void |
112 | auto_conf_iter (void *cls, | 112 | auto_conf_iter(void *cls, |
113 | const char *section, | 113 | const char *section, |
114 | const char *option, | 114 | const char *option, |
115 | const char *value) | 115 | const char *value) |
116 | { | 116 | { |
117 | struct GNUNET_CONFIGURATION_Handle *new_cfg = cls; | 117 | struct GNUNET_CONFIGURATION_Handle *new_cfg = cls; |
118 | 118 | ||
119 | printf ("%s: %s\n", option, value); | 119 | printf("%s: %s\n", option, value); |
120 | if (NULL != new_cfg) | 120 | if (NULL != new_cfg) |
121 | GNUNET_CONFIGURATION_set_value_string (new_cfg, section, option, value); | 121 | GNUNET_CONFIGURATION_set_value_string(new_cfg, section, option, value); |
122 | } | 122 | } |
123 | 123 | ||
124 | 124 | ||
@@ -132,10 +132,10 @@ auto_conf_iter (void *cls, | |||
132 | * @param type what the situation of the NAT | 132 | * @param type what the situation of the NAT |
133 | */ | 133 | */ |
134 | static void | 134 | static void |
135 | auto_config_cb (void *cls, | 135 | auto_config_cb(void *cls, |
136 | const struct GNUNET_CONFIGURATION_Handle *diff, | 136 | const struct GNUNET_CONFIGURATION_Handle *diff, |
137 | enum GNUNET_NAT_StatusCode result, | 137 | enum GNUNET_NAT_StatusCode result, |
138 | enum GNUNET_NAT_Type type) | 138 | enum GNUNET_NAT_Type type) |
139 | { | 139 | { |
140 | const char *nat_type; | 140 | const char *nat_type; |
141 | char unknown_type[64]; | 141 | char unknown_type[64]; |
@@ -143,80 +143,84 @@ auto_config_cb (void *cls, | |||
143 | 143 | ||
144 | ah = NULL; | 144 | ah = NULL; |
145 | switch (type) | 145 | switch (type) |
146 | { | 146 | { |
147 | case GNUNET_NAT_TYPE_NO_NAT: | 147 | case GNUNET_NAT_TYPE_NO_NAT: |
148 | nat_type = "NO NAT"; | 148 | nat_type = "NO NAT"; |
149 | break; | 149 | break; |
150 | case GNUNET_NAT_TYPE_UNREACHABLE_NAT: | 150 | |
151 | nat_type = "NAT but we can traverse"; | 151 | case GNUNET_NAT_TYPE_UNREACHABLE_NAT: |
152 | break; | 152 | nat_type = "NAT but we can traverse"; |
153 | case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT: | 153 | break; |
154 | nat_type = "NAT but STUN is able to identify the correct information"; | 154 | |
155 | break; | 155 | case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT: |
156 | case GNUNET_NAT_TYPE_UPNP_NAT: | 156 | nat_type = "NAT but STUN is able to identify the correct information"; |
157 | nat_type = "NAT but UPNP opened the ports"; | 157 | break; |
158 | break; | 158 | |
159 | default: | 159 | case GNUNET_NAT_TYPE_UPNP_NAT: |
160 | sprintf (unknown_type, "NAT unknown, type %u", type); | 160 | nat_type = "NAT but UPNP opened the ports"; |
161 | nat_type = unknown_type; | 161 | break; |
162 | break; | 162 | |
163 | } | 163 | default: |
164 | 164 | sprintf(unknown_type, "NAT unknown, type %u", type); | |
165 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 165 | nat_type = unknown_type; |
166 | "NAT status: %s/%s\n", | 166 | break; |
167 | GNUNET_NAT_AUTO_status2string (result), | 167 | } |
168 | nat_type); | 168 | |
169 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, | ||
170 | "NAT status: %s/%s\n", | ||
171 | GNUNET_NAT_AUTO_status2string(result), | ||
172 | nat_type); | ||
169 | 173 | ||
170 | if (NULL == diff) | 174 | if (NULL == diff) |
171 | return; | 175 | return; |
172 | 176 | ||
173 | /* Shortcut: if there are no changes suggested, bail out early. */ | 177 | /* Shortcut: if there are no changes suggested, bail out early. */ |
174 | if (GNUNET_NO == GNUNET_CONFIGURATION_is_dirty (diff)) | 178 | if (GNUNET_NO == GNUNET_CONFIGURATION_is_dirty(diff)) |
175 | { | 179 | { |
176 | test_finished (); | 180 | test_finished(); |
177 | return; | 181 | return; |
178 | } | 182 | } |
179 | 183 | ||
180 | /* Apply diff to original configuration and show changes | 184 | /* Apply diff to original configuration and show changes |
181 | to the user */ | 185 | to the user */ |
182 | new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL; | 186 | new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup(cfg) : NULL; |
183 | 187 | ||
184 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 188 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, |
185 | _ ("Suggested configuration changes:\n")); | 189 | _("Suggested configuration changes:\n")); |
186 | GNUNET_CONFIGURATION_iterate_section_values (diff, | 190 | GNUNET_CONFIGURATION_iterate_section_values(diff, |
187 | "nat", | 191 | "nat", |
188 | &auto_conf_iter, | 192 | &auto_conf_iter, |
189 | new_cfg); | 193 | new_cfg); |
190 | 194 | ||
191 | /* If desired, write configuration to file; we write only the | 195 | /* If desired, write configuration to file; we write only the |
192 | changes to the defaults to keep things compact. */ | 196 | changes to the defaults to keep things compact. */ |
193 | if (write_cfg) | 197 | if (write_cfg) |
194 | { | ||
195 | struct GNUNET_CONFIGURATION_Handle *def_cfg; | ||
196 | |||
197 | GNUNET_CONFIGURATION_set_value_string (new_cfg, "ARM", "CONFIG", NULL); | ||
198 | def_cfg = GNUNET_CONFIGURATION_create (); | ||
199 | GNUNET_break (GNUNET_OK == GNUNET_CONFIGURATION_load (def_cfg, NULL)); | ||
200 | if (GNUNET_OK != | ||
201 | GNUNET_CONFIGURATION_write_diffs (def_cfg, new_cfg, cfg_file)) | ||
202 | { | 198 | { |
203 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 199 | struct GNUNET_CONFIGURATION_Handle *def_cfg; |
204 | _ ("Failed to write configuration to `%s'\n"), | 200 | |
205 | cfg_file); | 201 | GNUNET_CONFIGURATION_set_value_string(new_cfg, "ARM", "CONFIG", NULL); |
206 | global_ret = 1; | 202 | def_cfg = GNUNET_CONFIGURATION_create(); |
203 | GNUNET_break(GNUNET_OK == GNUNET_CONFIGURATION_load(def_cfg, NULL)); | ||
204 | if (GNUNET_OK != | ||
205 | GNUNET_CONFIGURATION_write_diffs(def_cfg, new_cfg, cfg_file)) | ||
206 | { | ||
207 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, | ||
208 | _("Failed to write configuration to `%s'\n"), | ||
209 | cfg_file); | ||
210 | global_ret = 1; | ||
211 | } | ||
212 | else | ||
213 | { | ||
214 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, | ||
215 | _("Wrote updated configuration to `%s'\n"), | ||
216 | cfg_file); | ||
217 | } | ||
218 | GNUNET_CONFIGURATION_destroy(def_cfg); | ||
207 | } | 219 | } |
208 | else | ||
209 | { | ||
210 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
211 | _ ("Wrote updated configuration to `%s'\n"), | ||
212 | cfg_file); | ||
213 | } | ||
214 | GNUNET_CONFIGURATION_destroy (def_cfg); | ||
215 | } | ||
216 | 220 | ||
217 | if (NULL != new_cfg) | 221 | if (NULL != new_cfg) |
218 | GNUNET_CONFIGURATION_destroy (new_cfg); | 222 | GNUNET_CONFIGURATION_destroy(new_cfg); |
219 | test_finished (); | 223 | test_finished(); |
220 | } | 224 | } |
221 | 225 | ||
222 | 226 | ||
@@ -228,11 +232,11 @@ auto_config_cb (void *cls, | |||
228 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code | 232 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code |
229 | */ | 233 | */ |
230 | static void | 234 | static void |
231 | test_report_cb (void *cls, enum GNUNET_NAT_StatusCode result) | 235 | test_report_cb(void *cls, enum GNUNET_NAT_StatusCode result) |
232 | { | 236 | { |
233 | nt = NULL; | 237 | nt = NULL; |
234 | printf ("NAT test result: %s\n", GNUNET_NAT_AUTO_status2string (result)); | 238 | printf("NAT test result: %s\n", GNUNET_NAT_AUTO_status2string(result)); |
235 | test_finished (); | 239 | test_finished(); |
236 | } | 240 | } |
237 | 241 | ||
238 | 242 | ||
@@ -242,18 +246,18 @@ test_report_cb (void *cls, enum GNUNET_NAT_StatusCode result) | |||
242 | * @param cls NULL | 246 | * @param cls NULL |
243 | */ | 247 | */ |
244 | static void | 248 | static void |
245 | do_shutdown (void *cls) | 249 | do_shutdown(void *cls) |
246 | { | 250 | { |
247 | if (NULL != ah) | 251 | if (NULL != ah) |
248 | { | 252 | { |
249 | GNUNET_NAT_AUTO_autoconfig_cancel (ah); | 253 | GNUNET_NAT_AUTO_autoconfig_cancel(ah); |
250 | ah = NULL; | 254 | ah = NULL; |
251 | } | 255 | } |
252 | if (NULL != nt) | 256 | if (NULL != nt) |
253 | { | 257 | { |
254 | GNUNET_NAT_AUTO_test_stop (nt); | 258 | GNUNET_NAT_AUTO_test_stop(nt); |
255 | nt = NULL; | 259 | nt = NULL; |
256 | } | 260 | } |
257 | } | 261 | } |
258 | 262 | ||
259 | 263 | ||
@@ -266,29 +270,29 @@ do_shutdown (void *cls) | |||
266 | * @param c configuration | 270 | * @param c configuration |
267 | */ | 271 | */ |
268 | static void | 272 | static void |
269 | run (void *cls, | 273 | run(void *cls, |
270 | char *const *args, | 274 | char *const *args, |
271 | const char *cfgfile, | 275 | const char *cfgfile, |
272 | const struct GNUNET_CONFIGURATION_Handle *c) | 276 | const struct GNUNET_CONFIGURATION_Handle *c) |
273 | { | 277 | { |
274 | cfg_file = cfgfile; | 278 | cfg_file = cfgfile; |
275 | cfg = c; | 279 | cfg = c; |
276 | 280 | ||
277 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | 281 | GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); |
278 | 282 | ||
279 | if (do_auto) | 283 | if (do_auto) |
280 | { | 284 | { |
281 | ah = GNUNET_NAT_AUTO_autoconfig_start (c, &auto_config_cb, NULL); | 285 | ah = GNUNET_NAT_AUTO_autoconfig_start(c, &auto_config_cb, NULL); |
282 | } | 286 | } |
283 | 287 | ||
284 | if (use_tcp && use_udp) | 288 | if (use_tcp && use_udp) |
285 | { | 289 | { |
286 | if (do_auto) | 290 | if (do_auto) |
291 | return; | ||
292 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n"); | ||
293 | global_ret = 1; | ||
287 | return; | 294 | return; |
288 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n"); | 295 | } |
289 | global_ret = 1; | ||
290 | return; | ||
291 | } | ||
292 | proto = 0; | 296 | proto = 0; |
293 | if (use_tcp) | 297 | if (use_tcp) |
294 | proto = IPPROTO_TCP; | 298 | proto = IPPROTO_TCP; |
@@ -296,14 +300,14 @@ run (void *cls, | |||
296 | proto = IPPROTO_UDP; | 300 | proto = IPPROTO_UDP; |
297 | 301 | ||
298 | if (NULL != section_name) | 302 | if (NULL != section_name) |
299 | { | 303 | { |
300 | nt = GNUNET_NAT_AUTO_test_start (c, | 304 | nt = GNUNET_NAT_AUTO_test_start(c, |
301 | proto, | 305 | proto, |
302 | section_name, | 306 | section_name, |
303 | &test_report_cb, | 307 | &test_report_cb, |
304 | NULL); | 308 | NULL); |
305 | } | 309 | } |
306 | test_finished (); | 310 | test_finished(); |
307 | } | 311 | } |
308 | 312 | ||
309 | 313 | ||
@@ -315,47 +319,47 @@ run (void *cls, | |||
315 | * @return 0 on success, -1 on error | 319 | * @return 0 on success, -1 on error |
316 | */ | 320 | */ |
317 | int | 321 | int |
318 | main (int argc, char *const argv[]) | 322 | main(int argc, char *const argv[]) |
319 | { | 323 | { |
320 | struct GNUNET_GETOPT_CommandLineOption options[] = | 324 | struct GNUNET_GETOPT_CommandLineOption options[] = |
321 | {GNUNET_GETOPT_option_flag ('a', | 325 | { GNUNET_GETOPT_option_flag('a', |
322 | "auto", | 326 | "auto", |
323 | gettext_noop ("run autoconfiguration"), | 327 | gettext_noop("run autoconfiguration"), |
324 | &do_auto), | 328 | &do_auto), |
325 | 329 | ||
326 | GNUNET_GETOPT_option_string ( | 330 | GNUNET_GETOPT_option_string( |
327 | 'S', | 331 | 'S', |
328 | "section", | 332 | "section", |
329 | "NAME", | 333 | "NAME", |
330 | gettext_noop ( | 334 | gettext_noop( |
331 | "section name providing the configuration for the adapter"), | 335 | "section name providing the configuration for the adapter"), |
332 | §ion_name), | 336 | §ion_name), |
333 | 337 | ||
334 | GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp), | 338 | GNUNET_GETOPT_option_flag('t', "tcp", gettext_noop("use TCP"), &use_tcp), |
335 | 339 | ||
336 | GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp), | 340 | GNUNET_GETOPT_option_flag('u', "udp", gettext_noop("use UDP"), &use_udp), |
337 | 341 | ||
338 | GNUNET_GETOPT_option_flag ( | 342 | GNUNET_GETOPT_option_flag( |
339 | 'w', | 343 | 'w', |
340 | "write", | 344 | "write", |
341 | gettext_noop ("write configuration file (for autoconfiguration)"), | 345 | gettext_noop("write configuration file (for autoconfiguration)"), |
342 | &write_cfg), | 346 | &write_cfg), |
343 | GNUNET_GETOPT_OPTION_END}; | 347 | GNUNET_GETOPT_OPTION_END }; |
344 | 348 | ||
345 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 349 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) |
346 | return 2; | 350 | return 2; |
347 | if (GNUNET_OK != | 351 | if (GNUNET_OK != |
348 | GNUNET_PROGRAM_run (argc, | 352 | GNUNET_PROGRAM_run(argc, |
349 | argv, | 353 | argv, |
350 | "gnunet-nat-auto [options]", | 354 | "gnunet-nat-auto [options]", |
351 | _ ("GNUnet NAT traversal autoconfiguration"), | 355 | _("GNUnet NAT traversal autoconfiguration"), |
352 | options, | 356 | options, |
353 | &run, | 357 | &run, |
354 | NULL)) | 358 | NULL)) |
355 | { | 359 | { |
356 | global_ret = 1; | 360 | global_ret = 1; |
357 | } | 361 | } |
358 | GNUNET_free ((void *) argv); | 362 | GNUNET_free((void *)argv); |
359 | return global_ret; | 363 | return global_ret; |
360 | } | 364 | } |
361 | 365 | ||