hxing typemap
[oweals/gnunet.git] / src / core / gnunet-service-core.c
index e97e9fe46e85767e51827520e07f68f979f3bd3c..34b35f92a122136f9cffcb2394398b5ba9a77a7d 100644 (file)
@@ -44,9 +44,9 @@
 #include "core.h"
 
 
-#define DEBUG_HANDSHAKE GNUNET_NO
+#define DEBUG_HANDSHAKE GNUNET_EXTRA_LOGGING
 
-#define DEBUG_CORE_QUOTA GNUNET_NO
+#define DEBUG_CORE_QUOTA GNUNET_EXTRA_LOGGING
 
 /**
  * Receive and send buffer windows grow over time.  For
@@ -71,7 +71,7 @@
 
 /**
  * After how much time past the "official" expiration time do
- * we discard messages?  Should not be zero since we may 
+ * we discard messages?  Should not be zero since we may
  * intentionally defer transmission until close to the deadline
  * and then may be slightly past the deadline due to inaccuracy
  * in sleep and our own CPU consumption.
@@ -410,7 +410,7 @@ struct MessageEntry
 
 
 /**
- * Record kept for each request for transmission issued by a 
+ * Record kept for each request for transmission issued by a
  * client that is still pending.
  */
 struct ClientActiveRequest;
@@ -439,13 +439,13 @@ struct Neighbour
   struct MessageEntry *encrypted_tail;
 
   /**
-   * Head of list of requests from clients for transmission to 
+   * Head of list of requests from clients for transmission to
    * this peer.
    */
   struct ClientActiveRequest *active_client_request_head;
 
   /**
-   * Tail of list of requests from clients for transmission to 
+   * Tail of list of requests from clients for transmission to
    * this peer.
    */
   struct ClientActiveRequest *active_client_request_tail;
@@ -684,7 +684,7 @@ struct Client
 
 
 /**
- * Record kept for each request for transmission issued by a 
+ * Record kept for each request for transmission issued by a
  * client that is still pending.
  */
 struct ClientActiveRequest
