From 5cec57cd094d584d3a5b034f02c68cfae05a496d Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 28 Nov 2012 12:25:37 +0000 Subject: [PATCH] -implementing limit to number of stream client connections --- src/fs/fs.conf.in | 8 +++++++ src/fs/gnunet-service-fs_stream.c | 37 +++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/fs/fs.conf.in b/src/fs/fs.conf.in index ceb0a9ea2..f45a78fc4 100644 --- a/src/fs/fs.conf.in +++ b/src/fs/fs.conf.in @@ -54,6 +54,14 @@ EXPECTED_NEIGHBOUR_COUNT = 128 # This option is mostly for testing. DISABLE_ANON_TRANSFER = NO +# Maximum number of non-anonymous transfers this peer will support +# at the same time. Excessive values mostly have the problem that +# the service might use more memory, so we need to bound this at +# some reasonable level. And if we have a very, very large +# number, we probably won't have enough bandwidth to suppor them +# well anyway, so better have a moderate cap. +MAX_STREAM_CLIENTS = 128 + [gnunet-auto-share] BINARY = gnunet-auto-share diff --git a/src/fs/gnunet-service-fs_stream.c b/src/fs/gnunet-service-fs_stream.c index 5b6a89aff..4daf21e44 100644 --- a/src/fs/gnunet-service-fs_stream.c +++ b/src/fs/gnunet-service-fs_stream.c @@ -22,9 +22,6 @@ * @file fs/gnunet-service-fs_stream.c * @brief non-anonymous file-transfer * @author Christian Grothoff - * - * TODO: - * - limit # concurrent clients */ #include "platform.h" #include "gnunet_constants.h" @@ -288,6 +285,16 @@ static struct StreamClient *sc_head; */ static struct StreamClient *sc_tail; +/** + * Number of active stream clients in the 'sc_*'-DLL. + */ +static unsigned int sc_count; + +/** + * Maximum allowed number of stream clients. + */ +static unsigned long long sc_count_max; + /** * Map from peer identities to 'struct StreamHandles' with streams to * those peers. @@ -885,6 +892,7 @@ terminate_stream (struct StreamClient *sc) GNUNET_CONTAINER_DLL_remove (sc_head, sc_tail, sc); + sc_count--; GNUNET_free (sc); } @@ -1237,6 +1245,13 @@ accept_cb (void *cls, if (NULL == socket) return GNUNET_SYSERR; + if (sc_count >= sc_count_max) + { + GNUNET_STATISTICS_update (GSF_stats, + gettext_noop ("# stream client connections rejected"), 1, + GNUNET_NO); + return GNUNET_SYSERR; + } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Accepting inbound stream connection from `%s'\n", GNUNET_i2s (initiator)); @@ -1254,6 +1269,7 @@ accept_cb (void *cls, GNUNET_CONTAINER_DLL_insert (sc_head, sc_tail, sc); + sc_count++; refresh_timeout_task (sc); return GNUNET_OK; } @@ -1266,10 +1282,17 @@ void GSF_stream_start () { stream_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES); - listen_socket = GNUNET_STREAM_listen (GSF_cfg, - GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER, - &accept_cb, NULL, - GNUNET_STREAM_OPTION_END); + if (GNUNET_YES == + GNUNET_CONFIGURATION_get_value_number (GSF_cfg, + "fs", + "MAX_STREAM_CLIENTS", + &sc_count_max)) + { + listen_socket = GNUNET_STREAM_listen (GSF_cfg, + GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER, + &accept_cb, NULL, + GNUNET_STREAM_OPTION_END); + } } -- 2.25.1