GNUnet  0.11.x
Data Structures | Macros | Functions | Variables
plugin_rest_peerinfo.c File Reference
#include "platform.h"
#include "gnunet_rest_plugin.h"
#include "gnunet_peerinfo_service.h"
#include "gnunet_transport_service.h"
#include "gnunet_rest_lib.h"
#include "gnunet_json_lib.h"
#include "microhttpd.h"
#include <jansson.h>
Include dependency graph for plugin_rest_peerinfo.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 
struct  AddressRecord
 Record we keep for each printable address. More...
 
struct  PrintContext
 Structure we use to collect printable address information. More...
 
struct  RequestHandle
 The request handle. More...
 

Macros

#define GNUNET_REST_API_NS_PEERINFO   "/peerinfo"
 Peerinfo Namespace. More...
 
#define GNUNET_REST_PEERINFO_PEER   "peer"
 Peerinfo parameter peer. More...
 
#define GNUNET_REST_PEERINFO_FRIEND   "friend"
 Peerinfo parameter friend. More...
 
#define GNUNET_REST_PEERINFO_ARRAY   "array"
 Peerinfo parameter array. More...
 
#define GNUNET_REST_PEERINFO_ERROR_UNKNOWN   "Unknown Error"
 Error message Unknown Error. More...
 
#define TIMEOUT   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
 How long until we time out during address lookup? More...
 

Functions

static void cleanup_handle (void *cls)
 Cleanup lookup handle. More...
 
static void do_error (void *cls)
 Task run on errors. More...
 
static void peerinfo_list_finished (void *cls)
 Function that assembles the response. More...
 
