dbg
[oweals/gnunet.git] / src / core / core_api.c
index a233134998beff20892675bde63dbce7bc1c1ec7..f8c7c0aa6ef79a231e1d5454bb897117db764cc8 100644 (file)
@@ -55,12 +55,6 @@ struct GNUNET_CORE_Handle
    */
   GNUNET_CORE_StartupCallback init;
 
-  /**
-   * Function to call whenever we're notified about a peer connecting
-   * (pre-connects, no session key exchange yet).
-   */
-  GNUNET_CORE_ConnectEventHandler pre_connects;
-
   /**
    * Function to call whenever we're notified about a peer connecting.
    */
@@ -91,11 +85,6 @@ struct GNUNET_CORE_Handle
    */
   struct GNUNET_CLIENT_Connection *client_notifications;
 
-  /**
-   * Our connection to the service for normal requests.
-   */
-  struct GNUNET_CLIENT_Connection *client_requests;
-
   /**
    * Handle for our current transmission request.
    */
@@ -196,17 +185,6 @@ struct GNUNET_CORE_TransmitHandle
    */
   void *get_message_cls;
 
-  /**
-   * If this entry is for a configuration request, pointer
-   * to the information callback; otherwise NULL.
-   */
-  GNUNET_CORE_PeerConfigurationInfoCallback info;
-
-  /**
-   * Closure for info.
-   */
-  void *info_cls;
-
   /**
    * If this entry is for a transmission request, pointer
    * to the notify callback; otherwise NULL.
@@ -247,6 +225,10 @@ struct GNUNET_CORE_TransmitHandle
 };
 
 
+static void
+reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
 /**
  * Function called when we are ready to transmit our
  * "START" message (or when this operation timed out).
@@ -268,15 +250,22 @@ static size_t transmit_start (void *cls, size_t size, void *buf);
 static void
 reconnect (struct GNUNET_CORE_Handle *h)
 {
-  GNUNET_CLIENT_disconnect (h->client_notifications);
+  if (h->client_notifications != NULL)
+    GNUNET_CLIENT_disconnect (h->client_notifications, GNUNET_NO);
   h->currently_down = GNUNET_YES;
   h->client_notifications = GNUNET_CLIENT_connect (h->sched, "core", h->cfg);
-  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client_notifications,
-                                               sizeof (struct InitMessage) +
-                                               sizeof (uint16_t) * h->hcnt,
-                                               GNUNET_TIME_UNIT_SECONDS,
-                                              GNUNET_NO,
-                                               &transmit_start, h);
+  if (h->client_notifications == NULL)
+    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
+                                                     GNUNET_TIME_UNIT_SECONDS,
+                                                     &reconnect_task,
+                                                     h);
+  else
+    h->th = GNUNET_CLIENT_notify_transmit_ready (h->client_notifications,
+                                                sizeof (struct InitMessage) +
+                                                sizeof (uint16_t) * h->hcnt,
+                                                GNUNET_TIME_UNIT_SECONDS,
+                                                GNUNET_NO,
+                                                &transmit_start, h);
 }
 
 
@@ -292,8 +281,6 @@ timeout_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CORE_TransmitHandle *th = cls;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Transmission request timed out.\n");
   th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
 }
@@ -353,7 +340,13 @@ trigger_next_request (struct GNUNET_CORE_Handle *h)
   struct GNUNET_CORE_TransmitHandle *th;
 
   if (h->currently_down)
-    return;                     /* connection temporarily down */
+    {
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "In trigger_next_request, connection currently down...\n");
+#endif
+      return;                     /* connection temporarily down */
+    }
   if (NULL == (th = h->pending_head))
     return;                     /* no requests pending */
   GNUNET_assert (NULL == h->th);
@@ -362,7 +355,7 @@ trigger_next_request (struct GNUNET_CORE_Handle *h)
       GNUNET_SCHEDULER_cancel (h->sched, th->timeout_task);
       th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
     }
-  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client_requests,
+  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client_notifications,
                                                th->msize,
                                                GNUNET_TIME_absolute_get_remaining
                                                (th->timeout), 
