gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r35376 - in gnunet/src: gns identity rest


From: gnunet
Subject: [GNUnet-SVN] r35376 - in gnunet/src: gns identity rest
Date: Thu, 12 Mar 2015 17:40:40 +0100

Author: schanzen
Date: 2015-03-12 17:40:40 +0100 (Thu, 12 Mar 2015)
New Revision: 35376

Added:
   gnunet/src/identity/plugin_rest_identity.c
Modified:
   gnunet/src/gns/plugin_rest_gns.c
   gnunet/src/identity/Makefile.am
   gnunet/src/rest/gnunet-rest-server.c
Log:
-add identity REST, fixes

Modified: gnunet/src/gns/plugin_rest_gns.c
===================================================================
--- gnunet/src/gns/plugin_rest_gns.c    2015-03-11 18:45:23 UTC (rev 35375)
+++ gnunet/src/gns/plugin_rest_gns.c    2015-03-12 16:40:40 UTC (rev 35376)
@@ -33,7 +33,7 @@
 #include <gnunet_gns_service.h>
 #include <jansson.h>
 
-#define API_NAMESPACE "gns"
+#define API_NAMESPACE "/gns"
 
 /**
  * @brief struct returned by the initialization function of the plugin
@@ -694,7 +694,7 @@
  * @return always NULL
  */
 void *
