![]() |
GNUnet
0.11.x
|
REST service for GNUnet services. More...
#include "platform.h"
#include <microhttpd.h>
#include "gnunet_util_lib.h"
#include "gnunet_rest_plugin.h"
#include "gnunet_mhd_compat.h"
Go to the source code of this file.
Data Structures | |
struct | PluginListEntry |
A plugin list entry. More... | |
struct | MhdConnectionHandle |
MHD Connection handle. More... | |
struct | AcceptedRequest |
Accepted requests. More... | |
Macros | |
#define | GNUNET_REST_SERVICE_PORT 7776 |
Default Socks5 listen port. More... | |
#define | MAX_HTTP_URI_LENGTH 2048 |
Maximum supported length for a URI. More... | |
#define | HTTP_PORT 80 |
Port for plaintext HTTP. More... | |
#define | HTTPS_PORT 443 |
Port for HTTPS. More... | |
#define | MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) |
After how long do we clean up unused MHD SSL/TLS instances? More... | |
#define | GN_REST_STATE_INIT 0 |
#define | GN_REST_STATE_PROCESSING 1 |
Functions | |
static void | do_httpd (void *cls) |
Task run whenever HTTP server operations are pending. More... | |
static void | run_mhd_now () |
Run MHD now, we have extra data ready for the callback. More... | |
static void | plugin_callback (void *cls, struct MHD_Response *resp, int status) |
Plugin result callback. More... | |
static int | cleanup_url_map (void *cls, const struct GNUNET_HashCode *key, void *value) |
static void | cleanup_handle (struct MhdConnectionHandle *handle) |
static void | cleanup_ar (struct AcceptedRequest *ar) |
static int | header_iterator (void *cls, enum MHD_ValueKind kind, const char *key, const char *value) |
static int | url_iterator (void *cls, enum MHD_ValueKind kind, const char *key, const char *value) |
static MHD_RESULT | post_data_iter (void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) |
static MHD_RESULT | create_response (void *cls, struct MHD_Connection *con, const char *url, const char *meth, const char *ver, const char *upload_data, size_t *upload_data_size, void **con_cls) |
Main MHD callback for handling requests. More... | |
static void | kill_httpd () |
Kill the MHD daemon. More... | |
static void | schedule_httpd () |
Schedule MHD. More... | |
static void * | mhd_log_callback (void *cls, const char *url, struct MHD_Connection *connection) |
Function called when MHD first processes an incoming connection. More... | |
static void | mhd_completed_cb (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe) |
Function called when MHD decides that we are done with a connection. More... | |
static void | mhd_connection_cb (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_ConnectionNotificationCode cnc) |
Function called when MHD connection is opened or closed. More... | |
static void | do_accept (void *cls) |
Accept new incoming connections. More... | |
static void | do_shutdown (void *cls) |
Task run on shutdown. More... | |
static struct GNUNET_NETWORK_Handle * | bind_v4 () |
Create an IPv4 listen socket bound to our port. More... | |
static struct GNUNET_NETWORK_Handle * | bind_v6 () |
Create an IPv6 listen socket bound to our port. More... | |
static void | load_plugin (void *cls, const char *libname, void *lib_ret) |
Callback for plugin load. More... | |
static void | run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) |
Main function that will be run. More... | |
int | main (int argc, char *const *argv) |
The main function for gnunet-rest-service. More... | |
Variables | |
static struct GNUNET_SCHEDULER_Task * | httpd_task |
The task ID. More... | |
static in_addr_t | address |
The address to bind to. More... | |
static struct in6_addr | address6 |
The IPv6 address to bind to. More... | |
static unsigned long long | port = 7776 |
The port the service is running on (default 7776) More... | |
static struct GNUNET_NETWORK_Handle * | lsock4 |
The listen socket of the service for IPv4. More... | |
static struct GNUNET_NETWORK_Handle * | lsock6 |
The listen socket of the service for IPv6. More... | |
static struct GNUNET_SCHEDULER_Task * | ltask4 |
The listen task ID for IPv4. More... | |
static struct GNUNET_SCHEDULER_Task * | ltask6 |
The listen task ID for IPv6. More... | |
static struct MHD_Daemon * | httpd |
Daemon for HTTP. More... | |
static struct MHD_Response * | failure_response |
Response we return on failures. More... | |
static const struct GNUNET_CONFIGURATION_Handle * | cfg |
Our configuration. More... | |
static int | echo_origin |
Echo request Origin in CORS. More... | |
static char * | allow_origins |
Allowed Origins (CORS) More... | |
static char * | allow_headers |
Allowed Headers (CORS) More... | |
static char * | allow_credentials |
Allowed Credentials (CORS) More... | |
static struct PluginListEntry * | plugins_head |
Plugin list head. More... | |
static struct PluginListEntry * | plugins_tail |
Plugin list tail. More... | |
static struct AcceptedRequest * | req_list_head |
AcceptedRequest list head. More... | |
static struct AcceptedRequest * | req_list_tail |
AcceptedRequest list tail. More... | |
REST service for GNUnet services.
Definition in file gnunet-rest-server.c.
#define GNUNET_REST_SERVICE_PORT 7776 |
Default Socks5 listen port.
Definition at line 35 of file gnunet-rest-server.c.
Referenced by run().
#define MAX_HTTP_URI_LENGTH 2048 |
#define HTTP_PORT 80 |
Port for plaintext HTTP.
Definition at line 46 of file gnunet-rest-server.c.
#define HTTPS_PORT 443 |
Port for HTTPS.
Definition at line 51 of file gnunet-rest-server.c.
#define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) |
After how long do we clean up unused MHD SSL/TLS instances?
Definition at line 56 of file gnunet-rest-server.c.
#define GN_REST_STATE_INIT 0 |
Definition at line 59 of file gnunet-rest-server.c.
Referenced by create_response().
#define GN_REST_STATE_PROCESSING 1 |
Definition at line 60 of file gnunet-rest-server.c.
Referenced by create_response().
|
static |
Task run whenever HTTP server operations are pending.
cls | NULL |
Definition at line 855 of file gnunet-rest-server.c.
References httpd, and schedule_httpd().
Referenced by run_mhd_now(), and schedule_httpd().
|
static |
Run MHD now, we have extra data ready for the callback.
Definition at line 244 of file gnunet-rest-server.c.
References do_httpd(), GNUNET_SCHEDULER_add_now(), and GNUNET_SCHEDULER_cancel().
Referenced by create_response(), and plugin_callback().
|
static |
Plugin result callback.
cls | closure (MHD connection handle) |
data | the data to return to the caller |
len | length of the data |
status | GNUNET_OK if successful |
Definition at line 264 of file gnunet-rest-server.c.
References MhdConnectionHandle::con, handle, MhdConnectionHandle::response, run_mhd_now(), status, and MhdConnectionHandle::status.
Referenced by create_response().
|
static |
Definition at line 276 of file gnunet-rest-server.c.
References GNUNET_free, and GNUNET_YES.
Referenced by cleanup_handle().
|
static |
Definition at line 283 of file gnunet-rest-server.c.
References cleanup_url_map(), MhdConnectionHandle::data_handle, GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_free, GNUNET_REST_RequestHandle::header_param_map, MhdConnectionHandle::response, and GNUNET_REST_RequestHandle::url_param_map.
Referenced by cleanup_ar(), and mhd_completed_cb().
|
static |
Definition at line 312 of file gnunet-rest-server.c.
References cleanup_handle(), AcceptedRequest::con_handle, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_free_memory_only_(), GNUNET_YES, AcceptedRequest::sock, and AcceptedRequest::socket_with_mhd.
Referenced by mhd_connection_cb().
|
static |
Definition at line 332 of file gnunet-rest-server.c.
References GNUNET_asprintf(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_strdup, GNUNET_STRINGS_utf8_tolower(), handle, and GNUNET_REST_RequestHandle::header_param_map.
Referenced by create_response().
|
static |
Definition at line 363 of file gnunet-rest-server.c.
References GNUNET_asprintf(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, handle, MHD_RESULT, and GNUNET_REST_RequestHandle::url_param_map.
Referenced by create_response().
|
static |
Definition at line 390 of file gnunet-rest-server.c.
References GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_OK, handle, MHD_RESULT, and GNUNET_REST_RequestHandle::url_param_map.
Referenced by create_response().
|
static |
Main MHD callback for handling requests.
cls | unused |
con | MHD connection handle |
url | the url in the request |
meth | the HTTP method used ("GET", "PUT", etc.) |
ver | the HTTP version string (i.e. "HTTP/1.1") |
upload_data | the data being uploaded (excluding HEADERS, for a POST that fits into memory and that is encoded with a supported encoding, the POST data will NOT be given in upload_data and is instead available as part of MHD_get_connection_values; very large POST data will be made available incrementally in upload_data) |
upload_data_size | set initially to the size of the upload_data provided; the method must update this value to the number of bytes NOT processed; |
con_cls | pointer to location where we store the 'struct Request' |
Request not handled
Definition at line 456 of file gnunet-rest-server.c.
References allow_credentials, allow_headers, allow_origins, MhdConnectionHandle::con, AcceptedRequest::con_handle, GNUNET_REST_RequestHandle::data, MhdConnectionHandle::data_handle, GNUNET_REST_RequestHandle::data_size, echo_origin, failure_response, GN_REST_STATE_INIT, GN_REST_STATE_PROCESSING, GNUNET_break, GNUNET_CONTAINER_multihashmap_create(), GNUNET_CONTAINER_multihashmap_get(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_new, GNUNET_NO, GNUNET_strdup, GNUNET_YES, header_iterator(), GNUNET_REST_RequestHandle::header_param_map, GNUNET_REST_RequestHandle::method, MHD_RESULT, PluginListEntry::next, PluginListEntry::plugin, plugin_callback(), post_data_iter(), MhdConnectionHandle::pp, GNUNET_REST_Plugin::process_request, MhdConnectionHandle::response, ret, run_mhd_now(), MhdConnectionHandle::state, MhdConnectionHandle::status, GNUNET_REST_RequestHandle::url, url_iterator(), and GNUNET_REST_RequestHandle::url_param_map.
Referenced by run().
|
static |
Kill the MHD daemon.
Definition at line 617 of file gnunet-rest-server.c.
References GNUNET_NETWORK_socket_close(), GNUNET_SCHEDULER_cancel(), and httpd.
Referenced by do_shutdown(), and schedule_httpd().
|
static |
Schedule MHD.
This function should be called initially when an MHD is first getting its client socket, and will then automatically always be called later whenever there is work to be done.
hd | the daemon to schedule |
Definition at line 661 of file gnunet-rest-server.c.
References do_httpd(), GNUNET_NETWORK_fdset_copy_native(), GNUNET_NETWORK_fdset_create(), GNUNET_NETWORK_fdset_destroy(), GNUNET_SCHEDULER_add_select(), GNUNET_SCHEDULER_cancel(), GNUNET_SCHEDULER_PRIORITY_DEFAULT, GNUNET_TIME_UNIT_FOREVER_REL, httpd, kill_httpd(), GNUNET_TIME_Relative::rel_value_us, and timeout.
Referenced by do_accept(), and do_httpd().
|
static |
Function called when MHD first processes an incoming connection.
Gives us the respective URI information.
We use this to associate the struct MHD_Connection
with our internal struct AcceptedRequest
data structure (by checking for matching sockets).
cls | the HTTP server handle (a struct MhdHttpList ) |
url | the URL that is being requested |
connection | MHD connection object for the request |
struct Socks5Request
that this connection is for Definition at line 733 of file gnunet-rest-server.c.
References GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, and GNUNET_log.
Referenced by run().
|
static |
Function called when MHD decides that we are done with a connection.
cls | NULL |
connection | connection handle |
con_cls | value as set by the last call to the MHD_AccessHandlerCallback, should be our handle |
toe | reason for request termination (ignored) |
Definition at line 764 of file gnunet-rest-server.c.
References cleanup_handle(), AcceptedRequest::con_handle, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_YES, and AcceptedRequest::socket_with_mhd.
Referenced by run().
|
static |
Function called when MHD connection is opened or closed.
cls | NULL |
connection | connection handle |
con_cls | value as set by the last call to the MHD_AccessHandlerCallback, should be our struct Socks5Request * |
toe | connection notification type |
Definition at line 795 of file gnunet-rest-server.c.
References cleanup_ar(), GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_NETWORK_get_fd(), AcceptedRequest::next, and AcceptedRequest::sock.
Referenced by run().
|
static |
Accept new incoming connections.
cls | the closure with the lsock4 or lsock6 |
tc | the scheduler context |
Definition at line 870 of file gnunet-rest-server.c.
References _, GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_get_addr(), GNUNET_NETWORK_get_addrlen(), GNUNET_NETWORK_get_fd(), GNUNET_NETWORK_socket_accept(), GNUNET_NETWORK_socket_close(), GNUNET_new, GNUNET_SCHEDULER_add_read_net(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, httpd, len, schedule_httpd(), AcceptedRequest::sock, and AcceptedRequest::socket_with_mhd.
Referenced by run().
|
static |
Task run on shutdown.
cls | closure |
Definition at line 930 of file gnunet-rest-server.c.
References allow_credentials, allow_headers, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log, GNUNET_PLUGIN_unload(), kill_httpd(), PluginListEntry::libname, PluginListEntry::plugin, and plugins_head.
Referenced by run().
|
static |
Create an IPv4 listen socket bound to our port.
Definition at line 957 of file gnunet-rest-server.c.
References address, GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_OK, ls, and port.
Referenced by run().
|
static |
Create an IPv6 listen socket bound to our port.
Definition at line 992 of file gnunet-rest-server.c.
References address6, GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_OK, ls, and port.
Referenced by run().
|
static |
Callback for plugin load.
cls | NULL |
libname | the name of the library loaded |
lib_ret | the object returned by the plugin initializer |
Definition at line 1029 of file gnunet-rest-server.c.
References GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_new, GNUNET_strdup, PluginListEntry::libname, GNUNET_REST_Plugin::name, and PluginListEntry::plugin.
Referenced by run().
|
static |
Main function that will be run.
cls | closure |
args | remaining command-line arguments |
cfgfile | name of the configuration file used (for saving, can be NULL) |
c | configuration |
Definition at line 1062 of file gnunet-rest-server.c.
References address, address6, allow_credentials, allow_headers, allow_origins, bind_v4(), bind_v6(), create_response(), do_accept(), do_shutdown(), echo_origin, GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_listen(), GNUNET_OK, GNUNET_PLUGIN_load_all(), GNUNET_REST_SERVICE_PORT, GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_TIME_UNIT_FOREVER_REL, httpd, load_plugin(), mhd_completed_cb(), mhd_connection_cb(), mhd_log_callback(), and port.
Referenced by main().
int main | ( | int | argc, |
char *const * | argv | ||
) |
The main function for gnunet-rest-service.
argc | number of arguments from the cli |
argv | command line arguments |
Definition at line 1250 of file gnunet-rest-server.c.
References _, failure_response, GNUNET_free_nz, GNUNET_GETOPT_OPTION_END, GNUNET_log_setup(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), ret, and run().
|
static |
The task ID.
Definition at line 65 of file gnunet-rest-server.c.
|
static |
The address to bind to.
Definition at line 70 of file gnunet-rest-server.c.
|
static |
The IPv6 address to bind to.
Definition at line 75 of file gnunet-rest-server.c.
|
static |
The port the service is running on (default 7776)
Definition at line 80 of file gnunet-rest-server.c.
|
static |
The listen socket of the service for IPv4.
Definition at line 85 of file gnunet-rest-server.c.
|
static |
The listen socket of the service for IPv6.
Definition at line 90 of file gnunet-rest-server.c.
|
static |
The listen task ID for IPv4.
Definition at line 95 of file gnunet-rest-server.c.
|
static |
The listen task ID for IPv6.
Definition at line 100 of file gnunet-rest-server.c.
|
static |
Daemon for HTTP.
Definition at line 105 of file gnunet-rest-server.c.
Referenced by do_accept(), do_httpd(), kill_httpd(), run(), and schedule_httpd().
|
static |
Response we return on failures.
Definition at line 110 of file gnunet-rest-server.c.
Referenced by create_response(), and main().
|
static |
Our configuration.
Definition at line 115 of file gnunet-rest-server.c.
|
static |
Echo request Origin in CORS.
Definition at line 120 of file gnunet-rest-server.c.
Referenced by create_response(), and run().
|
static |
Allowed Origins (CORS)
Definition at line 125 of file gnunet-rest-server.c.
Referenced by create_response(), and run().
|
static |
Allowed Headers (CORS)
Definition at line 130 of file gnunet-rest-server.c.
Referenced by create_response(), do_shutdown(), and run().
|
static |
Allowed Credentials (CORS)
Definition at line 135 of file gnunet-rest-server.c.
Referenced by create_response(), do_shutdown(), and run().
|
static |
|
static |
Plugin list tail.
Definition at line 145 of file gnunet-rest-server.c.
|
static |
AcceptedRequest list head.
Definition at line 221 of file gnunet-rest-server.c.
|
static |
AcceptedRequest list tail.
Definition at line 226 of file gnunet-rest-server.c.