trying to fix #4003
[oweals/gnunet.git] / src / transport / plugin_transport_wlan.c
index b0be343463d39d4bb8c847ab04352b9790aa0dfb..d5932bb30df2b70e9dd9480c1c6230a79dbddce1 100644 (file)
@@ -1,6 +1,6 @@
 /*
   This file is part of GNUnet
-  (C) 2010-2014 Christian Grothoff (and other contributing authors)
+  Copyright (C) 2010-2014 Christian Grothoff (and other contributing authors)
 
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
   You should have received a copy of the GNU General Public License
   along with GNUnet; see the file COPYING.  If not, write to the
-  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-  Boston, MA 02111-1307, USA.
+  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+  Boston, MA 02110-1301, USA.
 */
 
 /**
 #define WLAN_MTU 1430
 
 
+/**
+ * Which network scope do we belong to?
+ */
+#if BUILD_WLAN
+static const enum GNUNET_ATS_Network_Type scope = GNUNET_ATS_NET_WLAN;
+#else
+static const enum GNUNET_ATS_Network_Type scope = GNUNET_ATS_NET_BT;
+#endif
+
 
 /**
  * Maximum number of messages in defragmentation queue per MAC
@@ -307,6 +316,12 @@ struct FragmentMessage
    */
   GNUNET_TRANSPORT_TransmitContinuation cont;
 
+  /**
+   * Message we need to fragment and transmit, NULL after the
+   * @e fragmentcontext has been created.
+   */
+  struct GNUNET_MessageHeader *msg;
+
   /**
    * Closure for @e cont
    */
@@ -484,7 +499,7 @@ struct Plugin
   /**
    * Task that periodically sends a HELLO beacon via the helper.
    */
-  struct GNUNET_SCHEDULER_Task * beacon_task;
+  struct GNUNET_SCHEDULER_Task *beacon_task;
 
   /**
    * Tracker for bandwidth limit
@@ -688,7 +703,8 @@ get_wlan_header (struct Plugin *plugin,
  * @param hdr pointer to the hdr where the ack is stored
  */
 static void
