Removing all the tests for the bluetooth transport plugin.
[oweals/gnunet.git] / src / transport / plugin_transport_wlan.c
index 281c3a82c248d216f6d7eadd8c70dd33fcaeb9e7..ec09476487aaac2bd28cdd6bad6847800cb9373e 100644 (file)
@@ -105,6 +105,16 @@ struct WlanHeader
      multiple messages! */
 
 };
+
+
+struct WlanAddress
+{
+  uint32_t options GNUNET_PACKED;
+
+  struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
+};
+
+
 GNUNET_NETWORK_STRUCT_END
 
 
@@ -148,13 +158,6 @@ struct PendingMessage
 
 };
 
-GNUNET_NETWORK_STRUCT_BEGIN
-struct WlanAddress
-{
-       uint32_t options GNUNET_PACKED;
-  struct GNUNET_TRANSPORT_WLAN_MacAddress mac  GNUNET_PACKED;
-};
-GNUNET_NETWORK_STRUCT_END
 
 /**
  * Session handle for connections with other peers.
@@ -209,6 +212,8 @@ struct Session
    */
   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
 
+  int inbound;
+
 };
 
 
@@ -348,6 +353,11 @@ struct MacEndpoint
    */
   struct WlanAddress addr;
 
+  /**
+   * Inbound or outbound session
+   */
+  int inbound;
+
   /**
    * Message delay for fragmentation context
    */
@@ -585,6 +595,12 @@ send_ack (void *cls, uint32_t msg_id,
   size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
   char buf[size];
 
+  if (NULL == endpoint)
+  {
+       GNUNET_break (0);
+       return;
+  }
+
   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
     GNUNET_break (0);
@@ -689,7 +705,7 @@ session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
-  if (0 == timeout.rel_value) 
+  if (0 == timeout.rel_value_us
   {
     free_session (session);
     return;
@@ -699,45 +715,86 @@ session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+
 /**
- * Create a new session
+ * Lookup a new session
  *
  * @param endpoint pointer to the mac endpoint of the peer
  * @param peer peer identity to use for this session
- * @return returns the session
+ * @return returns the session or NULL
  */
 static struct Session *
-create_session (struct MacEndpoint *endpoint,
-                const struct GNUNET_PeerIdentity *peer)
+lookup_session (struct MacEndpoint *endpoint,
+                const struct GNUNET_PeerIdentity *peer,
+                int inbound)
 {
   struct Session *session;
 
   for (session = endpoint->sessions_head; NULL != session; session = session->next)
-    if (0 == memcmp (peer, &session->target,
-                    sizeof (struct GNUNET_PeerIdentity)))
+    if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)) &&
+               (session->inbound == inbound))
     {
       session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
       return session;
     }
+  return NULL;
+}
+
+/**
+ * Create a new session
+ *
+ * @param endpoint pointer to the mac endpoint of the peer
+ * @param peer peer identity to use for this session
+ * @return returns the session or NULL
+ */
+static struct Session *
+create_session (struct MacEndpoint *endpoint,
+                const struct GNUNET_PeerIdentity *peer, int inbound)
+{
+  struct Session *session;
+
   GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN sessions allocated"), 1,
                             GNUNET_NO);
   session = GNUNET_malloc (sizeof (struct Session));
   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
                                     endpoint->sessions_tail,
                                    session);
+  session->inbound = inbound;
   session->mac = endpoint;
   session->target = *peer;
   session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
   session->timeout_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, 
-       "Created new session for peer `%s' with endpoint %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Created new %s session %p for peer `%s' with endpoint %s\n",
+       (GNUNET_YES == inbound) ? "inbound" : "outbound",
+       session,
        GNUNET_i2s (peer),
-       mac_to_string (&endpoint->addr));
+       mac_to_string (&endpoint->addr.mac));
+
   return session;
 }
 
 
