34 #include "microhttpd.h" 40 #define GNUNET_REST_API_NS_NAMESTORE "/namestore" 45 #define GNUNET_REST_NAMESTORE_ERROR_UNKNOWN "Unknown Error" 50 #define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found" 56 #define GNUNET_REST_NAMESTORE_FAILED "Namestore action failed" 61 #define GNUNET_REST_NAMESTORE_INVALID_DATA "Data invalid" 66 #define GNUNET_REST_NAMESTORE_NO_DATA "No data" 71 #define ID_REST_STATE_INIT 0 76 #define ID_REST_STATE_POST_INIT 1 298 if (NULL != handle->
url)
300 if (NULL != handle->
emsg)
302 if (NULL != handle->
rd)
304 for (
int i = 0; i < handle->
rd_count; i++)
306 if (NULL != handle->
rd[i].
data)
315 if (NULL != handle->
ns_qe)
338 struct MHD_Response *resp;
339 json_t *json_error = json_object ();
342 if (NULL == handle->
emsg)
345 json_object_set_new (json_error,
"error", json_string (handle->
emsg));
349 response = json_dumps (json_error, 0);
351 MHD_add_response_header (resp,
"Content-Type",
"application/json");
353 json_decref (json_error);
377 tmp = strtok (copy,
"/");
378 for (ego_entry = ego_head; NULL != ego_entry;
379 ego_entry = ego_entry->
next)
381 if (0 != strcasecmp (tmp, ego_entry->
identifier))
418 struct MHD_Response *resp;
420 handle->
ns_qe = NULL;
434 handle->
proc (handle->
proc_cls, resp, MHD_HTTP_NO_CONTENT);
451 handle->
ns_qe = NULL;
473 MHD_HTTP_NO_CONTENT);
489 struct MHD_Response *resp;
499 MHD_add_response_header (resp,
"Content-Type",
"application/json");
526 for (i = 0; i < rd_len; i++)
531 rd_filtered[j] = rd[i];
541 json_array_append_new (handle->
resp_object, record_obj);
575 handle->
ns_qe = NULL;
578 for (i = 0; i < rd_len; i++)
583 rd_filtered[j] = rd[i];
593 json_array_append_new (handle->
resp_object, record_obj);
631 if (NULL == ego_entry)
652 labelname = &egoname[strlen (ego_entry->
identifier)];
654 if (1 >= strlen (labelname))
681 if (NULL == handle->
ns_qe)
694 unsigned int rd_count,
704 for (i = 0; i < rd_count; i++)
707 for (j = 0; j < handle->rd_count; j++)
708 rd_new[i + j] = handle->rd[j];
716 if (NULL == handle->ns_qe)
756 data_js = json_loads (term_data, JSON_DECODE_ANY, &err);
765 json_decref (data_js);
774 json_decref (data_js);
777 json_decref (data_js);
793 if (NULL == ego_entry)
808 if (NULL == handle->
ns_qe)
883 if (NULL == ego_entry)
891 labelname = &egoname[strlen (ego_entry->
identifier)];
893 if (1 >= strlen (labelname))
909 if (NULL == handle->
ns_qe)
930 struct MHD_Response *resp;
935 MHD_add_response_header (resp,
"Access-Control-Allow-Methods",
allow_methods);
946 const char *identifier)
961 ego_entry->
ego = ego;
968 if (identifier != NULL)
970 for (ego_entry = ego_head; NULL != ego_entry;
971 ego_entry = ego_entry->
next)
973 if (ego_entry->
ego == ego)
981 if (NULL == ego_entry)
987 ego_entry->
ego = ego;
997 for (ego_entry = ego_head; NULL != ego_entry;
998 ego_entry = ego_entry->
next)
1000 if (ego_entry->
ego == ego)
1003 if (NULL == ego_entry)
1053 if (handle->
url[strlen (handle->
url) - 1] ==
'/')
1054 handle->
url[strlen (handle->
url) - 1] =
'\0';
1080 static struct Plugin plugin;
1084 if (NULL != plugin.
cfg)
1086 memset (&plugin, 0,
sizeof(
struct Plugin));
1094 "%s, %s, %s, %s, %s",
1095 MHD_HTTP_METHOD_GET,
1096 MHD_HTTP_METHOD_POST,
1097 MHD_HTTP_METHOD_PUT,
1098 MHD_HTTP_METHOD_DELETE,
1099 MHD_HTTP_METHOD_OPTIONS);
1124 while (NULL != (request = requests_head))
1126 if (NULL != identity_handle)
1128 if (NULL != ns_handle)
1131 for (ego_entry = ego_head; NULL != ego_entry;)
1133 ego_tmp = ego_entry;
1134 ego_entry = ego_entry->
next;
struct GNUNET_IDENTITY_Handle * GNUNET_IDENTITY_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_IDENTITY_Callback cb, void *cb_cls)
Connect to the identity service.
void * cls
Closure for parser and cleaner.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static struct GNUNET_NAMESTORE_Handle * ns_handle
Handle to NAMESTORE.
void namestore_update(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle namestore PUT request.
void(* GNUNET_REST_ResultProcessor)(void *cls, struct MHD_Response *resp, int status)
Iterator called on obtained result for a REST result.
#define GNUNET_REST_NAMESTORE_FAILED
Error message Failed request.
int record_type
Record type to look up.
static enum GNUNET_GenericReturnValue rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function processing the REST call.
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
#define GNUNET_REST_NAMESTORE_ERROR_UNKNOWN
Error message Unknown Error.
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration handle.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
static void namestore_iteration_error(void *cls)
Does internal server error when iteration failed.
void namestore_delete(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle namestore DELETE request.
void * cls
The closure of the plugin.
void * libgnunet_plugin_rest_namestore_done(void *cls)
Exit point from the plugin.
size_t data_size
The POST data size.
struct EgoEntry * get_egoentry_namestore(struct RequestHandle *handle, char *name)
Get EgoEntry from list with either a public key or a name If public key and name are not NULL...
enum GNUNET_GenericReturnValue GNUNET_JSON_parse(const json_t *root, struct GNUNET_JSON_Specification *spec, const char **error_json_name, unsigned int *error_line)
Navigate and parse data in a JSON tree.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
char * GNUNET_IDENTITY_public_key_to_string(const struct GNUNET_IDENTITY_PublicKey *key)
Creates a (Base32) string representation of the public key.
void * libgnunet_plugin_rest_namestore_init(void *cls)
Entry point for the plugin.
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (i.e.
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/'*'.
static void namestore_list_iteration(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone_key, const char *rname, unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd)
Create a response with requested records.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void create_finished(void *cls, int32_t success, const char *emsg)
Create finished callback.
A private key for an identity as per LSD0001.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
struct returned by the initialization function of the plugin
GNUNET_GenericReturnValue
Named constants for return values.
uint32_t record_type
Record type filter.
static void ns_get_lookup_cb(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd)
Entry in parser specification for GNUNET_JSON_parse().
static void ns_lookup_error_cb(void *cls)
Handle lookup error.
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_store(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_IDENTITY_PrivateKey *pkey, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, GNUNET_NAMESTORE_ContinuationWithStatus cont, void *cont_cls)
Store an item in the namestore.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_NAMESTORE_zone_iterator_next(struct GNUNET_NAMESTORE_ZoneIterator *it, uint64_t limit)
Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start for the next record...
void(* proc)(struct GNUNET_REST_RequestHandle *handle, const char *url, void *cls)
Namespace to handle.
int GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL)...
void GNUNET_IDENTITY_ego_get_public_key(struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_IDENTITY_PublicKey *pk)
Get the identifier (public key) of an ego.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * proc_cls
The closure of the result processor.
Handle for an operation with the identity service.
#define GNUNET_REST_HANDLER_END
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
void GNUNET_NAMESTORE_cancel(struct GNUNET_NAMESTORE_QueueEntry *qe)
Cancel a namestore operation.
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
static struct RequestHandle * requests_head
DLL.
static char * zone
Name of the zone we manage.
void namestore_add(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle namestore POST request.
struct GNUNET_IDENTITY_Ego * ego
The Ego.
void GNUNET_NAMESTORE_disconnect(struct GNUNET_NAMESTORE_Handle *h)
Disconnect from the namestore service (and free associated resources).
struct GNUNET_NAMESTORE_ZoneIterator * list_it
Handle to NAMESTORE it.
Handle for a zone iterator operation.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
static struct EgoEntry * ego_tail
Ego list.
char * record_name
Records to store.
json_t * resp_object
Response object.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
const void * data
Binary value stored in the DNS record.
char * emsg
Error response message.
void namestore_add_or_update(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle namestore POST/PUT request.
struct GNUNET_CONTAINER_MultiHashMap * url_param_map
Map of url parameters.
#define GNUNET_REST_IDENTITY_NOT_FOUND
Error message No identity found.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
const char * url
The url as string.
Connection to the NAMESTORE service.
static int state
The processing state.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
static struct EgoEntry * ego_head
Ego list.
struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
unsigned int rd_count
Number of records in rd.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
void GNUNET_IDENTITY_disconnect(struct GNUNET_IDENTITY_Handle *h)
Disconnect from identity service.
enum UpdateStrategy update_strategy
How to update the record set.
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Respond to OPTIONS request.
static void namestore_list_finished(void *cls)
Iteration over all results finished, build final response.
struct GNUNET_NAMESTORE_ZoneIterator * GNUNET_NAMESTORE_zone_iteration_start(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_IDENTITY_PrivateKey *zone, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls, GNUNET_SCHEDULER_TaskCallback finish_cb, void *finish_cb_cls)
Starts a new zone iteration (used to periodically PUT all of our records into our DHT)...
int GNUNET_REST_handle_request(struct GNUNET_REST_RequestHandle *conn, const struct GNUNET_REST_RequestHandler *handlers, struct GNUNET_REST_RequestHandlerError *err, void *cls)
void GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
Stops iteration and releases the namestore handle for further calls.
struct GNUNET_REST_RequestHandle * rest_handle
Rest connection.
int response_code
Response code.
#define GNUNET_REST_NAMESTORE_NO_DATA
Error message No data.
static char * plugin
Solver plugin name as string.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
const char * data
The POST data.
struct RequestHandle * next
DLL.
char * keystring
Public key string.
#define GNUNET_free_nz(ptr)
Wrapper around free.
static void ns_lookup_cb(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
struct GNUNET_JSON_Specification GNUNET_GNSRECORD_JSON_spec_gnsrecord(struct GNUNET_GNSRECORD_Data **rd, unsigned int *rd_count, char **name)
JSON Specification for GNS Records.
An QueueEntry used to store information for a pending NAMESTORE record operation. ...
static char * allow_methods
HTTP methods allows for this plugin.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
#define ID_REST_STATE_INIT
State while collecting all egos.
static void do_error(void *cls)
Task run on errors.
struct GNUNET_NAMESTORE_Handle * GNUNET_NAMESTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the namestore service.
struct RequestHandle * prev
DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
static struct GNUNET_IDENTITY_Handle * identity_handle
Handle to Identity service.
static struct GNUNET_SCHEDULER_Task * timeout_task
Task to be run on timeout.
#define GNUNET_REST_NAMESTORE_INVALID_DATA
Error message invalid data.
json_t * GNUNET_GNSRECORD_JSON_from_gnsrecord(const char *rname, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Convert GNS record to JSON.
struct GNUNET_NAMESTORE_QueueEntry * ns_qe
NAMESTORE Operation.
static void list_ego(void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, const char *identifier)
char * identifier
Ego Identifier.
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_lookup(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_IDENTITY_PrivateKey *pkey, const char *label, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor rm, void *rm_cls)
Lookup an item in the namestore.
#define ID_REST_STATE_POST_INIT
Done collecting egos.
void namestore_get(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle namestore GET request.
An identity key as per LSD0001.
uint32_t record_type
Type of the GNS/DNS record.
const struct GNUNET_IDENTITY_PrivateKey * zone_pkey
Private key for the zone.
const struct GNUNET_IDENTITY_PrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
struct GNUNET_SCHEDULER_Task * timeout_task
ID of a task associated with the resolution process.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
#define GNUNET_REST_API_NS_NAMESTORE
Namestore Namespace.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
static struct MHD_Response * response
Our canonical response.
struct GNUNET_GNSRECORD_Data * rd
Records to store.
static void del_finished(void *cls, int32_t success, const char *emsg)
Delete finished callback.
struct EgoEntry * prev
DLL.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_end(void)
End of a parser specification.
static struct GNUNET_ARM_Operation * op
Current operation.
struct EgoEntry * ego_entry
IDENTITY Operation.
enum GNUNET_GenericReturnValue(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
static struct RequestHandle * requests_tail
DLL.
struct EgoEntry * next
DLL.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.