diff options
Diffstat (limited to 'src/nat/gnunet-nat.c')
-rw-r--r-- | src/nat/gnunet-nat.c | 140 |
1 files changed, 76 insertions, 64 deletions
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c index 80bfb5726..50e0dff0f 100644 --- a/src/nat/gnunet-nat.c +++ b/src/nat/gnunet-nat.c | |||
@@ -34,9 +34,9 @@ | |||
34 | static int global_ret; | 34 | static int global_ret; |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * Name of section in configuration file to use for | 37 | * Name of section in configuration file to use for |
38 | * additional options. | 38 | * additional options. |
39 | */ | 39 | */ |
40 | static char *section_name; | 40 | static char *section_name; |
41 | 41 | ||
42 | /** | 42 | /** |
@@ -72,7 +72,7 @@ static char *remote_addr; | |||
72 | /** | 72 | /** |
73 | * Should we actually bind to #bind_addr and receive and process STUN requests? | 73 | * Should we actually bind to #bind_addr and receive and process STUN requests? |
74 | */ | 74 | */ |
75 | static unsigned int do_stun; | 75 | static int do_stun; |
76 | 76 | ||
77 | /** | 77 | /** |
78 | * Handle to NAT operation. | 78 | * Handle to NAT operation. |
@@ -81,7 +81,7 @@ static struct GNUNET_NAT_Handle *nh; | |||
81 | 81 | ||
82 | /** | 82 | /** |
83 | * Listen socket for STUN processing. | 83 | * Listen socket for STUN processing. |
84 | */ | 84 | */ |
85 | static struct GNUNET_NETWORK_Handle *ls; | 85 | static struct GNUNET_NETWORK_Handle *ls; |
86 | 86 | ||
87 | /** | 87 | /** |
@@ -110,7 +110,7 @@ test_finished () | |||
110 | * a function to call whenever our set of 'valid' addresses changes. | 110 | * a function to call whenever our set of 'valid' addresses changes. |
111 | * | 111 | * |
112 | * @param cls closure, NULL | 112 | * @param cls closure, NULL |
113 | * @param add_remove #GNUNET_YES to add a new public IP address, | 113 | * @param add_remove #GNUNET_YES to add a new public IP address, |
114 | * #GNUNET_NO to remove a previous (now invalid) one | 114 | * #GNUNET_NO to remove a previous (now invalid) one |
115 | * @param ac address class the address belongs to | 115 | * @param ac address class the address belongs to |
116 | * @param addr either the previous or the new public IP address | 116 | * @param addr either the previous or the new public IP address |
@@ -123,12 +123,12 @@ address_cb (void *cls, | |||
123 | const struct sockaddr *addr, | 123 | const struct sockaddr *addr, |
124 | socklen_t addrlen) | 124 | socklen_t addrlen) |
125 | { | 125 | { |
126 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 126 | fprintf (stdout, |
127 | "%s %s (%d)\n", | 127 | "%s %s (%d)\n", |
128 | add_remove ? "+" : "-", | 128 | add_remove ? "+" : "-", |
129 | GNUNET_a2s (addr, | 129 | GNUNET_a2s (addr, |
130 | addrlen), | 130 | addrlen), |
131 | (int) ac); | 131 | (int) ac); |
132 | } | 132 | } |
133 | 133 | ||
134 | 134 | ||
@@ -186,7 +186,7 @@ static void | |||
186 | stun_read_task (void *cls) | 186 | stun_read_task (void *cls) |
187 | { | 187 | { |
188 | ssize_t size; | 188 | ssize_t size; |
189 | 189 | ||
190 | rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 190 | rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
191 | ls, | 191 | ls, |
192 | &stun_read_task, | 192 | &stun_read_task, |
@@ -204,7 +204,7 @@ stun_read_task (void *cls) | |||
204 | struct sockaddr_storage sa; | 204 | struct sockaddr_storage sa; |
205 | socklen_t salen = sizeof (sa); | 205 | socklen_t salen = sizeof (sa); |
206 | ssize_t ret; | 206 | ssize_t ret; |
207 | 207 | ||
208 | ret = GNUNET_NETWORK_socket_recvfrom (ls, | 208 | ret = GNUNET_NETWORK_socket_recvfrom (ls, |
209 | buf, | 209 | buf, |
210 | size + 1, | 210 | size + 1, |
@@ -269,6 +269,10 @@ run (void *cls, | |||
269 | global_ret = 1; | 269 | global_ret = 1; |
270 | return; | 270 | return; |
271 | } | 271 | } |
272 | local_len = 0; | ||
273 | local_sa = NULL; | ||
274 | remote_len = 0; | ||
275 | remote_sa = NULL; | ||
272 | if (NULL != local_addr) | 276 | if (NULL != local_addr) |
273 | { | 277 | { |
274 | local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, | 278 | local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, |
@@ -279,13 +283,10 @@ run (void *cls, | |||
279 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 283 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
280 | "Invalid socket address `%s'\n", | 284 | "Invalid socket address `%s'\n", |
281 | local_addr); | 285 | local_addr); |
282 | global_ret = 1; | 286 | goto fail_and_shutdown; |
283 | return; | ||
284 | } | 287 | } |
285 | } | 288 | } |
286 | 289 | ||
287 | remote_len = 0; | ||
288 | |||
289 | if (NULL != remote_addr) | 290 | if (NULL != remote_addr) |
290 | { | 291 | { |
291 | remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr, | 292 | remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr, |
@@ -296,8 +297,7 @@ run (void *cls, | |||
296 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 297 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
297 | "Invalid socket address `%s'\n", | 298 | "Invalid socket address `%s'\n", |
298 | remote_addr); | 299 | remote_addr); |
299 | global_ret = 1; | 300 | goto fail_and_shutdown; |
300 | return; | ||
301 | } | 301 | } |
302 | } | 302 | } |
303 | 303 | ||
@@ -318,32 +318,26 @@ run (void *cls, | |||
318 | else if (listen_reversal) | 318 | else if (listen_reversal) |
319 | { | 319 | { |
320 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 320 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
321 | "Use of `-W` only effective in combination with `-i`\n"); | 321 | "Use of `-W` only effective in combination with `-i`\n"); |
322 | global_ret = 1; | 322 | goto fail_and_shutdown; |
323 | GNUNET_SCHEDULER_shutdown (); | ||
324 | return; | ||
325 | } | 323 | } |
326 | 324 | ||
327 | if (NULL != remote_addr) | 325 | if (NULL != remote_addr) |
328 | { | 326 | { |
329 | int ret; | 327 | int ret; |
330 | 328 | ||
331 | if ( (NULL == nh) || | 329 | if ( (NULL == nh) || |
332 | (sizeof (struct sockaddr_in) != local_len) ) | 330 | (sizeof (struct sockaddr_in) != local_len) ) |
333 | { | 331 | { |
334 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 332 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
335 | "Require IPv4 local address to initiate connection reversal\n"); | 333 | "Require IPv4 local address to initiate connection reversal\n"); |
336 | global_ret = 1; | 334 | goto fail_and_shutdown; |
337 | GNUNET_SCHEDULER_shutdown (); | ||
338 | return; | ||
339 | } | 335 | } |
340 | if (sizeof (struct sockaddr_in) != remote_len) | 336 | if (sizeof (struct sockaddr_in) != remote_len) |
341 | { | 337 | { |
342 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 338 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
343 | "Require IPv4 reversal target address\n"); | 339 | "Require IPv4 reversal target address\n"); |
344 | global_ret = 1; | 340 | goto fail_and_shutdown; |
345 | GNUNET_SCHEDULER_shutdown (); | ||
346 | return; | ||
347 | } | 341 | } |
348 | GNUNET_assert (AF_INET == local_sa->sa_family); | 342 | GNUNET_assert (AF_INET == local_sa->sa_family); |
349 | GNUNET_assert (AF_INET == remote_sa->sa_family); | 343 | GNUNET_assert (AF_INET == remote_sa->sa_family); |
@@ -365,24 +359,20 @@ run (void *cls, | |||
365 | break; | 359 | break; |
366 | } | 360 | } |
367 | } | 361 | } |
368 | 362 | ||
369 | if (do_stun) | 363 | if (do_stun) |
370 | { | 364 | { |
371 | if (NULL == local_addr) | 365 | if (NULL == local_addr) |
372 | { | 366 | { |
373 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 367 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
374 | "Require local address to support STUN requests\n"); | 368 | "Require local address to support STUN requests\n"); |
375 | global_ret = 1; | 369 | goto fail_and_shutdown; |
376 | GNUNET_SCHEDULER_shutdown (); | ||
377 | return; | ||
378 | } | 370 | } |
379 | if (IPPROTO_UDP != proto) | 371 | if (IPPROTO_UDP != proto) |
380 | { | 372 | { |
381 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 373 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, |
382 | "STUN only supported over UDP\n"); | 374 | "STUN only supported over UDP\n"); |
383 | global_ret = 1; | 375 | goto fail_and_shutdown; |
384 | GNUNET_SCHEDULER_shutdown (); | ||
385 | return; | ||
386 | } | 376 | } |
387 | ls = GNUNET_NETWORK_socket_create (af, | 377 | ls = GNUNET_NETWORK_socket_create (af, |
388 | SOCK_DGRAM, | 378 | SOCK_DGRAM, |
@@ -397,17 +387,22 @@ run (void *cls, | |||
397 | GNUNET_a2s (local_sa, | 387 | GNUNET_a2s (local_sa, |
398 | local_len), | 388 | local_len), |
399 | STRERROR (errno)); | 389 | STRERROR (errno)); |
400 | global_ret = 1; | 390 | goto fail_and_shutdown; |
401 | GNUNET_SCHEDULER_shutdown (); | ||
402 | return; | ||
403 | } | 391 | } |
404 | rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 392 | rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
405 | ls, | 393 | ls, |
406 | &stun_read_task, | 394 | &stun_read_task, |
407 | NULL); | 395 | NULL); |
408 | } | 396 | } |
409 | 397 | GNUNET_free_non_null (remote_sa); | |
398 | GNUNET_free_non_null (local_sa); | ||
410 | test_finished (); | 399 | test_finished (); |
400 | return; | ||
401 | fail_and_shutdown: | ||
402 | global_ret = 1; | ||
403 | GNUNET_SCHEDULER_shutdown (); | ||
404 | GNUNET_free_non_null (remote_sa); | ||
405 | GNUNET_free_non_null (local_sa); | ||
411 | } | 406 | } |
412 | 407 | ||
413 | 408 | ||
@@ -422,29 +417,46 @@ int | |||
422 | main (int argc, | 417 | main (int argc, |
423 | char *const argv[]) | 418 | char *const argv[]) |
424 | { | 419 | { |
425 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 420 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
426 | {'i', "in", "ADDRESS", | 421 | |
427 | gettext_noop ("which IP and port are we locally using to bind/listen to"), | 422 | GNUNET_GETOPT_OPTION_STRING ('i', |
428 | GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr }, | 423 | "in", |
429 | {'r', "remote", "ADDRESS", | 424 | "ADDRESS", |
430 | gettext_noop ("which remote IP and port should be asked for connection reversal"), | 425 | gettext_noop ("which IP and port are we locally using to bind/listen to"), |
431 | GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr }, | 426 | &local_addr), |
432 | {'S', "section", NULL, | 427 | |
433 | gettext_noop ("name of configuration section to find additional options, such as manual host punching data"), | 428 | GNUNET_GETOPT_OPTION_STRING ('r', |
434 | GNUNET_YES, &GNUNET_GETOPT_set_string, §ion_name }, | 429 | "remote", |
435 | {'s', "stun", NULL, | 430 | "ADDRESS", |
436 | gettext_noop ("enable STUN processing"), | 431 | gettext_noop ("which remote IP and port should be asked for connection reversal"), |
437 | GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun }, | 432 | &remote_addr), |
438 | {'t', "tcp", NULL, | 433 | |
439 | gettext_noop ("use TCP"), | 434 | GNUNET_GETOPT_OPTION_STRING ('S', |
440 | GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp }, | 435 | "section", |
441 | {'u', "udp", NULL, | 436 | NULL, |
442 | gettext_noop ("use UDP"), | 437 | gettext_noop ("name of configuration section to find additional options, such as manual host punching data"), |
443 | GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp }, | 438 | §ion_name), |
444 | {'W', "watch", NULL, | 439 | |
445 | gettext_noop ("watch for connection reversal requests"), | 440 | GNUNET_GETOPT_OPTION_SET_ONE ('s', |
446 | GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal }, | 441 | "stun", |
447 | GNUNET_GETOPT_OPTION_END | 442 | gettext_noop ("enable STUN processing"), |
443 | &do_stun), | ||
444 | |||
445 | GNUNET_GETOPT_OPTION_SET_ONE ('t', | ||
446 | "tcp", | ||
447 | gettext_noop ("use TCP"), | ||
448 | &use_tcp), | ||
449 | |||
450 | GNUNET_GETOPT_OPTION_SET_ONE ('u', | ||
451 | "udp", | ||
452 | gettext_noop ("use UDP"), | ||
453 | &use_udp), | ||
454 | |||
455 | GNUNET_GETOPT_OPTION_SET_ONE ('W', | ||
456 | "watch", | ||
457 | gettext_noop ("watch for connection reversal requests"), | ||
458 | &listen_reversal), | ||
459 | GNUNET_GETOPT_OPTION_END | ||
448 | }; | 460 | }; |
449 | 461 | ||
450 | if (GNUNET_OK != | 462 | if (GNUNET_OK != |