- use proper signedness
[oweals/gnunet.git] / src / transport / gnunet-service-transport_neighbours.c
index c4d4677b15cbadbac344fa9f4460bb468aaa358b..534af0aa06ff1eb62b6c6ff31a73ab58a4f3cc7f 100644 (file)
@@ -116,6 +116,28 @@ struct SessionConnectMessage
 };
 
 
+/**
+ * Message a peer sends to another when connected to indicate that a
+ * session is in use and the peer is still alive or to respond to a keep alive.
+ * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
+ * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
+ * When the keep alive response with type is received, transport service
+ * will call the respective plugin to update the session timeout
+ */
+struct SessionKeepAliveMessage
+{
+  /**
+   * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
+   * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * A nonce to identify the session the keep alive is used for
+   */
+  uint32_t nonce GNUNET_PACKED;
+};
+
 /**
  * Message we send to the other peer to notify him that we intentionally
  * are disconnecting (to reduce timeouts).  This is just a friendly
@@ -410,6 +432,10 @@ struct NeighbourAddress
    */
   int ats_active;
 
+  /**
+   * The current nonce sent in the last keep alive messages
+   */
+  uint32_t keep_alive_nonce;
 };
 
 
@@ -648,10 +674,15 @@ lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
 }
 
 
+/**
+ * Convert state to human-readable string.
+ *
+ * @param state the state value
+ * @return corresponding string
+ */
 static const char *
-print_state (int state)
+print_state (enum State state)
 {
-
   switch (state)
   {
   case S_NOT_CONNECTED:
@@ -775,6 +806,7 @@ free_address (struct NeighbourAddress *na)
   }
 
   na->ats_active = GNUNET_NO;
+  na->keep_alive_nonce = 0;
   if (NULL != na->address)
   {
     GNUNET_HELLO_address_free (na->address);
@@ -847,6 +879,7 @@ set_address (struct NeighbourAddress *na,
   na->bandwidth_out = bandwidth_out;
   na->session = session;
   na->ats_active = is_active;
+  na->keep_alive_nonce = 0;
   if (GNUNET_YES == is_active)
   {
     /* Telling ATS about new session */
@@ -1082,6 +1115,10 @@ send_disconnect (struct NeighbourMapEntry *n)
 static void
 disconnect_neighbour (struct NeighbourMapEntry *n)
 {
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Disconnecting from peer %s in state %s\n",
+              GNUNET_i2s (&n->id),
+              print_state (n->state));
   /* depending on state, notify neighbour and/or upper layers of this peer
      about disconnect */
   switch (n->state)
@@ -1188,12 +1225,12 @@ transmit_send_continuation (void *cls,
   }
   if (bytes_in_send_queue < mq->message_buf_size)
   {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
-                  bytes_in_send_queue, mq->message_buf_size,
-                  (GNUNET_OK == success) ? "OK" : "FAIL",
-                         size_payload, physical);
-      GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
+                bytes_in_send_queue, mq->message_buf_size,
+                (GNUNET_OK == success) ? "OK" : "FAIL",
+                size_payload, physical);
+    GNUNET_break (0);
   }
 
 
@@ -1272,7 +1309,9 @@ try_transmission_to_peer (struct NeighbourMapEntry *n)
                              1, GNUNET_NO);
     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
     n->is_active = mq;
-    transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0);     /* timeout */
+    transmit_send_continuation (mq, &n->id,
+                                GNUNET_SYSERR,
+                                mq->message_buf_size, 0);     /* timeout */
   }
   if (NULL == mq)
     return;                     /* no more messages */
@@ -1296,16 +1335,28 @@ try_transmission_to_peer (struct NeighbourMapEntry *n)
 static void
 send_keepalive (struct NeighbourMapEntry *n)
 {
-  struct GNUNET_MessageHeader m;
+  struct SessionKeepAliveMessage m;
   struct GNUNET_TIME_Relative timeout;
+  uint32_t nonce;
 
   GNUNET_assert ((S_CONNECTED == n->state) ||
                  (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
                  (S_CONNECTED_SWITCHING_CONNECT_SENT));
   if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
     return; /* no keepalive needed at this time */
-  m.size = htons (sizeof (struct GNUNET_MessageHeader));
-  m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
+
+  nonce = 0; /* 0 indicates 'not set' */
+  while (0 == nonce)
+    nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "Sending keep alive to peer `%s' with nonce %u\n",
+      GNUNET_i2s (&n->id), nonce);
+
+  m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
+  m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
+  m.nonce = htonl (nonce);
+
   timeout = send_with_session (n,
                               (const void *) &m, sizeof (m),
                               UINT32_MAX /* priority */,
@@ -1313,9 +1364,11 @@ send_keepalive (struct NeighbourMapEntry *n)
                               NULL, NULL);
   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
                            GNUNET_NO);
