43d9171ebb9eda9b257ca4311a377e3b2557bec1
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 /**
22  * @file transport/plugin_transport_http.c
23  * @brief http transport service plugin
24  * @author Matthias Wachs
25  */
26
27 #include "plugin_transport_http.h"
28
29 #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
30 #define _RECEIVE 0
31 #define _SEND 1
32
33 static void
34 server_log (void *arg, const char *fmt, va_list ap)
35 {
36   char text[1024];
37
38   vsnprintf (text, sizeof (text), fmt, ap);
39   va_end (ap);
40   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: %s\n", text);
41 }
42
43 struct ServerConnection
44 {
45   /* _RECV or _SEND */
46   int direction;
47
48   /* should this connection get disconnected? GNUNET_YES/NO  */
49   int disconnect;
50
51   struct Session *session;
52   struct MHD_Connection * mhd_conn;
53 };
54
55 /**
56  * Check if incoming connection is accepted.
57  * NOTE: Here every connection is accepted
58  * @param cls plugin as closure
59  * @param addr address of incoming connection
60  * @param addr_len address length of incoming connection
61  * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
62  *
63  */
64 static int
65 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
66 {
67   struct Plugin * plugin = cls;
68
69   if (plugin->cur_connections <= plugin->max_connections)
70     return MHD_YES;
71   else
72   {
73     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Cannot accept new connections\n");
74     return MHD_NO;
75   }
76 }
77
78
79 /**
80  * Callback called by MHD when it needs data to send
81  * @param cls current session
82  * @param pos position in buffer
83  * @param buf the buffer to write data to
84  * @param max max number of bytes available in buffer
85  * @return bytes written to buffer
86  */
87 #if 0
88 static ssize_t
89 server_send_cb (void *cls, uint64_t pos, char *buf, size_t max)
90 {
91
92   return 0;
93 }
94 #endif
95
96
97 #if BUILD_HTTPS
98 static char *
99 server_load_file (const char *file)
100 {
101   struct GNUNET_DISK_FileHandle *gn_file;
102   struct stat fstat;
103   char *text = NULL;
104
105   if (0 != STAT (file, &fstat))
106     return NULL;
107   text = GNUNET_malloc (fstat.st_size + 1);
108   gn_file =
109       GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
110                              GNUNET_DISK_PERM_USER_READ);
111   if (gn_file == NULL)
112   {
113     GNUNET_free (text);
114     return NULL;
115   }
116   if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
117   {
118     GNUNET_free (text);
119     GNUNET_DISK_file_close (gn_file);
120     return NULL;
121   }
122   text[fstat.st_size] = '\0';
123   GNUNET_DISK_file_close (gn_file);
124   return text;
125 }
126 #endif
127
128
129 #if BUILD_HTTPS
130
131 static int
132 server_load_certificate (struct Plugin *plugin)
133 {
134   int res = GNUNET_OK;
135
136   char *key_file;
137   char *cert_file;
138
139   /* Get crypto init string from config
140    * If not present just use default values */
141   GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
142                                          "CRYPTO_INIT", &plugin->crypto_init);
143
144   if (GNUNET_OK !=
145       GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
146                                                "KEY_FILE", &key_file))
147   {
148     key_file = "https_key.key";
149   }
150
151   if (GNUNET_OK !=
152       GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
153                                                "CERT_FILE", &cert_file))
154   {
155     cert_file = "https_cert.crt";
156   }
157
158   /* read key & certificates from file */
159 #if VERBOSE_SERVER
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161               "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
162               key_file, cert_file);
163 #endif
164
165   plugin->key = server_load_file (key_file);
166   plugin->cert = server_load_file (cert_file);
167
168   if ((plugin->key == NULL) || (plugin->cert == NULL))
169   {
170     struct GNUNET_OS_Process *cert_creation;
171
172     GNUNET_free_non_null (plugin->key);
173     plugin->key = NULL;
174     GNUNET_free_non_null (plugin->cert);
175     plugin->cert = NULL;
176
177 #if VERBOSE_SERVER
178     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179                 "No usable TLS certificate found, creating certificate\n");
180 #endif
181     errno = 0;
182     cert_creation =
183         GNUNET_OS_start_process (NULL, NULL,
184                                  "gnunet-transport-certificate-creation",
185                                  "gnunet-transport-certificate-creation",
186                                  key_file, cert_file, NULL);
187     if (cert_creation == NULL)
188     {
189       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
190                        _
191                        ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
192       GNUNET_free (key_file);
193       GNUNET_free (cert_file);
194
195       GNUNET_free_non_null (plugin->key);
196       GNUNET_free_non_null (plugin->cert);
197       GNUNET_free_non_null (plugin->crypto_init);
198
199       return GNUNET_SYSERR;
200     }
201     GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
202     GNUNET_OS_process_close (cert_creation);
203
204     plugin->key = server_load_file (key_file);
205     plugin->cert = server_load_file (cert_file);
206   }
207
208   if ((plugin->key == NULL) || (plugin->cert == NULL))
209   {
210     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
211                      _
212                      ("No usable TLS certificate found and creating one failed!\n"),
213                      "transport-https");
214     GNUNET_free (key_file);
215     GNUNET_free (cert_file);
216
217     GNUNET_free_non_null (plugin->key);
218     GNUNET_free_non_null (plugin->cert);
219     GNUNET_free_non_null (plugin->crypto_init);
220
221     return GNUNET_SYSERR;
222   }
223   GNUNET_free (key_file);
224   GNUNET_free (cert_file);
225 #if DEBUG_HTTP
226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
227 #endif
228
229   return res;
230 }
231 #endif
232
233
234 /**
235  * Process GET or PUT request received via MHD.  For
236  * GET, queue response that will send back our pending
237  * messages.  For PUT, process incoming data and send
238  * to GNUnet core.  In either case, check if a session
239  * already exists and create a new one if not.
240  */
241 static int
242 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
243                   const char *url, const char *method, const char *version,
244                   const char *upload_data, size_t * upload_data_size,
245                   void **httpSessionCache)
246 {
247   struct Plugin *plugin = cls;
248   struct ServerConnection *sc = *httpSessionCache;
249   struct Session *s = NULL;
250
251   int res = MHD_YES;
252   struct MHD_Response *response;
253
254   GNUNET_assert (cls != NULL);
255   /* new connection */
256   if (sc == NULL)
257     {
258     uint32_t tag;
259     const union MHD_ConnectionInfo *conn_info;
260     size_t addrlen;
261     struct GNUNET_PeerIdentity target;
262     int check = GNUNET_NO;
263     struct Session * t;
264     int direction;
265
266     conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
267     if (conn_info->client_addr->sa_family == AF_INET)
268       addrlen = sizeof (struct sockaddr_in);
269     else if (conn_info->client_addr->sa_family == AF_INET6)
270       addrlen = sizeof (struct sockaddr_in6);
271     else
272       return MHD_NO;
273
274     if ((strlen(&url[1]) >= 105)  && (url[104] == ';'))
275     {
276       char hash[104];
277       char * tagc = (char *) &url[105];
278       memcpy(&hash, &url[1], 103);
279       hash [103] = '\0';
280       if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash, &(target.hashPubKey)))
281       {
282         tag = strtoul (tagc, NULL, 10);
283         if (tagc > 0)
284           check = GNUNET_YES;
285       }
286     }
287
288     if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
289       direction = _RECEIVE;
290     if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
291       direction = _SEND;
292
293     if (check == GNUNET_NO)
294       goto error;
295
296     plugin->cur_connections++;
297
298 #if VERBOSE_SERVER
299     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: New inbound connection from %s with tag %u\n", GNUNET_h2s_full(&(target.hashPubKey)), tag);
300 #endif
301     /* find duplicate session */
302
303     t = plugin->head;
304
305     while (t != NULL)
306     {
307       if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
308           /* FIXME add source address comparison */
309           (t->tag == tag))
310       break;
311       t = t->next;
312     }
313     if (t != NULL)
314     {
315 #if VERBOSE_SERVER
316       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
317 #endif
318       goto error;
319     }
320
321     /* find semi-session */
322     t = plugin->server_semi_head;
323
324     while (t != NULL)
325     {
326       /* FIXME add source address comparison */
327       if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
328           (t->tag == tag))
329       {
330         break;
331       }
332       t = t->next;
333     }
334
335     if (t == NULL)
336       goto create;
337
338 #if VERBOSE_SERVER
339       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
340 #endif
341
342     if ((direction == _SEND) && (t->server_send != NULL))
343     {
344 #if VERBOSE_SERVER
345       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
346 #endif
347       goto error;
348     }
349     else
350     {
351       s = t;
352       GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
353       GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
354 #if VERBOSE_SERVER
355       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
356 #endif
357
358       goto found;
359     }
360     if ((direction == _RECEIVE) && (t->server_recv != NULL))
361     {
362 #if VERBOSE_SERVER
363       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
364 #endif
365       goto error;
366     }
367     else
368     {
369       s = t;
370       GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
371       GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
372 #if VERBOSE_SERVER
373       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
374 #endif
375       goto found;
376     }
377
378 create:
379 /* create new session */
380 #if VERBOSE_SERVER
381       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
382 #endif
383
384     s = create_session(plugin,
385                         &target,
386                         conn_info->client_addr,
387                         addrlen,
388                         NULL,
389                         NULL);
390
391     s->inbound = GNUNET_YES;
392     s->tag= tag;
393     if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
394       s->server_recv = s;
395     if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
396       s->server_send = s;
397     GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
398
399     goto found;
400 error:
401         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Invalid connection request\n");
402         response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
403         res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
404         MHD_destroy_response (response);
405         return res;
406
407
408 found:
409
410
411     sc = GNUNET_malloc (sizeof (struct ServerConnection));
412     sc->mhd_conn = mhd_connection;
413     sc->direction = direction;
414     sc->session = s;
415     if (direction == _SEND)
416       s->server_send = sc;
417     if (direction == _RECEIVE)
418       s->server_recv = sc;
419
420     (*httpSessionCache) = sc;
421     return MHD_YES;
422   }
423   /* existing connection */
424   sc = (*httpSessionCache);
425   s = sc->session;
426
427   /* connection is to be disconnected*/
428   if (sc->disconnect == GNUNET_YES)
429   {
430     response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
431     res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
432 #if VERBOSE_SERVER
433     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434                 "Sent HTTP/1.1: 200 OK as PUT Response\n");
435 #endif
436     MHD_destroy_response (response);
437     return MHD_YES;
438   }
439
440   return res;
441 }
442
443 static void
444 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
445                       void **httpSessionCache)
446 {
447   struct ServerConnection *sc = *httpSessionCache;
448   struct ServerConnection *tc = *httpSessionCache;
449   struct Session * s = NULL;
450   struct Session * t = NULL;
451   struct Plugin * plugin = NULL;
452
453   if (sc == NULL)
454     return;
455
456   s = sc->session;
457   plugin = s-> plugin;
458   if (sc->direction == _SEND)
459   {
460 #if VERBOSE_SERVER
461   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
462                    "Server: peer `%s' PUT on address `%s' disconnected\n",
463                    GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
464 #endif
465     s->server_send = NULL;
466     /* FIXME miminimize timeout here */
467     if (s->server_recv != NULL)
468     {
469       tc = s->server_recv;
470       tc->disconnect = GNUNET_YES;
471     }
472   }
473   if (sc->direction == _RECEIVE)
474   {
475 #if VERBOSE_SERVER
476   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
477                    "Server: peer `%s' GET on address `%s' disconnected\n",
478                    GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
479 #endif
480     s->server_recv = NULL;
481     //MHD_
482     if (s->server_send != NULL)
483     {
484       tc = s->server_send;
485       tc->disconnect = GNUNET_YES;
486     }
487   }
488   GNUNET_free (sc);
489
490   t = plugin->server_semi_head;
491   while (t != NULL)
492   {
493     if (t == s)
494     {
495       GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
496       GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
497       break;
498     }
499     t = t->next;
500   }
501   plugin->cur_connections--;
502
503   if ((s->server_send == NULL) && (s->server_recv == NULL))
504   {
505 #if VERBOSE_SERVER
506   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
507                    "Server: peer `%s' on address `%s' disconnected\n",
508                    GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
509 #endif
510     notify_session_end(s->plugin, &s->target, s);
511   }
512 }
513
514 int
515 server_disconnect (struct Session *s)
516 {
517   struct Plugin *plugin = s->plugin;
518   struct Session *t = plugin->head;
519
520   while (t != NULL)
521   {
522     if (t->inbound == GNUNET_YES)
523     {
524       if (t->server_send != NULL)
525       {
526         ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
527       }
528       if (t->server_send != NULL)
529       {
530         ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
531       }
532     }
533     t = t->next;
534   }
535
536
537   return GNUNET_OK;
538 }
539
540 int
541 server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
542 {
543   return GNUNET_OK;
544 }
545
546 /**
547  * Function that queries MHD's select sets and
548  * starts the task waiting for them.
549  * @param plugin plugin
550  * @param daemon_handle the MHD daemon handle
551  * @return gnunet task identifier
552  */
553 static GNUNET_SCHEDULER_TaskIdentifier
554 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
555
556 /**
557  * Call MHD IPv4 to process pending requests and then go back
558  * and schedule the next run.
559  * @param cls plugin as closure
560  * @param tc task context
561  */
562 static void
563 server_v4_run (void *cls,
564                            const struct GNUNET_SCHEDULER_TaskContext *tc)
565 {
566   struct Plugin *plugin = cls;
567   GNUNET_assert (cls != NULL);
568
569   plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
570
571   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
572     return;
573
574   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
575   plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
576 }
577
578
579 /**
580  * Call MHD IPv6 to process pending requests and then go back
581  * and schedule the next run.
582  * @param cls plugin as closure
583  * @param tc task context
584  */
585 static void
586 server_v6_run (void *cls,
587                            const struct GNUNET_SCHEDULER_TaskContext *tc)
588 {
589   struct Plugin *plugin = cls;
590   GNUNET_assert (cls != NULL);
591
592   plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
593
594   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
595     return;
596
597   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
598   plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
599 }
600
601 /**
602  * Function that queries MHD's select sets and
603  * starts the task waiting for them.
604  * @param plugin plugin
605  * @param daemon_handle the MHD daemon handle
606  * @return gnunet task identifier
607  */
608 static GNUNET_SCHEDULER_TaskIdentifier
609 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
610 {
611   GNUNET_SCHEDULER_TaskIdentifier ret;
612   fd_set rs;
613   fd_set ws;
614   fd_set es;
615   struct GNUNET_NETWORK_FDSet *wrs;
616   struct GNUNET_NETWORK_FDSet *wws;
617   struct GNUNET_NETWORK_FDSet *wes;
618   int max;
619   unsigned long long timeout;
620   int haveto;
621   struct GNUNET_TIME_Relative tv;
622
623   ret = GNUNET_SCHEDULER_NO_TASK;
624   FD_ZERO (&rs);
625   FD_ZERO (&ws);
626   FD_ZERO (&es);
627   wrs = GNUNET_NETWORK_fdset_create ();
628   wes = GNUNET_NETWORK_fdset_create ();
629   wws = GNUNET_NETWORK_fdset_create ();
630   max = -1;
631   GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
632   haveto = MHD_get_timeout (daemon_handle, &timeout);
633   if (haveto == MHD_YES)
634     tv.rel_value = (uint64_t) timeout;
635   else
636     tv = GNUNET_TIME_UNIT_SECONDS;
637   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
638   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
639   GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
640   if (daemon_handle == plugin->server_v4)
641   {
642     if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
643     {
644       GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
645       plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
646     }
647
648     ret =
649         GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
650                                      GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
651                                      &server_v4_run, plugin);
652   }
653   if (daemon_handle == plugin->server_v6)
654   {
655     if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
656     {
657       GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
658       plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
659     }
660
661     ret =
662         GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
663                                      GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
664                                      &server_v6_run, plugin);
665   }
666   GNUNET_NETWORK_fdset_destroy (wrs);
667   GNUNET_NETWORK_fdset_destroy (wws);
668   GNUNET_NETWORK_fdset_destroy (wes);
669   return ret;
670 }
671
672 int
673 server_start (struct Plugin *plugin)
674 {
675   int res = GNUNET_OK;
676
677 #if BUILD_HTTPS
678   res = server_load_certificate (plugin);
679   if (res == GNUNET_SYSERR)
680   {
681     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
682     return res;
683   }
684 #endif
685
686   plugin->server_v4 = NULL;
687   if (plugin->ipv4 == GNUNET_YES)
688   {
689     plugin->server_v4 = MHD_start_daemon (
690 #if VERBOSE_SERVER
691                                            MHD_USE_DEBUG |
692 #endif
693 #if BUILD_HTTPS
694                                            MHD_USE_SSL |
695 #endif
696                                            MHD_NO_FLAG, plugin->port,
697                                            &server_accept_cb, plugin,
698                                            &server_access_cb, plugin,
699                                            //MHD_OPTION_SOCK_ADDR,
700                                            //(struct sockaddr_in *)
701                                            //plugin->bind4_address,
702                                            MHD_OPTION_CONNECTION_LIMIT,
703                                            (unsigned int)
704                                            plugin->max_connections,
705 #if BUILD_HTTPS
706                                            MHD_OPTION_HTTPS_PRIORITIES,
707                                            plugin->crypto_init,
708                                            MHD_OPTION_HTTPS_MEM_KEY,
709                                            plugin->key,
710                                            MHD_OPTION_HTTPS_MEM_CERT,
711                                            plugin->cert,
712 #endif
713                                            MHD_OPTION_CONNECTION_TIMEOUT,
714                                            (unsigned int) 3,
715                                            MHD_OPTION_CONNECTION_MEMORY_LIMIT,
716                                            (size_t) (2 *
717                                                      GNUNET_SERVER_MAX_MESSAGE_SIZE),
718                                            MHD_OPTION_NOTIFY_COMPLETED,
719                                            &server_disconnect_cb, plugin,
720                                            MHD_OPTION_EXTERNAL_LOGGER,
721                                            server_log, NULL, MHD_OPTION_END);
722     if (plugin->server_v4 == NULL)
723       res = GNUNET_SYSERR;
724   }
725   plugin->server_v6 = NULL;
726   if (plugin->ipv6 == GNUNET_YES)
727   {
728     plugin->server_v6 = MHD_start_daemon (
729 #if VERBOSE_SERVER
730                                            MHD_USE_DEBUG |
731 #endif
732 #if BUILD_HTTPS
733                                            MHD_USE_SSL |
734 #endif
735                                            MHD_USE_IPv6, plugin->port,
736                                            &server_accept_cb, plugin,
737                                            &server_access_cb, plugin,
738                                            //MHD_OPTION_SOCK_ADDR,
739                                            //tmp,
740                                            MHD_OPTION_CONNECTION_LIMIT,
741                                            (unsigned int)
742                                            plugin->max_connections,
743 #if BUILD_HTTPS
744                                            MHD_OPTION_HTTPS_PRIORITIES,
745                                            plugin->crypto_init,
746                                            MHD_OPTION_HTTPS_MEM_KEY,
747                                            plugin->key,
748                                            MHD_OPTION_HTTPS_MEM_CERT,
749                                            plugin->cert,
750 #endif
751                                            MHD_OPTION_CONNECTION_TIMEOUT,
752                                            (unsigned int) 3,
753                                            MHD_OPTION_CONNECTION_MEMORY_LIMIT,
754                                            (size_t) (2 *
755                                                      GNUNET_SERVER_MAX_MESSAGE_SIZE),
756                                            MHD_OPTION_NOTIFY_COMPLETED,
757                                            &server_disconnect_cb, plugin,
758                                            MHD_OPTION_EXTERNAL_LOGGER,
759                                            server_log, NULL, MHD_OPTION_END);
760
761     if (plugin->server_v6 == NULL)
762       res = GNUNET_SYSERR;
763   }
764
765   if (plugin->server_v4 != NULL)
766     plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
767   if (plugin->server_v6 != NULL)
768     plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
769
770 #if DEBUG_HTTP
771   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
772                    "%s server component started on port %u\n", plugin->name,
773                    plugin->port);
774 #endif
775   return res;
776 }
777
778 void
779 server_stop (struct Plugin *plugin)
780 {
781   struct Session *s = NULL;
782   struct Session *t = NULL;
783
784   if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
785   {
786     GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
787     plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
788   }
789
790   if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
791   {
792     GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
793     plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
794   }
795
796   if (plugin->server_v4 != NULL)
797   {
798     MHD_stop_daemon (plugin->server_v4);
799     plugin->server_v4 = NULL;
800   }
801   if (plugin->server_v6 != NULL)
802   {
803     MHD_stop_daemon (plugin->server_v6);
804     plugin->server_v6 = NULL;
805   }
806
807   /* cleaning up semi-sessions never propagated */
808   s = plugin->server_semi_head;
809   while (s != NULL)
810   {
811     t = s->next;
812     delete_session (s);
813     s = t;
814   }
815
816 #if BUILD_HTTPS
817   GNUNET_free_non_null (plugin->crypto_init);
818   GNUNET_free_non_null (plugin->cert);
819   GNUNET_free_non_null (plugin->key);
820 #endif
821
822 #if DEBUG_HTTP
823   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
824                    "%s server component stopped\n", plugin->name);
825 #endif
826 }
827
828
829
830 /* end of plugin_transport_http.c */