GNUnet  0.10.x
gnunet-rest-server.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012-2015 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
27 #include <microhttpd.h>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_rest_plugin.h"
30 
31 
35 #define GNUNET_REST_SERVICE_PORT 7776
36 
41 #define MAX_HTTP_URI_LENGTH 2048
42 
46 #define HTTP_PORT 80
47 
51 #define HTTPS_PORT 443
52 
56 #define MHD_CACHE_TIMEOUT \
57  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
58 
59 #define GN_REST_STATE_INIT 0
60 #define GN_REST_STATE_PROCESSING 1
61 
66 
70 static in_addr_t address;
71 
75 static struct in6_addr address6;
76 
80 static unsigned long long port = GNUNET_REST_SERVICE_PORT;
81 
86 
91 
96 
101 
105 static struct MHD_Daemon *httpd;
106 
110 static struct MHD_Response *failure_response;
111 
115 static const struct GNUNET_CONFIGURATION_Handle *cfg;
116 
121 
125 static int echo_origin;
126 
130 static char *allow_origins;
131 
135 static char *allow_headers;
136 
140 static char *allow_credentials;
141 
146 {
147  struct MHD_Connection *con;
148 
149  struct MHD_Response *response;
150 
152 
154 
155  struct MHD_PostProcessor *pp;
156 
157  int status;
158 
159  int state;
160 };
161 
162 /* ************************* Global helpers ********************* */
163 
164 
170 static void
171 do_httpd (void *cls);
172 
173 
177 static void
179 {
180  if (NULL != httpd_task)
181  {
182  GNUNET_SCHEDULER_cancel (httpd_task);
183  httpd_task = NULL;
184  }
185  httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL);
186 }
187 
196 static void
197 plugin_callback (void *cls, struct MHD_Response *resp, int status)
198 {
199  struct MhdConnectionHandle *handle = cls;
200  handle->status = status;
201  handle->response = resp;
202  MHD_resume_connection (handle->con);
203  run_mhd_now ();
204 }
205 
206 
207 static int
208 cleanup_url_map (void *cls, const struct GNUNET_HashCode *key, void *value)
209 {
210  GNUNET_free_non_null (value);
211  return GNUNET_YES;
212 }
213 
214 
215 static void
217 {
218  if (NULL != handle->response)
219  MHD_destroy_response (handle->response);
220  if (NULL != handle->data_handle)
221  {
222 
223  if (NULL != handle->data_handle->header_param_map)
224  {
228  NULL);
230  handle->data_handle->header_param_map);
231  }
232  if (NULL != handle->data_handle->url_param_map)
233  {
236  NULL);
238  handle->data_handle->url_param_map);
239  }
240  GNUNET_free (handle->data_handle);
241  }
242  GNUNET_free (handle);
243 }
244 
245 static int
246 header_iterator (void *cls,
247  enum MHD_ValueKind kind,
248  const char *key,
249  const char *value)
250 {
251  struct GNUNET_REST_RequestHandle *handle = cls;
252  struct GNUNET_HashCode hkey;
253  char *val;
254  char *lowerkey;
255 
256  lowerkey = GNUNET_strdup (key);
257  GNUNET_STRINGS_utf8_tolower (key, lowerkey);
258  GNUNET_CRYPTO_hash (lowerkey, strlen (lowerkey), &hkey);
259  GNUNET_asprintf (&val, "%s", value);
261  handle->header_param_map,
262  &hkey,
263  val,
265  {
267  "Could not load add header `%s'=%s\n",
268  lowerkey,
269  value);
270  }
271  GNUNET_free (lowerkey);
272  return MHD_YES;
273 }
274 
275 
276 static int
277 url_iterator (void *cls,
278  enum MHD_ValueKind kind,
279  const char *key,
280  const char *value)
281 {
282  struct GNUNET_REST_RequestHandle *handle = cls;
283  struct GNUNET_HashCode hkey;
284  char *val;
285 
286  GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
287  GNUNET_asprintf (&val, "%s", value);
289  handle->url_param_map,
290  &hkey,
291  val,
293  {
295  "Could not load add url param `%s'=%s\n",
296  key,
297  value);
298  }
299  return MHD_YES;
300 }
301 
302 static int
303 post_data_iter (void *cls,
304  enum MHD_ValueKind kind,
305  const char *key,
306  const char *filename,
307  const char *content_type,
308  const char *transfer_encoding,
309  const char *data,
310  uint64_t off,
311  size_t size)
312 {
313  struct GNUNET_REST_RequestHandle *handle = cls;
314  struct GNUNET_HashCode hkey;
315  char *val;
316 
317  if (MHD_POSTDATA_KIND != kind)
318  return MHD_YES;
319 
320  GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
321  GNUNET_asprintf (&val, "%s", data);
323  handle->url_param_map,
324  &hkey,
325  val,
327  {
329  "Could not load add url param '%s'=%s\n",
330  key,
331  data);
332  GNUNET_free (val);
333  }
334  return MHD_YES;
335 }
336 
337 /* ********************************* MHD response generation ******************* */
338 
362 static int
363 create_response (void *cls,
364  struct MHD_Connection *con,
365  const char *url,
366  const char *meth,
367  const char *ver,
368  const char *upload_data,
369  size_t *upload_data_size,
370  void **con_cls)
371 {
372  char *plugin_name;
373  char *origin;
374  struct GNUNET_HashCode key;
375  struct MhdConnectionHandle *con_handle;
376  struct GNUNET_REST_RequestHandle *rest_conndata_handle;
377 
378  con_handle = *con_cls;
379 
380  if (NULL == *con_cls)
381  {
382  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url);
383  char tmp_url[strlen (url) + 1];
384  strcpy (tmp_url, url);
385  con_handle = GNUNET_new (struct MhdConnectionHandle);
386  con_handle->con = con;
387  con_handle->state = GN_REST_STATE_INIT;
388  *con_cls = con_handle;
389 
390  plugin_name = strtok (tmp_url, "/");
391 
392  if (NULL != plugin_name)
393  {
394  GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key);
395 
396  con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map, &key);
397  }
398  if (NULL == con_handle->plugin)
399  {
400  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing response with MHD\n");
401  GNUNET_free (con_handle);
402  return MHD_queue_response (con, MHD_HTTP_NOT_FOUND, failure_response);
403  }
404 
405  return MHD_YES;
406  }
407  if (GN_REST_STATE_INIT == con_handle->state)
408  {
409  rest_conndata_handle = GNUNET_new (struct GNUNET_REST_RequestHandle);
410  rest_conndata_handle->method = meth;
411  rest_conndata_handle->url = url;
412  rest_conndata_handle->data = upload_data;
413  rest_conndata_handle->data_size = *upload_data_size;
414  rest_conndata_handle->url_param_map =
416  rest_conndata_handle->header_param_map =
418  con_handle->data_handle = rest_conndata_handle;
419  MHD_get_connection_values (con,
420  MHD_GET_ARGUMENT_KIND,
421  (MHD_KeyValueIterator) &url_iterator,
422  rest_conndata_handle);
423  MHD_get_connection_values (con,
424  MHD_HEADER_KIND,
425  (MHD_KeyValueIterator) &header_iterator,
426  rest_conndata_handle);
427  con_handle->pp = MHD_create_post_processor (con,
428  65536,
430  rest_conndata_handle);
431  if (*upload_data_size)
432  {
433  MHD_post_process (con_handle->pp, upload_data, *upload_data_size);
434  }
435  MHD_destroy_post_processor (con_handle->pp);
436 
437  con_handle->state = GN_REST_STATE_PROCESSING;
438  con_handle->plugin->process_request (rest_conndata_handle,
440  con_handle);
441  *upload_data_size = 0;
442  run_mhd_now ();
443  return MHD_YES;
444  }
445  if (NULL == con_handle->response)
446  {
447  //Suspend connection until plugin is done
448  MHD_suspend_connection (con_handle->con);
449  return MHD_YES;
450  }
451  MHD_resume_connection (con_handle->con);
453  "Queueing response from plugin with MHD\n");
454  //Handle Preflights for extensions
455  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking origin\n");
456  GNUNET_CRYPTO_hash ("origin", strlen ("origin"), &key);
457  origin = GNUNET_CONTAINER_multihashmap_get (con_handle->data_handle
459  &key);
460  if (NULL != origin)
461  {
462  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Origin: %s\n", origin);
463  //Only echo for browser plugins
464  if (GNUNET_YES == echo_origin)
465  {
466  if ((0 ==
467  strncmp ("moz-extension://", origin, strlen ("moz-extension://"))) ||
468  (0 == strncmp ("chrome-extension://",
469  origin,
470  strlen ("chrome-extension://"))))
471  {
472  MHD_add_response_header (con_handle->response,
473  MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
474  origin);
475  }
476  }
477  if (NULL != allow_origins)
478  {
479  char *tmp = GNUNET_strdup (allow_origins);
480  char *allow_origin = strtok (tmp, ",");
481  while (NULL != allow_origin)
482  {
483  if (0 == strncmp (allow_origin, origin, strlen (allow_origin)))
484  {
485  MHD_add_response_header (con_handle->response,
486  MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
487  allow_origin);
488  break;
489  }
490  allow_origin = strtok (NULL, ",");
491  }
492  GNUNET_free (tmp);
493  }
494  }
495  if (NULL != allow_credentials)
496  {
497  MHD_add_response_header (con_handle->response,
498  "Access-Control-Allow-Credentials",
500  }
501  if (NULL != allow_headers)
502  {
503  MHD_add_response_header (con_handle->response,
504  "Access-Control-Allow-Headers",
505  allow_headers);
506  }
507  run_mhd_now ();
508  int ret = MHD_queue_response (con, con_handle->status, con_handle->response);
509  cleanup_handle (con_handle);
510  return ret;
511 }
512 
513 
514 /* ******************** MHD HTTP setup and event loop ******************** */
515 
525 static void
526 mhd_completed_cb (void *cls,
527  struct MHD_Connection *connection,
528  void **con_cls,
529  enum MHD_RequestTerminationCode toe)
530 {
531  if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
533  "MHD encountered error handling request: %d\n",
534  toe);
535 }
536 
537 
541 static void
543 {
544  if (NULL != httpd)
545  {
546  MHD_stop_daemon (httpd);
547  httpd = NULL;
548  }
549  if (NULL != httpd_task)
550  {
551  GNUNET_SCHEDULER_cancel (httpd_task);
552  httpd_task = NULL;
553  }
554  if (NULL != ltask4)
555  {
556  GNUNET_SCHEDULER_cancel (ltask4);
557  ltask4 = NULL;
558  }
559  if (NULL != ltask6)
560  {
561  GNUNET_SCHEDULER_cancel (ltask6);
562  ltask6 = NULL;
563  }
564 
565  if (NULL != lsock4)
566  {
568  lsock4 = NULL;
569  }
570  if (NULL != lsock6)
571  {
573  lsock6 = NULL;
574  }
575 }
576 
577 
585 static void
587 {
588  fd_set rs;
589  fd_set ws;
590  fd_set es;
591  struct GNUNET_NETWORK_FDSet *wrs;
592  struct GNUNET_NETWORK_FDSet *wws;
593  int max;
594  int haveto;
595  MHD_UNSIGNED_LONG_LONG timeout;
596  struct GNUNET_TIME_Relative tv;
597 
598  FD_ZERO (&rs);
599  FD_ZERO (&ws);
600  FD_ZERO (&es);
601  max = -1;
602  if (MHD_YES != MHD_get_fdset (httpd, &rs, &ws, &es, &max))
603  {
604  kill_httpd ();
605  return;
606  }
607  haveto = MHD_get_timeout (httpd, &timeout);
608  if (MHD_YES == haveto)
609  tv.rel_value_us = (uint64_t) timeout * 1000LL;
610  else
612  if (-1 != max)
613  {
616  GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
617  GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
618  }
619  else
620  {
621  wrs = NULL;
622  wws = NULL;
623  }
624  if (NULL != httpd_task)
625  {
626  GNUNET_SCHEDULER_cancel (httpd_task);
627  httpd_task = NULL;
628  }
629  if ((MHD_YES == haveto) || (-1 != max))
630  {
632  tv,
633  wrs,
634  wws,
635  &do_httpd,
636  NULL);
637  }
638  if (NULL != wrs)
640  if (NULL != wws)
642 }
643 
649 static void
650 do_httpd (void *cls)
651 {
652  httpd_task = NULL;
653  MHD_run (httpd);
654  schedule_httpd ();
655 }
656 
657 
664 static void
665 do_accept (void *cls)
666 {
667  struct GNUNET_NETWORK_Handle *lsock = cls;
668  struct GNUNET_NETWORK_Handle *s;
669  int fd;
670  const struct sockaddr *addr;
671  socklen_t len;
672 
673  GNUNET_assert (NULL != lsock);
674  if (lsock == lsock4)
675  {
677  lsock,
678  &do_accept,
679  lsock);
680  }
681  else if (lsock == lsock6)
682  {
684  lsock,
685  &do_accept,
686  lsock);
687  }
688  else
689  GNUNET_assert (0);
690  s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
691  if (NULL == s)
692  {
694  return;
695  }
697  "Got an inbound connection, waiting for data\n");
698  fd = GNUNET_NETWORK_get_fd (s);
699  addr = GNUNET_NETWORK_get_addr (s);
700  len = GNUNET_NETWORK_get_addrlen (s);
701  if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
702  {
704  _ ("Failed to pass client to MHD\n"));
705  return;
706  }
707  GNUNET_free (s);
708  schedule_httpd ();
709 }
710 
711 
717 static void
718 do_shutdown (void *cls)
719 {
720  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down...\n");
721  kill_httpd ();
724 }
725 
726 
732 static struct GNUNET_NETWORK_Handle *
734 {
735  struct GNUNET_NETWORK_Handle *ls;
736  struct sockaddr_in sa4;
737  int eno;
738 
739  memset (&sa4, 0, sizeof (sa4));
740  sa4.sin_family = AF_INET;
741  sa4.sin_port = htons (port);
742  sa4.sin_addr.s_addr = address;
743 #if HAVE_SOCKADDR_IN_SIN_LEN
744  sa4.sin_len = sizeof (sa4);
745 #endif
746  ls = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
747  if (NULL == ls)
748  return NULL;
750  (const struct sockaddr *) &sa4,
751  sizeof (sa4)))
752  {
753  eno = errno;
755  errno = eno;
756  return NULL;
757  }
758  return ls;
759 }
760 
761 
767 static struct GNUNET_NETWORK_Handle *
769 {
770  struct GNUNET_NETWORK_Handle *ls;
771  struct sockaddr_in6 sa6;
772  int eno;
773 
774  memset (&sa6, 0, sizeof (sa6));
775  sa6.sin6_family = AF_INET6;
776  sa6.sin6_port = htons (port);
777  sa6.sin6_addr = address6;
778 #if HAVE_SOCKADDR_IN_SIN_LEN
779  sa6.sin6_len = sizeof (sa6);
780 #endif
781  ls = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
782  if (NULL == ls)
783  return NULL;
785  (const struct sockaddr *) &sa6,
786  sizeof (sa6)))
787  {
788  eno = errno;
790  errno = eno;
791  return NULL;
792  }
793  return ls;
794 }
795 
796 
804 static void
805 load_plugin (void *cls, const char *libname, void *lib_ret)
806 {
807  struct GNUNET_REST_Plugin *plugin = lib_ret;
808  struct GNUNET_HashCode key;
809  if (NULL == lib_ret)
810  {
812  "Could not load plugin `%s'\n",
813  libname);
814  return;
815  }
816  GNUNET_assert (1 < strlen (plugin->name));
817  GNUNET_assert ('/' == *plugin->name);
818  GNUNET_CRYPTO_hash (plugin->name + 1, strlen (plugin->name + 1), &key);
820  plugin_map,
821  &key,
822  plugin,
824  {
826  "Could not load add plugin `%s'\n",
827  libname);
828  return;
829  }
830  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded plugin `%s'\n", libname);
831 }
832 
833 
842 static void
843 run (void *cls,
844  char *const *args,
845  const char *cfgfile,
846  const struct GNUNET_CONFIGURATION_Handle *c)
847 {
848  char *addr_str;
849  cfg = c;
851 
852  /* Get port to bind to */
853  if (GNUNET_OK !=
854  GNUNET_CONFIGURATION_get_value_number (cfg, "rest", "HTTP_PORT", &port))
855  {
856  //No address specified
857  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Using default port...\n");
859  }
860 
861  /* Get address to bind to */
862  if (GNUNET_OK !=
863  GNUNET_CONFIGURATION_get_value_string (cfg, "rest", "BIND_TO", &addr_str))
864  {
865  //No address specified
866  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind to...\n");
868  return;
869  }
870  if (1 != inet_pton (AF_INET, addr_str, &address))
871  {
873  "Unable to parse address %s\n",
874  addr_str);
875  GNUNET_free (addr_str);
877  return;
878  }
879  GNUNET_free (addr_str);
880  /* Get address to bind to */
882  "rest",
883  "BIND_TO6",
884  &addr_str))
885  {
886  //No address specified
887  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind6 to...\n");
889  return;
890  }
891  if (1 != inet_pton (AF_INET6, addr_str, &address6))
892  {
894  "Unable to parse IPv6 address %s\n",
895  addr_str);
896  GNUNET_free (addr_str);
898  return;
899  }
900  GNUNET_free (addr_str);
901 
902 
903  /* Get CORS data from cfg */
904  echo_origin =
906  "rest",
907  "REST_ECHO_ORIGIN_WEBEXT");
908  allow_origins = NULL;
910  "rest",
911  "REST_ALLOW_ORIGIN",
912  &allow_origins))
913  {
915  "No CORS Access-Control-Allow-Origin header will be sent...\n");
916  }
917  if (GNUNET_OK !=
919  "rest",
920  "REST_ALLOW_CREDENTIALS",
922  {
923  //No origin specified
925  "No CORS Credential Header will be sent...\n");
926  }
927 
929  "rest",
930  "REST_ALLOW_HEADERS",
931  &allow_headers))
932  {
933  //No origin specified
935  "No CORS Access-Control-Allow-Headers Header will be sent...\n");
936  }
937 
938  /* Open listen socket proxy */
939  lsock6 = bind_v6 ();
940  if (NULL == lsock6)
941  {
943  }
944  else
945  {
946  if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
947  {
950  lsock6 = NULL;
951  }
952  else
953  {
955  lsock6,
956  &do_accept,
957  lsock6);
958  }
959  }
960  lsock4 = bind_v4 ();
961  if (NULL == lsock4)
962  {
964  }
965  else
966  {
967  if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
968  {
971  lsock4 = NULL;
972  }
973  else
974  {
976  lsock4,
977  &do_accept,
978  lsock4);
979  }
980  }
981  if ((NULL == lsock4) && (NULL == lsock6))
982  {
984  return;
985  }
986  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service listens on port %llu\n", port);
987  httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET |
988  MHD_ALLOW_SUSPEND_RESUME,
989  0,
990  NULL,
991  NULL,
993  NULL,
994  MHD_OPTION_CONNECTION_TIMEOUT,
995  (unsigned int) 16,
996  MHD_OPTION_NOTIFY_COMPLETED,
998  NULL,
999  MHD_OPTION_END);
1000  if (NULL == httpd)
1001  {
1003  return;
1004  }
1005  /* Load plugins */
1006  GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
1007  (void *) cfg,
1008  &load_plugin,
1009  NULL);
1011 }
1012 
1013 
1023 int
1024 main (int argc, char *const *argv)
1025 {
1027  static const char *err_page = "{}";
1028  int ret;
1029 
1030  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1031  return 2;
1032  GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL);
1033  failure_response = MHD_create_response_from_buffer (strlen (err_page),
1034  (void *) err_page,
1035  MHD_RESPMEM_PERSISTENT);
1036  ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
1037  argv,
1038  "gnunet-rest-server",
1039  _ ("GNUnet REST server"),
1040  options,
1041  &run,
1042  NULL))
1043  ? 0
1044  : 1;
1045  MHD_destroy_response (failure_response);
1046  GNUNET_free_non_null ((char *) argv);
1047  return ret;
1048 }
1049 
1050 /* end of gnunet-rest-server.c */
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:796
static char * allow_headers
Allowed Headers (CORS)
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1268
struct sockaddr * GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc)
Return the sockaddr for this network handle.
Definition: network.c:1281
static struct MHD_Response * failure_response
Response we return on failures.
static unsigned long long port
The port the service is running on (default 7776)
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
uint64_t rel_value_us
The actual value.
static void load_plugin(void *cls, const char *libname, void *lib_ret)
Callback for plugin load.
static struct GNUNET_SCHEDULER_Task * httpd_task
The task ID.
#define GNUNET_REST_SERVICE_PORT
Default Socks5 listen port.
socklen_t GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc)
Return sockaddr length for this network handle.
Definition: network.c:1294
size_t data_size
The POST data size.
MHD Connection handle.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1293
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void do_accept(void *cls)
Accept new incoming connections.
static struct in6_addr address6
The IPv6 address to bind to.
static void kill_httpd()
Kill the MHD daemon.
struct GNUNET_REST_RequestHandle * data_handle
int GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:522
struct GNUNET_REST_Plugin * plugin
struct returned by the initialization function of the plugin
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_HashMapIterator it, void *it_cls)
Iterate over all entries in the map.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Definition of a command line option.
void(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
static int 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.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
static int ret
Final status code.
Definition: gnunet-arm.c:89
static void cleanup_handle(struct MhdConnectionHandle *handle)
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
Internal representation of the hash map.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
void GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to, const fd_set *from, int nfds)
Copy a native fd set into the GNUnet representation.
Definition: network.c:1308
static int url_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:468
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.
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
Definition: network.c:1554
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create(void)
Creates an fd set.
Definition: network.c:1538
static int echo_origin
Echo request Origin in CORS.
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:114
static char * value
Value of the record to add/remove.
#define GN_REST_STATE_INIT
struct GNUNET_CONTAINER_MultiHashMap * url_param_map
Map of url parameters.
static struct GNUNET_NETWORK_Handle * lsock4
The listen socket of the service for IPv4.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
const char * url
The url as string.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static struct GNUNET_SCHEDULER_Task * ltask6
The listen task ID for IPv6.
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:1273
char * name
Plugin name.
static char * allow_origins
Allowed Origins (CORS)
collection of IO descriptors
const char * method
The HTTP method as MHD value (see microhttpd.h)
static char * filename
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio, struct GNUNET_TIME_Relative delay, const struct GNUNET_NETWORK_FDSet *rs, const struct GNUNET_NETWORK_FDSet *ws, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when any of the specified file descriptor set...
Definition: scheduler.c:1829
static int header_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static char * plugin_name
Solver plugin name as string.
static void do_shutdown(void *cls)
Task run on shutdown.
static in_addr_t address
The address to bind to.
A 512-bit hashcode.
static struct GNUNET_NETWORK_Handle * lsock6
The listen socket of the service for IPv6.
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
#define GN_REST_STATE_PROCESSING
const char * data
The POST data.
There must only be one value per key; storing a value should fail if a value under the same key alrea...
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:67
static struct GNUNET_SCHEDULER_Task * ltask4
The listen task ID for IPv4.
void GNUNET_PLUGIN_load_all(const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name.
Definition: plugin.c:375
static char * allow_credentials
Allowed Credentials (CORS)
struct MHD_Connection * con
static struct GNUNET_CONTAINER_MultiHashMap * plugin_map
Map of loaded plugins.
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
static struct GNUNET_NETWORK_Handle * ls
Listen socket for STUN processing.
Definition: gnunet-nat.c:85
struct MHD_PostProcessor * pp
static int cleanup_url_map(void *cls, const struct GNUNET_HashCode *key, void *value)
static struct GNUNET_NETWORK_Handle * bind_v6()
Create an IPv6 listen socket bound to our port.
configuration data
Definition: configuration.c:85
static void do_httpd(void *cls)
Task run whenever HTTP server operations are pending.
static void schedule_httpd()
Schedule MHD.
static int inet_pton(int af, const char *cp, struct in_addr *buf)
Convert IPv4 address from text to binary form.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
static struct MHD_Daemon * httpd
Daemon for HTTP.
handle to a socket
Definition: network.c:46
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:361
#define GNUNET_YES
Definition: gnunet_common.h:80
struct GNUNET_CONTAINER_MultiHashMap * header_param_map
Map of headers.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1478
Run with the default priority (normal P2P operations).
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
static void plugin_callback(void *cls, struct MHD_Response *resp, int status)
Plugin result callback.
uint32_t data
The data value.
struct MHD_Response * response
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void run_mhd_now()
Run MHD now, we have extra data ready for the callback.
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:604
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
#define GNUNET_free(ptr)
Wrapper around free.
static int 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)
Time for relative time used by GNUnet, in microseconds.
int main(int argc, char *const *argv)
The main function for gnunet-rest-service.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
void GNUNET_STRINGS_utf8_tolower(const char *input, char *output)
Convert the utf-8 input string to lower case.
Definition: strings.c:558
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:1037
static struct GNUNET_NETWORK_Handle * bind_v4()
Create an IPv4 listen socket bound to our port.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965