+  n->primary_address.keep_alive_nonce = nonce;
   n->expect_latency_response = GNUNET_YES;
   n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
   n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
+
 }
 
 
@@ -1324,13 +1377,20 @@ send_keepalive (struct NeighbourMapEntry *n)
  * we received a KEEPALIVE (or equivalent); send a response.
  *
  * @param neighbour neighbour to keep alive (by sending keep alive response)
+ * @param m the keep alive message containing the nonce to respond to
  */
 void
-GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
+GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
+    const struct GNUNET_MessageHeader *m)
 {
   struct NeighbourMapEntry *n;
-  struct GNUNET_MessageHeader m;
+  const struct SessionKeepAliveMessage *msg_in;
+  struct SessionKeepAliveMessage msg;
+
+  if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
+    return;
 
+  msg_in = (struct SessionKeepAliveMessage *) m;
   if (NULL == (n = lookup_neighbour (neighbour)))
   {
     GNUNET_STATISTICS_update (GST_stats,
@@ -1347,11 +1407,17 @@ GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
                               1, GNUNET_NO);
     return;
   }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "Received keep alive request from peer `%s' with nonce %u\n",
+      GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
+
   /* send reply to allow neighbour to measure latency */
-  m.size = htons (sizeof (struct GNUNET_MessageHeader));
-  m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
+  msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
+  msg.nonce = msg_in->nonce;
   (void) send_with_session(n,
-                          (const void *) &m, sizeof (m),
+                          (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
                           UINT32_MAX /* priority */,
                           GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
                           NULL, NULL);
@@ -1364,14 +1430,22 @@ GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
  * plus calculated latency) to ATS.
  *
  * @param neighbour neighbour to keep alive
+ * @param m the message containing the keep alive response
  */
 void
-GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
+GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
+    const struct GNUNET_MessageHeader *m)
 {
   struct NeighbourMapEntry *n;
+  const struct SessionKeepAliveMessage *msg;
+  struct GNUNET_TRANSPORT_PluginFunctions *papi;
   uint32_t latency;
   struct GNUNET_ATS_Information ats;
 
+  if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
+    return;
+
+  msg = (const struct SessionKeepAliveMessage *) m;
   if (NULL == (n = lookup_neighbour (neighbour)))
   {
     GNUNET_STATISTICS_update (GST_stats,
@@ -1389,6 +1463,44 @@ GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
                               1, GNUNET_NO);
     return;
   }
+  if (NULL == n->primary_address.address)
+  {
+    GNUNET_STATISTICS_update (GST_stats,
+                              gettext_noop
+                              ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
+                              1, GNUNET_NO);
+    return;
+  }
+  if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
+  {
+    GNUNET_STATISTICS_update (GST_stats,
+                              gettext_noop
+                              ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
+                              1, GNUNET_NO);
+    return;
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+        "Received keep alive response from peer `%s' for session %p\n",
+        GNUNET_i2s (&n->id), n->primary_address.session);
+
+  }
+
+  /* Update session timeout here */
+  if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+        "Updating session for peer `%s' for session %p\n",
+        GNUNET_i2s (&n->id), n->primary_address.session);
+    papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
+  }
+  else
+  {
+    GNUNET_break (0);
+  }
+
+  n->primary_address.keep_alive_nonce = 0;
   n->expect_latency_response = GNUNET_NO;
   n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
   n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
@@ -1404,10 +1516,8 @@ GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
   else
     latency = n->latency.rel_value_us;
   ats.value = htonl (latency);
-  GST_ats_update_metrics (&n->id,
-                                                                                         n->primary_address.address,
-                                                                                       n->primary_address.session,
-                                                                                       &ats, 1);
+  GST_ats_update_metrics (&n->id, n->primary_address.address,
+      n->primary_address.session, &ats, 1);
 }
 
 
