2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/plugin_transport_http_server.c
23 * @brief HTTP/S server transport plugin
24 * @author Matthias Wachs
28 #include "gnunet_protocols.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_transport_plugin.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_nat_lib.h"
38 #include "plugin_transport_http_common.h"
39 #include "microhttpd.h"
42 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
43 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
45 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
46 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
49 #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>"
54 * Encapsulation of all of the state of the plugin.
59 * Session handle for connections.
64 * Stored in a linked list.
69 * Stored in a linked list.
74 * To whom are we talking to (set to our identity
75 * if we are still waiting for the welcome message)
77 struct GNUNET_PeerIdentity target;
80 * Pointer to the global plugin struct.
82 struct HTTP_Server_Plugin *plugin;
85 * next pointer for double linked list
87 struct HTTP_Message *msg_head;
90 * previous pointer for double linked list
92 struct HTTP_Message *msg_tail;
95 * Message stream tokenizer for incoming data
97 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
102 struct ServerConnection *server_recv;
107 struct ServerConnection *server_send;
120 * Unique HTTP/S connection tag for this connection
125 * ATS network type in NBO
127 uint32_t ats_address_network_type;
130 * Was session given to transport service?
135 * Absolute time when to receive data again
136 * Used for receive throttling
138 struct GNUNET_TIME_Absolute next_receive;
141 * Session timeout task
143 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
146 struct ServerConnection
151 /* Should this connection get disconnected? GNUNET_YES/NO */
154 /* The session this server connection belongs to */
155 struct Session *session;
157 /* The MHD connection */
158 struct MHD_Connection *mhd_conn;
161 struct MHD_Daemon *mhd_daemon;
165 * Encapsulation of all of the state of the plugin.
167 struct HTTP_Server_Plugin
172 struct GNUNET_TRANSPORT_PluginEnvironment *env;
175 * Linked list head of open sessions.
178 struct Session *head;
181 * Linked list tail of open sessions.
183 struct Session *tail;
198 char *external_hostname;
201 * Maximum number of sockets the plugin can use
202 * Each http inbound /outbound connections are two connections
204 unsigned int max_connections;
207 * Current number of sockets the plugin can use
208 * Each http inbound /outbound connections are two connections
210 unsigned int cur_connections;
213 * External hostname the plugin can be connected to, can be different to
214 * the host's FQDN, used e.g. for reverse proxying
219 * External address length
239 * Task calling transport service about external address
241 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
244 * NAT handle & address management
246 struct GNUNET_NAT_Handle *nat;
249 * List of own addresses
253 * IPv4 addresses DLL head
255 struct HttpAddressWrapper *addr_head;
258 * IPv4 addresses DLL tail
260 struct HttpAddressWrapper *addr_tail;
263 * IPv4 server socket to bind to
265 struct sockaddr_in *server_addr_v4;
268 * IPv6 server socket to bind to
270 struct sockaddr_in6 *server_addr_v6;
275 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
280 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
283 * The IPv4 server is scheduled to run asap
285 int server_v4_immediately;
288 * The IPv6 server is scheduled to run asap
290 int server_v6_immediately;
295 struct MHD_Daemon *server_v4;
300 struct MHD_Daemon *server_v6;
308 * Use RC4-128 instead of AES:
309 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
328 * Wrapper to manage addresses
330 struct HttpAddressWrapper
335 struct HttpAddressWrapper *next;
338 * Linked list previous
340 struct HttpAddressWrapper *prev;
348 * Message to send using http
353 * next pointer for double linked list
355 struct HTTP_Message *next;
358 * previous pointer for double linked list
360 struct HTTP_Message *prev;
363 * buffer containing data to send
368 * amount of data already sent
378 * Continuation function to call once the transmission buffer
379 * has again space available. NULL if there is no
380 * continuation to call.
382 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
385 * Closure for transmit_cont.
387 void *transmit_cont_cls;
391 static struct HTTP_Server_Plugin * p;
394 * Start session timeout
397 server_start_session_timeout (struct Session *s);
400 * Increment session timeout due to activity
403 server_reschedule_session_timeout (struct Session *s);
409 server_stop_session_timeout (struct Session *s);
412 * Disconnect a session
415 server_disconnect (struct Session *s);
418 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
421 * Reschedule the execution of both IPv4 and IPv6 server
422 * @param plugin the plugin
423 * @param server which server to schedule v4 or v6?
424 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
428 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server, int now);
431 * Function that can be used by the transport service to transmit
432 * a message using the plugin. Note that in the case of a
433 * peer disconnecting, the continuation MUST be called
434 * prior to the disconnect notification itself. This function
435 * will be called with this peer's HELLO message to initiate
436 * a fresh connection to another peer.
439 * @param session which session must be used
440 * @param msgbuf the message to transmit
441 * @param msgbuf_size number of bytes in 'msgbuf'
442 * @param priority how important is the message (most plugins will
443 * ignore message priority and just FIFO)
444 * @param to how long to wait at most for the transmission (does not
445 * require plugins to discard the message after the timeout,
446 * just advisory for the desired delay; most plugins will ignore
448 * @param cont continuation to call once the message has
449 * been transmitted (or if the transport is ready
450 * for the next transmission call; or if the
451 * peer disconnected...); can be NULL
452 * @param cont_cls closure for cont
453 * @return number of bytes used (on the physical network, with overheads);
454 * -1 on hard errors (i.e. address invalid); 0 is a legal value
455 * and does NOT mean that the message was not transmitted (DV)
458 http_server_plugin_send (void *cls,
459 struct Session *session,
460 const char *msgbuf, size_t msgbuf_size,
461 unsigned int priority,
462 struct GNUNET_TIME_Relative to,
463 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
465 struct HTTP_Server_Plugin *plugin = cls;
466 struct HTTP_Message *msg;
469 GNUNET_assert (plugin != NULL);
470 GNUNET_assert (session != NULL);
472 if (GNUNET_NO == server_exist_session (plugin, session))
475 return GNUNET_SYSERR;
477 GNUNET_assert (NULL != session->server_send);
479 if (GNUNET_YES == session->server_send->disconnect)
480 return GNUNET_SYSERR;
482 /* create new message and schedule */
483 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
484 msg = GNUNET_malloc (bytes_sent);
486 msg->size = msgbuf_size;
488 msg->buf = (char *) &msg[1];
489 msg->transmit_cont = cont;
490 msg->transmit_cont_cls = cont_cls;
491 memcpy (msg->buf, msgbuf, msgbuf_size);
493 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
495 server_reschedule (session->plugin, session->server_send->mhd_daemon, GNUNET_YES);
496 server_reschedule_session_timeout (session);
498 /* struct Plugin *plugin = cls; */
505 * Function that can be used to force the plugin to disconnect
506 * from the given peer and cancel all previous transmissions
507 * (and their continuationc).
510 * @param target peer from which to disconnect
513 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
515 struct HTTP_Server_Plugin *plugin = cls;
516 struct Session *next = NULL;
517 struct Session *pos = NULL;
519 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
520 "Transport tells me to disconnect `%s'\n",
521 GNUNET_i2s (target));
524 while (NULL != (pos = next))
527 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
529 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
530 "Disconnecting session %p to `%s'\n",
531 pos, GNUNET_i2s (target));
532 GNUNET_assert (GNUNET_OK == server_disconnect (pos));
540 * Another peer has suggested an address for this
541 * peer and transport plugin. Check that this could be a valid
542 * address. If so, consider adding it to the list
546 * @param addr pointer to the address
547 * @param addrlen length of addr
548 * @return GNUNET_OK if this is a plausible address for this peer
552 http_server_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
554 struct HTTP_Server_Plugin *plugin = cls;
555 struct HttpAddressWrapper *next;
556 struct HttpAddressWrapper *pos;
559 if ((NULL != plugin->ext_addr) && GNUNET_YES == (http_common_cmp_addresses (addr, addrlen, plugin->ext_addr, plugin->ext_addr_len)))
562 next = plugin->addr_head;
563 while (NULL != (pos = next))
566 if (GNUNET_YES == (http_common_cmp_addresses(addr,
578 * Creates a new outbound session the transport
579 * service will use to send data to the peer
581 * Since HTTP/S server cannot create sessions, always return NULL
583 * @param cls the plugin
584 * @param address the address
585 * @return always NULL
587 static struct Session *
588 http_server_plugin_get_session (void *cls,
589 const struct GNUNET_HELLO_Address *address)
596 * Deleting the session
597 * Must not be used afterwards
601 server_delete_session (struct Session *s)
603 struct HTTP_Server_Plugin *plugin = s->plugin;
604 server_stop_session_timeout(s);
606 if (GNUNET_YES == s->session_passed)
607 plugin->env->session_end (plugin->env->cls, &s->target, s);
609 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
610 struct HTTP_Message *msg = s->msg_head;
611 struct HTTP_Message *tmp = NULL;
617 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
618 if (msg->transmit_cont != NULL)
620 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
626 if (s->msg_tk != NULL)
628 GNUNET_SERVER_mst_destroy (s->msg_tk);
631 GNUNET_free (s->addr);
632 GNUNET_free_non_null (s->server_recv);
633 GNUNET_free_non_null (s->server_send);
636 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
637 "Session %p destroyed\n", s);
645 server_stop_session_timeout (struct Session *s)
647 GNUNET_assert (NULL != s);
649 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
651 GNUNET_SCHEDULER_cancel (s->timeout_task);
652 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
653 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
658 * Function that queries MHD's select sets and
659 * starts the task waiting for them.
660 * @param plugin plugin
661 * @param daemon_handle the MHD daemon handle
662 * @return gnunet task identifier
664 static GNUNET_SCHEDULER_TaskIdentifier
665 server_schedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *daemon_handle,
669 * Reschedule the execution of both IPv4 and IPv6 server
670 * @param plugin the plugin
671 * @param server which server to schedule v4 or v6?
672 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
676 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server, int now)
678 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
680 if (GNUNET_YES == plugin->server_v4_immediately)
681 return; /* No rescheduling, server will run asap */
683 if (GNUNET_YES == now)
684 plugin->server_v4_immediately = GNUNET_YES;
686 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
688 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
689 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
691 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
694 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
696 if (GNUNET_YES == plugin->server_v6_immediately)
697 return; /* No rescheduling, server will run asap */
699 if (GNUNET_YES == now)
700 plugin->server_v6_immediately = GNUNET_YES;
702 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
704 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
705 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
707 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
712 server_disconnect (struct Session *s)
714 struct ServerConnection * send;
715 struct ServerConnection * recv;
717 send = (struct ServerConnection *) s->server_send;
718 if (s->server_send != NULL)
720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
721 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
722 s, s->server_send, GNUNET_i2s (&s->target));
724 send->disconnect = GNUNET_YES;
725 #if MHD_VERSION >= 0x00090E00
726 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
729 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
732 recv = (struct ServerConnection *) s->server_recv;
735 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
736 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
737 s, s->server_recv, GNUNET_i2s (&s->target));
739 recv->disconnect = GNUNET_YES;
740 #if MHD_VERSION >= 0x00090E00
741 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
744 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
750 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
752 #if MHD_VERSION >= 0x00090E00
753 /* Setting timeouts for other connections */
754 if (s->server_recv != NULL)
756 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
757 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
758 MHD_set_connection_option (s->server_recv->mhd_conn,
759 MHD_CONNECTION_OPTION_TIMEOUT,
761 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
763 if (s->server_send != NULL)
765 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
766 "Setting timeout for %p to %u sec.\n", s->server_send, to);
767 MHD_set_connection_option (s->server_send->mhd_conn,
768 MHD_CONNECTION_OPTION_TIMEOUT,
770 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
776 static struct ServerConnection *
777 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
778 struct MHD_Connection *mhd_connection, const char *url,
781 struct Session *s = NULL;
782 struct ServerConnection *sc = NULL;
783 const union MHD_ConnectionInfo *conn_info;
784 struct GNUNET_ATS_Information ats;
789 struct GNUNET_PeerIdentity target;
791 int direction = GNUNET_SYSERR;
794 /* url parsing variables */
802 conn_info = MHD_get_connection_info (mhd_connection,
803 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
804 if ((conn_info->client_addr->sa_family != AF_INET) &&
805 (conn_info->client_addr->sa_family != AF_INET6))
808 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
809 "New %s connection from %s\n", method, url);
811 * URL is valid if it is in the form [peerid[103];tag]*/
812 url_len = strlen (url);
813 url_end = (char *) &url[url_len];
818 goto error; /* too short */
820 hash_start = strrchr (url, '/');
821 if (NULL == hash_start)
824 goto error; /* '/' delimiter not found */
826 if (hash_start >= url_end)
829 goto error; /* mal formed */
833 hash_end = strrchr (hash_start, ';');
834 if (NULL == hash_end)
837 goto error; /* ';' delimiter not found */
840 if (hash_end >= url_end)
843 goto error; /* mal formed */
846 if (hash_start >= hash_end)
849 goto error; /* mal formed */
852 if ((strlen(hash_start) - strlen(hash_end)) != 103)
855 goto error; /* invalid hash length */
859 memcpy (hash, hash_start, 103);
861 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target.hashPubKey)))
864 goto error; /* mal formed */
867 if (hash_end >= url_end)
870 goto error; /* mal formed */
873 tag_start = &hash_end[1];
876 tag = strtoul (tag_start, &tag_end, 10);
880 goto error; /* mal formed */
885 goto error; /* mal formed */
887 if (tag_end != url_end)
890 goto error; /* mal formed */
893 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
894 direction = _RECEIVE;
895 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
902 plugin->cur_connections++;
903 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
904 "New %s connection from %s with tag %u (%u of %u)\n",
906 GNUNET_i2s (&target), tag,
907 plugin->cur_connections, plugin->max_connections);
909 /* find duplicate session */
913 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
920 if ((_RECEIVE == direction) && (NULL != s->server_recv))
922 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
923 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
924 GNUNET_i2s (&target),
928 if ((_SEND == direction) && (NULL != s->server_send))
930 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
931 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
932 GNUNET_i2s (&target),
939 /* create new session */
940 switch (conn_info->client_addr->sa_family)
943 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
944 addr_len = http_common_address_get_size (addr);
945 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
948 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
949 addr_len = http_common_address_get_size (addr);
950 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
957 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
958 "Creating new session for peer `%s' connecting from `%s'\n",
959 GNUNET_i2s (&target),
960 http_common_plugin_address_to_string (NULL, addr, addr_len));
962 s = GNUNET_malloc (sizeof (struct Session));
963 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
966 s->addrlen = addr_len;
967 s->ats_address_network_type = ats.value;
968 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
970 s->server_recv = NULL;
971 s->server_send = NULL;
972 s->session_passed = GNUNET_NO;
973 server_start_session_timeout(s);
974 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
977 sc = GNUNET_malloc (sizeof (struct ServerConnection));
978 if (conn_info->client_addr->sa_family == AF_INET)
979 sc->mhd_daemon = plugin->server_v4;
980 if (conn_info->client_addr->sa_family == AF_INET6)
981 sc->mhd_daemon = plugin->server_v6;
982 sc->mhd_conn = mhd_connection;
983 sc->direction = direction;
985 if (direction == _SEND)
987 if (direction == _RECEIVE)
990 #if MHD_VERSION >= 0x00090E00
991 if ((NULL == s->server_recv) || (NULL == s->server_send))
993 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
994 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
995 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
999 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1000 "Session %p for peer `%s' fully connected\n",
1001 s, GNUNET_i2s (&target));
1002 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1003 server_mhd_connection_timeout (plugin, s, to);
1006 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1007 "Setting timeout for %p to %u sec.\n", sc, to);
1011 /* Error condition */
1013 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1014 "Invalid connection request\n");
1018 static struct Session *
1019 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1020 struct ServerConnection * sc)
1024 for (s = plugin->head; NULL != s; s = s->next)
1025 if ((s->server_recv == sc) || (s->server_send == sc))
1031 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1033 struct Session * head;
1035 GNUNET_assert (NULL != plugin);
1036 GNUNET_assert (NULL != s);
1038 for (head = plugin->head; head != NULL; head = head->next)
1048 * Callback called by MHD when it needs data to send
1049 * @param cls current session
1050 * @param pos position in buffer
1051 * @param buf the buffer to write data to
1052 * @param max max number of bytes available in buffer
1053 * @return bytes written to buffer
1056 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1058 struct Session *s = cls;
1059 ssize_t bytes_read = 0;
1060 struct HTTP_Message *msg;
1062 GNUNET_assert (NULL != p);
1063 if (GNUNET_NO == server_exist_session (p, s))
1069 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1071 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1072 msg->pos += bytes_read;
1074 /* removing message */
1075 if (msg->pos == msg->size)
1077 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1078 if (NULL != msg->transmit_cont)
1079 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
1083 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1084 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1092 * Callback called by MessageStreamTokenizer when a message has arrived
1093 * @param cls current session as closure
1094 * @param client clien
1095 * @param message the message to be forwarded to transport service
1098 server_receive_mst_cb (void *cls, void *client,
1099 const struct GNUNET_MessageHeader *message)
1101 struct Session *s = cls;
1102 struct GNUNET_ATS_Information atsi[2];
1103 struct GNUNET_TIME_Relative delay;
1105 GNUNET_assert (NULL != p);
1106 if (GNUNET_NO == server_exist_session(p, s))
1109 struct HTTP_Server_Plugin *plugin = s->plugin;
1111 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1112 atsi[0].value = htonl (1);
1113 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1114 atsi[1].value = s->ats_address_network_type;
1115 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1117 delay = plugin->env->receive (plugin->env->cls,
1120 (const struct GNUNET_ATS_Information *) &atsi, 2,
1121 s, s->addr, s->addrlen);
1122 s->session_passed = GNUNET_YES;
1123 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1124 if (delay.rel_value > 0)
1126 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1127 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1128 GNUNET_i2s (&s->target),
1129 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1132 server_reschedule_session_timeout (s);
1137 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1138 const char *url, const char *method, const char *version,
1139 const char *upload_data, size_t * upload_data_size,
1140 void **httpSessionCache)
1142 struct HTTP_Server_Plugin *plugin = cls;
1145 struct ServerConnection *sc = *httpSessionCache;
1147 struct MHD_Response *response;
1149 GNUNET_assert (cls != NULL);
1152 /* new connection */
1153 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1155 (*httpSessionCache) = sc;
1158 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1159 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1160 MHD_destroy_response (response);
1166 /* 'old' connection */
1167 if (NULL == server_lookup_session (plugin, sc))
1169 /* Session was already disconnected */
1174 /* existing connection */
1175 sc = (*httpSessionCache);
1178 GNUNET_assert (NULL != s);
1179 /* connection is to be disconnected */
1180 if (sc->disconnect == GNUNET_YES)
1182 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1183 response = MHD_create_response_from_data (strlen ("Thank you!"),
1186 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1187 MHD_destroy_response (response);
1191 GNUNET_assert (s != NULL);
1192 /* Check if both directions are connected */
1193 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
1195 /* Delayed read from since not both semi-connections are connected */
1199 if (sc->direction == _SEND)
1201 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1203 &server_send_callback, s,
1205 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1206 MHD_destroy_response (response);
1209 if (sc->direction == _RECEIVE)
1211 if (*upload_data_size == 0)
1213 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1214 "Peer `%s' PUT on address `%s' connected\n",
1215 GNUNET_i2s (&s->target),
1216 http_common_plugin_address_to_string (NULL,
1222 /* Receiving data */
1223 if ((*upload_data_size > 0))
1225 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1226 "Peer `%s' PUT on address `%s' received %u bytes\n",
1227 GNUNET_i2s (&s->target),
1228 http_common_plugin_address_to_string (NULL,
1232 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1234 if ((s->next_receive.abs_value <= now.abs_value))
1236 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1237 "PUT with %u bytes forwarded to MST\n",
1239 if (s->msg_tk == NULL)
1241 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1243 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1244 *upload_data_size, GNUNET_NO, GNUNET_NO);
1245 #if MHD_VERSION >= 0x00090E00
1246 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1248 (*upload_data_size) = 0;
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "%p no inbound bandwidth available! Next read was delayed by %llu ms\n",
1254 s, now.abs_value - s->next_receive.abs_value);
1265 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1266 void **httpSessionCache)
1268 struct ServerConnection *sc = *httpSessionCache;
1269 struct Session *s = NULL;
1270 struct Session *t = NULL;
1271 struct HTTP_Server_Plugin *plugin = NULL;
1276 if (NULL == (s = server_lookup_session (p, sc)))
1279 GNUNET_assert (NULL != p);
1280 for (t = p->head; t != NULL; t = t->next)
1289 if (sc->direction == _SEND)
1292 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1293 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1294 GNUNET_i2s (&s->target), s->server_send,
1295 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1296 s->server_send = NULL;
1297 if (NULL != (s->server_recv))
1299 s->server_recv->disconnect = GNUNET_YES;
1300 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1301 #if MHD_VERSION >= 0x00090E00
1302 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1305 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1308 if (sc->direction == _RECEIVE)
1310 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1311 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1312 GNUNET_i2s (&s->target), s->server_recv,
1313 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1314 s->server_recv = NULL;
1315 if (NULL != (s->server_send))
1317 s->server_send->disconnect = GNUNET_YES;
1318 GNUNET_assert (NULL != s->server_send->mhd_conn);
1319 #if MHD_VERSION >= 0x00090E00
1320 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1323 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1325 if (s->msg_tk != NULL)
1327 GNUNET_SERVER_mst_destroy (s->msg_tk);
1333 plugin->cur_connections--;
1334 if ((s->server_send == NULL) && (s->server_recv == NULL))
1336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1337 "Peer `%s' on address `%s' disconnected\n",
1338 GNUNET_i2s (&s->target),
1339 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1341 server_delete_session (s);
1346 * Check if incoming connection is accepted.
1347 * NOTE: Here every connection is accepted
1348 * @param cls plugin as closure
1349 * @param addr address of incoming connection
1350 * @param addr_len address length of incoming connection
1351 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1355 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1357 struct HTTP_Server_Plugin *plugin = cls;
1359 if (plugin->cur_connections <= plugin->max_connections)
1363 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1364 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1365 plugin->max_connections);
1371 server_log (void *arg, const char *fmt, va_list ap)
1375 vsnprintf (text, sizeof (text), fmt, ap);
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1382 * Call MHD IPv4 to process pending requests and then go back
1383 * and schedule the next run.
1384 * @param cls plugin as closure
1385 * @param tc task context
1388 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1390 struct HTTP_Server_Plugin *plugin = cls;
1392 GNUNET_assert (cls != NULL);
1394 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1395 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1398 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1399 "Running IPv4 server\n");
1401 plugin->server_v4_immediately = GNUNET_NO;
1402 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1403 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1408 * Call MHD IPv6 to process pending requests and then go back
1409 * and schedule the next run.
1410 * @param cls plugin as closure
1411 * @param tc task context
1414 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1416 struct HTTP_Server_Plugin *plugin = cls;
1418 GNUNET_assert (cls != NULL);
1419 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1420 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1424 "Running IPv6 server\n");
1426 plugin->server_v6_immediately = GNUNET_NO;
1427 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1428 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1433 * Function that queries MHD's select sets and
1434 * starts the task waiting for them.
1435 * @param plugin plugin
1436 * @param daemon_handle the MHD daemon handle
1437 * @return gnunet task identifier
1439 static GNUNET_SCHEDULER_TaskIdentifier
1440 server_schedule (struct HTTP_Server_Plugin *plugin,
1441 struct MHD_Daemon *daemon_handle,
1444 GNUNET_SCHEDULER_TaskIdentifier ret;
1448 struct GNUNET_NETWORK_FDSet *wrs;
1449 struct GNUNET_NETWORK_FDSet *wws;
1450 struct GNUNET_NETWORK_FDSet *wes;
1452 unsigned MHD_LONG_LONG timeout;
1453 static unsigned long long last_timeout = 0;
1456 struct GNUNET_TIME_Relative tv;
1458 ret = GNUNET_SCHEDULER_NO_TASK;
1462 wrs = GNUNET_NETWORK_fdset_create ();
1463 wes = GNUNET_NETWORK_fdset_create ();
1464 wws = GNUNET_NETWORK_fdset_create ();
1466 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1467 haveto = MHD_get_timeout (daemon_handle, &timeout);
1468 if (haveto == MHD_YES)
1470 if (timeout != last_timeout)
1473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1474 "SELECT Timeout changed from %llu to %llu\n",
1475 last_timeout, timeout);
1477 last_timeout = timeout;
1479 tv.rel_value = (uint64_t) timeout;
1482 tv = GNUNET_TIME_UNIT_SECONDS;
1483 /* Force immediate run, since we have outbound data to send */
1484 if (now == GNUNET_YES)
1485 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1486 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1487 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1488 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1490 if (daemon_handle == plugin->server_v4)
1492 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1494 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1495 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1498 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1499 "Scheduling IPv4 server task in %llu ms\n", tv);
1502 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1504 &server_v4_run, plugin);
1506 if (daemon_handle == plugin->server_v6)
1508 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1510 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1511 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1514 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1515 "Scheduling IPv6 server task in %llu ms\n", tv);
1518 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1520 &server_v6_run, plugin);
1522 GNUNET_NETWORK_fdset_destroy (wrs);
1523 GNUNET_NETWORK_fdset_destroy (wws);
1524 GNUNET_NETWORK_fdset_destroy (wes);
1531 server_load_file (const char *file)
1533 struct GNUNET_DISK_FileHandle *gn_file;
1537 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1538 &fsize, GNUNET_NO, GNUNET_YES))
1540 text = GNUNET_malloc (fsize + 1);
1542 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1543 GNUNET_DISK_PERM_USER_READ);
1544 if (gn_file == NULL)
1549 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1552 GNUNET_DISK_file_close (gn_file);
1556 GNUNET_DISK_file_close (gn_file);
1565 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1567 int res = GNUNET_OK;
1572 /* Get crypto init string from config
1573 * If not present just use default values */
1576 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1579 &plugin->crypto_init))
1580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1581 "Using crypto init string `%s'\n",
1582 plugin->crypto_init);
1584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1585 "Using default crypto init string \n");
1588 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1589 "KEY_FILE", &key_file))
1591 key_file = GNUNET_strdup ("https_key.key");
1595 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1596 "CERT_FILE", &cert_file))
1598 GNUNET_asprintf (&cert_file, "%s", "https_cert.crt");
1601 /* read key & certificates from file */
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1604 key_file, cert_file);
1606 plugin->key = server_load_file (key_file);
1607 plugin->cert = server_load_file (cert_file);
1609 if ((plugin->key == NULL) || (plugin->cert == NULL))
1611 struct GNUNET_OS_Process *cert_creation;
1613 GNUNET_free_non_null (plugin->key);
1615 GNUNET_free_non_null (plugin->cert);
1616 plugin->cert = NULL;
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "No usable TLS certificate found, creating certificate\n");
1622 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1623 "gnunet-transport-certificate-creation",
1624 "gnunet-transport-certificate-creation",
1625 key_file, cert_file, NULL);
1626 if (cert_creation == NULL)
1628 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1630 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1631 GNUNET_free (key_file);
1632 GNUNET_free (cert_file);
1634 GNUNET_free_non_null (plugin->key);
1636 GNUNET_free_non_null (plugin->cert);
1637 plugin->cert = NULL;
1638 GNUNET_free_non_null (plugin->crypto_init);
1639 plugin->crypto_init = NULL;
1641 return GNUNET_SYSERR;
1643 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1644 GNUNET_OS_process_destroy (cert_creation);
1646 plugin->key = server_load_file (key_file);
1647 plugin->cert = server_load_file (cert_file);
1650 if ((plugin->key == NULL) || (plugin->cert == NULL))
1652 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1654 ("No usable TLS certificate found and creating one failed!\n"),
1656 GNUNET_free (key_file);
1657 GNUNET_free (cert_file);
1659 GNUNET_free_non_null (plugin->key);
1661 GNUNET_free_non_null (plugin->cert);
1662 plugin->cert = NULL;
1663 GNUNET_free_non_null (plugin->crypto_init);
1664 plugin->crypto_init = NULL;
1666 return GNUNET_SYSERR;
1668 GNUNET_free (key_file);
1669 GNUNET_free (cert_file);
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1676 server_start (struct HTTP_Server_Plugin *plugin)
1678 unsigned int timeout;
1679 GNUNET_assert (NULL != plugin);
1682 if (GNUNET_SYSERR == server_load_certificate (plugin))
1684 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1685 "Could not load or create server certificate! Loading plugin failed!\n");
1686 return GNUNET_SYSERR;
1691 #if MHD_VERSION >= 0x00090E00
1692 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1693 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1694 "MHD can set timeout per connection! Default time out %u sec.\n",
1697 timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
1698 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1699 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1702 plugin->server_v4 = NULL;
1703 if (plugin->use_ipv4 == GNUNET_YES)
1705 plugin->server_v4 = MHD_start_daemon (
1712 MHD_NO_FLAG, plugin->port,
1713 &server_accept_cb, plugin,
1714 &server_access_cb, plugin,
1715 MHD_OPTION_SOCK_ADDR,
1716 (struct sockaddr_in *)
1717 plugin->server_addr_v4,
1718 MHD_OPTION_CONNECTION_LIMIT,
1720 plugin->max_connections,
1722 MHD_OPTION_HTTPS_PRIORITIES,
1723 plugin->crypto_init,
1724 MHD_OPTION_HTTPS_MEM_KEY,
1726 MHD_OPTION_HTTPS_MEM_CERT,
1729 MHD_OPTION_CONNECTION_TIMEOUT,
1731 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1733 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1734 MHD_OPTION_NOTIFY_COMPLETED,
1735 &server_disconnect_cb, plugin,
1736 MHD_OPTION_EXTERNAL_LOGGER,
1737 server_log, NULL, MHD_OPTION_END);
1739 plugin->server_v6 = NULL;
1740 if (plugin->use_ipv6 == GNUNET_YES)
1742 plugin->server_v6 = MHD_start_daemon (
1749 MHD_USE_IPv6, plugin->port,
1750 &server_accept_cb, plugin,
1751 &server_access_cb, plugin,
1752 MHD_OPTION_SOCK_ADDR,
1753 (struct sockaddr_in6 *)
1754 plugin->server_addr_v6,
1755 MHD_OPTION_CONNECTION_LIMIT,
1757 plugin->max_connections,
1759 MHD_OPTION_HTTPS_PRIORITIES,
1760 plugin->crypto_init,
1761 MHD_OPTION_HTTPS_MEM_KEY,
1763 MHD_OPTION_HTTPS_MEM_CERT,
1766 MHD_OPTION_CONNECTION_TIMEOUT,
1768 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1770 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1771 MHD_OPTION_NOTIFY_COMPLETED,
1772 &server_disconnect_cb, plugin,
1773 MHD_OPTION_EXTERNAL_LOGGER,
1774 server_log, NULL, MHD_OPTION_END);
1778 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1780 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1781 "Failed to start %s IPv4 server component on port %u\n",
1782 plugin->name, plugin->port);
1783 return GNUNET_SYSERR;
1785 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1787 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1789 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1790 "Failed to start %s IPv6 server component on port %u\n",
1791 plugin->name, plugin->port);
1792 return GNUNET_SYSERR;
1794 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1795 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1796 "%s server component started on port %u\n", plugin->name,
1803 server_stop (struct HTTP_Server_Plugin *plugin)
1805 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1807 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1808 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1811 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1813 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1814 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1817 if (plugin->server_v4 != NULL)
1819 MHD_stop_daemon (plugin->server_v4);
1820 plugin->server_v4 = NULL;
1822 if ( plugin->server_v6 != NULL)
1824 MHD_stop_daemon (plugin->server_v6);
1825 plugin->server_v6 = NULL;
1831 GNUNET_free_non_null (plugin->crypto_init);
1832 GNUNET_free_non_null (plugin->cert);
1833 GNUNET_free_non_null (plugin->key);
1836 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1837 "%s server component stopped\n", plugin->name);
1841 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
1844 struct HTTP_Server_Plugin *plugin = cls;
1845 struct HttpAddressWrapper *w = NULL;
1847 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
1848 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
1849 if (NULL == w->addr)
1854 w->addrlen = http_common_address_get_size (w->addr);
1856 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
1857 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1858 "Notifying transport to add address `%s'\n",
1859 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
1861 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
1866 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
1869 struct HTTP_Server_Plugin *plugin = cls;
1870 struct HttpAddressWrapper *w = plugin->addr_head;
1872 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
1875 saddr_len = http_common_address_get_size (saddr);
1879 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
1883 GNUNET_free (saddr);
1888 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1889 "Notifying transport to remove address `%s'\n",
1890 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
1891 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
1892 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
1893 GNUNET_free (w->addr);
1900 * Our external IP address/port mapping has changed.
1902 * @param cls closure, the 'struct LocalAddrList'
1903 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1904 * the previous (now invalid) one
1905 * @param addr either the previous or the new public IP address
1906 * @param addrlen actual lenght of the address
1909 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
1912 GNUNET_assert (cls != NULL);
1913 struct HTTP_Server_Plugin *plugin = cls;
1915 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1916 "NAT called to %s address `%s'\n",
1917 (add_remove == GNUNET_NO) ? "remove" : "add",
1918 GNUNET_a2s (addr, addrlen));
1920 if (AF_INET == addr->sa_family)
1922 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
1924 if (GNUNET_NO == plugin->use_ipv4)
1927 if ((NULL != plugin->server_addr_v4) &&
1928 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
1929 &s4->sin_addr, sizeof (struct in_addr))))
1931 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1932 "Skipping address `%s' (not bindto address)\n",
1933 GNUNET_a2s (addr, addrlen));
1938 if (AF_INET6 == addr->sa_family)
1940 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
1941 if (GNUNET_NO == plugin->use_ipv6)
1944 if ((NULL != plugin->server_addr_v6) &&
1945 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
1946 &s6->sin6_addr, sizeof (struct in6_addr))))
1948 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1949 "Skipping address `%s' (not bindto address)\n",
1950 GNUNET_a2s (addr, addrlen));
1958 server_add_address (cls, add_remove, addr, addrlen);
1961 server_remove_address (cls, add_remove, addr, addrlen);
1968 server_get_addresses (struct HTTP_Server_Plugin *plugin,
1969 const char *serviceName,
1970 const struct GNUNET_CONFIGURATION_Handle *cfg,
1971 struct sockaddr ***addrs, socklen_t ** addr_lens)
1974 unsigned long long port;
1975 struct addrinfo hints;
1976 struct addrinfo *res;
1977 struct addrinfo *pos;
1978 struct addrinfo *next;
1982 struct sockaddr **saddrs;
1983 socklen_t *saddrlens;
1989 disablev6 = !plugin->use_ipv6;
1992 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
1994 GNUNET_break (GNUNET_OK ==
1995 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
1999 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2001 ("Require valid port number for service in configuration!\n"));
2002 return GNUNET_SYSERR;
2007 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2008 "Starting in listen only mode\n");
2009 return -1; /* listen only */
2013 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2015 GNUNET_break (GNUNET_OK ==
2016 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2017 "BINDTO", &hostname));
2022 if (hostname != NULL)
2024 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2025 "Resolving `%s' since that is where `%s' will bind to.\n",
2026 hostname, serviceName);
2027 memset (&hints, 0, sizeof (struct addrinfo));
2029 hints.ai_family = AF_INET;
2030 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2034 hostname, gai_strerror (ret));
2035 GNUNET_free (hostname);
2036 return GNUNET_SYSERR;
2040 while (NULL != (pos = next))
2042 next = pos->ai_next;
2043 if ((disablev6) && (pos->ai_family == AF_INET6))
2049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2050 _("Failed to find %saddress for `%s'.\n"),
2051 disablev6 ? "IPv4 " : "", hostname);
2053 GNUNET_free (hostname);
2054 return GNUNET_SYSERR;
2057 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2058 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2061 while (NULL != (pos = next))
2063 next = pos->ai_next;
2064 if ((disablev6) && (pos->ai_family == AF_INET6))
2066 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2067 continue; /* not TCP */
2068 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2069 continue; /* huh? */
2070 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2071 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2073 if (pos->ai_family == AF_INET)
2075 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2076 saddrlens[i] = pos->ai_addrlen;
2077 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2078 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2079 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2083 GNUNET_assert (pos->ai_family == AF_INET6);
2084 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2085 saddrlens[i] = pos->ai_addrlen;
2086 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2087 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2088 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2092 GNUNET_free (hostname);
2098 /* will bind against everything, just set port */
2104 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2105 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2107 saddrlens[i] = sizeof (struct sockaddr_in);
2108 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2109 #if HAVE_SOCKADDR_IN_SIN_LEN
2110 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2112 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2113 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2119 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2120 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2122 saddrlens[i] = sizeof (struct sockaddr_in6);
2123 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2124 #if HAVE_SOCKADDR_IN_SIN_LEN
2125 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2127 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2128 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2130 saddrlens[i] = sizeof (struct sockaddr_in);
2131 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2132 #if HAVE_SOCKADDR_IN_SIN_LEN
2133 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2135 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2136 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2140 *addr_lens = saddrlens;
2145 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2147 int res = GNUNET_OK;
2148 struct sockaddr **addrs;
2149 socklen_t *addrlens;
2151 res = server_get_addresses (plugin,
2152 plugin->name, plugin->env->cfg,
2154 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2155 _("Found %u addresses to report to NAT service\n"), res);
2157 if (GNUNET_SYSERR == res)
2164 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2166 (const struct sockaddr **) addrs, addrlens,
2167 &server_nat_port_map_callback, NULL, plugin);
2171 GNUNET_assert (addrs[res] != NULL);
2172 GNUNET_free (addrs[res]);
2174 GNUNET_free_non_null (addrs);
2175 GNUNET_free_non_null (addrlens);
2180 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2182 /* Stop NAT handle */
2183 if (NULL != plugin->nat)
2184 GNUNET_NAT_unregister (plugin->nat);
2186 /* Clean up addresses */
2187 struct HttpAddressWrapper *w;
2189 while (plugin->addr_head != NULL)
2191 w = plugin->addr_head;
2192 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2193 GNUNET_free (w->addr);
2200 * Check if IPv6 supported on this system
2203 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2205 struct GNUNET_NETWORK_Handle *desc = NULL;
2206 int res = GNUNET_NO;
2208 /* Probe IPv6 support */
2209 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2212 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2215 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2217 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2219 ("Disabling IPv6 since it is not supported on this system!\n"));
2224 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2228 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2229 "Testing IPv6 on this system: %s\n",
2230 (res == GNUNET_YES) ? "successful" : "failed");
2236 * Function called when the service shuts down. Unloads our plugins
2237 * and cancels pending validations.
2239 * @param cls closure, unused
2240 * @param tc task context (unused)
2243 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2245 struct HTTP_Server_Plugin *plugin = cls;
2247 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2249 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2252 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol, plugin->external_hostname);
2253 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2254 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2255 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2256 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2257 plugin->ext_addr, plugin->ext_addr_len,
2263 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2265 unsigned long long port;
2266 unsigned long long max_connections;
2267 char *bind4_address = NULL;
2268 char *bind6_address = NULL;
2271 if (GNUNET_CONFIGURATION_have_value
2272 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2275 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2279 plugin->use_ipv4 = GNUNET_YES;
2280 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2281 _("IPv4 support is %s\n"),
2282 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2285 if (GNUNET_CONFIGURATION_have_value
2286 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2289 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2293 plugin->use_ipv6 = GNUNET_YES;
2294 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2295 _("IPv6 support is %s\n"),
2296 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2298 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2300 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2302 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2304 return GNUNET_SYSERR;
2307 /* Reading port number from config file */
2309 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2310 "PORT", &port)) || (port > 65535))
2312 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2313 _("Port is required! Fix in configuration\n"),
2315 return GNUNET_SYSERR;
2317 plugin->port = port;
2319 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2320 _("Using port %u\n"), plugin->port);
2322 if ((plugin->use_ipv4 == GNUNET_YES) &&
2323 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2324 plugin->name, "BINDTO", &bind4_address)))
2326 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2327 "Binding %s plugin to specific IPv4 address: `%s'\n",
2328 plugin->protocol, bind4_address);
2329 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2330 if (1 != inet_pton (AF_INET, bind4_address,
2331 &plugin->server_addr_v4->sin_addr))
2333 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2335 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2337 GNUNET_free (bind4_address);
2338 GNUNET_free (plugin->server_addr_v4);
2339 plugin->server_addr_v4 = NULL;
2340 return GNUNET_SYSERR;
2344 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2345 _("Binding to IPv4 address %s\n"), bind4_address);
2346 plugin->server_addr_v4->sin_family = AF_INET;
2347 plugin->server_addr_v4->sin_port = htons (plugin->port);
2349 GNUNET_free (bind4_address);
2352 if ((plugin->use_ipv6 == GNUNET_YES) &&
2354 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2355 "BINDTO6", &bind6_address)))
2357 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2358 "Binding %s plugin to specific IPv6 address: `%s'\n",
2359 plugin->protocol, bind6_address);
2360 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2362 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2364 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2366 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2368 GNUNET_free (bind6_address);
2369 GNUNET_free (plugin->server_addr_v6);
2370 plugin->server_addr_v6 = NULL;
2371 return GNUNET_SYSERR;
2375 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2376 _("Binding to IPv6 address %s\n"), bind6_address);
2377 plugin->server_addr_v6->sin6_family = AF_INET6;
2378 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2380 GNUNET_free (bind6_address);
2383 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2384 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2386 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2387 _("Using external hostname `%s'\n"), plugin->external_hostname);
2388 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2392 "No external hostname configured\n");
2395 /* Optional parameters */
2396 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2398 "MAX_CONNECTIONS", &max_connections))
2399 max_connections = 128;
2400 plugin->max_connections = max_connections;
2402 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2403 _("Maximum number of connections is %u\n"),
2404 plugin->max_connections);
2409 * Session was idle, so disconnect it
2413 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2415 GNUNET_assert (NULL != cls);
2416 struct Session *s = cls;
2418 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2419 GNUNET_log (TIMEOUT_LOG,
2420 "Session %p was idle for %llu ms, disconnecting\n",
2421 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2423 /* call session destroy function */
2424 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2428 * Start session timeout
2431 server_start_session_timeout (struct Session *s)
2433 GNUNET_assert (NULL != s);
2434 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2435 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2436 &server_session_timeout,
2438 GNUNET_log (TIMEOUT_LOG,
2439 "Timeout for session %p set to %llu ms\n",
2440 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2445 * Increment session timeout due to activity
2448 server_reschedule_session_timeout (struct Session *s)
2450 GNUNET_assert (NULL != s);
2451 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2453 GNUNET_SCHEDULER_cancel (s->timeout_task);
2454 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2455 &server_session_timeout,
2457 GNUNET_log (TIMEOUT_LOG,
2458 "Timeout rescheduled for session %p set to %llu ms\n",
2459 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2463 * Exit point from the plugin.
2466 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2468 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2469 struct HTTP_Server_Plugin *plugin = api->cls;
2470 struct Session *pos;
2471 struct Session *next;
2473 if (NULL == api->cls)
2475 /* Free for stub mode */
2480 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2481 _("Shutting down plugin `%s'\n"),
2484 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2486 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2487 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2490 if (NULL != plugin->ext_addr)
2492 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2493 "Notifying transport to remove address `%s'\n",
2494 http_common_plugin_address_to_string (NULL,
2496 plugin->ext_addr_len));
2497 plugin->env->notify_address (plugin->env->cls,
2500 plugin->ext_addr_len,
2504 /* Stop to report addresses to transport service */
2505 server_stop_report_addresses (plugin);
2507 server_stop (plugin);
2509 next = plugin->head;
2510 while (NULL != (pos = next))
2513 GNUNET_CONTAINER_DLL_remove( plugin->head, plugin->tail, pos);
2514 server_delete_session (pos);
2518 GNUNET_free_non_null (plugin->external_hostname);
2519 GNUNET_free_non_null (plugin->ext_addr);
2520 GNUNET_free_non_null (plugin->server_addr_v4);
2521 GNUNET_free_non_null (plugin->server_addr_v6);
2523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2524 _("Shutdown for plugin `%s' complete\n"),
2527 GNUNET_free (plugin);
2534 * Entry point for the plugin.
2537 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2539 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2540 struct GNUNET_TRANSPORT_PluginFunctions *api;
2541 struct HTTP_Server_Plugin *plugin;
2543 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2547 if (NULL == env->receive)
2549 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2550 initialze the plugin or the API */
2551 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2553 api->address_to_string = &http_common_plugin_address_to_string;
2554 api->string_to_address = &http_common_plugin_string_to_address;
2555 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2559 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2561 api->send = &http_server_plugin_send;
2562 api->disconnect = &http_server_plugin_disconnect;
2563 api->check_address = &http_server_plugin_address_suggested;
2564 api->get_session = &http_server_plugin_get_session;
2566 api->address_to_string = &http_common_plugin_address_to_string;
2567 api->string_to_address = &http_common_plugin_string_to_address;
2568 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2571 plugin->name = "transport-https_server";
2572 plugin->protocol = "https";
2574 plugin->name = "transport-http_server";
2575 plugin->protocol = "http";
2578 /* Configure plugin */
2579 if (GNUNET_SYSERR == server_configure_plugin (plugin))
2581 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2585 /* Check IPv6 support */
2586 if (GNUNET_YES == plugin->use_ipv6)
2587 plugin->use_ipv6 = server_check_ipv6_support (plugin);
2589 /* Report addresses to transport service */
2590 server_start_report_addresses (plugin);
2592 if (GNUNET_SYSERR == server_start (plugin))
2594 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2604 /* end of plugin_transport_http_server.c */