GNUnet  0.10.x
plugin_rest_gns.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012-2015 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
27 #include "gnunet_rest_plugin.h"
28 #include "gnunet_rest_lib.h"
29 #include "gnunet_json_lib.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_gns_service.h"
32 #include "microhttpd.h"
33 #include <jansson.h>
34 
38 #define GNUNET_REST_API_NS_GNS "/gns"
39 
43 #define GNUNET_REST_GNS_PARAM_RECORD_TYPE "record_type"
44 
48 #define GNUNET_REST_GNS_ERROR_UNKNOWN "Unknown Error"
49 
53 #define GNUNET_REST_GNS_NOT_FOUND "Record not found"
54 
59 
63 static char *allow_methods;
64 
68 struct Plugin {
69  const struct GNUNET_CONFIGURATION_Handle *cfg;
70 };
71 
75 struct RequestHandle {
80 
85 
89  char *name;
90 
95 
99  struct GNUNET_REST_RequestHandle *rest_handle;
100 
105 
110 
115 
119  void *proc_cls;
120 
124  char *url;
125 
129  char *emsg;
130 
134  int response_code;
135 };
136 
137 
142 static void
143 cleanup_handle(void *cls)
144 {
145  struct RequestHandle *handle = cls;
146 
147  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
148 
149  if (NULL != handle->gns_lookup)
150  {
152  handle->gns_lookup = NULL;
153  }
154  if (NULL != handle->gns)
155  {
156  GNUNET_GNS_disconnect(handle->gns);
157  handle->gns = NULL;
158  }
159 
160  if (NULL != handle->timeout_task)
161  {
163  handle->timeout_task = NULL;
164  }
165  if (NULL != handle->url)
166  GNUNET_free(handle->url);
167  if (NULL != handle->name)
168  GNUNET_free(handle->name);
169  if (NULL != handle->emsg)
170  GNUNET_free(handle->emsg);
171 
172  GNUNET_free(handle);
173 }
174 
175 
181 static void
182 do_error(void *cls)
183 {
184  struct RequestHandle *handle = cls;
185  struct MHD_Response *resp;
186  json_t *json_error = json_object();
187  char *response;
188 
189  if (NULL != handle->timeout_task)
191  handle->timeout_task = NULL;
192  if (NULL == handle->emsg)
194 
195  json_object_set_new(json_error, "error", json_string(handle->emsg));
196 
197  if (0 == handle->response_code)
198  handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
199  response = json_dumps(json_error, 0);
200  resp = GNUNET_REST_create_response(response);
201  handle->proc(handle->proc_cls, resp, handle->response_code);
202  json_decref(json_error);
203  GNUNET_free(response);
205 }
206 
207 
208 static void
209 do_timeout(void *cls)
210 {
211  struct RequestHandle *handle = cls;
212 
213  handle->timeout_task = NULL;
214  handle->response_code = MHD_HTTP_REQUEST_TIMEOUT;
215  do_error(handle);
216 }
217 
218 
227 static void
229  int was_gns,
230  uint32_t rd_count,
231  const struct GNUNET_GNSRECORD_Data *rd)
232 {
233  struct RequestHandle *handle = cls;
234  struct MHD_Response *resp;
235  json_t *result_obj;
236  char *result;
237 
238  handle->gns_lookup = NULL;
239 
240  if (GNUNET_NO == was_gns)
241  {
242  handle->response_code = MHD_HTTP_NOT_FOUND;
245  return;
246  }
247 
248  result_obj = GNUNET_JSON_from_gnsrecord(handle->name, rd, rd_count);
249 
250  result = json_dumps(result_obj, 0);
251  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
252  resp = GNUNET_REST_create_response(result);
253  handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
254  GNUNET_free(result);
255  json_decref(result_obj);
257 }
258 
259 
267 void
269  const char *url,
270  void *cls)
271 {
272  struct RequestHandle *handle = cls;
273  struct GNUNET_HashCode key;
274  char *record_type;
275  char *name;
276 
277  name = NULL;
278  handle->name = NULL;
279  if (strlen(GNUNET_REST_API_NS_GNS) < strlen(handle->url))
280  {
281  name = &handle->url[strlen(GNUNET_REST_API_NS_GNS) + 1];
282  }
283 
284  if (NULL == name)
285  {
286  handle->response_code = MHD_HTTP_NOT_FOUND;
289  return;
290  }
291  if (0 >= strlen(name))
292  {
293  handle->response_code = MHD_HTTP_NOT_FOUND;
296  return;
297  }
298  handle->name = GNUNET_strdup(name);
299 
300  handle->record_type = UINT32_MAX;
303  &key);
304  if (GNUNET_YES ==
306  {
307  record_type =
309  handle->record_type = GNUNET_GNSRECORD_typename_to_number(record_type);
310  }
311 
312  if (UINT32_MAX == handle->record_type)
313  {
315  }
316 
317  handle->gns_lookup = GNUNET_GNS_lookup_with_tld(handle->gns,
318  handle->name,
319  handle->record_type,
320  GNUNET_NO,
322  handle);
323 }
324 
325 
333 static void
335  const char *url,
336  void *cls)
337 {
338  struct MHD_Response *resp;
339  struct RequestHandle *handle = cls;
340 
341  //independent of path return all options
342  resp = GNUNET_REST_create_response(NULL);
343  MHD_add_response_header(resp, "Access-Control-Allow-Methods", allow_methods);
344  handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
346  return;
347 }
348 
349 
355 static void
357 {
359  static const struct GNUNET_REST_RequestHandler handlers[] =
360  { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont },
361  { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont },
363 
364  if (GNUNET_NO ==
365  GNUNET_REST_handle_request(handle->rest_handle, handlers, &err, handle))
366  {
367  handle->response_code = err.error_code;
369  }
370 }
371 
372 
384 static void
387  void *proc_cls)
388 {
389  struct RequestHandle *handle = GNUNET_new(struct RequestHandle);
390 
391  handle->response_code = 0;
392  handle->timeout =
394  handle->proc_cls = proc_cls;
395  handle->proc = proc;
396  handle->rest_handle = rest_handle;
397 
398  handle->url = GNUNET_strdup(rest_handle->url);
399  if (handle->url[strlen(handle->url) - 1] == '/')
400  handle->url[strlen(handle->url) - 1] = '\0';
401  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
402  handle->gns = GNUNET_GNS_connect(cfg);
403  init_cont(handle);
404 
405  handle->timeout_task =
407 
408  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
409 }
410 
411 
418 void *
420 {
421  static struct Plugin plugin;
422  struct GNUNET_REST_Plugin *api;
423 
424  cfg = cls;
425  if (NULL != plugin.cfg)
426  return NULL; /* can only initialize once! */
427  memset(&plugin, 0, sizeof(struct Plugin));
428  plugin.cfg = cfg;
429  api = GNUNET_new(struct GNUNET_REST_Plugin);
430  api->cls = &plugin;
434  "%s, %s, %s, %s, %s",
435  MHD_HTTP_METHOD_GET,
436  MHD_HTTP_METHOD_POST,
437  MHD_HTTP_METHOD_PUT,
438  MHD_HTTP_METHOD_DELETE,
439  MHD_HTTP_METHOD_OPTIONS);
440 
441  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Gns REST API initialized\n"));
442  return api;
443 }
444 
445 
452 void *
454 {
455  struct GNUNET_REST_Plugin *api = cls;
456  struct Plugin *plugin = api->cls;
457 
458  plugin->cfg = NULL;
459 
461  GNUNET_free(api);
462  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Gns REST plugin is finished\n");
463  return NULL;
464 }
465 
466 /* end of plugin_rest_gns.c */
467 
Connection to the GNS service.
Definition: gns_api.h:35
char * name
Plugin name.
void(* GNUNET_REST_ResultProcessor)(void *cls, struct MHD_Response *resp, int status)
Iterator called on obtained result for a REST result.
static void rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function processing the REST call.
int record_type
Record type to look up.
void * libgnunet_plugin_rest_gns_init(void *cls)
Entry point for the plugin.
functions to parse JSON objects into GNUnet objects
void * cls
The closure of the plugin.
void * GNUNET_GNS_lookup_with_tld_cancel(struct GNUNET_GNS_LookupWithTldRequest *ltr)
Cancel pending lookup request.
Definition: gns_tld_api.c:328
GNUNET_REST_ResultProcessor proc
The plugin result processor.
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (i.e.
Definition: gnsrecord.c:203
#define GNUNET_GNSRECORD_TYPE_ANY
Record type indicating any record/&#39;*&#39;.
#define GNUNET_TIME_UNIT_SECONDS
One second.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void handle_gns_response(void *cls, int was_gns, uint32_t rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Iterator called on obtained result for a GNS lookup.
void * libgnunet_plugin_rest_gns_done(void *cls)
Exit point from the plugin.
struct GNUNET_GNS_LookupWithTldRequest * GNUNET_GNS_lookup_with_tld(struct GNUNET_GNS_Handle *handle, const char *name, uint32_t type, enum GNUNET_GNS_LocalOptions options, GNUNET_GNS_LookupResultProcessor2 proc, void *proc_cls)
Perform an asynchronous lookup operation on the GNS, determining the zone using the TLD of the given ...
Definition: gns_tld_api.c:240
struct returned by the initialization function of the plugin
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
void get_gns_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle gns GET request.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
static void init_cont(struct RequestHandle *handle)
Handle rest request.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * proc_cls
The closure of the result processor.
#define GNUNET_REST_HANDLER_END
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:46
The request handle.
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_REST_GNS_NOT_FOUND
Rest API GNS ERROR Record not found.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
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.
Definition: scheduler.c:1237
char * name
Name to look up.
#define GNUNET_REST_GNS_PARAM_RECORD_TYPE
Rest API GNS Parameter record_type.
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:114
char * emsg
Error response message.
struct GNUNET_CONTAINER_MultiHashMap * url_param_map
Map of url parameters.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
const char * url
The url as string.
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.
Definition: scheduler.c:1264
char * name
Plugin name.
void GNUNET_GNS_disconnect(struct GNUNET_GNS_Handle *handle)
Shutdown connection with the GNS service.
Definition: gns_api.c:282
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration handle.
struct GNUNET_TIME_Relative timeout
Timeout.
unsigned int rd_count
Number of records in rd.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static int result
Global testing status.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:440
static void cleanup_handle(void *cls)
Cleanup lookup handle.
struct GNUNET_GNS_Handle * GNUNET_GNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the GNS service.
Definition: gns_api.c:260
json_t * GNUNET_JSON_from_gnsrecord(const char *rname, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Convert Gns record to JSON.
int GNUNET_REST_handle_request(struct GNUNET_REST_RequestHandle *conn, const struct GNUNET_REST_RequestHandler *handlers, struct GNUNET_REST_RequestHandlerError *err, void *cls)
Definition: rest.c:75
struct GNUNET_REST_RequestHandle * rest_handle
Handle to rest request.
A 512-bit hashcode.
int response_code
HTTP response code.
static void do_error(void *cls)
Task run on errors.
static struct GNUNET_SCHEDULER_Task * timeout_task
Task to be run on timeout.
Definition: gnunet-arm.c:119
struct GNUNET_GNS_LookupWithTldRequest * gns_lookup
Active GNS lookup.
configuration data
Definition: configuration.c:83
Handle for a plugin.
Definition: block.c:37
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.
Definition: rest.c:56
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
#define GNUNET_REST_GNS_ERROR_UNKNOWN
Rest API GNS ERROR Unknown Error.
static struct MHD_Response * response
Our canonical response.
struct GNUNET_GNSRECORD_Data * rd
Records to store.
#define GNUNET_YES
Definition: gnunet_common.h:77
Handle to a lookup request.
Definition: gns_tld_api.c:44
#define GNUNET_REST_API_NS_GNS
Rest API GNS Namespace.
struct GNUNET_GNS_Handle * gns
Connection to GNS.
static void do_timeout(void *cls)
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Respond to OPTIONS request.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
char * url
The url.
static char * allow_methods
HTTP methods allows for this plugin.