33 #include "microhttpd.h"
39 #define GNUNET_REST_API_NS_PEERINFO "/peerinfo"
44 #define GNUNET_REST_PEERINFO_PEER "peer"
49 #define GNUNET_REST_PEERINFO_FRIEND "friend"
54 #define GNUNET_REST_PEERINFO_ARRAY "array"
59 #define GNUNET_REST_PEERINFO_ERROR_UNKNOWN "Unknown Error"
64 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
285 if (NULL !=
handle->timeout_task)
288 handle->timeout_task = NULL;
294 if (NULL !=
handle->address)
296 if (NULL !=
handle->expiration_str)
298 if (NULL !=
handle->pubkey)
301 if (NULL !=
handle->temp_array)
303 json_decref (
handle->temp_array);
304 handle->temp_array = NULL;
306 if (NULL !=
handle->response)
308 json_decref (
handle->response);
312 if (NULL !=
handle->list_it)
338 struct MHD_Response *resp;
339 json_t *json_error = json_object ();
345 json_object_set_new (json_error,
"error", json_string (
handle->emsg));
347 if (0 ==
handle->response_code)
349 response = json_dumps (json_error, 0);
351 MHD_add_response_header (resp,
"Content-Type",
"application/json");
353 json_decref (json_error);
369 struct MHD_Response *resp;
371 if (NULL ==
handle->response)
379 result_str = json_dumps (
handle->response, 0);
384 "application/json"));
426 json_t *response_entry;
431 json_t *friend_and_peer_json;
432 char *friend_and_peer;
435 response_entry = json_object ();
437 for (i = 0; i <
pc->num_addresses; i++)
439 if (NULL !=
pc->address_list[i].result)
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);
450 json_decref (expires);
453 json_decref (
object);
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,
471 json_array_append (
pc->handle->response, response_entry);
472 json_decref (friend_and_peer_json);
477 json_decref (response_entry);
487 (NULL ==
handle->list_it))
525 _ (
"Failure: Cannot convert address to string for peer `%s'\n"),
528 if (
pc->num_addresses ==
pc->address_list_size)
555 ar = &
pc->address_list[--
pc->off];
561 (
unsigned long)
address->address_length,
592 if (NULL ==
handle->response)
594 handle->response = json_array ();
634 pc->address_list_size =
pc->off;
661 char*include_friend_only_str;
673 if (0 == strcmp (include_friend_only_str,
"yes"))
679 specific_peer = NULL;
711 struct MHD_Response *resp;
716 MHD_add_response_header (resp,
717 "Access-Control-Allow-Methods",
749 handle->response_code = 0;
752 handle->proc_cls = proc_cls;
754 handle->rest_handle = rest_handle;
802 "%s, %s, %s, %s, %s",
804 MHD_HTTP_METHOD_POST,
806 MHD_HTTP_METHOD_DELETE,
807 MHD_HTTP_METHOD_OPTIONS);
811 _ (
"Peerinfo REST API initialized\n"));
837 "Peerinfo REST plugin is finished\n");
static char * expiration
Credential TTL.
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
struct TestcasePlugin * plugin
The process handle to the testbed service.
static char * address
GNS address for this phone.
static struct MHD_Response * response
Our canonical response.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static int include_friend_only
Option '-f'.
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
functions to parse JSON objects into GNUnet objects
Maintain the list of currently known hosts.
API for helper library to parse/create REST.
GNUnet service REST plugin header.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
enum GNUNET_GenericReturnValue 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.
int GNUNET_HELLO_is_friend_only(const struct GNUNET_HELLO_Message *h)
Return HELLO type.
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.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
struct GNUNET_PEERINFO_Handle * GNUNET_PEERINFO_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the peerinfo service.
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.
void GNUNET_PEERINFO_iterate_cancel(struct GNUNET_PEERINFO_IteratorContext *ic)
Cancel an iteration over peer information.
void GNUNET_PEERINFO_disconnect(struct GNUNET_PEERINFO_Handle *h)
Disconnect from the peerinfo service.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
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_REST_ResultProcessor)(void *cls, struct MHD_Response *resp, int status)
Iterator called on obtained result for a REST result.
#define GNUNET_REST_HANDLER_END
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.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
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.
#define GNUNET_TIME_UNIT_SECONDS
One second.
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?
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
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.
@ MHD_HTTP_OK
OK [RFC7231, Section 6.3.1].
@ MHD_HTTP_NOT_FOUND
Not Found [RFC7231, Section 6.5.4].
@ MHD_HTTP_INTERNAL_SERVER_ERROR
Internal Server Error [RFC7231, Section 6.6.1].
static struct GNUNET_PEERINFO_Handle * peerinfo_handle
Handle to PEERINFO.
static struct PrintContext * pc_head
Head of list of print contexts.
#define GNUNET_REST_PEERINFO_PEER
Peerinfo parameter peer.
void * libgnunet_plugin_rest_peerinfo_done(void *cls)
Exit point from the plugin.
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 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.
static struct RequestHandle * requests_head
DLL.
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Respond to OPTIONS request.
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.
#define GNUNET_REST_PEERINFO_ARRAY
Peerinfo parameter array.
static struct PrintContext * pc_tail
Tail of list of print contexts.
static char * allow_methods
HTTP methods allows for this plugin.
void * libgnunet_plugin_rest_peerinfo_init(void *cls)
Entry point for the plugin.
#define GNUNET_REST_PEERINFO_FRIEND
Peerinfo parameter friend.
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 struct RequestHandle * requests_tail
DLL.
#define GNUNET_REST_API_NS_PEERINFO
Peerinfo Namespace.
#define GNUNET_REST_PEERINFO_ERROR_UNKNOWN
Error message Unknown Error.
void peerinfo_get(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle peerinfo GET request.
static void peerinfo_list_finished(void *cls)
Function that assembles the response.
static void dump_pc(struct PrintContext *pc)
Print the collected address information to the console and free pc.
static int print_address(void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
Iterator callback to go over all addresses.
static void do_error(void *cls)
Task run on errors.
static void cleanup_handle(void *cls)
Cleanup lookup handle.
Record we keep for each printable address.
struct GNUNET_TIME_Absolute expiration
Address expiration time.
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.
void * cls
Closure for all of the callbacks.
An address for communicating with a peer.
A HELLO message is used to exchange information about transports with other peers.
Handle to the peerinfo service.
Context for an iteration request.
The identity of the host (wraps the signing key of the peer).
struct returned by the initialization function of the plugin
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.
const char * url
The url as string.
struct GNUNET_CONTAINER_MultiHashMap * url_param_map
Map of url parameters.
void(* proc)(struct GNUNET_REST_RequestHandle *handle, const char *url, void *cls)
Namespace to handle.
Entry in list of pending tasks.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
Context for the address lookup.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Structure we use to collect printable address information.
struct AddressRecord * address_list
List of printable addresses.
struct RequestHandle * handle
RequestHandle.
unsigned int off
Current offset in address_list (counted down).
unsigned int num_addresses
Number of completed addresses in address_list.
unsigned int address_list_size
Number of addresses allocated in address_list.
struct GNUNET_PeerIdentity peer
Identity of the peer.
int friend_only
Hello was friend only, GNUNET_YES or GNUNET_NO.
struct PrintContext * next
Kept in DLL.
struct PrintContext * prev
Kept in DLL.
char * emsg
Error response message.
struct RequestHandle * prev
DLL.
int response_code
Response code.
struct GNUNET_SCHEDULER_Task * timeout_task
ID of a task associated with the resolution process.
void * proc_cls
The closure of the result processor.
char * pubkey
Iteration peer public key.
json_t * temp_array
JSON temporary array.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
struct RequestHandle * next
DLL.
struct GNUNET_REST_RequestHandle * rest_handle
Rest connection.
json_t * response
JSON response.
char * expiration_str
Expiration time string.
const char * address
Address string.
struct GNUNET_PEERINFO_IteratorContext * list_it
Handle to PEERINFO it.
struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.