-send_ack (void *cls, uint32_t msg_id,
+send_ack (void *cls,
+          uint32_t msg_id,
          const struct GNUNET_MessageHeader *hdr)
 {
   struct MacEndpoint *endpoint = cls;
@@ -708,7 +724,8 @@ send_ack (void *cls, uint32_t msg_id,
     return;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Sending ACK to helper\n");
+       "Sending ACK to %s\n",
+       mac_to_string (&endpoint->wlan_addr.mac));
   radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
   get_radiotap_header (endpoint, radio_header, size);
   get_wlan_header (endpoint->plugin,
@@ -939,7 +956,6 @@ fragment_transmission_done (void *cls,
 {
   struct FragmentMessage *fm = cls;
 
-
   fm->sh = NULL;
   GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
 }
@@ -973,6 +989,11 @@ transmit_fragment (void *cls,
 
     radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
     get_radiotap_header (endpoint, radio_header, size);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Sending %u bytes of data to MAC `%s'\n",
+        (unsigned int) msize,
+        mac_to_string (&endpoint->wlan_addr.mac));
+
     get_wlan_header (endpoint->plugin,
                     &radio_header->frame,
                     &endpoint->wlan_addr.mac,
@@ -985,12 +1006,16 @@ transmit_fragment (void *cls,
                                 &fragment_transmission_done, fm);
     fm->size_on_wire += size;
     if (NULL != fm->sh)
+    {
       GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
                                 _("# message fragments sent"),
                                1,
                                 GNUNET_NO);
+    }
     else
+    {
       GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
+    }
     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
                               "# bytes currently in buffers",
                               -msize, GNUNET_NO);
@@ -1022,10 +1047,19 @@ free_fragment_message (struct FragmentMessage *fm)
     GNUNET_HELPER_send_cancel (fm->sh);
     fm->sh = NULL;
   }
-  GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
-                                   &endpoint->msg_delay,
-                                   &endpoint->ack_delay);
-  if (fm->timeout_task != NULL)
+  if (NULL != fm->msg)
+  {
+    GNUNET_free (fm->msg);
+    fm->msg = NULL;
+  }
+  if (NULL != fm->fragcontext)
+  {
+    GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
+                                     &endpoint->msg_delay,
+                                     &endpoint->ack_delay);
+    fm->fragcontext = NULL;
+  }
+  if (NULL != fm->timeout_task)
   {
     GNUNET_SCHEDULER_cancel (fm->timeout_task);
     fm->timeout_task = NULL;
@@ -1096,17 +1130,25 @@ send_with_fragmentation (struct MacEndpoint *endpoint,
   fm->cont = cont;
   fm->cont_cls = cont_cls;
   /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
-  fm->fragcontext =
-    GNUNET_FRAGMENT_context_create (plugin->env->stats,
-                                    WLAN_MTU,
-                                   &plugin->tracker,
-                                   endpoint->msg_delay,
-                                   endpoint->ack_delay,
-                                   msg,
-                                   &transmit_fragment, fm);
   fm->timeout_task =
     GNUNET_SCHEDULER_add_delayed (timeout,
-                                 &fragmentmessage_timeout, fm);
+                                  &fragmentmessage_timeout,
+                                  fm);
+  if (GNUNET_YES == plugin->have_mac)
+  {
+    fm->fragcontext =
+      GNUNET_FRAGMENT_context_create (plugin->env->stats,
+                                      WLAN_MTU,
+                                      &plugin->tracker,
+                                      fm->macendpoint->msg_delay,
+                                      fm->macendpoint->ack_delay,
+                                      msg,
+                                      &transmit_fragment, fm);
+  }
+  else
+  {
+    fm->msg = GNUNET_copy_message (msg);
+  }
   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
                                    endpoint->sending_messages_tail,
                                    fm);
@@ -1249,6 +1291,25 @@ wlan_plugin_get_network (void *cls,
 }
 
 
+/**
+ * Function obtain the network type for an address.
+ *
+ * @param cls closure (`struct Plugin *`)
+ * @param address the address
+ * @return the network type
+ */
+static enum GNUNET_ATS_Network_Type
+wlan_plugin_get_network_for_address (void *cls,
+                                    const struct GNUNET_HELLO_Address *address)
+{
+#if BUILD_WLAN
+  return GNUNET_ATS_NET_WLAN;
+#else
+  return GNUNET_ATS_NET_BT;
+#endif
+}
+
+
 /**
  * Creates a new outbound session the transport service will use to
  * send data to the peer
@@ -1362,13 +1423,13 @@ wlan_plugin_send (void *cls,
   wlanheader->sender = *plugin->env->my_identity;
   wlanheader->target = session->target;
   wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
-  memcpy (&wlanheader[1], msgbuf, msgbuf_size);
-
+  memcpy (&wlanheader[1],
+          msgbuf,
+          msgbuf_size);
   GNUNET_STATISTICS_update (plugin->env->stats,
                             "# bytes currently in buffers",
                             msgbuf_size,
                             GNUNET_NO);
-
   send_with_fragmentation (session->mac,
                           to,
                           &session->target,
@@ -1395,20 +1456,12 @@ process_data (void *cls,
   struct Plugin *plugin = cls;
   struct GNUNET_HELLO_Address *address;
   struct MacAndSession *mas = client;
-  struct MacAndSession xmas;
-  struct GNUNET_ATS_Information ats;
   struct FragmentMessage *fm;
   struct GNUNET_PeerIdentity tmpsource;
   const struct WlanHeader *wlanheader;
   int ret;
   uint16_t msize;
 
-  ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
-#if BUILD_WLAN
-  ats.value = htonl (GNUNET_ATS_NET_WLAN);
-#else
-  ats.value = htonl (GNUNET_ATS_NET_BT);
-#endif
   msize = ntohs (hdr->size);
 
   GNUNET_STATISTICS_update (plugin->env->stats,
@@ -1447,15 +1500,22 @@ process_data (void *cls,
                                              PLUGIN_NAME,
                                              &mas->endpoint->wlan_addr,
                                              sizeof (mas->endpoint->wlan_addr),
-                                             GNUNET_HELLO_ADDRESS_INFO_INBOUND);
+                                             GNUNET_HELLO_ADDRESS_INFO_NONE);
+    mas->session = lookup_session (mas->endpoint,
+                                   &tmpsource);
+    if (NULL == mas->session)
+    {
+      mas->session = create_session (mas->endpoint,
+                                     &tmpsource);
+      plugin->env->session_start (plugin->env->cls,
+                                  address,
+                                  mas->session,
+                                  scope);
+    }
     plugin->env->receive (plugin->env->cls,
                           address,
                           mas->session,
                           hdr);
-    plugin->env->update_address_metrics (plugin->env->cls,
-                                         address,
-                                         mas->session,
-                                         &ats, 1);
     GNUNET_HELLO_address_free (address);
     break;
   case GNUNET_MESSAGE_TYPE_FRAGMENT:
@@ -1520,11 +1580,12 @@ process_data (void *cls,
         break;
       }
     }
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "ACK not matched against any active fragmentation with MAC `%s'\n",
-         wlan_plugin_address_to_string (NULL,
-                                        &mas->endpoint->wlan_addr,
-                                        sizeof (mas->endpoint->wlan_addr)));
+    if (NULL == fm)
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "ACK not matched against any active fragmentation with MAC `%s'\n",
+           wlan_plugin_address_to_string (NULL,
+                                          &mas->endpoint->wlan_addr,
+                                          sizeof (mas->endpoint->wlan_addr)));
     break;
   case GNUNET_MESSAGE_TYPE_WLAN_DATA:
     if (NULL == mas->endpoint)
@@ -1557,34 +1618,34 @@ process_data (void *cls,
                                GNUNET_NO);
       break;
     }
-    xmas.endpoint = mas->endpoint;
-    if (NULL == (xmas.session = lookup_session (mas->endpoint,
-                                                &wlanheader->sender)))
+    mas->session = lookup_session (mas->endpoint,
+                                   &wlanheader->sender);
+    if (NULL == mas->session)
     {
-      xmas.session = create_session (mas->endpoint,
+      mas->session = create_session (mas->endpoint,
                                      &wlanheader->sender);
       address = GNUNET_HELLO_address_allocate (&wlanheader->sender,
                                                PLUGIN_NAME,
                                                &mas->endpoint->wlan_addr,
                                                sizeof (struct WlanAddress),
                                                GNUNET_HELLO_ADDRESS_INFO_NONE);
-      plugin->env->session_start (NULL,
+      plugin->env->session_start (plugin->env->cls,
                                   address,
-                                  xmas.session,
-                                  NULL, 0);
+                                  mas->session,
+                                  scope);
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-          "Notifying transport about peer `%s''s new session %p \n",
+           "Notifying transport about peer `%s''s new session %p \n",
            GNUNET_i2s (&wlanheader->sender),
-           xmas.session);
+           mas->session);
       GNUNET_HELLO_address_free (address);
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Processing %u bytes of DATA from peer `%s'\n",
         (unsigned int) msize,
         GNUNET_i2s (&wlanheader->sender));
-    xmas.session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
+    mas->session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
     (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
-                                     &xmas,
+                                     mas,
                                      (const char *) &wlanheader[1],
                                      msize - sizeof (struct WlanHeader),
                                      GNUNET_YES,
@@ -1610,16 +1671,67 @@ process_data (void *cls,
                           mas->session->address,
                           mas->session,
                           hdr);
-    plugin->env->update_address_metrics (plugin->env->cls,
-                                         mas->session->address,
-                                         mas->session,
-                                         &ats, 1);
     break;
   }
   return GNUNET_OK;
 }
 
 
+/**
+ * Task to (periodically) send a HELLO beacon
+ *
+ * @param cls pointer to the plugin struct
+ * @param tc scheduler context
+ */
+static void
+send_hello_beacon (void *cls,
+                  const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct Plugin *plugin = cls;
+  uint16_t size;
+  uint16_t hello_size;
+  struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
+  const struct GNUNET_MessageHeader *hello;
+
+  hello = plugin->env->get_our_hello ();
+  if (NULL != hello)
+  {
+    hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
+    GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
+    size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
+  {
+    char buf[size] GNUNET_ALIGN;
+
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Sending %u byte HELLO beacon\n",
+        (unsigned int) size);
+    radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
+    get_radiotap_header (NULL, radioHeader, size);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Broadcasting %u bytes of data to MAC `%s'\n",
+        (unsigned int) size,
+        mac_to_string (&bc_all_mac));
+    get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
+    memcpy (&radioHeader[1], hello, hello_size);
+    if (NULL !=
+       GNUNET_HELPER_send (plugin->suid_helper,
+                           &radioHeader->header,
+                           GNUNET_YES /* can drop */,
+                           NULL, NULL))
+      GNUNET_STATISTICS_update (plugin->env->stats,
+                                _("# HELLO beacons sent"),
+                               1, GNUNET_NO);
+  } }
+  plugin->beacon_task =
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                 (HELLO_BEACON_SCALING_FACTOR,
+                                  plugin->mac_count + 1),
+                                 &send_hello_beacon,
+                                 plugin);
+
+}
+
+
 /**
  * Function used for to process the data from the suid process
  *
@@ -1638,6 +1750,8 @@ handle_helper_message (void *cls, void *client,
   struct WlanAddress wa;
   struct MacAndSession mas;
   uint16_t msize;
+  struct FragmentMessage *fm;
+  struct MacEndpoint *endpoint;
 
   msize = ntohs (hdr->size);
   switch (ntohs (hdr->type))
@@ -1667,9 +1781,38 @@ handle_helper_message (void *cls, void *client,
                                    GNUNET_NO,
                                    my_address);
       GNUNET_HELLO_address_free (my_address);
+      plugin->mac_address = cm->mac;
+    }
+    else
+    {
+      plugin->mac_address = cm->mac;
+      plugin->have_mac = GNUNET_YES;
+      for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
+      {
+        for (fm = endpoint->sending_messages_head; NULL != fm; fm = fm->next)
+        {
+          if (NULL != fm->fragcontext)
+          {
+            GNUNET_break (0); /* should not happen */
+            continue;
+          }
+          fm->fragcontext =
+            GNUNET_FRAGMENT_context_create (plugin->env->stats,
+                                            WLAN_MTU,
+                                            &plugin->tracker,
+                                            fm->macendpoint->msg_delay,
+                                            fm->macendpoint->ack_delay,
+                                            fm->msg,
+                                            &transmit_fragment, fm);
+          GNUNET_free (fm->msg);
+          fm->msg = NULL;
+        }
+      }
+      GNUNET_break (NULL == plugin->beacon_task);
+      plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
+                                                      plugin);
+
     }
