#include "platform.h"
#include <microhttpd.h>
#include "gnunet_util_lib.h"
#include "gnunet_rest_plugin.h"
#include "gnunet_mhd_compat.h"
#include "config_plugin.h"
#include "copying_plugin.h"
#include "identity_plugin.h"
#include "namestore_plugin.h"
#include "gns_plugin.h"
#include "reclaim_plugin.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 enum GNUNET_GenericReturnValue | setup_plugin (const char *name, GNUNET_REST_ProcessingFunction proc, void *plugin_cls) |
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... | |
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 int | basic_auth_enabled |
Do basic auth of user. More... | |
static char * | basic_auth_secret |
Basic auth secret. More... | |
char | cuser [_POSIX_LOGIN_NAME_MAX] |
User of the service. 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... | |
struct GNUNET_REST_Plugin * | config_plugin |
plugins More... | |
struct GNUNET_REST_Plugin * | copying_plugin |
struct GNUNET_REST_Plugin * | identity_plugin |
struct GNUNET_REST_Plugin * | namestore_plugin |
struct GNUNET_REST_Plugin * | gns_plugin |
struct GNUNET_REST_Plugin * | reclaim_plugin |
#define GNUNET_REST_SERVICE_PORT 7776 |
Default Socks5 listen port.
Definition at line 45 of file gnunet-rest-server.c.
#define MAX_HTTP_URI_LENGTH 2048 |
#define HTTP_PORT 80 |
Port for plaintext HTTP.
Definition at line 56 of file gnunet-rest-server.c.
#define HTTPS_PORT 443 |
Port for HTTPS.
Definition at line 61 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 66 of file gnunet-rest-server.c.
#define GN_REST_STATE_INIT 0 |
Definition at line 69 of file gnunet-rest-server.c.
#define GN_REST_STATE_PROCESSING 1 |
Definition at line 70 of file gnunet-rest-server.c.
|
static |
Task run whenever HTTP server operations are pending.
cls | NULL |
Definition at line 932 of file gnunet-rest-server.c.
References httpd, httpd_task, 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 289 of file gnunet-rest-server.c.
References do_httpd(), GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_cancel(), and httpd_task.
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 309 of file gnunet-rest-server.c.
References handle, run_mhd_now(), and status.
Referenced by create_response().
|
static |
Definition at line 321 of file gnunet-rest-server.c.
References GNUNET_free, GNUNET_YES, and value.
Referenced by cleanup_handle().
|
static |
Definition at line 329 of file gnunet-rest-server.c.
References cleanup_url_map(), GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_free, and handle.
Referenced by cleanup_ar(), and mhd_completed_cb().
|
static |
Definition at line 359 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, req_list_head, req_list_tail, AcceptedRequest::sock, and AcceptedRequest::socket_with_mhd.
Referenced by mhd_connection_cb().
|
static |
Definition at line 382 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, key, and value.
Referenced by create_response().
|
static |
Definition at line 413 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, key, and value.
Referenced by create_response().
|
static |
Definition at line 440 of file gnunet-rest-server.c.
References data, 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, key, and size.
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 ("HTTP/1.1" for version 1.1, etc.) |
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 506 of file gnunet-rest-server.c.
References allow_credentials, allow_headers, allow_origins, basic_auth_enabled, basic_auth_secret, MhdConnectionHandle::con, AcceptedRequest::con_handle, cuser, 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_assert, GNUNET_break, GNUNET_CONTAINER_multihashmap_create(), GNUNET_CONTAINER_multihashmap_get(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_new, GNUNET_NO, GNUNET_strdup, GNUNET_YES, header_iterator(), GNUNET_REST_RequestHandle::header_param_map, key, GNUNET_REST_RequestHandle::method, MHD_RESULT, PluginListEntry::next, origin, PluginListEntry::plugin, plugin_callback(), plugins_head, post_data_iter(), MhdConnectionHandle::pp, PluginListEntry::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 694 of file gnunet-rest-server.c.
References GNUNET_NETWORK_socket_close(), GNUNET_SCHEDULER_cancel(), httpd, httpd_task, lsock4, lsock6, ltask4, and ltask6.
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 738 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, httpd_task, kill_httpd(), max, 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 811 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 841 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 873 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, req_list_head, and AcceptedRequest::sock.
Referenced by run().
|
static |
Accept new incoming connections.
cls | the closure with the lsock4 or lsock6 |
Definition at line 946 of file gnunet-rest-server.c.
References _, do_accept(), 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, lsock4, lsock6, ltask4, ltask6, req_list_head, req_list_tail, schedule_httpd(), AcceptedRequest::sock, and AcceptedRequest::socket_with_mhd.
Referenced by do_accept(), and run().
|
static |
Task run on shutdown.
cls | closure |
Definition at line 1006 of file gnunet-rest-server.c.
References allow_credentials, allow_headers, config_plugin, copying_plugin, failure_response, gns_plugin, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log, identity_plugin, kill_httpd(), PluginListEntry::libname, namestore_plugin, plugins_head, plugins_tail, reclaim_plugin, REST_config_done(), REST_copying_done(), REST_gns_done(), REST_identity_done(), REST_namestore_done(), REST_openid_done(), and REST_reclaim_done().
Referenced by run().
|
static |
Create an IPv4 listen socket bound to our port.
Definition at line 1042 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 1077 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 1114 of file gnunet-rest-server.c.
References GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_new, GNUNET_OK, GNUNET_strdup, GNUNET_SYSERR, PluginListEntry::libname, name, PluginListEntry::plugin, plugins_head, plugins_tail, and PluginListEntry::process_request.
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 1149 of file gnunet-rest-server.c.
References address, address6, allow_credentials, allow_headers, allow_origins, basic_auth_enabled, basic_auth_secret, bind_v4(), bind_v6(), cfg, config_plugin, copying_plugin, create_response(), cuser, do_accept(), do_shutdown(), echo_origin, failure_response, gns_plugin, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_DISK_file_test(), GNUNET_DISK_fn_read(), GNUNET_DISK_fn_write(), GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_strerror, GNUNET_log_strerror_file, GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_listen(), GNUNET_OK, GNUNET_REST_SERVICE_PORT, GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_strdup, GNUNET_STRINGS_data_to_string_alloc(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, httpd, identity_plugin, lsock4, lsock6, ltask4, ltask6, mhd_completed_cb(), mhd_connection_cb(), mhd_log_callback(), GNUNET_REST_Plugin::name, namestore_plugin, plugins_head, plugins_tail, port, reclaim_plugin, REST_config_init(), REST_config_process_request(), REST_copying_init(), REST_copying_process_request(), REST_gns_init(), REST_gns_process_request(), REST_identity_init(), REST_identity_process_request(), REST_namestore_init(), REST_namestore_process_request(), REST_openid_init(), REST_openid_process_request(), REST_reclaim_init(), REST_reclaim_process_request(), and setup_plugin().
|
static |
The task ID.
Definition at line 75 of file gnunet-rest-server.c.
Referenced by do_httpd(), kill_httpd(), run_mhd_now(), and schedule_httpd().
|
static |
The address to bind to.
Definition at line 80 of file gnunet-rest-server.c.
|
static |
The IPv6 address to bind to.
Definition at line 85 of file gnunet-rest-server.c.
|
static |
The port the service is running on (default 7776)
Definition at line 90 of file gnunet-rest-server.c.
|
static |
The listen socket of the service for IPv4.
Definition at line 95 of file gnunet-rest-server.c.
Referenced by do_accept(), kill_httpd(), and run().
|
static |
The listen socket of the service for IPv6.
Definition at line 100 of file gnunet-rest-server.c.
Referenced by do_accept(), kill_httpd(), and run().
|
static |
The listen task ID for IPv4.
Definition at line 105 of file gnunet-rest-server.c.
Referenced by do_accept(), kill_httpd(), and run().
|
static |
The listen task ID for IPv6.
Definition at line 110 of file gnunet-rest-server.c.
Referenced by do_accept(), kill_httpd(), and run().
|
static |
Daemon for HTTP.
Definition at line 115 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 120 of file gnunet-rest-server.c.
Referenced by create_response(), do_shutdown(), and run().
|
static |
|
static |
Echo request Origin in CORS.
Definition at line 130 of file gnunet-rest-server.c.
Referenced by create_response(), and run().
|
static |
Do basic auth of user.
Definition at line 135 of file gnunet-rest-server.c.
Referenced by create_response(), and run().
|
static |
Basic auth secret.
Definition at line 140 of file gnunet-rest-server.c.
Referenced by create_response(), and run().
char cuser[_POSIX_LOGIN_NAME_MAX] |
User of the service.
Definition at line 145 of file gnunet-rest-server.c.
Referenced by create_response(), and run().
|
static |
Allowed Origins (CORS)
Definition at line 150 of file gnunet-rest-server.c.
Referenced by create_response(), and run().
|
static |
Allowed Headers (CORS)
Definition at line 155 of file gnunet-rest-server.c.
Referenced by create_response(), do_shutdown(), and run().
|
static |
Allowed Credentials (CORS)
Definition at line 160 of file gnunet-rest-server.c.
Referenced by create_response(), do_shutdown(), and run().
|
static |
Plugin list head.
Definition at line 165 of file gnunet-rest-server.c.
Referenced by create_response(), do_shutdown(), run(), and setup_plugin().
|
static |
Plugin list tail.
Definition at line 170 of file gnunet-rest-server.c.
Referenced by do_shutdown(), run(), and setup_plugin().
|
static |
AcceptedRequest list head.
Definition at line 251 of file gnunet-rest-server.c.
Referenced by cleanup_ar(), do_accept(), and mhd_connection_cb().
|
static |
AcceptedRequest list tail.
Definition at line 256 of file gnunet-rest-server.c.
Referenced by cleanup_ar(), and do_accept().
struct GNUNET_REST_Plugin* config_plugin |
plugins
Definition at line 263 of file gnunet-rest-server.c.
Referenced by do_shutdown(), and run().
struct GNUNET_REST_Plugin* copying_plugin |
Definition at line 264 of file gnunet-rest-server.c.
Referenced by do_shutdown(), and run().
struct GNUNET_REST_Plugin* identity_plugin |
Definition at line 265 of file gnunet-rest-server.c.
Referenced by do_shutdown(), and run().
struct GNUNET_REST_Plugin* namestore_plugin |
Definition at line 266 of file gnunet-rest-server.c.
Referenced by do_shutdown(), and run().
struct GNUNET_REST_Plugin* gns_plugin |
Definition at line 267 of file gnunet-rest-server.c.
Referenced by do_shutdown(), and run().
struct GNUNET_REST_Plugin* reclaim_plugin |
Definition at line 271 of file gnunet-rest-server.c.
Referenced by do_shutdown(), and run().