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.

Referenced by libgnunet_plugin_rest_peerinfo_init(), and rest_process_request().

◆ GNUNET_REST_PEERINFO_PEER

#define GNUNET_REST_PEERINFO_PEER   "peer"

Peerinfo parameter peer.

Definition at line 44 of file plugin_rest_peerinfo.c.

Referenced by dump_pc(), and peerinfo_get().

◆ GNUNET_REST_PEERINFO_FRIEND

#define GNUNET_REST_PEERINFO_FRIEND   "friend"

Peerinfo parameter friend.

Definition at line 49 of file plugin_rest_peerinfo.c.

Referenced by peerinfo_get().

◆ GNUNET_REST_PEERINFO_ARRAY

#define GNUNET_REST_PEERINFO_ARRAY   "array"

Peerinfo parameter array.

Definition at line 54 of file plugin_rest_peerinfo.c.

Referenced by dump_pc().

◆ 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.

Referenced by do_error().

◆ TIMEOUT

How long until we time out during address lookup?

Definition at line 64 of file plugin_rest_peerinfo.c.

Referenced by print_address().

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.

References RequestHandle::address, RequestHandle::emsg, RequestHandle::expiration_str, 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, RequestHandle::list_it, RequestHandle::pubkey, RequestHandle::response, RequestHandle::temp_array, RequestHandle::timeout_task, and RequestHandle::url.

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

280 {
281  struct RequestHandle *handle = cls;
282 
284  "Cleaning up\n");
285  if (NULL != handle->timeout_task)
286  {
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);
325  GNUNET_free (handle);
326 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static struct GNUNET_PEERINFO_Handle * peerinfo_handle
Handle to PEERINFO.
The request handle.
struct GNUNET_NAMESTORE_ZoneIterator * list_it
Handle to NAMESTORE it.
void GNUNET_PEERINFO_iterate_cancel(struct GNUNET_PEERINFO_IteratorContext *ic)
Cancel an iteration over peer information.
Definition: peerinfo_api.c:520
char * emsg
Error response message.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static struct RequestHandle * requests_head
DLL.
#define GNUNET_free_nz(ptr)
Wrapper around free.
json_t * response
JSON response.
static struct RequestHandle * requests_tail
DLL.
struct GNUNET_SCHEDULER_Task * timeout_task
ID of a task associated with the resolution process.
char * expiration_str
Expiration time string.
json_t * temp_array
JSON temporary array.
#define GNUNET_log(kind,...)
void GNUNET_PEERINFO_disconnect(struct GNUNET_PEERINFO_Handle *h)
Disconnect from the peerinfo service.
Definition: peerinfo_api.c:156
const char * address
Address string.
char * pubkey
Iteration peer public key.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
char * url
The url.
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.

References cleanup_handle(), RequestHandle::emsg, GNUNET_free, GNUNET_REST_create_response(), GNUNET_REST_PEERINFO_ERROR_UNKNOWN, GNUNET_SCHEDULER_add_now(), GNUNET_strdup, handle, RequestHandle::proc, RequestHandle::proc_cls, response, and RequestHandle::response_code.

Referenced by peerinfo_list_finished(), peerinfo_list_iteration(), and rest_process_request().

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);
350  resp = GNUNET_REST_create_response (response);
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);
354  GNUNET_free (response);
356 }
GNUNET_REST_ResultProcessor proc
The plugin result processor.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * proc_cls
The closure of the result processor.
The request handle.
char * emsg
Error response message.
#define GNUNET_REST_PEERINFO_ERROR_UNKNOWN
Error message Unknown Error.
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
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
int response_code
Response code.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
Definition: rest.c:57
static struct MHD_Response * response
Our canonical response.
#define GNUNET_free(ptr)
Wrapper around free.
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.

References cleanup_handle(), do_error(), RequestHandle::emsg, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_REST_create_response(), GNUNET_SCHEDULER_add_now(), GNUNET_strdup, handle, RequestHandle::proc, RequestHandle::proc_cls, RequestHandle::response, and RequestHandle::response_code.

