- fix error messages
[oweals/gnunet.git] / src / transport / plugin_transport_wlan.c
index 932d3b76265f9b5de36ee31f13f7e0bb8f4e5336..1fafc40708737254762477b32300e0dca598d849 100644 (file)
@@ -597,8 +597,8 @@ send_ack (void *cls, uint32_t msg_id,
 
   if (NULL == endpoint)
   {
-       GNUNET_break (0);
-       return;
+    GNUNET_break (0);
+    return;
   }
 
   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
@@ -654,10 +654,12 @@ wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
 /**
  * Free a session
  *
+ * @param cls our `struct Plugin`.
  * @param session the session free
  */
-static void
-free_session (struct Session *session)
+static int
+wlan_plugin_disconnect_session (void *cls,
+                                struct Session *session)
 {
   struct MacEndpoint *endpoint = session->mac;
   struct PendingMessage *pm;
@@ -685,9 +687,26 @@ free_session (struct Session *session)
     GNUNET_SCHEDULER_cancel (session->timeout_task);
     session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN sessions allocated"), -1,
+  GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
+                            _("# WLAN sessions allocated"), -1,
                             GNUNET_NO);
   GNUNET_free (session);
+  return GNUNET_OK;
+}
+
+
+/**
+ * Function that is called to get the keepalive factor.
+ * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
+ * calculate the interval between keepalive packets.
+ *
+ * @param cls closure with the `struct Plugin`
+ * @return keepalive factor
+ */
+static unsigned int
+wlan_plugin_query_keepalive_factor (void *cls)
+{
+  return 3;
 }
 
 
@@ -700,14 +719,15 @@ free_session (struct Session *session)
 static void
 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct Session * session = cls;
+  struct Session *session = cls;
   struct GNUNET_TIME_Relative timeout;
 
   session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
   if (0 == timeout.rel_value_us)
   {
-    free_session (session);
+    wlan_plugin_disconnect_session (session->mac->plugin,
+                                    session);
     return;
   }
   session->timeout_task =
@@ -721,6 +741,7 @@ session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *
  * @param endpoint pointer to the mac endpoint of the peer
  * @param peer peer identity to use for this session
+ * @param inbound inbound session?
  * @return returns the session or NULL
  */
 static struct Session *
@@ -745,6 +766,7 @@ lookup_session (struct MacEndpoint *endpoint,
  *
  * @param endpoint pointer to the mac endpoint of the peer
  * @param peer peer identity to use for this session
+ * @param inbound inbound session?
  * @return returns the session or NULL
  */
 static struct Session *
@@ -755,7 +777,7 @@ create_session (struct MacEndpoint *endpoint,
 
   GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN sessions allocated"), 1,
                             GNUNET_NO);
-  session = GNUNET_malloc (sizeof (struct Session));
+  session = GNUNET_new (struct Session);
   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
                                     endpoint->sessions_tail,
                                    session);
