remove port from transport section because it was getting in the way of making sense...
[oweals/gnunet.git] / src / transport / plugin_transport_tcp.c
index ffc738f5312465f368ef9f39f99f57de8786b673..8c1d3d3339399447cc771b76c72ab1ff128df3c7 100644 (file)
@@ -37,8 +37,8 @@
 #include "plugin_transport.h"
 #include "transport.h"
 
-#define DEBUG_TCP GNUNET_YES
-#define DEBUG_TCP_NAT GNUNET_YES
+#define DEBUG_TCP GNUNET_NO
+#define DEBUG_TCP_NAT GNUNET_NO
 
 /**
  * How long until we give up on transmitting the welcome message?
@@ -304,6 +304,11 @@ struct Session
    */
   int inbound;
 
+  /**
+   * Was this session created using NAT traversal?
+   */
+  int is_nat;
+
 };
 
 
@@ -585,13 +590,14 @@ create_session (struct Plugin *plugin,
     GNUNET_assert (client == NULL);
 
 #if DEBUG_TCP
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   "Creating new session for peer `%4s'\n",
                   GNUNET_i2s (target));
 #endif
   ret = GNUNET_malloc (sizeof (struct Session));
   ret->last_activity = GNUNET_TIME_absolute_get ();
   ret->plugin = plugin;
+  ret->is_nat = is_nat;
   if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
     {
       ret->next = plugin->sessions;
@@ -1078,11 +1084,11 @@ tcp_plugin_send (void *cls,
            }
          if (session->inbound == GNUNET_YES)
            continue;
-         if (addrlen != session->connect_alen)
+         if ((addrlen != session->connect_alen) && (session->is_nat == GNUNET_NO))
            continue;
-         if (0 != memcmp (session->connect_addr,
+         if ((0 != memcmp (session->connect_addr,
                           addr,
-                          addrlen))
+                          addrlen)) && (session->is_nat == GNUNET_NO))
            continue;
          cand_session = select_better_session (cand_session,
                                                session);       
@@ -1152,12 +1158,12 @@ tcp_plugin_send (void *cls,
         return -1; /* NAT client only works with IPv4 addresses */
 
 
-      if ( (plugin->allow_nat == GNUNET_YES) && (is_natd == GNUNET_YES) &&
+      if ((plugin->allow_nat == GNUNET_YES) && (is_natd == GNUNET_YES) &&
            (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns, &target->hashPubKey)))
         {
 #if DEBUG_TCP_NAT
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                           _("Found valid IPv4 NAT address!\n"));
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                           _("Found valid IPv4 NAT address (creating session)!\n"));
 #endif
           session = create_session (plugin,
                                     target,
@@ -1183,7 +1189,7 @@ tcp_plugin_send (void *cls,
 
           GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns, &target->hashPubKey, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
 #if DEBUG_TCP_NAT
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                            "Created NAT WAIT connection to `%4s' at `%s'\n",
                            GNUNET_i2s (target),
                            GNUNET_a2s (sb, sbs));
@@ -1213,7 +1219,7 @@ tcp_plugin_send (void *cls,
          return -1;
        }
 #if DEBUG_TCP
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                        "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
                       GNUNET_i2s (target),
                       GNUNET_a2s (sb, sbs));
@@ -1573,17 +1579,13 @@ handle_tcp_nat_probe (void *cls,
       GNUNET_SERVER_client_keep (client);
       session->client = client;
       session->last_activity = GNUNET_TIME_absolute_get ();
-      /* FIXME: Should this be inbound or outbound?
-       * I think it should be outbound because we technically
-       * initiated it... But something goes wrong somewhere. */
-      /* session->inbound = GNUNET_YES; */
-      session->inbound = GNUNET_YES;
+      session->inbound = GNUNET_NO;
 
       if (GNUNET_OK ==
           GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
         {
 #if DEBUG_TCP_NAT
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                            "Found address `%s' for incoming connection %p\n",
                            GNUNET_a2s (vaddr, alen),
                            client);
@@ -1673,6 +1675,12 @@ handle_tcp_welcome (void *cls,
   if (session == NULL)
     {
       GNUNET_SERVER_client_keep (client);
+#if DEBUG_TCP_NAT
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                       "Received %s message from a `%4s/%p', creating session\n",
+                       "WELCOME",
+                       GNUNET_i2s (&wm->clientIdentity), client);
+#endif
       session = create_session (plugin,
                                &wm->clientIdentity, client, GNUNET_NO);
       session->inbound = GNUNET_YES;
@@ -1932,10 +1940,9 @@ process_interfaces (void *cls,
       GNUNET_break (0);
       return GNUNET_OK;
     }
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO |
-                   GNUNET_ERROR_TYPE_BULK,
-                  _("Found address `%s' (%s)\n"),
-                   GNUNET_a2s (addr, addrlen), name);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  _("Found address `%s' (%s) len %d\n"),
+                   GNUNET_a2s (addr, addrlen), name, args);
 
   plugin->env->notify_address (plugin->env->cls,
                                "tcp",
@@ -1943,10 +1950,9 @@ process_interfaces (void *cls,
 
   if (arg_nat != NULL)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO |
-                       GNUNET_ERROR_TYPE_BULK,
-                      _("Found address `%s' (%s)\n"),
-                      GNUNET_a2s (addr, addrlen), name);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      _("Found address `%s' (%s) len %d\n"),
+                      GNUNET_a2s (addr, addrlen), name, args);
       plugin->env->notify_address (plugin->env->cls,
                                    "tcp",
                                    arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
@@ -2468,11 +2474,12 @@ libgnunet_plugin_transport_tcp_init (void *cls)
       add_to_address_list (plugin, &t4.ipv4_addr, sizeof (uint32_t));
       plugin->env->notify_address (plugin->env->cls,
                                   "tcp",
-                                  &t4, sizeof(t4), GNUNET_TIME_UNIT_FOREVER_REL);
+                                   &t4, sizeof(t4), GNUNET_TIME_UNIT_FOREVER_REL);
     }
   else if ((plugin->external_address != NULL) && (inet_pton(AF_INET, plugin->external_address, &t4.ipv4_addr) == 1))
     {
       t4.t_port = htons(plugin->adv_port);
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Notifying transport of address %s:%d\n", plugin->external_address, plugin->adv_port);
       add_to_address_list (plugin, &t4.ipv4_addr, sizeof (uint32_t));
       plugin->env->notify_address (plugin->env->cls,
                                    "tcp",