@@ -407,23 +400,6 @@ main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
 #endif
   switch (ntohs (msg->type))
     {
-    case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_PRE_CONNECT:
-      if (NULL == h->pre_connects)
-        {
-          GNUNET_break (0);
-          break;
-        }
-      if (msize != sizeof (struct ConnectNotifyMessage))
-        {
-          GNUNET_break (0);
-          break;
-        }
-      cnm = (const struct ConnectNotifyMessage *) msg;
-      h->pre_connects (h->cls,
-                      &cnm->peer,
-                      GNUNET_TIME_relative_ntoh (cnm->latency),
-                      ntohl (cnm->distance));
-      break;
     case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
       if (NULL == h->connects)
         {
@@ -571,10 +547,12 @@ init_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
       (ntohs (msg->size) != sizeof (struct InitReplyMessage)) ||
       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                   _
-                  ("Error connecting to core service (failed to receive `%s' message).\n"),
-                  "INIT_REPLY");
+                  ("Error connecting to core service (failed to receive `%s' message, got message of type %u and size %u).\n"),
+                  "INIT_REPLY",
+                 (msg == NULL) ? -1 : ntohs (msg->type),
+                 (msg == NULL) ? -1 : ntohs (msg->size));
       GNUNET_break (msg == NULL);
       transmit_start (h, 0, NULL);
       return;
@@ -583,8 +561,7 @@ init_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
   /* start our message processing loop */
 #if DEBUG_CORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              _
-              ("Successfully connected to core service, starting processing loop.\n"));
+             "Successfully connected to core service, starting processing loop.\n");
 #endif
   h->currently_down = GNUNET_NO;
   trigger_next_request (h);
@@ -594,10 +571,6 @@ init_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
     {
       /* mark so we don't call init on reconnect */
       h->init = NULL;
-#if DEBUG_CORE
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  _("Successfully connected to core service.\n"));
-#endif
       GNUNET_CRYPTO_hash (&m->publicKey,
                           sizeof (struct
                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
@@ -668,8 +641,6 @@ transmit_start (void *cls, size_t size, void *buf)
   init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
   init->header.size = htons (msize);
   opt = GNUNET_CORE_OPTION_NOTHING;
-  if (h->pre_connects != NULL)
-    opt |= GNUNET_CORE_OPTION_SEND_PRE_CONNECT;
   if (h->connects != NULL)
     opt |= GNUNET_CORE_OPTION_SEND_CONNECT;
   if (h->disconnects != NULL)
@@ -711,7 +682,6 @@ transmit_start (void *cls, size_t size, void *buf)
  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
  * @param init callback to call on timeout or once we have successfully
  *        connected to the core service; note that timeout is only meaningful if init is not NULL
- * @param pre_connects function to call on peer pre-connect (no session key yet), can be NULL
  * @param connects function to call on peer connect, can be NULL
  * @param disconnects function to call on peer disconnect / timeout, can be NULL
  * @param inbound_notify function to call for all inbound messages, can be NULL
@@ -732,7 +702,6 @@ GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
                      struct GNUNET_TIME_Relative timeout,
                      void *cls,
                      GNUNET_CORE_StartupCallback init,
-                     GNUNET_CORE_ConnectEventHandler pre_connects,
                      GNUNET_CORE_ConnectEventHandler connects,
                      GNUNET_CORE_DisconnectEventHandler disconnects,
                      GNUNET_CORE_MessageCallback inbound_notify,
@@ -748,7 +717,6 @@ GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
   h->cfg = cfg;
   h->cls = cls;
   h->init = init;
-  h->pre_connects = pre_connects;
   h->connects = connects;
   h->disconnects = disconnects;
   h->inbound_notify = inbound_notify;
@@ -798,9 +766,8 @@ GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
     GNUNET_CORE_notify_transmit_ready_cancel (handle->solicit_transmit_req);
   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
-  GNUNET_CLIENT_disconnect (handle->client_notifications);
-  if (handle->client_requests != NULL)
-    GNUNET_CLIENT_disconnect (handle->client_requests);
+  if (handle->client_notifications != NULL)
+    GNUNET_CLIENT_disconnect (handle->client_notifications, GNUNET_NO);
   GNUNET_free_non_null (handle->solicit_buffer);
   GNUNET_free (handle);
 }
@@ -852,12 +819,22 @@ produce_send (void *cls, size_t size, void *buf)
   dt = notify (notify_cls, size - sizeof (struct SendMessage), &sm[1]);
   if (0 == dt)
     {
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Size of clients message to peer %s is 0!\n",
+              GNUNET_i2s(&th->peer));
+#endif
       /* client decided to send nothing! */
       return 0;
     }
   GNUNET_assert (dt >= sizeof (struct GNUNET_MessageHeader));
+  if (dt + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+    {
+      GNUNET_break (0);
+      return 0;
+    }
   sm->header.size = htons (dt + sizeof (struct SendMessage));
-  GNUNET_assert (dt + sizeof (struct SendMessage) < size);
+  GNUNET_assert (dt + sizeof (struct SendMessage) <= size);
   return dt + sizeof (struct SendMessage);
 }