static int count_address (void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
 Iterator callback to go over all addresses and count them. More...
 
static void dump_pc (struct PrintContext *pc)
 Print the collected address information to the console and free pc. More...
 
static void process_resolved_address (void *cls, const char *address, int res)
 Function to call with a human-readable format of an address. More...
 
static int print_address (void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
 Iterator callback to go over all addresses. More...
 
void peerinfo_list_iteration (void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HELLO_Message *hello, const char *err_msg)
 Callback that processes each of the known HELLOs for the iteration response construction. More...
 
void peerinfo_get (struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
 Handle peerinfo GET request. More...
 
static void options_cont (struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
 Respond to OPTIONS request. More...
 
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. More...
 
void * libgnunet_plugin_rest_peerinfo_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_rest_peerinfo_done (void *cls)
 Exit point from the plugin. More...
 

Variables

const struct GNUNET_CONFIGURATION_Handlecfg
 The configuration handle. More...
 
static char * allow_methods
 HTTP methods allows for this plugin. More...
 
static struct GNUNET_PEERINFO_Handlepeerinfo_handle
 Handle to PEERINFO. More...
 
static struct PrintContextpc_head
 Head of list of print contexts. More...
 
static struct PrintContextpc_tail
 Tail of list of print contexts. More...
 
static struct RequestHandlerequests_head
 DLL. More...
 
static struct RequestHandlerequests_tail
 DLL. More...
 

Macro Definition Documentation

◆ GNUNET_REST_API_NS_PEERINFO

#define GNUNET_REST_API_NS_PEERINFO   "/peerinfo"

Peerinfo Namespace.

Definition at line 39 of file plugin_rest_peerinfo.c.

◆ GNUNET_REST_PEERINFO_PEER

#define GNUNET_REST_PEERINFO_PEER   "peer"

Peerinfo parameter peer.

Definition at line 44 of file plugin_rest_peerinfo.c.

◆ GNUNET_REST_PEERINFO_FRIEND

#define GNUNET_REST_PEERINFO_FRIEND   "friend"

Peerinfo parameter friend.

Definition at line 49 of file plugin_rest_peerinfo.c.

◆ GNUNET_REST_PEERINFO_ARRAY

#define GNUNET_REST_PEERINFO_ARRAY   "array"

Peerinfo parameter array.

Definition at line 54 of file plugin_rest_peerinfo.c.

◆ GNUNET_REST_PEERINFO_ERROR_UNKNOWN

#define GNUNET_REST_PEERINFO_ERROR_UNKNOWN   "Unknown Error"

Error message Unknown Error.

Definition at line 59 of file plugin_rest_peerinfo.c.

◆ TIMEOUT

How long until we time out during address lookup?

Definition at line 64 of file plugin_rest_peerinfo.c.

Function Documentation

◆ cleanup_handle()

static void cleanup_handle ( void *  cls)
static

Cleanup lookup handle.

Parameters
handleHandle to clean up

Definition at line 279 of file plugin_rest_peerinfo.c.

280 {
281  struct RequestHandle *handle = cls;
282 
284  "Cleaning up\n");
285  if (NULL != handle->timeout_task)
286  {
287  GNUNET_SCHEDULER_cancel (handle->timeout_task);
288  handle->timeout_task = NULL;
289  }
290  if (NULL != handle->url)
291  GNUNET_free (handle->url);
292  if (NULL != handle->emsg)
293  GNUNET_free (handle->emsg);
294  if (NULL != handle->address)
295  GNUNET_free_nz ((char *) handle->address);
296  if (NULL != handle->expiration_str)
297  GNUNET_free (handle->expiration_str);
298  if (NULL != handle->pubkey)
299  GNUNET_free (handle->pubkey);
300 
301  if (NULL != handle->temp_array)
302  {
303  json_decref (handle->temp_array);
304  handle->temp_array = NULL;
305  }
306  if (NULL != handle->response)
307  {
308  json_decref (handle->response);
309  handle->response = NULL;
310  }
311 
312  if (NULL != handle->list_it)
313  {
315  handle->list_it = NULL;
316  }
317  if (NULL != peerinfo_handle)
318  {
320  peerinfo_handle = NULL;
321  }
324  handle);
326 }
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
#define GNUNET_log(kind,...)
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
void GNUNET_PEERINFO_iterate_cancel(struct GNUNET_PEERINFO_IteratorContext *ic)
Cancel an iteration over peer information.
Definition: peerinfo_api.c:520
void GNUNET_PEERINFO_disconnect(struct GNUNET_PEERINFO_Handle *h)
Disconnect from the peerinfo service.
Definition: peerinfo_api.c:156
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
static struct GNUNET_PEERINFO_Handle * peerinfo_handle
Handle to PEERINFO.
static struct RequestHandle * requests_head
DLL.
static struct RequestHandle * requests_tail
DLL.
The request handle.

References GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_free_nz, GNUNET_log, GNUNET_PEERINFO_disconnect(), GNUNET_PEERINFO_iterate_cancel(), GNUNET_SCHEDULER_cancel(), handle, peerinfo_handle, requests_head, and requests_tail.

Referenced by do_error(), libgnunet_plugin_rest_peerinfo_done(), options_cont(), and peerinfo_list_finished().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_error()

static void do_error ( void *  cls)
static

Task run on errors.

Reports an error and cleans up everything.

Parameters
clsthe struct RequestHandle

Definition at line 335 of file plugin_rest_peerinfo.c.

336 {
337  struct RequestHandle *handle = cls;
338  struct MHD_Response *resp;
339  json_t *json_error = json_object ();
340  char *response;
341 
342  if (NULL == handle->emsg)
344 
345  json_object_set_new (json_error, "error", json_string (handle->emsg));
346 
347  if (0 == handle->response_code)
348  handle->response_code = MHD_HTTP_OK;
349  response = json_dumps (json_error, 0);
351  MHD_add_response_header (resp, "Content-Type", "application/json");
352  handle->proc (handle->proc_cls, resp, handle->response_code);
353  json_decref (json_error);
356 }
static struct MHD_Response * response
Our canonical response.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
Definition: rest.c:57
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:1296
#define GNUNET_REST_PEERINFO_ERROR_UNKNOWN
Error message Unknown Error.
static void cleanup_handle(void *cls)
Cleanup lookup handle.

References cleanup_handle(), GNUNET_free, GNUNET_REST_create_response(), GNUNET_REST_PEERINFO_ERROR_UNKNOWN, GNUNET_SCHEDULER_add_now(), GNUNET_strdup, handle, and response.

Referenced by peerinfo_list_finished(), and peerinfo_list_iteration().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ peerinfo_list_finished()

static void peerinfo_list_finished ( void *  cls)
static

Function that assembles the response.

Parameters
clsthe struct RequestHandle

Definition at line 365 of file plugin_rest_peerinfo.c.

366 {
367  struct RequestHandle *handle = cls;
368  char *result_str;
369  struct MHD_Response *resp;
370 
371  if (NULL == handle->response)
372  {
373  handle->response_code = MHD_HTTP_NOT_FOUND;
374  handle->emsg = GNUNET_strdup ("No peers found");
376  return;
377  }
378 
379  result_str = json_dumps (handle->response, 0);
380  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
381  resp = GNUNET_REST_create_response (result_str);
382  GNUNET_assert (MHD_NO != MHD_add_response_header (resp,
383  "Content-Type",
384  "application/json"));
385  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
386  GNUNET_free (result_str);
388 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void do_error(void *cls)
Task run on errors.

References cleanup_handle(), do_error(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_REST_create_response(), GNUNET_SCHEDULER_add_now(), GNUNET_strdup, and handle.

Referenced by dump_pc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ count_address()

static int count_address ( void *  cls,
const struct GNUNET_HELLO_Address address,
struct GNUNET_TIME_Absolute  expiration 
)
static

Iterator callback to go over all addresses and count them.

Parameters
clsstruct PrintContext * with off to increment
addressthe address
expirationexpiration time
Returns
GNUNET_OK to keep the address and continue

Definition at line 400 of file plugin_rest_peerinfo.c.

403 {
404  struct PrintContext *pc = cls;
405 
406  if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
407  {
408  return GNUNET_OK; /* ignore expired address */
409  }
410 
411  pc->off++;
412  return GNUNET_OK;
413 }
static char * expiration
Credential TTL.
Definition: gnunet-abd.c:96
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
@ GNUNET_OK
Definition: gnunet_common.h:95
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:232
Structure we use to collect printable address information.

References expiration, GNUNET_OK, GNUNET_TIME_absolute_get_remaining(), and pc.

Referenced by peerinfo_list_iteration().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump_pc()

static void dump_pc ( struct PrintContext pc)
static

Print the collected address information to the console and free pc.

Parameters
pcprinting context

Definition at line 422 of file plugin_rest_peerinfo.c.

423 {
424  struct RequestHandle *handle;
425  unsigned int i;
426  json_t *response_entry;
427  json_t *temp_array;
428  json_t *object;
429  json_t *address;
430  json_t *expires;
431  json_t *friend_and_peer_json;
432  char *friend_and_peer;
433 
434  temp_array = json_array ();
435  response_entry = json_object ();
436 
437  for (i = 0; i < pc->num_addresses; i++)
438  {
439  if (NULL != pc->address_list[i].result)
440  {
441  object = json_object ();
442  address = json_string (pc->address_list[i].result);
443  expires = json_string (
445  pc->address_list[i].expiration));
446  json_object_set (object, "address", address);
447  json_object_set (object, "expires", expires);
448 
449  json_decref (address);
450  json_decref (expires);
451 
452  json_array_append (temp_array, object);
453  json_decref (object);
454  GNUNET_free (pc->address_list[i].result);
455  }
456  }
457 
458  if (0 < json_array_size (temp_array))
459  {
460  GNUNET_asprintf (&friend_and_peer,
461  "%s%s",
462  (GNUNET_YES == pc->friend_only) ? "F2F:" : "",
463  GNUNET_i2s_full (&pc->peer));
464  friend_and_peer_json = json_string (friend_and_peer);
465  json_object_set (response_entry,
467  friend_and_peer_json);
468  json_object_set (response_entry,
470  temp_array);
471  json_array_append (pc->handle->response, response_entry);
472  json_decref (friend_and_peer_json);
473  GNUNET_free (friend_and_peer);
474  }
475 
476  json_decref (temp_array);
477  json_decref (response_entry);
478 
479  GNUNET_free (pc->address_list);
481  pc_tail,
482  pc);
483  handle = pc->handle;
484  GNUNET_free (pc);
485 
486  if ((NULL == pc_head) &&
487  (NULL == handle->list_it))
488  {
490  }
491 }
static char * address
GNS address for this phone.
@ GNUNET_YES
Definition: gnunet_common.h:97
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:604
static struct PrintContext * pc_head
Head of list of print contexts.
#define GNUNET_REST_PEERINFO_PEER
Peerinfo parameter peer.
#define GNUNET_REST_PEERINFO_ARRAY
Peerinfo parameter array.
static struct PrintContext * pc_tail
Tail of list of print contexts.
static void peerinfo_list_finished(void *cls)
Function that assembles the response.
json_t * temp_array
JSON temporary array.

References address, GNUNET_asprintf(), GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_i2s_full(), GNUNET_REST_PEERINFO_ARRAY, GNUNET_REST_PEERINFO_PEER, GNUNET_SCHEDULER_add_now(), GNUNET_STRINGS_absolute_time_to_string(), GNUNET_YES, handle, pc, pc_head, pc_tail, peerinfo_list_finished(), and RequestHandle::temp_array.

Referenced by peerinfo_list_iteration(), and process_resolved_address().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_resolved_address()

static void process_resolved_address ( void *  cls,
const char *  address,
int  res 
)
static

Function to call with a human-readable format of an address.

Parameters
clsclosure
addressNULL on error, otherwise 0-terminated printable UTF-8 string
resresult of the address to string conversion: if GNUNET_OK: address was valid (conversion to string might still have failed) if GNUNET_SYSERR: address is invalid

Definition at line 505 of file plugin_rest_peerinfo.c.

508 {
509  struct AddressRecord *ar = cls;
510  struct PrintContext *pc = ar->pc;
511 
512  if (NULL != address)
513  {
514  if (0 != strlen (address))
515  {
516  if (NULL != ar->result)
517  GNUNET_free (ar->result);
518  ar->result = GNUNET_strdup (address);
519  }
520  return;
521  }
522  ar->atsc = NULL;
523  if (GNUNET_SYSERR == res)
525  _ ("Failure: Cannot convert address to string for peer `%s'\n"),
526  GNUNET_i2s (&ar->pc->peer));
527  pc->num_addresses++;
528  if (pc->num_addresses == pc->address_list_size)
529  dump_pc (ar->pc);
530 }
static int res
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_INFO
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
static void dump_pc(struct PrintContext *pc)
Print the collected address information to the console and free pc.
Record we keep for each printable address.
struct PrintContext * pc
Print context this address record belongs to.
struct GNUNET_TRANSPORT_AddressToStringContext * atsc
Current address-to-string context (if active, otherwise NULL).
char * result
Printable address.
struct GNUNET_PeerIdentity peer
Identity of the peer.

References _, address, AddressRecord::atsc, dump_pc(), GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_strdup, GNUNET_SYSERR, pc, AddressRecord::pc, PrintContext::peer, res, and AddressRecord::result.

Referenced by print_address().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_address()

static int print_address ( void *  cls,
const struct GNUNET_HELLO_Address address,
struct GNUNET_TIME_Absolute  expiration 
)
static

Iterator callback to go over all addresses.

Parameters
clsclosure
addressthe address
expirationexpiration time
Returns
GNUNET_OK to keep the address and continue

Definition at line 542 of file plugin_rest_peerinfo.c.

545 {
546  struct PrintContext *pc = cls;
547  struct AddressRecord *ar;
548 
549  if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
550  {
551  return GNUNET_OK; /* ignore expired address */
552  }
553 
554  GNUNET_assert (0 < pc->off);
555  ar = &pc->address_list[--pc->off];
556  ar->pc = pc;
557  ar->expiration = expiration;
558  GNUNET_asprintf (&ar->result,
559  "%s:%lu:%u",
560  address->transport_name,
561  (unsigned long) address->address_length,
562  address->local_info);
564  address,
565  GNUNET_NO,
566  TIMEOUT,
568  ar);
569  return GNUNET_OK;
570 }
@ GNUNET_NO
Definition: gnunet_common.h:94
struct GNUNET_TRANSPORT_AddressToStringContext * GNUNET_TRANSPORT_address_to_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_HELLO_Address *address, int numeric, struct GNUNET_TIME_Relative timeout, GNUNET_TRANSPORT_AddressToStringCallback aluc, void *aluc_cls)
Convert a binary address into a human readable address.
#define TIMEOUT
How long until we time out during address lookup?
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration handle.
static void process_resolved_address(void *cls, const char *address, int res)
Function to call with a human-readable format of an address.
struct GNUNET_TIME_Absolute expiration
Address expiration time.

