fix
[oweals/gnunet.git] / src / core / core_api.c
index e7e958bd7112bec3e0f00ea2e96afeaea95e82f8..4672c7843ee6b4157c91069ba6794359659c16e6 100644 (file)
@@ -87,9 +87,9 @@ struct GNUNET_CORE_Handle
   const struct GNUNET_CORE_MessageHandler *handlers;
 
   /**
-   * Our connection to the service.
+   * Our connection to the service for notifications.
    */
-  struct GNUNET_CLIENT_Connection *client;
+  struct GNUNET_CLIENT_Connection *client_notifications;
 
   /**
    * Handle for our current transmission request.
@@ -191,17 +191,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.
@@ -242,6 +231,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).
@@ -263,15 +256,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);
+  if (h->client_notifications != NULL)
+    GNUNET_CLIENT_disconnect (h->client_notifications, GNUNET_NO);
   h->currently_down = GNUNET_YES;
-  h->client = GNUNET_CLIENT_connect (h->sched, "core", h->cfg);
-  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
-                                               sizeof (struct InitMessage) +
-                                               sizeof (uint16_t) * h->hcnt,
-                                               GNUNET_TIME_UNIT_SECONDS,
-                                              GNUNET_NO,
-                                               &transmit_start, h);
+  h->client_notifications = GNUNET_CLIENT_connect (h->sched, "core", h->cfg);
+  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);
 }
 
 
@@ -287,8 +287,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));
 }
@@ -346,8 +344,15 @@ static void
 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);
@@ -356,7 +361,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,
+  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client_notifications,
                                                th->msize,
                                                GNUNET_TIME_absolute_get_remaining
                                                (th->timeout), 
@@ -367,20 +372,19 @@ trigger_next_request (struct GNUNET_CORE_Handle *h)
 
 
 /**
- * Handler for most messages received from the core.
+ * Handler for notification messages received from the core.
  *
  * @param cls our "struct GNUNET_CORE_Handle"
  * @param msg the message received from the core service
  */
 static void
-main_handler (void *cls, const struct GNUNET_MessageHeader *msg)
+main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_CORE_Handle *h = cls;
   unsigned int hpos;
   const struct ConnectNotifyMessage *cnm;
   const struct DisconnectNotifyMessage *dnm;
   const struct NotifyTrafficMessage *ntm;
-  const struct ConfigurationInfoMessage *cim;
   const struct GNUNET_MessageHeader *em;
   uint16_t msize;
   uint16_t et;
@@ -524,38 +528,15 @@ main_handler (void *cls, const struct GNUNET_MessageHeader *msg)
                          GNUNET_TIME_relative_ntoh (ntm->latency),
                          ntohl (ntm->distance));
       break;
-    case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
-      if (msize != sizeof (struct ConfigurationInfoMessage))
-        {
-          GNUNET_break (0);
-          break;
-        }
-      if (NULL == h->submitted)
-        break;
-      cim = (const struct ConfigurationInfoMessage *) msg;
-
-      /* process configuration data */
-      if (h->submitted->info != NULL)
-        h->submitted->info (h->submitted->info_cls,
-                            &h->submitted->peer,
-                            ntohl (cim->bpm_in),
-                            ntohl (cim->bpm_out),
-                            (int) ntohl (cim->reserved_amount),
-                            cim->preference);
-      /* done, clean up! */      
-      GNUNET_CORE_notify_transmit_ready_cancel (h->submitted); // HUH?
-      trigger_next_request (h);
-      break;
     default:
       GNUNET_break (0);
       break;
     }
-  GNUNET_CLIENT_receive (h->client,
-                         &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
+  GNUNET_CLIENT_receive (h->client_notifications,
+                         &main_notify_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
 
-
 /**
  * Function called when we are ready to transmit our
  * "START" message (or when this operation timed out).
@@ -601,21 +582,16 @@ 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);
-  GNUNET_CLIENT_receive (h->client,
-                         &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
+  GNUNET_CLIENT_receive (h->client_notifications,
+                         &main_notify_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
   if (NULL != (init = h->init))
     {
       /* 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),
@@ -710,7 +686,7 @@ transmit_start (void *cls, size_t size, void *buf)
   ts = (uint16_t *) & init[1];
   for (hpos = 0; hpos < h->hcnt; hpos++)
     ts[hpos] = htons (h->handlers[hpos].type);
-  GNUNET_CLIENT_receive (h->client,
+  GNUNET_CLIENT_receive (h->client_notifications,
                          &init_reply_handler,
                          h,
                          GNUNET_TIME_absolute_get_remaining
@@ -774,8 +750,8 @@ GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
   h->inbound_hdr_only = inbound_hdr_only;
   h->outbound_hdr_only = outbound_hdr_only;
   h->handlers = handlers;
-  h->client = GNUNET_CLIENT_connect (sched, "core", cfg);
-  if (h->client == NULL)
+  h->client_notifications = GNUNET_CLIENT_connect (sched, "core", cfg);
+  if (h->client_notifications == NULL)
     {
       GNUNET_free (h);
       return NULL;
@@ -793,7 +769,7 @@ GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
               timeout.value);
 #endif
   h->th =
-    GNUNET_CLIENT_notify_transmit_ready (h->client,
+    GNUNET_CLIENT_notify_transmit_ready (h->client_notifications,
                                          sizeof (struct InitMessage) +
                                          sizeof (uint16_t) * h->hcnt, timeout,
                                         GNUNET_YES,
@@ -816,7 +792,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);
+  if (handle->client_notifications != NULL)
+    GNUNET_CLIENT_disconnect (handle->client_notifications, GNUNET_NO);
   GNUNET_free_non_null (handle->solicit_buffer);
   GNUNET_free (handle);
 }
@@ -868,12 +845,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);
 }