aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_tcp.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2014-01-13 16:59:57 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2014-01-13 16:59:57 +0000
commit1257490cb630ade315a196a724d0233e662afb2c (patch)
tree0d9c01593a1aa73d8f6490127b640d67da69b54c /src/transport/plugin_transport_tcp.c
parent35d79cb1941c9f9607195b8760d8a14d836e6397 (diff)
downloadgnunet-1257490cb630ade315a196a724d0233e662afb2c.tar.gz
gnunet-1257490cb630ade315a196a724d0233e662afb2c.zip
implementing 0003268 to inbound information in HELLO addresses
All transport plugin functions are modified to use HELLO addresses instead of peer,address,address_length All plugins are modified to use HELLO addresses internally This commit can break transport functionality: core tests on my system still pass, but transport tests may still fail, errors messages may occurs or crashs Will be fixed asap
Diffstat (limited to 'src/transport/plugin_transport_tcp.c')
-rw-r--r--src/transport/plugin_transport_tcp.c1505
1 files changed, 648 insertions, 857 deletions
diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c
index a969bdd22..a2fbc7c8a 100644
--- a/src/transport/plugin_transport_tcp.c
+++ b/src/transport/plugin_transport_tcp.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2002--2013 Christian Grothoff (and other contributing authors) 3 (C) 2002--2013 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your 7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
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 General Public License for more details. 13 General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the 16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19 */
20/** 20/**
21 * @file transport/plugin_transport_tcp.c 21 * @file transport/plugin_transport_tcp.c
22 * @brief Implementation of the TCP transport service 22 * @brief Implementation of the TCP transport service
@@ -47,7 +47,6 @@
47 */ 47 */
48#define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) 48#define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
49 49
50
51GNUNET_NETWORK_STRUCT_BEGIN 50GNUNET_NETWORK_STRUCT_BEGIN
52 51
53/** 52/**
@@ -72,7 +71,6 @@ struct WelcomeMessage
72 71
73}; 72};
74 73
75
76/** 74/**
77 * Basically a WELCOME message, but with the purpose 75 * Basically a WELCOME message, but with the purpose
78 * of giving the waiting peer a client handle to use 76 * of giving the waiting peer a client handle to use
@@ -129,7 +127,6 @@ struct TCPProbeContext
129 struct Plugin *plugin; 127 struct Plugin *plugin;
130}; 128};
131 129
132
133GNUNET_NETWORK_STRUCT_BEGIN 130GNUNET_NETWORK_STRUCT_BEGIN
134 131
135/** 132/**
@@ -154,7 +151,6 @@ struct IPv4TcpAddress
154 151
155}; 152};
156 153
157
158/** 154/**
159 * Network format for IPv6 addresses. 155 * Network format for IPv6 addresses.
160 */ 156 */
@@ -183,7 +179,6 @@ GNUNET_NETWORK_STRUCT_END
183 */ 179 */
184struct Plugin; 180struct Plugin;
185 181
186
187/** 182/**
188 * Information kept for each message that is yet to 183 * Information kept for each message that is yet to
189 * be transmitted. 184 * be transmitted.
@@ -232,7 +227,6 @@ struct PendingMessage
232 227
233}; 228};
234 229
235
236/** 230/**
237 * Session handle for TCP connections. 231 * Session handle for TCP connections.
238 */ 232 */
@@ -291,19 +285,19 @@ struct Session
291 */ 285 */
292 GNUNET_SCHEDULER_TaskIdentifier timeout_task; 286 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
293 287
288 struct GNUNET_HELLO_Address *address;
289
294 /** 290 /**
295 * Address of the other peer (either based on our 'connect' 291 * Address of the other peer (either based on our 'connect'
296 * call or on our 'accept' call). 292 * call or on our 'accept' call).
297 * 293 *
298 * struct IPv4TcpAddress or struct IPv6TcpAddress 294 * struct IPv4TcpAddress or struct IPv6TcpAddress
299 */ 295 */
300 void *addr; 296 //void *addr;
301
302 /** 297 /**
303 * Length of @e addr. 298 * Length of @e addr.
304 */ 299 */
305 size_t addrlen; 300 //size_t addrlen;
306
307 /** 301 /**
308 * Last activity on this connection. Used to select preferred 302 * Last activity on this connection. Used to select preferred
309 * connection. 303 * connection.
@@ -316,11 +310,6 @@ struct Session
316 int expecting_welcome; 310 int expecting_welcome;
317 311
318 /** 312 /**
319 * Was this a connection that was inbound (we accepted)? (#GNUNET_YES/#GNUNET_NO)
320 */
321 int inbound;
322
323 /**
324 * Was this session created using NAT traversal? 313 * Was this session created using NAT traversal?
325 */ 314 */
326 int is_nat; 315 int is_nat;
@@ -331,7 +320,6 @@ struct Session
331 enum GNUNET_ATS_Network_Type ats_address_network_type; 320 enum GNUNET_ATS_Network_Type ats_address_network_type;
332}; 321};
333 322
334
335/** 323/**
336 * Encapsulation of all of the state of the plugin. 324 * Encapsulation of all of the state of the plugin.
337 */ 325 */
@@ -421,7 +409,6 @@ struct Plugin
421 409
422}; 410};
423 411
424
425/** 412/**
426 * Function called for a quick conversion of the binary address to 413 * Function called for a quick conversion of the binary address to
427 * a numeric address. Note that the caller must not free the 414 * a numeric address. Note that the caller must not free the
@@ -434,10 +421,7 @@ struct Plugin
434 * @return string representing the same address 421 * @return string representing the same address
435 */ 422 */
436static const char * 423static const char *
437tcp_address_to_string (void *cls, 424tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
438 const void *addr,
439 size_t addrlen);
440
441 425
442/** 426/**
443 * Function to check if an inbound connection is acceptable. 427 * Function to check if an inbound connection is acceptable.
@@ -453,20 +437,19 @@ tcp_address_to_string (void *cls,
453 */ 437 */
454static int 438static int
455plugin_tcp_access_check (void *cls, 439plugin_tcp_access_check (void *cls,
456 const struct GNUNET_CONNECTION_Credentials *ucred, 440 const struct GNUNET_CONNECTION_Credentials *ucred,
457 const struct sockaddr *addr, socklen_t addrlen) 441 const struct sockaddr *addr, socklen_t addrlen)
458{ 442{
459 struct Plugin *plugin = cls; 443 struct Plugin *plugin = cls;
460 LOG (GNUNET_ERROR_TYPE_DEBUG, 444 LOG(GNUNET_ERROR_TYPE_DEBUG,
461 "Accepting new incoming TCP connection from `%s'\n", 445 "Accepting new incoming TCP connection from `%s'\n",
462 GNUNET_a2s (addr, addrlen)); 446 GNUNET_a2s (addr, addrlen));
463 if (plugin->cur_connections >= plugin->max_connections) 447 if (plugin->cur_connections >= plugin->max_connections)
464 return GNUNET_NO; 448 return GNUNET_NO;
465 plugin->cur_connections ++; 449 plugin->cur_connections++;
466 return GNUNET_YES; 450 return GNUNET_YES;
467} 451}
468 452
469
470/** 453/**
471 * Our external IP address/port mapping has changed. 454 * Our external IP address/port mapping has changed.
472 * 455 *
@@ -478,50 +461,51 @@ plugin_tcp_access_check (void *cls,
478 */ 461 */
479static void 462static void
480tcp_nat_port_map_callback (void *cls, int add_remove, 463tcp_nat_port_map_callback (void *cls, int add_remove,
481 const struct sockaddr *addr, 464 const struct sockaddr *addr, socklen_t addrlen)
482 socklen_t addrlen)
483{ 465{
484 struct Plugin *plugin = cls; 466 struct Plugin *plugin = cls;
467 struct GNUNET_HELLO_Address *address;
485 struct IPv4TcpAddress t4; 468 struct IPv4TcpAddress t4;
486 struct IPv6TcpAddress t6; 469 struct IPv6TcpAddress t6;
487 void *arg; 470 void *arg;
488 size_t args; 471 size_t args;
489 472
490 LOG (GNUNET_ERROR_TYPE_INFO, 473 LOG(GNUNET_ERROR_TYPE_INFO, "NAT notification to %s address `%s'\n",
491 "NAT notification to %s address `%s'\n", 474 (GNUNET_YES == add_remove) ? "add" : "remove",
492 (GNUNET_YES == add_remove) ? "add" : "remove", 475 GNUNET_a2s (addr, addrlen));
493 GNUNET_a2s (addr, addrlen));
494 /* convert 'addr' to our internal format */ 476 /* convert 'addr' to our internal format */
495 switch (addr->sa_family) 477 switch (addr->sa_family)
496 { 478 {
497 case AF_INET: 479 case AF_INET:
498 GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); 480 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
499 memset (&t4,0, sizeof (t4)); 481 memset (&t4, 0, sizeof(t4));
500 t4.options = htonl (myoptions); 482 t4.options = htonl (myoptions);
501 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr; 483 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
502 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port; 484 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
503 arg = &t4; 485 arg = &t4;
504 args = sizeof (t4); 486 args = sizeof(t4);
505 break; 487 break;
506 case AF_INET6: 488 case AF_INET6:
507 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); 489 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
508 memset (&t6, 0, sizeof (t6)); 490 memset (&t6, 0, sizeof(t6));
509 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr, 491 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
510 sizeof (struct in6_addr)); 492 sizeof(struct in6_addr));
511 t6.options = htonl (myoptions); 493 t6.options = htonl (myoptions);
512 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port; 494 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
513 arg = &t6; 495 arg = &t6;
514 args = sizeof (t6); 496 args = sizeof(t6);
515 break; 497 break;
516 default: 498 default:
517 GNUNET_break (0); 499 GNUNET_break(0);
518 return; 500 return;
519 } 501 }
520 /* modify our published address list */ 502 /* modify our published address list */
521 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "tcp"); 503 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
504 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
505 plugin->env->notify_address (plugin->env->cls, add_remove, address);
506 GNUNET_HELLO_address_free(address);
522} 507}
523 508
524
525/** 509/**
526 * Function called for a quick conversion of the binary address to 510 * Function called for a quick conversion of the binary address to
527 * a numeric address. Note that the caller must not free the 511 * a numeric address. Note that the caller must not free the
@@ -549,52 +533,44 @@ tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
549 533
550 switch (addrlen) 534 switch (addrlen)
551 { 535 {
552 case sizeof (struct IPv6TcpAddress): 536 case sizeof(struct IPv6TcpAddress):
553 t6 = addr; 537 t6 = addr;
554 af = AF_INET6; 538 af = AF_INET6;
555 port = ntohs (t6->t6_port); 539 port = ntohs (t6->t6_port);
556 options = ntohl (t6->options); 540 options = ntohl (t6->options);
557 memcpy (&a6, &t6->ipv6_addr, sizeof (a6)); 541 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
558 sb = &a6; 542 sb = &a6;
559 break; 543 break;
560 case sizeof (struct IPv4TcpAddress): 544 case sizeof(struct IPv4TcpAddress):
561 t4 = addr; 545 t4 = addr;
562 af = AF_INET; 546 af = AF_INET;
563 port = ntohs (t4->t4_port); 547 port = ntohs (t4->t4_port);
564 options = ntohl (t4->options); 548 options = ntohl (t4->options);
565 memcpy (&a4, &t4->ipv4_addr, sizeof (a4)); 549 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
566 sb = &a4; 550 sb = &a4;
567 break; 551 break;
568 case 0: 552 case 0:
569 { 553 {
570 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s", 554 GNUNET_snprintf (rbuf, sizeof(rbuf), "%s",
571 TRANSPORT_SESSION_INBOUND_STRING); 555 TRANSPORT_SESSION_INBOUND_STRING);
572 return rbuf; 556 return rbuf;
573 } 557 }
574 default: 558 default:
575 LOG (GNUNET_ERROR_TYPE_WARNING, 559 LOG(GNUNET_ERROR_TYPE_WARNING, _("Unexpected address length: %u bytes\n"),
576 _("Unexpected address length: %u bytes\n"), 560 (unsigned int ) addrlen);
577 (unsigned int) addrlen); 561 return NULL ;
578 return NULL;
579 } 562 }
580 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN)) 563 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
581 { 564 {
582 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); 565 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
583 return NULL; 566 return NULL ;
584 } 567 }
585 GNUNET_snprintf (rbuf, 568 GNUNET_snprintf (rbuf, sizeof(rbuf),
586 sizeof (rbuf), 569 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u", PLUGIN_NAME, options,
587 (af == AF_INET6) 570 buf, port);
588 ? "%s.%u.[%s]:%u"
589 : "%s.%u.%s:%u",
590 PLUGIN_NAME,
591 options,
592 buf,
593 port);
594 return rbuf; 571 return rbuf;
595} 572}
596 573
597
598/** 574/**
599 * Function called to convert a string address to 575 * Function called to convert a string address to
600 * a binary address. 576 * a binary address.
@@ -608,10 +584,8 @@ tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
608 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 584 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
609 */ 585 */
610static int 586static int
611tcp_string_to_address (void *cls, 587tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
612 const char *addr, 588 void **buf, size_t *added)
613 uint16_t addrlen,
614 void **buf, size_t *added)
615{ 589{
616 struct sockaddr_storage socket_address; 590 struct sockaddr_storage socket_address;
617 char *address; 591 char *address;
@@ -625,82 +599,81 @@ tcp_string_to_address (void *cls,
625 optionstr = NULL; 599 optionstr = NULL;
626 if ((NULL == addr) || (addrlen == 0)) 600 if ((NULL == addr) || (addrlen == 0))
627 { 601 {
628 GNUNET_break (0); 602 GNUNET_break(0);
629 return GNUNET_SYSERR; 603 return GNUNET_SYSERR;
630 } 604 }
631 if ('\0' != addr[addrlen - 1]) 605 if ('\0' != addr[addrlen - 1])
632 { 606 {
633 GNUNET_break (0); 607 GNUNET_break(0);
634 return GNUNET_SYSERR; 608 return GNUNET_SYSERR;
635 } 609 }
636 if (strlen (addr) != addrlen - 1) 610 if (strlen (addr) != addrlen - 1)
637 { 611 {
638 GNUNET_break (0); 612 GNUNET_break(0);
639 return GNUNET_SYSERR; 613 return GNUNET_SYSERR;
640 } 614 }
641 plugin = GNUNET_strdup (addr); 615 plugin = GNUNET_strdup (addr);
642 optionstr = strchr (plugin, '.'); 616 optionstr = strchr (plugin, '.');
643 if (NULL == optionstr) 617 if (NULL == optionstr)
644 { 618 {
645 GNUNET_break (0); 619 GNUNET_break(0);
646 GNUNET_free (plugin); 620 GNUNET_free(plugin);
647 return GNUNET_SYSERR; 621 return GNUNET_SYSERR;
648 } 622 }
649 optionstr[0] = '\0'; 623 optionstr[0] = '\0';
650 optionstr ++; 624 optionstr++;
651 options = atol (optionstr); 625 options = atol (optionstr);
652 address = strchr (optionstr, '.'); 626 address = strchr (optionstr, '.');
653 if (NULL == address) 627 if (NULL == address)
654 { 628 {
655 GNUNET_break (0); 629 GNUNET_break(0);
656 GNUNET_free (plugin); 630 GNUNET_free(plugin);
657 return GNUNET_SYSERR; 631 return GNUNET_SYSERR;
658 } 632 }
659 address[0] = '\0'; 633 address[0] = '\0';
660 address ++; 634 address++;
661 635
662 if (GNUNET_OK != 636 if (GNUNET_OK
663 GNUNET_STRINGS_to_address_ip (address, strlen (address), 637 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
664 &socket_address)) 638 &socket_address))
665 { 639 {
666 GNUNET_break (0); 640 GNUNET_break(0);
667 GNUNET_free (plugin); 641 GNUNET_free(plugin);
668 return GNUNET_SYSERR; 642 return GNUNET_SYSERR;
669 } 643 }
670 644
671 GNUNET_free (plugin); 645 GNUNET_free(plugin);
672 switch (socket_address.ss_family) 646 switch (socket_address.ss_family)
673 { 647 {
674 case AF_INET: 648 case AF_INET:
675 { 649 {
676 struct IPv4TcpAddress *t4; 650 struct IPv4TcpAddress *t4;
677 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address; 651 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
678 t4 = GNUNET_new (struct IPv4TcpAddress); 652 t4 = GNUNET_new (struct IPv4TcpAddress);
679 t4->options = htonl (options); 653 t4->options = htonl (options);
680 t4->ipv4_addr = in4->sin_addr.s_addr; 654 t4->ipv4_addr = in4->sin_addr.s_addr;
681 t4->t4_port = in4->sin_port; 655 t4->t4_port = in4->sin_port;
682 *buf = t4; 656 *buf = t4;
683 *added = sizeof (struct IPv4TcpAddress); 657 *added = sizeof(struct IPv4TcpAddress);
684 return GNUNET_OK; 658 return GNUNET_OK;
685 } 659 }
686 case AF_INET6: 660 case AF_INET6:
687 { 661 {
688 struct IPv6TcpAddress *t6; 662 struct IPv6TcpAddress *t6;
689 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address; 663 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
690 t6 = GNUNET_new (struct IPv6TcpAddress); 664 t6 = GNUNET_new (struct IPv6TcpAddress);
691 t6->options = htonl (options); 665 t6->options = htonl (options);
692 t6->ipv6_addr = in6->sin6_addr; 666 t6->ipv6_addr = in6->sin6_addr;
693 t6->t6_port = in6->sin6_port; 667 t6->t6_port = in6->sin6_port;
694 *buf = t6; 668 *buf = t6;
695 *added = sizeof (struct IPv6TcpAddress); 669 *added = sizeof(struct IPv6TcpAddress);
696 return GNUNET_OK; 670 return GNUNET_OK;
697 } 671 }
698 default: 672 default:
699 return GNUNET_SYSERR; 673 return GNUNET_SYSERR;
700 } 674 }
701} 675}
702 676
703
704/** 677/**
705 * Closure for #session_lookup_by_client_it(). 678 * Closure for #session_lookup_by_client_it().
706 */ 679 */
@@ -717,11 +690,9 @@ struct SessionClientCtx
717 struct Session *ret; 690 struct Session *ret;
718}; 691};
719 692
720
721static int 693static int
722session_lookup_by_client_it (void *cls, 694session_lookup_by_client_it (void *cls, const struct GNUNET_PeerIdentity *key,
723 const struct GNUNET_PeerIdentity *key, 695 void *value)
724 void *value)
725{ 696{
726 struct SessionClientCtx *sc_ctx = cls; 697 struct SessionClientCtx *sc_ctx = cls;
727 struct Session *s = value; 698 struct Session *s = value;
@@ -734,7 +705,6 @@ session_lookup_by_client_it (void *cls,
734 return GNUNET_YES; 705 return GNUNET_YES;
735} 706}
736 707
737
738/** 708/**
739 * Find the session handle for the given client. 709 * Find the session handle for the given client.
740 * Currently uses both the hashmap and the client 710 * Currently uses both the hashmap and the client
@@ -747,7 +717,7 @@ session_lookup_by_client_it (void *cls,
747 */ 717 */
748static struct Session * 718static struct Session *
749lookup_session_by_client (struct Plugin *plugin, 719lookup_session_by_client (struct Plugin *plugin,
750 struct GNUNET_SERVER_Client *client) 720 struct GNUNET_SERVER_Client *client)
751{ 721{
752 struct Session *ret; 722 struct Session *ret;
753 struct SessionClientCtx sc_ctx; 723 struct SessionClientCtx sc_ctx;
@@ -756,13 +726,12 @@ lookup_session_by_client (struct Plugin *plugin,
756 sc_ctx.client = client; 726 sc_ctx.client = client;
757 sc_ctx.ret = NULL; 727 sc_ctx.ret = NULL;
758 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, 728 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
759 &session_lookup_by_client_it, &sc_ctx); 729 &session_lookup_by_client_it, &sc_ctx);
760 /* check both methods yield the same result */ 730 /* check both methods yield the same result */
761 GNUNET_break (ret == sc_ctx.ret); 731 GNUNET_break(ret == sc_ctx.ret);
762 return sc_ctx.ret; 732 return sc_ctx.ret;
763} 733}
764 734
765
766/** 735/**
767 * Functions with this signature are called whenever we need 736 * Functions with this signature are called whenever we need
768 * to close a session due to a disconnect or failure to 737 * to close a session due to a disconnect or failure to
@@ -773,16 +742,15 @@ lookup_session_by_client (struct Plugin *plugin,
773 * @return #GNUNET_OK on success 742 * @return #GNUNET_OK on success
774 */ 743 */
775static int 744static int
776tcp_disconnect_session (void *cls, 745tcp_disconnect_session (void *cls, struct Session *session)
777 struct Session *session)
778{ 746{
779 struct Plugin *plugin = cls; 747 struct Plugin *plugin = cls;
780 struct PendingMessage *pm; 748 struct PendingMessage *pm;
781 749
782 LOG (GNUNET_ERROR_TYPE_DEBUG, 750 LOG(GNUNET_ERROR_TYPE_DEBUG,
783 "Disconnecting session of peer `%s' address `%s'\n", 751 "Disconnecting session of peer `%s' address `%s'\n",
784 GNUNET_i2s (&session->target), 752 GNUNET_i2s (&session->target),
785 tcp_address_to_string (NULL, session->addr, session->addrlen)); 753 tcp_address_to_string (NULL, session->address->address, session->address->address_length));
786 754
787 if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task) 755 if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task)
788 { 756 {
@@ -790,25 +758,20 @@ tcp_disconnect_session (void *cls,
790 session->timeout_task = GNUNET_SCHEDULER_NO_TASK; 758 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
791 } 759 }
792 760
793 if (GNUNET_YES == 761 if (GNUNET_YES
794 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap, 762 == GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
795 &session->target, 763 &session->target, session))
796 session))
797 { 764 {
798 GNUNET_STATISTICS_update (session->plugin->env->stats, 765 GNUNET_STATISTICS_update (session->plugin->env->stats,
799 gettext_noop ("# TCP sessions active"), -1, 766 gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
800 GNUNET_NO);
801 } 767 }
802 else 768 else
803 { 769 {
804 GNUNET_assert (GNUNET_YES == 770 GNUNET_assert(
805 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, 771 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &session->target, session));
806 &session->target,
807 session));
808 } 772 }
809 if (NULL != session->client) 773 if (NULL != session->client)
810 GNUNET_SERVER_client_set_user_context (session->client, 774 GNUNET_SERVER_client_set_user_context(session->client, (void *) NULL);
811 (void *) NULL);
812 775
813 /* clean up state */ 776 /* clean up state */
814 if (NULL != session->transmit_handle) 777 if (NULL != session->transmit_handle)
@@ -817,7 +780,7 @@ tcp_disconnect_session (void *cls,
817 session->transmit_handle = NULL; 780 session->transmit_handle = NULL;
818 } 781 }
819 session->plugin->env->session_end (session->plugin->env->cls, 782 session->plugin->env->session_end (session->plugin->env->cls,
820 &session->target, session); 783 &session->target, session);
821 784
822 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout) 785 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
823 { 786 {
@@ -827,26 +790,22 @@ tcp_disconnect_session (void *cls,
827 790
828 while (NULL != (pm = session->pending_messages_head)) 791 while (NULL != (pm = session->pending_messages_head))
829 { 792 {
830 LOG (GNUNET_ERROR_TYPE_DEBUG, 793 LOG(GNUNET_ERROR_TYPE_DEBUG,
831 pm->transmit_cont != 794 pm->transmit_cont != NULL ? "Could not deliver message to `%4s'.\n" : "Could not deliver message to `%4s', notifying.\n",
832 NULL ? "Could not deliver message to `%4s'.\n" : 795 GNUNET_i2s (&session->target));
833 "Could not deliver message to `%4s', notifying.\n",
834 GNUNET_i2s (&session->target));
835 GNUNET_STATISTICS_update (session->plugin->env->stats,
836 gettext_noop ("# bytes currently in TCP buffers"),
837 -(int64_t) pm->message_size, GNUNET_NO);
838 GNUNET_STATISTICS_update (session->plugin->env->stats, 796 GNUNET_STATISTICS_update (session->plugin->env->stats,
839 gettext_noop 797 gettext_noop ("# bytes currently in TCP buffers"),
840 ("# bytes discarded by TCP (disconnect)"), 798 -(int64_t) pm->message_size, GNUNET_NO);
841 pm->message_size, GNUNET_NO); 799 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
842 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head, 800 ("# bytes discarded by TCP (disconnect)"), pm->message_size, GNUNET_NO);
843 session->pending_messages_tail, pm); 801 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
802 session->pending_messages_tail, pm);
844 if (NULL != pm->transmit_cont) 803 if (NULL != pm->transmit_cont)
845 pm->transmit_cont (pm->transmit_cont_cls, &session->target, 804 pm->transmit_cont (pm->transmit_cont_cls, &session->target, GNUNET_SYSERR,
846 GNUNET_SYSERR, pm->message_size, 0); 805 pm->message_size, 0);
847 GNUNET_free (pm); 806 GNUNET_free(pm);
848 } 807 }
849 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK) 808 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK )
850 { 809 {
851 GNUNET_SCHEDULER_cancel (session->receive_delay_task); 810 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
852 if (NULL != session->client) 811 if (NULL != session->client)
@@ -858,13 +817,12 @@ tcp_disconnect_session (void *cls,
858 GNUNET_SERVER_client_drop (session->client); 817 GNUNET_SERVER_client_drop (session->client);
859 session->client = NULL; 818 session->client = NULL;
860 } 819 }
861 GNUNET_free_non_null (session->addr); 820 GNUNET_HELLO_address_free(session->address);
862 GNUNET_assert (NULL == session->transmit_handle); 821 GNUNET_assert(NULL == session->transmit_handle);
863 GNUNET_free (session); 822 GNUNET_free(session);
864 return GNUNET_OK; 823 return GNUNET_OK;
865} 824}
866 825
867
868/** 826/**
869 * Function that is called to get the keepalive factor. 827 * Function that is called to get the keepalive factor.
870 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to 828 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
@@ -879,7 +837,6 @@ tcp_query_keepalive_factor (void *cls)
879 return 3; 837 return 3;
880} 838}
881 839
882
883/** 840/**
884 * Session was idle, so disconnect it 841 * Session was idle, so disconnect it
885 * 842 *
@@ -887,22 +844,18 @@ tcp_query_keepalive_factor (void *cls)
887 * @param tc scheduler context 844 * @param tc scheduler context
888 */ 845 */
889static void 846static void
890session_timeout (void *cls, 847session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
891 const struct GNUNET_SCHEDULER_TaskContext *tc)
892{ 848{
893 struct Session *s = cls; 849 struct Session *s = cls;
894 850
895 s->timeout_task = GNUNET_SCHEDULER_NO_TASK; 851 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 852 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
897 "Session %p was idle for %s, disconnecting\n", 853 "Session %p was idle for %s, disconnecting\n", s,
898 s, 854 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_YES));
899 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
900 GNUNET_YES));
901 /* call session destroy function */ 855 /* call session destroy function */
902 tcp_disconnect_session (s->plugin, s); 856 tcp_disconnect_session (s->plugin, s);
903} 857}
904 858
905
906/** 859/**
907 * Increment session timeout due to activity 860 * Increment session timeout due to activity
908 * 861 *
@@ -911,19 +864,15 @@ session_timeout (void *cls,
911static void 864static void
912reschedule_session_timeout (struct Session *s) 865reschedule_session_timeout (struct Session *s)
913{ 866{
914 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task); 867 GNUNET_assert(GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
915 GNUNET_SCHEDULER_cancel (s->timeout_task); 868 GNUNET_SCHEDULER_cancel (s->timeout_task);
916 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 869 s->timeout_task = GNUNET_SCHEDULER_add_delayed (
917 &session_timeout, 870 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, s);
918 s); 871 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 872 "Timeout rescheduled for session %p set to %s\n", s,
920 "Timeout rescheduled for session %p set to %s\n", 873 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_YES));
921 s,
922 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
923 GNUNET_YES));
924} 874}
925 875
926
927/** 876/**
928 * Create a new session. Also queues a welcome message. 877 * Create a new session. Also queues a welcome message.
929 * 878 *
@@ -936,22 +885,20 @@ reschedule_session_timeout (struct Session *s)
936 * @return new session object 885 * @return new session object
937 */ 886 */
938static struct Session * 887static struct Session *
939create_session (struct Plugin *plugin, 888create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
940 const struct GNUNET_PeerIdentity *target, 889 struct GNUNET_SERVER_Client *client, int is_nat)
941 struct GNUNET_SERVER_Client *client, int is_nat)
942{ 890{
943 struct Session *session; 891 struct Session *session;
944 struct PendingMessage *pm; 892 struct PendingMessage *pm;
945 struct WelcomeMessage welcome; 893 struct WelcomeMessage welcome;
946 894
947 if (GNUNET_YES != is_nat) 895 if (GNUNET_YES != is_nat)
948 GNUNET_assert (NULL != client); 896 GNUNET_assert(NULL != client);
949 else 897 else
950 GNUNET_assert (NULL == client); 898 GNUNET_assert(NULL == client);
951 899
952 LOG (GNUNET_ERROR_TYPE_DEBUG, 900 LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new session for peer `%4s'\n",
953 "Creating new session for peer `%4s'\n", 901 GNUNET_i2s (target));
954 GNUNET_i2s (target));
955 session = GNUNET_new (struct Session); 902 session = GNUNET_new (struct Session);
956 session->last_activity = GNUNET_TIME_absolute_get (); 903 session->last_activity = GNUNET_TIME_absolute_get ();
957 session->plugin = plugin; 904 session->plugin = plugin;
@@ -961,32 +908,29 @@ create_session (struct Plugin *plugin,
961 session->expecting_welcome = GNUNET_YES; 908 session->expecting_welcome = GNUNET_YES;
962 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED; 909 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
963 pm = GNUNET_malloc (sizeof (struct PendingMessage) + 910 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
964 sizeof (struct WelcomeMessage)); 911 sizeof (struct WelcomeMessage));
965 pm->msg = (const char *) &pm[1]; 912 pm->msg = (const char *) &pm[1];
966 pm->message_size = sizeof (struct WelcomeMessage); 913 pm->message_size = sizeof(struct WelcomeMessage);
967 welcome.header.size = htons (sizeof (struct WelcomeMessage)); 914 welcome.header.size = htons (sizeof(struct WelcomeMessage));
968 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME); 915 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
969 welcome.clientIdentity = *plugin->env->my_identity; 916 welcome.clientIdentity = *plugin->env->my_identity;
970 memcpy (&pm[1], &welcome, sizeof (welcome)); 917 memcpy (&pm[1], &welcome, sizeof(welcome));
971 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS; 918 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
972 GNUNET_STATISTICS_update (plugin->env->stats, 919 GNUNET_STATISTICS_update (plugin->env->stats,
973 gettext_noop ("# bytes currently in TCP buffers"), 920 gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
974 pm->message_size, GNUNET_NO); 921 GNUNET_NO);
975 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head, 922 GNUNET_CONTAINER_DLL_insert(session->pending_messages_head,
976 session->pending_messages_tail, pm); 923 session->pending_messages_tail, pm);
977 if (GNUNET_YES != is_nat) 924 if (GNUNET_YES != is_nat)
978 { 925 {
979 GNUNET_STATISTICS_update (plugin->env->stats, 926 GNUNET_STATISTICS_update (plugin->env->stats,
980 gettext_noop ("# TCP sessions active"), 1, 927 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
981 GNUNET_NO);
982 } 928 }
983 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 929 session->timeout_task = GNUNET_SCHEDULER_add_delayed (
984 &session_timeout, 930 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
985 session);
986 return session; 931 return session;
987} 932}
988 933
989
990/** 934/**
991 * If we have pending messages, ask the server to 935 * If we have pending messages, ask the server to
992 * transmit them (schedule the respective tasks, etc.) 936 * transmit them (schedule the respective tasks, etc.)
@@ -996,7 +940,6 @@ create_session (struct Plugin *plugin,
996static void 940static void
997process_pending_messages (struct Session *session); 941process_pending_messages (struct Session *session);
998 942
999
1000/** 943/**
1001 * Function called to notify a client about the socket 944 * Function called to notify a client about the socket
1002 * being ready to queue more data. "buf" will be 945 * being ready to queue more data. "buf" will be
@@ -1025,24 +968,24 @@ do_transmit (void *cls, size_t size, void *buf)
1025 plugin = session->plugin; 968 plugin = session->plugin;
1026 if (NULL == buf) 969 if (NULL == buf)
1027 { 970 {
1028 LOG (GNUNET_ERROR_TYPE_DEBUG, 971 LOG(GNUNET_ERROR_TYPE_DEBUG,
1029 "Timeout trying to transmit to peer `%4s', discarding message queue.\n", 972 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1030 GNUNET_i2s (&session->target)); 973 GNUNET_i2s (&session->target));
1031 /* timeout; cancel all messages that have already expired */ 974 /* timeout; cancel all messages that have already expired */
1032 hd = NULL; 975 hd = NULL;
1033 tl = NULL; 976 tl = NULL;
1034 ret = 0; 977 ret = 0;
1035 now = GNUNET_TIME_absolute_get (); 978 now = GNUNET_TIME_absolute_get ();
1036 while ((NULL != (pos = session->pending_messages_head)) && 979 while ((NULL != (pos = session->pending_messages_head))
1037 (pos->timeout.abs_value_us <= now.abs_value_us)) 980 && (pos->timeout.abs_value_us <= now.abs_value_us))
1038 { 981 {
1039 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head, 982 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
1040 session->pending_messages_tail, pos); 983 session->pending_messages_tail, pos);
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, 984 LOG(GNUNET_ERROR_TYPE_DEBUG,
1042 "Failed to transmit %u byte message to `%4s'.\n", 985 "Failed to transmit %u byte message to `%4s'.\n", pos->message_size,
1043 pos->message_size, GNUNET_i2s (&session->target)); 986 GNUNET_i2s (&session->target));
1044 ret += pos->message_size; 987 ret += pos->message_size;
1045 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos); 988 GNUNET_CONTAINER_DLL_insert_after(hd, tl, tl, pos);
1046 } 989 }
1047 /* do this call before callbacks (so that if callbacks destroy 990 /* do this call before callbacks (so that if callbacks destroy
1048 * session, they have a chance to cancel actions done by this 991 * session, they have a chance to cancel actions done by this
@@ -1053,18 +996,17 @@ do_transmit (void *cls, size_t size, void *buf)
1053 * the callbacks may abort the session */ 996 * the callbacks may abort the session */
1054 while (NULL != (pos = hd)) 997 while (NULL != (pos = hd))
1055 { 998 {
1056 GNUNET_CONTAINER_DLL_remove (hd, tl, pos); 999 GNUNET_CONTAINER_DLL_remove(hd, tl, pos);
1057 if (pos->transmit_cont != NULL) 1000 if (pos->transmit_cont != NULL )
1058 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0); 1001 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR,
1059 GNUNET_free (pos); 1002 pos->message_size, 0);
1003 GNUNET_free(pos);
1060 } 1004 }
1061 GNUNET_STATISTICS_update (plugin->env->stats, 1005 GNUNET_STATISTICS_update (plugin->env->stats,
1062 gettext_noop ("# bytes currently in TCP buffers"), 1006 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1063 -(int64_t) ret, GNUNET_NO); 1007 GNUNET_NO);
1064 GNUNET_STATISTICS_update (plugin->env->stats, 1008 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1065 gettext_noop 1009 ("# bytes discarded by TCP (timeout)"), ret, GNUNET_NO);
1066 ("# bytes discarded by TCP (timeout)"), ret,
1067 GNUNET_NO);
1068 return 0; 1010 return 0;
1069 } 1011 }
1070 /* copy all pending messages that would fit */ 1012 /* copy all pending messages that would fit */
@@ -1076,18 +1018,17 @@ do_transmit (void *cls, size_t size, void *buf)
1076 { 1018 {
1077 if (ret + pos->message_size > size) 1019 if (ret + pos->message_size > size)
1078 break; 1020 break;
1079 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head, 1021 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
1080 session->pending_messages_tail, pos); 1022 session->pending_messages_tail, pos);
1081 GNUNET_assert (size >= pos->message_size); 1023 GNUNET_assert(size >= pos->message_size);
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, 1024 LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting message of type %u\n",
1083 "Transmitting message of type %u\n", 1025 ntohs (((struct GNUNET_MessageHeader * ) pos->msg)->type));
1084 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type));
1085 /* FIXME: this memcpy can be up to 7% of our total runtime */ 1026 /* FIXME: this memcpy can be up to 7% of our total runtime */
1086 memcpy (cbuf, pos->msg, pos->message_size); 1027 memcpy (cbuf, pos->msg, pos->message_size);
1087 cbuf += pos->message_size; 1028 cbuf += pos->message_size;
1088 ret += pos->message_size; 1029 ret += pos->message_size;
1089 size -= pos->message_size; 1030 size -= pos->message_size;
1090 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos); 1031 GNUNET_CONTAINER_DLL_insert_tail(hd, tl, pos);
1091 } 1032 }
1092 /* schedule 'continuation' before callbacks so that callbacks that 1033 /* schedule 'continuation' before callbacks so that callbacks that
1093 * cancel everything don't cause us to use a session that no longer 1034 * cancel everything don't cause us to use a session that no longer
@@ -1099,25 +1040,23 @@ do_transmit (void *cls, size_t size, void *buf)
1099 * we should not use 'session' after this point */ 1040 * we should not use 'session' after this point */
1100 while (NULL != (pos = hd)) 1041 while (NULL != (pos = hd))
1101 { 1042 {
1102 GNUNET_CONTAINER_DLL_remove (hd, tl, pos); 1043 GNUNET_CONTAINER_DLL_remove(hd, tl, pos);
1103 if (pos->transmit_cont != NULL) 1044 if (pos->transmit_cont != NULL )
1104 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */ 1045 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK,
1105 GNUNET_free (pos); 1046 pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
1047 GNUNET_free(pos);
1106 } 1048 }
1107 GNUNET_assert (hd == NULL); 1049 GNUNET_assert(hd == NULL);
1108 GNUNET_assert (tl == NULL); 1050 GNUNET_assert(tl == NULL);
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n", 1051 LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n", ret);
1110 ret);
1111 GNUNET_STATISTICS_update (plugin->env->stats, 1052 GNUNET_STATISTICS_update (plugin->env->stats,
1112 gettext_noop ("# bytes currently in TCP buffers"), 1053 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1113 -(int64_t) ret, GNUNET_NO); 1054 GNUNET_NO);
1114 GNUNET_STATISTICS_update (plugin->env->stats, 1055 GNUNET_STATISTICS_update (plugin->env->stats,
1115 gettext_noop ("# bytes transmitted via TCP"), ret, 1056 gettext_noop ("# bytes transmitted via TCP"), ret, GNUNET_NO);
1116 GNUNET_NO);
1117 return ret; 1057 return ret;
1118} 1058}
1119 1059
1120
1121/** 1060/**
1122 * If we have pending messages, ask the server to 1061 * If we have pending messages, ask the server to
1123 * transmit them (schedule the respective tasks, etc.) 1062 * transmit them (schedule the respective tasks, etc.)
@@ -1129,20 +1068,17 @@ process_pending_messages (struct Session *session)
1129{ 1068{
1130 struct PendingMessage *pm; 1069 struct PendingMessage *pm;
1131 1070
1132 GNUNET_assert (NULL != session->client); 1071 GNUNET_assert(NULL != session->client);
1133 if (NULL != session->transmit_handle) 1072 if (NULL != session->transmit_handle)
1134 return; 1073 return;
1135 if (NULL == (pm = session->pending_messages_head)) 1074 if (NULL == (pm = session->pending_messages_head))
1136 return; 1075 return;
1137 1076
1138 session->transmit_handle = 1077 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (
1139 GNUNET_SERVER_notify_transmit_ready (session->client, pm->message_size, 1078 session->client, pm->message_size,
1140 GNUNET_TIME_absolute_get_remaining 1079 GNUNET_TIME_absolute_get_remaining (pm->timeout), &do_transmit, session);
1141 (pm->timeout), &do_transmit,
1142 session);
1143} 1080}
1144 1081
1145
1146#if EXTRA_CHECKS 1082#if EXTRA_CHECKS
1147/** 1083/**
1148 * Closure for #session_it(). 1084 * Closure for #session_it().
@@ -1160,7 +1096,6 @@ struct FindSessionContext
1160 int res; 1096 int res;
1161}; 1097};
1162 1098
1163
1164/** 1099/**
1165 * Function called to check if a session is in our maps. 1100 * Function called to check if a session is in our maps.
1166 * 1101 *
@@ -1170,9 +1105,7 @@ struct FindSessionContext
1170 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session 1105 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1171 */ 1106 */
1172static int 1107static int
1173session_it (void *cls, 1108session_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1174 const struct GNUNET_PeerIdentity *key,
1175 void *value)
1176{ 1109{
1177 struct FindSessionContext *res = cls; 1110 struct FindSessionContext *res = cls;
1178 struct Session *session = value; 1111 struct Session *session = value;
@@ -1185,7 +1118,6 @@ session_it (void *cls,
1185 return GNUNET_YES; 1118 return GNUNET_YES;
1186} 1119}
1187 1120
1188
1189/** 1121/**
1190 * Check that the given session is known to the plugin and 1122 * Check that the given session is known to the plugin and
1191 * is in one of our maps. 1123 * is in one of our maps.
@@ -1195,30 +1127,28 @@ session_it (void *cls,
1195 * @return #GNUNET_OK if all is well, #GNUNET_SYSERR if the session is invalid 1127 * @return #GNUNET_OK if all is well, #GNUNET_SYSERR if the session is invalid
1196 */ 1128 */
1197static int 1129static int
1198find_session (struct Plugin *plugin, 1130find_session (struct Plugin *plugin, struct Session *session)
1199 struct Session *session)
1200{ 1131{
1201 struct FindSessionContext session_map_res; 1132 struct FindSessionContext session_map_res;
1202 struct FindSessionContext nat_map_res; 1133 struct FindSessionContext nat_map_res;
1203 1134
1204 session_map_res.s = session; 1135 session_map_res.s = session;
1205 session_map_res.res = GNUNET_SYSERR; 1136 session_map_res.res = GNUNET_SYSERR;
1206 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, 1137 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_it,
1207 &session_it, &session_map_res); 1138 &session_map_res);
1208 if (GNUNET_SYSERR != session_map_res.res) 1139 if (GNUNET_SYSERR != session_map_res.res)
1209 return GNUNET_OK; 1140 return GNUNET_OK;
1210 nat_map_res.s = session; 1141 nat_map_res.s = session;
1211 nat_map_res.res = GNUNET_SYSERR; 1142 nat_map_res.res = GNUNET_SYSERR;
1212 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, 1143 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, &session_it,
1213 &session_it, &nat_map_res); 1144 &nat_map_res);
1214 if (GNUNET_SYSERR != nat_map_res.res) 1145 if (GNUNET_SYSERR != nat_map_res.res)
1215 return GNUNET_OK; 1146 return GNUNET_OK;
1216 GNUNET_break (0); 1147 GNUNET_break(0);
1217 return GNUNET_SYSERR; 1148 return GNUNET_SYSERR;
1218} 1149}
1219#endif 1150#endif
1220 1151
1221
1222/** 1152/**
1223 * Function that can be used by the transport service to transmit 1153 * Function that can be used by the transport service to transmit
1224 * a message using the plugin. Note that in the case of a 1154 * a message using the plugin. Note that in the case of a
@@ -1247,12 +1177,9 @@ find_session (struct Plugin *plugin,
1247 * and does NOT mean that the message was not transmitted (DV) 1177 * and does NOT mean that the message was not transmitted (DV)
1248 */ 1178 */
1249static ssize_t 1179static ssize_t
1250tcp_plugin_send (void *cls, 1180tcp_plugin_send (void *cls, struct Session *session, const char *msgbuf,
1251 struct Session *session, 1181 size_t msgbuf_size, unsigned int priority, struct GNUNET_TIME_Relative to,
1252 const char *msgbuf, size_t msgbuf_size, 1182 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1253 unsigned int priority,
1254 struct GNUNET_TIME_Relative to,
1255 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1256{ 1183{
1257 struct Plugin * plugin = cls; 1184 struct Plugin * plugin = cls;
1258 struct PendingMessage *pm; 1185 struct PendingMessage *pm;
@@ -1260,9 +1187,8 @@ tcp_plugin_send (void *cls,
1260#if EXTRA_CHECKS 1187#if EXTRA_CHECKS
1261 if (GNUNET_SYSERR == find_session (plugin, session)) 1188 if (GNUNET_SYSERR == find_session (plugin, session))
1262 { 1189 {
1263 LOG (GNUNET_ERROR_TYPE_ERROR, 1190 LOG(GNUNET_ERROR_TYPE_ERROR, _("Trying to send with invalid session %p\n"));
1264 _("Trying to send with invalid session %p\n")); 1191 GNUNET_assert(0);
1265 GNUNET_assert (0);
1266 return GNUNET_SYSERR; 1192 return GNUNET_SYSERR;
1267 } 1193 }
1268#endif 1194#endif
@@ -1275,63 +1201,55 @@ tcp_plugin_send (void *cls,
1275 pm->transmit_cont = cont; 1201 pm->transmit_cont = cont;
1276 pm->transmit_cont_cls = cont_cls; 1202 pm->transmit_cont_cls = cont_cls;
1277 1203
1278 LOG (GNUNET_ERROR_TYPE_DEBUG, 1204 LOG(GNUNET_ERROR_TYPE_DEBUG,
1279 "Asked to transmit %u bytes to `%s', added message to list.\n", 1205 "Asked to transmit %u bytes to `%s', added message to list.\n",
1280 msgbuf_size, GNUNET_i2s (&session->target)); 1206 msgbuf_size, GNUNET_i2s (&session->target));
1281 1207
1282 if (GNUNET_YES == 1208 if (GNUNET_YES
1283 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap, 1209 == GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1284 &session->target, 1210 &session->target, session))
1285 session))
1286 { 1211 {
1287 GNUNET_assert (NULL != session->client); 1212 GNUNET_assert(NULL != session->client);
1288 GNUNET_SERVER_client_set_timeout (session->client, 1213 GNUNET_SERVER_client_set_timeout (session->client,
1289 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 1214 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1290 GNUNET_STATISTICS_update (plugin->env->stats, 1215 GNUNET_STATISTICS_update (plugin->env->stats,
1291 gettext_noop ("# bytes currently in TCP buffers"), 1216 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1292 msgbuf_size, GNUNET_NO); 1217 GNUNET_NO);
1293 1218
1294 /* append pm to pending_messages list */ 1219 /* append pm to pending_messages list */
1295 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head, 1220 GNUNET_CONTAINER_DLL_insert_tail(session->pending_messages_head,
1296 session->pending_messages_tail, pm); 1221 session->pending_messages_tail, pm);
1297 1222
1298 process_pending_messages (session); 1223 process_pending_messages (session);
1299 return msgbuf_size; 1224 return msgbuf_size;
1300 } 1225 }
1301 else if (GNUNET_YES == 1226 else if (GNUNET_YES
1302 GNUNET_CONTAINER_multipeermap_contains_value(plugin->nat_wait_conns, 1227 == GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1303 &session->target, 1228 &session->target, session))
1304 session))
1305 { 1229 {
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, 1230 LOG(GNUNET_ERROR_TYPE_DEBUG,
1307 "This NAT WAIT session for peer `%s' is not yet ready!\n", 1231 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1308 GNUNET_i2s (&session->target)); 1232 GNUNET_i2s (&session->target));
1309 GNUNET_STATISTICS_update (plugin->env->stats, 1233 GNUNET_STATISTICS_update (plugin->env->stats,
1310 gettext_noop ("# bytes currently in TCP buffers"), 1234 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1311 msgbuf_size, GNUNET_NO); 1235 GNUNET_NO);
1312 1236
1313 /* append pm to pending_messages list */ 1237 /* append pm to pending_messages list */
1314 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head, 1238 GNUNET_CONTAINER_DLL_insert_tail(session->pending_messages_head,
1315 session->pending_messages_tail, pm); 1239 session->pending_messages_tail, pm);
1316 return msgbuf_size; 1240 return msgbuf_size;
1317 } 1241 }
1318 else 1242 else
1319 { 1243 {
1320 LOG (GNUNET_ERROR_TYPE_ERROR, 1244 LOG(GNUNET_ERROR_TYPE_ERROR, "Invalid session %p\n", session);
1321 "Invalid session %p\n", session);
1322 if (NULL != cont) 1245 if (NULL != cont)
1323 cont (cont_cls, 1246 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1324 &session->target, 1247 GNUNET_break(0);
1325 GNUNET_SYSERR, 1248 GNUNET_free(pm);
1326 pm->message_size,
1327 0);
1328 GNUNET_break (0);
1329 GNUNET_free (pm);
1330 return GNUNET_SYSERR; /* session does not exist here */ 1249 return GNUNET_SYSERR; /* session does not exist here */
1331 } 1250 }
1332} 1251}
1333 1252
1334
1335/** 1253/**
1336 * Closure for #session_lookup_it(). 1254 * Closure for #session_lookup_it().
1337 */ 1255 */
@@ -1340,19 +1258,14 @@ struct SessionItCtx
1340 /** 1258 /**
1341 * Address we are looking for. 1259 * Address we are looking for.
1342 */ 1260 */
1343 void *addr; 1261 const struct GNUNET_HELLO_Address *address;
1344
1345 /**
1346 * Number of bytes in @e addr.
1347 */
1348 size_t addrlen;
1349 1262
1350 /** 1263 /**
1351 * Where to store the session (if we found it). 1264 * Where to store the session (if we found it).
1352 */ 1265 */
1353 struct Session *result; 1266 struct Session *result;
1354};
1355 1267
1268};
1356 1269
1357/** 1270/**
1358 * Look for a session by address. 1271 * Look for a session by address.
@@ -1363,23 +1276,19 @@ struct SessionItCtx
1363 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session 1276 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1364 */ 1277 */
1365static int 1278static int
1366session_lookup_it (void *cls, 1279session_lookup_it (void *cls, const struct GNUNET_PeerIdentity *key,
1367 const struct GNUNET_PeerIdentity *key, 1280 void *value)
1368 void *value)
1369{ 1281{
1370 struct SessionItCtx * si_ctx = cls; 1282 struct SessionItCtx * si_ctx = cls;
1371 struct Session * session = value; 1283 struct Session * session = value;
1372 1284
1373 if (session->addrlen != si_ctx->addrlen) 1285 if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
1374 return GNUNET_YES;
1375 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1376 return GNUNET_YES; 1286 return GNUNET_YES;
1377 /* Found existing session */ 1287 /* Found existing session */
1378 si_ctx->result = session; 1288 si_ctx->result = session;
1379 return GNUNET_NO; 1289 return GNUNET_NO;
1380} 1290}
1381 1291
1382
1383/** 1292/**
1384 * Task cleaning up a NAT connection attempt after timeout 1293 * Task cleaning up a NAT connection attempt after timeout
1385 * 1294 *
@@ -1387,26 +1296,21 @@ session_lookup_it (void *cls,
1387 * @param tc scheduler context (unused) 1296 * @param tc scheduler context (unused)
1388 */ 1297 */
1389static void 1298static void
1390nat_connect_timeout (void *cls, 1299nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1391 const struct GNUNET_SCHEDULER_TaskContext *tc)
1392{ 1300{
1393 struct Session *session = cls; 1301 struct Session *session = cls;
1394 1302
1395 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK; 1303 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, 1304 LOG(GNUNET_ERROR_TYPE_DEBUG,
1397 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n", 1305 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1398 GNUNET_i2s (&session->target), 1306 GNUNET_i2s (&session->target),
1399 tcp_address_to_string (NULL, 1307 tcp_address_to_string (NULL, session->address->address, session->address->address_length));
1400 session->addr, session->addrlen)); 1308 tcp_disconnect_session (session->plugin, session);
1401 tcp_disconnect_session (session->plugin,
1402 session);
1403} 1309}
1404 1310
1405
1406static void 1311static void
1407tcp_plugin_update_session_timeout (void *cls, 1312tcp_plugin_update_session_timeout (void *cls,
1408 const struct GNUNET_PeerIdentity *peer, 1313 const struct GNUNET_PeerIdentity *peer, struct Session *session)
1409 struct Session *session)
1410{ 1314{
1411 struct Plugin *plugin = cls; 1315 struct Plugin *plugin = cls;
1412 1316
@@ -1415,7 +1319,6 @@ tcp_plugin_update_session_timeout (void *cls,
1415 reschedule_session_timeout (session); 1319 reschedule_session_timeout (session);
1416} 1320}
1417 1321
1418
1419/** 1322/**
1420 * Create a new session to transmit data to the target 1323 * Create a new session to transmit data to the target
1421 * This session will used to send data to this peer and the plugin will 1324 * This session will used to send data to this peer and the plugin will
@@ -1426,8 +1329,7 @@ tcp_plugin_update_session_timeout (void *cls,
1426 * @return the session if the address is valid, NULL otherwise 1329 * @return the session if the address is valid, NULL otherwise
1427 */ 1330 */
1428static struct Session * 1331static struct Session *
1429tcp_plugin_get_session (void *cls, 1332tcp_plugin_get_session (void *cls, const struct GNUNET_HELLO_Address *address)
1430 const struct GNUNET_HELLO_Address *address)
1431{ 1333{
1432 struct Plugin *plugin = cls; 1334 struct Plugin *plugin = cls;
1433 struct Session *session = NULL; 1335 struct Session *session = NULL;
@@ -1444,48 +1346,44 @@ tcp_plugin_get_session (void *cls,
1444 size_t addrlen; 1346 size_t addrlen;
1445 1347
1446 addrlen = address->address_length; 1348 addrlen = address->address_length;
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, 1349 LOG(GNUNET_ERROR_TYPE_DEBUG,
1448 "Trying to get session for `%s' address of peer `%s'\n", 1350 "Trying to get session for `%s' address of peer `%s'\n",
1449 tcp_address_to_string(NULL, address->address, address->address_length), 1351 tcp_address_to_string(NULL, address->address, address->address_length),
1450 GNUNET_i2s (&address->peer)); 1352 GNUNET_i2s (&address->peer));
1451 1353
1452 /* look for existing session */ 1354 /* look for existing session */
1453 if (GNUNET_YES == 1355 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1454 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap, 1356 &address->peer))
1455 &address->peer))
1456 { 1357 {
1457 struct SessionItCtx si_ctx; 1358 struct SessionItCtx si_ctx;
1458 1359
1459 si_ctx.addr = (void *) address->address; 1360 si_ctx.address = address;
1460 si_ctx.addrlen = address->address_length;
1461
1462 si_ctx.result = NULL; 1361 si_ctx.result = NULL;
1463 1362
1464 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, 1363 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1465 &address->peer, 1364 &address->peer, &session_lookup_it, &si_ctx);
1466 &session_lookup_it, &si_ctx); 1365 if (si_ctx.result != NULL )
1467 if (si_ctx.result != NULL)
1468 { 1366 {
1469 session = si_ctx.result; 1367 session = si_ctx.result;
1470 LOG (GNUNET_ERROR_TYPE_DEBUG, 1368 LOG(GNUNET_ERROR_TYPE_DEBUG,
1471 "Found existing session for `%s' address `%s' session %p\n", 1369 "Found existing session for `%s' address `%s' session %p\n",
1472 GNUNET_i2s (&address->peer), 1370 GNUNET_i2s (&address->peer),
1473 tcp_address_to_string(NULL, address->address, address->address_length), 1371 tcp_address_to_string(NULL, address->address, address->address_length),
1474 session); 1372 session);
1475 return session; 1373 return session;
1476 } 1374 }
1477 LOG (GNUNET_ERROR_TYPE_DEBUG, 1375 LOG(GNUNET_ERROR_TYPE_DEBUG,
1478 "Existing sessions did not match address `%s' or peer `%s'\n", 1376 "Existing sessions did not match address `%s' or peer `%s'\n",
1479 tcp_address_to_string(NULL, address->address, address->address_length), 1377 tcp_address_to_string(NULL, address->address, address->address_length),
1480 GNUNET_i2s (&address->peer)); 1378 GNUNET_i2s (&address->peer));
1481 } 1379 }
1482 1380
1483 if (addrlen == sizeof (struct IPv6TcpAddress)) 1381 if (addrlen == sizeof(struct IPv6TcpAddress))
1484 { 1382 {
1485 GNUNET_assert (NULL != address->address); /* make static analysis happy */ 1383 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1486 t6 = address->address; 1384 t6 = address->address;
1487 af = AF_INET6; 1385 af = AF_INET6;
1488 memset (&a6, 0, sizeof (a6)); 1386 memset (&a6, 0, sizeof(a6));
1489#if HAVE_SOCKADDR_IN_SIN_LEN 1387#if HAVE_SOCKADDR_IN_SIN_LEN
1490 a6.sin6_len = sizeof (a6); 1388 a6.sin6_len = sizeof (a6);
1491#endif 1389#endif
@@ -1493,16 +1391,16 @@ tcp_plugin_get_session (void *cls,
1493 a6.sin6_port = t6->t6_port; 1391 a6.sin6_port = t6->t6_port;
1494 if (t6->t6_port == 0) 1392 if (t6->t6_port == 0)
1495 is_natd = GNUNET_YES; 1393 is_natd = GNUNET_YES;
1496 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr)); 1394 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1497 sb = &a6; 1395 sb = &a6;
1498 sbs = sizeof (a6); 1396 sbs = sizeof(a6);
1499 } 1397 }
1500 else if (addrlen == sizeof (struct IPv4TcpAddress)) 1398 else if (addrlen == sizeof(struct IPv4TcpAddress))
1501 { 1399 {
1502 GNUNET_assert (NULL != address->address); /* make static analysis happy */ 1400 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1503 t4 = address->address; 1401 t4 = address->address;
1504 af = AF_INET; 1402 af = AF_INET;
1505 memset (&a4, 0, sizeof (a4)); 1403 memset (&a4, 0, sizeof(a4));
1506#if HAVE_SOCKADDR_IN_SIN_LEN 1404#if HAVE_SOCKADDR_IN_SIN_LEN
1507 a4.sin_len = sizeof (a4); 1405 a4.sin_len = sizeof (a4);
1508#endif 1406#endif
@@ -1512,144 +1410,123 @@ tcp_plugin_get_session (void *cls,
1512 is_natd = GNUNET_YES; 1410 is_natd = GNUNET_YES;
1513 a4.sin_addr.s_addr = t4->ipv4_addr; 1411 a4.sin_addr.s_addr = t4->ipv4_addr;
1514 sb = &a4; 1412 sb = &a4;
1515 sbs = sizeof (a4); 1413 sbs = sizeof(a4);
1516 } 1414 }
1517 else 1415 else
1518 { 1416 {
1519 GNUNET_STATISTICS_update (plugin->env->stats, 1417 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1520 gettext_noop 1418 ("# requests to create session with invalid address"), 1, GNUNET_NO);
1521 ("# requests to create session with invalid address"), 1419 return NULL ;
1522 1, GNUNET_NO);
1523 return NULL;
1524 } 1420 }
1525 1421
1526 ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs); 1422 ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1527 1423
1528 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress))) 1424 if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1529 { 1425 {
1530 /* NAT client only works with IPv4 addresses */ 1426 /* NAT client only works with IPv4 addresses */
1531 return NULL; 1427 return NULL ;
1532 } 1428 }
1533 1429
1534 if (plugin->cur_connections >= plugin->max_connections) 1430 if (plugin->cur_connections >= plugin->max_connections)
1535 { 1431 {
1536 /* saturated */ 1432 /* saturated */
1537 return NULL; 1433 return NULL ;
1538 } 1434 }
1539 1435
1540 if ((is_natd == GNUNET_YES) && 1436 if ((is_natd == GNUNET_YES)
1541 (GNUNET_YES == 1437 && (GNUNET_YES
1542 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns, 1438 == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1543 &address->peer))) 1439 &address->peer)))
1544 { 1440 {
1545 /* Only do one NAT punch attempt per peer identity */ 1441 /* Only do one NAT punch attempt per peer identity */
1546 return NULL; 1442 return NULL ;
1547 } 1443 }
1548 1444
1549 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) && 1445 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1550 (GNUNET_NO == 1446 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1551 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns, 1447 &address->peer)))
1552 &address->peer)))
1553 { 1448 {
1554 LOG (GNUNET_ERROR_TYPE_DEBUG, 1449 LOG (GNUNET_ERROR_TYPE_DEBUG,
1555 "Found valid IPv4 NAT address (creating session)!\n") ; 1450 "Found valid IPv4 NAT address (creating session)!\n");
1556 session = create_session (plugin, 1451 session = create_session (plugin, &address->peer, NULL, GNUNET_YES);
1557 &address->peer, 1452 session->address = GNUNET_HELLO_address_copy (address);
1558 NULL, 1453 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1559 GNUNET_YES); 1454 ats.value);
1560 session->addrlen = 0; 1455 GNUNET_break(
1561 session->addr = NULL; 1456 session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1562 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value);
1563 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1564 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT, 1457 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1565 &nat_connect_timeout, 1458 &nat_connect_timeout, session);
1566 session); 1459 GNUNET_assert(session != NULL);
1567 GNUNET_assert (session != NULL); 1460 GNUNET_assert(
1568 GNUNET_assert (GNUNET_OK == 1461 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns, &session->target, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1569 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1570 &session->target,
1571 session,
1572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1573 1462
1574 LOG (GNUNET_ERROR_TYPE_DEBUG, 1463 LOG(GNUNET_ERROR_TYPE_DEBUG,
1575 "Created NAT WAIT connection to `%4s' at `%s'\n", 1464 "Created NAT WAIT connection to `%4s' at `%s'\n",
1576 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs)); 1465 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1577 1466
1578 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4)) 1467 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1579 return session; 1468 return session;
1580 else 1469 else
1581 { 1470 {
1582 LOG (GNUNET_ERROR_TYPE_DEBUG, 1471 LOG(GNUNET_ERROR_TYPE_DEBUG,
1583 "Running NAT client for `%4s' at `%s' failed\n", 1472 "Running NAT client for `%4s' at `%s' failed\n",
1584 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs)); 1473 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1585 tcp_disconnect_session (plugin, session); 1474 tcp_disconnect_session (plugin, session);
1586 return NULL; 1475 return NULL ;
1587 } 1476 }
1588 } 1477 }
1589 1478
1590 /* create new outbound session */ 1479 /* create new outbound session */
1591 GNUNET_assert (plugin->cur_connections <= plugin->max_connections); 1480 GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1592 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs); 1481 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1593 if (NULL == sa) 1482 if (NULL == sa)
1594 { 1483 {
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, 1484 LOG(GNUNET_ERROR_TYPE_DEBUG,
1596 "Failed to create connection to `%4s' at `%s'\n", 1485 "Failed to create connection to `%4s' at `%s'\n",
1597 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs)); 1486 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1598 return NULL; 1487 return NULL ;
1599 } 1488 }
1600 plugin->cur_connections++; 1489 plugin->cur_connections++;
1601 if (plugin->cur_connections == plugin->max_connections) 1490 if (plugin->cur_connections == plugin->max_connections)
1602 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */ 1491 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1603 1492
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, 1493 LOG(GNUNET_ERROR_TYPE_DEBUG,
1605 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n", 1494 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1606 GNUNET_i2s (&address->peer), 1495 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1607 GNUNET_a2s (sb, sbs)); 1496
1608 1497 session = create_session (plugin, &address->peer,
1609 session = create_session (plugin, 1498 GNUNET_SERVER_connect_socket (plugin->server, sa), GNUNET_NO);
1610 &address->peer, 1499 session->address = GNUNET_HELLO_address_copy (address);
1611 GNUNET_SERVER_connect_socket (plugin->server, sa), 1500 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1612 GNUNET_NO); 1501 ats.value);
1613 session->addr = GNUNET_malloc (addrlen); 1502 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1614 memcpy (session->addr, address->address, addrlen); 1503 GNUNET_SERVER_client_set_user_context(session->client, session);
1615 session->addrlen = addrlen; 1504 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
1616 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value); 1505 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1617 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED); 1506 LOG(GNUNET_ERROR_TYPE_DEBUG,
1618 GNUNET_SERVER_client_set_user_context (session->client, session); 1507 "Creating new session for `%s' address `%s' session %p\n",
1619 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, 1508 GNUNET_i2s (&address->peer),
1620 &session->target, 1509 tcp_address_to_string(NULL, address->address, address->address_length),
1621 session, 1510 session);
1622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1623 LOG (GNUNET_ERROR_TYPE_DEBUG,
1624 "Creating new session for `%s' address `%s' session %p\n",
1625 GNUNET_i2s (&address->peer),
1626 tcp_address_to_string(NULL, address->address, address->address_length),
1627 session);
1628 /* Send TCP Welcome */ 1511 /* Send TCP Welcome */
1629 process_pending_messages (session); 1512 process_pending_messages (session);
1630 1513
1631 return session; 1514 return session;
1632} 1515}
1633 1516
1634
1635static int 1517static int
1636session_disconnect_it (void *cls, 1518session_disconnect_it (void *cls, const struct GNUNET_PeerIdentity *key,
1637 const struct GNUNET_PeerIdentity *key, 1519 void *value)
1638 void *value)
1639{ 1520{
1640 struct Plugin *plugin = cls; 1521 struct Plugin *plugin = cls;
1641 struct Session *session = value; 1522 struct Session *session = value;
1642 1523
1643 GNUNET_STATISTICS_update (session->plugin->env->stats, 1524 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
1644 gettext_noop 1525 ("# transport-service disconnect requests for TCP"), 1, GNUNET_NO);
1645 ("# transport-service disconnect requests for TCP"), 1526 tcp_disconnect_session (plugin, session);
1646 1, GNUNET_NO);
1647 tcp_disconnect_session (plugin,
1648 session);
1649 return GNUNET_YES; 1527 return GNUNET_YES;
1650} 1528}
1651 1529
1652
1653/** 1530/**
1654 * Function that can be called to force a disconnect from the 1531 * Function that can be called to force a disconnect from the
1655 * specified neighbour. This should also cancel all previously 1532 * specified neighbour. This should also cancel all previously
@@ -1667,21 +1544,18 @@ session_disconnect_it (void *cls,
1667 * to be cancelled 1544 * to be cancelled
1668 */ 1545 */
1669static void 1546static void
1670tcp_plugin_disconnect (void *cls, 1547tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1671 const struct GNUNET_PeerIdentity *target)
1672{ 1548{
1673 struct Plugin *plugin = cls; 1549 struct Plugin *plugin = cls;
1674 1550
1675 LOG (GNUNET_ERROR_TYPE_DEBUG, 1551 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s'\n",
1676 "Disconnecting peer `%4s'\n", 1552 GNUNET_i2s (target));
1677 GNUNET_i2s (target));
1678 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target, 1553 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target,
1679 &session_disconnect_it, plugin); 1554 &session_disconnect_it, plugin);
1680 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns, target, 1555 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns, target,
1681 &session_disconnect_it, plugin); 1556 &session_disconnect_it, plugin);
1682} 1557}
1683 1558
1684
1685/** 1559/**
1686 * Running pretty printers: head 1560 * Running pretty printers: head
1687 */ 1561 */
@@ -1743,10 +1617,8 @@ struct PrettyPrinterContext
1743 uint32_t options; 1617 uint32_t options;
1744}; 1618};
1745 1619
1746
1747static void 1620static void
1748ppc_cancel_task (void *cls, 1621ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1749 const struct GNUNET_SCHEDULER_TaskContext *tc)
1750{ 1622{
1751 struct PrettyPrinterContext *ppc = cls; 1623 struct PrettyPrinterContext *ppc = cls;
1752 1624
@@ -1756,11 +1628,10 @@ ppc_cancel_task (void *cls,
1756 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle); 1628 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
1757 ppc->resolver_handle = NULL; 1629 ppc->resolver_handle = NULL;
1758 } 1630 }
1759 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc); 1631 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
1760 GNUNET_free (ppc); 1632 GNUNET_free(ppc);
1761} 1633}
1762 1634
1763
1764/** 1635/**
1765 * Append our port and forward the result. 1636 * Append our port and forward the result.
1766 * 1637 *
@@ -1776,12 +1647,12 @@ append_port (void *cls, const char *hostname)
1776 1647
1777 if (NULL == hostname) 1648 if (NULL == hostname)
1778 { 1649 {
1779 ppc->asc (ppc->asc_cls, NULL); 1650 ppc->asc (ppc->asc_cls, NULL );
1780 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc); 1651 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
1781 GNUNET_SCHEDULER_cancel (ppc->timeout_task); 1652 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
1782 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK; 1653 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1783 ppc->resolver_handle = NULL; 1654 ppc->resolver_handle = NULL;
1784 GNUNET_free (ppc); 1655 GNUNET_free(ppc);
1785 return; 1656 return;
1786 } 1657 }
1787 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next) 1658 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
@@ -1789,29 +1660,20 @@ append_port (void *cls, const char *hostname)
1789 break; 1660 break;
1790 if (NULL == cur) 1661 if (NULL == cur)
1791 { 1662 {
1792 GNUNET_break (0); 1663 GNUNET_break(0);
1793 return; 1664 return;
1794 } 1665 }
1795 1666
1796 if (GNUNET_YES == ppc->ipv6) 1667 if (GNUNET_YES == ppc->ipv6)
1797 GNUNET_asprintf (&ret, 1668 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname,
1798 "%s.%u.[%s]:%d", 1669 ppc->port);
1799 PLUGIN_NAME,
1800 ppc->options,
1801 hostname,
1802 ppc->port);
1803 else 1670 else
1804 GNUNET_asprintf (&ret, 1671 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname,
1805 "%s.%u.%s:%d", 1672 ppc->port);
1806 PLUGIN_NAME,
1807 ppc->options,
1808 hostname,
1809 ppc->port);
1810 ppc->asc (ppc->asc_cls, ret); 1673 ppc->asc (ppc->asc_cls, ret);
1811 GNUNET_free (ret); 1674 GNUNET_free(ret);
1812} 1675}
1813 1676
1814
1815/** 1677/**
1816 * Convert the transports address to a nice, human-readable 1678 * Convert the transports address to a nice, human-readable
1817 * format. 1679 * format.
@@ -1828,11 +1690,9 @@ append_port (void *cls, const char *hostname)
1828 */ 1690 */
1829static void 1691static void
1830tcp_plugin_address_pretty_printer (void *cls, const char *type, 1692tcp_plugin_address_pretty_printer (void *cls, const char *type,
1831 const void *addr, size_t addrlen, 1693 const void *addr, size_t addrlen, int numeric,
1832 int numeric, 1694 struct GNUNET_TIME_Relative timeout,
1833 struct GNUNET_TIME_Relative timeout, 1695 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
1834 GNUNET_TRANSPORT_AddressStringCallback asc,
1835 void *asc_cls)
1836{ 1696{
1837 struct PrettyPrinterContext *ppc; 1697 struct PrettyPrinterContext *ppc;
1838 const void *sb; 1698 const void *sb;
@@ -1844,45 +1704,45 @@ tcp_plugin_address_pretty_printer (void *cls, const char *type,
1844 uint16_t port; 1704 uint16_t port;
1845 uint32_t options; 1705 uint32_t options;
1846 1706
1847 if (addrlen == sizeof (struct IPv6TcpAddress)) 1707 if (addrlen == sizeof(struct IPv6TcpAddress))
1848 { 1708 {
1849 t6 = addr; 1709 t6 = addr;
1850 memset (&a6, 0, sizeof (a6)); 1710 memset (&a6, 0, sizeof(a6));
1851 a6.sin6_family = AF_INET6; 1711 a6.sin6_family = AF_INET6;
1852 a6.sin6_port = t6->t6_port; 1712 a6.sin6_port = t6->t6_port;
1853 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr)); 1713 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1854 port = ntohs (t6->t6_port); 1714 port = ntohs (t6->t6_port);
1855 options = ntohl (t6->options); 1715 options = ntohl (t6->options);
1856 sb = &a6; 1716 sb = &a6;
1857 sbs = sizeof (a6); 1717 sbs = sizeof(a6);
1858 } 1718 }
1859 else if (addrlen == sizeof (struct IPv4TcpAddress)) 1719 else if (addrlen == sizeof(struct IPv4TcpAddress))
1860 { 1720 {
1861 t4 = addr; 1721 t4 = addr;
1862 memset (&a4, 0, sizeof (a4)); 1722 memset (&a4, 0, sizeof(a4));
1863 a4.sin_family = AF_INET; 1723 a4.sin_family = AF_INET;
1864 a4.sin_port = t4->t4_port; 1724 a4.sin_port = t4->t4_port;
1865 a4.sin_addr.s_addr = t4->ipv4_addr; 1725 a4.sin_addr.s_addr = t4->ipv4_addr;
1866 port = ntohs (t4->t4_port); 1726 port = ntohs (t4->t4_port);
1867 options = ntohl (t4->options); 1727 options = ntohl (t4->options);
1868 sb = &a4; 1728 sb = &a4;
1869 sbs = sizeof (a4); 1729 sbs = sizeof(a4);
1870 } 1730 }
1871 else if (0 == addrlen) 1731 else if (0 == addrlen)
1872 { 1732 {
1873 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING); 1733 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1874 asc (asc_cls, NULL); 1734 asc (asc_cls, NULL );
1875 return; 1735 return;
1876 } 1736 }
1877 else 1737 else
1878 { 1738 {
1879 /* invalid address */ 1739 /* invalid address */
1880 GNUNET_break_op (0); 1740 GNUNET_break_op(0);
1881 asc (asc_cls, NULL); 1741 asc (asc_cls, NULL );
1882 return; 1742 return;
1883 } 1743 }
1884 ppc = GNUNET_new (struct PrettyPrinterContext); 1744 ppc = GNUNET_new (struct PrettyPrinterContext);
1885 if (addrlen == sizeof (struct IPv6TcpAddress)) 1745 if (addrlen == sizeof(struct IPv6TcpAddress))
1886 ppc->ipv6 = GNUNET_YES; 1746 ppc->ipv6 = GNUNET_YES;
1887 else 1747 else
1888 ppc->ipv6 = GNUNET_NO; 1748 ppc->ipv6 = GNUNET_NO;
@@ -1890,24 +1750,22 @@ tcp_plugin_address_pretty_printer (void *cls, const char *type,
1890 ppc->asc_cls = asc_cls; 1750 ppc->asc_cls = asc_cls;
1891 ppc->port = port; 1751 ppc->port = port;
1892 ppc->options = options; 1752 ppc->options = options;
1893 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2), 1753 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (
1894 &ppc_cancel_task, ppc); 1754 GNUNET_TIME_relative_multiply (timeout, 2), &ppc_cancel_task, ppc);
1895 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, 1755 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
1896 timeout, 1756 timeout, &append_port, ppc);
1897 &append_port, ppc);
1898 if (NULL != ppc->resolver_handle) 1757 if (NULL != ppc->resolver_handle)
1899 { 1758 {
1900 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc); 1759 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc);
1901 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc); 1760 GNUNET_CONTAINER_DLL_insert(ppc_dll_head, ppc_dll_tail, ppc);
1902 } 1761 }
1903 else 1762 else
1904 { 1763 {
1905 GNUNET_break (0); 1764 GNUNET_break(0);
1906 GNUNET_free (ppc); 1765 GNUNET_free(ppc);
1907 } 1766 }
1908} 1767}
1909 1768
1910
1911/** 1769/**
1912 * Check if the given port is plausible (must be either our listen 1770 * Check if the given port is plausible (must be either our listen
1913 * port or our advertised port), or any port if we are behind NAT 1771 * port or our advertised port), or any port if we are behind NAT
@@ -1926,7 +1784,6 @@ check_port (struct Plugin *plugin, uint16_t in_port)
1926 return GNUNET_SYSERR; 1784 return GNUNET_SYSERR;
1927} 1785}
1928 1786
1929
1930/** 1787/**
1931 * Function that will be called to check if a binary address for this 1788 * Function that will be called to check if a binary address for this
1932 * plugin is well-formed and corresponds to an address for THIS peer 1789 * plugin is well-formed and corresponds to an address for THIS peer
@@ -1949,26 +1806,26 @@ tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1949 struct IPv4TcpAddress *v4; 1806 struct IPv4TcpAddress *v4;
1950 struct IPv6TcpAddress *v6; 1807 struct IPv6TcpAddress *v6;
1951 1808
1952 if ((addrlen != sizeof (struct IPv4TcpAddress)) && 1809 if ((addrlen != sizeof(struct IPv4TcpAddress))
1953 (addrlen != sizeof (struct IPv6TcpAddress))) 1810 && (addrlen != sizeof(struct IPv6TcpAddress)))
1954 { 1811 {
1955 GNUNET_break_op (0); 1812 GNUNET_break_op(0);
1956 return GNUNET_SYSERR; 1813 return GNUNET_SYSERR;
1957 } 1814 }
1958 1815
1959 if (addrlen == sizeof (struct IPv4TcpAddress)) 1816 if (addrlen == sizeof(struct IPv4TcpAddress))
1960 { 1817 {
1961 v4 = (struct IPv4TcpAddress *) addr; 1818 v4 = (struct IPv4TcpAddress *) addr;
1962 if (0 != memcmp (&v4->options, &myoptions, sizeof (myoptions))) 1819 if (0 != memcmp (&v4->options, &myoptions, sizeof(myoptions)))
1963 { 1820 {
1964 GNUNET_break (0); 1821 GNUNET_break(0);
1965 return GNUNET_SYSERR; 1822 return GNUNET_SYSERR;
1966 } 1823 }
1967 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port))) 1824 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1968 return GNUNET_SYSERR; 1825 return GNUNET_SYSERR;
1969 if (GNUNET_OK != 1826 if (GNUNET_OK
1970 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr, 1827 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1971 sizeof (struct in_addr))) 1828 sizeof(struct in_addr)))
1972 return GNUNET_SYSERR; 1829 return GNUNET_SYSERR;
1973 } 1830 }
1974 else 1831 else
@@ -1976,25 +1833,24 @@ tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1976 v6 = (struct IPv6TcpAddress *) addr; 1833 v6 = (struct IPv6TcpAddress *) addr;
1977 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr)) 1834 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1978 { 1835 {
1979 GNUNET_break_op (0); 1836 GNUNET_break_op(0);
1980 return GNUNET_SYSERR; 1837 return GNUNET_SYSERR;
1981 } 1838 }
1982 if (0 != memcmp (&v6->options, &myoptions, sizeof (myoptions))) 1839 if (0 != memcmp (&v6->options, &myoptions, sizeof(myoptions)))
1983 { 1840 {
1984 GNUNET_break (0); 1841 GNUNET_break(0);
1985 return GNUNET_SYSERR; 1842 return GNUNET_SYSERR;
1986 } 1843 }
1987 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port))) 1844 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1988 return GNUNET_SYSERR; 1845 return GNUNET_SYSERR;
1989 if (GNUNET_OK != 1846 if (GNUNET_OK
1990 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr, 1847 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1991 sizeof (struct in6_addr))) 1848 sizeof(struct in6_addr)))
1992 return GNUNET_SYSERR; 1849 return GNUNET_SYSERR;
1993 } 1850 }
1994 return GNUNET_OK; 1851 return GNUNET_OK;
1995} 1852}
1996 1853
1997
1998/** 1854/**
1999 * We've received a nat probe from this peer via TCP. Finish 1855 * We've received a nat probe from this peer via TCP. Finish
2000 * creating the client session and resume sending of queued 1856 * creating the client session and resume sending of queued
@@ -2005,9 +1861,8 @@ tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
2005 * @param message the actual message 1861 * @param message the actual message
2006 */ 1862 */
2007static void 1863static void
2008handle_tcp_nat_probe (void *cls, 1864handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
2009 struct GNUNET_SERVER_Client *client, 1865 const struct GNUNET_MessageHeader *message)
2010 const struct GNUNET_MessageHeader *message)
2011{ 1866{
2012 struct Plugin *plugin = cls; 1867 struct Plugin *plugin = cls;
2013 struct Session *session; 1868 struct Session *session;
@@ -2019,45 +1874,40 @@ handle_tcp_nat_probe (void *cls,
2019 const struct sockaddr_in *s4; 1874 const struct sockaddr_in *s4;
2020 const struct sockaddr_in6 *s6; 1875 const struct sockaddr_in6 *s6;
2021 1876
2022 LOG (GNUNET_ERROR_TYPE_DEBUG, 1877 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
2023 "Received NAT probe\n");
2024 /* We have received a TCP NAT probe, meaning we (hopefully) initiated 1878 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2025 * a connection to this peer by running gnunet-nat-client. This peer 1879 * a connection to this peer by running gnunet-nat-client. This peer
2026 * received the punch message and now wants us to use the new connection 1880 * received the punch message and now wants us to use the new connection
2027 * as the default for that peer. Do so and then send a WELCOME message 1881 * as the default for that peer. Do so and then send a WELCOME message
2028 * so we can really be connected! 1882 * so we can really be connected!
2029 */ 1883 */
2030 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage)) 1884 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2031 { 1885 {
2032 GNUNET_break_op (0); 1886 GNUNET_break_op(0);
2033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2034 return; 1888 return;
2035 } 1889 }
2036 1890
2037 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message; 1891 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2038 if (0 == 1892 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2039 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity, 1893 sizeof(struct GNUNET_PeerIdentity)))
2040 sizeof (struct GNUNET_PeerIdentity)))
2041 { 1894 {
2042 /* refuse connections from ourselves */ 1895 /* refuse connections from ourselves */
2043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1896 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2044 return; 1897 return;
2045 } 1898 }
2046 1899
2047 session = 1900 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2048 GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns, 1901 &tcp_nat_probe->clientIdentity);
2049 &tcp_nat_probe->clientIdentity); 1902 if (session == NULL )
2050 if (session == NULL)
2051 { 1903 {
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, 1904 LOG(GNUNET_ERROR_TYPE_DEBUG, "Did NOT find session for NAT probe!\n");
2053 "Did NOT find session for NAT probe!\n");
2054 GNUNET_SERVER_receive_done (client, GNUNET_OK); 1905 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2055 return; 1906 return;
2056 } 1907 }
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, 1908 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found session for NAT probe!\n");
2058 "Found session for NAT probe!\n");
2059 1909
2060 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK) 1910 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK )
2061 { 1911 {
2062 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout); 1912 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2063 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK; 1913 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
@@ -2065,24 +1915,19 @@ handle_tcp_nat_probe (void *cls,
2065 1915
2066 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen)) 1916 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2067 { 1917 {
2068 GNUNET_break (0); 1918 GNUNET_break(0);
2069 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2070 tcp_disconnect_session (plugin, session); 1920 tcp_disconnect_session (plugin, session);
2071 return; 1921 return;
2072 } 1922 }
2073 GNUNET_assert (GNUNET_CONTAINER_multipeermap_remove 1923 GNUNET_assert(
2074 (plugin->nat_wait_conns, 1924 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
2075 &tcp_nat_probe->clientIdentity, 1925 GNUNET_SERVER_client_set_user_context(client, session);
2076 session) == GNUNET_YES); 1926 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2077 GNUNET_SERVER_client_set_user_context (client, session); 1927 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2078 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2079 &session->target, session,
2080 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2081 session->last_activity = GNUNET_TIME_absolute_get (); 1928 session->last_activity = GNUNET_TIME_absolute_get ();
2082 session->inbound = GNUNET_NO; 1929 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found address `%s' for incoming connection\n",
2083 LOG (GNUNET_ERROR_TYPE_DEBUG, 1930 GNUNET_a2s (vaddr, alen));
2084 "Found address `%s' for incoming connection\n",
2085 GNUNET_a2s (vaddr, alen));
2086 switch (((const struct sockaddr *) vaddr)->sa_family) 1931 switch (((const struct sockaddr *) vaddr)->sa_family)
2087 { 1932 {
2088 case AF_INET: 1933 case AF_INET:
@@ -2091,39 +1936,38 @@ handle_tcp_nat_probe (void *cls,
2091 t4->options = 0; 1936 t4->options = 0;
2092 t4->t4_port = s4->sin_port; 1937 t4->t4_port = s4->sin_port;
2093 t4->ipv4_addr = s4->sin_addr.s_addr; 1938 t4->ipv4_addr = s4->sin_addr.s_addr;
2094 session->addr = t4; 1939 session->address = GNUNET_HELLO_address_allocate (
2095 session->addrlen = sizeof (struct IPv4TcpAddress); 1940 &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t4,
1941 sizeof(struct IPv4TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
2096 break; 1942 break;
2097 case AF_INET6: 1943 case AF_INET6:
2098 s6 = vaddr; 1944 s6 = vaddr;
2099 t6 = GNUNET_new (struct IPv6TcpAddress); 1945 t6 = GNUNET_new (struct IPv6TcpAddress);
2100 t6->options = 0; 1946 t6->options = 0;
2101 t6->t6_port = s6->sin6_port; 1947 t6->t6_port = s6->sin6_port;
2102 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr)); 1948 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2103 session->addr = t6; 1949 session->address = GNUNET_HELLO_address_allocate (
2104 session->addrlen = sizeof (struct IPv6TcpAddress); 1950 &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t6,
1951 sizeof(struct IPv6TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
2105 break; 1952 break;
2106 default: 1953 default:
2107 GNUNET_break_op (0); 1954 GNUNET_break_op(0);
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, 1955 LOG(GNUNET_ERROR_TYPE_DEBUG, "Bad address for incoming connection!\n");
2109 "Bad address for incoming connection!\n"); 1956 GNUNET_free(vaddr);
2110 GNUNET_free (vaddr);
2111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2112 tcp_disconnect_session (plugin, session); 1958 tcp_disconnect_session (plugin, session);
2113 return; 1959 return;
2114 } 1960 }
2115 GNUNET_free (vaddr); 1961 GNUNET_free(vaddr);
2116 GNUNET_break (NULL == session->client); 1962 GNUNET_break(NULL == session->client);
2117 GNUNET_SERVER_client_keep (client); 1963 GNUNET_SERVER_client_keep (client);
2118 session->client = client; 1964 session->client = client;
2119 GNUNET_STATISTICS_update (plugin->env->stats, 1965 GNUNET_STATISTICS_update (plugin->env->stats,
2120 gettext_noop ("# TCP sessions active"), 1, 1966 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2121 GNUNET_NO);
2122 process_pending_messages (session); 1967 process_pending_messages (session);
2123 GNUNET_SERVER_receive_done (client, GNUNET_OK); 1968 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2124} 1969}
2125 1970
2126
2127/** 1971/**
2128 * We've received a welcome from this peer via TCP. Possibly create a 1972 * We've received a welcome from this peer via TCP. Possibly create a
2129 * fresh client record and send back our welcome. 1973 * fresh client record and send back our welcome.
@@ -2134,135 +1978,120 @@ handle_tcp_nat_probe (void *cls,
2134 */ 1978 */
2135static void 1979static void
2136handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client, 1980handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2137 const struct GNUNET_MessageHeader *message) 1981 const struct GNUNET_MessageHeader *message)
2138{ 1982{
2139 struct Plugin *plugin = cls; 1983 struct Plugin *plugin = cls;
2140 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message; 1984 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2141 struct Session *session; 1985 struct Session *session;
2142 size_t alen; 1986 size_t alen;
2143 void *vaddr; 1987 void *vaddr;
2144 struct IPv4TcpAddress *t4; 1988 struct IPv4TcpAddress t4;
2145 struct IPv6TcpAddress *t6; 1989 struct IPv6TcpAddress t6;
2146 const struct sockaddr_in *s4; 1990 const struct sockaddr_in *s4;
2147 const struct sockaddr_in6 *s6; 1991 const struct sockaddr_in6 *s6;
2148 struct GNUNET_ATS_Information ats; 1992 struct GNUNET_ATS_Information ats;
2149 1993
2150 if (0 == 1994 if (0 == memcmp (&wm->clientIdentity, plugin->env->my_identity,
2151 memcmp (&wm->clientIdentity, plugin->env->my_identity, 1995 sizeof(struct GNUNET_PeerIdentity)))
2152 sizeof (struct GNUNET_PeerIdentity)))
2153 { 1996 {
2154 /* refuse connections from ourselves */ 1997 /* refuse connections from ourselves */
2155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 1998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2156 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen)) 1999 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2157 { 2000 {
2158 LOG (GNUNET_ERROR_TYPE_WARNING, 2001 LOG(GNUNET_ERROR_TYPE_WARNING,
2159 "Received %s message from my own identity `%4s' on address `%s'\n", 2002 "Received %s message from my own identity `%4s' on address `%s'\n",
2160 "WELCOME", GNUNET_i2s (&wm->clientIdentity), GNUNET_a2s (vaddr, alen)); 2003 "WELCOME", GNUNET_i2s (&wm->clientIdentity),
2161 GNUNET_free (vaddr); 2004 GNUNET_a2s (vaddr, alen));
2005 GNUNET_free(vaddr);
2162 } 2006 }
2163 GNUNET_break_op (0); 2007 GNUNET_break_op(0);
2164 return; 2008 return;
2165 } 2009 }
2166 LOG (GNUNET_ERROR_TYPE_DEBUG, 2010 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received %s message from `%4s' %p\n", "WELCOME",
2167 "Received %s message from `%4s' %p\n", "WELCOME", 2011 GNUNET_i2s (&wm->clientIdentity), client);
2168 GNUNET_i2s (&wm->clientIdentity), client);
2169 GNUNET_STATISTICS_update (plugin->env->stats, 2012 GNUNET_STATISTICS_update (plugin->env->stats,
2170 gettext_noop ("# TCP WELCOME messages received"), 1, 2013 gettext_noop ("# TCP WELCOME messages received"), 1, GNUNET_NO);
2171 GNUNET_NO);
2172 session = lookup_session_by_client (plugin, client); 2014 session = lookup_session_by_client (plugin, client);
2173 if (NULL != session) 2015 if (NULL != session)
2174 { 2016 {
2175 if (GNUNET_OK == 2017 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2176 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2177 { 2018 {
2178 LOG (GNUNET_ERROR_TYPE_DEBUG, 2019 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p for peer `%s'\n",
2179 "Found existing session %p for peer `%s'\n", 2020 session, GNUNET_a2s (vaddr, alen));
2180 session, 2021 GNUNET_free(vaddr);
2181 GNUNET_a2s (vaddr, alen));
2182 GNUNET_free (vaddr);
2183 } 2022 }
2184 } 2023 }
2185 else 2024 else
2186 { 2025 {
2187 GNUNET_SERVER_client_keep (client); 2026 GNUNET_SERVER_client_keep (client);
2188 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */ 2027 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2189 plugin->cur_connections++; 2028 plugin->cur_connections++;
2190 if (plugin->cur_connections == plugin->max_connections) 2029 if (plugin->cur_connections == plugin->max_connections)
2191 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */ 2030 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2192 2031
2193 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO); 2032 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
2194 session->inbound = GNUNET_YES;
2195 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen)) 2033 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2196 { 2034 {
2197 if (alen == sizeof (struct sockaddr_in)) 2035 if (alen == sizeof(struct sockaddr_in))
2198 { 2036 {
2199 s4 = vaddr; 2037 s4 = vaddr;
2200 t4 = GNUNET_new (struct IPv4TcpAddress); 2038 memset (&t4, '\0', sizeof (t4));
2201 t4->options = htonl (0); 2039 t4.options = htonl (0);
2202 t4->t4_port = s4->sin_port; 2040 t4.t4_port = s4->sin_port;
2203 t4->ipv4_addr = s4->sin_addr.s_addr; 2041 t4.ipv4_addr = s4->sin_addr.s_addr;
2204 session->addr = t4; 2042 session->address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2205 session->addrlen = sizeof (struct IPv4TcpAddress); 2043 PLUGIN_NAME, &t4, sizeof(struct IPv4TcpAddress),
2044 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2206 } 2045 }
2207 else if (alen == sizeof (struct sockaddr_in6)) 2046 else if (alen == sizeof(struct sockaddr_in6))
2208 { 2047 {
2209 s6 = vaddr; 2048 s6 = vaddr;
2210 t6 = GNUNET_new (struct IPv6TcpAddress); 2049 memset (&t6, '\0', sizeof (t6));
2211 t6->options = htonl (0); 2050 t6.options = htonl (0);
2212 t6->t6_port = s6->sin6_port; 2051 t6.t6_port = s6->sin6_port;
2213 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr)); 2052 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2214 session->addr = t6; 2053 session->address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2215 session->addrlen = sizeof (struct IPv6TcpAddress); 2054 PLUGIN_NAME, &t6, sizeof(struct IPv6TcpAddress),
2055 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2216 } 2056 }
2217 2057
2218 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen); 2058 ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
2219 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value); 2059 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
2220 LOG (GNUNET_ERROR_TYPE_DEBUG, 2060 ats.value);
2221 "Creating new session %p for peer `%s'\n", 2061 LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new session %p for peer `%s'\n",
2222 session, 2062 session, GNUNET_a2s (vaddr, alen));
2223 GNUNET_a2s (vaddr, alen)); 2063 GNUNET_free(vaddr);
2224 GNUNET_free (vaddr); 2064 GNUNET_SERVER_client_set_user_context(session->client, session);
2225 GNUNET_SERVER_client_set_user_context (session->client, session); 2065 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2226 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, 2066 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2227 &session->target,
2228 session,
2229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2230 } 2067 }
2231 else 2068 else
2232 { 2069 {
2233 LOG (GNUNET_ERROR_TYPE_DEBUG, 2070 LOG(GNUNET_ERROR_TYPE_DEBUG,
2234 "Did not obtain TCP socket address for incoming connection\n"); 2071 "Did not obtain TCP socket address for incoming connection\n");
2235 GNUNET_break (0); 2072 GNUNET_break(0);
2236 } 2073 }
2237 } 2074 }
2238 2075
2239 if (session->expecting_welcome != GNUNET_YES) 2076 if (session->expecting_welcome != GNUNET_YES)
2240 { 2077 {
2241 GNUNET_break_op (0); 2078 GNUNET_break_op(0);
2242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 2079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2243 GNUNET_break (0); 2080 GNUNET_break(0);
2244 return; 2081 return;
2245 } 2082 }
2246 session->last_activity = GNUNET_TIME_absolute_get (); 2083 session->last_activity = GNUNET_TIME_absolute_get ();
2247 session->expecting_welcome = GNUNET_NO; 2084 session->expecting_welcome = GNUNET_NO;
2248 2085
2249 /* Notify transport and ATS about new session */ 2086 /* Notify transport and ATS about new session */
2250 if (GNUNET_YES == session->inbound) 2087 plugin->env->session_start (NULL, session->address, session, &ats, 1);
2251 { 2088
2252 plugin->env->session_start (NULL,
2253 &wm->clientIdentity,
2254 PLUGIN_NAME,
2255 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2256 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2257 session, &ats, 1);
2258 }
2259 process_pending_messages (session); 2089 process_pending_messages (session);
2260 GNUNET_SERVER_client_set_timeout (client, 2090 GNUNET_SERVER_client_set_timeout (client,
2261 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 2091 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2262 GNUNET_SERVER_receive_done (client, GNUNET_OK); 2092 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2263} 2093}
2264 2094
2265
2266/** 2095/**
2267 * Task to signal the server that we can continue 2096 * Task to signal the server that we can continue
2268 * receiving from the TCP client now. 2097 * receiving from the TCP client now.
@@ -2271,8 +2100,7 @@ handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2271 * @param tc task context (unused) 2100 * @param tc task context (unused)
2272 */ 2101 */
2273static void 2102static void
2274delayed_done (void *cls, 2103delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2275 const struct GNUNET_SCHEDULER_TaskContext *tc)
2276{ 2104{
2277 struct Session *session = cls; 2105 struct Session *session = cls;
2278 2106
@@ -2282,7 +2110,6 @@ delayed_done (void *cls,
2282 GNUNET_SERVER_receive_done (session->client, GNUNET_OK); 2110 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2283} 2111}
2284 2112
2285
2286/** 2113/**
2287 * We've received data for this peer via TCP. Unbox, 2114 * We've received data for this peer via TCP. Unbox,
2288 * compute latency and forward. 2115 * compute latency and forward.
@@ -2293,7 +2120,7 @@ delayed_done (void *cls,
2293 */ 2120 */
2294static void 2121static void
2295handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client, 2122handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2296 const struct GNUNET_MessageHeader *message) 2123 const struct GNUNET_MessageHeader *message)
2297{ 2124{
2298 struct Plugin *plugin = cls; 2125 struct Plugin *plugin = cls;
2299 struct Session *session; 2126 struct Session *session;
@@ -2301,8 +2128,8 @@ handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2301 uint16_t type; 2128 uint16_t type;
2302 2129
2303 type = ntohs (message->type); 2130 type = ntohs (message->type);
2304 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) || 2131 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type)
2305 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type)) 2132 || (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2306 { 2133 {
2307 /* We don't want to propagate WELCOME and NAT Probe messages up! */ 2134 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2308 GNUNET_SERVER_receive_done (client, GNUNET_OK); 2135 GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -2316,12 +2143,11 @@ handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2316 size_t alen; 2143 size_t alen;
2317 2144
2318 GNUNET_SERVER_client_get_address (client, &vaddr, &alen); 2145 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2319 LOG (GNUNET_ERROR_TYPE_ERROR, 2146 LOG(GNUNET_ERROR_TYPE_ERROR,
2320 "Received unexpected %u bytes of type %u from `%s'\n", 2147 "Received unexpected %u bytes of type %u from `%s'\n",
2321 (unsigned int) ntohs (message->size), 2148 (unsigned int ) ntohs (message->size),
2322 (unsigned int) ntohs (message->type), 2149 (unsigned int ) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2323 GNUNET_a2s(vaddr, alen)); 2150 GNUNET_break_op(0);
2324 GNUNET_break_op (0);
2325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 2151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2326 GNUNET_free_non_null(vaddr); 2152 GNUNET_free_non_null(vaddr);
2327 return; 2153 return;
@@ -2333,48 +2159,38 @@ handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2333 size_t alen; 2159 size_t alen;
2334 2160
2335 GNUNET_SERVER_client_get_address (client, &vaddr, &alen); 2161 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2336 LOG (GNUNET_ERROR_TYPE_ERROR, 2162 LOG(GNUNET_ERROR_TYPE_ERROR,
2337 "Received unexpected %u bytes of type %u from `%s'\n", 2163 "Received unexpected %u bytes of type %u from `%s'\n",
2338 (unsigned int) ntohs (message->size), 2164 (unsigned int ) ntohs (message->size),
2339 (unsigned int) ntohs (message->type), 2165 (unsigned int ) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2340 GNUNET_a2s(vaddr, alen)); 2166 GNUNET_break_op(0);
2341 GNUNET_break_op (0);
2342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 2167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2343 GNUNET_free_non_null(vaddr); 2168 GNUNET_free_non_null(vaddr);
2344 return; 2169 return;
2345 } 2170 }
2346 2171
2347 session->last_activity = GNUNET_TIME_absolute_get (); 2172 session->last_activity = GNUNET_TIME_absolute_get ();
2348 LOG (GNUNET_ERROR_TYPE_DEBUG, 2173 LOG(GNUNET_ERROR_TYPE_DEBUG,
2349 "Passing %u bytes of type %u from `%4s' to transport service.\n", 2174 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2350 (unsigned int) ntohs (message->size), 2175 (unsigned int ) ntohs (message->size),
2351 (unsigned int) ntohs (message->type), 2176 (unsigned int ) ntohs (message->type), GNUNET_i2s (&session->target));
2352 GNUNET_i2s (&session->target));
2353 2177
2354 GNUNET_STATISTICS_update (plugin->env->stats, 2178 GNUNET_STATISTICS_update (plugin->env->stats,
2355 gettext_noop ("# bytes received via TCP"), 2179 gettext_noop ("# bytes received via TCP"), ntohs (message->size),
2356 ntohs (message->size), GNUNET_NO); 2180 GNUNET_NO);
2357 struct GNUNET_ATS_Information distance; 2181 struct GNUNET_ATS_Information distance;
2358 2182
2359 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE); 2183 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2360 distance.value = htonl ((uint32_t) session->ats_address_network_type); 2184 distance.value = htonl ((uint32_t) session->ats_address_network_type);
2361 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED); 2185 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2362 2186
2363 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap, 2187 GNUNET_assert(
2364 &session->target, 2188 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2365 session)); 2189 &session->target, session));
2366 2190
2367 delay = plugin->env->receive (plugin->env->cls, 2191 delay = plugin->env->receive (plugin->env->cls, session->address, session, message);
2368 &session->target, 2192 plugin->env->update_address_metrics (plugin->env->cls, session->address,
2369 message, 2193 session, &distance, 1);
2370 session,
2371 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2372 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2373 plugin->env->update_address_metrics (plugin->env->cls,
2374 &session->target,
2375 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2376 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2377 session, &distance, 1);
2378 reschedule_session_timeout (session); 2194 reschedule_session_timeout (session);
2379 if (0 == delay.rel_value_us) 2195 if (0 == delay.rel_value_us)
2380 { 2196 {
@@ -2382,17 +2198,15 @@ handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2382 } 2198 }
2383 else 2199 else
2384 { 2200 {
2385 LOG (GNUNET_ERROR_TYPE_DEBUG, 2201 LOG(GNUNET_ERROR_TYPE_DEBUG, "Throttling receiving from `%s' for %s\n",
2386 "Throttling receiving from `%s' for %s\n", 2202 GNUNET_i2s (&session->target),
2387 GNUNET_i2s (&session->target), 2203 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2388 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2389 GNUNET_SERVER_disable_receive_done_warning (client); 2204 GNUNET_SERVER_disable_receive_done_warning (client);
2390 session->receive_delay_task = 2205 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2391 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session); 2206 &delayed_done, session);
2392 } 2207 }
2393} 2208}
2394 2209
2395
2396/** 2210/**
2397 * Functions with this signature are called whenever a peer 2211 * Functions with this signature are called whenever a peer
2398 * is disconnected on the network level. 2212 * is disconnected on the network level.
@@ -2406,36 +2220,30 @@ disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2406 struct Plugin *plugin = cls; 2220 struct Plugin *plugin = cls;
2407 struct Session *session; 2221 struct Session *session;
2408 2222
2409 if (client == NULL) 2223 if (client == NULL )
2410 return; 2224 return;
2411 session = lookup_session_by_client (plugin, client); 2225 session = lookup_session_by_client (plugin, client);
2412 if (session == NULL) 2226 if (session == NULL )
2413 return; /* unknown, nothing to do */ 2227 return; /* unknown, nothing to do */
2414 LOG (GNUNET_ERROR_TYPE_DEBUG, 2228 LOG(GNUNET_ERROR_TYPE_DEBUG,
2415 "Destroying session of `%4s' with %s due to network-level disconnect.\n", 2229 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2416 GNUNET_i2s (&session->target), 2230 GNUNET_i2s (&session->target),
2417 (session->addr != 2231 tcp_address_to_string (session->plugin, session->address->address,
2418 NULL) ? tcp_address_to_string (session->plugin, 2232 session->address->address_length));
2419 session->addr,
2420 session->addrlen) :
2421 "*");
2422 2233
2423 if (plugin->cur_connections == plugin->max_connections) 2234 if (plugin->cur_connections == plugin->max_connections)
2424 GNUNET_SERVER_resume (plugin->server); /* Resume server */ 2235 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2425 2236
2426 if (plugin->cur_connections < 1) 2237 if (plugin->cur_connections < 1)
2427 GNUNET_break (0); 2238 GNUNET_break(0);
2428 else 2239 else
2429 plugin->cur_connections--; 2240 plugin->cur_connections--;
2430 2241
2431 GNUNET_STATISTICS_update (session->plugin->env->stats, 2242 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2432 gettext_noop 2243 ("# network-level TCP disconnect events"), 1, GNUNET_NO);
2433 ("# network-level TCP disconnect events"), 1,
2434 GNUNET_NO);
2435 tcp_disconnect_session (plugin, session); 2244 tcp_disconnect_session (plugin, session);
2436} 2245}
2437 2246
2438
2439/** 2247/**
2440 * We can now send a probe message, copy into buffer to really send. 2248 * We can now send a probe message, copy into buffer to really send.
2441 * 2249 *
@@ -2452,24 +2260,23 @@ notify_send_probe (void *cls, size_t size, void *buf)
2452 size_t ret; 2260 size_t ret;
2453 2261
2454 tcp_probe_ctx->transmit_handle = NULL; 2262 tcp_probe_ctx->transmit_handle = NULL;
2455 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail, 2263 GNUNET_CONTAINER_DLL_remove(plugin->probe_head, plugin->probe_tail,
2456 tcp_probe_ctx); 2264 tcp_probe_ctx);
2457 if (buf == NULL) 2265 if (buf == NULL )
2458 { 2266 {
2459 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock); 2267 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2460 GNUNET_free (tcp_probe_ctx); 2268 GNUNET_free(tcp_probe_ctx);
2461 return 0; 2269 return 0;
2462 } 2270 }
2463 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message)); 2271 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2464 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message)); 2272 memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2465 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server, 2273 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2466 tcp_probe_ctx->sock); 2274 tcp_probe_ctx->sock);
2467 ret = sizeof (tcp_probe_ctx->message); 2275 ret = sizeof(tcp_probe_ctx->message);
2468 GNUNET_free (tcp_probe_ctx); 2276 GNUNET_free(tcp_probe_ctx);
2469 return ret; 2277 return ret;
2470} 2278}
2471 2279
2472
2473/** 2280/**
2474 * Function called by the NAT subsystem suggesting another peer wants 2281 * Function called by the NAT subsystem suggesting another peer wants
2475 * to connect to us via connection reversal. Try to connect back to the 2282 * to connect to us via connection reversal. Try to connect back to the
@@ -2481,7 +2288,7 @@ notify_send_probe (void *cls, size_t size, void *buf)
2481 */ 2288 */
2482static void 2289static void
2483try_connection_reversal (void *cls, const struct sockaddr *addr, 2290try_connection_reversal (void *cls, const struct sockaddr *addr,
2484 socklen_t addrlen) 2291 socklen_t addrlen)
2485{ 2292{
2486 struct Plugin *plugin = cls; 2293 struct Plugin *plugin = cls;
2487 struct GNUNET_CONNECTION_Handle *sock; 2294 struct GNUNET_CONNECTION_Handle *sock;
@@ -2492,7 +2299,7 @@ try_connection_reversal (void *cls, const struct sockaddr *addr,
2492 * that wants to connect to us! Send a message to establish a connection. 2299 * that wants to connect to us! Send a message to establish a connection.
2493 */ 2300 */
2494 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen); 2301 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2495 if (sock == NULL) 2302 if (sock == NULL )
2496 { 2303 {
2497 /* failed for some odd reason (out of sockets?); ignore attempt */ 2304 /* failed for some odd reason (out of sockets?); ignore attempt */
2498 return; 2305 return;
@@ -2501,27 +2308,22 @@ try_connection_reversal (void *cls, const struct sockaddr *addr,
2501 /* FIXME: do we need to track these probe context objects so that 2308 /* FIXME: do we need to track these probe context objects so that
2502 * we can clean them up on plugin unload? */ 2309 * we can clean them up on plugin unload? */
2503 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext); 2310 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2504 tcp_probe_ctx->message.header.size = 2311 tcp_probe_ctx->message.header.size = htons (
2505 htons (sizeof (struct TCP_NAT_ProbeMessage)); 2312 sizeof(struct TCP_NAT_ProbeMessage));
2506 tcp_probe_ctx->message.header.type = 2313 tcp_probe_ctx->message.header.type = htons (
2507 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE); 2314 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2508 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity, 2315 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2509 sizeof (struct GNUNET_PeerIdentity)); 2316 sizeof(struct GNUNET_PeerIdentity));
2510 tcp_probe_ctx->plugin = plugin; 2317 tcp_probe_ctx->plugin = plugin;
2511 tcp_probe_ctx->sock = sock; 2318 tcp_probe_ctx->sock = sock;
2512 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail, 2319 GNUNET_CONTAINER_DLL_insert(plugin->probe_head, plugin->probe_tail,
2513 tcp_probe_ctx); 2320 tcp_probe_ctx);
2514 tcp_probe_ctx->transmit_handle = 2321 tcp_probe_ctx->transmit_handle = GNUNET_CONNECTION_notify_transmit_ready (
2515 GNUNET_CONNECTION_notify_transmit_ready (sock, 2322 sock, ntohs (tcp_probe_ctx->message.header.size),
2516 ntohs (tcp_probe_ctx-> 2323 GNUNET_TIME_UNIT_FOREVER_REL, &notify_send_probe, tcp_probe_ctx);
2517 message.header.size),
2518 GNUNET_TIME_UNIT_FOREVER_REL,
2519 &notify_send_probe,
2520 tcp_probe_ctx);
2521 2324
2522} 2325}
2523 2326
2524
2525/** 2327/**
2526 * Function obtain the network type for a session 2328 * Function obtain the network type for a session
2527 * 2329 *
@@ -2530,14 +2332,12 @@ try_connection_reversal (void *cls, const struct sockaddr *addr,
2530 * @return the network type in HBO or #GNUNET_SYSERR 2332 * @return the network type in HBO or #GNUNET_SYSERR
2531 */ 2333 */
2532static enum GNUNET_ATS_Network_Type 2334static enum GNUNET_ATS_Network_Type
2533tcp_get_network (void *cls, 2335tcp_get_network (void *cls, struct Session *session)
2534 struct Session *session)
2535{ 2336{
2536 GNUNET_assert (NULL != session); 2337 GNUNET_assert(NULL != session);
2537 return session->ats_address_network_type; 2338 return session->ats_address_network_type;
2538} 2339}
2539 2340
2540
2541/** 2341/**
2542 * Entry point for the plugin. 2342 * Entry point for the plugin.
2543 * 2343 *
@@ -2547,14 +2347,12 @@ tcp_get_network (void *cls,
2547void * 2347void *
2548libgnunet_plugin_transport_tcp_init (void *cls) 2348libgnunet_plugin_transport_tcp_init (void *cls)
2549{ 2349{
2550 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = { 2350 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = { {
2551 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, 2351 &handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2552 sizeof (struct WelcomeMessage)}, 2352 sizeof(struct WelcomeMessage) }, { &handle_tcp_nat_probe, NULL,
2553 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, 2353 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2554 sizeof (struct TCP_NAT_ProbeMessage)}, 2354 sizeof(struct TCP_NAT_ProbeMessage) }, { &handle_tcp_data, NULL,
2555 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0}, 2355 GNUNET_MESSAGE_TYPE_ALL, 0 }, { NULL, NULL, 0, 0 } };
2556 {NULL, NULL, 0, 0}
2557 };
2558 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls; 2356 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2559 struct GNUNET_TRANSPORT_PluginFunctions *api; 2357 struct GNUNET_TRANSPORT_PluginFunctions *api;
2560 struct Plugin *plugin; 2358 struct Plugin *plugin;
@@ -2569,11 +2367,10 @@ libgnunet_plugin_transport_tcp_init (void *cls)
2569 struct sockaddr **addrs; 2367 struct sockaddr **addrs;
2570 socklen_t *addrlens; 2368 socklen_t *addrlens;
2571 2369
2572
2573 if (NULL == env->receive) 2370 if (NULL == env->receive)
2574 { 2371 {
2575 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully 2372 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2576 initialze the plugin or the API */ 2373 initialze the plugin or the API */
2577 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions); 2374 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2578 api->cls = NULL; 2375 api->cls = NULL;
2579 api->address_pretty_printer = &tcp_plugin_address_pretty_printer; 2376 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
@@ -2582,26 +2379,24 @@ libgnunet_plugin_transport_tcp_init (void *cls)
2582 return api; 2379 return api;
2583 } 2380 }
2584 2381
2585 GNUNET_assert (NULL != env->cfg); 2382 GNUNET_assert(NULL != env->cfg);
2586 if (GNUNET_OK != 2383 if (GNUNET_OK
2587 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", 2384 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2588 "MAX_CONNECTIONS", 2385 "MAX_CONNECTIONS", &max_connections))
2589 &max_connections))
2590 max_connections = 128; 2386 max_connections = 128;
2591 2387
2592 aport = 0; 2388 aport = 0;
2593 if ((GNUNET_OK != 2389 if ((GNUNET_OK
2594 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT", 2390 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2595 &bport)) || (bport > 65535) || 2391 "PORT", &bport)) || (bport > 65535)
2596 ((GNUNET_OK == 2392 || ((GNUNET_OK
2597 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", 2393 == GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2598 "ADVERTISED-PORT", &aport)) && 2394 "ADVERTISED-PORT", &aport)) && (aport > 65535)))
2599 (aport > 65535))) 2395 {
2600 { 2396 LOG(GNUNET_ERROR_TYPE_ERROR,
2601 LOG (GNUNET_ERROR_TYPE_ERROR, 2397 _("Require valid port number for service `%s' in configuration!\n"),
2602 _("Require valid port number for service `%s' in configuration!\n"), 2398 "transport-tcp");
2603 "transport-tcp"); 2399 return NULL ;
2604 return NULL;
2605 } 2400 }
2606 if (aport == 0) 2401 if (aport == 0)
2607 aport = bport; 2402 aport = bport;
@@ -2609,12 +2404,12 @@ libgnunet_plugin_transport_tcp_init (void *cls)
2609 aport = 0; 2404 aport = 0;
2610 if (bport != 0) 2405 if (bport != 0)
2611 { 2406 {
2612 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE); 2407 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg,
2613 if (service == NULL) 2408 GNUNET_SERVICE_OPTION_NONE);
2409 if (service == NULL )
2614 { 2410 {
2615 LOG (GNUNET_ERROR_TYPE_WARNING, 2411 LOG(GNUNET_ERROR_TYPE_WARNING, _("Failed to start service.\n"));
2616 _("Failed to start service.\n")); 2412 return NULL ;
2617 return NULL;
2618 } 2413 }
2619 } 2414 }
2620 else 2415 else
@@ -2624,42 +2419,42 @@ libgnunet_plugin_transport_tcp_init (void *cls)
2624 myoptions = 0; 2419 myoptions = 0;
2625 2420
2626 plugin = GNUNET_new (struct Plugin); 2421 plugin = GNUNET_new (struct Plugin);
2627 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections, GNUNET_YES); 2422 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2423 GNUNET_YES);
2628 plugin->max_connections = max_connections; 2424 plugin->max_connections = max_connections;
2629 plugin->cur_connections = 0; 2425 plugin->cur_connections = 0;
2630 plugin->open_port = bport; 2426 plugin->open_port = bport;
2631 plugin->adv_port = aport; 2427 plugin->adv_port = aport;
2632 plugin->env = env; 2428 plugin->env = env;
2633 plugin->lsock = NULL; 2429 plugin->lsock = NULL;
2634 if ((service != NULL) && 2430 if ((service != NULL )&&
2635 (GNUNET_SYSERR != 2431 (GNUNET_SYSERR !=
2636 (ret_s = 2432 (ret_s =
2637 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs, 2433 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2638 &addrlens)))) 2434 &addrlens)))){
2639 { 2435 for (ret = ret_s-1; ret >= 0; ret--)
2640 for (ret = ret_s-1; ret >= 0; ret--) 2436 LOG (GNUNET_ERROR_TYPE_INFO,
2641 LOG (GNUNET_ERROR_TYPE_INFO, 2437 "Binding to address `%s'\n",
2642 "Binding to address `%s'\n", 2438 GNUNET_a2s (addrs[ret], addrlens[ret]));
2643 GNUNET_a2s (addrs[ret], addrlens[ret])); 2439 plugin->nat =
2644 plugin->nat = 2440 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2645 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s, 2441 (const struct sockaddr **) addrs, addrlens,
2646 (const struct sockaddr **) addrs, addrlens, 2442 &tcp_nat_port_map_callback,
2647 &tcp_nat_port_map_callback, 2443 &try_connection_reversal, plugin);
2648 &try_connection_reversal, plugin); 2444 for (ret = ret_s -1; ret >= 0; ret--)
2649 for (ret = ret_s -1; ret >= 0; ret--) 2445 {
2650 { 2446 GNUNET_assert (addrs[ret] != NULL);
2651 GNUNET_assert (addrs[ret] != NULL); 2447 GNUNET_free (addrs[ret]);
2652 GNUNET_free (addrs[ret]); 2448 }
2653 } 2449 GNUNET_free_non_null (addrs);
2654 GNUNET_free_non_null (addrs); 2450 GNUNET_free_non_null (addrlens);
2655 GNUNET_free_non_null (addrlens); 2451}
2656 } 2452else
2657 else 2453{
2658 { 2454 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2659 plugin->nat = GNUNET_NAT_register (plugin->env->cfg, 2455 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2660 GNUNET_YES, 0, 0, NULL, NULL, NULL, 2456 &try_connection_reversal, plugin);
2661 &try_connection_reversal, plugin); 2457}
2662 }
2663 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions); 2458 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2664 api->cls = plugin; 2459 api->cls = plugin;
2665 api->send = &tcp_plugin_send; 2460 api->send = &tcp_plugin_send;
@@ -2681,51 +2476,48 @@ libgnunet_plugin_transport_tcp_init (void *cls)
2681 } 2476 }
2682 else 2477 else
2683 { 2478 {
2684 if (GNUNET_OK != 2479 if (GNUNET_OK
2685 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp", 2480 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2686 "TIMEOUT", &idle_timeout)) 2481 "TIMEOUT", &idle_timeout))
2687 { 2482 {
2688 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2483 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "transport-tcp",
2689 "transport-tcp", "TIMEOUT"); 2484 "TIMEOUT");
2690 if (plugin->nat != NULL) 2485 if (plugin->nat != NULL )
2691 GNUNET_NAT_unregister (plugin->nat); 2486 GNUNET_NAT_unregister (plugin->nat);
2692 GNUNET_free (plugin); 2487 GNUNET_free(plugin);
2693 GNUNET_free (api); 2488 GNUNET_free(api);
2694 return NULL; 2489 return NULL ;
2695 } 2490 }
2696 plugin->server = 2491 plugin->server = GNUNET_SERVER_create_with_sockets (
2697 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin, 2492 &plugin_tcp_access_check, plugin, NULL, idle_timeout, GNUNET_YES);
2698 NULL, idle_timeout, GNUNET_YES);
2699 } 2493 }
2700 plugin->handlers = GNUNET_malloc (sizeof (my_handlers)); 2494 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2701 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers)); 2495 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2702 for (i = 0; 2496 for (i = 0;
2703 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler); 2497 i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);
2704 i++) 2498 i++)
2705 plugin->handlers[i].callback_cls = plugin; 2499 plugin->handlers[i].callback_cls = plugin;
2706 2500
2707 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers); 2501 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2708 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin); 2502 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2709 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_YES); 2503 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2504 GNUNET_YES);
2710 if (bport != 0) 2505 if (bport != 0)
2711 LOG (GNUNET_ERROR_TYPE_INFO, 2506 LOG(GNUNET_ERROR_TYPE_INFO, _("TCP transport listening on port %llu\n"),
2712 _("TCP transport listening on port %llu\n"), bport); 2507 bport);
2713 else 2508 else
2714 LOG (GNUNET_ERROR_TYPE_INFO, 2509 LOG(GNUNET_ERROR_TYPE_INFO,
2715 _("TCP transport not listening on any port (client only)\n")); 2510 _("TCP transport not listening on any port (client only)\n"));
2716 if (aport != bport) 2511 if (aport != bport)
2717 LOG (GNUNET_ERROR_TYPE_INFO, 2512 LOG(GNUNET_ERROR_TYPE_INFO,
2718 _("TCP transport advertises itself as being on port %llu\n"), 2513 _("TCP transport advertises itself as being on port %llu\n"), aport);
2719 aport);
2720 /* Initially set connections to 0 */ 2514 /* Initially set connections to 0 */
2721 GNUNET_assert (NULL != plugin->env->stats); 2515 GNUNET_assert(NULL != plugin->env->stats);
2722 GNUNET_STATISTICS_set (plugin->env->stats, 2516 GNUNET_STATISTICS_set (plugin->env->stats,
2723 gettext_noop ("# TCP sessions active"), 0, 2517 gettext_noop ("# TCP sessions active"), 0, GNUNET_NO);
2724 GNUNET_NO);
2725 return api; 2518 return api;
2726} 2519}
2727 2520
2728
2729/** 2521/**
2730 * Exit point from the plugin. 2522 * Exit point from the plugin.
2731 * 2523 *
@@ -2743,50 +2535,49 @@ libgnunet_plugin_transport_tcp_done (void *cls)
2743 2535
2744 if (NULL == plugin) 2536 if (NULL == plugin)
2745 { 2537 {
2746 GNUNET_free (api); 2538 GNUNET_free(api);
2747 return NULL; 2539 return NULL ;
2748 } 2540 }
2749 LOG (GNUNET_ERROR_TYPE_DEBUG, 2541 LOG(GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2750 "Shutting down TCP plugin\n");
2751 2542
2752 /* Removing leftover sessions */ 2543 /* Removing leftover sessions */
2753 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, 2544 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2754 &session_disconnect_it, plugin); 2545 &session_disconnect_it, plugin);
2755 /* Removing leftover NAT sessions */ 2546 /* Removing leftover NAT sessions */
2756 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, 2547 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2757 &session_disconnect_it, plugin); 2548 &session_disconnect_it, plugin);
2758 2549
2759 next = ppc_dll_head; 2550 next = ppc_dll_head;
2760 for (cur = next; NULL != cur; cur = next) 2551 for (cur = next; NULL != cur; cur = next)
2761 { 2552 {
2762 next = cur->next; 2553 next = cur->next;
2763 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur); 2554 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, cur);
2764 if (NULL != cur->resolver_handle) 2555 if (NULL != cur->resolver_handle)
2765 GNUNET_RESOLVER_request_cancel (cur->resolver_handle); 2556 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2766 GNUNET_SCHEDULER_cancel (cur->timeout_task); 2557 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2767 GNUNET_free (cur); 2558 GNUNET_free(cur);
2768 GNUNET_break (0); 2559 GNUNET_break(0);
2769 } 2560 }
2770 2561
2771 if (plugin->service != NULL) 2562 if (plugin->service != NULL )
2772 GNUNET_SERVICE_stop (plugin->service); 2563 GNUNET_SERVICE_stop (plugin->service);
2773 else 2564 else
2774 GNUNET_SERVER_destroy (plugin->server); 2565 GNUNET_SERVER_destroy (plugin->server);
2775 GNUNET_free (plugin->handlers); 2566 GNUNET_free(plugin->handlers);
2776 if (plugin->nat != NULL) 2567 if (plugin->nat != NULL )
2777 GNUNET_NAT_unregister (plugin->nat); 2568 GNUNET_NAT_unregister (plugin->nat);
2778 while (NULL != (tcp_probe = plugin->probe_head)) 2569 while (NULL != (tcp_probe = plugin->probe_head))
2779 { 2570 {
2780 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail, 2571 GNUNET_CONTAINER_DLL_remove(plugin->probe_head, plugin->probe_tail,
2781 tcp_probe); 2572 tcp_probe);
2782 GNUNET_CONNECTION_destroy (tcp_probe->sock); 2573 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2783 GNUNET_free (tcp_probe); 2574 GNUNET_free(tcp_probe);
2784 } 2575 }
2785 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns); 2576 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2786 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap); 2577 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2787 GNUNET_free (plugin); 2578 GNUNET_free(plugin);
2788 GNUNET_free (api); 2579 GNUNET_free(api);
2789 return NULL; 2580 return NULL ;
2790} 2581}
2791 2582
2792/* end of plugin_transport_tcp.c */ 2583/* end of plugin_transport_tcp.c */