References address, AddressRecord::atsc, cfg, expiration, AddressRecord::expiration, GNUNET_asprintf(), GNUNET_assert, GNUNET_NO, GNUNET_OK, GNUNET_TIME_absolute_get_remaining(), GNUNET_TRANSPORT_address_to_string(), pc, AddressRecord::pc, process_resolved_address(), AddressRecord::result, and TIMEOUT.

Referenced by peerinfo_list_iteration().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ peerinfo_list_iteration()

void peerinfo_list_iteration ( void *  cls,
const struct GNUNET_PeerIdentity peer,
const struct GNUNET_HELLO_Message hello,
const char *  err_msg 
)

Callback that processes each of the known HELLOs for the iteration response construction.

Parameters
clsclosure, NULL
peerid of the peer, NULL for last call
hellohello message for the peer (can be NULL)
err_msgmessage

Definition at line 583 of file plugin_rest_peerinfo.c.

587 {
588  struct RequestHandle *handle = cls;
589  struct PrintContext *pc;
590  int friend_only;
591 
592  if (NULL == handle->response)
593  {
594  handle->response = json_array ();
595  }
596 
597  if (NULL == peer)
598  {
599  handle->list_it = NULL;
600  handle->emsg = GNUNET_strdup ("Error in communication with peerinfo");
601  if (NULL != err_msg)
602  {
603  GNUNET_free (handle->emsg);
604  handle->emsg = GNUNET_strdup (err_msg);
605  handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
606  }
607  if (NULL == pc_head)
609  return;
610  }
611  if (NULL == hello)
612  return;
613 
615  if (NULL != hello)
617 
618  pc = GNUNET_new (struct PrintContext);
620  pc_tail,
621  pc);
622  pc->peer = *peer;
623  pc->friend_only = friend_only;
624  pc->handle = handle;
626  GNUNET_NO,
627  &count_address,
628  pc);
629  if (0 == pc->off)
630  {
631  dump_pc (pc);
632  return;
633  }
634  pc->address_list_size = pc->off;
635  pc->address_list = GNUNET_malloc (
636  sizeof(struct AddressRecord) * pc->off);
638  GNUNET_NO,
639  &print_address,
640  pc);
641 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
int GNUNET_HELLO_is_friend_only(const struct GNUNET_HELLO_Message *h)
Return HELLO type.
Definition: hello.c:89
struct GNUNET_HELLO_Message * GNUNET_HELLO_iterate_addresses(const struct GNUNET_HELLO_Message *msg, int return_modified, GNUNET_HELLO_AddressIterator it, void *it_cls)
Iterate over all of the addresses in the HELLO.
Definition: hello.c:254
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
static int count_address(void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
Iterator callback to go over all addresses and count them.
static int print_address(void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
Iterator callback to go over all addresses.
int friend_only
Hello was friend only, GNUNET_YES or GNUNET_NO.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.

References count_address(), do_error(), dump_pc(), PrintContext::friend_only, GNUNET_CONTAINER_DLL_insert, GNUNET_free, GNUNET_HELLO_is_friend_only(), GNUNET_HELLO_iterate_addresses(), GNUNET_malloc, GNUNET_new, GNUNET_NO, GNUNET_SCHEDULER_add_now(), GNUNET_strdup, handle, pc, pc_head, pc_tail, peer, and print_address().

Referenced by peerinfo_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ peerinfo_get()

void peerinfo_get ( struct GNUNET_REST_RequestHandle con_handle,
const char *  url,
void *  cls 
)

Handle peerinfo GET request.

Parameters
con_handlethe connection handle
urlthe url
clsthe RequestHandle

Definition at line 652 of file plugin_rest_peerinfo.c.

655 {
656  struct RequestHandle *handle = cls;
657  struct GNUNET_HashCode key;
658  const struct GNUNET_PeerIdentity *specific_peer;
659  // GNUNET_PEER_Id peer_id;
661  char*include_friend_only_str;
662 
666  &key);
667  if (GNUNET_YES
669  &key))
670  {
671  include_friend_only_str = GNUNET_CONTAINER_multihashmap_get (
672  con_handle->url_param_map, &key);
673  if (0 == strcmp (include_friend_only_str, "yes"))
674  {
676  }
677  }
678 
679  specific_peer = NULL;
681  strlen (GNUNET_REST_PEERINFO_PEER),
682  &key);
683  if (GNUNET_YES
685  &key))
686  {
687  // peer_id = *(unsigned int*)GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key);
688  // specific_peer = GNUNET_PEER_resolve2(peer_id);
689  }
690 
693  specific_peer,
695  handle);
696 }
struct GNUNET_HashCode key
The key used in the DHT.
static int include_friend_only
Option '-f'.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
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_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.
struct GNUNET_PEERINFO_IteratorContext * GNUNET_PEERINFO_iterate(struct GNUNET_PEERINFO_Handle *h, int include_friend_only, const struct GNUNET_PeerIdentity *peer, GNUNET_PEERINFO_Processor callback, void *callback_cls)
Call a method for each known matching host.
Definition: peerinfo_api.c:487
void peerinfo_list_iteration(void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HELLO_Message *hello, const char *err_msg)
Callback that processes each of the known HELLOs for the iteration response construction.
#define GNUNET_REST_PEERINFO_FRIEND
Peerinfo parameter friend.
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CONTAINER_MultiHashMap * url_param_map
Map of url parameters.

