705fcbeb39bb25bb42ab4c50cd8e96dea9c0fbb2
[oweals/gnunet.git] / src / rest / gnunet-rest-server.c
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
6    it under the terms of the GNU General Public License as published
7    by the Free Software Foundation; either version 3, or (at your
8    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    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with GNUnet; see the file COPYING.  If not, write to the
17    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19    */
20 /**
21  * @author Martin Schanzenbach
22  * @file src/rest/gnunet-rest-server.c
23  * @brief REST service for GNUnet services
24  *
25  */
26 #include "platform.h"
27 #include <microhttpd.h>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_rest_plugin.h"
30
31
32 /**
33  * Default Socks5 listen port.
34  */
35 #define GNUNET_REST_SERVICE_PORT 7776
36
37 /**
38  * Maximum supported length for a URI.
39  * Should die. @deprecated
40  */
41 #define MAX_HTTP_URI_LENGTH 2048
42
43 /**
44  * Port for plaintext HTTP.
45  */
46 #define HTTP_PORT 80
47
48 /**
49  * Port for HTTPS.
50  */
51 #define HTTPS_PORT 443
52
53 /**
54  * After how long do we clean up unused MHD SSL/TLS instances?
55  */
56 #define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
57
58 #define GN_REST_STATE_INIT 0
59 #define GN_REST_STATE_PROCESSING 1
60
61 /**
62  * The task ID
63  */
64 static struct GNUNET_SCHEDULER_Task * httpd_task;
65
66 /**
67  * The port the service is running on (default 7776)
68  */
69 static unsigned long port = GNUNET_REST_SERVICE_PORT;
70
71 /**
72  * The listen socket of the service for IPv4
73  */
74 static struct GNUNET_NETWORK_Handle *lsock4;
75
76 /**
77  * The listen socket of the service for IPv6
78  */
79 static struct GNUNET_NETWORK_Handle *lsock6;
80
81 /**
82  * The listen task ID for IPv4
83  */
84 static struct GNUNET_SCHEDULER_Task * ltask4;
85
86 /**
87  * The listen task ID for IPv6
88  */
89 static struct GNUNET_SCHEDULER_Task * ltask6;
90
91 /**
92  * Daemon for HTTP
93  */
94 static struct MHD_Daemon *httpd;
95
96 /**
97  * Response we return on failures.
98  */
99 static struct MHD_Response *failure_response;
100
101 /**
102  * Our configuration.
103  */
104 static const struct GNUNET_CONFIGURATION_Handle *cfg;
105
106 /**
107  * Map of loaded plugins.
108  */
109 static struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
110
111 /**
112  * Allowed Origins (CORS)
113  */
114 static char* allow_origin;
115
116 /**
117  * Allowed Headers (CORS)
118  */
119 static char* allow_headers;
120
121 /**
122  * MHD Connection handle
123  */
124 struct MhdConnectionHandle
125 {
126   struct MHD_Connection *con;
127
128   struct MHD_Response *response;
129
130   struct GNUNET_REST_Plugin *plugin;
131
132   struct RestConnectionDataHandle *data_handle;
133
134   int status;
135
136   int state;
137 };
138
139 /* ************************* Global helpers ********************* */
140
141
142 /**
143  * Task run whenever HTTP server operations are pending.
144  *
145  * @param cls NULL
146  */
147 static void
148 do_httpd (void *cls);
149
150
151 /**
152  * Run MHD now, we have extra data ready for the callback.
153  */
154 static void
155 run_mhd_now ()
156 {
157   if (NULL !=
158       httpd_task)
159     GNUNET_SCHEDULER_cancel (httpd_task);
160   httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
161                                          NULL);
162 }
163
164 /**
165  * Plugin result callback
166  *
167  * @param cls closure (MHD connection handle)
168  * @param data the data to return to the caller
169  * @param len length of the data
170  * @param status #GNUNET_OK if successful
171  */
172 static void
173 plugin_callback (void *cls,
174                  struct MHD_Response *resp,
175                  int status)
176 {
177   struct MhdConnectionHandle *handle = cls;
178   handle->status = status;
179   handle->response = resp;
180   run_mhd_now();
181 }
182
183
184 static int
185 cleanup_url_map (void *cls,
186                  const struct GNUNET_HashCode *key,
187                  void *value)
188 {
189   GNUNET_free_non_null (value);
190   return GNUNET_YES;
191 }
192
193
194 static void
195 cleanup_handle (struct MhdConnectionHandle *handle)
196 {
197   if (NULL != handle->response)
198     MHD_destroy_response (handle->response);
199   if (NULL != handle->data_handle)
200   {
201     if (NULL != handle->data_handle->url_param_map)
202     {
203       GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle->url_param_map,
204                                              &cleanup_url_map,
205                                              NULL);
206       GNUNET_CONTAINER_multihashmap_destroy (handle->data_handle->url_param_map);
207     }
208     GNUNET_free (handle->data_handle);
209   }
210   GNUNET_free (handle);
211 }
212
213
214 static int
215 url_iterator (void *cls,
216               enum MHD_ValueKind kind,
217               const char *key,
218               const char *value)
219 {
220   struct RestConnectionDataHandle *handle = cls;
221   struct GNUNET_HashCode hkey;
222   char *val;
223
224   GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
225   GNUNET_asprintf (&val, "%s", value);
226   if (GNUNET_OK !=
227       GNUNET_CONTAINER_multihashmap_put (handle->url_param_map,
228                                          &hkey,
229                                          val,
230                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233                 "Could not load add url param `%s'=%s\n",
234                 key, value);
235   }
236   return MHD_YES;
237 }
238
239 /* ********************************* MHD response generation ******************* */
240
241 /**
242  * Main MHD callback for handling requests.
243  *
244  * @param cls unused
245  * @param con MHD connection handle
246  * @param url the url in the request
247  * @param meth the HTTP method used ("GET", "PUT", etc.)
248  * @param ver the HTTP version string (i.e. "HTTP/1.1")
249  * @param upload_data the data being uploaded (excluding HEADERS,
250  *        for a POST that fits into memory and that is encoded
251  *        with a supported encoding, the POST data will NOT be
252  *        given in upload_data and is instead available as
253  *        part of MHD_get_connection_values; very large POST
254  *        data *will* be made available incrementally in
255  *        upload_data)
256  * @param upload_data_size set initially to the size of the
257  *        @a upload_data provided; the method must update this
258  *        value to the number of bytes NOT processed;
259  * @param con_cls pointer to location where we store the 'struct Request'
260  * @return MHD_YES if the connection was handled successfully,
261  *         MHD_NO if the socket must be closed due to a serious
262  *         error while handling the request
263  */
264 static int
265 create_response (void *cls,
266                  struct MHD_Connection *con,
267                  const char *url,
268                  const char *meth,
269                  const char *ver,
270                  const char *upload_data,
271                  size_t *upload_data_size,
272                  void **con_cls)
273 {
274   char *plugin_name;
275   struct GNUNET_HashCode key;
276   struct MhdConnectionHandle *con_handle;
277   struct RestConnectionDataHandle *rest_conndata_handle;
278
279   con_handle = *con_cls;
280
281   if (NULL == *con_cls)
282   {
283     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284                 "New connection %s\n", url);
285     char tmp_url[strlen(url)+1];
286     strcpy (tmp_url, url);
287     con_handle = GNUNET_new (struct MhdConnectionHandle);
288     con_handle->con = con;
289     con_handle->state = GN_REST_STATE_INIT;
290     *con_cls = con_handle;
291
292     plugin_name = strtok(tmp_url, "/");
293
294     if (NULL != plugin_name)
295     {
296       GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key);
297
298       con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map,
299                                                               &key);
300     }
301     if (NULL == con_handle->plugin)
302     {
303       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304                   "Queueing response with MHD\n");
305       GNUNET_free (con_handle);
306       return MHD_queue_response (con,
307                                  MHD_HTTP_NOT_FOUND,
308                                  failure_response);
309     }
310     return MHD_YES;
311   }
312   if (GN_REST_STATE_INIT == con_handle->state)
313   {
314     rest_conndata_handle = GNUNET_new (struct RestConnectionDataHandle);
315     rest_conndata_handle->method = meth;
316     rest_conndata_handle->url = url;
317     rest_conndata_handle->data = upload_data;
318     rest_conndata_handle->data_size = *upload_data_size;
319     rest_conndata_handle->url_param_map = GNUNET_CONTAINER_multihashmap_create (16,
320                                                                                 GNUNET_NO);
321     con_handle->data_handle = rest_conndata_handle;
322     MHD_get_connection_values (con,
323                                MHD_GET_ARGUMENT_KIND,
324                                &url_iterator,
325                                rest_conndata_handle);
326     con_handle->state = GN_REST_STATE_PROCESSING;
327     con_handle->plugin->process_request (rest_conndata_handle,
328                                          &plugin_callback,
329                                          con_handle);
330     *upload_data_size = 0;
331   }
332   if (NULL != con_handle->response)
333   {
334     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335                 "Queueing response from plugin with MHD\n");
336     //Handle Preflights
337     if (NULL != allow_origin)
338     {
339       MHD_add_response_header (con_handle->response,
340                                MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
341                                allow_origin);
342     }
343     if (NULL != allow_headers)
344     {
345       MHD_add_response_header (con_handle->response,
346                                "Access-Control-Allow-Headers",
347                                allow_headers);
348     }
349     int ret = MHD_queue_response (con,
350                                   con_handle->status,
351                                   con_handle->response);
352     cleanup_handle (con_handle);
353     return ret;
354   }
355   return MHD_YES;
356 }
357
358
359 /* ******************** MHD HTTP setup and event loop ******************** */
360
361 /**
362  * Function called when MHD decides that we are done with a connection.
363  *
364  * @param cls NULL
365  * @param connection connection handle
366  * @param con_cls value as set by the last call to
367  *        the MHD_AccessHandlerCallback, should be our handle
368  * @param toe reason for request termination (ignored)
369  */
370 static void
371 mhd_completed_cb (void *cls,
372                   struct MHD_Connection *connection,
373                   void **con_cls,
374                   enum MHD_RequestTerminationCode toe)
375 {
376   if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
377     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378                 "MHD encountered error handling request: %d\n",
379                 toe);
380 }
381
382
383 /**
384  * Kill the MHD daemon.
385  */
386 static void
387 kill_httpd ()
388 {
389   if (NULL != httpd)
390   {
391     MHD_stop_daemon (httpd);
392     httpd = NULL;
393   }
394   if (NULL != httpd_task)
395   {
396     GNUNET_SCHEDULER_cancel (httpd_task);
397     httpd_task = NULL;
398   }
399 }
400
401
402 /**
403  * Task run whenever HTTP server is idle for too long. Kill it.
404  *
405  * @param cls NULL
406  */
407 static void
408 kill_httpd_task (void *cls)
409 {
410   httpd_task = NULL;
411   kill_httpd ();
412 }
413
414
415 /**
416  * Schedule MHD.  This function should be called initially when an
417  * MHD is first getting its client socket, and will then automatically
418  * always be called later whenever there is work to be done.
419  *
420  * @param hd the daemon to schedule
421  */
422 static void
423 schedule_httpd ()
424 {
425   fd_set rs;
426   fd_set ws;
427   fd_set es;
428   struct GNUNET_NETWORK_FDSet *wrs;
429   struct GNUNET_NETWORK_FDSet *wws;
430   int max;
431   int haveto;
432   MHD_UNSIGNED_LONG_LONG timeout;
433   struct GNUNET_TIME_Relative tv;
434
435   FD_ZERO (&rs);
436   FD_ZERO (&ws);
437   FD_ZERO (&es);
438   max = -1;
439   if (MHD_YES != MHD_get_fdset (httpd, &rs, &ws, &es, &max))
440   {
441     kill_httpd ();
442     return;
443   }
444   haveto = MHD_get_timeout (httpd, &timeout);
445   if (MHD_YES == haveto)
446     tv.rel_value_us = (uint64_t) timeout * 1000LL;
447   else
448     tv = GNUNET_TIME_UNIT_FOREVER_REL;
449   if (-1 != max)
450   {
451     wrs = GNUNET_NETWORK_fdset_create ();
452     wws = GNUNET_NETWORK_fdset_create ();
453     GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
454     GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
455   }
456   else
457   {
458     wrs = NULL;
459     wws = NULL;
460   }
461   if (NULL != httpd_task)
462     GNUNET_SCHEDULER_cancel (httpd_task);
463   if ( (MHD_YES == haveto) ||
464        (-1 != max))
465   {
466     httpd_task =
467       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
468                                    tv, wrs, wws,
469                                    &do_httpd, NULL);
470   }
471   if (NULL != wrs)
472     GNUNET_NETWORK_fdset_destroy (wrs);
473   if (NULL != wws)
474     GNUNET_NETWORK_fdset_destroy (wws);
475 }
476
477 /**
478  * Task run whenever HTTP server operations are pending.
479  *
480  * @param cls NULL
481  */
482 static void
483 do_httpd (void *cls)
484 {
485   httpd_task = NULL;
486   MHD_run (httpd);
487   schedule_httpd ();
488 }
489
490
491 /**
492  * Accept new incoming connections
493  *
494  * @param cls the closure with the lsock4 or lsock6
495  * @param tc the scheduler context
496  */
497 static void
498 do_accept (void *cls)
499 {
500   struct GNUNET_NETWORK_Handle *lsock = cls;
501   const struct GNUNET_SCHEDULER_TaskContext *tc;
502   struct GNUNET_NETWORK_Handle *s;
503   int fd;
504   const struct sockaddr *addr;
505   socklen_t len;
506
507   if (lsock == lsock4)
508     ltask4 = NULL;
509   else
510     ltask6 = NULL;
511   tc = GNUNET_SCHEDULER_get_task_context ();
512   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
513     return;
514   if (lsock == lsock4)
515     ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
516                                             lsock,
517                                             &do_accept, lsock);
518   else
519     ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
520                                             lsock,
521                                             &do_accept, lsock);
522   s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
523   if (NULL == s)
524   {
525     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
526     return;
527   }
528   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529               "Got an inbound connection, waiting for data\n");
530   fd = GNUNET_NETWORK_get_fd (s);
531   addr = GNUNET_NETWORK_get_addr (s);
532   len = GNUNET_NETWORK_get_addrlen (s);
533   if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
534   {
535     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
536                 _("Failed to pass client to MHD\n"));
537     return;
538   }
539
540   schedule_httpd ();
541 }
542
543
544 /**
545  * Task run on shutdown
546  *
547  * @param cls closure
548  */
549 static void
550 do_shutdown (void *cls)
551 {
552   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
553               "Shutting down...\n");
554   kill_httpd ();
555   GNUNET_free_non_null (allow_origin);
556   GNUNET_free_non_null (allow_headers);
557 }
558
559
560 /**
561  * Create an IPv4 listen socket bound to our port.
562  *
563  * @return NULL on error
564  */
565 static struct GNUNET_NETWORK_Handle *
566 bind_v4 ()
567 {
568   struct GNUNET_NETWORK_Handle *ls;
569   struct sockaddr_in sa4;
570   int eno;
571
572   memset (&sa4, 0, sizeof (sa4));
573   sa4.sin_family = AF_INET;
574   sa4.sin_port = htons (port);
575 #if HAVE_SOCKADDR_IN_SIN_LEN
576   sa4.sin_len = sizeof (sa4);
577 #endif
578   ls = GNUNET_NETWORK_socket_create (AF_INET,
579                                      SOCK_STREAM,
580                                      0);
581   if (NULL == ls)
582     return NULL;
583   if (GNUNET_OK !=
584       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
585                                   sizeof (sa4)))
586   {
587     eno = errno;
588     GNUNET_NETWORK_socket_close (ls);
589     errno = eno;
590     return NULL;
591   }
592   return ls;
593 }
594
595
596 /**
597  * Create an IPv6 listen socket bound to our port.
598  *
599  * @return NULL on error
600  */
601 static struct GNUNET_NETWORK_Handle *
602 bind_v6 ()
603 {
604   struct GNUNET_NETWORK_Handle *ls;
605   struct sockaddr_in6 sa6;
606   int eno;
607
608   memset (&sa6, 0, sizeof (sa6));
609   sa6.sin6_family = AF_INET6;
610   sa6.sin6_port = htons (port);
611 #if HAVE_SOCKADDR_IN_SIN_LEN
612   sa6.sin6_len = sizeof (sa6);
613 #endif
614   ls = GNUNET_NETWORK_socket_create (AF_INET6,
615                                      SOCK_STREAM,
616                                      0);
617   if (NULL == ls)
618     return NULL;
619   if (GNUNET_OK !=
620       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa6,
621                                   sizeof (sa6)))
622   {
623     eno = errno;
624     GNUNET_NETWORK_socket_close (ls);
625     errno = eno;
626     return NULL;
627   }
628   return ls;
629 }
630
631
632 /**
633  * Callback for plugin load
634  *
635  * @param cls NULL
636  * @param libname the name of the library loaded
637  * @param lib_ret the object returned by the plugin initializer
638  */
639 static void
640 load_plugin (void *cls,
641              const char *libname,
642              void *lib_ret)
643 {
644   struct GNUNET_REST_Plugin *plugin = lib_ret;
645   struct GNUNET_HashCode key;
646   if (NULL == lib_ret)
647   {
648     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
649                 "Could not load plugin `%s'\n",
650                 libname);
651     return;
652   }
653   GNUNET_assert (1 < strlen (plugin->name));
654   GNUNET_assert ('/' == *plugin->name);
655   GNUNET_CRYPTO_hash (plugin->name+1, strlen (plugin->name+1), &key);
656   if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map,
657                                                       &key,
658                                                       plugin,
659                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
660   {
661     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662                 "Could not load add plugin `%s'\n",
663                 libname);
664     return;
665   }
666   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667               "Loaded plugin `%s'\n",
668               libname);
669 }
670
671
672 /**
673  * Main function that will be run
674  *
675  * @param cls closure
676  * @param args remaining command-line arguments
677  * @param cfgfile name of the configuration file used (for saving, can be NULL)
678  * @param c configuration
679  */
680 static void
681 run (void *cls,
682      char *const *args,
683      const char *cfgfile,
684      const struct GNUNET_CONFIGURATION_Handle *c)
685 {
686   cfg = c;
687   plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
688
689   /* Get CORS data from cfg */
690   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
691                                                           "REST_ALLOW_ORIGIN",
692                                                           &allow_origin))
693   {
694     //No origin specified
695     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
696                 "No CORS Access-Control-Allow-Origin Header will be sent...\n");
697   }
698
699   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
700                                                           "REST_ALLOW_HEADERS",
701                                                           &allow_headers))
702   {
703     //No origin specified
704     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
705                 "No CORS Access-Control-Allow-Headers Header will be sent...\n");
706   }
707
708   /* Open listen socket proxy */
709   lsock6 = bind_v6 ();
710   if (NULL == lsock6)
711   {
712     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
713   }
714   else
715   {
716     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
717     {
718       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
719       GNUNET_NETWORK_socket_close (lsock6);
720       lsock6 = NULL;
721     }
722     else
723     {
724       ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
725                                               lsock6, &do_accept, lsock6);
726     }
727   }
728   lsock4 = bind_v4 ();
729   if (NULL == lsock4)
730   {
731     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
732   }
733   else
734   {
735     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
736     {
737       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
738       GNUNET_NETWORK_socket_close (lsock4);
739       lsock4 = NULL;
740     }
741     else
742     {
743       ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
744                                               lsock4, &do_accept, lsock4);
745     }
746   }
747   if ( (NULL == lsock4) &&
748        (NULL == lsock6) )
749   {
750     GNUNET_SCHEDULER_shutdown ();
751     return;
752   }
753   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754               "Service listens on port %u\n",
755               port);
756   httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
757                             0,
758                             NULL, NULL,
759                             &create_response, NULL,
760                             MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
761                             MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
762                             MHD_OPTION_END);
763   if (NULL == httpd)
764   {
765     GNUNET_SCHEDULER_shutdown ();
766     return;
767   }
768   /* Load plugins */
769   GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
770                           (void *) cfg,
771                           &load_plugin,
772                           NULL);
773   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
774                                 &do_shutdown, NULL);
775 }
776
777
778 /**
779  *
780  * The main function for gnunet-rest-service
781  *
782  * @param argc number of arguments from the cli
783  * @param argv command line arguments
784  * @return 0 ok, 1 on error
785  *
786  */
787 int
788 main (int argc, char *const *argv)
789 {
790   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
791     {'p', "port", NULL,
792       gettext_noop ("listen on specified port (default: 7776)"), 1,
793       &GNUNET_GETOPT_set_ulong, &port},
794     GNUNET_GETOPT_OPTION_END
795   };
796   static const char* err_page =
797     "{}";
798   int ret;
799
800   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
801     return 2;
802   GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL);
803   failure_response = MHD_create_response_from_buffer (strlen(err_page),
804                                                       (void*)err_page,
805                                                       MHD_RESPMEM_PERSISTENT);
806   ret =
807     (GNUNET_OK ==
808      GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-server",
809                          _("GNUnet REST server"),
810                          options,
811                          &run, NULL)) ? 0: 1;
812   MHD_destroy_response (failure_response);
813   GNUNET_free_non_null ((char *) argv);
814   return ret;
815 }
816
817 /* end of gnunet-rest-server.c */