-libgnunet_plugin_namestore_sqlite_done (void *cls)
+libgnunet_plugin_rest_gns_done (void *cls)
 {
   struct GNUNET_REST_Plugin *api = cls;
   struct Plugin *plugin = api->cls;

Modified: gnunet/src/identity/Makefile.am
===================================================================
--- gnunet/src/identity/Makefile.am     2015-03-11 18:45:23 UTC (rev 35375)
+++ gnunet/src/identity/Makefile.am     2015-03-12 16:40:40 UTC (rev 35376)
@@ -1,6 +1,8 @@
 # This Makefile.am is in the public domain
 AM_CPPFLAGS = -I$(top_srcdir)/src/include
 
+ plugindir = $(libdir)/gnunet
+
 if MINGW
  WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols 
 endif
@@ -17,9 +19,26 @@
 pkgcfg_DATA = \
   identity.conf
 
+if HAVE_REST
+REST_PLUGIN = libgnunet_plugin_rest_identity.la
+plugin_LTLIBRARIES = ${REST_PLUGIN}
+endif
 
-lib_LTLIBRARIES = libgnunetidentity.la
+lib_LTLIBRARIES = \
+       libgnunetidentity.la 
 
+
+libgnunet_plugin_rest_identity_la_SOURCES = \
+  plugin_rest_identity.c
+libgnunet_plugin_rest_identity_la_LIBADD = \
+  $(top_builddir)/src/identity/libgnunetidentity.la \
+  $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \
+  $(LTLIBINTL) -ljansson -lmicrohttpd
+libgnunet_plugin_rest_identity_la_LDFLAGS = \
+ $(GN_PLUGIN_LDFLAGS)
+
+
+
 libgnunetidentity_la_SOURCES = \
   identity_api.c \
   identity_api_lookup.c \
@@ -64,6 +83,7 @@
 TESTS = $(check_PROGRAMS) 
 endif
 
+
 test_identity_SOURCES = \
  test_identity.c
 test_identity_LDADD = \

Added: gnunet/src/identity/plugin_rest_identity.c
===================================================================
--- gnunet/src/identity/plugin_rest_identity.c                          (rev 0)
+++ gnunet/src/identity/plugin_rest_identity.c  2015-03-12 16:40:40 UTC (rev 
35376)
@@ -0,0 +1,429 @@
+/*
+   This file is part of GNUnet.
+   Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
+
+   GNUnet is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation; either version 3, or (at your
+   option) any later version.
+
+   GNUnet is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNUnet; see the file COPYING.  If not, write to the
+   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+   */
+/**
+ * @author Martin Schanzenbach
+ * @file namestore/plugin_rest_namestore.c
+ * @brief GNUnet Namestore REST plugin
+ *
+ */
+
+#include "platform.h"
+#include "gnunet_rest_plugin.h"
+#include "gnunet_identity_service.h"
+#include "microhttpd.h"
+#include <jansson.h>
+
+#define API_NAMESPACE "/identity"
+
+#define EGO_NAMESPACE "/identity/ego"
+
+#define SVC_NAMESPACE "/identity/service"
+
+#define ID_REST_STATE_INIT 0
+
+#define ID_REST_STATE_POST_INIT 1
+
+/**
+ * @brief struct returned by the initialization function of the plugin
+ */
+struct Plugin
+{
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
+};
+
+const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+struct EgoEntry
+{
+  /**
+   * DLL
+   */
+  struct EgoEntry *next;
+  
+  /**
+   * DLL
+   */
+  struct EgoEntry *prev;
+  
+  /**
+   * Ego Identifier
+   */
+  char *identifier;
+  
+  /**
+   * Ego Pkey
+   */
+  struct GNUNET_CRYPTO_EcdsaPublicKey pk;
+};
+
+struct RequestHandle
+{
+  /**
+   * Ego list
+   */
+  struct EgoEntry *ego_head;
+
+  /**
+   * Ego list
+   */
+  struct EgoEntry *ego_tail;
+  
+  /**
+   * The processing state
+   */
+  int state;
+
+  /**
+   * Handle to GNS service.
+   */
+  struct GNUNET_IDENTITY_Handle *identity_handle;
+
+  /**
+   * Desired timeout for the lookup (default is no timeout).
+   */
+  struct GNUNET_TIME_Relative timeout;
+
+  /**
+   * ID of a task associated with the resolution process.
+   */
+  struct GNUNET_SCHEDULER_Task * timeout_task;    
+
+  /**
+   * The root of the received JSON or NULL
+   */
+  json_t *json_root;
+
+  /**
+   * The plugin result processor
+   */
+  GNUNET_REST_ResultProcessor proc;
+
+  /**
+   * The closure of the result processor
+   */
+  void *proc_cls;
+
+  /**
+   * The name to look up
+   */
+  char *name;
+
+  /**
+   * The ego set from REST
+   */
+  char *set_ego;
+  
+  /**
+   * The subsystem set from REST
+   */
+  char *set_subsystem;
+
+  /**
+   * The url
+   */
+  const char *url;
+
+  /**
+   * The data from the REST request
+   */
+  const char* data;
+
+  /**
+   * the length of the REST data
+   */
+  size_t data_size;
+
+};
+
+/**
+ * Cleanup lookup handle
+ * @praram handle Handle to clean up
+ */
+void
+cleanup_handle (struct RequestHandle *handle)
+{
+  struct EgoEntry *ego_entry;
+  struct EgoEntry *ego_tmp;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Cleaning up\n");
+  if (NULL != handle->json_root)
+    json_decref (handle->json_root);
+  if (NULL != handle->name)
+    GNUNET_free (handle->name);
+  if (NULL != handle->timeout_task)
+    GNUNET_SCHEDULER_cancel (handle->timeout_task);
+  if (NULL != handle->identity_handle)
+    GNUNET_IDENTITY_disconnect (handle->identity_handle);
+  if (NULL != handle->set_subsystem)
+    GNUNET_free (handle->set_subsystem);
+  if (NULL != handle->set_ego)
+    GNUNET_free (handle->set_ego);
+  for (ego_entry = handle->ego_head;
+       NULL != ego_entry;)
+  {
+    ego_tmp = ego_entry;
+    ego_entry = ego_entry->next;
+    GNUNET_free (ego_tmp->identifier);
+    GNUNET_free (ego_tmp);
+  }
+  GNUNET_free (handle);
+}
+
+
+/**
+ * Task run on shutdown.  Cleans up everything.
+ *
+ * @param cls unused
+ * @param tc scheduler context
+ */
+static void
+do_error (void *cls,
+          const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct RequestHandle *handle = cls;
+  handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+  cleanup_handle (handle);
+}
+
+
+
+void
+ego_info_response (struct RequestHandle *handle)
+{
+  const char* egoname;
+  char* keystring;
+  char* result_str;
+  struct EgoEntry *ego_entry;
+  json_t *ego_arr;
+  json_t *ego_json;
+
+  if (strlen (EGO_NAMESPACE) > strlen (handle->url))
+  {
+    handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
+    cleanup_handle (handle);
+    GNUNET_break (0);
+    return;
+  }
+  ego_arr = json_array ();
+
+  egoname = &handle->url[strlen (EGO_NAMESPACE)];
+
+  if (strlen (EGO_NAMESPACE) + 1 >= strlen (handle->url))
+  {
+    egoname = NULL;
+  }
+
+  //Return all egos
+    for (ego_entry = handle->ego_head;
+       NULL != ego_entry;
+       ego_entry = ego_entry->next)
+  {
+    if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
+      continue;
+    ego_json = json_object ();
+    keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk);
+    json_object_set_new (ego_json, "identity", json_string 
(ego_entry->identifier));
+    json_object_set_new (ego_json, "key", json_string (keystring));
+    json_array_append (ego_arr, ego_json);
+    json_decref (ego_json);
+    GNUNET_free (keystring);
+  }
+  result_str = json_dumps (ego_arr, JSON_COMPACT);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
+  json_decref (ego_arr);
+  handle->proc (handle->proc_cls, result_str, strlen (result_str), GNUNET_OK);
+  GNUNET_free (result_str);
+  cleanup_handle (handle);
+
+}
+
+
+/**
+ * If listing is enabled, prints information about the egos.
+ *
+ * This function is initially called for all egos and then again
+ * whenever a ego's identifier changes or if it is deleted.  At the
+ * end of the initial pass over all egos, the function is once called
+ * with 'NULL' for 'ego'. That does NOT mean that the callback won't
+ * be invoked in the future or that there was an error.
+ *
+ * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
+ * this function is only called ONCE, and 'NULL' being passed in
+ * 'ego' does indicate an error (i.e. name is taken or no default
+ * value is known).  If 'ego' is non-NULL and if '*ctx'
+ * is set in those callbacks, the value WILL be passed to a subsequent
+ * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
+ * that one was not NULL).
+ *
+ * When an identity is renamed, this function is called with the
+ * (known) ego but the NEW identifier.
+ *
+ * When an identity is deleted, this function is called with the
+ * (known) ego and "NULL" for the 'identifier'.  In this case,
+ * the 'ego' is henceforth invalid (and the 'ctx' should also be
+ * cleaned up).
+ *
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+*/
+static void
+list_ego (void *cls,
+          struct GNUNET_IDENTITY_Ego *ego,
+          void **ctx,
+          const char *identifier)
+{
+  struct RequestHandle *handle = cls;
+  struct EgoEntry *ego_entry;
+  
+  if ( (NULL == handle->set_ego) &&
+       (NULL != ego) &&
+       (NULL != identifier) &&
+       (0 == strcmp (identifier,
+                     handle->set_ego)) )
+  {
+    /*handle->set_op = GNUNET_IDENTITY_set (sh,
+                                          handle->set_subsystem,
+                                          ego,
+                                          &set_done,
+                                          handle);
+    GNUNET_free (handle->set_subsystem);
+    handle->set_subsystem = NULL;
+    GNUNET_free (handle->set_ego); //decref?
+    handle->set_ego = NULL;TODO*/
+  }
+  if ( (NULL == ego) &&
+       (NULL != handle->set_ego) )
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not set ego to `%s' for subsystem `%s', ego not 
known\n",
+                handle->set_ego,
+                handle->set_subsystem);
+    GNUNET_free (handle->set_subsystem);
+    handle->set_subsystem = NULL;
+    GNUNET_free (handle->set_ego); //decref?
+    handle->set_ego = NULL;
+  }
+  if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
+  {
+    //TODO all read
+    handle->state = ID_REST_STATE_POST_INIT;
+    ego_info_response (handle);
+    return;
+  }
+  if (ID_REST_STATE_INIT == handle->state) {
+        ego_entry = GNUNET_new (struct EgoEntry);
+    GNUNET_IDENTITY_ego_get_public_key (ego, &(ego_entry->pk));
+    GNUNET_asprintf (&ego_entry->identifier, "%s", identifier);
+    GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, 
ego_entry);
+  }
+}
+
+/**
+ * Function processing the REST call
+ *
+ * @param method HTTP method
+ * @param url URL of the HTTP request
+ * @param data body of the HTTP request (optional)
+ * @param data_size length of the body
+ * @param proc callback function for the result
+ * @param proc_cls closure for callback function
+ * @return GNUNET_OK if request accepted
+ */
+void
+rest_identity_process_request(const char *method,
+                               const char *url,
+                               const char *data,
+                               size_t data_size,
+                               GNUNET_REST_ResultProcessor proc,
+                               void *proc_cls)
+{
+  struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
+
+
+
+  handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
+
+  handle->proc_cls = proc_cls;
+  handle->proc = proc;
+  handle->state = ID_REST_STATE_INIT;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Connecting...\n");
+  handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &list_ego, handle); 
+  handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
+                                                       &do_error, handle);
+  handle->data = data;
+  handle->data_size = data_size;
+  handle->url = url;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Connected\n");
+}
+
+/**
+ * Entry point for the plugin.
+ *
+ * @param cls Config info
+ * @return NULL on error, otherwise the plugin context
+ */
+void *
+libgnunet_plugin_rest_identity_init (void *cls)
+{
+  static struct Plugin plugin;
+  cfg = cls;
+  struct GNUNET_REST_Plugin *api;
+
+  if (NULL != plugin.cfg)
+    return NULL;                /* can only initialize once! */
+  memset (&plugin, 0, sizeof (struct Plugin));
+  plugin.cfg = cfg;
+  api = GNUNET_new (struct GNUNET_REST_Plugin);
+  api->cls = &plugin;
+  api->name = API_NAMESPACE;
+  api->process_request = &rest_identity_process_request;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              _("Identity REST API initialized\n"));
+  return api;
+}
+
+
+/**
+ * Exit point from the plugin.
+ *
+ * @param cls the plugin context (as returned by "init")
+ * @return always NULL
+ */
+void *
+libgnunet_plugin_rest_identity_done (void *cls)
+{
+  struct GNUNET_REST_Plugin *api = cls;
+  struct Plugin *plugin = api->cls;
+
+  plugin->cfg = NULL;
+  GNUNET_free (api);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Identity REST plugin is finished\n");
+  return NULL;
+}
+
+/* end of plugin_rest_gns.c */

Modified: gnunet/src/rest/gnunet-rest-server.c
===================================================================
--- gnunet/src/rest/gnunet-rest-server.c        2015-03-11 18:45:23 UTC (rev 
35375)
+++ gnunet/src/rest/gnunet-rest-server.c        2015-03-12 16:40:40 UTC (rev 
35376)
@@ -597,7 +597,9 @@
                 libname);
     return;
   }
-  GNUNET_CRYPTO_hash (plugin->name, strlen (plugin->name), &key);
+  GNUNET_assert (1 < strlen (plugin->name));
+  GNUNET_assert ('/' == *plugin->name);
+  GNUNET_CRYPTO_hash (plugin->name+1, strlen (plugin->name+1), &key);
   if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map,
                                                       &key,
                                                       plugin,
@@ -608,7 +610,7 @@
                 libname);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Loaded plugin `%s'\n",
               libname);
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]