diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-08-15 21:46:35 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-08-15 21:46:35 +0000 |
commit | 502af2167f7c218366666ca4944bd7cc54b5b19a (patch) | |
tree | a91fec5cc9769d260640bd91c6633cb9cf395524 /src/nat/nat_mini.c | |
parent | 03af5a603b7cc53432249d5854cd412aa90dde0d (diff) | |
download | gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.tar.gz gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.zip |
indentation
Diffstat (limited to 'src/nat/nat_mini.c')
-rw-r--r-- | src/nat/nat_mini.c | 423 |
1 files changed, 190 insertions, 233 deletions
diff --git a/src/nat/nat_mini.c b/src/nat/nat_mini.c index 058e8dda2..c40231067 100644 --- a/src/nat/nat_mini.c +++ b/src/nat/nat_mini.c | |||
@@ -109,8 +109,7 @@ struct GNUNET_NAT_ExternalHandle | |||
109 | * @param tc scheduler context | 109 | * @param tc scheduler context |
110 | */ | 110 | */ |
111 | static void | 111 | static void |
112 | read_external_ipv4 (void *cls, | 112 | read_external_ipv4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
113 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
114 | { | 113 | { |
115 | struct GNUNET_NAT_ExternalHandle *eh = cls; | 114 | struct GNUNET_NAT_ExternalHandle *eh = cls; |
116 | ssize_t ret; | 115 | ssize_t ret; |
@@ -118,39 +117,34 @@ read_external_ipv4 (void *cls, | |||
118 | int iret; | 117 | int iret; |
119 | 118 | ||
120 | eh->task = GNUNET_SCHEDULER_NO_TASK; | 119 | eh->task = GNUNET_SCHEDULER_NO_TASK; |
121 | if (GNUNET_YES == | 120 | if (GNUNET_YES == GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r)) |
122 | GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, | ||
123 | eh->r)) | ||
124 | ret = GNUNET_DISK_file_read (eh->r, | 121 | ret = GNUNET_DISK_file_read (eh->r, |
125 | &eh->buf[eh->off], | 122 | &eh->buf[eh->off], sizeof (eh->buf) - eh->off); |
126 | sizeof (eh->buf)-eh->off); | ||
127 | else | 123 | else |
128 | ret = -1; /* error reading, timeout, etc. */ | 124 | ret = -1; /* error reading, timeout, etc. */ |
129 | if (ret > 0) | 125 | if (ret > 0) |
130 | { | 126 | { |
131 | /* try to read more */ | 127 | /* try to read more */ |
132 | eh->off += ret; | 128 | eh->off += ret; |
133 | eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining (eh->timeout), | 129 | eh->task = |
134 | eh->r, | 130 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining |
135 | &read_external_ipv4, | 131 | (eh->timeout), eh->r, |
136 | eh); | 132 | &read_external_ipv4, eh); |
137 | return; | 133 | return; |
138 | } | 134 | } |
139 | iret = GNUNET_NO; | 135 | iret = GNUNET_NO; |
140 | if ( (eh->off > 7) && | 136 | if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n')) |
141 | (eh->buf[eh->off-1] == '\n') ) | 137 | { |
138 | eh->buf[eh->off - 1] = '\0'; | ||
139 | if (1 == inet_pton (AF_INET, eh->buf, &addr)) | ||
142 | { | 140 | { |
143 | eh->buf[eh->off-1] = '\0'; | 141 | if (addr.s_addr == 0) |
144 | if (1 == inet_pton (AF_INET, eh->buf, &addr)) | 142 | iret = GNUNET_NO; /* got 0.0.0.0 */ |
145 | { | 143 | else |
146 | if (addr.s_addr == 0) | 144 | iret = GNUNET_OK; |
147 | iret = GNUNET_NO; /* got 0.0.0.0 */ | ||
148 | else | ||
149 | iret = GNUNET_OK; | ||
150 | } | ||
151 | } | 145 | } |
152 | eh->cb (eh->cb_cls, | 146 | } |
153 | (iret == GNUNET_OK) ? &addr : NULL); | 147 | eh->cb (eh->cb_cls, (iret == GNUNET_OK) ? &addr : NULL); |
154 | GNUNET_NAT_mini_get_external_ipv4_cancel (eh); | 148 | GNUNET_NAT_mini_get_external_ipv4_cancel (eh); |
155 | } | 149 | } |
156 | 150 | ||
@@ -165,40 +159,33 @@ read_external_ipv4 (void *cls, | |||
165 | */ | 159 | */ |
166 | struct GNUNET_NAT_ExternalHandle * | 160 | struct GNUNET_NAT_ExternalHandle * |
167 | GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout, | 161 | GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout, |
168 | GNUNET_NAT_IPCallback cb, | 162 | GNUNET_NAT_IPCallback cb, void *cb_cls) |
169 | void *cb_cls) | ||
170 | { | 163 | { |
171 | struct GNUNET_NAT_ExternalHandle *eh; | 164 | struct GNUNET_NAT_ExternalHandle *eh; |
172 | 165 | ||
173 | eh = GNUNET_malloc (sizeof (struct GNUNET_NAT_ExternalHandle)); | 166 | eh = GNUNET_malloc (sizeof (struct GNUNET_NAT_ExternalHandle)); |
174 | eh->cb = cb; | 167 | eh->cb = cb; |
175 | eh->cb_cls = cb_cls; | 168 | eh->cb_cls = cb_cls; |
176 | eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, | 169 | eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); |
177 | GNUNET_NO, | ||
178 | GNUNET_YES); | ||
179 | if (NULL == eh->opipe) | 170 | if (NULL == eh->opipe) |
180 | { | 171 | { |
181 | GNUNET_free (eh); | 172 | GNUNET_free (eh); |
182 | return NULL; | 173 | return NULL; |
183 | } | 174 | } |
184 | eh->eip = GNUNET_OS_start_process (NULL, | 175 | eh->eip = GNUNET_OS_start_process (NULL, |
185 | eh->opipe, | 176 | eh->opipe, |
186 | "external-ip", | 177 | "external-ip", "external-ip", NULL); |
187 | "external-ip", NULL); | ||
188 | if (NULL == eh->eip) | 178 | if (NULL == eh->eip) |
189 | { | 179 | { |
190 | GNUNET_DISK_pipe_close (eh->opipe); | 180 | GNUNET_DISK_pipe_close (eh->opipe); |
191 | GNUNET_free (eh); | 181 | GNUNET_free (eh); |
192 | return NULL; | 182 | return NULL; |
193 | } | 183 | } |
194 | GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); | 184 | GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); |
195 | eh->timeout = GNUNET_TIME_relative_to_absolute (timeout); | 185 | eh->timeout = GNUNET_TIME_relative_to_absolute (timeout); |
196 | eh->r = GNUNET_DISK_pipe_handle (eh->opipe, | 186 | eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); |
197 | GNUNET_DISK_PIPE_END_READ); | ||
198 | eh->task = GNUNET_SCHEDULER_add_read_file (timeout, | 187 | eh->task = GNUNET_SCHEDULER_add_read_file (timeout, |
199 | eh->r, | 188 | eh->r, &read_external_ipv4, eh); |
200 | &read_external_ipv4, | ||
201 | eh); | ||
202 | return eh; | 189 | return eh; |
203 | } | 190 | } |
204 | 191 | ||
@@ -222,7 +209,7 @@ GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh) | |||
222 | 209 | ||
223 | /** | 210 | /** |
224 | * Handle to a mapping created with upnpc. | 211 | * Handle to a mapping created with upnpc. |
225 | */ | 212 | */ |
226 | struct GNUNET_NAT_MiniHandle | 213 | struct GNUNET_NAT_MiniHandle |
227 | { | 214 | { |
228 | 215 | ||
@@ -274,7 +261,7 @@ struct GNUNET_NAT_MiniHandle | |||
274 | 261 | ||
275 | /** | 262 | /** |
276 | * Did we find our mapping during refresh scan? | 263 | * Did we find our mapping during refresh scan? |
277 | */ | 264 | */ |
278 | int found; | 265 | int found; |
279 | 266 | ||
280 | /** | 267 | /** |
@@ -292,8 +279,7 @@ struct GNUNET_NAT_MiniHandle | |||
292 | * @param tc scheduler context | 279 | * @param tc scheduler context |
293 | */ | 280 | */ |
294 | static void | 281 | static void |
295 | do_refresh (void *cls, | 282 | do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); |
296 | const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
297 | 283 | ||
298 | 284 | ||
299 | /** | 285 | /** |
@@ -302,9 +288,7 @@ do_refresh (void *cls, | |||
302 | * @param cls the 'struct GNUNET_NAT_MiniHandle' | 288 | * @param cls the 'struct GNUNET_NAT_MiniHandle' |
303 | * @param line line of output, NULL at the end | 289 | * @param line line of output, NULL at the end |
304 | */ | 290 | */ |
305 | static void | 291 | static void process_map_output (void *cls, const char *line); |
306 | process_map_output (void *cls, | ||
307 | const char *line); | ||
308 | 292 | ||
309 | 293 | ||
310 | /** | 294 | /** |
@@ -315,8 +299,7 @@ process_map_output (void *cls, | |||
315 | * @param line line of output, NULL at the end | 299 | * @param line line of output, NULL at the end |
316 | */ | 300 | */ |
317 | static void | 301 | static void |
318 | process_refresh_output (void *cls, | 302 | process_refresh_output (void *cls, const char *line) |
319 | const char *line) | ||
320 | { | 303 | { |
321 | struct GNUNET_NAT_MiniHandle *mini = cls; | 304 | struct GNUNET_NAT_MiniHandle *mini = cls; |
322 | char pstr[9]; | 305 | char pstr[9]; |
@@ -325,99 +308,92 @@ process_refresh_output (void *cls, | |||
325 | struct in_addr exip; | 308 | struct in_addr exip; |
326 | 309 | ||
327 | if (NULL == line) | 310 | if (NULL == line) |
311 | { | ||
312 | GNUNET_OS_command_stop (mini->refresh_cmd); | ||
313 | mini->refresh_cmd = NULL; | ||
314 | if (mini->found == GNUNET_NO) | ||
328 | { | 315 | { |
329 | GNUNET_OS_command_stop (mini->refresh_cmd); | 316 | /* mapping disappeared, try to re-create */ |
330 | mini->refresh_cmd = NULL; | 317 | if (mini->did_map) |
331 | if (mini->found == GNUNET_NO) | 318 | { |
332 | { | 319 | mini->ac (mini->ac_cls, GNUNET_NO, |
333 | /* mapping disappeared, try to re-create */ | 320 | (const struct sockaddr *) &mini->current_addr, |
334 | if (mini->did_map) | 321 | sizeof (mini->current_addr)); |
335 | { | 322 | mini->did_map = GNUNET_NO; |
336 | mini->ac (mini->ac_cls, GNUNET_NO, | 323 | } |
337 | (const struct sockaddr*) &mini->current_addr, | 324 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); |
338 | sizeof (mini->current_addr)); | 325 | mini->map_cmd = GNUNET_OS_command_run (&process_map_output, |
339 | mini->did_map = GNUNET_NO; | 326 | mini, |
340 | } | 327 | MAP_TIMEOUT, |
341 | GNUNET_snprintf (pstr, sizeof (pstr), | 328 | "upnpc", |
342 | "%u", | 329 | "upnpc", |
343 | (unsigned int) mini->port); | 330 | "-r", pstr, |
344 | mini->map_cmd = GNUNET_OS_command_run (&process_map_output, | 331 | mini->is_tcp ? "tcp" : "udp", |
345 | mini, | 332 | NULL); |
346 | MAP_TIMEOUT, | 333 | if (NULL != mini->map_cmd) |
347 | "upnpc", | 334 | return; |
348 | "upnpc", | ||
349 | "-r", pstr, | ||
350 | mini->is_tcp ? "tcp" : "udp", | ||
351 | NULL); | ||
352 | if (NULL != mini->map_cmd) | ||
353 | return; | ||
354 | } | ||
355 | mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | ||
356 | &do_refresh, | ||
357 | mini); | ||
358 | return; | ||
359 | } | 335 | } |
360 | if (! mini->did_map) | 336 | mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, |
361 | return; /* never mapped, won't find our mapping anyway */ | 337 | &do_refresh, mini); |
338 | return; | ||
339 | } | ||
340 | if (!mini->did_map) | ||
341 | return; /* never mapped, won't find our mapping anyway */ | ||
362 | 342 | ||
363 | /* we're looking for output of the form: | 343 | /* we're looking for output of the form: |
364 | "ExternalIPAddress = 12.134.41.124" */ | 344 | * "ExternalIPAddress = 12.134.41.124" */ |
365 | 345 | ||
366 | s = strstr (line, "ExternalIPAddress = "); | 346 | s = strstr (line, "ExternalIPAddress = "); |
367 | if (NULL != s) | 347 | if (NULL != s) |
368 | { | 348 | { |
369 | s += strlen ("ExternalIPAddress = "); | 349 | s += strlen ("ExternalIPAddress = "); |
370 | if (1 != inet_pton (AF_INET, | 350 | if (1 != inet_pton (AF_INET, s, &exip)) |
371 | s, &exip)) | 351 | return; /* skip */ |
372 | return; /* skip */ | 352 | if (exip.s_addr == mini->current_addr.sin_addr.s_addr) |
373 | if (exip.s_addr == mini->current_addr.sin_addr.s_addr) | 353 | return; /* no change */ |
374 | return; /* no change */ | 354 | /* update mapping */ |
375 | /* update mapping */ | 355 | mini->ac (mini->ac_cls, GNUNET_NO, |
376 | mini->ac (mini->ac_cls, GNUNET_NO, | 356 | (const struct sockaddr *) &mini->current_addr, |
377 | (const struct sockaddr*) &mini->current_addr, | 357 | sizeof (mini->current_addr)); |
378 | sizeof (mini->current_addr)); | 358 | mini->current_addr.sin_addr = exip; |
379 | mini->current_addr.sin_addr = exip; | 359 | mini->ac (mini->ac_cls, GNUNET_YES, |
380 | mini->ac (mini->ac_cls, GNUNET_YES, | 360 | (const struct sockaddr *) &mini->current_addr, |
381 | (const struct sockaddr*) &mini->current_addr, | 361 | sizeof (mini->current_addr)); |
382 | sizeof (mini->current_addr)); | 362 | return; |
383 | return; | 363 | } |
384 | } | ||
385 | /* | 364 | /* |
386 | we're looking for output of the form: | 365 | * we're looking for output of the form: |
387 | 366 | * | |
388 | "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''" | 367 | * "0 TCP 3000->192.168.2.150:3000 'libminiupnpc' ''" |
389 | "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''" | 368 | * "1 UDP 3001->192.168.2.150:3001 'libminiupnpc' ''" |
390 | 369 | * | |
391 | the pattern we look for is: | 370 | * the pattern we look for is: |
392 | 371 | * | |
393 | "%s TCP PORT->STRING:OURPORT *" or | 372 | * "%s TCP PORT->STRING:OURPORT *" or |
394 | "%s UDP PORT->STRING:OURPORT *" | 373 | * "%s UDP PORT->STRING:OURPORT *" |
395 | */ | 374 | */ |
396 | GNUNET_snprintf (pstr, sizeof (pstr), | 375 | GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port); |
397 | ":%u ", | ||
398 | mini->port); | ||
399 | if (NULL == (s = strstr (line, "->"))) | 376 | if (NULL == (s = strstr (line, "->"))) |
400 | return; /* skip */ | 377 | return; /* skip */ |
401 | if (NULL == strstr (s, pstr)) | 378 | if (NULL == strstr (s, pstr)) |
402 | return; /* skip */ | 379 | return; /* skip */ |
403 | if (1 != sscanf (line, | 380 | if (1 != sscanf (line, |
404 | (mini->is_tcp) | 381 | (mini->is_tcp) |
405 | ? "%*u TCP %u->%*s:%*u %*s" | 382 | ? "%*u TCP %u->%*s:%*u %*s" |
406 | : "%*u UDP %u->%*s:%*u %*s", | 383 | : "%*u UDP %u->%*s:%*u %*s", &nport)) |
407 | &nport)) | 384 | return; /* skip */ |
408 | return; /* skip */ | ||
409 | mini->found = GNUNET_YES; | 385 | mini->found = GNUNET_YES; |
410 | if (nport == ntohs (mini->current_addr.sin_port)) | 386 | if (nport == ntohs (mini->current_addr.sin_port)) |
411 | return; /* no change */ | 387 | return; /* no change */ |
412 | 388 | ||
413 | /* external port changed, update mapping */ | 389 | /* external port changed, update mapping */ |
414 | mini->ac (mini->ac_cls, GNUNET_NO, | 390 | mini->ac (mini->ac_cls, GNUNET_NO, |
415 | (const struct sockaddr*) &mini->current_addr, | 391 | (const struct sockaddr *) &mini->current_addr, |
416 | sizeof (mini->current_addr)); | 392 | sizeof (mini->current_addr)); |
417 | mini->current_addr.sin_port = htons ((uint16_t) nport); | 393 | mini->current_addr.sin_port = htons ((uint16_t) nport); |
418 | mini->ac (mini->ac_cls, GNUNET_YES, | 394 | mini->ac (mini->ac_cls, GNUNET_YES, |
419 | (const struct sockaddr*) &mini->current_addr, | 395 | (const struct sockaddr *) &mini->current_addr, |
420 | sizeof (mini->current_addr)); | 396 | sizeof (mini->current_addr)); |
421 | } | 397 | } |
422 | 398 | ||
423 | 399 | ||
@@ -428,20 +404,16 @@ process_refresh_output (void *cls, | |||
428 | * @param tc scheduler context | 404 | * @param tc scheduler context |
429 | */ | 405 | */ |
430 | static void | 406 | static void |
431 | do_refresh (void *cls, | 407 | do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
432 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
433 | { | 408 | { |
434 | struct GNUNET_NAT_MiniHandle *mini = cls; | 409 | struct GNUNET_NAT_MiniHandle *mini = cls; |
435 | 410 | ||
436 | mini->refresh_task = GNUNET_SCHEDULER_NO_TASK; | 411 | mini->refresh_task = GNUNET_SCHEDULER_NO_TASK; |
437 | mini->found = GNUNET_NO; | 412 | mini->found = GNUNET_NO; |
438 | mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, | 413 | mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, |
439 | mini, | 414 | mini, |
440 | MAP_TIMEOUT, | 415 | MAP_TIMEOUT, |
441 | "upnpc", | 416 | "upnpc", "upnpc", "-l", NULL); |
442 | "upnpc", | ||
443 | "-l", | ||
444 | NULL); | ||
445 | } | 417 | } |
446 | 418 | ||
447 | 419 | ||
@@ -452,8 +424,7 @@ do_refresh (void *cls, | |||
452 | * @param line line of output, NULL at the end | 424 | * @param line line of output, NULL at the end |
453 | */ | 425 | */ |
454 | static void | 426 | static void |
455 | process_map_output (void *cls, | 427 | process_map_output (void *cls, const char *line) |
456 | const char *line) | ||
457 | { | 428 | { |
458 | struct GNUNET_NAT_MiniHandle *mini = cls; | 429 | struct GNUNET_NAT_MiniHandle *mini = cls; |
459 | const char *ipaddr; | 430 | const char *ipaddr; |
@@ -462,35 +433,32 @@ process_map_output (void *cls, | |||
462 | unsigned int port; | 433 | unsigned int port; |
463 | 434 | ||
464 | if (NULL == line) | 435 | if (NULL == line) |
465 | { | 436 | { |
466 | GNUNET_OS_command_stop (mini->map_cmd); | 437 | GNUNET_OS_command_stop (mini->map_cmd); |
467 | mini->map_cmd = NULL; | 438 | mini->map_cmd = NULL; |
468 | mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 439 | mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, |
469 | &do_refresh, | 440 | &do_refresh, mini); |
470 | mini); | 441 | return; |
471 | return; | 442 | } |
472 | } | ||
473 | /* | 443 | /* |
474 | The upnpc output we're after looks like this: | 444 | * The upnpc output we're after looks like this: |
475 | 445 | * | |
476 | "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000" | 446 | * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000" |
477 | */ | 447 | */ |
478 | if ( (NULL == (ipaddr = strstr (line, " "))) || | 448 | if ((NULL == (ipaddr = strstr (line, " "))) || |
479 | (NULL == (pstr = strstr (ipaddr, ":"))) || | 449 | (NULL == (pstr = strstr (ipaddr, ":"))) || |
480 | (1 != sscanf (pstr + 1, "%u", &port)) ) | 450 | (1 != sscanf (pstr + 1, "%u", &port))) |
481 | { | 451 | { |
482 | return; /* skip line */ | 452 | return; /* skip line */ |
483 | } | 453 | } |
484 | ipa = GNUNET_strdup (ipaddr + 1); | 454 | ipa = GNUNET_strdup (ipaddr + 1); |
485 | strstr (ipa, ":")[0] = '\0'; | 455 | strstr (ipa, ":")[0] = '\0'; |
486 | if (1 != inet_pton (AF_INET, | 456 | if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr)) |
487 | ipa, | 457 | { |
488 | &mini->current_addr.sin_addr)) | 458 | GNUNET_free (ipa); |
489 | { | 459 | return; /* skip line */ |
490 | GNUNET_free (ipa); | 460 | } |
491 | return; /* skip line */ | 461 | GNUNET_free (ipa); |
492 | } | ||
493 | GNUNET_free (ipa); | ||
494 | 462 | ||
495 | mini->current_addr.sin_port = htons (port); | 463 | mini->current_addr.sin_port = htons (port); |
496 | mini->current_addr.sin_family = AF_INET; | 464 | mini->current_addr.sin_family = AF_INET; |
@@ -499,8 +467,8 @@ process_map_output (void *cls, | |||
499 | #endif | 467 | #endif |
500 | mini->did_map = GNUNET_YES; | 468 | mini->did_map = GNUNET_YES; |
501 | mini->ac (mini->ac_cls, GNUNET_YES, | 469 | mini->ac (mini->ac_cls, GNUNET_YES, |
502 | (const struct sockaddr*) &mini->current_addr, | 470 | (const struct sockaddr *) &mini->current_addr, |
503 | sizeof (mini->current_addr)); | 471 | sizeof (mini->current_addr)); |
504 | } | 472 | } |
505 | 473 | ||
506 | 474 | ||
@@ -519,37 +487,31 @@ process_map_output (void *cls, | |||
519 | */ | 487 | */ |
520 | struct GNUNET_NAT_MiniHandle * | 488 | struct GNUNET_NAT_MiniHandle * |
521 | GNUNET_NAT_mini_map_start (uint16_t port, | 489 | GNUNET_NAT_mini_map_start (uint16_t port, |
522 | int is_tcp, | 490 | int is_tcp, |
523 | GNUNET_NAT_AddressCallback ac, | 491 | GNUNET_NAT_AddressCallback ac, void *ac_cls) |
524 | void *ac_cls) | ||
525 | { | 492 | { |
526 | struct GNUNET_NAT_MiniHandle *ret; | 493 | struct GNUNET_NAT_MiniHandle *ret; |
527 | char pstr[6]; | 494 | char pstr[6]; |
528 | 495 | ||
529 | if (GNUNET_SYSERR == | 496 | if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc")) |
530 | GNUNET_OS_check_helper_binary ("upnpc")) | ||
531 | return NULL; | 497 | return NULL; |
532 | ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle)); | 498 | ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_MiniHandle)); |
533 | ret->ac = ac; | 499 | ret->ac = ac; |
534 | ret->ac_cls = ac_cls; | 500 | ret->ac_cls = ac_cls; |
535 | ret->is_tcp = is_tcp; | 501 | ret->is_tcp = is_tcp; |
536 | ret->port = port; | 502 | ret->port = port; |
537 | GNUNET_snprintf (pstr, sizeof (pstr), | 503 | GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) port); |
538 | "%u", | ||
539 | (unsigned int) port); | ||
540 | ret->map_cmd = GNUNET_OS_command_run (&process_map_output, | 504 | ret->map_cmd = GNUNET_OS_command_run (&process_map_output, |
541 | ret, | 505 | ret, |
542 | MAP_TIMEOUT, | 506 | MAP_TIMEOUT, |
543 | "upnpc", | 507 | "upnpc", |
544 | "upnpc", | 508 | "upnpc", |
545 | "-r", pstr, | 509 | "-r", pstr, |
546 | is_tcp ? "tcp" : "udp", | 510 | is_tcp ? "tcp" : "udp", NULL); |
547 | NULL); | ||
548 | if (NULL != ret->map_cmd) | 511 | if (NULL != ret->map_cmd) |
549 | return ret; | 512 | return ret; |
550 | ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, | 513 | ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, |
551 | &do_refresh, | 514 | &do_refresh, ret); |
552 | ret); | ||
553 | 515 | ||
554 | return ret; | 516 | return ret; |
555 | } | 517 | } |
@@ -562,23 +524,20 @@ GNUNET_NAT_mini_map_start (uint16_t port, | |||
562 | * @param line line of output, NULL at the end | 524 | * @param line line of output, NULL at the end |
563 | */ | 525 | */ |
564 | static void | 526 | static void |
565 | process_unmap_output (void *cls, | 527 | process_unmap_output (void *cls, const char *line) |
566 | const char *line) | ||
567 | { | 528 | { |
568 | struct GNUNET_NAT_MiniHandle *mini = cls; | 529 | struct GNUNET_NAT_MiniHandle *mini = cls; |
569 | 530 | ||
570 | if (NULL == line) | 531 | if (NULL == line) |
571 | { | 532 | { |
572 | #if DEBUG_NAT | 533 | #if DEBUG_NAT |
573 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 534 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", "UPnP unmap done\n"); |
574 | "nat", | ||
575 | "UPnP unmap done\n"); | ||
576 | #endif | 535 | #endif |
577 | GNUNET_OS_command_stop (mini->unmap_cmd); | 536 | GNUNET_OS_command_stop (mini->unmap_cmd); |
578 | mini->unmap_cmd = NULL; | 537 | mini->unmap_cmd = NULL; |
579 | GNUNET_free (mini); | 538 | GNUNET_free (mini); |
580 | return; | 539 | return; |
581 | } | 540 | } |
582 | /* we don't really care about the output... */ | 541 | /* we don't really care about the output... */ |
583 | } | 542 | } |
584 | 543 | ||
@@ -597,48 +556,46 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) | |||
597 | char pstr[6]; | 556 | char pstr[6]; |
598 | 557 | ||
599 | if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task) | 558 | if (GNUNET_SCHEDULER_NO_TASK != mini->refresh_task) |
600 | { | 559 | { |
601 | GNUNET_SCHEDULER_cancel (mini->refresh_task); | 560 | GNUNET_SCHEDULER_cancel (mini->refresh_task); |
602 | mini->refresh_task = GNUNET_SCHEDULER_NO_TASK; | 561 | mini->refresh_task = GNUNET_SCHEDULER_NO_TASK; |
603 | } | 562 | } |
604 | if (mini->refresh_cmd != NULL) | 563 | if (mini->refresh_cmd != NULL) |
564 | { | ||
565 | GNUNET_OS_command_stop (mini->refresh_cmd); | ||
566 | mini->refresh_cmd = NULL; | ||
567 | } | ||
568 | if (!mini->did_map) | ||
569 | { | ||
570 | if (mini->map_cmd != NULL) | ||
605 | { | 571 | { |
606 | GNUNET_OS_command_stop (mini->refresh_cmd); | 572 | GNUNET_OS_command_stop (mini->map_cmd); |
607 | mini->refresh_cmd = NULL; | 573 | mini->map_cmd = NULL; |
608 | } | ||
609 | if (! mini->did_map) | ||
610 | { | ||
611 | if (mini->map_cmd != NULL) | ||
612 | { | ||
613 | GNUNET_OS_command_stop (mini->map_cmd); | ||
614 | mini->map_cmd = NULL; | ||
615 | } | ||
616 | GNUNET_free (mini); | ||
617 | return; | ||
618 | } | 574 | } |
575 | GNUNET_free (mini); | ||
576 | return; | ||
577 | } | ||
619 | mini->ac (mini->ac_cls, GNUNET_NO, | 578 | mini->ac (mini->ac_cls, GNUNET_NO, |
620 | (const struct sockaddr*) &mini->current_addr, | 579 | (const struct sockaddr *) &mini->current_addr, |
621 | sizeof (mini->current_addr)); | 580 | sizeof (mini->current_addr)); |
622 | /* Note: oddly enough, deletion uses the external port whereas | 581 | /* Note: oddly enough, deletion uses the external port whereas |
623 | addition uses the internal port; this rarely matters since they | 582 | * addition uses the internal port; this rarely matters since they |
624 | often are the same, but it might... */ | 583 | * often are the same, but it might... */ |
625 | GNUNET_snprintf (pstr, sizeof (pstr), | 584 | GNUNET_snprintf (pstr, sizeof (pstr), |
626 | "%u", | 585 | "%u", (unsigned int) ntohs (mini->current_addr.sin_port)); |
627 | (unsigned int) ntohs (mini->current_addr.sin_port)); | ||
628 | #if DEBUG_NAT | 586 | #if DEBUG_NAT |
629 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 587 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
630 | "nat", | 588 | "nat", |
631 | "Unmapping port %u with UPnP\n", | 589 | "Unmapping port %u with UPnP\n", |
632 | ntohs (mini->current_addr.sin_port)); | 590 | ntohs (mini->current_addr.sin_port)); |
633 | #endif | 591 | #endif |
634 | mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, | 592 | mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, |
635 | mini, | 593 | mini, |
636 | UNMAP_TIMEOUT, | 594 | UNMAP_TIMEOUT, |
637 | "upnpc", | 595 | "upnpc", |
638 | "upnpc", | 596 | "upnpc", |
639 | "-d", pstr, | 597 | "-d", pstr, |
640 | mini->is_tcp ? "tcp" : "udp", | 598 | mini->is_tcp ? "tcp" : "udp", NULL); |
641 | NULL); | ||
642 | } | 599 | } |
643 | 600 | ||
644 | 601 | ||