Referenced by dump_pc().

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  MHD_add_response_header (resp, "Content-Type", "application/json");
383  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
384  GNUNET_free (result_str);
386 }
GNUNET_REST_ResultProcessor proc
The plugin result processor.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * proc_cls
The closure of the result processor.
The request handle.
char * emsg
Error response message.
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
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
int response_code
Response code.
json_t * response
JSON response.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
Definition: rest.c:57
#define GNUNET_log(kind,...)
#define GNUNET_free(ptr)
Wrapper around free.
static void do_error(void *cls)
Task run on errors.
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 398 of file plugin_rest_peerinfo.c.

References GNUNET_OK, GNUNET_TIME_absolute_get_remaining(), PrintContext::off, and pc.

Referenced by peerinfo_list_iteration().

401 {
402  struct PrintContext *pc = cls;
403 
404  if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
405  {
406  return GNUNET_OK; /* ignore expired address */
407  }
408 
409  pc->off++;
410  return GNUNET_OK;
411 }
Structure we use to collect printable address information.
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
unsigned int off
Current offset in address_list (counted down).
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:331
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 420 of file plugin_rest_peerinfo.c.

References address, PrintContext::address_list, AddressRecord::expiration, PrintContext::friend_only, 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, PrintContext::handle, RequestHandle::list_it, PrintContext::num_addresses, PrintContext::peer, peerinfo_list_finished(), RequestHandle::response, AddressRecord::result, and RequestHandle::temp_array.

Referenced by peerinfo_list_iteration(), and process_resolved_address().

421 {
422  struct RequestHandle *handle;
423  unsigned int i;
424  json_t *response_entry;
425  json_t *temp_array;
426  json_t *object;
427  json_t *address;
428  json_t *expires;
429  json_t *friend_and_peer_json;
430  char *friend_and_peer;
431 
432  temp_array = json_array ();
433  response_entry = json_object ();
434 
435  for (i = 0; i < pc->num_addresses; i++)
436  {
437  if (NULL != pc->address_list[i].result)
438  {
439  object = json_object ();
440  address = json_string (pc->address_list[i].result);
441  expires = json_string (
443  pc->address_list[i].expiration));
444  json_object_set (object, "address", address);
445  json_object_set (object, "expires", expires);
446 
447  json_decref (address);
448  json_decref (expires);
449 
450  json_array_append (temp_array, object);
451  json_decref (object);
453  }
454  }
455 
456  if (0 < json_array_size (temp_array))
457  {
458  GNUNET_asprintf (&friend_and_peer,
459  "%s%s",
460  (GNUNET_YES == pc->friend_only) ? "F2F:" : "",
461  GNUNET_i2s_full (&pc->peer));
462  friend_and_peer_json = json_string (friend_and_peer);
463  json_object_set (response_entry,
465  friend_and_peer_json);
466  json_object_set (response_entry,
468  temp_array);
469  json_array_append (pc->handle->response, response_entry);
470  json_decref (friend_and_peer_json);
471  GNUNET_free (friend_and_peer);
472  }
473 
474  json_decref (temp_array);
475  json_decref (response_entry);
476 
479  pc_tail,
480  pc);
481  handle = pc->handle;
482  GNUNET_free (pc);
483 
484  if ((NULL == pc_head) &&
485  (NULL == handle->list_it))
486  {
488  }
489 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_PeerIdentity peer
Identity of the peer.
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
The request handle.
unsigned int num_addresses
Number of completed addresses in address_list.
struct GNUNET_NAMESTORE_ZoneIterator * list_it
Handle to NAMESTORE it.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
static void peerinfo_list_finished(void *cls)
Function that assembles the response.
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
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
struct GNUNET_TIME_Absolute expiration
Address expiration time.
#define GNUNET_REST_PEERINFO_ARRAY
Peerinfo parameter array.
json_t * response
JSON response.
#define GNUNET_REST_PEERINFO_PEER
Peerinfo parameter peer.
char * result
Printable address.
static struct PrintContext * pc_head
Head of list of print contexts.
json_t * temp_array
JSON temporary array.
int friend_only
Hello was friend only, GNUNET_YES or GNUNET_NO.
struct AddressRecord * address_list
List of printable addresses.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:757
static char * address
GNS address for this phone.
struct RequestHandle * handle
RequestHandle.
static struct PrintContext * pc_tail
Tail of list of print contexts.
#define GNUNET_free(ptr)
Wrapper around free.
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 503 of file plugin_rest_peerinfo.c.

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