References GNUNET_CONTAINER_multihashmap_contains(), GNUNET_CONTAINER_multihashmap_get(), GNUNET_CRYPTO_hash(), GNUNET_NO, GNUNET_PEERINFO_iterate(), GNUNET_REST_PEERINFO_FRIEND, GNUNET_REST_PEERINFO_PEER, GNUNET_YES, handle, include_friend_only, key, peerinfo_handle, peerinfo_list_iteration(), and GNUNET_REST_RequestHandle::url_param_map.

Here is the call graph for this function:

◆ options_cont()

static void options_cont ( struct GNUNET_REST_RequestHandle con_handle,
const char *  url,
void *  cls 
)
static

Respond to OPTIONS request.

Parameters
con_handlethe connection handle
urlthe url
clsthe RequestHandle

Definition at line 707 of file plugin_rest_peerinfo.c.

710 {
711  struct MHD_Response *resp;
712  struct RequestHandle *handle = cls;
713 
714  // independent of path return all options
715  resp = GNUNET_REST_create_response (NULL);
716  MHD_add_response_header (resp,
717  "Access-Control-Allow-Methods",
718  allow_methods);
719  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
721  return;
722 }
static char * allow_methods
HTTP methods allows for this plugin.

References allow_methods, cleanup_handle(), GNUNET_REST_create_response(), GNUNET_SCHEDULER_add_now(), and handle.