-    plugin->mac_address = cm->mac;
-    plugin->have_mac = GNUNET_YES;
 
     memset (&wa, 0, sizeof (struct WlanAddress));
     wa.mac = plugin->mac_address;
@@ -1733,6 +1876,14 @@ handle_helper_message (void *cls, void *client,
         "Receiving %u bytes of data from MAC `%s'\n",
         (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
         mac_to_string (&rxinfo->frame.addr2));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Receiving %u bytes of data to MAC `%s'\n",
+        (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
+        mac_to_string (&rxinfo->frame.addr1));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Receiving %u bytes of data with BSSID MAC `%s'\n",
+        (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
+        mac_to_string (&rxinfo->frame.addr3));
     wa.mac = rxinfo->frame.addr2;
     wa.options = htonl (0);
     mas.endpoint = create_macendpoint (plugin, &wa);
@@ -1754,55 +1905,6 @@ handle_helper_message (void *cls, void *client,
 }
 
 
-/**
- * Task to (periodically) send a HELLO beacon
- *
- * @param cls pointer to the plugin struct
- * @param tc scheduler context
- */
-static void
-send_hello_beacon (void *cls,
-                  const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct Plugin *plugin = cls;
-  uint16_t size;
-  uint16_t hello_size;
-  struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
-  const struct GNUNET_MessageHeader *hello;
-
-  hello = plugin->env->get_our_hello ();
-  hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
-  GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
-  size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
-  {
-    char buf[size] GNUNET_ALIGN;
-
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "Sending %u byte HELLO beacon\n",
-        (unsigned int) size);
-    radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
-    get_radiotap_header (NULL, radioHeader, size);
-    get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
-    memcpy (&radioHeader[1], hello, hello_size);
-    if (NULL !=
-       GNUNET_HELPER_send (plugin->suid_helper,
-                           &radioHeader->header,
-                           GNUNET_YES /* can drop */,
-                           NULL, NULL))
-      GNUNET_STATISTICS_update (plugin->env->stats,
-                                _("# HELLO beacons sent"),
-                               1, GNUNET_NO);
-  }
-  plugin->beacon_task =
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
-                                 (HELLO_BEACON_SCALING_FACTOR,
-                                  plugin->mac_count + 1),
-                                 &send_hello_beacon,
-                                 plugin);
-
-}
-
-
 /**
  * Another peer has suggested an address for this
  * peer and transport plugin.  Check that this could be a valid
@@ -2188,11 +2290,12 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
                                  GNUNET_BANDWIDTH_value_init (100 * 1024 *
                                                               1024 / 8),
                                  100);
-  plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
-  plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
-  plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
-  plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
-                                                 plugin);
+  plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data,
+                                                              plugin);
+  plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data,
+                                                                    plugin);
+  plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data,
+                                                               plugin);
 
   plugin->options = 0;
 
@@ -2248,6 +2351,7 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
   api->address_to_string = &wlan_plugin_address_to_string;
   api->string_to_address = &wlan_plugin_string_to_address;
   api->get_network = &wlan_plugin_get_network;
+  api->get_network_for_address = &wlan_plugin_get_network_for_address;
   api->update_session_timeout = &wlan_plugin_update_session_timeout;
   api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
   api->setup_monitor = &wlan_plugin_setup_monitor;