Referenced by print_address().

506 {
507  struct AddressRecord *ar = cls;
508  struct PrintContext *pc = ar->pc;
509 
510  if (NULL != address)
511  {
512  if (0 != strlen (address))
513  {
514  if (NULL != ar->result)
515  GNUNET_free (ar->result);
516  ar->result = GNUNET_strdup (address);
517  }
518  return;
519  }
520  ar->atsc = NULL;
521  if (GNUNET_SYSERR == res)
523  _ ("Failure: Cannot convert address to string for peer `%s'\n"),
524  GNUNET_i2s (&ar->pc->peer));
525  pc->num_addresses++;
526  if (pc->num_addresses == pc->address_list_size)
527  dump_pc (ar->pc);
528 }
struct GNUNET_PeerIdentity peer
Identity of the peer.
struct PrintContext * pc
Print context this address record belongs to.
Structure we use to collect printable address information.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
unsigned int num_addresses
Number of completed addresses in address_list.
Record we keep for each printable address.
static int res
struct GNUNET_TRANSPORT_AddressToStringContext * atsc
Current address-to-string context (if active, otherwise NULL).
char * result
Printable address.
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
#define GNUNET_log(kind,...)
static void dump_pc(struct PrintContext *pc)
Print the collected address information to the console and free pc.
unsigned int address_list_size
Number of addresses allocated in address_list.
static char * address
GNS address for this phone.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_free(ptr)
Wrapper around free.
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 540 of file plugin_rest_peerinfo.c.

References GNUNET_HELLO_Address::address_length, PrintContext::address_list, AddressRecord::atsc, AddressRecord::expiration, expiration, GNUNET_asprintf(), GNUNET_assert, GNUNET_NO, GNUNET_OK, GNUNET_TIME_absolute_get_remaining(), GNUNET_TRANSPORT_address_to_string(), GNUNET_HELLO_Address::local_info, PrintContext::off, pc, AddressRecord::pc, process_resolved_address(), AddressRecord::result, TIMEOUT, and GNUNET_HELLO_Address::transport_name.

Referenced by peerinfo_list_iteration().

543 {
544  struct PrintContext *pc = cls;
545  struct AddressRecord *ar;
546 
547  if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
548  {
549  return GNUNET_OK; /* ignore expired address */
550  }
551 
552  GNUNET_assert (0 < pc->off);
553  ar = &pc->address_list[--pc->off];
554  ar->pc = pc;
555  ar->expiration = expiration;
556  GNUNET_asprintf (&ar->result,
557  "%s:%u:%u",
558  address->transport_name,
559  address->address_length,
560  address->local_info);
562  address,
563  GNUNET_NO,
564  TIMEOUT,
566  ar);
567  return GNUNET_OK;
568 }
size_t address_length
Number of bytes in address.
enum GNUNET_HELLO_AddressInfo local_info
Extended information about address.
struct PrintContext * pc
Print context this address record belongs to.
Structure we use to collect printable address information.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
Record we keep for each printable address.
const char * transport_name
Name of the transport plugin enabling the communication using this address.
static char * expiration
Credential TTL.
Definition: gnunet-abd.c:96
struct GNUNET_TIME_Absolute expiration
Address expiration time.
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_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.
struct GNUNET_TRANSPORT_AddressToStringContext * atsc
Current address-to-string context (if active, otherwise NULL).
char * result
Printable address.
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
unsigned int off
Current offset in address_list (counted down).
#define TIMEOUT
How long until we time out during address lookup?
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:331
struct AddressRecord * address_list
List of printable addresses.
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration handle.
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 581 of file plugin_rest_peerinfo.c.

