diff options
Diffstat (limited to 'src/nat/gnunet-service-nat_mini.c')
-rw-r--r-- | src/nat/gnunet-service-nat_mini.c | 263 |
1 files changed, 105 insertions, 158 deletions
diff --git a/src/nat/gnunet-service-nat_mini.c b/src/nat/gnunet-service-nat_mini.c index c156d0eb0..375188ff2 100644 --- a/src/nat/gnunet-service-nat_mini.c +++ b/src/nat/gnunet-service-nat_mini.c | |||
@@ -29,7 +29,7 @@ | |||
29 | #include "gnunet-service-nat_mini.h" | 29 | #include "gnunet-service-nat_mini.h" |
30 | #include "nat.h" | 30 | #include "nat.h" |
31 | 31 | ||
32 | #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__) | 32 | #define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__) |
33 | 33 | ||
34 | /** | 34 | /** |
35 | * How long do we give upnpc to create a mapping? | 35 | * How long do we give upnpc to create a mapping? |
@@ -39,12 +39,14 @@ | |||
39 | /** | 39 | /** |
40 | * How long do we give upnpc to remove a mapping? | 40 | * How long do we give upnpc to remove a mapping? |
41 | */ | 41 | */ |
42 | #define UNMAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) | 42 | #define UNMAP_TIMEOUT \ |
43 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) | ||
43 | 44 | ||
44 | /** | 45 | /** |
45 | * How often do we check for changes in the mapping? | 46 | * How often do we check for changes in the mapping? |
46 | */ | 47 | */ |
47 | #define MAP_REFRESH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) | 48 | #define MAP_REFRESH_FREQ \ |
49 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) | ||
48 | 50 | ||
49 | 51 | ||
50 | /* ************************* external-ip calling ************************ */ | 52 | /* ************************* external-ip calling ************************ */ |
@@ -117,30 +119,27 @@ read_external_ipv4 (void *cls) | |||
117 | 119 | ||
118 | eh->task = NULL; | 120 | eh->task = NULL; |
119 | ret = GNUNET_DISK_file_read (eh->r, | 121 | ret = GNUNET_DISK_file_read (eh->r, |
120 | &eh->buf[eh->off], | 122 | &eh->buf[eh->off], |
121 | sizeof (eh->buf) - eh->off); | 123 | sizeof (eh->buf) - eh->off); |
122 | if (ret > 0) | 124 | if (ret > 0) |
123 | { | 125 | { |
124 | /* try to read more */ | 126 | /* try to read more */ |
125 | eh->off += ret; | 127 | eh->off += ret; |
126 | eh->task | 128 | eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
127 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 129 | eh->r, |
128 | eh->r, | 130 | &read_external_ipv4, |
129 | &read_external_ipv4, | 131 | eh); |
130 | eh); | ||
131 | return; | 132 | return; |
132 | } | 133 | } |
133 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID; | 134 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID; |
134 | if ( (eh->off > 7) && | 135 | if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n')) |
135 | (eh->buf[eh->off - 1] == '\n') ) | ||
136 | { | 136 | { |
137 | eh->buf[eh->off - 1] = '\0'; | 137 | eh->buf[eh->off - 1] = '\0'; |
138 | if (1 == inet_pton (AF_INET, | 138 | if (1 == inet_pton (AF_INET, eh->buf, &addr)) |
139 | eh->buf, | ||
140 | &addr)) | ||
141 | { | 139 | { |
142 | if (0 == addr.s_addr) | 140 | if (0 == addr.s_addr) |
143 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */ | 141 | eh->ret = |
142 | GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */ | ||
144 | else | 143 | else |
145 | eh->ret = GNUNET_NAT_ERROR_SUCCESS; | 144 | eh->ret = GNUNET_NAT_ERROR_SUCCESS; |
146 | } | 145 | } |
@@ -163,9 +162,7 @@ signal_external_ip_error (void *cls) | |||
163 | struct GNUNET_NAT_ExternalHandle *eh = cls; | 162 | struct GNUNET_NAT_ExternalHandle *eh = cls; |
164 | 163 | ||
165 | eh->task = NULL; | 164 | eh->task = NULL; |
166 | eh->cb (eh->cb_cls, | 165 | eh->cb (eh->cb_cls, NULL, eh->ret); |
167 | NULL, | ||
168 | eh->ret); | ||
169 | GNUNET_free (eh); | 166 | GNUNET_free (eh); |
170 | } | 167 | } |
171 | 168 | ||
@@ -178,8 +175,7 @@ signal_external_ip_error (void *cls) | |||
178 | * @return handle for cancellation (can only be used until @a cb is called), never NULL | 175 | * @return handle for cancellation (can only be used until @a cb is called), never NULL |
179 | */ | 176 | */ |
180 | struct GNUNET_NAT_ExternalHandle * | 177 | struct GNUNET_NAT_ExternalHandle * |
181 | GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, | 178 | GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls) |
182 | void *cb_cls) | ||
183 | { | 179 | { |
184 | struct GNUNET_NAT_ExternalHandle *eh; | 180 | struct GNUNET_NAT_ExternalHandle *eh; |
185 | 181 | ||
@@ -188,56 +184,43 @@ GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, | |||
188 | eh->cb_cls = cb_cls; | 184 | eh->cb_cls = cb_cls; |
189 | eh->ret = GNUNET_NAT_ERROR_SUCCESS; | 185 | eh->ret = GNUNET_NAT_ERROR_SUCCESS; |
190 | if (GNUNET_SYSERR == | 186 | if (GNUNET_SYSERR == |
191 | GNUNET_OS_check_helper_binary ("external-ip", | 187 | GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL)) |
192 | GNUNET_NO, | ||
193 | NULL)) | ||
194 | { | 188 | { |
195 | LOG (GNUNET_ERROR_TYPE_INFO, | 189 | LOG (GNUNET_ERROR_TYPE_INFO, _ ("`external-ip' command not found\n")); |
196 | _("`external-ip' command not found\n")); | ||
197 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; | 190 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; |
198 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, | 191 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); |
199 | eh); | ||
200 | return eh; | 192 | return eh; |
201 | } | 193 | } |
202 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 194 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
203 | "Running `external-ip' to determine our external IP\n"); | 195 | "Running `external-ip' to determine our external IP\n"); |
204 | eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, | 196 | eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); |
205 | GNUNET_YES, | ||
206 | GNUNET_NO, | ||
207 | GNUNET_YES); | ||
208 | if (NULL == eh->opipe) | 197 | if (NULL == eh->opipe) |
209 | { | 198 | { |
210 | eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; | 199 | eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; |
211 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, | 200 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); |
212 | eh); | ||
213 | return eh; | 201 | return eh; |
214 | } | 202 | } |
215 | eh->eip = | 203 | eh->eip = GNUNET_OS_start_process (GNUNET_NO, |
216 | GNUNET_OS_start_process (GNUNET_NO, | 204 | 0, |
217 | 0, | 205 | NULL, |
218 | NULL, | 206 | eh->opipe, |
219 | eh->opipe, | 207 | NULL, |
220 | NULL, | 208 | "external-ip", |
221 | "external-ip", | 209 | "external-ip", |
222 | "external-ip", | 210 | NULL); |
223 | NULL); | ||
224 | if (NULL == eh->eip) | 211 | if (NULL == eh->eip) |
225 | { | 212 | { |
226 | GNUNET_DISK_pipe_close (eh->opipe); | 213 | GNUNET_DISK_pipe_close (eh->opipe); |
227 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; | 214 | eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; |
228 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, | 215 | eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); |
229 | eh); | ||
230 | return eh; | 216 | return eh; |
231 | } | 217 | } |
232 | GNUNET_DISK_pipe_close_end (eh->opipe, | 218 | GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); |
233 | GNUNET_DISK_PIPE_END_WRITE); | 219 | eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); |
234 | eh->r = GNUNET_DISK_pipe_handle (eh->opipe, | 220 | eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
235 | GNUNET_DISK_PIPE_END_READ); | 221 | eh->r, |
236 | eh->task | 222 | &read_external_ipv4, |
237 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 223 | eh); |
238 | eh->r, | ||
239 | &read_external_ipv4, | ||
240 | eh); | ||
241 | return eh; | 224 | return eh; |
242 | } | 225 | } |
243 | 226 | ||
@@ -252,10 +235,8 @@ GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh) | |||
252 | { | 235 | { |
253 | if (NULL != eh->eip) | 236 | if (NULL != eh->eip) |
254 | { | 237 | { |
255 | (void) GNUNET_OS_process_kill (eh->eip, | 238 | (void) GNUNET_OS_process_kill (eh->eip, SIGKILL); |
256 | SIGKILL); | 239 | GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (eh->eip)); |
257 | GNUNET_break (GNUNET_OK == | ||
258 | GNUNET_OS_process_wait (eh->eip)); | ||
259 | GNUNET_OS_process_destroy (eh->eip); | 240 | GNUNET_OS_process_destroy (eh->eip); |
260 | } | 241 | } |
261 | if (NULL != eh->opipe) | 242 | if (NULL != eh->opipe) |
@@ -336,7 +317,6 @@ struct GNUNET_NAT_MiniHandle | |||
336 | * Which port are we mapping? | 317 | * Which port are we mapping? |
337 | */ | 318 | */ |
338 | uint16_t port; | 319 | uint16_t port; |
339 | |||
340 | }; | 320 | }; |
341 | 321 | ||
342 | 322 | ||
@@ -356,8 +336,7 @@ do_refresh (void *cls); | |||
356 | * @param line line of output, NULL at the end | 336 | * @param line line of output, NULL at the end |
357 | */ | 337 | */ |
358 | static void | 338 | static void |
359 | process_map_output (void *cls, | 339 | process_map_output (void *cls, const char *line); |
360 | const char *line); | ||
361 | 340 | ||
362 | 341 | ||
363 | /** | 342 | /** |
@@ -370,26 +349,22 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini) | |||
370 | { | 349 | { |
371 | char pstr[6]; | 350 | char pstr[6]; |
372 | 351 | ||
373 | GNUNET_snprintf (pstr, | 352 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); |
374 | sizeof (pstr), | 353 | mini->map_cmd = GNUNET_OS_command_run (&process_map_output, |
375 | "%u", | 354 | mini, |
376 | (unsigned int) mini->port); | 355 | MAP_TIMEOUT, |
377 | mini->map_cmd | 356 | "upnpc", |
378 | = GNUNET_OS_command_run (&process_map_output, | 357 | "upnpc", |
379 | mini, | 358 | "-r", |
380 | MAP_TIMEOUT, | 359 | pstr, |
381 | "upnpc", | 360 | mini->is_tcp ? "tcp" : "udp", |
382 | "upnpc", | 361 | NULL); |
383 | "-r", | ||
384 | pstr, | ||
385 | mini->is_tcp ? "tcp" : "udp", | ||
386 | NULL); | ||
387 | if (NULL == mini->map_cmd) | 362 | if (NULL == mini->map_cmd) |
388 | { | 363 | { |
389 | mini->ac (mini->ac_cls, | 364 | mini->ac (mini->ac_cls, |
390 | GNUNET_SYSERR, | 365 | GNUNET_SYSERR, |
391 | NULL, | 366 | NULL, |
392 | 0, | 367 | 0, |
393 | GNUNET_NAT_ERROR_UPNPC_FAILED); | 368 | GNUNET_NAT_ERROR_UPNPC_FAILED); |
394 | return; | 369 | return; |
395 | } | 370 | } |
@@ -404,8 +379,7 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini) | |||
404 | * @param line line of output, NULL at the end | 379 | * @param line line of output, NULL at the end |
405 | */ | 380 | */ |
406 | static void | 381 | static void |
407 | process_refresh_output (void *cls, | 382 | process_refresh_output (void *cls, const char *line) |
408 | const char *line) | ||
409 | { | 383 | { |
410 | struct GNUNET_NAT_MiniHandle *mini = cls; | 384 | struct GNUNET_NAT_MiniHandle *mini = cls; |
411 | char pstr[9]; | 385 | char pstr[9]; |
@@ -434,31 +408,28 @@ process_refresh_output (void *cls, | |||
434 | return; | 408 | return; |
435 | } | 409 | } |
436 | if (! mini->did_map) | 410 | if (! mini->did_map) |
437 | return; /* never mapped, won't find our mapping anyway */ | 411 | return; /* never mapped, won't find our mapping anyway */ |
438 | 412 | ||
439 | /* we're looking for output of the form: | 413 | /* we're looking for output of the form: |
440 | * "ExternalIPAddress = 12.134.41.124" */ | 414 | * "ExternalIPAddress = 12.134.41.124" */ |
441 | 415 | ||
442 | s = strstr (line, | 416 | s = strstr (line, "ExternalIPAddress = "); |
443 | "ExternalIPAddress = "); | ||
444 | if (NULL != s) | 417 | if (NULL != s) |
445 | { | 418 | { |
446 | s += strlen ("ExternalIPAddress = "); | 419 | s += strlen ("ExternalIPAddress = "); |
447 | if (1 != inet_pton (AF_INET, | 420 | if (1 != inet_pton (AF_INET, s, &exip)) |
448 | s, | 421 | return; /* skip */ |
449 | &exip)) | ||
450 | return; /* skip */ | ||
451 | if (exip.s_addr == mini->current_addr.sin_addr.s_addr) | 422 | if (exip.s_addr == mini->current_addr.sin_addr.s_addr) |
452 | return; /* no change */ | 423 | return; /* no change */ |
453 | /* update mapping */ | 424 | /* update mapping */ |
454 | mini->ac (mini->ac_cls, | 425 | mini->ac (mini->ac_cls, |
455 | GNUNET_NO, | 426 | GNUNET_NO, |
456 | (const struct sockaddr *) &mini->current_addr, | 427 | (const struct sockaddr *) &mini->current_addr, |
457 | sizeof (mini->current_addr), | 428 | sizeof (mini->current_addr), |
458 | GNUNET_NAT_ERROR_SUCCESS); | 429 | GNUNET_NAT_ERROR_SUCCESS); |
459 | mini->current_addr.sin_addr = exip; | 430 | mini->current_addr.sin_addr = exip; |
460 | mini->ac (mini->ac_cls, | 431 | mini->ac (mini->ac_cls, |
461 | GNUNET_YES, | 432 | GNUNET_YES, |
462 | (const struct sockaddr *) &mini->current_addr, | 433 | (const struct sockaddr *) &mini->current_addr, |
463 | sizeof (mini->current_addr), | 434 | sizeof (mini->current_addr), |
464 | GNUNET_NAT_ERROR_SUCCESS); | 435 | GNUNET_NAT_ERROR_SUCCESS); |
@@ -475,32 +446,29 @@ process_refresh_output (void *cls, | |||
475 | * "%s TCP PORT->STRING:OURPORT *" or | 446 | * "%s TCP PORT->STRING:OURPORT *" or |
476 | * "%s UDP PORT->STRING:OURPORT *" | 447 | * "%s UDP PORT->STRING:OURPORT *" |
477 | */ | 448 | */ |
478 | GNUNET_snprintf (pstr, | 449 | GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port); |
479 | sizeof (pstr), | ||
480 | ":%u ", | ||
481 | mini->port); | ||
482 | if (NULL == (s = strstr (line, "->"))) | 450 | if (NULL == (s = strstr (line, "->"))) |
483 | return; /* skip */ | 451 | return; /* skip */ |
484 | if (NULL == strstr (s, pstr)) | 452 | if (NULL == strstr (s, pstr)) |
485 | return; /* skip */ | 453 | return; /* skip */ |
486 | if (1 != | 454 | if (1 != sscanf (line, |
487 | SSCANF (line, | 455 | (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" |
488 | (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" : | 456 | : "%*u UDP %u->%*s:%*u %*s", |
489 | "%*u UDP %u->%*s:%*u %*s", &nport)) | 457 | &nport)) |
490 | return; /* skip */ | 458 | return; /* skip */ |
491 | mini->found = GNUNET_YES; | 459 | mini->found = GNUNET_YES; |
492 | if (nport == ntohs (mini->current_addr.sin_port)) | 460 | if (nport == ntohs (mini->current_addr.sin_port)) |
493 | return; /* no change */ | 461 | return; /* no change */ |
494 | 462 | ||
495 | /* external port changed, update mapping */ | 463 | /* external port changed, update mapping */ |
496 | mini->ac (mini->ac_cls, | 464 | mini->ac (mini->ac_cls, |
497 | GNUNET_NO, | 465 | GNUNET_NO, |
498 | (const struct sockaddr *) &mini->current_addr, | 466 | (const struct sockaddr *) &mini->current_addr, |
499 | sizeof (mini->current_addr), | 467 | sizeof (mini->current_addr), |
500 | GNUNET_NAT_ERROR_SUCCESS); | 468 | GNUNET_NAT_ERROR_SUCCESS); |
501 | mini->current_addr.sin_port = htons ((uint16_t) nport); | 469 | mini->current_addr.sin_port = htons ((uint16_t) nport); |
502 | mini->ac (mini->ac_cls, | 470 | mini->ac (mini->ac_cls, |
503 | GNUNET_YES, | 471 | GNUNET_YES, |
504 | (const struct sockaddr *) &mini->current_addr, | 472 | (const struct sockaddr *) &mini->current_addr, |
505 | sizeof (mini->current_addr), | 473 | sizeof (mini->current_addr), |
506 | GNUNET_NAT_ERROR_SUCCESS); | 474 | GNUNET_NAT_ERROR_SUCCESS); |
@@ -518,10 +486,8 @@ do_refresh (void *cls) | |||
518 | struct GNUNET_NAT_MiniHandle *mini = cls; | 486 | struct GNUNET_NAT_MiniHandle *mini = cls; |
519 | int ac; | 487 | int ac; |
520 | 488 | ||
521 | mini->refresh_task | 489 | mini->refresh_task = |
522 | = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 490 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); |
523 | &do_refresh, | ||
524 | mini); | ||
525 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 491 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
526 | "Running `upnpc' to check if our mapping still exists\n"); | 492 | "Running `upnpc' to check if our mapping still exists\n"); |
527 | mini->found = GNUNET_NO; | 493 | mini->found = GNUNET_NO; |
@@ -540,19 +506,18 @@ do_refresh (void *cls) | |||
540 | mini->refresh_cmd = NULL; | 506 | mini->refresh_cmd = NULL; |
541 | ac = GNUNET_YES; | 507 | ac = GNUNET_YES; |
542 | } | 508 | } |
543 | mini->refresh_cmd | 509 | mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, |
544 | = GNUNET_OS_command_run (&process_refresh_output, | 510 | mini, |
545 | mini, | 511 | MAP_TIMEOUT, |
546 | MAP_TIMEOUT, | 512 | "upnpc", |
547 | "upnpc", | 513 | "upnpc", |
548 | "upnpc", | 514 | "-l", |
549 | "-l", | 515 | NULL); |
550 | NULL); | ||
551 | if (GNUNET_YES == ac) | 516 | if (GNUNET_YES == ac) |
552 | mini->ac (mini->ac_cls, | 517 | mini->ac (mini->ac_cls, |
553 | GNUNET_SYSERR, | 518 | GNUNET_SYSERR, |
554 | NULL, | 519 | NULL, |
555 | 0, | 520 | 0, |
556 | GNUNET_NAT_ERROR_UPNPC_TIMEOUT); | 521 | GNUNET_NAT_ERROR_UPNPC_TIMEOUT); |
557 | } | 522 | } |
558 | 523 | ||
@@ -564,8 +529,7 @@ do_refresh (void *cls) | |||
564 | * @param line line of output, NULL at the end | 529 | * @param line line of output, NULL at the end |
565 | */ | 530 | */ |
566 | static void | 531 | static void |
567 | process_map_output (void *cls, | 532 | process_map_output (void *cls, const char *line) |
568 | const char *line) | ||
569 | { | 533 | { |
570 | struct GNUNET_NAT_MiniHandle *mini = cls; | 534 | struct GNUNET_NAT_MiniHandle *mini = cls; |
571 | const char *ipaddr; | 535 | const char *ipaddr; |
@@ -581,13 +545,11 @@ process_map_output (void *cls, | |||
581 | mini->ac (mini->ac_cls, | 545 | mini->ac (mini->ac_cls, |
582 | GNUNET_SYSERR, | 546 | GNUNET_SYSERR, |
583 | NULL, | 547 | NULL, |
584 | 0, | 548 | 0, |
585 | GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); | 549 | GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); |
586 | if (NULL == mini->refresh_task) | 550 | if (NULL == mini->refresh_task) |
587 | mini->refresh_task | 551 | mini->refresh_task = |
588 | = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 552 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); |
589 | &do_refresh, | ||
590 | mini); | ||
591 | return; | 553 | return; |
592 | } | 554 | } |
593 | /* | 555 | /* |
@@ -597,18 +559,16 @@ process_map_output (void *cls, | |||
597 | */ | 559 | */ |
598 | if ((NULL == (ipaddr = strstr (line, " "))) || | 560 | if ((NULL == (ipaddr = strstr (line, " "))) || |
599 | (NULL == (pstr = strstr (ipaddr, ":"))) || | 561 | (NULL == (pstr = strstr (ipaddr, ":"))) || |
600 | (1 != SSCANF (pstr + 1, "%u", &port))) | 562 | (1 != sscanf (pstr + 1, "%u", &port))) |
601 | { | 563 | { |
602 | return; /* skip line */ | 564 | return; /* skip line */ |
603 | } | 565 | } |
604 | ipa = GNUNET_strdup (ipaddr + 1); | 566 | ipa = GNUNET_strdup (ipaddr + 1); |
605 | strstr (ipa, ":")[0] = '\0'; | 567 | strstr (ipa, ":")[0] = '\0'; |
606 | if (1 != inet_pton (AF_INET, | 568 | if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr)) |
607 | ipa, | ||
608 | &mini->current_addr.sin_addr)) | ||
609 | { | 569 | { |
610 | GNUNET_free (ipa); | 570 | GNUNET_free (ipa); |
611 | return; /* skip line */ | 571 | return; /* skip line */ |
612 | } | 572 | } |
613 | GNUNET_free (ipa); | 573 | GNUNET_free (ipa); |
614 | 574 | ||
@@ -619,7 +579,7 @@ process_map_output (void *cls, | |||
619 | #endif | 579 | #endif |
620 | mini->did_map = GNUNET_YES; | 580 | mini->did_map = GNUNET_YES; |
621 | mini->ac (mini->ac_cls, | 581 | mini->ac (mini->ac_cls, |
622 | GNUNET_YES, | 582 | GNUNET_YES, |
623 | (const struct sockaddr *) &mini->current_addr, | 583 | (const struct sockaddr *) &mini->current_addr, |
624 | sizeof (mini->current_addr), | 584 | sizeof (mini->current_addr), |
625 | GNUNET_NAT_ERROR_SUCCESS); | 585 | GNUNET_NAT_ERROR_SUCCESS); |
@@ -647,30 +607,20 @@ GNUNET_NAT_mini_map_start (uint16_t port, | |||
647 | { | 607 | { |
648 | struct GNUNET_NAT_MiniHandle *ret; | 608 | struct GNUNET_NAT_MiniHandle *ret; |
649 | 609 | ||
650 | if (GNUNET_SYSERR == | 610 | if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)) |
651 | GNUNET_OS_check_helper_binary ("upnpc", | ||
652 | GNUNET_NO, | ||
653 | NULL)) | ||
654 | { | 611 | { |
655 | LOG (GNUNET_ERROR_TYPE_INFO, | 612 | LOG (GNUNET_ERROR_TYPE_INFO, _ ("`upnpc' command not found\n")); |
656 | _("`upnpc' command not found\n")); | 613 | ac (ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); |
657 | ac (ac_cls, | ||
658 | GNUNET_SYSERR, | ||
659 | NULL, 0, | ||
660 | GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); | ||
661 | return NULL; | 614 | return NULL; |
662 | } | 615 | } |
663 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 616 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n"); |
664 | "Running `upnpc' to install mapping\n"); | ||
665 | ret = GNUNET_new (struct GNUNET_NAT_MiniHandle); | 617 | ret = GNUNET_new (struct GNUNET_NAT_MiniHandle); |
666 | ret->ac = ac; | 618 | ret->ac = ac; |
667 | ret->ac_cls = ac_cls; | 619 | ret->ac_cls = ac_cls; |
668 | ret->is_tcp = is_tcp; | 620 | ret->is_tcp = is_tcp; |
669 | ret->port = port; | 621 | ret->port = port; |
670 | ret->refresh_task = | 622 | ret->refresh_task = |
671 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 623 | GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret); |
672 | &do_refresh, | ||
673 | ret); | ||
674 | run_upnpc_r (ret); | 624 | run_upnpc_r (ret); |
675 | return ret; | 625 | return ret; |
676 | } | 626 | } |
@@ -683,15 +633,13 @@ GNUNET_NAT_mini_map_start (uint16_t port, | |||
683 | * @param line line of output, NULL at the end | 633 | * @param line line of output, NULL at the end |
684 | */ | 634 | */ |
685 | static void | 635 | static void |
686 | process_unmap_output (void *cls, | 636 | process_unmap_output (void *cls, const char *line) |
687 | const char *line) | ||
688 | { | 637 | { |
689 | struct GNUNET_NAT_MiniHandle *mini = cls; | 638 | struct GNUNET_NAT_MiniHandle *mini = cls; |
690 | 639 | ||
691 | if (NULL == line) | 640 | if (NULL == line) |
692 | { | 641 | { |
693 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 642 | LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n"); |
694 | "UPnP unmap done\n"); | ||
695 | GNUNET_OS_command_stop (mini->unmap_cmd); | 643 | GNUNET_OS_command_stop (mini->unmap_cmd); |
696 | mini->unmap_cmd = NULL; | 644 | mini->unmap_cmd = NULL; |
697 | GNUNET_free (mini); | 645 | GNUNET_free (mini); |
@@ -735,7 +683,7 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) | |||
735 | return; | 683 | return; |
736 | } | 684 | } |
737 | mini->ac (mini->ac_cls, | 685 | mini->ac (mini->ac_cls, |
738 | GNUNET_NO, | 686 | GNUNET_NO, |
739 | (const struct sockaddr *) &mini->current_addr, | 687 | (const struct sockaddr *) &mini->current_addr, |
740 | sizeof (mini->current_addr), | 688 | sizeof (mini->current_addr), |
741 | GNUNET_NAT_ERROR_SUCCESS); | 689 | GNUNET_NAT_ERROR_SUCCESS); |
@@ -749,16 +697,15 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) | |||
749 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 697 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
750 | "Unmapping port %u with UPnP\n", | 698 | "Unmapping port %u with UPnP\n", |
751 | ntohs (mini->current_addr.sin_port)); | 699 | ntohs (mini->current_addr.sin_port)); |
752 | mini->unmap_cmd | 700 | mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, |
753 | = GNUNET_OS_command_run (&process_unmap_output, | 701 | mini, |
754 | mini, | 702 | UNMAP_TIMEOUT, |
755 | UNMAP_TIMEOUT, | 703 | "upnpc", |
756 | "upnpc", | 704 | "upnpc", |
757 | "upnpc", | 705 | "-d", |
758 | "-d", | 706 | pstr, |
759 | pstr, | 707 | mini->is_tcp ? "tcp" : "udp", |
760 | mini->is_tcp ? "tcp" : "udp", | 708 | NULL); |
761 | NULL); | ||
762 | } | 709 | } |
763 | 710 | ||
764 | 711 | ||