-fixing #2397
[oweals/gnunet.git] / src / dht / dht_api.c
index 37b65b022276c695b6787fa07036a1645ba53e67..53702fd77cd25135e811d5d16f00e0b375464a70 100644 (file)
@@ -175,7 +175,7 @@ struct GNUNET_DHT_GetHandle
   /**
    * Key that this get request is for
    */
-  GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
 
   /**
    * Unique identifier for this request (for key collisions).
@@ -213,7 +213,7 @@ struct GNUNET_DHT_MonitorHandle
   /**
    * Key being looked for, NULL == all.
    */
-  GNUNET_HashCode *key;
+  struct GNUNET_HashCode *key;
 
   /**
    * Callback for each received message of type get.
@@ -362,7 +362,7 @@ try_connect (struct GNUNET_DHT_Handle *handle)
  * @return GNUNET_YES (always)
  */
 static int
-add_request_to_pending (void *cls, const GNUNET_HashCode * key, void *value)
+add_request_to_pending (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct GNUNET_DHT_Handle *handle = cls;
   struct GNUNET_DHT_GetHandle *rh = value;
@@ -577,7 +577,7 @@ transmit_pending (void *cls, size_t size, void *buf)
  *         GNUNET_NO if the reply is malformed
  */
 static int
-process_reply (void *cls, const GNUNET_HashCode * key, void *value)
+process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   const struct GNUNET_DHT_ClientResultMessage *dht_msg = cls;
   struct GNUNET_DHT_GetHandle *get_handle = value;
@@ -648,7 +648,7 @@ process_monitor_get_message (struct GNUNET_DHT_Handle *handle,
 
     type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
     key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
-                                              sizeof (GNUNET_HashCode)));
+                                              sizeof (struct GNUNET_HashCode)));
     if (type_ok && key_ok && (NULL != h->get_cb))
       h->get_cb (h->cb_cls,
                 ntohl (msg->options),
@@ -699,7 +699,7 @@ process_monitor_get_resp_message (struct GNUNET_DHT_Handle *handle,
 
     type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
     key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
-                                              sizeof (GNUNET_HashCode)));
+                                              sizeof (struct GNUNET_HashCode)));
     if (type_ok && key_ok && (NULL != h->get_resp_cb))
       h->get_resp_cb (h->cb_cls,
                       (enum GNUNET_BLOCK_Type) ntohl(msg->type),
@@ -749,7 +749,7 @@ process_monitor_put_message (struct GNUNET_DHT_Handle *handle,
 
     type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
     key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
-                                              sizeof (GNUNET_HashCode)));
+                                              sizeof (struct GNUNET_HashCode)));
     if (type_ok && key_ok && (NULL != h->put_cb))
       h->put_cb (h->cb_cls,
                  ntohl (msg->options),
@@ -1040,7 +1040,7 @@ mark_put_message_gone (void *cls,
  * @param cont_cls closure for cont
  */
 struct GNUNET_DHT_PutHandle *
-GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const GNUNET_HashCode * key,
+GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode * key,
                 uint32_t desired_replication_level,
                 enum GNUNET_DHT_RouteOption options,
                 enum GNUNET_BLOCK_Type type, size_t size, const char *data,
@@ -1135,7 +1135,6 @@ GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph)
  * also "GNUNET_BLOCK_evaluate".
  *
  * @param handle handle to the DHT service
- * @param timeout how long to wait for transmission of this request to the service
  * @param type expected type of the response object
  * @param key the key to look up
  * @param desired_replication_level estimate of how many
@@ -1149,8 +1148,7 @@ GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph)
  */
 struct GNUNET_DHT_GetHandle *
 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
-                      struct GNUNET_TIME_Relative timeout,
-                      enum GNUNET_BLOCK_Type type, const GNUNET_HashCode * key,
+                      enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode * key,
                       uint32_t desired_replication_level,
                       enum GNUNET_DHT_RouteOption options, const void *xquery,
                       size_t xquery_size, GNUNET_DHT_GetIterator iter,
@@ -1268,14 +1266,14 @@ GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
 struct GNUNET_DHT_MonitorHandle *
 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
                           enum GNUNET_BLOCK_Type type,
-                          const GNUNET_HashCode *key,
+                          const struct GNUNET_HashCode *key,
                           GNUNET_DHT_MonitorGetCB get_cb,
                           GNUNET_DHT_MonitorGetRespCB get_resp_cb,
                           GNUNET_DHT_MonitorPutCB put_cb,
                           void *cb_cls)
 {
   struct GNUNET_DHT_MonitorHandle *h;
-  struct GNUNET_DHT_MonitorStartMessage *m;
+  struct GNUNET_DHT_MonitorStartStopMessage *m;
   struct PendingMessage *pending;
 
   h = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorHandle));
@@ -1289,25 +1287,25 @@ GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
   h->dht_handle = handle;
   if (NULL != key)
   {
-    h->key = GNUNET_malloc (sizeof(GNUNET_HashCode));
-    memcpy (h->key, key, sizeof(GNUNET_HashCode));
+    h->key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
+    memcpy (h->key, key, sizeof(struct GNUNET_HashCode));
   }
 
-  pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartMessage) +
+  pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
                            sizeof (struct PendingMessage));
-  m = (struct GNUNET_DHT_MonitorStartMessage *) &pending[1];
+  m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
   pending->msg = &m->header;
   pending->handle = handle;
   pending->free_on_send = GNUNET_YES;
   m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_START);
-  m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartMessage));
+  m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
   m->type = htonl(type);
-  m->get = (NULL != get_cb);
-  m->get_resp = (NULL != get_resp_cb);
-  m->put = (NULL != put_cb);
+  m->get = htons(NULL != get_cb);
+  m->get_resp = htons(NULL != get_resp_cb);
+  m->put = htons(NULL != put_cb);
   if (NULL != key) {
-    m->filter_key = 1;
-    memcpy (&m->key, key, sizeof(GNUNET_HashCode));
+    m->filter_key = htons(1);
+    memcpy (&m->key, key, sizeof(struct GNUNET_HashCode));
   }
   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
                                pending);
@@ -1328,11 +1326,36 @@ GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
 void
 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle)
 {
-  GNUNET_free_non_null (handle->key);
+  struct GNUNET_DHT_MonitorStartStopMessage *m;
+  struct PendingMessage *pending;
+
   GNUNET_CONTAINER_DLL_remove (handle->dht_handle->monitor_head,
                                handle->dht_handle->monitor_tail,
                                handle);
-  /* FIXME notify service of stop */
+
+  pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
+                           sizeof (struct PendingMessage));
+  m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
+  pending->msg = &m->header;
+  pending->handle = handle->dht_handle;
+  pending->free_on_send = GNUNET_YES;
+  m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP);
+  m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
+  m->type = htonl(handle->type);
+  m->get = htons(NULL != handle->get_cb);
+  m->get_resp = htons(NULL != handle->get_resp_cb);
+  m->put = htons(NULL != handle->put_cb);
+  if (NULL != handle->key) {
+    m->filter_key = htons(1);
+    memcpy (&m->key, handle->key, sizeof(struct GNUNET_HashCode));
+  }
+  GNUNET_CONTAINER_DLL_insert (handle->dht_handle->pending_head,
+                               handle->dht_handle->pending_tail,
+                               pending);
+  pending->in_pending_queue = GNUNET_YES;
+  process_pending_messages (handle->dht_handle);
+  
+  GNUNET_free_non_null (handle->key);
   GNUNET_free (handle);
 }