*/
unsigned long long max_connections;
+ /**
+ * How many more TCP sessions do we have right now?
+ */
+ unsigned long long cur_connections;
+
/**
* ID of task used to update our addresses when one expires.
*/
const struct sockaddr *addr, socklen_t addrlen)
{
struct Plugin *plugin = cls;
-
- LOG (GNUNET_ERROR_TYPE_ERROR,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Accepting new incoming TCP connection from `%s'\n",
GNUNET_a2s (addr, addrlen));
- if (0 == plugin->max_connections)
+ if (plugin->cur_connections >= plugin->max_connections)
return GNUNET_NO;
- plugin->max_connections--;
+ plugin->cur_connections ++;
return GNUNET_YES;
}
create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
struct GNUNET_SERVER_Client *client, int is_nat)
{
- struct Session *ret;
+ struct Session *session;
struct PendingMessage *pm;
struct WelcomeMessage welcome;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Creating new session for peer `%4s'\n",
GNUNET_i2s (target));
- ret = GNUNET_malloc (sizeof (struct Session));
- ret->last_activity = GNUNET_TIME_absolute_get ();
- ret->plugin = plugin;
- ret->is_nat = is_nat;
- ret->client = client;
- ret->target = *target;
- ret->expecting_welcome = GNUNET_YES;
- ret->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
+ session = GNUNET_malloc (sizeof (struct Session));
+ session->last_activity = GNUNET_TIME_absolute_get ();
+ session->plugin = plugin;
+ session->is_nat = is_nat;
+ session->client = client;
+ session->target = *target;
+ session->expecting_welcome = GNUNET_YES;
+ session->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
pm = GNUNET_malloc (sizeof (struct PendingMessage) +
sizeof (struct WelcomeMessage));
pm->msg = (const char *) &pm[1];
GNUNET_STATISTICS_update (plugin->env->stats,
gettext_noop ("# bytes currently in TCP buffers"),
pm->message_size, GNUNET_NO);
- GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
- ret->pending_messages_tail, pm);
+ GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
+ session->pending_messages_tail, pm);
if (GNUNET_YES != is_nat)
{
GNUNET_STATISTICS_update (plugin->env->stats,
gettext_noop ("# TCP sessions active"), 1,
GNUNET_NO);
}
- start_session_timeout (ret);
+ start_session_timeout (session);
- return ret;
+ return session;
}
{
GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
if (pos->transmit_cont != NULL)
- pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR);
+ pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0);
GNUNET_free (pos);
}
GNUNET_STATISTICS_update (plugin->env->stats,
{
GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
if (pos->transmit_cont != NULL)
- pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK);
+ pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
GNUNET_free (pos);
}
GNUNET_assert (hd == NULL);
session->pending_messages_tail, pm);
if (NULL != pm->transmit_cont)
pm->transmit_cont (pm->transmit_cont_cls, &session->target,
- GNUNET_SYSERR);
+ GNUNET_SYSERR, pm->message_size, 0);
GNUNET_free (pm);
}
if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
GNUNET_free (session);
}
-/* FIXME WORKAROUND FOR MANTIS 0002445 */
-struct result
+
+struct FindSessionContext
{
struct Session *s;
int res;
const struct GNUNET_HashCode * key,
void *value)
{
- struct result *res = cls;
-
+ struct FindSessionContext *res = cls;
if (res->s == value)
{
res->res = GNUNET_OK;
return GNUNET_NO;
}
else
- {
return GNUNET_YES;
+}
+
+int find_session (struct Plugin *plugin, struct Session *session)
+{
+ struct FindSessionContext session_map_res;
+ struct FindSessionContext nat_map_res;
+
+ session_map_res.s = session;
+ session_map_res.res = GNUNET_SYSERR;
+ GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_it, &session_map_res);
+
+ nat_map_res.s = session;
+ nat_map_res.res = GNUNET_SYSERR;
+ GNUNET_CONTAINER_multihashmap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
+
+ if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
}
+ return GNUNET_OK;
}
-/* FIXME END WORKAROUND FOR MANTIS 0002445 */
/**
* Function that can be used by the transport service to transmit
GNUNET_assert (NULL != plugin);
GNUNET_assert (NULL != session);
- /* FIXME WORKAROUND FOR MANTIS 0002445 */
- struct result res1;
- struct result res2;
-
- res1.s = session;
- res1.res = GNUNET_SYSERR;
- GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_it, &res1);
-
- res2.s = session;
- res2.res = GNUNET_SYSERR;
- GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_it, &res2);
-
- if ((res1.res == GNUNET_SYSERR) && (res2.res == GNUNET_SYSERR))
+ if (GNUNET_SYSERR == find_session(plugin, session))
{
- LOG (GNUNET_ERROR_TYPE_ERROR,
- "WORKAROUND MANTIS BUG 2445: This Trying to send to invalid session %p\n", session);
- GNUNET_break (0);
- return GNUNET_SYSERR;
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ _("Trying to send with invalid session %p\n"));
+ return GNUNET_SYSERR;
}
- /* FIXME END WORKAROUND FOR MANTIS 0002445 */
/* create new message entry */
pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
"Asked to transmit %u bytes to `%s', added message to list.\n",
msgbuf_size, GNUNET_i2s (&session->target));
- if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessionmap, &session->target.hashPubKey, session))
+ if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
+ &session->target.hashPubKey,
+ session))
{
GNUNET_assert (session->client != NULL);
reschedule_session_timeout (session);
LOG (GNUNET_ERROR_TYPE_ERROR,
"Invalid session %p\n", session);
if (NULL != cont)
- cont (cont_cls, &session->target, GNUNET_SYSERR);
+ cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
GNUNET_break (0);
GNUNET_free (pm);
return GNUNET_SYSERR; /* session does not exist here */
{
struct Session *session = cls;
+ session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
GNUNET_i2s (&session->target), tcp_address_to_string(NULL, session->addr, session->addrlen));
tcp_plugin_get_session (void *cls,
const struct GNUNET_HELLO_Address *address)
{
- struct Plugin * plugin = cls;
- struct Session * session = NULL;
+ struct Plugin *plugin = cls;
+ struct Session *session = NULL;
int af;
const void *sb;
size_t sbs;
/* look for existing session */
if (GNUNET_YES ==
- GNUNET_CONTAINER_multihashmap_contains(plugin->sessionmap, &address->peer.hashPubKey))
+ GNUNET_CONTAINER_multihashmap_contains (plugin->sessionmap,
+ &address->peer.hashPubKey))
{
struct SessionItCtx si_ctx;
si_ctx.result = NULL;
- GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessionmap, &address->peer.hashPubKey, &session_lookup_it, &si_ctx);
+ GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap,
+ &address->peer.hashPubKey,
+ &session_lookup_it, &si_ctx);
if (si_ctx.result != NULL)
{
session = si_ctx.result;
return NULL;
}
- if (0 == plugin->max_connections)
+ if (plugin->cur_connections >= plugin->max_connections)
{
/* saturated */
return NULL;
session->addrlen = 0;
session->addr = NULL;
session->ats_address_network_type = ats.value;
- session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed(NAT_TIMEOUT,
- &nat_connect_timeout,
- session);
+ session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
+ &nat_connect_timeout,
+ session);
GNUNET_assert (session != NULL);
- GNUNET_assert (GNUNET_CONTAINER_multihashmap_put
- (plugin->nat_wait_conns, &address->peer.hashPubKey, session,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (plugin->nat_wait_conns,
+ &session->target.hashPubKey,
+ session,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Created NAT WAIT connection to `%4s' at `%s'\n",
}
/* create new outbound session */
- GNUNET_assert (0 != plugin->max_connections);
+ GNUNET_assert (plugin->cur_connections <= plugin->max_connections);
sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
if (sa == NULL)
{
GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
return NULL;
}
- plugin->max_connections--;
+ plugin->cur_connections++;
+ if (plugin->cur_connections == plugin->max_connections)
+ GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
session->addrlen = addrlen;
session->ats_address_network_type = ats.value;
- GNUNET_CONTAINER_multihashmap_put(plugin->sessionmap, &address->peer.hashPubKey, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
+ &session->target.hashPubKey,
+ session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
inc_sessions (plugin, session, __LINE__);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Creating new session for `%s' address `%s' session %p\n",
GNUNET_i2s (&address->peer),
tcp_address_to_string(NULL, address->address, address->address_length),
(plugin->nat_wait_conns,
&tcp_nat_probe->clientIdentity.hashPubKey,
session) == GNUNET_YES);
- GNUNET_CONTAINER_multihashmap_put(plugin->sessionmap,
- &session->target.hashPubKey, session,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
+ &session->target.hashPubKey, session,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
session->last_activity = GNUNET_TIME_absolute_get ();
session->inbound = GNUNET_NO;
LOG (GNUNET_ERROR_TYPE_DEBUG,
else
{
GNUNET_SERVER_client_keep (client);
+ if (plugin->service != NULL) /* Otherwise value is incremented in tcp_access_check */
+ plugin->cur_connections++;
+ if (plugin->cur_connections == plugin->max_connections)
+ GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
+
session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
session->inbound = GNUNET_YES;
if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Did not obtain TCP socket address for incoming connection\n");
}
- GNUNET_CONTAINER_multihashmap_put(plugin->sessionmap, &wm->clientIdentity.hashPubKey, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
+ &session->target.hashPubKey,
+ session,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
inc_sessions (plugin, session, __LINE__);
}
delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct Session *session = cls;
- struct GNUNET_TIME_Relative delay;
- struct GNUNET_ATS_Information ats;
session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
- delay =
- session->plugin->env->receive (session->plugin->env->cls,
- &session->target, NULL, &ats, 0, session,
- NULL, 0);
reschedule_session_timeout (session);
- if (delay.rel_value == 0)
- GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
- else
- session->receive_delay_task =
- GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
+ GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
}
delay = plugin->env->receive (plugin->env->cls,
&session->target,
message,
- (const struct GNUNET_ATS_Information *) &distance,
- 1, session,
+ session,
(GNUNET_YES == session->inbound) ? NULL : session->addr,
(GNUNET_YES == session->inbound) ? 0 : session->addrlen);
+ plugin->env->update_address_metrics (plugin->env->cls,
+ &session->target,
+ (GNUNET_YES == session->inbound) ? NULL : session->addr,
+ (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
+ session,
+ (struct GNUNET_ATS_Information *) &distance,
+ 2);
reschedule_session_timeout (session);
if (client == NULL)
return;
- plugin->max_connections++;
session = lookup_session_by_client (plugin, client);
if (session == NULL)
return; /* unknown, nothing to do */
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Destroying session of `%4s' with %s due to network-level disconnect.\n",
GNUNET_i2s (&session->target),
(session->addr !=
session->addr,
session->addrlen) :
"*");
+
+ if (plugin->cur_connections == plugin->max_connections)
+ GNUNET_SERVER_resume (plugin->server); /* Resume server */
+
+ if (plugin->cur_connections < 1)
+ GNUNET_break (0);
+ else
+ plugin->cur_connections--;
+
GNUNET_STATISTICS_update (session->plugin->env->stats,
gettext_noop
("# network-level TCP disconnect events"), 1,
return api;
}
+ GNUNET_assert (NULL != env->cfg);
+ GNUNET_assert (NULL != env->stats);
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
"MAX_CONNECTIONS",
service = NULL;
plugin = GNUNET_malloc (sizeof (struct Plugin));
- plugin->sessionmap = GNUNET_CONTAINER_multihashmap_create(max_connections);
+ plugin->sessionmap = GNUNET_CONTAINER_multihashmap_create (max_connections, GNUNET_YES);
plugin->max_connections = max_connections;
+ plugin->cur_connections = 0;
plugin->open_port = bport;
plugin->adv_port = aport;
plugin->env = env;
}
else
{
- plugin->nat =
- GNUNET_NAT_register (env->cfg, GNUNET_YES, 0, 0, NULL, NULL, NULL,
+ plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
+ GNUNET_YES, 0, 0, NULL, NULL, NULL,
&try_connection_reversal, plugin);
}
api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
i++)
plugin->handlers[i].callback_cls = plugin;
+
GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
- plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create (16);
+ plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
if (bport != 0)
LOG (GNUNET_ERROR_TYPE_INFO,
_("TCP transport listening on port %llu\n"), bport);