References PrintContext::address_list, PrintContext::address_list_size, count_address(), do_error(), dump_pc(), RequestHandle::emsg, 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, PrintContext::handle, RequestHandle::list_it, PrintContext::off, pc, PrintContext::peer, peer, print_address(), RequestHandle::response, and RequestHandle::response_code.

Referenced by peerinfo_get().

585 {
586  struct RequestHandle *handle = cls;
587  struct PrintContext *pc;
588  int friend_only;
589 
590  if (NULL == handle->response)
591  {
592  handle->response = json_array ();
593  }
594 
595  if (NULL == peer)
596  {
597  handle->list_it = NULL;
598  handle->emsg = GNUNET_strdup ("Error in communication with peerinfo");
599  if (NULL != err_msg)
600  {
601  GNUNET_free (handle->emsg);
602  handle->emsg = GNUNET_strdup (err_msg);
603  handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
604  }
605  if (NULL == pc_head)
607  return;
608  }
609  if (NULL == hello)
610  return;
611 
612  friend_only = GNUNET_NO;
613  if (NULL != hello)
614  friend_only = GNUNET_HELLO_is_friend_only (hello);
615 
616  pc = GNUNET_new (struct PrintContext);
618  pc_tail,
619  pc);
620  pc->peer = *peer;
621  pc->friend_only = friend_only;
622  pc->handle = handle;
624  GNUNET_NO,
625  &count_address,
626  pc);
627  if (0 == pc->off)
628  {
629  dump_pc (pc);
630  return;
631  }
632  pc->address_list_size = pc->off;
634  sizeof(struct AddressRecord) * pc->off);
636  GNUNET_NO,
637  &print_address,
638  pc);
639 }
struct GNUNET_PeerIdentity peer
Identity of the peer.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
Structure we use to collect printable address information.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
The request handle.
struct GNUNET_NAMESTORE_ZoneIterator * list_it
Handle to NAMESTORE it.
Record we keep for each printable address.
static int print_address(void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
Iterator callback to go over all addresses.
char * emsg
Error response message.
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
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
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
int response_code
Response code.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
json_t * response
JSON response.
int GNUNET_HELLO_is_friend_only(const struct GNUNET_HELLO_Message *h)
Return HELLO type.
Definition: hello.c:89
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
static struct PrintContext * pc_head
Head of list of print contexts.
unsigned int off
Current offset in address_list (counted down).
int friend_only
Hello was friend only, GNUNET_YES or GNUNET_NO.
struct AddressRecord * address_list
List of printable addresses.
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 void dump_pc(struct PrintContext *pc)
Print the collected address information to the console and free pc.
unsigned int address_list_size
Number of addresses allocated in address_list.
struct RequestHandle * handle
RequestHandle.
#define GNUNET_malloc(size)
Wrapper around malloc.
static struct PrintContext * pc_tail
Tail of list of print contexts.
#define GNUNET_free(ptr)
Wrapper around free.
static void do_error(void *cls)
Task run on errors.
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 650 of file plugin_rest_peerinfo.c.

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, RequestHandle::list_it, peerinfo_list_iteration(), and GNUNET_REST_RequestHandle::url_param_map.

Referenced by rest_process_request().

653 {
654  struct RequestHandle *handle = cls;
655  struct GNUNET_HashCode key;
656  const struct GNUNET_PeerIdentity *specific_peer;
657  // GNUNET_PEER_Id peer_id;
659  char*include_friend_only_str;
660 
661  include_friend_only = GNUNET_NO;
664  &key);
665  if (GNUNET_YES
667  &key))
668  {
669  include_friend_only_str = GNUNET_CONTAINER_multihashmap_get (
670  con_handle->url_param_map, &key);
671  if (0 == strcmp (include_friend_only_str, "yes"))
672  {
673  include_friend_only = GNUNET_YES;
674  }
675  }
676 
677  specific_peer = NULL;
679  strlen (GNUNET_REST_PEERINFO_PEER),
680  &key);
681  if (GNUNET_YES
683  &key))
684  {
685  // peer_id = *(unsigned int*)GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key);
686  // specific_peer = GNUNET_PEER_resolve2(peer_id);
687  }
688 
690  include_friend_only,
691  specific_peer,
693  handle);
694 }
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.
static int include_friend_only
Option &#39;-f&#39;.
static struct GNUNET_PEERINFO_Handle * peerinfo_handle
Handle to PEERINFO.
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)...
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.
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
struct GNUNET_NAMESTORE_ZoneIterator * list_it
Handle to NAMESTORE it.
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:48
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
A 512-bit hashcode.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_REST_PEERINFO_PEER
Peerinfo parameter peer.
The identity of the host (wraps the signing key of the peer).
#define GNUNET_REST_PEERINFO_FRIEND
Peerinfo parameter friend.
Here is the call graph for this function:
Here is the caller 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 705 of file plugin_rest_peerinfo.c.

