-more logging, avoid duplicate re-scheduling
[oweals/gnunet.git] / src / transport / plugin_transport_wlan.c
index e5080c0464e253044b51a9cfa02456d137f442a5..f9f76ac7c5f9041faf6e2a8ac3f043bf0a6e0a7e 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
@@ -212,7 +221,7 @@ struct PendingMessage
 /**
  * Session handle for connections with other peers.
  */
-struct Session
+struct GNUNET_ATS_Session
 {
   /**
    * To whom are we talking to (set to our identity
@@ -224,13 +233,13 @@ struct Session
    * We keep all sessions in a DLL at their respective
    * `struct MACEndpoint *`.
    */
-  struct Session *next;
+  struct GNUNET_ATS_Session *next;
 
   /**
    * We keep all sessions in a DLL at their respective
    * `struct MACEndpoint *`.
    */
-  struct Session *prev;
+  struct GNUNET_ATS_Session *prev;
 
   /**
    * MAC endpoint with the address of this peer.
@@ -355,12 +364,12 @@ struct MacEndpoint
   /**
    * Head of sessions that use this MAC.
    */
-  struct Session *sessions_head;
+  struct GNUNET_ATS_Session *sessions_head;
 
   /**
    * Tail of sessions that use this MAC.
    */
-  struct Session *sessions_tail;
+  struct GNUNET_ATS_Session *sessions_tail;
 
   /**
    * Head of messages we are currently sending to this MAC.
@@ -530,7 +539,7 @@ struct MacAndSession
   /**
    * NULL if the identity of the other peer is not known.
    */
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
 
   /**
    * MAC address of the other peer, NULL if not known.
@@ -607,7 +616,7 @@ wlan_plugin_address_to_string (void *cls,
  */
 static void
 notify_session_monitor (struct Plugin *plugin,
-                        struct Session *session,
+                        struct GNUNET_ATS_Session *session,
                         enum GNUNET_TRANSPORT_SessionState state)
 {
   struct GNUNET_TRANSPORT_SessionInfo info;
@@ -771,7 +780,7 @@ wlan_data_message_handler (void *cls,
  */
 static int
 wlan_plugin_disconnect_session (void *cls,
-                                struct Session *session)
+                                struct GNUNET_ATS_Session *session)
 {
   struct MacEndpoint *endpoint = session->mac;
   struct Plugin *plugin = endpoint->plugin;
@@ -825,7 +834,7 @@ static void
 session_timeout (void *cls,
                  const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct Session *session = cls;
+  struct GNUNET_ATS_Session *session = cls;
   struct GNUNET_TIME_Relative left;
 
   session->timeout_task = NULL;
@@ -851,11 +860,11 @@ session_timeout (void *cls,
  * @param peer peer identity to use for this session
  * @return returns the session or NULL
  */
-static struct Session *
+static struct GNUNET_ATS_Session *
 lookup_session (struct MacEndpoint *endpoint,
                 const struct GNUNET_PeerIdentity *peer)
 {
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
 
   for (session = endpoint->sessions_head; NULL != session; session = session->next)
     if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
@@ -871,17 +880,17 @@ lookup_session (struct MacEndpoint *endpoint,
  * @param peer peer identity to use for this session
  * @return returns the session or NULL
  */
-static struct Session *
+static struct GNUNET_ATS_Session *
 create_session (struct MacEndpoint *endpoint,
                 const struct GNUNET_PeerIdentity *peer)
 {
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
 
   GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
                             _("# Sessions allocated"),
                             1,
                             GNUNET_NO);
-  session = GNUNET_new (struct Session);
+  session = GNUNET_new (struct GNUNET_ATS_Session);
   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
                                     endpoint->sessions_tail,
                                    session);
@@ -919,11 +928,11 @@ create_session (struct MacEndpoint *endpoint,
  * @param peer peer identity to use for this session
  * @return returns the session
  */
-static struct Session *
+static struct GNUNET_ATS_Session *
 get_session (struct MacEndpoint *endpoint,
              const struct GNUNET_PeerIdentity *peer)
 {
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
 
   if (NULL != (session = lookup_session (endpoint, peer)))
     return session;
@@ -1156,7 +1165,7 @@ free_macendpoint (struct MacEndpoint *endpoint)
 {
   struct Plugin *plugin = endpoint->plugin;
   struct FragmentMessage *fm;
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
 
   GNUNET_STATISTICS_update (plugin->env->stats,
                            _("# MAC endpoints allocated"),
@@ -1272,7 +1281,26 @@ create_macendpoint (struct Plugin *plugin,
  */
 static enum GNUNET_ATS_Network_Type
 wlan_plugin_get_network (void *cls,
-                         struct Session *session)
+                         struct GNUNET_ATS_Session *session)
+{
+#if BUILD_WLAN
+  return GNUNET_ATS_NET_WLAN;
+#else
+  return GNUNET_ATS_NET_BT;
+#endif
+}
+
+
+/**
+ * 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;
@@ -1290,7 +1318,7 @@ wlan_plugin_get_network (void *cls,
  * @param address the address
  * @return the session or NULL of max connections exceeded
  */
-static struct Session *
+static struct GNUNET_ATS_Session *
 wlan_plugin_get_session (void *cls,
                         const struct GNUNET_HELLO_Address *address)
 {
@@ -1329,7 +1357,7 @@ wlan_plugin_disconnect_peer (void *cls,
                              const struct GNUNET_PeerIdentity *target)
 {
   struct Plugin *plugin = cls;
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
   struct MacEndpoint *endpoint;
 
   for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
@@ -1372,7 +1400,7 @@ wlan_plugin_disconnect_peer (void *cls,
  */
 static ssize_t
 wlan_plugin_send (void *cls,
-                  struct Session *session,
+                  struct GNUNET_ATS_Session *session,
                   const char *msgbuf, size_t msgbuf_size,
                   unsigned int priority,
                   struct GNUNET_TIME_Relative to,
@@ -1428,19 +1456,12 @@ process_data (void *cls,
   struct Plugin *plugin = cls;
   struct GNUNET_HELLO_Address *address;
   struct MacAndSession *mas = client;
-  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,
@@ -1489,16 +1510,12 @@ process_data (void *cls,
       plugin->env->session_start (plugin->env->cls,
                                   address,
                                   mas->session,
-                                  &ats, 1);
+                                  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:
@@ -1563,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)
@@ -1614,7 +1632,7 @@ process_data (void *cls,
       plugin->env->session_start (plugin->env->cls,
                                   address,
                                   mas->session,
-                                  NULL, 0);
+                                  scope);
       LOG (GNUNET_ERROR_TYPE_DEBUG,
            "Notifying transport about peer `%s''s new session %p \n",
            GNUNET_i2s (&wlanheader->sender),
@@ -1653,10 +1671,6 @@ 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;
@@ -1680,9 +1694,11 @@ send_hello_beacon (void *cls,
   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;
+  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;
 
@@ -1705,7 +1721,7 @@ send_hello_beacon (void *cls,
       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,
@@ -2127,7 +2143,7 @@ wlan_plugin_setup_monitor (void *cls,
 {
   struct Plugin *plugin = cls;
   struct MacEndpoint *mac;
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
 
   plugin->sic = sic;
   plugin->sic_cls = sic_cls;
@@ -2161,7 +2177,7 @@ wlan_plugin_setup_monitor (void *cls,
 static void
 wlan_plugin_update_session_timeout (void *cls,
                                     const struct GNUNET_PeerIdentity *peer,
-                                    struct Session *session)
+                                    struct GNUNET_ATS_Session *session)
 {
   GNUNET_assert (NULL != session->timeout_task);
   session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
@@ -2181,7 +2197,7 @@ wlan_plugin_update_session_timeout (void *cls,
 static void
 wlan_plugin_update_inbound_delay (void *cls,
                                   const struct GNUNET_PeerIdentity *peer,
-                                  struct Session *session,
+                                  struct GNUNET_ATS_Session *session,
                                   struct GNUNET_TIME_Relative delay)
 {
   /* does nothing, as inbound delay is not supported by WLAN */
@@ -2335,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;