diff options
Diffstat (limited to 'src/nat/gnunet-nat.c')
-rw-r--r-- | src/nat/gnunet-nat.c | 279 |
1 files changed, 0 insertions, 279 deletions
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c index 4171babf9..02d68d787 100644 --- a/src/nat/gnunet-nat.c +++ b/src/nat/gnunet-nat.c | |||
@@ -34,11 +34,6 @@ | |||
34 | static int global_ret; | 34 | static int global_ret; |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * Handle to ongoing autoconfiguration. | ||
38 | */ | ||
39 | static struct GNUNET_NAT_AutoHandle *ah; | ||
40 | |||
41 | /** | ||
42 | * Name of section in configuration file to use for | 37 | * Name of section in configuration file to use for |
43 | * additional options. | 38 | * additional options. |
44 | */ | 39 | */ |
@@ -60,39 +55,11 @@ static int listen_reversal; | |||
60 | static int use_tcp; | 55 | static int use_tcp; |
61 | 56 | ||
62 | /** | 57 | /** |
63 | * If we do auto-configuration, should we write the result | ||
64 | * to a file? | ||
65 | */ | ||
66 | static int write_cfg; | ||
67 | |||
68 | /** | ||
69 | * Configuration filename. | ||
70 | */ | ||
71 | static const char *cfg_file; | ||
72 | |||
73 | /** | ||
74 | * Original configuration. | ||
75 | */ | ||
76 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
77 | |||
78 | /** | ||
79 | * Protocol to use. | 58 | * Protocol to use. |
80 | */ | 59 | */ |
81 | static uint8_t proto; | 60 | static uint8_t proto; |
82 | 61 | ||
83 | /** | 62 | /** |
84 | * Address we are bound to (in test), or should bind to | ||
85 | * (if #do_stun is set). | ||
86 | */ | ||
87 | static char *bind_addr; | ||
88 | |||
89 | /** | ||
90 | * External IP address and port to use for the test. | ||
91 | * If not set, use #bind_addr. | ||
92 | */ | ||
93 | static char *extern_addr; | ||
94 | |||
95 | /** | ||
96 | * Local address to use for connection reversal request. | 63 | * Local address to use for connection reversal request. |
97 | */ | 64 | */ |
98 | static char *local_addr; | 65 | static char *local_addr; |
@@ -108,16 +75,6 @@ static char *remote_addr; | |||
108 | static unsigned int do_stun; | 75 | static unsigned int do_stun; |
109 | 76 | ||
110 | /** | 77 | /** |
111 | * Should we run autoconfiguration? | ||
112 | */ | ||
113 | static unsigned int do_auto; | ||
114 | |||
115 | /** | ||
116 | * Handle to a NAT test operation. | ||
117 | */ | ||
118 | static struct GNUNET_NAT_Test *nt; | ||
119 | |||
120 | /** | ||
121 | * Handle to NAT operation. | 78 | * Handle to NAT operation. |
122 | */ | 79 | */ |
123 | static struct GNUNET_NAT_Handle *nh; | 80 | static struct GNUNET_NAT_Handle *nh; |
@@ -140,10 +97,6 @@ static struct GNUNET_SCHEDULER_Task *rtask; | |||
140 | static void | 97 | static void |
141 | test_finished () | 98 | test_finished () |
142 | { | 99 | { |
143 | if (NULL != ah) | ||
144 | return; | ||
145 | if (NULL != nt) | ||
146 | return; | ||
147 | if (NULL != nh) | 100 | if (NULL != nh) |
148 | return; | 101 | return; |
149 | if (NULL != rtask) | 102 | if (NULL != rtask) |
@@ -153,160 +106,6 @@ test_finished () | |||
153 | 106 | ||
154 | 107 | ||
155 | /** | 108 | /** |
156 | * Function to iterate over sugested changes options | ||
157 | * | ||
158 | * @param cls closure | ||
159 | * @param section name of the section | ||
160 | * @param option name of the option | ||
161 | * @param value value of the option | ||
162 | */ | ||
163 | static void | ||
164 | auto_conf_iter (void *cls, | ||
165 | const char *section, | ||
166 | const char *option, | ||
167 | const char *value) | ||
168 | { | ||
169 | struct GNUNET_CONFIGURATION_Handle *new_cfg = cls; | ||
170 | |||
171 | PRINTF ("%s: %s\n", | ||
172 | option, | ||
173 | value); | ||
174 | if (NULL != new_cfg) | ||
175 | GNUNET_CONFIGURATION_set_value_string (new_cfg, | ||
176 | section, | ||
177 | option, | ||
178 | value); | ||
179 | } | ||
180 | |||
181 | |||
182 | /** | ||
183 | * Function called with the result from the autoconfiguration. | ||
184 | * | ||
185 | * @param cls closure | ||
186 | * @param diff minimal suggested changes to the original configuration | ||
187 | * to make it work (as best as we can) | ||
188 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code | ||
189 | * @param type what the situation of the NAT | ||
190 | */ | ||
191 | static void | ||
192 | auto_config_cb (void *cls, | ||
193 | const struct GNUNET_CONFIGURATION_Handle *diff, | ||
194 | enum GNUNET_NAT_StatusCode result, | ||
195 | enum GNUNET_NAT_Type type) | ||
196 | { | ||
197 | const char *nat_type; | ||
198 | char unknown_type[64]; | ||
199 | struct GNUNET_CONFIGURATION_Handle *new_cfg; | ||
200 | |||
201 | ah = NULL; | ||
202 | switch (type) | ||
203 | { | ||
204 | case GNUNET_NAT_TYPE_NO_NAT: | ||
205 | nat_type = "NO NAT"; | ||
206 | break; | ||
207 | case GNUNET_NAT_TYPE_UNREACHABLE_NAT: | ||
208 | nat_type = "NAT but we can traverse"; | ||
209 | break; | ||
210 | case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT: | ||
211 | nat_type = "NAT but STUN is able to identify the correct information"; | ||
212 | break; | ||
213 | case GNUNET_NAT_TYPE_UPNP_NAT: | ||
214 | nat_type = "NAT but UPNP opened the ports"; | ||
215 | break; | ||
216 | default: | ||
217 | SPRINTF (unknown_type, | ||
218 | "NAT unknown, type %u", | ||
219 | type); | ||
220 | nat_type = unknown_type; | ||
221 | break; | ||
222 | } | ||
223 | |||
224 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
225 | "NAT status: %s/%s\n", | ||
226 | GNUNET_NAT_status2string (result), | ||
227 | nat_type); | ||
228 | |||
229 | /* Shortcut: if there are no changes suggested, bail out early. */ | ||
230 | if (GNUNET_NO == | ||
231 | GNUNET_CONFIGURATION_is_dirty (diff)) | ||
232 | { | ||
233 | test_finished (); | ||
234 | return; | ||
235 | } | ||
236 | |||
237 | /* Apply diff to original configuration and show changes | ||
238 | to the user */ | ||
239 | new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL; | ||
240 | |||
241 | if (NULL != diff) | ||
242 | { | ||
243 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
244 | _("Suggested configuration changes:\n")); | ||
245 | GNUNET_CONFIGURATION_iterate_section_values (diff, | ||
246 | "nat", | ||
247 | &auto_conf_iter, | ||
248 | new_cfg); | ||
249 | } | ||
250 | |||
251 | /* If desired, write configuration to file; we write only the | ||
252 | changes to the defaults to keep things compact. */ | ||
253 | if ( (write_cfg) && | ||
254 | (NULL != diff) ) | ||
255 | { | ||
256 | struct GNUNET_CONFIGURATION_Handle *def_cfg; | ||
257 | |||
258 | GNUNET_CONFIGURATION_set_value_string (new_cfg, | ||
259 | "ARM", | ||
260 | "CONFIG", | ||
261 | NULL); | ||
262 | def_cfg = GNUNET_CONFIGURATION_create (); | ||
263 | GNUNET_break (GNUNET_OK == | ||
264 | GNUNET_CONFIGURATION_load (def_cfg, | ||
265 | NULL)); | ||
266 | if (GNUNET_OK != | ||
267 | GNUNET_CONFIGURATION_write_diffs (def_cfg, | ||
268 | new_cfg, | ||
269 | cfg_file)) | ||
270 | { | ||
271 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
272 | _("Failed to write configuration to `%s'\n"), | ||
273 | cfg_file); | ||
274 | global_ret = 1; | ||
275 | } | ||
276 | else | ||
277 | { | ||
278 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
279 | _("Wrote updated configuration to `%s'\n"), | ||
280 | cfg_file); | ||
281 | } | ||
282 | GNUNET_CONFIGURATION_destroy (def_cfg); | ||
283 | } | ||
284 | |||
285 | if (NULL != new_cfg) | ||
286 | GNUNET_CONFIGURATION_destroy (new_cfg); | ||
287 | test_finished (); | ||
288 | } | ||
289 | |||
290 | |||
291 | /** | ||
292 | * Function called to report success or failure for | ||
293 | * NAT configuration test. | ||
294 | * | ||
295 | * @param cls closure | ||
296 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code | ||
297 | */ | ||
298 | static void | ||
299 | test_report_cb (void *cls, | ||
300 | enum GNUNET_NAT_StatusCode result) | ||
301 | { | ||
302 | nt = NULL; | ||
303 | PRINTF ("NAT test result: %s\n", | ||
304 | GNUNET_NAT_status2string (result)); | ||
305 | test_finished (); | ||
306 | } | ||
307 | |||
308 | |||
309 | /** | ||
310 | * Signature of the callback passed to #GNUNET_NAT_register() for | 109 | * Signature of the callback passed to #GNUNET_NAT_register() for |
311 | * a function to call whenever our set of 'valid' addresses changes. | 110 | * a function to call whenever our set of 'valid' addresses changes. |
312 | * | 111 | * |
@@ -362,16 +161,6 @@ reversal_cb (void *cls, | |||
362 | static void | 161 | static void |
363 | do_shutdown (void *cls) | 162 | do_shutdown (void *cls) |
364 | { | 163 | { |
365 | if (NULL != ah) | ||
366 | { | ||
367 | GNUNET_NAT_autoconfig_cancel (ah); | ||
368 | ah = NULL; | ||
369 | } | ||
370 | if (NULL != nt) | ||
371 | { | ||
372 | GNUNET_NAT_test_stop (nt); | ||
373 | nt = NULL; | ||
374 | } | ||
375 | if (NULL != nh) | 164 | if (NULL != nh) |
376 | { | 165 | { |
377 | GNUNET_NAT_unregister (nh); | 166 | GNUNET_NAT_unregister (nh); |
@@ -452,16 +241,11 @@ run (void *cls, | |||
452 | const struct GNUNET_CONFIGURATION_Handle *c) | 241 | const struct GNUNET_CONFIGURATION_Handle *c) |
453 | { | 242 | { |
454 | uint8_t af; | 243 | uint8_t af; |
455 | struct sockaddr_in bind_sa; | ||
456 | struct sockaddr_in extern_sa; | ||
457 | struct sockaddr *local_sa; | 244 | struct sockaddr *local_sa; |
458 | struct sockaddr *remote_sa; | 245 | struct sockaddr *remote_sa; |
459 | socklen_t local_len; | 246 | socklen_t local_len; |
460 | size_t remote_len; | 247 | size_t remote_len; |
461 | 248 | ||
462 | cfg_file = cfgfile; | ||
463 | cfg = c; | ||
464 | |||
465 | if (use_tcp && use_udp) | 249 | if (use_tcp && use_udp) |
466 | { | 250 | { |
467 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 251 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
@@ -478,50 +262,13 @@ run (void *cls, | |||
478 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | 262 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, |
479 | NULL); | 263 | NULL); |
480 | 264 | ||
481 | if (do_auto) | ||
482 | { | ||
483 | ah = GNUNET_NAT_autoconfig_start (c, | ||
484 | &auto_config_cb, | ||
485 | NULL); | ||
486 | } | ||
487 | |||
488 | if (0 == proto) | 265 | if (0 == proto) |
489 | { | 266 | { |
490 | if (do_auto) | ||
491 | return; /* all good, we just run auto config */ | ||
492 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 267 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
493 | "Must specify either TCP or UDP\n"); | 268 | "Must specify either TCP or UDP\n"); |
494 | global_ret = 1; | 269 | global_ret = 1; |
495 | return; | 270 | return; |
496 | } | 271 | } |
497 | if (NULL != bind_addr) | ||
498 | { | ||
499 | if (GNUNET_OK != | ||
500 | GNUNET_STRINGS_to_address_ipv4 (bind_addr, | ||
501 | strlen (bind_addr), | ||
502 | &bind_sa)) | ||
503 | { | ||
504 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
505 | "Invalid socket address `%s'\n", | ||
506 | bind_addr); | ||
507 | global_ret = 1; | ||
508 | return; | ||
509 | } | ||
510 | } | ||
511 | if (NULL != extern_addr) | ||
512 | { | ||
513 | if (GNUNET_OK != | ||
514 | GNUNET_STRINGS_to_address_ipv4 (extern_addr, | ||
515 | strlen (extern_addr), | ||
516 | &extern_sa)) | ||
517 | { | ||
518 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
519 | "Invalid socket address `%s'\n", | ||
520 | extern_addr); | ||
521 | global_ret = 1; | ||
522 | return; | ||
523 | } | ||
524 | } | ||
525 | if (NULL != local_addr) | 272 | if (NULL != local_addr) |
526 | { | 273 | { |
527 | local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, | 274 | local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, |
@@ -551,20 +298,6 @@ run (void *cls, | |||
551 | } | 298 | } |
552 | } | 299 | } |
553 | 300 | ||
554 | if (NULL != bind_addr) | ||
555 | { | ||
556 | if (NULL == extern_addr) | ||
557 | extern_sa = bind_sa; | ||
558 | nt = GNUNET_NAT_test_start (c, | ||
559 | proto, | ||
560 | bind_sa.sin_addr, | ||
561 | ntohs (bind_sa.sin_port), | ||
562 | extern_sa.sin_addr, | ||
563 | ntohs (extern_sa.sin_port), | ||
564 | &test_report_cb, | ||
565 | NULL); | ||
566 | } | ||
567 | |||
568 | if (NULL != local_addr) | 301 | if (NULL != local_addr) |
569 | { | 302 | { |
570 | nh = GNUNET_NAT_register (c, | 303 | nh = GNUNET_NAT_register (c, |
@@ -683,15 +416,6 @@ main (int argc, | |||
683 | char *const argv[]) | 416 | char *const argv[]) |
684 | { | 417 | { |
685 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 418 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
686 | {'a', "auto", NULL, | ||
687 | gettext_noop ("run autoconfiguration"), | ||
688 | GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto }, | ||
689 | {'b', "bind", "ADDRESS", | ||
690 | gettext_noop ("which IP and port are we bound to"), | ||
691 | GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr }, | ||
692 | {'e', "external", "ADDRESS", | ||
693 | gettext_noop ("which external IP and port should be used to test"), | ||
694 | GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr }, | ||
695 | {'i', "in", "ADDRESS", | 419 | {'i', "in", "ADDRESS", |
696 | gettext_noop ("which IP and port are we locally using to bind/listen to"), | 420 | gettext_noop ("which IP and port are we locally using to bind/listen to"), |
697 | GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr }, | 421 | GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr }, |
@@ -710,9 +434,6 @@ main (int argc, | |||
710 | {'u', "udp", NULL, | 434 | {'u', "udp", NULL, |
711 | gettext_noop ("use UDP"), | 435 | gettext_noop ("use UDP"), |
712 | GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp }, | 436 | GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp }, |
713 | {'w', "write", NULL, | ||
714 | gettext_noop ("write configuration file (for autoconfiguration)"), | ||
715 | GNUNET_NO, &GNUNET_GETOPT_set_one, &write_cfg }, | ||
716 | {'W', "watch", NULL, | 437 | {'W', "watch", NULL, |
717 | gettext_noop ("watch for connection reversal requests"), | 438 | gettext_noop ("watch for connection reversal requests"), |
718 | GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal }, | 439 | GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal }, |