@@ -781,6 +803,7 @@ create_session (struct MacEndpoint *endpoint,
  *
  * @param endpoint pointer to the mac endpoint of the peer
  * @param peer peer identity to use for this session
+ * @param inbound inbound session?
  * @return returns the session
  */
 static struct Session *
@@ -801,7 +824,7 @@ get_session (struct MacEndpoint *endpoint,
  * the next fragment.
  *
  * @param cls the 'struct FragmentMessage'
- * @param result result of the operation (GNUNET_OK on success, GNUNET_NO if the helper died, GNUNET_SYSERR
+ * @param result result of the operation (#GNUNET_OK on success, #GNUNET_NO if the helper died, #GNUNET_SYSERR
  *        if the helper was stopped)
  */
 static void
@@ -891,8 +914,8 @@ free_fragment_message (struct FragmentMessage *fm)
     fm->sh = NULL;
   }
   GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
-                                                                  &endpoint->msg_delay,
-                                                                  &endpoint->ack_delay);
+                                   &endpoint->msg_delay,
+                                   &endpoint->ack_delay);
   if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
   {
     GNUNET_SCHEDULER_cancel (fm->timeout_task);
@@ -941,7 +964,7 @@ fragmentmessage_timeout (void *cls,
 static void
 send_with_fragmentation (struct MacEndpoint *endpoint,
                         struct GNUNET_TIME_Relative timeout,
-                        const struct GNUNET_PeerIdentity *target,                      
+                        const struct GNUNET_PeerIdentity *target,
                         const struct GNUNET_MessageHeader *msg,
                         size_t payload_size,
                         GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
@@ -951,7 +974,7 @@ send_with_fragmentation (struct MacEndpoint *endpoint,
   struct Plugin *plugin;
 
   plugin = endpoint->plugin;
-  fm = GNUNET_malloc (sizeof (struct FragmentMessage));
+  fm = GNUNET_new (struct FragmentMessage);
   fm->macendpoint = endpoint;
   fm->target = *target;
   fm->size_payload = payload_size;
@@ -991,7 +1014,8 @@ free_macendpoint (struct MacEndpoint *endpoint)
   GNUNET_STATISTICS_update (plugin->env->stats,
                            _("# WLAN MAC endpoints allocated"), -1, GNUNET_NO);
   while (NULL != (session = endpoint->sessions_head))
-    free_session (session);
+    wlan_plugin_disconnect_session (plugin,
+                                    session);
   while (NULL != (fm = endpoint->sending_messages_head))
     free_fragment_message (fm);
   GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
@@ -1055,7 +1079,7 @@ create_macendpoint (struct Plugin *plugin,
   for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
     if (0 == memcmp (addr, &pos->addr, sizeof (struct WlanAddress)))
       return pos;
-  pos = GNUNET_malloc (sizeof (struct MacEndpoint));
+  pos = GNUNET_new (struct MacEndpoint);
   pos->addr = *addr;
   pos->plugin = plugin;
   pos->defrag =
@@ -1139,7 +1163,8 @@ wlan_plugin_get_session (void *cls,
  * @param target peer from which to disconnect
  */
 static void
-wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
+wlan_plugin_disconnect_peer (void *cls,
+                             const struct GNUNET_PeerIdentity *target)
 {
   struct Plugin *plugin = cls;
   struct Session *session;
@@ -1150,7 +1175,7 @@ wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
       if (0 == memcmp (target, &session->target,
                       sizeof (struct GNUNET_PeerIdentity)))
       {
-        free_session (session);
+        wlan_plugin_disconnect_session (plugin, session);
        break; /* inner-loop only (in case peer has another MAC as well!) */
       }
 }
@@ -1367,14 +1392,15 @@ process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
       break;
     }
     xmas.endpoint = mas->endpoint;
-    if (NULL == (xmas.session =  lookup_session (mas->endpoint, &wlanheader->sender, GNUNET_YES)))
+    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);
+      xmas.session = create_session (mas->endpoint, &wlanheader->sender, GNUNET_YES);
+      plugin->env->session_start (NULL, &wlanheader->sender,
+          PLUGIN_NAME, &mas->endpoint->addr,
+          sizeof (struct WlanAddress), 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);
+          "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",
@@ -1820,7 +1846,7 @@ wlan_string_to_address (void *cls, const char *addr, uint16_t addrlen,
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-  wa = GNUNET_malloc (sizeof (struct WlanAddress));
+  wa = GNUNET_new (struct WlanAddress);
   for (i=0;i<6;i++)
     wa->mac.mac[i] = a[i];
   wa->options = htonl (0);
@@ -1830,6 +1856,18 @@ wlan_string_to_address (void *cls, const char *addr, uint16_t addrlen,
 }
 
 
+static void
+wlan_plugin_update_session_timeout (void *cls,
+                                  const struct GNUNET_PeerIdentity *peer,
+                                  struct Session *session)
+{
+  if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (session->timeout_task);
+
+  session->timeout_task = GNUNET_SCHEDULER_add_delayed (
+      GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
+}
+
 /**
  * Entry point for the plugin.
  *
@@ -1851,7 +1889,7 @@ libgnunet_plugin_transport_wlan_init (void *cls)
   {
     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
        initialze the plugin or the API */
-    api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
+    api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
     api->cls = NULL;
     api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
     api->address_to_string = &wlan_plugin_address_to_string;
@@ -1893,7 +1931,7 @@ libgnunet_plugin_transport_wlan_init (void *cls)
     return NULL;
   }
 
-  plugin = GNUNET_malloc (sizeof (struct Plugin));
+  plugin = GNUNET_new (struct Plugin);
   plugin->interface = interface;
   plugin->env = env;
   GNUNET_STATISTICS_set (plugin->env->stats, _("# WLAN sessions allocated"),
@@ -1951,16 +1989,19 @@ libgnunet_plugin_transport_wlan_init (void *cls)
     GNUNET_assert (0);
   }
 
-  api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
+  api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
   api->cls = plugin;
   api->send = &wlan_plugin_send;
   api->get_session = &wlan_plugin_get_session;
-  api->disconnect = &wlan_plugin_disconnect;
+  api->disconnect_peer = &wlan_plugin_disconnect_peer;
+  api->disconnect_session = &wlan_plugin_disconnect_session;
+  api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
   api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
   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;
+  api->update_session_timeout = &wlan_plugin_update_session_timeout;
   return api;
 }