/*
This file is part of GNUnet
- Copyright (C) 2002-2014 GNUnet e.V.
+ Copyright (C) 2002-2014, 2017 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
#include "gnunet_util_lib.h"
#include "gnunet_statistics_service.h"
#include "gnunet_transport_plugin.h"
-#include "gnunet_nat_lib.h"
+#include "gnunet_nat_service.h"
#include "plugin_transport_http_common.h"
#include <microhttpd.h>
#include <regex.h>
*/
int connected;
+ /**
+ * Currently suspended
+ */
+ bool suspended;
+
};
/**
* Message stream tokenizer for incoming data
*/
- struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
+ struct GNUNET_MessageStreamTokenizer *msg_tk;
/**
* Client recv handle
/**
* MHD IPv4 task
*/
- struct GNUNET_SCHEDULER_Task * server_v4_task;
+ struct GNUNET_SCHEDULER_Task *server_v4_task;
/**
* MHD IPv6 task
*/
- struct GNUNET_SCHEDULER_Task * server_v6_task;
+ struct GNUNET_SCHEDULER_Task *server_v6_task;
/**
* Task calling transport service about external address
*/
- struct GNUNET_SCHEDULER_Task * notify_ext_task;
+ struct GNUNET_SCHEDULER_Task *notify_ext_task;
/**
* Notify transport only about external address
server_wake_up (void *cls)
{
struct GNUNET_ATS_Session *s = cls;
- const struct GNUNET_SCHEDULER_TaskContext *tc;
- tc = GNUNET_SCHEDULER_get_task_context ();
s->recv_wakeup_task = NULL;
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Session %p: Waking up PUT handle\n",
s);
+ GNUNET_assert (s->server_recv->suspended);
MHD_resume_connection (s->server_recv->mhd_conn);
+ s->server_recv->suspended = false;
}
GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
s->recv_wakeup_task = NULL;
if (NULL != s->server_recv)
+ {
+ GNUNET_assert (s->server_recv->suspended);
+ s->server_recv->suspended = false;
MHD_resume_connection (s->server_recv->mhd_conn);
+ }
}
GNUNET_assert (GNUNET_OK ==
GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
MHD_set_connection_option (s->server_send->mhd_conn,
MHD_CONNECTION_OPTION_TIMEOUT,
1 /* 0 = no timeout, so this is MIN */);
- server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_YES);
+ if (s->server_send->suspended)
+ {
+ s->server_send->suspended = false;
+ MHD_resume_connection (s->server_send->mhd_conn);
+ }
+ server_reschedule (plugin,
+ s->server_send->mhd_daemon,
+ GNUNET_YES);
}
if (NULL != s->server_recv)
MHD_set_connection_option (s->server_recv->mhd_conn,
MHD_CONNECTION_OPTION_TIMEOUT,
1 /* 0 = no timeout, so this is MIN */);
- server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_YES);
+ server_reschedule (plugin,
+ s->server_recv->mhd_daemon,
+ GNUNET_YES);
}
notify_session_monitor (plugin,
s,
}
if (NULL != s->msg_tk)
{
- GNUNET_SERVER_mst_destroy (s->msg_tk);
+ GNUNET_MST_destroy (s->msg_tk);
s->msg_tk = NULL;
}
GNUNET_HELLO_address_free (s->address);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Session %p destroyed\n",
s);
-
GNUNET_free (s);
}
msg->buf = (char *) &msg[1];
msg->transmit_cont = cont;
msg->transmit_cont_cls = cont_cls;
- memcpy (msg->buf,
+ GNUNET_memcpy (msg->buf,
msgbuf,
msgbuf_size);
GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
GNUNET_free (stat_txt);
if (NULL != session->server_send)
+ {
+ if (session->server_send->suspended)
+ {
+ MHD_resume_connection (session->server_send->mhd_conn);
+ session->server_send->suspended = false;
+ }
server_reschedule (session->plugin,
session->server_send->mhd_daemon,
GNUNET_YES);
+ }
return bytes_sent;
}
*/
static int
destroy_session_shutdown_cb (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- void *value)
+ const struct GNUNET_PeerIdentity *peer,
+ void *value)
{
struct GNUNET_ATS_Session *s = value;
struct ServerRequest *sc_send;
sc_send = s->server_send;
sc_recv = s->server_recv;
server_delete_session (s);
-
- GNUNET_free_non_null (sc_send);
- GNUNET_free_non_null (sc_recv);
+ if (NULL != sc_send)
+ sc_send->session = NULL;
+ if (NULL != sc_recv)
+ sc_recv->session = NULL;
return GNUNET_OK;
}
+
/**
* Terminate session.
*
return GNUNET_OK;
}
+
/**
* Function that can be used to force the plugin to disconnect
* from the given peer and cancel all previous transmissions
server_v4_run (void *cls)
{
struct HTTP_Server_Plugin *plugin = cls;
- const struct GNUNET_SCHEDULER_TaskContext *tc;
plugin->server_v4_task = NULL;
- tc = GNUNET_SCHEDULER_get_task_context ();
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
plugin->server_v4_immediately = GNUNET_NO;
GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
server_v6_run (void *cls)
{
struct HTTP_Server_Plugin *plugin = cls;
- const struct GNUNET_SCHEDULER_TaskContext *tc;
plugin->server_v6_task = NULL;
- tc = GNUNET_SCHEDULER_get_task_context ();
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
plugin->server_v6_immediately = GNUNET_NO;
GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
/* sending */
bytes_read = GNUNET_MIN (msg->size - msg->pos,
max);
- memcpy (buf, &msg->buf[msg->pos], bytes_read);
+ GNUNET_memcpy (buf, &msg->buf[msg->pos], bytes_read);
msg->pos += bytes_read;
/* removing message */
s);
return MHD_CONTENT_READER_END_OF_STREAM;
}
+ else
+ {
+ MHD_suspend_connection (s->server_send->mhd_conn);
+ s->server_send->suspended = true;
+ return 0;
+ }
return bytes_read;
}
* Callback called by MessageStreamTokenizer when a message has arrived
*
* @param cls current session as closure
- * @param client client
* @param message the message to be forwarded to transport service
- * @return #GNUNET_OK
+ * @return #GNUNET_OK (all OK)
*/
static int
server_receive_mst_cb (void *cls,
- void *client,
const struct GNUNET_MessageHeader *message)
{
struct GNUNET_ATS_Session *s = cls;
"# bytes received via %s_server",
plugin->protocol);
GNUNET_STATISTICS_update (plugin->env->stats,
- stat_txt, ntohs (message->size), GNUNET_NO);
+ stat_txt,
+ ntohs (message->size),
+ GNUNET_NO);
GNUNET_free (stat_txt);
s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
if (delay.rel_value_us > 0)
*upload_data_size);
if (s->msg_tk == NULL)
{
- s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
+ s->msg_tk = GNUNET_MST_create (&server_receive_mst_cb,
+ s);
}
- GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size,
- GNUNET_NO, GNUNET_NO);
+ GNUNET_MST_from_buffer (s->msg_tk,
+ upload_data,
+ *upload_data_size,
+ GNUNET_NO, GNUNET_NO);
server_mhd_connection_timeout (plugin, s,
- GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
- / 1000LL);
+ GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
+ / 1000LL);
(*upload_data_size) = 0;
}
else
{
/* delay processing */
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
- "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
- s, sc, GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
+ s,
+ sc,
+ GNUNET_STRINGS_relative_time_to_string (delay,
+ GNUNET_YES));
GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
MHD_suspend_connection (s->server_recv->mhd_conn);
+ s->server_recv->suspended = true;
if (NULL == s->recv_wakeup_task)
- s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
- &server_wake_up, s);
+ s->recv_wakeup_task
+ = GNUNET_SCHEDULER_add_delayed (delay,
+ &server_wake_up,
+ s);
}
return MHD_YES;
}
sc->session->server_recv = NULL;
if (NULL != sc->session->msg_tk)
{
- GNUNET_SERVER_mst_destroy (sc->session->msg_tk);
+ GNUNET_MST_destroy (sc->session->msg_tk);
sc->session->msg_tk = NULL;
}
}
}
+/**
+ * Callback from MHD when a connection starts/stops
+ *
+ * @param cls closure with the `struct HTTP_Server_Plugin *`
+ * @param connection connection handle
+ * @param socket_context socket-specific pointer
+ * @param toe reason for connection notification
+ * @see #MHD_OPTION_NOTIFY_CONNECTION
+ */
+static void
+server_connection_cb (void *cls,
+ struct MHD_Connection *connection,
+ void **socket_context,
+ enum MHD_ConnectionNotificationCode toe)
+{
+ struct HTTP_Server_Plugin *plugin = cls;
+ const union MHD_ConnectionInfo *info;
+
+ if (MHD_CONNECTION_NOTIFY_STARTED == toe)
+ return;
+
+ /* Reschedule to remove closed socket from our select set */
+ info = MHD_get_connection_info (connection,
+ MHD_CONNECTION_INFO_DAEMON);
+ GNUNET_assert (NULL != info);
+ server_reschedule (plugin,
+ info->daemon,
+ GNUNET_YES);
+}
+
+
/**
* Check if incoming connection is accepted.
*
timeout,
MHD_OPTION_CONNECTION_MEMORY_LIMIT,
(size_t) (2 *
- GNUNET_SERVER_MAX_MESSAGE_SIZE),
+ GNUNET_MAX_MESSAGE_SIZE),
MHD_OPTION_NOTIFY_COMPLETED,
&server_disconnect_cb, plugin,
+ MHD_OPTION_NOTIFY_CONNECTION,
+ &server_connection_cb, plugin,
MHD_OPTION_EXTERNAL_LOGGER,
&server_log, NULL,
MHD_OPTION_END);
* @param cls closure, the 'struct LocalAddrList'
* @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
* the previous (now invalid) one
+ * @param ac address class the address belongs to
* @param addr either the previous or the new public IP address
* @param addrlen actual lenght of the address
*/
static void
server_nat_port_map_callback (void *cls,
int add_remove,
+ enum GNUNET_NAT_AddressClass ac,
const struct sockaddr *addr,
socklen_t addrlen)
{
if ((NULL != plugin->server_addr_v4) &&
(0 != memcmp (&plugin->server_addr_v4->sin_addr,
- &s4->sin_addr, sizeof (struct in_addr))))
+ &s4->sin_addr,
+ sizeof (struct in_addr))))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Skipping address `%s' (not bindto address)\n",
GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
saddrlens[i] = pos->ai_addrlen;
saddrs[i] = GNUNET_malloc (saddrlens[i]);
- memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
+ GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
}
else
GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
saddrlens[i] = pos->ai_addrlen;
saddrs[i] = GNUNET_malloc (saddrlens[i]);
- memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
+ GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
}
i++;
return;
}
- plugin->nat =
- GNUNET_NAT_register (plugin->env->cfg,
- GNUNET_YES,
- plugin->port,
+ plugin->nat
+ = GNUNET_NAT_register (plugin->env->cfg,
+ "transport-http_server",
+ IPPROTO_TCP,
(unsigned int) res,
- (const struct sockaddr **) addrs, addrlens,
- &server_nat_port_map_callback, NULL, plugin, NULL);
+ (const struct sockaddr **) addrs,
+ addrlens,
+ &server_nat_port_map_callback,
+ NULL,
+ plugin);
while (res > 0)
{
res--;
int res = GNUNET_NO;
/* Probe IPv6 support */
- desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
+ desc = GNUNET_NETWORK_socket_create (PF_INET6,
+ SOCK_STREAM,
+ 0);
if (NULL == desc)
{
- if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
- (errno == EACCES))
+ if ( (errno == ENOBUFS) ||
+ (errno == ENOMEM) ||
+ (errno == ENFILE) ||
+ (errno == EACCES) )
{
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+ "socket");
}
LOG (GNUNET_ERROR_TYPE_WARNING,
_("Disabling IPv6 since it is not supported on this system!\n"));
}
else
{
- GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
+ GNUNET_break (GNUNET_OK ==
+ GNUNET_NETWORK_socket_close (desc));
desc = NULL;
res = GNUNET_YES;
}
size_t ext_addr_len;
unsigned int urlen;
char *url;
- const struct GNUNET_SCHEDULER_TaskContext *tc;
plugin->notify_ext_task = NULL;
- tc = GNUNET_SCHEDULER_get_task_context ();
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
-
- GNUNET_asprintf(&url,
- "%s://%s",
- plugin->protocol,
- plugin->external_hostname);
-
+ GNUNET_asprintf (&url,
+ "%s://%s",
+ plugin->protocol,
+ plugin->external_hostname);
urlen = strlen (url) + 1;
ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
ext_addr->options = htonl (plugin->options);
ext_addr->urlen = htonl (urlen);
ext_addr_len = sizeof (struct HttpAddress) + urlen;
- memcpy (&ext_addr[1], url, urlen);
+ GNUNET_memcpy (&ext_addr[1], url, urlen);
GNUNET_free (url);
LOG (GNUNET_ERROR_TYPE_DEBUG,
LOG (GNUNET_ERROR_TYPE_INFO,
"Enabling SSL verification for external hostname address `%s'\n",
plugin->external_hostname);
- plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
- "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
- plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
+ plugin->ext_addr
+ = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
+ "https_client",
+ ext_addr,
+ ext_addr_len,
+ GNUNET_HELLO_ADDRESS_INFO_NONE);
+ plugin->env->notify_address (plugin->env->cls,
+ GNUNET_YES,
+ plugin->ext_addr);
GNUNET_free (ext_addr);
#else
- plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
- "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
- plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
+ plugin->ext_addr
+ = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
+ "http_client",
+ ext_addr,
+ ext_addr_len,
+ GNUNET_HELLO_ADDRESS_INFO_NONE);
+ plugin->env->notify_address (plugin->env->cls,
+ GNUNET_YES,
+ plugin->ext_addr);
GNUNET_free (ext_addr);
#endif
}
LOG (GNUNET_ERROR_TYPE_INFO,
_("Using port %u\n"), plugin->port);
- if ((plugin->use_ipv4 == GNUNET_YES) &&
- (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
- plugin->name, "BINDTO", &bind4_address)))
+ if ( (plugin->use_ipv4 == GNUNET_YES) &&
+ (GNUNET_YES ==
+ GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
+ plugin->name,
+ "BINDTO",
+ &bind4_address)))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Binding %s plugin to specific IPv4 address: `%s'\n",
- plugin->protocol, bind4_address);
+ plugin->protocol,
+ bind4_address);
plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
- if (1 != inet_pton (AF_INET, bind4_address,
+ if (1 != inet_pton (AF_INET,
+ bind4_address,
&plugin->server_addr_v4->sin_addr))
{
LOG (GNUNET_ERROR_TYPE_ERROR,
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- _("Binding to IPv4 address %s\n"),
+ "Binding to IPv4 address %s\n",
bind4_address);
plugin->server_addr_v4->sin_family = AF_INET;
plugin->server_addr_v4->sin_port = htons (plugin->port);
(GNUNET_YES ==
GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
plugin->name,
- "BINDTO6", &bind6_address)))
+ "BINDTO6",
+ &bind6_address)))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Binding %s plugin to specific IPv6 address: `%s'\n",
plugin->protocol, bind6_address);
plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
if (1 !=
- inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
+ inet_pton (AF_INET6,
+ bind6_address,
+ &plugin->server_addr_v6->sin6_addr))
{
LOG (GNUNET_ERROR_TYPE_ERROR,
_("Specific IPv6 address `%s' in configuration file is invalid!\n"),
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- _("Binding to IPv6 address %s\n"),
+ "Binding to IPv6 address %s\n",
bind6_address);
plugin->server_addr_v6->sin6_family = AF_INET6;
plugin->server_addr_v6->sin6_port = htons (plugin->port);
plugin->verify_external_hostname = GNUNET_NO;
#if BUILD_HTTPS
- plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
- plugin->name,
- "VERIFY_EXTERNAL_HOSTNAME");
+ plugin->verify_external_hostname
+ = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
+ plugin->name,
+ "VERIFY_EXTERNAL_HOSTNAME");
if (GNUNET_SYSERR == plugin->verify_external_hostname)
plugin->verify_external_hostname = GNUNET_NO;
if (GNUNET_YES == plugin->verify_external_hostname)
plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
#endif
- external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
- plugin->name,
- "EXTERNAL_HOSTNAME_USE_PORT");
+ external_hostname_use_port
+ = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
+ plugin->name,
+ "EXTERNAL_HOSTNAME_USE_PORT");
if (GNUNET_SYSERR == external_hostname_use_port)
external_hostname_use_port = GNUNET_NO;
"%s:%u/%s",
tmp,
(uint16_t) port,
- (NULL == pos_url) ? "" : pos_url);
+ pos_url);
}
else
GNUNET_asprintf (&plugin->external_hostname,
/* Stop to report addresses to transport service */
server_stop_report_addresses (plugin);
- if (NULL != plugin->server_v4)
- {
- MHD_stop_daemon (plugin->server_v4);
- plugin->server_v4 = NULL;
- }
- if (NULL != plugin->server_v6)
- {
- MHD_stop_daemon (plugin->server_v6);
- plugin->server_v6 = NULL;
- }
if (NULL != plugin->server_v4_task)
{
GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
plugin);
GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
plugin->sessions = NULL;
+ if (NULL != plugin->server_v4)
+ {
+ MHD_stop_daemon (plugin->server_v4);
+ plugin->server_v4 = NULL;
+ }
+ if (NULL != plugin->server_v6)
+ {
+ MHD_stop_daemon (plugin->server_v6);
+ plugin->server_v6 = NULL;
+ }
/* Clean up */
GNUNET_free_non_null (plugin->external_hostname);
GNUNET_free_non_null (plugin->ext_addr);
}
/* Compile URL regex */
- if (regcomp(&plugin->url_regex,
- URL_REGEX,
- REG_EXTENDED))
+ if (regcomp (&plugin->url_regex,
+ URL_REGEX,
+ REG_EXTENDED))
{
LOG (GNUNET_ERROR_TYPE_ERROR,
- _("Unable to compile URL regex\n"));
+ _("Unable to compile URL regex\n"));
LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
return NULL;
}