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