1f909c58c3ca4fbdfb64a15c5a337055235618e8
[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., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, 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 struct GNUNET_SCHEDULER_Task * httpd_task;
65
66 /**
67  * is this an ssl daemon? //TODO
68  */
69 int is_ssl;
70
71 /**
72  * The port the service is running on (default 7776)
73  */
74 static unsigned long port = GNUNET_REST_SERVICE_PORT;
75
76 /**
77  * The listen socket of the service for IPv4
78  */
79 static struct GNUNET_NETWORK_Handle *lsock4;
80
81 /**
82  * The listen socket of the service for IPv6
83  */
84 static struct GNUNET_NETWORK_Handle *lsock6;
85
86 /**
87  * The listen task ID for IPv4
88  */
89 static struct GNUNET_SCHEDULER_Task * ltask4;
90
91 /**
92  * The listen task ID for IPv6
93  */
94 static struct GNUNET_SCHEDULER_Task * ltask6;
95
96 /**
97  * Daemon for HTTP
98  */
99 static struct MHD_Daemon *httpd;
100
101 /**
102  * Response we return on failures.
103  */
104 static struct MHD_Response *failure_response;
105
106 /**
107  * Our configuration.
108  */
109 static const struct GNUNET_CONFIGURATION_Handle *cfg;
110
111 /**
112  * Map of loaded plugins.
113  */
114 struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
115
116 /**
117  * MHD Connection handle 
118  */
119 struct MhdConnectionHandle
120 {
121   struct MHD_Connection *con;
122
123   struct MHD_Response *response;
124
125   struct GNUNET_REST_Plugin *plugin;
126
127   struct RestConnectionDataHandle *data_handle;
128
129   int status;
130
131   int state;
132 };
133
134 /* ************************* Global helpers ********************* */
135
136
137 /**
138  * Task run whenever HTTP server operations are pending.
139  *
140  * @param cls NULL
141  * @param tc sched context
142  */
143 static void
144 do_httpd (void *cls,
145           const struct GNUNET_SCHEDULER_TaskContext *tc);
146
147
148 /**
149  * Run MHD now, we have extra data ready for the callback.
150  */
151 static void
152 run_mhd_now ()
153 {
154   if (NULL !=
155       httpd_task)
156     GNUNET_SCHEDULER_cancel (httpd_task);
157   httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
158                                          NULL);
159 }
160
161 /**
162  * Plugin result callback
163  *
164  * @param cls closure (MHD connection handle)
165  * @param data the data to return to the caller
166  * @param len length of the data
167  * @param status GNUNET_OK if successful
168  */
169 void
170 plugin_callback (void *cls,
171                  const char *data,
172                  size_t len,
173                  int status)
174 {
175   struct MhdConnectionHandle *handle = cls;
176   struct MHD_Response *resp = MHD_create_response_from_buffer (len,
177                                                                (void*)data,
178                                                                MHD_RESPMEM_MUST_COPY);
179   (void) MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
180   handle->status = status;
181   handle->response = resp;
182   run_mhd_now(); 
183 }
184
185 int
186 cleanup_url_map (void *cls,
187                  const struct GNUNET_HashCode *key,
188                  void *value)
189 {
190   GNUNET_free_non_null (value);
191   return GNUNET_YES;
192 }
193
194 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 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     else
302       con_handle->plugin = NULL;
303
304     if (NULL == con_handle->plugin)
305     {
306       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307                   "Queueing response with MHD\n");
308       GNUNET_free (con_handle);
309       MHD_queue_response (con,
310                           MHD_HTTP_INTERNAL_SERVER_ERROR,
311                           failure_response);
312     }
313     return MHD_YES;
314   }
315   if (GN_REST_STATE_INIT == con_handle->state)
316   {
317     rest_conndata_handle = GNUNET_new (struct RestConnectionDataHandle);
318     rest_conndata_handle->method = meth;
319     rest_conndata_handle->url = url;
320     rest_conndata_handle->data = upload_data;
321     rest_conndata_handle->data_size = *upload_data_size;
322     rest_conndata_handle->url_param_map = GNUNET_CONTAINER_multihashmap_create (16,
323                                                                                 GNUNET_NO);
324     con_handle->data_handle = rest_conndata_handle;
325     MHD_get_connection_values (con,
326                                MHD_GET_ARGUMENT_KIND,
327                                &url_iterator,
328                                rest_conndata_handle);
329     con_handle->state = GN_REST_STATE_PROCESSING;
330     con_handle->plugin->process_request (rest_conndata_handle,
331                                          &plugin_callback,
332                                          con_handle);
333     *upload_data_size = 0;
334
335   }
336   if (NULL != con_handle->response)
337   {
338
339     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340                 "Queueing response from plugin with MHD\n");
341     if (GNUNET_OK == con_handle->status) {
342       return MHD_queue_response (con,
343                                  MHD_HTTP_OK,
344                                  con_handle->response);
345     } else {
346       return MHD_queue_response (con,
347                                  MHD_HTTP_BAD_REQUEST,
348                                  con_handle->response);
349     }
350     cleanup_handle (con_handle);
351   }
352   return MHD_YES;
353 }
354
355 /* ******************** MHD HTTP setup and event loop ******************** */
356
357 /**
358  * Function called when MHD decides that we are done with a connection.
359  *
360  * @param cls NULL
361  * @param connection connection handle
362  * @param con_cls value as set by the last call to
363  *        the MHD_AccessHandlerCallback, should be our handle
364  * @param toe reason for request termination (ignored)
365  */
366 static void
367 mhd_completed_cb (void *cls,
368                   struct MHD_Connection *connection,
369                   void **con_cls,
370                   enum MHD_RequestTerminationCode toe)
371 {
372
373   if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
374     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375                 "MHD encountered error handling request: %d\n",
376                 toe);
377   *con_cls = NULL;
378 }
379
380 /**
381  * Kill the MHD daemon.
382  */
383 static void
384 kill_httpd ()
385 {
386   if (NULL != httpd)
387   {
388     MHD_stop_daemon (httpd);
389     httpd = NULL;
390   }
391   if (NULL != httpd_task)
392   { 
393     GNUNET_SCHEDULER_cancel (httpd_task);
394     httpd_task = NULL;
395   }
396 }
397
398 /**
399  * Task run whenever HTTP server is idle for too long. Kill it.
400  *
401  * @param cls NULL
402  * @param tc sched context
403  */
404 static void
405 kill_httpd_task (void *cls,
406                  const struct GNUNET_SCHEDULER_TaskContext *tc)
407 {
408   httpd_task = NULL;
409   kill_httpd ();
410 }
411 /**
412  * Schedule MHD.  This function should be called initially when an
413  * MHD is first getting its client socket, and will then automatically
414  * always be called later whenever there is work to be done.
415  *
416  * @param hd the daemon to schedule
417  */
418 static void
419 schedule_httpd ()
420 {
421   fd_set rs;
422   fd_set ws;
423   fd_set es;
424   struct GNUNET_NETWORK_FDSet *wrs;
425   struct GNUNET_NETWORK_FDSet *wws;
426   int max;
427   int haveto;
428   MHD_UNSIGNED_LONG_LONG timeout;
429   struct GNUNET_TIME_Relative tv;
430
431   FD_ZERO (&rs);
432   FD_ZERO (&ws);
433   FD_ZERO (&es);
434   max = -1;
435   if (MHD_YES != MHD_get_fdset (httpd, &rs, &ws, &es, &max))
436   {
437     kill_httpd ();
438     return;
439   }
440   haveto = MHD_get_timeout (httpd, &timeout);
441   if (MHD_YES == haveto)
442     tv.rel_value_us = (uint64_t) timeout * 1000LL;
443   else
444     tv = GNUNET_TIME_UNIT_FOREVER_REL;
445   if (-1 != max)
446   {
447     wrs = GNUNET_NETWORK_fdset_create ();
448     wws = GNUNET_NETWORK_fdset_create ();
449     GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
450     GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
451   }
452   else
453   {
454     wrs = NULL;
455     wws = NULL;
456   }
457   if (NULL != httpd_task)
458     GNUNET_SCHEDULER_cancel (httpd_task);
459   if ( (MHD_YES != haveto) &&
460        (-1 == max))
461   {
462     /* daemon is idle, kill after timeout */
463     httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT,
464                                                &kill_httpd_task,
465                                                NULL);
466   }
467   else
468   {
469     httpd_task =
470       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
471                                    tv, wrs, wws,
472                                    &do_httpd, NULL);
473   }
474   if (NULL != wrs)
475     GNUNET_NETWORK_fdset_destroy (wrs);
476   if (NULL != wws)
477     GNUNET_NETWORK_fdset_destroy (wws);
478 }
479
480 /**
481  * Task run whenever HTTP server operations are pending.
482  *
483  * @param cls NULL
484  * @param tc scheduler context
485  */
486 static void
487 do_httpd (void *cls,
488           const struct GNUNET_SCHEDULER_TaskContext *tc)
489 {
490   httpd_task = NULL;
491   MHD_run (httpd);
492   schedule_httpd ();
493 }
494
495 /**
496  * Accept new incoming connections
497  *
498  * @param cls the closure with the lsock4 or lsock6
499  * @param tc the scheduler context
500  */
501 static void
502 do_accept (void *cls,
503            const struct GNUNET_SCHEDULER_TaskContext *tc)
504 {
505   struct GNUNET_NETWORK_Handle *lsock = cls;
506   struct GNUNET_NETWORK_Handle *s;
507   int fd;
508   const struct sockaddr *addr;
509   socklen_t len;
510
511   if (lsock == lsock4)
512     ltask4 = NULL;
513   else
514     ltask6 = NULL;
515   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
516     return; 
517   if (lsock == lsock4)
518     ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
519                                             lsock,
520                                             &do_accept, lsock);
521   else
522     ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
523                                             lsock,
524                                             &do_accept, lsock);
525   s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
526   if (NULL == s)
527   {
528     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
529     return;
530   }
531   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532               "Got an inbound connection, waiting for data\n");
533   fd = GNUNET_NETWORK_get_fd (s);
534   addr = GNUNET_NETWORK_get_addr (s);
535   len = GNUNET_NETWORK_get_addrlen (s);
536   if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
537   {
538     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
539                 _("Failed to pass client to MHD\n"));
540     return;
541   }
542
543   schedule_httpd ();
544 }
545
546 /**
547  * Task run on shutdown
548  *
549  * @param cls closure
550  * @param tc task context
551  */
552 static void
553 do_shutdown (void *cls,
554              const struct GNUNET_SCHEDULER_TaskContext *tc)
555 {
556   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
557               "Shutting down...\n");
558   kill_httpd ();
559 }
560
561 /**
562  * Create an IPv4 listen socket bound to our port.
563  *
564  * @return NULL on error
565  */
566 static struct GNUNET_NETWORK_Handle *
567 bind_v4 ()
568 {
569   struct GNUNET_NETWORK_Handle *ls;
570   struct sockaddr_in sa4;
571   int eno;
572
573   memset (&sa4, 0, sizeof (sa4));
574   sa4.sin_family = AF_INET;
575   sa4.sin_port = htons (port);
576 #if HAVE_SOCKADDR_IN_SIN_LEN
577   sa4.sin_len = sizeof (sa4);
578 #endif 
579   ls = GNUNET_NETWORK_socket_create (AF_INET,
580                                      SOCK_STREAM,
581                                      0);
582   if (NULL == ls)
583     return NULL;
584   if (GNUNET_OK !=
585       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
586                                   sizeof (sa4)))
587   {
588     eno = errno;
589     GNUNET_NETWORK_socket_close (ls);
590     errno = eno;
591     return NULL;
592   }
593   return ls;
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  * Callback for plugin load
633  *
634  * @param cls NULL
635  * @param libname the name of the library loaded
636  * @param lib_ret the object returned by the plugin initializer
637  */
638 void
639 load_plugin (void *cls,
640              const char *libname,
641              void *lib_ret)
642 {
643   struct GNUNET_REST_Plugin *plugin = lib_ret;
644   struct GNUNET_HashCode key;
645   if (NULL == lib_ret)
646   {
647     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
648                 "Could not load plugin `%s'\n",
649                 libname);
650     return;
651   }
652   GNUNET_assert (1 < strlen (plugin->name));
653   GNUNET_assert ('/' == *plugin->name);
654   GNUNET_CRYPTO_hash (plugin->name+1, strlen (plugin->name+1), &key);
655   if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map,
656                                                       &key,
657                                                       plugin,
658                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
659   {
660     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
661                 "Could not load add plugin `%s'\n",
662                 libname);
663     return;
664   }
665   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666               "Loaded plugin `%s'\n",
667               libname);
668 }
669
670 /**
671  * Main function that will be run
672  *
673  * @param cls closure
674  * @param args remaining command-line arguments
675  * @param cfgfile name of the configuration file used (for saving, can be NULL)
676  * @param c configuration
677  */
678 static void
679 run (void *cls, char *const *args, const char *cfgfile,
680      const struct GNUNET_CONFIGURATION_Handle *c)
681 {
682   cfg = c;
683   plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
684
685   /* Open listen socket proxy */
686   lsock6 = bind_v6 ();
687   if (NULL == lsock6)
688     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
689   else
690   {
691     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
692     {
693       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
694       GNUNET_NETWORK_socket_close (lsock6);
695       lsock6 = NULL;
696     }
697     else
698     {
699       ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
700                                               lsock6, &do_accept, lsock6);
701     }
702   }
703   lsock4 = bind_v4 ();
704   if (NULL == lsock4)
705     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
706   else
707   {
708     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
709     {
710       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
711       GNUNET_NETWORK_socket_close (lsock4);
712       lsock4 = NULL;
713     }
714     else
715     {
716       ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
717                                               lsock4, &do_accept, lsock4);
718     }
719   }
720   if ( (NULL == lsock4) &&
721        (NULL == lsock6) )
722   {
723     GNUNET_SCHEDULER_shutdown ();
724     return;
725   } 
726   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727               "Service listens on port %u\n",
728               port);
729   httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
730                             0,
731                             NULL, NULL,
732                             &create_response, NULL,
733                             MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
734                             MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
735                             MHD_OPTION_END);
736   if (NULL == httpd)
737   {
738     GNUNET_SCHEDULER_shutdown ();
739     return;
740   }
741   /* Load plugins */
742   GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
743                           (void *) cfg,
744                           &load_plugin,
745                           NULL);
746   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
747                                 &do_shutdown, NULL);
748 }
749
750 /**
751  *
752  * The main function for gnunet-rest-service
753  *
754  * @param argc number of arguments from the cli
755  * @param argv command line arguments
756  * @return 0 ok, 1 on error
757  *
758  */
759 int
760 main (int argc, char *const *argv)
761 {
762   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
763     {'p', "port", NULL,
764       gettext_noop ("listen on specified port (default: 7776)"), 1,
765       &GNUNET_GETOPT_set_ulong, &port},
766     GNUNET_GETOPT_OPTION_END
767   };
768   static const char* err_page =
769     "{}";
770   int ret;
771
772   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
773     return 2;
774   GNUNET_log_setup ("gnunet-rest-service", "WARNING", NULL);
775   failure_response = MHD_create_response_from_buffer (strlen(err_page),
776                                                       (void*)err_page,
777                                                       MHD_RESPMEM_PERSISTENT);
778   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
779               "Start\n");
780   ret =
781     (GNUNET_OK ==
782      GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-server",
783                          _("GNUnet REST server"),
784                          options,
785                          &run, NULL)) ? 0: 1;
786   MHD_destroy_response (failure_response);
787   GNUNET_free_non_null ((char *) argv);
788   return ret;
789 }
790
791 /* end of gnunet-rest-server.c */