References allow_methods, cleanup_handle(), GNUNET_REST_create_response(), GNUNET_SCHEDULER_add_now(), handle, RequestHandle::proc, and RequestHandle::proc_cls.

Referenced by rest_process_request().

708 {
709  struct MHD_Response *resp;
710  struct RequestHandle *handle = cls;
711 
712  // independent of path return all options
713  resp = GNUNET_REST_create_response (NULL);
714  MHD_add_response_header (resp,
715  "Access-Control-Allow-Methods",
716  allow_methods);
717  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
719  return;
720 }
static char * allow_methods
HTTP methods allows for this plugin.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
void * proc_cls
The closure of the result processor.
The request handle.
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
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
Definition: rest.c:57
Here is the call graph for this function:
Here is the caller 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 735 of file plugin_rest_peerinfo.c.

References cleanup_handle(), do_error(), GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_new, GNUNET_NO, GNUNET_REST_API_NS_PEERINFO, GNUNET_REST_handle_request(), GNUNET_REST_HANDLER_END, GNUNET_SCHEDULER_add_delayed(), GNUNET_strdup, GNUNET_TIME_relative_multiply(), GNUNET_TIME_UNIT_SECONDS, GNUNET_YES, handle, options_cont(), peerinfo_get(), GNUNET_REST_RequestHandler::proc, RequestHandle::proc, RequestHandle::proc_cls, RequestHandle::response_code, RequestHandle::rest_handle, RequestHandle::timeout, RequestHandle::timeout_task, GNUNET_REST_RequestHandle::url, and RequestHandle::url.

Referenced by libgnunet_plugin_rest_peerinfo_init().

