-logging
[oweals/gnunet.git] / src / rest / gnunet-rest-server.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
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     /* daemon is idle, kill after timeout */
471     httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT,
472                                                &kill_httpd_task,
473                                                NULL);
474   }
475   else
476   {
477     httpd_task =
478       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
479                                    tv, wrs, wws,
480                                    &do_httpd, NULL);
481   }
482   if (NULL != wrs)
483     GNUNET_NETWORK_fdset_destroy (wrs);
484   if (NULL != wws)
485     GNUNET_NETWORK_fdset_destroy (wws);
486 }
487
488 /**
489  * Task run whenever HTTP server operations are pending.
490  *
491  * @param cls NULL
492  * @param tc scheduler context
493  */
494 static void
495 do_httpd (void *cls,
496           const struct GNUNET_SCHEDULER_TaskContext *tc)
497 {
498   httpd_task = NULL;
499   MHD_run (httpd);
500   schedule_httpd ();
501 }
502
503
504 /**
505  * Accept new incoming connections
506  *
507  * @param cls the closure with the lsock4 or lsock6
508  * @param tc the scheduler context
509  */
510 static void
511 do_accept (void *cls,
512            const struct GNUNET_SCHEDULER_TaskContext *tc)
513 {
514   struct GNUNET_NETWORK_Handle *lsock = cls;
515   struct GNUNET_NETWORK_Handle *s;
516   int fd;
517   const struct sockaddr *addr;
518   socklen_t len;
519
520   if (lsock == lsock4)
521     ltask4 = NULL;
522   else
523     ltask6 = NULL;
524   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
525     return; 
526   if (lsock == lsock4)
527     ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
528                                             lsock,
529                                             &do_accept, lsock);
530   else
531     ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
532                                             lsock,
533                                             &do_accept, lsock);
534   s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
535   if (NULL == s)
536   {
537     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
538     return;
539   }
540   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541               "Got an inbound connection, waiting for data\n");
542   fd = GNUNET_NETWORK_get_fd (s);
543   addr = GNUNET_NETWORK_get_addr (s);
544   len = GNUNET_NETWORK_get_addrlen (s);
545   if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
546   {
547     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
548                 _("Failed to pass client to MHD\n"));
549     return;
550   }
551
552   schedule_httpd ();
553 }
554
555
556 /**
557  * Task run on shutdown
558  *
559  * @param cls closure
560  * @param tc task context
561  */
562 static void
563 do_shutdown (void *cls,
564              const struct GNUNET_SCHEDULER_TaskContext *tc)
565 {
566   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
567               "Shutting down...\n");
568   kill_httpd ();
569   GNUNET_free_non_null (allow_origin);
570   GNUNET_free_non_null (allow_headers);
571 }
572
573
574 /**
575  * Create an IPv4 listen socket bound to our port.
576  *
577  * @return NULL on error
578  */
579 static struct GNUNET_NETWORK_Handle *
580 bind_v4 ()
581 {
582   struct GNUNET_NETWORK_Handle *ls;
583   struct sockaddr_in sa4;
584   int eno;
585
586   memset (&sa4, 0, sizeof (sa4));
587   sa4.sin_family = AF_INET;
588   sa4.sin_port = htons (port);
589 #if HAVE_SOCKADDR_IN_SIN_LEN
590   sa4.sin_len = sizeof (sa4);
591 #endif 
592   ls = GNUNET_NETWORK_socket_create (AF_INET,
593                                      SOCK_STREAM,
594                                      0);
595   if (NULL == ls)
596     return NULL;
597   if (GNUNET_OK !=
598       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
599                                   sizeof (sa4)))
600   {
601     eno = errno;
602     GNUNET_NETWORK_socket_close (ls);
603     errno = eno;
604     return NULL;
605   }
606   return ls;
607 }
608
609
610 /**
611  * Create an IPv6 listen socket bound to our port.
612  *
613  * @return NULL on error
614  */
615 static struct GNUNET_NETWORK_Handle *
616 bind_v6 ()
617 {
618   struct GNUNET_NETWORK_Handle *ls;
619   struct sockaddr_in6 sa6;
620   int eno;
621
622   memset (&sa6, 0, sizeof (sa6));
623   sa6.sin6_family = AF_INET6;
624   sa6.sin6_port = htons (port);
625 #if HAVE_SOCKADDR_IN_SIN_LEN
626   sa6.sin6_len = sizeof (sa6);
627 #endif 
628   ls = GNUNET_NETWORK_socket_create (AF_INET6,
629                                      SOCK_STREAM,
630                                      0);
631   if (NULL == ls)
632     return NULL;
633   if (GNUNET_OK !=
634       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa6,
635                                   sizeof (sa6)))
636   {
637     eno = errno;
638     GNUNET_NETWORK_socket_close (ls);
639     errno = eno;
640     return NULL;
641   }
642   return ls;
643 }
644
645
646 /**
647  * Callback for plugin load
648  *
649  * @param cls NULL
650  * @param libname the name of the library loaded
651  * @param lib_ret the object returned by the plugin initializer
652  */
653 static void
654 load_plugin (void *cls,
655              const char *libname,
656              void *lib_ret)
657 {
658   struct GNUNET_REST_Plugin *plugin = lib_ret;
659   struct GNUNET_HashCode key;
660   if (NULL == lib_ret)
661   {
662     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
663                 "Could not load plugin `%s'\n",
664                 libname);
665     return;
666   }
667   GNUNET_assert (1 < strlen (plugin->name));
668   GNUNET_assert ('/' == *plugin->name);
669   GNUNET_CRYPTO_hash (plugin->name+1, strlen (plugin->name+1), &key);
670   if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map,
671                                                       &key,
672                                                       plugin,
673                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
674   {
675     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
676                 "Could not load add plugin `%s'\n",
677                 libname);
678     return;
679   }
680   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681               "Loaded plugin `%s'\n",
682               libname);
683 }
684
685
686 /**
687  * Main function that will be run
688  *
689  * @param cls closure
690  * @param args remaining command-line arguments
691  * @param cfgfile name of the configuration file used (for saving, can be NULL)
692  * @param c configuration
693  */
694 static void
695 run (void *cls, 
696      char *const *args, 
697      const char *cfgfile,
698      const struct GNUNET_CONFIGURATION_Handle *c)
699 {
700   cfg = c;
701   plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
702
703   /* Get CORS data from cfg */
704   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
705                                                           "REST_ALLOW_ORIGIN",
706                                                           &allow_origin))
707   {
708     //No origin specified
709     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
710                 "No CORS Access-Control-Allow-Origin Header will be sent...\n");
711   }
712
713   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
714                                                           "REST_ALLOW_HEADERS",
715                                                           &allow_headers))
716   {
717     //No origin specified
718     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
719                 "No CORS Access-Control-Allow-Headers Header will be sent...\n");
720   }
721
722   /* Open listen socket proxy */
723   lsock6 = bind_v6 ();
724   if (NULL == lsock6)
725   {
726     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
727   }
728   else
729   {
730     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
731     {
732       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
733       GNUNET_NETWORK_socket_close (lsock6);
734       lsock6 = NULL;
735     }
736     else
737     {
738       ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
739                                               lsock6, &do_accept, lsock6);
740     }
741   }
742   lsock4 = bind_v4 ();
743   if (NULL == lsock4)
744   {
745     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
746   }
747   else
748   {
749     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
750     {
751       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
752       GNUNET_NETWORK_socket_close (lsock4);
753       lsock4 = NULL;
754     }
755     else
756     {
757       ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
758                                               lsock4, &do_accept, lsock4);
759     }
760   }
761   if ( (NULL == lsock4) &&
762        (NULL == lsock6) )
763   {
764     GNUNET_SCHEDULER_shutdown ();
765     return;
766   } 
767   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768               "Service listens on port %u\n",
769               port);
770   httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
771                             0,
772                             NULL, NULL,
773                             &create_response, NULL,
774                             MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
775                             MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
776                             MHD_OPTION_END);
777   if (NULL == httpd)
778   {
779     GNUNET_SCHEDULER_shutdown ();
780     return;
781   }
782   /* Load plugins */
783   GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
784                           (void *) cfg,
785                           &load_plugin,
786                           NULL);
787   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
788                                 &do_shutdown, NULL);
789 }
790
791
792 /**
793  *
794  * The main function for gnunet-rest-service
795  *
796  * @param argc number of arguments from the cli
797  * @param argv command line arguments
798  * @return 0 ok, 1 on error
799  *
800  */
801 int
802 main (int argc, char *const *argv)
803 {
804   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
805     {'p', "port", NULL,
806       gettext_noop ("listen on specified port (default: 7776)"), 1,
807       &GNUNET_GETOPT_set_ulong, &port},
808     GNUNET_GETOPT_OPTION_END
809   };
810   static const char* err_page =
811     "{}";
812   int ret;
813
814   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
815     return 2;
816   GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL);
817   failure_response = MHD_create_response_from_buffer (strlen(err_page),
818                                                       (void*)err_page,
819                                                       MHD_RESPMEM_PERSISTENT);
820   ret =
821     (GNUNET_OK ==
822      GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-server",
823                          _("GNUnet REST server"),
824                          options,
825                          &run, NULL)) ? 0: 1;
826   MHD_destroy_response (failure_response);
827   GNUNET_free_non_null ((char *) argv);
828   return ret;
829 }
830
831 /* end of gnunet-rest-server.c */