add nick name for cached records
[oweals/gnunet.git] / src / regex / gnunet-service-regex.c
index 4bddb5042c0fa2597d933f0d0c9afa8eb964aa2b..cc32c46de1ba738e6a30db85a9f547faf85ac719 100644 (file)
 
 /**
  * @file regex/gnunet-service-regex.c
- * @brief program that tracks template
+ * @brief service to advertise capabilities described as regex and to
+ *        lookup capabilities by regex
  * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_regex_lib.h"
+#include "regex_internal_lib.h"
 #include "regex_ipc.h"
 
 
@@ -53,12 +54,12 @@ struct ClientEntry
   /**
    * Search handle (if this client is searching).
    */
-  struct GNUNET_REGEX_search_handle *sh;
+  struct REGEX_INTERNAL_Search *sh;
 
   /**
    * Announcement handle (if this client is announcing).
    */
-  struct GNUNET_REGEX_announce_handle *ah;
+  struct REGEX_INTERNAL_Announcement *ah;
 
   /**
    * Refresh frequency for announcements.
@@ -93,6 +94,16 @@ static struct ClientEntry *client_head;
  */
 static struct ClientEntry *client_tail;
 
+/**
+ * Our notification context, used to send back results to the client.
+ */
+static struct GNUNET_SERVER_NotificationContext *nc;
+
+/**
+ * Private key for this peer.
+ */
+static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
+
 
 /**
  * Task run during shutdown.
@@ -107,6 +118,10 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   dht = NULL;
   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
   stats = NULL;
+  GNUNET_SERVER_notification_context_destroy (nc);
+  nc = NULL;
+  GNUNET_free (my_private_key);
+  my_private_key = NULL;
 }
 
 
@@ -135,12 +150,12 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
       }
       if (NULL != ce->ah)
       {
-       GNUNET_REGEX_announce_cancel (ce->ah);
+       REGEX_INTERNAL_announce_cancel (ce->ah);
        ce->ah = NULL;
       }
       if (NULL != ce->sh)
       {
-       GNUNET_REGEX_search_cancel (ce->sh);
+       REGEX_INTERNAL_search_cancel (ce->sh);
        ce->sh = NULL;
       }
       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
@@ -163,7 +178,7 @@ reannounce (void *cls,
 {
   struct ClientEntry *ce = cls;
 
-  GNUNET_REGEX_reannounce (ce->ah);
+  REGEX_INTERNAL_reannounce (ce->ah);
   ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
                                                   &reannounce,
                                                   ce);
@@ -178,7 +193,7 @@ reannounce (void *cls,
  * @param message the actual message
  */
 static void
-handle_announce (void *cls, 
+handle_announce (void *cls,
                 struct GNUNET_SERVER_Client *client,
                 const struct GNUNET_MessageHeader *message)
 {
@@ -197,26 +212,33 @@ handle_announce (void *cls,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
+
   ce = GNUNET_new (struct ClientEntry);
   ce->client = client;
-  ce->ah = GNUNET_REGEX_announce (dht,
-                                 &am->pid,
-                                 regex,
-                                 ntohs (am->compression),
-                                 stats);
+  ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
+  ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
+                                                  &reannounce,
+                                                  ce);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Starting to announce regex `%s' every %s\n",
+             regex,
+             GNUNET_STRINGS_relative_time_to_string (ce->frequency,
+                                                     GNUNET_NO));
+  ce->ah = REGEX_INTERNAL_announce (dht,
+                                   my_private_key,
+                                   regex,
+                                   ntohs (am->compression),
+                                   stats);
   if (NULL == ce->ah)
   {
     GNUNET_break (0);
+    GNUNET_SCHEDULER_cancel (ce->refresh_task);
     GNUNET_free (ce);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
-  ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
-                                                  &reannounce,
-                                                  ce);
   GNUNET_CONTAINER_DLL_insert (client_head,
-                              client_tail, 
+                              client_tail,
                               ce);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -232,7 +254,7 @@ handle_announce (void *cls,
  * @param put_path Path of the put request.
  * @param put_path_length Length of the put_path.
  */
-static void 
+static void
 handle_search_result (void *cls,
                      const struct GNUNET_PeerIdentity *id,
                      const struct GNUNET_PeerIdentity *get_path,
@@ -240,7 +262,37 @@ handle_search_result (void *cls,
                      const struct GNUNET_PeerIdentity *put_path,
                      unsigned int put_path_length)
 {
+  struct ClientEntry *ce = cls;
+  struct ResultMessage *result;
+  struct GNUNET_PeerIdentity *gp;
+  uint16_t size;
 
+  if ( (get_path_length >= 65536) ||
+       (put_path_length >= 65536) ||
+       ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity))
+       + sizeof (struct ResultMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity) + sizeof (struct ResultMessage);
+  result = GNUNET_malloc (size);
+  result->header.size = htons (size);
+  result->header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_RESULT);
+  result->get_path_length = htons ((uint16_t) get_path_length);
+  result->put_path_length = htons ((uint16_t) put_path_length);
+  result->id = *id;
+  gp = &result->id;
+  memcpy (&gp[1],
+         get_path,
+         get_path_length * sizeof (struct GNUNET_PeerIdentity));
+  memcpy (&gp[1 + get_path_length],
+         put_path,
+         put_path_length * sizeof (struct GNUNET_PeerIdentity));
+  GNUNET_SERVER_notification_context_unicast (nc,
+                                             ce->client,
+                                             &result->header, GNUNET_NO);
+  GNUNET_free (result);
 }
 
 
@@ -252,7 +304,7 @@ handle_search_result (void *cls,
  * @param message the actual message
  */
 static void
-handle_search (void *cls, 
+handle_search (void *cls,
               struct GNUNET_SERVER_Client *client,
               const struct GNUNET_MessageHeader *message)
 {
@@ -273,7 +325,7 @@ handle_search (void *cls,
   }
   ce = GNUNET_new (struct ClientEntry);
   ce->client = client;
-  ce->sh = GNUNET_REGEX_search (dht,
+  ce->sh = REGEX_INTERNAL_search (dht,
                                string,
                                &handle_search_result,
                                ce,
@@ -286,8 +338,9 @@ handle_search (void *cls,
     return;
   }
   GNUNET_CONTAINER_DLL_insert (client_head,
-                              client_tail, 
+                              client_tail,
                               ce);
+  GNUNET_SERVER_notification_context_add (nc, client);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -308,14 +361,24 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     {&handle_search, NULL, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 0},
     {NULL, NULL, 0, 0}
   };
+
+  my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
+  if (NULL == my_private_key)
+  {
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
   dht = GNUNET_DHT_connect (cfg, 1024);
   if (NULL == dht)
   {
+    GNUNET_free (my_private_key);
+    my_private_key = NULL;
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
                                 NULL);
+  nc = GNUNET_SERVER_notification_context_create (server, 1);
   stats = GNUNET_STATISTICS_create ("regex", cfg);
   GNUNET_SERVER_add_handlers (server, handlers);
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);