aboutsummaryrefslogtreecommitdiff
path: root/src/nat-auto/gnunet-service-nat-auto_legacy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat-auto/gnunet-service-nat-auto_legacy.c')
-rw-r--r--src/nat-auto/gnunet-service-nat-auto_legacy.c1008
1 files changed, 503 insertions, 505 deletions
diff --git a/src/nat-auto/gnunet-service-nat-auto_legacy.c b/src/nat-auto/gnunet-service-nat-auto_legacy.c
index 00bdeb693..30f1f855d 100644
--- a/src/nat-auto/gnunet-service-nat-auto_legacy.c
+++ b/src/nat-auto/gnunet-service-nat-auto_legacy.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file nat/nat_auto.c 22 * @file nat/nat_auto.c
@@ -30,21 +30,20 @@
30#include "gnunet_nat_lib.h" 30#include "gnunet_nat_lib.h"
31#include "nat.h" 31#include "nat.h"
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__)
34 34
35 35
36/** 36/**
37 * How long do we wait for the NAT test to report success? 37 * How long do we wait for the NAT test to report success?
38 */ 38 */
39#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 39#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
40 40
41#define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) 41#define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
42 42
43/** 43/**
44 * Phases of the auto configuration. 44 * Phases of the auto configuration.
45 */ 45 */
46enum AutoPhase 46enum AutoPhase {
47{
48 /** 47 /**
49 * Initial start value. 48 * Initial start value.
50 */ 49 */
@@ -58,7 +57,7 @@ enum AutoPhase
58 /** 57 /**
59 * Test our external IP. 58 * Test our external IP.
60 */ 59 */
61 AUTO_STUN, 60 AUTO_STUN,
62 61
63 /** 62 /**
64 * Test our internal IP. 63 * Test our internal IP.
@@ -89,16 +88,13 @@ enum AutoPhase
89 * Last phase, we're done. 88 * Last phase, we're done.
90 */ 89 */
91 AUTO_DONE 90 AUTO_DONE
92
93}; 91};
94 92
95 93
96/** 94/**
97 * Handle to auto-configuration in progress. 95 * Handle to auto-configuration in progress.
98 */ 96 */
99struct GNUNET_NAT_AutoHandle 97struct GNUNET_NAT_AutoHandle {
100{
101
102 /** 98 /**
103 * Handle to the active NAT test. 99 * Handle to the active NAT test.
104 */ 100 */
@@ -209,7 +205,7 @@ static unsigned int stun_port = 3478;
209 * @param ah auto test handle 205 * @param ah auto test handle
210 */ 206 */
211static void 207static void
212next_phase (struct GNUNET_NAT_AutoHandle *ah); 208next_phase(struct GNUNET_NAT_AutoHandle *ah);
213 209
214 210
215static void 211static void
@@ -217,12 +213,12 @@ process_stun_reply(struct sockaddr_in *answer,
217 struct GNUNET_NAT_AutoHandle *ah) 213 struct GNUNET_NAT_AutoHandle *ah)
218{ 214{
219 ah->stun_ip = inet_ntoa(answer->sin_addr); 215 ah->stun_ip = inet_ntoa(answer->sin_addr);
220 ah->stun_port = ntohs (answer->sin_port); 216 ah->stun_port = ntohs(answer->sin_port);
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 217 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
222 "External IP is: %s , with port %u\n", 218 "External IP is: %s , with port %u\n",
223 ah->stun_ip, 219 ah->stun_ip,
224 ah->stun_port); 220 ah->stun_port);
225 next_phase (ah); 221 next_phase(ah);
226} 222}
227 223
228 224
@@ -230,22 +226,22 @@ process_stun_reply(struct sockaddr_in *answer,
230 * Function that terminates the test. 226 * Function that terminates the test.
231 */ 227 */
232static void 228static void
233stop_stun () 229stop_stun()
234{ 230{
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 231 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
236 "Stopping STUN and quitting...\n"); 232 "Stopping STUN and quitting...\n");
237 /* Clean task */ 233 /* Clean task */
238 if (NULL != ltask4) 234 if (NULL != ltask4)
239 { 235 {
240 GNUNET_SCHEDULER_cancel (ltask4); 236 GNUNET_SCHEDULER_cancel(ltask4);
241 ltask4 = NULL; 237 ltask4 = NULL;
242 } 238 }
243 /* Clean socket */ 239 /* Clean socket */
244 if (NULL != lsock4) 240 if (NULL != lsock4)
245 { 241 {
246 GNUNET_NETWORK_socket_close (lsock4); 242 GNUNET_NETWORK_socket_close(lsock4);
247 lsock4 = NULL; 243 lsock4 = NULL;
248 } 244 }
249} 245}
250 246
251 247
@@ -256,7 +252,7 @@ stop_stun ()
256 * @param cls 252 * @param cls
257 */ 253 */
258static void 254static void
259do_udp_read (void *cls) 255do_udp_read(void *cls)
260{ 256{
261 struct GNUNET_NAT_AutoHandle *ah = cls; 257 struct GNUNET_NAT_AutoHandle *ah = cls;
262 unsigned char reply_buf[1024]; 258 unsigned char reply_buf[1024];
@@ -264,51 +260,51 @@ do_udp_read (void *cls)
264 struct sockaddr_in answer; 260 struct sockaddr_in answer;
265 const struct GNUNET_SCHEDULER_TaskContext *tc; 261 const struct GNUNET_SCHEDULER_TaskContext *tc;
266 262
267 tc = GNUNET_SCHEDULER_get_task_context (); 263 tc = GNUNET_SCHEDULER_get_task_context();
268 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && 264 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
269 (GNUNET_NETWORK_fdset_isset (tc->read_ready, 265 (GNUNET_NETWORK_fdset_isset(tc->read_ready,
270 lsock4))) 266 lsock4)))
271 {
272 rlen = GNUNET_NETWORK_socket_recv (lsock4,
273 reply_buf,
274 sizeof (reply_buf));
275
276 //Lets handle the packet
277 memset (&answer, 0, sizeof(struct sockaddr_in));
278 if (ah->phase == AUTO_NAT_PUNCHED)
279 { 267 {
280 //Destroy the connection 268 rlen = GNUNET_NETWORK_socket_recv(lsock4,
281 GNUNET_NETWORK_socket_close (lsock4); 269 reply_buf,
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 270 sizeof(reply_buf));
283 "The external server was able to connect back"); 271
284 ah->connected_back = GNUNET_YES; 272 //Lets handle the packet
285 next_phase (ah); 273 memset(&answer, 0, sizeof(struct sockaddr_in));
286 } 274 if (ah->phase == AUTO_NAT_PUNCHED)
287 else 275 {
288 { 276 //Destroy the connection
289 if (GNUNET_OK == 277 GNUNET_NETWORK_socket_close(lsock4);
290 GNUNET_NAT_stun_handle_packet (reply_buf, rlen, &answer)) 278 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
291 { 279 "The external server was able to connect back");
292 //Process the answer 280 ah->connected_back = GNUNET_YES;
293 process_stun_reply (&answer, ah); 281 next_phase(ah);
294 } 282 }
295 else 283 else
296 { 284 {
297 next_phase (ah); 285 if (GNUNET_OK ==
298 } 286 GNUNET_NAT_stun_handle_packet(reply_buf, rlen, &answer))
287 {
288 //Process the answer
289 process_stun_reply(&answer, ah);
290 }
291 else
292 {
293 next_phase(ah);
294 }
295 }
299 } 296 }
300 }
301 else 297 else
302 {
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304 "TIMEOUT while waiting for an answer\n");
305 if (ah->phase == AUTO_NAT_PUNCHED)
306 { 298 {
307 stop_stun(); 299 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
308 } 300 "TIMEOUT while waiting for an answer\n");
301 if (ah->phase == AUTO_NAT_PUNCHED)
302 {
303 stop_stun();
304 }
309 305
310 next_phase (ah); 306 next_phase(ah);
311 } 307 }
312} 308}
313 309
314 310
@@ -318,45 +314,45 @@ do_udp_read (void *cls)
318 * @return NULL on error 314 * @return NULL on error
319 */ 315 */
320static struct GNUNET_NETWORK_Handle * 316static struct GNUNET_NETWORK_Handle *
321bind_v4 () 317bind_v4()
322{ 318{
323 struct GNUNET_NETWORK_Handle *ls; 319 struct GNUNET_NETWORK_Handle *ls;
324 struct sockaddr_in sa4; 320 struct sockaddr_in sa4;
325 int eno; 321 int eno;
326 322
327 memset (&sa4, 0, sizeof (sa4)); 323 memset(&sa4, 0, sizeof(sa4));
328 sa4.sin_family = AF_INET; 324 sa4.sin_family = AF_INET;
329 sa4.sin_port = htons (port); 325 sa4.sin_port = htons(port);
330#if HAVE_SOCKADDR_IN_SIN_LEN 326#if HAVE_SOCKADDR_IN_SIN_LEN
331 sa4.sin_len = sizeof (sa4); 327 sa4.sin_len = sizeof(sa4);
332#endif 328#endif
333 ls = GNUNET_NETWORK_socket_create (AF_INET, 329 ls = GNUNET_NETWORK_socket_create(AF_INET,
334 SOCK_DGRAM, 330 SOCK_DGRAM,
335 0); 331 0);
336 if (NULL == ls) 332 if (NULL == ls)
337 return NULL; 333 return NULL;
338 if (GNUNET_OK != 334 if (GNUNET_OK !=
339 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4, 335 GNUNET_NETWORK_socket_bind(ls, (const struct sockaddr *)&sa4,
340 sizeof (sa4))) 336 sizeof(sa4)))
341 { 337 {
342 eno = errno; 338 eno = errno;
343 GNUNET_NETWORK_socket_close (ls); 339 GNUNET_NETWORK_socket_close(ls);
344 errno = eno; 340 errno = eno;
345 return NULL; 341 return NULL;
346 } 342 }
347 return ls; 343 return ls;
348} 344}
349 345
350 346
351static void 347static void
352request_callback (void *cls, 348request_callback(void *cls,
353 enum GNUNET_NAT_StatusCode result) 349 enum GNUNET_NAT_StatusCode result)
354{ 350{
355 // struct GNUNET_NAT_AutoHandle *ah = cls; 351 // struct GNUNET_NAT_AutoHandle *ah = cls;
356 352
357 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 353 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
358 "Request callback: stop and quit\n"); 354 "Request callback: stop and quit\n");
359 stop_stun (); 355 stop_stun();
360 356
361 // next_phase (ah); FIXME this always will be NULL, as called in test_stun() 357 // next_phase (ah); FIXME this always will be NULL, as called in test_stun()
362} 358}
@@ -371,22 +367,22 @@ request_callback (void *cls,
371 * @param emsg NULL on success, otherwise an error message 367 * @param emsg NULL on success, otherwise an error message
372 */ 368 */
373static void 369static void
374result_callback (void *cls, 370result_callback(void *cls,
375 enum GNUNET_NAT_StatusCode ret) 371 enum GNUNET_NAT_StatusCode ret)
376{ 372{
377 struct GNUNET_NAT_AutoHandle *ah = cls; 373 struct GNUNET_NAT_AutoHandle *ah = cls;
378 374
379 if (GNUNET_NAT_ERROR_SUCCESS == ret) 375 if (GNUNET_NAT_ERROR_SUCCESS == ret)
380 GNUNET_NAT_test_stop (ah->tst); 376 GNUNET_NAT_test_stop(ah->tst);
381 ah->tst = NULL; 377 ah->tst = NULL;
382 ah->ret = ret; 378 ah->ret = ret;
383 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 379 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
384 GNUNET_NAT_ERROR_SUCCESS == ret 380 GNUNET_NAT_ERROR_SUCCESS == ret
385 ? _("NAT traversal with ICMP Server succeeded.\n") 381 ? _("NAT traversal with ICMP Server succeeded.\n")
386 : _("NAT traversal with ICMP Server failed.\n")); 382 : _("NAT traversal with ICMP Server failed.\n"));
387 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER", 383 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "ENABLE_ICMP_SERVER",
388 GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES"); 384 GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES");
389 next_phase (ah); 385 next_phase(ah);
390} 386}
391 387
392 388
@@ -396,16 +392,16 @@ result_callback (void *cls,
396 * @param cls the `struct GNUNET_NAT_AutoHandle` 392 * @param cls the `struct GNUNET_NAT_AutoHandle`
397 */ 393 */
398static void 394static void
399reversal_test (void *cls) 395reversal_test(void *cls)
400{ 396{
401 struct GNUNET_NAT_AutoHandle *ah = cls; 397 struct GNUNET_NAT_AutoHandle *ah = cls;
402 398
403 ah->task = NULL; 399 ah->task = NULL;
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 400 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
405 _("Testing connection reversal with ICMP server.\n")); 401 _("Testing connection reversal with ICMP server.\n"));
406 GNUNET_RESOLVER_connect (ah->cfg); 402 GNUNET_RESOLVER_connect(ah->cfg);
407 ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT, 403 ah->tst = GNUNET_NAT_test_start(ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
408 &result_callback, ah); 404 &result_callback, ah);
409} 405}
410 406
411 407
@@ -418,9 +414,9 @@ reversal_test (void *cls)
418 * @param emsg NULL on success, otherwise an error message 414 * @param emsg NULL on success, otherwise an error message
419 */ 415 */
420static void 416static void
421set_external_ipv4 (void *cls, 417set_external_ipv4(void *cls,
422 const struct in_addr *addr, 418 const struct in_addr *addr,
423 enum GNUNET_NAT_StatusCode ret) 419 enum GNUNET_NAT_StatusCode ret)
424{ 420{
425 struct GNUNET_NAT_AutoHandle *ah = cls; 421 struct GNUNET_NAT_AutoHandle *ah = cls;
426 char buf[INET_ADDRSTRLEN]; 422 char buf[INET_ADDRSTRLEN];
@@ -428,35 +424,35 @@ set_external_ipv4 (void *cls,
428 ah->eh = NULL; 424 ah->eh = NULL;
429 ah->ret = ret; 425 ah->ret = ret;
430 if (GNUNET_NAT_ERROR_SUCCESS != ret) 426 if (GNUNET_NAT_ERROR_SUCCESS != ret)
431 { 427 {
432 next_phase (ah); 428 next_phase(ah);
433 return; 429 return;
434 } 430 }
435 /* enable 'behind nat' */ 431 /* enable 'behind nat' */
436 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 432 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
437 _("Detected external IP `%s'\n"), 433 _("Detected external IP `%s'\n"),
438 inet_ntop (AF_INET, 434 inet_ntop(AF_INET,
439 addr, 435 addr,
440 buf, 436 buf,
441 sizeof (buf))); 437 sizeof(buf)));
442 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES"); 438 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "BEHIND_NAT", "YES");
443 439
444 /* set external IP address */ 440 /* set external IP address */
445 if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf))) 441 if (NULL == inet_ntop(AF_INET, addr, buf, sizeof(buf)))
446 { 442 {
447 GNUNET_break (0); 443 GNUNET_break(0);
448 /* actually, this should never happen, as the caller already executed just 444 /* actually, this should never happen, as the caller already executed just
449 * this check, but for consistency (eg: future changes in the caller) 445 * this check, but for consistency (eg: future changes in the caller)
450 * we still need to report this error... 446 * we still need to report this error...
451 */ 447 */
452 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; 448 ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
453 next_phase (ah); 449 next_phase(ah);
454 return; 450 return;
455 } 451 }
456 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS", 452 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "EXTERNAL_ADDRESS",
457 buf); 453 buf);
458 ah->upnp_set_external_address = GNUNET_YES; 454 ah->upnp_set_external_address = GNUNET_YES;
459 next_phase (ah); 455 next_phase(ah);
460} 456}
461 457
462 458
@@ -466,15 +462,15 @@ set_external_ipv4 (void *cls,
466 * @param ah auto setup context 462 * @param ah auto setup context
467 */ 463 */
468static void 464static void
469test_external_ip (struct GNUNET_NAT_AutoHandle *ah) 465test_external_ip(struct GNUNET_NAT_AutoHandle *ah)
470{ 466{
471 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret) 467 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
472 next_phase (ah); 468 next_phase(ah);
473 469
474 // FIXME: CPS? 470 // FIXME: CPS?
475 /* try to detect external IP */ 471 /* try to detect external IP */
476 ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT, 472 ah->eh = GNUNET_NAT_mini_get_external_ipv4(TIMEOUT,
477 &set_external_ipv4, ah); 473 &set_external_ipv4, ah);
478} 474}
479 475
480 476
@@ -484,54 +480,53 @@ test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
484 * @param ah auto setup context 480 * @param ah auto setup context
485 */ 481 */
486static void 482static void
487test_stun (struct GNUNET_NAT_AutoHandle *ah) 483test_stun(struct GNUNET_NAT_AutoHandle *ah)
488{ 484{
489 485 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
490 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
491 486
492 /* Get port from the configuration */ 487 /* Get port from the configuration */
493 if (GNUNET_OK != 488 if (GNUNET_OK !=
494 GNUNET_CONFIGURATION_get_value_number (ah->cfg, 489 GNUNET_CONFIGURATION_get_value_number(ah->cfg,
495 "transport-udp", 490 "transport-udp",
496 "PORT", 491 "PORT",
497 &port)) 492 &port))
498 { 493 {
499 port = 2086; 494 port = 2086;
500 } 495 }
501 496
502 //Lets create the socket 497 //Lets create the socket
503 lsock4 = bind_v4 (); 498 lsock4 = bind_v4();
504 if (NULL == lsock4) 499 if (NULL == lsock4)
505 { 500 {
506 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); 501 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "bind");
507 next_phase(ah); 502 next_phase(ah);
508 return; 503 return;
509 } 504 }
510 else 505 else
511 { 506 {
512 //Lets call our function now when it accepts 507 //Lets call our function now when it accepts
513 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT, 508 ltask4 = GNUNET_SCHEDULER_add_read_net(NAT_SERVER_TIMEOUT,
514 lsock4, 509 lsock4,
515 &do_udp_read, 510 &do_udp_read,
516 ah); 511 ah);
517 } 512 }
518 513
519 514
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 515 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
521 "STUN service listens on port %u\n", 516 "STUN service listens on port %u\n",
522 (unsigned int) port); 517 (unsigned int)port);
523 if (GNUNET_NO == 518 if (GNUNET_NO ==
524 GNUNET_NAT_stun_make_request (stun_server, 519 GNUNET_NAT_stun_make_request(stun_server,
525 stun_port, 520 stun_port,
526 lsock4, 521 lsock4,
527 &request_callback, 522 &request_callback,
528 NULL)) 523 NULL))
529 { 524 {
530 /*An error happened*/ 525 /*An error happened*/
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n"); 526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n");
532 stop_stun (); 527 stop_stun();
533 next_phase (ah); 528 next_phase(ah);
534 } 529 }
535} 530}
536 531
537 532
@@ -549,57 +544,57 @@ test_stun (struct GNUNET_NAT_AutoHandle *ah)
549 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort 544 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
550 */ 545 */
551static int 546static int
552process_if (void *cls, 547process_if(void *cls,
553 const char *name, 548 const char *name,
554 int isDefault, 549 int isDefault,
555 const struct sockaddr *addr, 550 const struct sockaddr *addr,
556 const struct sockaddr *broadcast_addr, 551 const struct sockaddr *broadcast_addr,
557 const struct sockaddr *netmask, 552 const struct sockaddr *netmask,
558 socklen_t addrlen) 553 socklen_t addrlen)
559{ 554{
560 struct GNUNET_NAT_AutoHandle *ah = cls; 555 struct GNUNET_NAT_AutoHandle *ah = cls;
561 const struct sockaddr_in *in; 556 const struct sockaddr_in *in;
562 char buf[INET_ADDRSTRLEN]; 557 char buf[INET_ADDRSTRLEN];
563 558
564 559
565 if ( (sizeof (struct sockaddr_in6) == addrlen) && 560 if ((sizeof(struct sockaddr_in6) == addrlen) &&
566 (0 != GNUNET_memcmp (&in6addr_loopback, &((const struct sockaddr_in6 *) addr)->sin6_addr)) && 561 (0 != GNUNET_memcmp(&in6addr_loopback, &((const struct sockaddr_in6 *)addr)->sin6_addr)) &&
567 (! IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *) addr)->sin6_addr)) ) 562 (!IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *)addr)->sin6_addr)))
568 { 563 {
569 ah->have_v6 = GNUNET_YES; 564 ah->have_v6 = GNUNET_YES;
570 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 565 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
571 _("This system has a global IPv6 address, setting IPv6 to supported.\n")); 566 _("This system has a global IPv6 address, setting IPv6 to supported.\n"));
572 567
568 return GNUNET_OK;
569 }
570 if (addrlen != sizeof(struct sockaddr_in))
573 return GNUNET_OK; 571 return GNUNET_OK;
574 } 572 in = (const struct sockaddr_in *)addr;
575 if (addrlen != sizeof (struct sockaddr_in))
576 return GNUNET_OK;
577 in = (const struct sockaddr_in *) addr;
578 573
579 574
580 /* set internal IP address */ 575 /* set internal IP address */
581 if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf))) 576 if (NULL == inet_ntop(AF_INET, &in->sin_addr, buf, sizeof(buf)))
582 { 577 {
583 GNUNET_break (0); 578 GNUNET_break(0);
584 return GNUNET_OK; 579 return GNUNET_OK;
585 } 580 }
586 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS", 581 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "INTERNAL_ADDRESS",
587 buf); 582 buf);
588 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 583 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
589 _("Detected internal network address `%s'.\n"), 584 _("Detected internal network address `%s'.\n"),
590 buf); 585 buf);
591 586
592 587
593 ah->ret = GNUNET_NAT_ERROR_SUCCESS; 588 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
594 589
595 /* Check if our internal IP is the same as the External detect by STUN*/ 590 /* Check if our internal IP is the same as the External detect by STUN*/
596 if(ah->stun_ip && (strcmp(buf, ah->stun_ip) == 0) ) 591 if (ah->stun_ip && (strcmp(buf, ah->stun_ip) == 0))
597 { 592 {
598 ah->internal_ip_is_public = GNUNET_YES; 593 ah->internal_ip_is_public = GNUNET_YES;
599 GNUNET_log (GNUNET_ERROR_TYPE_INFO,"A internal IP is the sameas the external"); 594 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "A internal IP is the sameas the external");
600 /* No need to continue*/ 595 /* No need to continue*/
601 return GNUNET_SYSERR; 596 return GNUNET_SYSERR;
602 } 597 }
603 598
604 /* no need to continue iteration if we found the default */ 599 /* no need to continue iteration if we found the default */
605 if (!isDefault) 600 if (!isDefault)
@@ -615,15 +610,15 @@ process_if (void *cls,
615 * @param ah auto setup context 610 * @param ah auto setup context
616 */ 611 */
617static void 612static void
618test_local_ip (struct GNUNET_NAT_AutoHandle *ah) 613test_local_ip(struct GNUNET_NAT_AutoHandle *ah)
619{ 614{
620 ah->have_v6 = GNUNET_NO; 615 ah->have_v6 = GNUNET_NO;
621 ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP 616 ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP
622 GNUNET_OS_network_interfaces_list (&process_if, ah); 617 GNUNET_OS_network_interfaces_list(&process_if, ah);
623 618
624 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6", 619 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "DISABLEV6",
625 (GNUNET_YES == ah->have_v6) ? "NO" : "YES"); 620 (GNUNET_YES == ah->have_v6) ? "NO" : "YES");
626 next_phase (ah); 621 next_phase(ah);
627} 622}
628 623
629 624
@@ -635,15 +630,15 @@ test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
635 * @param error error code 630 * @param error error code
636 */ 631 */
637static void 632static void
638mq_error_handler (void *cls, 633mq_error_handler(void *cls,
639 enum GNUNET_MQ_Error error) 634 enum GNUNET_MQ_Error error)
640{ 635{
641 struct GNUNET_NAT_AutoHandle *ah = cls; 636 struct GNUNET_NAT_AutoHandle *ah = cls;
642 637
643 GNUNET_MQ_destroy (ah->mq); 638 GNUNET_MQ_destroy(ah->mq);
644 ah->mq = NULL; 639 ah->mq = NULL;
645 /* wait a bit first? */ 640 /* wait a bit first? */
646 next_phase (ah); 641 next_phase(ah);
647} 642}
648 643
649 644
@@ -653,50 +648,50 @@ mq_error_handler (void *cls,
653 * @param ah auto setup context 648 * @param ah auto setup context
654 */ 649 */
655static void 650static void
656test_nat_punched (struct GNUNET_NAT_AutoHandle *ah) 651test_nat_punched(struct GNUNET_NAT_AutoHandle *ah)
657{ 652{
658 struct GNUNET_NAT_TestMessage *msg; 653 struct GNUNET_NAT_TestMessage *msg;
659 struct GNUNET_MQ_Envelope *env; 654 struct GNUNET_MQ_Envelope *env;
660 655
661 if (! ah->stun_ip) 656 if (!ah->stun_ip)
662 { 657 {
663 LOG (GNUNET_ERROR_TYPE_INFO, 658 LOG(GNUNET_ERROR_TYPE_INFO,
664 "We don't have a STUN IP"); 659 "We don't have a STUN IP");
665 next_phase (ah); 660 next_phase(ah);
666 return; 661 return;
667 } 662 }
668 663
669 LOG (GNUNET_ERROR_TYPE_INFO, 664 LOG(GNUNET_ERROR_TYPE_INFO,
670 "Asking gnunet-nat-server to connect to `%s'\n", 665 "Asking gnunet-nat-server to connect to `%s'\n",
671 ah->stun_ip); 666 ah->stun_ip);
672 ah->mq = GNUNET_CLIENT_connect (ah->cfg, 667 ah->mq = GNUNET_CLIENT_connect(ah->cfg,
673 "gnunet-nat-server", 668 "gnunet-nat-server",
674 NULL, 669 NULL,
675 &mq_error_handler, 670 &mq_error_handler,
676 ah); 671 ah);
677 if (NULL == ah->mq) 672 if (NULL == ah->mq)
678 { 673 {
679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 674 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
680 _("Failed to connect to `gnunet-nat-server'\n")); 675 _("Failed to connect to `gnunet-nat-server'\n"));
681 next_phase (ah); 676 next_phase(ah);
682 return; 677 return;
683 } 678 }
684 env = GNUNET_MQ_msg (msg, 679 env = GNUNET_MQ_msg(msg,
685 GNUNET_MESSAGE_TYPE_NAT_TEST); 680 GNUNET_MESSAGE_TYPE_NAT_TEST);
686 msg->dst_ipv4 = inet_addr (ah->stun_ip); 681 msg->dst_ipv4 = inet_addr(ah->stun_ip);
687 msg->dport = htons (ah->stun_port); 682 msg->dport = htons(ah->stun_port);
688 msg->data = port; 683 msg->data = port;
689 msg->is_tcp = htonl ((uint32_t) GNUNET_NO); 684 msg->is_tcp = htonl((uint32_t)GNUNET_NO);
690 GNUNET_MQ_send (ah->mq, 685 GNUNET_MQ_send(ah->mq,
691 env); 686 env);
692 if (NULL != ltask4) 687 if (NULL != ltask4)
693 { 688 {
694 GNUNET_SCHEDULER_cancel (ltask4); 689 GNUNET_SCHEDULER_cancel(ltask4);
695 ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT, 690 ltask4 = GNUNET_SCHEDULER_add_read_net(NAT_SERVER_TIMEOUT,
696 lsock4, 691 lsock4,
697 &do_udp_read, 692 &do_udp_read,
698 ah); 693 ah);
699 } 694 }
700} 695}
701 696
702 697
@@ -706,26 +701,24 @@ test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
706 * @param ah auto setup context 701 * @param ah auto setup context
707 */ 702 */
708static void 703static void
709test_upnpc (struct GNUNET_NAT_AutoHandle *ah) 704test_upnpc(struct GNUNET_NAT_AutoHandle *ah)
710{ 705{
711
712 int have_upnpc; 706 int have_upnpc;
713 707
714 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret) 708 if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
715 next_phase (ah); 709 next_phase(ah);
716 710
717 // test if upnpc is available 711 // test if upnpc is available
718 have_upnpc = (GNUNET_SYSERR != 712 have_upnpc = (GNUNET_SYSERR !=
719 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)); 713 GNUNET_OS_check_helper_binary("upnpc", GNUNET_NO, NULL));
720 //FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP 714 //FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP
721 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 715 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
722 (have_upnpc) 716 (have_upnpc)
723 ? _("upnpc found, enabling its use\n") 717 ? _("upnpc found, enabling its use\n")
724 : _("upnpc not found\n")); 718 : _("upnpc not found\n"));
725 GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_UPNP", 719 GNUNET_CONFIGURATION_set_value_string(ah->cfg, "nat", "ENABLE_UPNP",
726 (GNUNET_YES == have_upnpc) ? "YES" : "NO"); 720 (GNUNET_YES == have_upnpc) ? "YES" : "NO");
727 next_phase (ah); 721 next_phase(ah);
728
729} 722}
730 723
731 724
@@ -735,66 +728,66 @@ test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
735 * @param ah auto setup context 728 * @param ah auto setup context
736 */ 729 */
737static void 730static void
738test_icmp_server (struct GNUNET_NAT_AutoHandle *ah) 731test_icmp_server(struct GNUNET_NAT_AutoHandle *ah)
739{ 732{
740
741 int ext_ip; 733 int ext_ip;
742 int nated; 734 int nated;
743 int binary; 735 int binary;
744 char *tmp; 736 char *tmp;
745 char *helper; 737 char *helper;
738
746 ext_ip = GNUNET_NO; 739 ext_ip = GNUNET_NO;
747 nated = GNUNET_NO; 740 nated = GNUNET_NO;
748 binary = GNUNET_NO; 741 binary = GNUNET_NO;
749 742
750 tmp = NULL; 743 tmp = NULL;
751 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server"); 744 helper = GNUNET_OS_get_libexec_binary_path("gnunet-helper-nat-server");
752 if ( (GNUNET_OK == 745 if ((GNUNET_OK ==
753 GNUNET_CONFIGURATION_get_value_string (ah->cfg, 746 GNUNET_CONFIGURATION_get_value_string(ah->cfg,
754 "nat", 747 "nat",
755 "EXTERNAL_ADDRESS", 748 "EXTERNAL_ADDRESS",
756 &tmp)) && 749 &tmp)) &&
757 (0 < strlen (tmp)) ) 750 (0 < strlen(tmp)))
758 { 751 {
759 ext_ip = GNUNET_OK; 752 ext_ip = GNUNET_OK;
760 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 753 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
761 _("test_icmp_server not possible, as we have no public IPv4 address\n")); 754 _("test_icmp_server not possible, as we have no public IPv4 address\n"));
762 } 755 }
763 else 756 else
764 goto err; 757 goto err;
765 758
766 if (GNUNET_YES == 759 if (GNUNET_YES ==
767 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, 760 GNUNET_CONFIGURATION_get_value_yesno(ah->cfg,
768 "nat", 761 "nat",
769 "BEHIND_NAT")) 762 "BEHIND_NAT"))
770 { 763 {
771 nated = GNUNET_YES; 764 nated = GNUNET_YES;
772 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 765 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
773 _("test_icmp_server not possible, as we are not behind NAT\n")); 766 _("test_icmp_server not possible, as we are not behind NAT\n"));
774 } 767 }
775 else 768 else
776 goto err; 769 goto err;
777 770
778 if (GNUNET_YES == 771 if (GNUNET_YES ==
779 GNUNET_OS_check_helper_binary (helper, 772 GNUNET_OS_check_helper_binary(helper,
780 GNUNET_YES, 773 GNUNET_YES,
781 "-d 127.0.0.1" )) 774 "-d 127.0.0.1"))
782 { 775 {
783 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing 776 binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
784 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 777 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
785 _("No working gnunet-helper-nat-server found\n")); 778 _("No working gnunet-helper-nat-server found\n"));
786 } 779 }
787err: 780err:
788 GNUNET_free_non_null (tmp); 781 GNUNET_free_non_null(tmp);
789 GNUNET_free (helper); 782 GNUNET_free(helper);
790 783
791 if ( (GNUNET_OK == ext_ip) && 784 if ((GNUNET_OK == ext_ip) &&
792 (GNUNET_YES == nated) && 785 (GNUNET_YES == nated) &&
793 (GNUNET_OK == binary) ) 786 (GNUNET_OK == binary))
794 ah->task = GNUNET_SCHEDULER_add_now (&reversal_test, 787 ah->task = GNUNET_SCHEDULER_add_now(&reversal_test,
795 ah); 788 ah);
796 else 789 else
797 next_phase (ah); 790 next_phase(ah);
798} 791}
799 792
800 793
@@ -804,51 +797,51 @@ err:
804 * @param ah auto setup context 797 * @param ah auto setup context
805 */ 798 */
806static void 799static void
807test_icmp_client (struct GNUNET_NAT_AutoHandle *ah) 800test_icmp_client(struct GNUNET_NAT_AutoHandle *ah)
808{ 801{
809 char *tmp; 802 char *tmp;
810 char *helper; 803 char *helper;
811 804
812 tmp = NULL; 805 tmp = NULL;
813 helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client"); 806 helper = GNUNET_OS_get_libexec_binary_path("gnunet-helper-nat-client");
814 if ( (GNUNET_OK == 807 if ((GNUNET_OK ==
815 GNUNET_CONFIGURATION_get_value_string (ah->cfg, 808 GNUNET_CONFIGURATION_get_value_string(ah->cfg,
816 "nat", 809 "nat",
817 "INTERNAL_ADDRESS", 810 "INTERNAL_ADDRESS",
818 &tmp)) && 811 &tmp)) &&
819 (0 < strlen (tmp)) ) 812 (0 < strlen(tmp)))
820 { 813 {
821 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 814 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
822 _("test_icmp_client not possible, as we have no internal IPv4 address\n")); 815 _("test_icmp_client not possible, as we have no internal IPv4 address\n"));
823 } 816 }
824 else 817 else
825 goto err; 818 goto err;
826 819
827 if (GNUNET_YES != 820 if (GNUNET_YES !=
828 GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, 821 GNUNET_CONFIGURATION_get_value_yesno(ah->cfg,
829 "nat", 822 "nat",
830 "BEHIND_NAT")) 823 "BEHIND_NAT"))
831 { 824 {
832 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 825 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
833 _("test_icmp_server not possible, as we are not behind NAT\n")); 826 _("test_icmp_server not possible, as we are not behind NAT\n"));
834 } 827 }
835 else 828 else
836 goto err; 829 goto err;
837 830
838 if (GNUNET_YES == 831 if (GNUNET_YES ==
839 GNUNET_OS_check_helper_binary (helper, 832 GNUNET_OS_check_helper_binary(helper,
840 GNUNET_YES, 833 GNUNET_YES,
841 "-d 127.0.0.1 127.0.0.2 42")) 834 "-d 127.0.0.1 127.0.0.2 42"))
842 { 835 {
843 // none of these parameters are actually used in privilege testing mode 836 // none of these parameters are actually used in privilege testing mode
844 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 837 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
845 _("No working gnunet-helper-nat-server found\n")); 838 _("No working gnunet-helper-nat-server found\n"));
846 } 839 }
847err: 840err:
848 GNUNET_free_non_null (tmp); 841 GNUNET_free_non_null(tmp);
849 GNUNET_free (helper); 842 GNUNET_free(helper);
850 843
851 next_phase (ah); 844 next_phase(ah);
852} 845}
853 846
854 847
@@ -856,156 +849,161 @@ err:
856 * Run the next phase of the auto test. 849 * Run the next phase of the auto test.
857 */ 850 */
858static void 851static void
859next_phase (struct GNUNET_NAT_AutoHandle *ah) 852next_phase(struct GNUNET_NAT_AutoHandle *ah)
860{ 853{
861 struct GNUNET_CONFIGURATION_Handle *diff; 854 struct GNUNET_CONFIGURATION_Handle *diff;
862 855
863 ah->phase++; 856 ah->phase++;
864 switch (ah->phase) 857 switch (ah->phase)
865 {
866 case AUTO_INIT:
867 GNUNET_assert (0);
868 break;
869 case AUTO_EXTERNAL_IP:
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 "Will run AUTO_EXTERNAL_IP\n");
872 test_external_ip (ah);
873 break;
874 case AUTO_STUN:
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Will run AUTO_STUN\n");
877 test_stun (ah);
878 break;
879 case AUTO_LOCAL_IP:
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Will run AUTO_LOCAL_IP\n");
882 test_local_ip (ah);
883 break;
884 case AUTO_NAT_PUNCHED:
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Will run AUTO_NAT_PUNCHED\n");
887 test_nat_punched (ah);
888 break;
889 case AUTO_UPNPC:
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Will run AUTO_UPNPC\n");
892 test_upnpc (ah);
893 break;
894 case AUTO_ICMP_SERVER:
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Will run AUTO_ICMP_SERVER\n");
897 test_icmp_server (ah);
898 break;
899 case AUTO_ICMP_CLIENT:
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Will run AUTO_ICMP_CLIENT\n");
902 test_icmp_client (ah);
903 break;
904 case AUTO_DONE:
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Done with tests\n");
907 if (!ah->internal_ip_is_public)
908 { 858 {
909 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 859 case AUTO_INIT:
910 "nat", 860 GNUNET_assert(0);
911 "BEHIND_NAT", 861 break;
912 "YES"); 862
913 863 case AUTO_EXTERNAL_IP:
914 if (ah->connected_back) 864 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
915 { 865 "Will run AUTO_EXTERNAL_IP\n");
916 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 866 test_external_ip(ah);
917 "nat", 867 break;
918 "PUNCHED_NAT", 868
919 "YES"); 869 case AUTO_STUN:
920 } 870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
921 else 871 "Will run AUTO_STUN\n");
922 { 872 test_stun(ah);
923 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 873 break;
924 "nat", 874
925 "PUNCHED_NAT", 875 case AUTO_LOCAL_IP:
926 "NO"); 876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
927 } 877 "Will run AUTO_LOCAL_IP\n");
928 878 test_local_ip(ah);
929 if (ah->stun_ip) 879 break;
930 { 880
931 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 881 case AUTO_NAT_PUNCHED:
932 "nat", 882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
933 "EXTERNAL_ADDRESS", 883 "Will run AUTO_NAT_PUNCHED\n");
934 ah->stun_ip); 884 test_nat_punched(ah);
935 if (ah->connected_back) 885 break;
886
887 case AUTO_UPNPC:
888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
889 "Will run AUTO_UPNPC\n");
890 test_upnpc(ah);
891 break;
892
893 case AUTO_ICMP_SERVER:
894 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
895 "Will run AUTO_ICMP_SERVER\n");
896 test_icmp_server(ah);
897 break;
898
899 case AUTO_ICMP_CLIENT:
900 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
901 "Will run AUTO_ICMP_CLIENT\n");
902 test_icmp_client(ah);
903 break;
904
905 case AUTO_DONE:
906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
907 "Done with tests\n");
908 if (!ah->internal_ip_is_public)
936 { 909 {
937 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT; 910 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
938 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 911 "nat",
939 "nat", 912 "BEHIND_NAT",
940 "USE_STUN", 913 "YES");
941 "YES"); 914
915 if (ah->connected_back)
916 {
917 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
918 "nat",
919 "PUNCHED_NAT",
920 "YES");
921 }
922 else
923 {
924 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
925 "nat",
926 "PUNCHED_NAT",
927 "NO");
928 }
929
930 if (ah->stun_ip)
931 {
932 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
933 "nat",
934 "EXTERNAL_ADDRESS",
935 ah->stun_ip);
936 if (ah->connected_back)
937 {
938 ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT;
939 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
940 "nat",
941 "USE_STUN",
942 "YES");
943 }
944 else
945 {
946 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
947 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
948 "nat",
949 "USE_STUN",
950 "NO");
951 }
952 }
953 if (0 != ah->stun_port)
954 {
955 GNUNET_CONFIGURATION_set_value_number(ah->cfg,
956 "transport-udp",
957 "ADVERTISED_PORT",
958 ah->stun_port);
959 }
942 } 960 }
943 else
944 {
945 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
946 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
947 "nat",
948 "USE_STUN",
949 "NO");
950 }
951
952 }
953 if (0 != ah->stun_port)
954 {
955 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
956 "transport-udp",
957 "ADVERTISED_PORT",
958 ah->stun_port);
959 }
960
961 }
962 else
963 {
964 //The internal IP is the same as public, but we didn't got a incoming connection
965 if (ah->connected_back)
966 {
967 ah->type = GNUNET_NAT_TYPE_NO_NAT;
968 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
969 "nat",
970 "BEHIND_NAT",
971 "NO");
972 }
973 else 961 else
974 {
975 GNUNET_CONFIGURATION_set_value_string (ah->cfg,
976 "nat",
977 "BEHIND_NAT",
978 "YES");
979 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
980 if (ah->stun_ip)
981 { 962 {
982 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 963 //The internal IP is the same as public, but we didn't got a incoming connection
983 "nat", 964 if (ah->connected_back)
984 "EXTERNAL_ADDRESS", 965 {
985 ah->stun_ip); 966 ah->type = GNUNET_NAT_TYPE_NO_NAT;
967 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
968 "nat",
969 "BEHIND_NAT",
970 "NO");
971 }
972 else
973 {
974 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
975 "nat",
976 "BEHIND_NAT",
977 "YES");
978 ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
979 if (ah->stun_ip)
980 {
981 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
982 "nat",
983 "EXTERNAL_ADDRESS",
984 ah->stun_ip);
985 }
986 if (0 != ah->stun_port)
987 {
988 GNUNET_CONFIGURATION_set_value_number(ah->cfg,
989 "transport-udp",
990 "ADVERTISED_PORT",
991 ah->stun_port);
992 }
993 }
986 } 994 }
987 if (0 != ah->stun_port)
988 {
989 GNUNET_CONFIGURATION_set_value_number (ah->cfg,
990 "transport-udp",
991 "ADVERTISED_PORT",
992 ah->stun_port);
993 995
994 } 996 diff = GNUNET_CONFIGURATION_get_diff(ah->initial_cfg,
995 } 997 ah->cfg);
996 }
997
998 diff = GNUNET_CONFIGURATION_get_diff (ah->initial_cfg,
999 ah->cfg);
1000 998
1001 999
1002 ah->fin_cb (ah->fin_cb_cls, 1000 ah->fin_cb(ah->fin_cb_cls,
1003 diff, 1001 diff,
1004 ah->ret, 1002 ah->ret,
1005 ah->type); 1003 ah->type);
1006 GNUNET_CONFIGURATION_destroy (diff); 1004 GNUNET_CONFIGURATION_destroy(diff);
1007 GNUNET_NAT_autoconfig_cancel (ah); 1005 GNUNET_NAT_autoconfig_cancel(ah);
1008 } 1006 }
1009} 1007}
1010 1008
1011 1009
@@ -1019,26 +1017,26 @@ next_phase (struct GNUNET_NAT_AutoHandle *ah)
1019 * @return handle to cancel operation 1017 * @return handle to cancel operation
1020 */ 1018 */
1021struct GNUNET_NAT_AutoHandle * 1019struct GNUNET_NAT_AutoHandle *
1022GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 1020GNUNET_NAT_autoconfig_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
1023 GNUNET_NAT_AutoResultCallback cb, 1021 GNUNET_NAT_AutoResultCallback cb,
1024 void *cb_cls) 1022 void *cb_cls)
1025{ 1023{
1026 struct GNUNET_NAT_AutoHandle *ah; 1024 struct GNUNET_NAT_AutoHandle *ah;
1027 1025
1028 ah = GNUNET_new (struct GNUNET_NAT_AutoHandle); 1026 ah = GNUNET_new(struct GNUNET_NAT_AutoHandle);
1029 ah->fin_cb = cb; 1027 ah->fin_cb = cb;
1030 ah->fin_cb_cls = cb_cls; 1028 ah->fin_cb_cls = cb_cls;
1031 ah->ret = GNUNET_NAT_ERROR_SUCCESS; 1029 ah->ret = GNUNET_NAT_ERROR_SUCCESS;
1032 ah->cfg = GNUNET_CONFIGURATION_dup (cfg); 1030 ah->cfg = GNUNET_CONFIGURATION_dup(cfg);
1033 ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg); 1031 ah->initial_cfg = GNUNET_CONFIGURATION_dup(cfg);
1034 1032
1035 /* never use loopback addresses if user wanted autoconfiguration */ 1033 /* never use loopback addresses if user wanted autoconfiguration */
1036 GNUNET_CONFIGURATION_set_value_string (ah->cfg, 1034 GNUNET_CONFIGURATION_set_value_string(ah->cfg,
1037 "nat", 1035 "nat",
1038 "USE_LOCALADDR", 1036 "USE_LOCALADDR",
1039 "NO"); 1037 "NO");
1040 1038
1041 next_phase (ah); 1039 next_phase(ah);
1042 return ah; 1040 return ah;
1043} 1041}
1044 1042
@@ -1049,31 +1047,31 @@ GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1049 * @param ah handle for operation to abort 1047 * @param ah handle for operation to abort
1050 */ 1048 */
1051void 1049void
1052GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah) 1050GNUNET_NAT_autoconfig_cancel(struct GNUNET_NAT_AutoHandle *ah)
1053{ 1051{
1054 if (NULL != ah->tst) 1052 if (NULL != ah->tst)
1055 { 1053 {
1056 GNUNET_NAT_test_stop (ah->tst); 1054 GNUNET_NAT_test_stop(ah->tst);
1057 ah->tst = NULL; 1055 ah->tst = NULL;
1058 } 1056 }
1059 if (NULL != ah->eh) 1057 if (NULL != ah->eh)
1060 { 1058 {
1061 GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh); 1059 GNUNET_NAT_mini_get_external_ipv4_cancel(ah->eh);
1062 ah->eh = NULL; 1060 ah->eh = NULL;
1063 } 1061 }
1064 if (NULL != ah->mq) 1062 if (NULL != ah->mq)
1065 { 1063 {
1066 GNUNET_MQ_destroy (ah->mq); 1064 GNUNET_MQ_destroy(ah->mq);
1067 ah->mq = NULL; 1065 ah->mq = NULL;
1068 } 1066 }
1069 if (NULL != ah->task) 1067 if (NULL != ah->task)
1070 { 1068 {
1071 GNUNET_SCHEDULER_cancel (ah->task); 1069 GNUNET_SCHEDULER_cancel(ah->task);
1072 ah->task = NULL; 1070 ah->task = NULL;
1073 } 1071 }
1074 GNUNET_CONFIGURATION_destroy (ah->cfg); 1072 GNUNET_CONFIGURATION_destroy(ah->cfg);
1075 GNUNET_CONFIGURATION_destroy (ah->initial_cfg); 1073 GNUNET_CONFIGURATION_destroy(ah->initial_cfg);
1076 GNUNET_free (ah); 1074 GNUNET_free(ah);
1077} 1075}
1078 1076
1079 1077