diff options
Diffstat (limited to 'src/nat/nat_mini.c')
-rw-r--r-- | src/nat/nat_mini.c | 90 |
1 files changed, 39 insertions, 51 deletions
diff --git a/src/nat/nat_mini.c b/src/nat/nat_mini.c index c40231067..e88c9ca55 100644 --- a/src/nat/nat_mini.c +++ b/src/nat/nat_mini.c | |||
@@ -118,8 +118,9 @@ read_external_ipv4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
118 | 118 | ||
119 | eh->task = GNUNET_SCHEDULER_NO_TASK; | 119 | eh->task = GNUNET_SCHEDULER_NO_TASK; |
120 | if (GNUNET_YES == GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r)) | 120 | if (GNUNET_YES == GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r)) |
121 | ret = GNUNET_DISK_file_read (eh->r, | 121 | ret = |
122 | &eh->buf[eh->off], sizeof (eh->buf) - eh->off); | 122 | GNUNET_DISK_file_read (eh->r, &eh->buf[eh->off], |
123 | sizeof (eh->buf) - eh->off); | ||
123 | else | 124 | else |
124 | ret = -1; /* error reading, timeout, etc. */ | 125 | ret = -1; /* error reading, timeout, etc. */ |
125 | if (ret > 0) | 126 | if (ret > 0) |
@@ -172,9 +173,9 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout, | |||
172 | GNUNET_free (eh); | 173 | GNUNET_free (eh); |
173 | return NULL; | 174 | return NULL; |
174 | } | 175 | } |
175 | eh->eip = GNUNET_OS_start_process (NULL, | 176 | eh->eip = |
176 | eh->opipe, | 177 | GNUNET_OS_start_process (NULL, eh->opipe, "external-ip", "external-ip", |
177 | "external-ip", "external-ip", NULL); | 178 | NULL); |
178 | if (NULL == eh->eip) | 179 | if (NULL == eh->eip) |
179 | { | 180 | { |
180 | GNUNET_DISK_pipe_close (eh->opipe); | 181 | GNUNET_DISK_pipe_close (eh->opipe); |
@@ -184,8 +185,8 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout, | |||
184 | GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); | 185 | GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); |
185 | eh->timeout = GNUNET_TIME_relative_to_absolute (timeout); | 186 | eh->timeout = GNUNET_TIME_relative_to_absolute (timeout); |
186 | eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); | 187 | eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); |
187 | eh->task = GNUNET_SCHEDULER_add_read_file (timeout, | 188 | eh->task = |
188 | eh->r, &read_external_ipv4, eh); | 189 | GNUNET_SCHEDULER_add_read_file (timeout, eh->r, &read_external_ipv4, eh); |
189 | return eh; | 190 | return eh; |
190 | } | 191 | } |
191 | 192 | ||
@@ -278,8 +279,8 @@ struct GNUNET_NAT_MiniHandle | |||
278 | * @param cls the 'struct GNUNET_NAT_MiniHandle' | 279 | * @param cls the 'struct GNUNET_NAT_MiniHandle' |
279 | * @param tc scheduler context | 280 | * @param tc scheduler context |
280 | */ | 281 | */ |
281 | static void | 282 | static void do_refresh (void *cls, |
282 | do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | 283 | const struct GNUNET_SCHEDULER_TaskContext *tc); |
283 | 284 | ||
284 | 285 | ||
285 | /** | 286 | /** |
@@ -322,19 +323,15 @@ process_refresh_output (void *cls, const char *line) | |||
322 | mini->did_map = GNUNET_NO; | 323 | mini->did_map = GNUNET_NO; |
323 | } | 324 | } |
324 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); | 325 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); |
325 | mini->map_cmd = GNUNET_OS_command_run (&process_map_output, | 326 | mini->map_cmd = |
326 | mini, | 327 | GNUNET_OS_command_run (&process_map_output, mini, MAP_TIMEOUT, |
327 | MAP_TIMEOUT, | 328 | "upnpc", "upnpc", "-r", pstr, |
328 | "upnpc", | 329 | mini->is_tcp ? "tcp" : "udp", NULL); |
329 | "upnpc", | ||
330 | "-r", pstr, | ||
331 | mini->is_tcp ? "tcp" : "udp", | ||
332 | NULL); | ||
333 | if (NULL != mini->map_cmd) | 330 | if (NULL != mini->map_cmd) |
334 | return; | 331 | return; |
335 | } | 332 | } |
336 | mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 333 | mini->refresh_task = |
337 | &do_refresh, mini); | 334 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); |
338 | return; | 335 | return; |
339 | } | 336 | } |
340 | if (!mini->did_map) | 337 | if (!mini->did_map) |
@@ -377,10 +374,11 @@ process_refresh_output (void *cls, const char *line) | |||
377 | return; /* skip */ | 374 | return; /* skip */ |
378 | if (NULL == strstr (s, pstr)) | 375 | if (NULL == strstr (s, pstr)) |
379 | return; /* skip */ | 376 | return; /* skip */ |
380 | if (1 != sscanf (line, | 377 | if (1 != |
381 | (mini->is_tcp) | 378 | sscanf (line, |
382 | ? "%*u TCP %u->%*s:%*u %*s" | 379 | (mini-> |
383 | : "%*u UDP %u->%*s:%*u %*s", &nport)) | 380 | is_tcp) ? "%*u TCP %u->%*s:%*u %*s" : |
381 | "%*u UDP %u->%*s:%*u %*s", &nport)) | ||
384 | return; /* skip */ | 382 | return; /* skip */ |
385 | mini->found = GNUNET_YES; | 383 | mini->found = GNUNET_YES; |
386 | if (nport == ntohs (mini->current_addr.sin_port)) | 384 | if (nport == ntohs (mini->current_addr.sin_port)) |
@@ -410,10 +408,9 @@ do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
410 | 408 | ||
411 | mini->refresh_task = GNUNET_SCHEDULER_NO_TASK; | 409 | mini->refresh_task = GNUNET_SCHEDULER_NO_TASK; |
412 | mini->found = GNUNET_NO; | 410 | mini->found = GNUNET_NO; |
413 | mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, | 411 | mini->refresh_cmd = |
414 | mini, | 412 | GNUNET_OS_command_run (&process_refresh_output, mini, MAP_TIMEOUT, |
415 | MAP_TIMEOUT, | 413 | "upnpc", "upnpc", "-l", NULL); |
416 | "upnpc", "upnpc", "-l", NULL); | ||
417 | } | 414 | } |
418 | 415 | ||
419 | 416 | ||
@@ -436,8 +433,8 @@ process_map_output (void *cls, const char *line) | |||
436 | { | 433 | { |
437 | GNUNET_OS_command_stop (mini->map_cmd); | 434 | GNUNET_OS_command_stop (mini->map_cmd); |
438 | mini->map_cmd = NULL; | 435 | mini->map_cmd = NULL; |
439 | mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 436 | mini->refresh_task = |
440 | &do_refresh, mini); | 437 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); |
441 | return; | 438 | return; |
442 | } | 439 | } |
443 | /* | 440 | /* |
@@ -486,8 +483,7 @@ process_map_output (void *cls, const char *line) | |||
486 | * @return NULL on error (no 'upnpc' installed) | 483 | * @return NULL on error (no 'upnpc' installed) |
487 | */ | 484 | */ |
488 | struct GNUNET_NAT_MiniHandle * | 485 | struct GNUNET_NAT_MiniHandle * |
489 | GNUNET_NAT_mini_map_start (uint16_t port, | 486 | GNUNET_NAT_mini_map_start (uint16_t port, int is_tcp, |
490 | int is_tcp, | ||
491 | GNUNET_NAT_AddressCallback ac, void *ac_cls) | 487 | GNUNET_NAT_AddressCallback ac, void *ac_cls) |
492 | { | 488 | { |
493 | struct GNUNET_NAT_MiniHandle *ret; | 489 | struct GNUNET_NAT_MiniHandle *ret; |
@@ -501,17 +497,13 @@ GNUNET_NAT_mini_map_start (uint16_t port, | |||
501 | ret->is_tcp = is_tcp; | 497 | ret->is_tcp = is_tcp; |
502 | ret->port = port; | 498 | ret->port = port; |
503 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) port); | 499 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) port); |
504 | ret->map_cmd = GNUNET_OS_command_run (&process_map_output, | 500 | ret->map_cmd = |
505 | ret, | 501 | GNUNET_OS_command_run (&process_map_output, ret, MAP_TIMEOUT, "upnpc", |
506 | MAP_TIMEOUT, | 502 | "upnpc", "-r", pstr, is_tcp ? "tcp" : "udp", NULL); |
507 | "upnpc", | ||
508 | "upnpc", | ||
509 | "-r", pstr, | ||
510 | is_tcp ? "tcp" : "udp", NULL); | ||
511 | if (NULL != ret->map_cmd) | 503 | if (NULL != ret->map_cmd) |
512 | return ret; | 504 | return ret; |
513 | ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 505 | ret->refresh_task = |
514 | &do_refresh, ret); | 506 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret); |
515 | 507 | ||
516 | return ret; | 508 | return ret; |
517 | } | 509 | } |
@@ -581,21 +573,17 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) | |||
581 | /* Note: oddly enough, deletion uses the external port whereas | 573 | /* Note: oddly enough, deletion uses the external port whereas |
582 | * addition uses the internal port; this rarely matters since they | 574 | * addition uses the internal port; this rarely matters since they |
583 | * often are the same, but it might... */ | 575 | * often are the same, but it might... */ |
584 | GNUNET_snprintf (pstr, sizeof (pstr), | 576 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", |
585 | "%u", (unsigned int) ntohs (mini->current_addr.sin_port)); | 577 | (unsigned int) ntohs (mini->current_addr.sin_port)); |
586 | #if DEBUG_NAT | 578 | #if DEBUG_NAT |
587 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 579 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", |
588 | "nat", | ||
589 | "Unmapping port %u with UPnP\n", | 580 | "Unmapping port %u with UPnP\n", |
590 | ntohs (mini->current_addr.sin_port)); | 581 | ntohs (mini->current_addr.sin_port)); |
591 | #endif | 582 | #endif |
592 | mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, | 583 | mini->unmap_cmd = |
593 | mini, | 584 | GNUNET_OS_command_run (&process_unmap_output, mini, UNMAP_TIMEOUT, |
594 | UNMAP_TIMEOUT, | 585 | "upnpc", "upnpc", "-d", pstr, |
595 | "upnpc", | 586 | mini->is_tcp ? "tcp" : "udp", NULL); |
596 | "upnpc", | ||
597 | "-d", pstr, | ||
598 | mini->is_tcp ? "tcp" : "udp", NULL); | ||
599 | } | 587 | } |
600 | 588 | ||
601 | 589 | ||