+/**
+ * Look up a session for a peer and create a new session if none is found
+ *
+ * @param endpoint pointer to the mac endpoint of the peer
+ * @param peer peer identity to use for this session
+ * @return returns the session
+ */
+static struct Session *
+get_session (struct MacEndpoint *endpoint,
+                const struct GNUNET_PeerIdentity *peer,
+                int inbound)
+{
+  struct Session *session;
+  if (NULL != (session = lookup_session (endpoint, peer, inbound)))
+       return session;
+  return create_session (endpoint, peer, inbound);
+}
+
+
 /**
  * Function called once we have successfully given the fragment
  * message to the SUID helper process and we are thus ready for
@@ -774,6 +831,12 @@ transmit_fragment (void *cls,
   size_t size;
   uint16_t msize;
 
+  if (NULL == endpoint)
+  {
+       GNUNET_break (0);
+       return;
+  }
+
   msize = ntohs (hdr->size);
   size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
   {
@@ -965,7 +1028,7 @@ macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
-  if (0 == timeout.rel_value) 
+  if (0 == timeout.rel_value_us
   {
     free_macendpoint (endpoint);
     return;
@@ -1020,6 +1083,22 @@ create_macendpoint (struct Plugin *plugin,
 }
 
 
+/**
+ * Function obtain the network type for a session
+ *
+ * @param cls closure ('struct Plugin*')
+ * @param session the session
+ * @return the network type in HBO or GNUNET_SYSERR
+ */
+static enum GNUNET_ATS_Network_Type
+wlan_get_network (void *cls, 
+                 struct Session *session)
+{
+  GNUNET_assert (NULL != session);
+  return GNUNET_ATS_NET_WLAN;
+}
+
+
 /**
  * Creates a new outbound session the transport service will use to send data to the
  * peer
@@ -1047,7 +1126,7 @@ wlan_plugin_get_session (void *cls,
        GNUNET_i2s (&address->peer),
        wlan_plugin_address_to_string(NULL, address->address, address->address_length));
   endpoint = create_macendpoint (plugin, address->address);
-  return create_session (endpoint, &address->peer);
+  return get_session (endpoint, &address->peer, GNUNET_NO);
 }
 
 
@@ -1183,7 +1262,7 @@ process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
       GNUNET_break_op (0);
       break;
     }
-    LOG (GNUNET_ERROR_TYPE_DEBUG, 
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
         (unsigned int) msize,
         GNUNET_i2s (&tmpsource),
@@ -1197,11 +1276,11 @@ process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
                          hdr, 
                          mas->session,
                          (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
-                         (mas->endpoint == NULL) ? 0 : sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
+                         (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress));
     plugin->env->update_address_metrics (plugin->env->cls,
                                         &tmpsource,
                                         (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
-                                        (mas->endpoint == NULL) ? 0 : sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress),
+                                        (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress),
                                         mas->session,
                                         &ats, 1);
     break;
@@ -1288,9 +1367,17 @@ process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
       break;
     }
     xmas.endpoint = mas->endpoint;
-    xmas.session = create_session (mas->endpoint, &wlanheader->sender);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, 
-        "Processing %u bytes of WLAN DATA from peer `%s'\n",
+    if (NULL == (xmas.session =  lookup_session (mas->endpoint, &wlanheader->sender, GNUNET_YES)))
+    {
+       xmas.session = create_session (mas->endpoint, &wlanheader->sender, GNUNET_YES);
+       plugin->env->session_start (NULL, &wlanheader->sender,
+                       PLUGIN_NAME, NULL, 0, xmas.session, NULL, 0);
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+               "Notifying transport about peer `%s''s new inbound session %p \n",
+               GNUNET_i2s (&wlanheader->sender), xmas.session);
+    }
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+               "Processing %u bytes of WLAN DATA from peer `%s'\n",
         (unsigned int) msize,
         GNUNET_i2s (&wlanheader->sender));
     (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer, 
@@ -1320,11 +1407,11 @@ process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
                          hdr, 
                          mas->session,
                          (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
-                         (mas->endpoint == NULL) ? 0 : sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
+                         (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress));
     plugin->env->update_address_metrics (plugin->env->cls,
                                         &mas->session->target,
                                         (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
-                                        (mas->endpoint == NULL) ? 0 : sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress),
+                                        (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress),
                                         mas->session,
                                         &ats, 1);
     break;
@@ -1575,7 +1662,6 @@ wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
   GNUNET_snprintf (macstr, sizeof (macstr), "%s.%u.%s",
                PLUGIN_NAME, ntohl (((struct WlanAddress *) addr)->options),
                mac_to_string (mac));
-
   return macstr;
 }
 
@@ -1707,7 +1793,7 @@ wlan_string_to_address (void *cls, const char *addr, uint16_t addrlen,
   struct WlanAddress *wa;
   unsigned int a[6];
   unsigned int i;
-  char plugin[4];
+  char plugin[5];
   uint32_t options;
 
   if ((NULL == addr) || (addrlen == 0))
@@ -1874,6 +1960,7 @@ libgnunet_plugin_transport_wlan_init (void *cls)
   api->check_address = &wlan_plugin_address_suggested;
   api->address_to_string = &wlan_plugin_address_to_string;
   api->string_to_address = &wlan_string_to_address;
+  api->get_network = &wlan_get_network;
   return api;
 }