Here is the call graph for this function:

◆ rest_process_request()

static enum GNUNET_GenericReturnValue rest_process_request ( struct GNUNET_REST_RequestHandle rest_handle,
GNUNET_REST_ResultProcessor  proc,
void *  proc_cls 
)
static

Function processing the REST call.

Parameters
methodHTTP method
urlURL of the HTTP request
databody of the HTTP request (optional)
data_sizelength of the body
proccallback function for the result
proc_clsclosure for callback function
Returns
GNUNET_OK if request accepted

Definition at line 707 of file plugin_rest_peerinfo.c.

740 {
741  struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
743  static const struct GNUNET_REST_RequestHandler handlers[] = {
744  { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_PEERINFO, &peerinfo_get },
745  { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_PEERINFO, &options_cont },
747  };
748 
749  handle->response_code = 0;
751  60);
752  handle->proc_cls = proc_cls;
753  handle->proc = proc;
754  handle->rest_handle = rest_handle;
755 
756  handle->url = GNUNET_strdup (rest_handle->url);
757  if (handle->url[strlen (handle->url) - 1] == '/')
758  handle->url[strlen (handle->url) - 1] = '\0';
759  handle->timeout_task =
761  &do_error,
762  handle);
765  handle);
766  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
767  if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle,
768  handlers,
769  &err,
770  handle))
771  {
773  return GNUNET_NO;
774  }
775  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
776  return GNUNET_YES;
777 }
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
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:77
#define GNUNET_REST_HANDLER_END
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:1269
#define GNUNET_TIME_UNIT_SECONDS
One second.
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:311
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Respond to OPTIONS request.
#define GNUNET_REST_API_NS_PEERINFO
Peerinfo Namespace.
void peerinfo_get(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle peerinfo GET request.
const char * url
The url as string.
void(* proc)(struct GNUNET_REST_RequestHandle *handle, const char *url, void *cls)
Namespace to handle.

Referenced by libgnunet_plugin_rest_peerinfo_init().

Here is the caller graph for this function:

◆ libgnunet_plugin_rest_peerinfo_init()

void* libgnunet_plugin_rest_peerinfo_init ( void *  cls)

Entry point for the plugin.

Parameters
clsConfig info
Returns
NULL on error, otherwise the plugin context

Definition at line 787 of file plugin_rest_peerinfo.c.

788 {
789  static struct Plugin plugin;
790  struct GNUNET_REST_Plugin *api;
791 
792  cfg = cls;
793  if (NULL != plugin.cfg)
794  return NULL; /* can only initialize once! */
795  memset (&plugin, 0, sizeof(struct Plugin));
796  plugin.cfg = cfg;
797  api = GNUNET_new (struct GNUNET_REST_Plugin);
798  api->cls = &plugin;
802  "%s, %s, %s, %s, %s",
803  MHD_HTTP_METHOD_GET,
804  MHD_HTTP_METHOD_POST,
805  MHD_HTTP_METHOD_PUT,
806  MHD_HTTP_METHOD_DELETE,
807  MHD_HTTP_METHOD_OPTIONS);
809 
811  _ ("Peerinfo REST API initialized\n"));
812  return api;
813 }
struct Plugin * plugin
The process handle to the testbed service.
struct GNUNET_PEERINFO_Handle * GNUNET_PEERINFO_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the peerinfo service.
Definition: peerinfo_api.c:130
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.
struct returned by the initialization function of the plugin
char * name
Plugin name.
void * cls
The closure of the plugin.
enum GNUNET_GenericReturnValue(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
Handle for a plugin.
Definition: block.c:38
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.

References _, allow_methods, Plugin::cfg, cfg, GNUNET_REST_Plugin::cls, GNUNET_asprintf(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_new, GNUNET_PEERINFO_connect(), GNUNET_REST_API_NS_PEERINFO, GNUNET_REST_Plugin::name, peerinfo_handle, plugin, GNUNET_REST_Plugin::process_request, and rest_process_request().

Here is the call graph for this function:

◆ libgnunet_plugin_rest_peerinfo_done()

void* libgnunet_plugin_rest_peerinfo_done ( void *  cls)

Exit point from the plugin.

Parameters
clsthe plugin context (as returned by "init")
Returns
always NULL

Definition at line 823 of file plugin_rest_peerinfo.c.

824 {
825  struct GNUNET_REST_Plugin *api = cls;
826  struct Plugin *plugin = api->cls;
827 
828  plugin->cfg = NULL;
829  while (NULL != requests_head)
831  if (NULL != peerinfo_handle)
833 
835  GNUNET_free (api);
837  "Peerinfo REST plugin is finished\n");
838  return NULL;
839 }
void * cls
Closure for all of the callbacks.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47

References allow_methods, Plugin::api, Plugin::cfg, cleanup_handle(), GNUNET_BLOCK_PluginFunctions::cls, GNUNET_REST_Plugin::cls, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_PEERINFO_disconnect(), peerinfo_handle, plugin, and requests_head.

Here is the call graph for this function:

Variable Documentation

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg

The configuration handle.

Handle to our configuration.

Definition at line 68 of file plugin_rest_peerinfo.c.

Referenced by libgnunet_plugin_rest_peerinfo_init(), and print_address().

◆ allow_methods

char* allow_methods
static

HTTP methods allows for this plugin.

Definition at line 73 of file plugin_rest_peerinfo.c.

Referenced by libgnunet_plugin_rest_peerinfo_done(), libgnunet_plugin_rest_peerinfo_init(), and options_cont().

◆ peerinfo_handle

struct GNUNET_PEERINFO_Handle* peerinfo_handle
static

◆ pc_head

struct PrintContext* pc_head
static

Head of list of print contexts.

Definition at line 170 of file plugin_rest_peerinfo.c.

Referenced by dump_pc(), and peerinfo_list_iteration().

◆ pc_tail

struct PrintContext* pc_tail
static

Tail of list of print contexts.

Definition at line 175 of file plugin_rest_peerinfo.c.

Referenced by dump_pc(), and peerinfo_list_iteration().

◆ requests_head

struct RequestHandle* requests_head
static

DLL.

Definition at line 267 of file plugin_rest_peerinfo.c.

Referenced by cleanup_handle(), and libgnunet_plugin_rest_peerinfo_done().

◆ requests_tail

struct RequestHandle* requests_tail
static

DLL.

Definition at line 272 of file plugin_rest_peerinfo.c.

Referenced by cleanup_handle().