738 {
739  struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
741  static const struct GNUNET_REST_RequestHandler handlers[] = {
742  { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_PEERINFO, &peerinfo_get },
743  { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_PEERINFO, &options_cont },
745  };
746 
747  handle->response_code = 0;
749  60);
750  handle->proc_cls = proc_cls;
751  handle->proc = proc;
752  handle->rest_handle = rest_handle;
753 
754  handle->url = GNUNET_strdup (rest_handle->url);
755  if (handle->url[strlen (handle->url) - 1] == '/')
756  handle->url[strlen (handle->url) - 1] = '\0';
757  handle->timeout_task =
759  &do_error,
760  handle);
763  handle);
764  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
766  handlers,
767  &err,
768  handle))
769  {
770  cleanup_handle (handle);
771  return GNUNET_NO;
772  }
773  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
774  return GNUNET_YES;
775 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
#define GNUNET_TIME_UNIT_SECONDS
One second.
void peerinfo_get(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle peerinfo 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.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * proc_cls
The closure of the result processor.
#define GNUNET_REST_HANDLER_END
The request handle.
#define GNUNET_REST_API_NS_PEERINFO
Peerinfo Namespace.
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
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
const char * url
The url as string.
struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
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:442
static struct RequestHandle * requests_head
DLL.
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
struct GNUNET_REST_RequestHandle * rest_handle
Rest connection.
int response_code
Response code.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
static struct RequestHandle * requests_tail
DLL.
struct GNUNET_SCHEDULER_Task * timeout_task
ID of a task associated with the resolution process.
#define GNUNET_log(kind,...)
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Respond to OPTIONS request.
static void do_error(void *cls)
Task run on errors.
char * url
The url.
Here is the call graph for this function:
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 785 of file plugin_rest_peerinfo.c.

References _, allow_methods, Plugin::api, Plugin::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, plugin, GNUNET_REST_Plugin::process_request, and rest_process_request().

786 {
787  static struct Plugin plugin;
788  struct GNUNET_REST_Plugin *api;
789 
790  cfg = cls;
791  if (NULL != plugin.cfg)
792  return NULL; /* can only initialize once! */
793  memset (&plugin, 0, sizeof(struct Plugin));
794  plugin.cfg = cfg;
795  api = GNUNET_new (struct GNUNET_REST_Plugin);
796  api->cls = &plugin;
800  "%s, %s, %s, %s, %s",
801  MHD_HTTP_METHOD_GET,
802  MHD_HTTP_METHOD_POST,
803  MHD_HTTP_METHOD_PUT,
804  MHD_HTTP_METHOD_DELETE,
805  MHD_HTTP_METHOD_OPTIONS);
807 
809  _ ("Peerinfo REST API initialized\n"));
810  return api;
811 }
static char * allow_methods
HTTP methods allows for this plugin.
struct GNUNET_PEERINFO_Handle * GNUNET_PEERINFO_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the peerinfo service.
Definition: peerinfo_api.c:130
void * cls
The closure of the plugin.
struct returned by the initialization function of the plugin
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static struct GNUNET_PEERINFO_Handle * peerinfo_handle
Handle to PEERINFO.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
#define GNUNET_REST_API_NS_PEERINFO
Peerinfo Namespace.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
char * name
Plugin name.
static char * plugin
Solver plugin name as string.
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.
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
enum GNUNET_GenericReturnValue(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration handle.
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 821 of file plugin_rest_peerinfo.c.

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

822 {
823  struct GNUNET_REST_Plugin *api = cls;
824  struct Plugin *plugin = api->cls;
825 
826  plugin->cfg = NULL;
827  while (NULL != requests_head)
829  if (NULL != peerinfo_handle)
831 
833  GNUNET_free (api);
835  "Peerinfo REST plugin is finished\n");
836  return NULL;
837 }
static char * allow_methods
HTTP methods allows for this plugin.
void * cls
The closure of the plugin.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct returned by the initialization function of the plugin
static struct GNUNET_PEERINFO_Handle * peerinfo_handle
Handle to PEERINFO.
static struct RequestHandle * requests_head
DLL.
static char * plugin
Solver plugin name as string.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
void GNUNET_PEERINFO_disconnect(struct GNUNET_PEERINFO_Handle *h)
Disconnect from the peerinfo service.
Definition: peerinfo_api.c:156
#define GNUNET_free(ptr)
Wrapper around free.
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.

◆ 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

Handle to PEERINFO.

Definition at line 78 of file plugin_rest_peerinfo.c.

◆ pc_head

struct PrintContext* pc_head
static

Head of list of print contexts.

Definition at line 170 of file plugin_rest_peerinfo.c.

◆ pc_tail

struct PrintContext* pc_tail
static

Tail of list of print contexts.

Definition at line 175 of file plugin_rest_peerinfo.c.

◆ requests_head

struct RequestHandle* requests_head
static

DLL.

Definition at line 267 of file plugin_rest_peerinfo.c.

◆ requests_tail

struct RequestHandle* requests_tail
static

DLL.

Definition at line 272 of file plugin_rest_peerinfo.c.