@@ -1418,9 +1528,9 @@ GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
  *
  * @param sender sender of the message
  * @param size size of the message
- * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
- *                   GNUNET_NO if the neighbour is not connected or violates the quota,
- *                   GNUNET_SYSERR if the connection is not fully up yet
+ * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
+ *                   #GNUNET_NO if the neighbour is not connected or violates the quota,
+ *                   #GNUNET_SYSERR if the connection is not fully up yet
  * @return how long to wait before reading more from this sender
  */
 struct GNUNET_TIME_Relative
@@ -1815,7 +1925,7 @@ handle_test_blacklist_cont (void *cls,
               GNUNET_i2s (peer),
               (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
   if (GNUNET_OK == result)
-    GST_ats_add_address (bcc->na.address, bcc->na.session);
+    GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
   else
   {
     /* Blacklist disagreed on connecting to a peer with this address
@@ -2222,9 +2332,10 @@ GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
   }
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "ATS tells us to switch to address '%s' session %p for "
+              "ATS tells us to switch to address '%s/%s' session %p for "
               "peer `%s' in state %s/%d (quota in/out %u %u )\n",
               (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
+              address->transport_name,
               session,
               GNUNET_i2s (peer),
               print_state (n->state),
@@ -2302,6 +2413,7 @@ GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
        address and check blacklist again */
     set_address (&n->primary_address,
                 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
+    n->state = S_CONNECT_RECV_BLACKLIST;
     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
     check_blacklist (&n->id,
                     n->connect_ack_timestamp,
@@ -2850,7 +2962,9 @@ GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
                        n->primary_address.bandwidth_in,
                        n->primary_address.bandwidth_out);
     /* Tell ATS that the outbound session we created to send CONNECT was successful */
-    GST_ats_add_address (n->primary_address.address, n->primary_address.session);
+    GST_ats_add_address (n->primary_address.address,
+                         n->primary_address.session,
+                         NULL, 0);
     set_address (&n->primary_address,
                 n->primary_address.address,
                 n->primary_address.session,
@@ -2896,7 +3010,9 @@ GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
     n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
     GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
 
-    GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
+    GST_ats_add_address (n->alternative_address.address,
+                         n->alternative_address.session,
+                         NULL, 0);
     set_address (&n->primary_address,
                 n->alternative_address.address,
                 n->alternative_address.session,
@@ -3122,7 +3238,9 @@ GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
                      n->primary_address.bandwidth_in,
                      n->primary_address.bandwidth_out);
 
-  GST_ats_add_address (n->primary_address.address, n->primary_address.session);
+  GST_ats_add_address (n->primary_address.address,
+                       n->primary_address.session,
+                       NULL, 0);
   set_address (&n->primary_address,
               n->primary_address.address,
               n->primary_address.session,
@@ -3172,8 +3290,9 @@ GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
   if (0 != ntohl (quota.value__))
     return;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
-              GNUNET_i2s (&n->id), "SET_QUOTA");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Disconnecting peer `%4s' due to SET_QUOTA\n",
+              GNUNET_i2s (&n->id));
   if (GNUNET_YES == test_connected (n))
     GNUNET_STATISTICS_update (GST_stats,
                               gettext_noop ("# disconnects due to quota of 0"),
@@ -3190,10 +3309,8 @@ GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
  * @param msg the disconnect message
  */
 void
-GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
-                                          *peer,
-                                          const struct GNUNET_MessageHeader
-                                          *msg)
+GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
+                                          const struct GNUNET_MessageHeader *msg)
 {
   struct NeighbourMapEntry *n;
   const struct SessionDisconnectMessage *sdm;
@@ -3259,6 +3376,9 @@ GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
                              gettext_noop
                              ("# other peer asked to disconnect from us"), 1,
                              GNUNET_NO);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Disconnecting by request from peer %s\n",
+              GNUNET_i2s (peer));
   disconnect_neighbour (n);
 }
 
@@ -3354,6 +3474,9 @@ GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
                              gettext_noop
                              ("# disconnected from peer upon explicit request"), 1,
                              GNUNET_NO);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Forced disconnect from peer %s\n",
+              GNUNET_i2s (target));
   disconnect_neighbour (n);
 }