@@ -962,10 +962,10 @@ send_to_client (struct Client *client, const struct GNUNET_MessageHeader *msg,
 /**
  * Send a message to all of our current clients that have
  * the right options set.
- * 
+ *
  * @param msg message to multicast
  * @param can_drop can this message be discarded if the queue is too long
- * @param options mask to use 
+ * @param options mask to use
  */
 static void
 send_to_all_clients (const struct GNUNET_MessageHeader *msg, int can_drop,
@@ -1225,7 +1225,7 @@ handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
       (n->status != PEER_STATE_KEY_CONFIRMED))
   {
     /* neighbour must have disconnected since request was issued,
-     * ignore (client will realize it once it processes the 
+     * ignore (client will realize it once it processes the
      * disconnect notification) */
 #if DEBUG_CORE_CLIENT
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1393,8 +1393,7 @@ handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
   irm.header.size = htons (sizeof (struct InitReplyMessage));
   irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
   irm.reserved = htonl (0);
-  memcpy (&irm.publicKey, &my_public_key,
-          sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
+  irm.my_identity = my_identity;
 #if DEBUG_CORE_CLIENT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n",
               "INIT_REPLY");
@@ -1705,7 +1704,7 @@ handle_client_request_info (void *cls, struct GNUNET_SERVER_Client *client,
   else
   {
     /* Technically, this COULD happen (due to asynchronous behavior),
-     * but it should be rare, so we should generate an info event 
+     * but it should be rare, so we should generate an info event
      * to help diagnosis of serious errors that might be masked by this */
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 _
@@ -1807,7 +1806,8 @@ free_neighbour (struct Neighbour *n)
  *
  * @param n neighbour to check.
  */
-static void process_encrypted_neighbour_queue (struct Neighbour *n);
+static void
+process_encrypted_neighbour_queue (struct Neighbour *n);
 
 
 /**
@@ -1854,11 +1854,12 @@ do_encrypt (struct Neighbour *n,
  *
  * @param n neighbour to consider discarding
  */
-static void consider_free_neighbour (struct Neighbour *n);
+static void
+consider_free_neighbour (struct Neighbour *n);
 
 
 /**
- * Task triggered when a neighbour entry is about to time out 
+ * Task triggered when a neighbour entry is about to time out
  * (and we should prevent this by sending a PING).
  *
  * @param cls the 'struct Neighbour'
@@ -2006,9 +2007,9 @@ notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
 #if DEBUG_CORE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
-                (unsigned int) ntohs (((struct GNUNET_MessageHeader *) &m[1])->
-                                      type), (unsigned int) ret,
-                GNUNET_i2s (&n->peer));
+                (unsigned int)
+                ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
+                (unsigned int) ret, GNUNET_i2s (&n->peer));
 #endif
     process_encrypted_neighbour_queue (n);
   }
@@ -2017,8 +2018,9 @@ notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
 #if DEBUG_CORE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Transmission of message of type %u and size %u failed\n",
-                (unsigned int) ntohs (((struct GNUNET_MessageHeader *) &m[1])->
-                                      type), (unsigned int) m->size);
+                (unsigned int)
+                ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
+                (unsigned int) m->size);
 #endif
   }
   GNUNET_free (m);
@@ -2038,7 +2040,8 @@ notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
  *
  * @param n neighbour to check.
  */
-static void process_plaintext_neighbour_queue (struct Neighbour *n);
+static void
+process_plaintext_neighbour_queue (struct Neighbour *n);
 
 
 /**
@@ -2071,12 +2074,11 @@ process_encrypted_neighbour_queue (struct Neighbour *n)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
               (unsigned int) m->size, GNUNET_i2s (&n->peer),
-              (unsigned long long) GNUNET_TIME_absolute_get_remaining (m->
-                                                                       deadline).
-              rel_value);
+              (unsigned long long)
+              GNUNET_TIME_absolute_get_remaining (m->deadline).rel_value);
 #endif
   n->th =
-      GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size,
+       GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size,
                                               m->priority,
                                               GNUNET_TIME_absolute_get_remaining
                                               (m->deadline),
@@ -2436,8 +2438,8 @@ batch_message (struct Neighbour *n, char *buf, size_t size,
                   (unsigned int)
                   ntohs (((const struct GNUNET_MessageHeader *) &pos[1])->type),
                   (unsigned int)
-                  ntohs (((const struct GNUNET_MessageHeader *) &pos[1])->
-                         size));
+                  ntohs (((const struct GNUNET_MessageHeader *)
+                          &pos[1])->size));
 #endif
       /* copy for encrypted transmission */
       memcpy (&buf[ret], &pos[1], pos->size);
@@ -2448,9 +2450,8 @@ batch_message (struct Neighbour *n, char *buf, size_t size,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Adding plaintext message of size %u with deadline %llu ms to batch\n",
                   (unsigned int) pos->size,
-                  (unsigned long long) GNUNET_TIME_absolute_get_remaining (pos->
-                                                                           deadline).
-                  rel_value);
+                  (unsigned long long)
+                  GNUNET_TIME_absolute_get_remaining (pos->deadline).rel_value);
 #endif
       deadline->abs_value =
           GNUNET_MIN (deadline->abs_value, pos->deadline.abs_value);
@@ -2490,13 +2491,16 @@ discard_expired_messages (struct Neighbour *n)
   struct GNUNET_TIME_Absolute now;
   struct GNUNET_TIME_Relative delta;
   int disc;
+  unsigned int queue_length;
 
   disc = GNUNET_NO;
   now = GNUNET_TIME_absolute_get ();
   prev = NULL;
+  queue_length = 0;
   pos = n->messages;
   while (pos != NULL)
   {
+    queue_length++;
     next = pos->next;
     delta = GNUNET_TIME_absolute_get_difference (pos->deadline, now);
     if (delta.rel_value > PAST_EXPIRATION_DISCARD_TIME.rel_value)
@@ -2521,7 +2525,8 @@ discard_expired_messages (struct Neighbour *n)
       prev = pos;
     pos = next;
   }
-  if (GNUNET_YES == disc)
+  if ( (GNUNET_YES == disc) &&
+       (queue_length == MAX_PEER_QUEUE_SIZE) )
     schedule_peer_messages (n);
 }
 
@@ -2548,7 +2553,8 @@ retry_plaintext_processing (void *cls,
  *
  * @param n the other peer
  */
-static void send_key (struct Neighbour *n);
+static void
+send_key (struct Neighbour *n);
 
 /**
  * Task that will retry "send_key" if our previous attempt failed
@@ -2730,13 +2736,13 @@ process_plaintext_neighbour_queue (struct Neighbour *n)
 /**
  * Function that recalculates the bandwidth quota for the
  * given neighbour and transmits it to the transport service.
- * 
+ *
  * @param cls neighbour for the quota update
  * @param tc context
  */
-static void neighbour_quota_update (void *cls,
-                                    const struct GNUNET_SCHEDULER_TaskContext
-                                    *tc);
+static void
+neighbour_quota_update (void *cls,
+                        const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
 /**
@@ -2847,7 +2853,7 @@ handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
   if ((n == NULL) || (GNUNET_YES != n->is_connected) ||
       (n->status != PEER_STATE_KEY_CONFIRMED))
   {
-    /* attempt to send message to peer that is not connected anymore 
+    /* attempt to send message to peer that is not connected anymore
      * (can happen due to asynchrony) */
     GNUNET_STATISTICS_update (stats,
                               gettext_noop
@@ -2962,52 +2968,6 @@ handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
 }
 
 
-/**
- * Handle CORE_REQUEST_CONNECT request.
- *
- * @param cls unused
- * @param client the client issuing the request
- * @param message the "struct ConnectMessage"
- */
-static void
-handle_client_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
-                               const struct GNUNET_MessageHeader *message)
-{
-  const struct ConnectMessage *cm = (const struct ConnectMessage *) message;
-  struct Neighbour *n;
-
-  if (0 ==
-      memcmp (&cm->peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
-  {
-    /* In this case a client has asked us to connect to ourselves, not really an error! */
-    GNUNET_SERVER_receive_done (client, GNUNET_OK);
-    return;
-  }
-  GNUNET_break (ntohl (cm->reserved) == 0);
-#if DEBUG_CORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core received `%s' request for `%4s', will try to establish connection\n",
-              "REQUEST_CONNECT", GNUNET_i2s (&cm->peer));
-#endif
-  GNUNET_STATISTICS_update (stats,
-                            gettext_noop ("# connection requests received"), 1,
-                            GNUNET_NO);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  n = find_neighbour (&cm->peer);
-  if ((n == NULL) || (GNUNET_YES != n->is_connected))
-  {
-    GNUNET_TRANSPORT_try_connect (transport, &cm->peer);
-  }
-  else
-  {
-    GNUNET_STATISTICS_update (stats,
-                              gettext_noop
-                              ("# connection requests ignored (already connected)"),
-                              1, GNUNET_NO);
-  }
-}
-
-
 /**
  * PEERINFO is giving us a HELLO for a peer.  Add the public key to
  * the neighbour's struct and retry send_key.  Or, if we did not get a
@@ -3269,9 +3229,9 @@ send_key (struct Neighbour *n)
 #if DEBUG_CORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Have %llu ms left for `%s' transmission.\n",
-              (unsigned long long) GNUNET_TIME_absolute_get_remaining (me->
-                                                                       deadline).
-              rel_value, "SET_KEY");
+              (unsigned long long)
+              GNUNET_TIME_absolute_get_remaining (me->deadline).rel_value,
+              "SET_KEY");
 #endif
 trigger_processing:
   /* trigger queue processing */
@@ -3293,9 +3253,10 @@ trigger_processing:
  * @param ats performance data
  * @param ats_count number of entries in ats (excluding 0-termination)
  */
-static void handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m,
-                            const struct GNUNET_TRANSPORT_ATS_Information *ats,
-                            uint32_t ats_count);
+static void
+handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m,
+                const struct GNUNET_TRANSPORT_ATS_Information *ats,
+                uint32_t ats_count);
 
 
 
@@ -3830,6 +3791,7 @@ send_p2p_message_to_client (struct Neighbour *sender, struct Client *client,
   struct GNUNET_TRANSPORT_ATS_Information *ats;
 
   GNUNET_assert (GNUNET_YES == sender->is_connected);
+  GNUNET_break (sender->status == PEER_STATE_KEY_CONFIRMED);
   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
     GNUNET_break (0);
@@ -3843,8 +3805,8 @@ send_p2p_message_to_client (struct Neighbour *sender, struct Client *client,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Core service passes message from `%4s' of type %u to client.\n",
               GNUNET_i2s (&sender->peer),
-              (unsigned int) ntohs (((const struct GNUNET_MessageHeader *) m)->
-                                    type));
+              (unsigned int)
+              ntohs (((const struct GNUNET_MessageHeader *) m)->type));
 #endif
   ntm = (struct NotifyTrafficMessage *) buf;
   ntm->header.size = htons (size);
@@ -3880,6 +3842,7 @@ deliver_message (void *cls, void *client, const struct GNUNET_MessageHeader *m)
   int deliver_full;
   int dropped;
 
+  GNUNET_break (sender->status == PEER_STATE_KEY_CONFIRMED);
   type = ntohs (m->type);
 #if DEBUG_CORE > 1
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -4252,7 +4215,7 @@ handle_transport_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
 /**
  * Function that recalculates the bandwidth quota for the
  * given neighbour and transmits it to the transport service.
- * 
+ *
  * @param cls neighbour for the quota update
  * @param tc context
  */
@@ -4279,7 +4242,7 @@ neighbour_quota_update (void *cls,
   n->quota_update_task = GNUNET_SCHEDULER_NO_TASK;
   /* calculate relative preference among all neighbours;
    * divides by a bit more to avoid division by zero AND to
-   * account for possibility of new neighbours joining any time 
+   * account for possibility of new neighbours joining any time
    * AND to convert to double... */
   neighbour_count = GNUNET_CONTAINER_multihashmap_size (neighbours);
   if (neighbour_count == 0)
@@ -4334,6 +4297,9 @@ neighbour_quota_update (void *cls,
                 "Forcing disconnect of `%4s' due to inactivity\n",
                 GNUNET_i2s (&n->peer));
 #endif
+    GNUNET_STATISTICS_update (stats,
+                             gettext_noop ("# peers disconnected due to inactivity"), 1,
+                             GNUNET_NO);
     q_in = GNUNET_BANDWIDTH_value_init (0);     /* force disconnect */
   }
 #if DEBUG_CORE_QUOTA
@@ -4443,6 +4409,9 @@ handle_transport_notify_disconnect (void *cls,
   GNUNET_break (n->is_connected == GNUNET_YES);
   if (n->status == PEER_STATE_KEY_CONFIRMED)
   {
+    GNUNET_STATISTICS_update (stats,
+                             gettext_noop ("# peers disconnected due to transport disconnect"), 1,
+                             GNUNET_NO);
     cnm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
     cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
     cnm.reserved = htonl (0);
@@ -4571,9 +4540,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
      sizeof (struct SendMessageRequest)},
     {&handle_client_send, NULL,
      GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
-    {&handle_client_request_connect, NULL,
-     GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT,
-     sizeof (struct ConnectMessage)},
     {NULL, NULL, 0, 0}
   };
   char *keyfile;
@@ -4636,14 +4602,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
                                 &handle_transport_notify_disconnect);
   GNUNET_assert (NULL != transport);
   stats = GNUNET_STATISTICS_create ("core", cfg);
-
-  GNUNET_STATISTICS_set (stats, gettext_noop ("# discarded CORE_SEND requests"),
-                         0, GNUNET_NO);
-  GNUNET_STATISTICS_set (stats,
-                         gettext_noop
-                         ("# discarded lower priority CORE_SEND requests"), 0,
-                         GNUNET_NO);
-
   mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
                                 NULL);