aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-06-27 11:58:05 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-06-27 11:58:05 +0000
commitc7a1c4045e54c5c7ac360007a33a5fa46ee74687 (patch)
tree19f272fc2fe790d7f7404c095c9928e0157af9ca
parent7641f95f5043db6051054e377f03a4bca158fc75 (diff)
downloadgnunet-c7a1c4045e54c5c7ac360007a33a5fa46ee74687.tar.gz
gnunet-c7a1c4045e54c5c7ac360007a33a5fa46ee74687.zip
restrict number of connections
-rw-r--r--src/transport/plugin_transport_http.c86
1 files changed, 58 insertions, 28 deletions
diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c
index 09b3b219f..1fdfcdbda 100644
--- a/src/transport/plugin_transport_http.c
+++ b/src/transport/plugin_transport_http.c
@@ -469,6 +469,16 @@ struct Plugin
469 int use_localaddresses; 469 int use_localaddresses;
470 470
471 /** 471 /**
472 * maximum number of connections
473 */
474 int max_connect_per_transport;
475
476 /**
477 * Current number of connections;
478 */
479 int current_connections;
480
481 /**
472 * Closure passed by MHD to the mhd_logger function 482 * Closure passed by MHD to the mhd_logger function
473 */ 483 */
474 void * mhd_log; 484 void * mhd_log;
@@ -919,7 +929,11 @@ mhd_termination_cb (void *cls,
919 if (ps == NULL) 929 if (ps == NULL)
920 return; 930 return;
921 struct HTTP_PeerContext * pc = ps->peercontext; 931 struct HTTP_PeerContext * pc = ps->peercontext;
922 932 struct Plugin *plugin = cls;
933
934 GNUNET_assert (cls != NULL);
935 plugin->current_connections--;
936
923 if (connection==ps->recv_endpoint) 937 if (connection==ps->recv_endpoint)
924 { 938 {
925#if DEBUG_CONNECTIONS 939#if DEBUG_CONNECTIONS
@@ -1028,11 +1042,15 @@ mhd_accept_cb (void *cls,
1028 const struct sockaddr *addr, 1042 const struct sockaddr *addr,
1029 socklen_t addr_len) 1043 socklen_t addr_len)
1030{ 1044{
1031#if 0
1032 struct Plugin *plugin = cls; 1045 struct Plugin *plugin = cls;
1033#endif 1046 GNUNET_assert (cls != NULL);
1034 /* Every connection is accepted, nothing more to do here */ 1047
1035 return MHD_YES; 1048 if (plugin->max_connect_per_transport > plugin->current_connections)
1049 {
1050 plugin->current_connections ++;
1051 return MHD_YES;
1052 }
1053 else return MHD_NO;
1036} 1054}
1037 1055
1038 1056
@@ -1068,6 +1086,7 @@ mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1068 1086
1069 if (msg!=NULL) 1087 if (msg!=NULL)
1070 { 1088 {
1089 /* sending */
1071 if ((msg->size-msg->pos) <= max) 1090 if ((msg->size-msg->pos) <= max)
1072 { 1091 {
1073 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos)); 1092 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
@@ -1081,6 +1100,7 @@ mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1081 bytes_read = max; 1100 bytes_read = max;
1082 } 1101 }
1083 1102
1103 /* removing message */
1084 if (msg->pos==msg->size) 1104 if (msg->pos==msg->size)
1085 { 1105 {
1086 if (NULL!=msg->transmit_cont) 1106 if (NULL!=msg->transmit_cont)
@@ -1875,8 +1895,6 @@ curl_handle_finished (struct Plugin *plugin)
1875 ps->send_connected = GNUNET_NO; 1895 ps->send_connected = GNUNET_NO;
1876 ps->send_active = GNUNET_NO; 1896 ps->send_active = GNUNET_NO;
1877 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint); 1897 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1878 //curl_easy_cleanup(ps->send_endpoint);
1879 //ps->send_endpoint=NULL;
1880 while (ps->pending_msgs_tail != NULL) 1898 while (ps->pending_msgs_tail != NULL)
1881 { 1899 {
1882 cur_msg = ps->pending_msgs_tail; 1900 cur_msg = ps->pending_msgs_tail;
@@ -1901,8 +1919,6 @@ curl_handle_finished (struct Plugin *plugin)
1901 ps->recv_connected = GNUNET_NO; 1919 ps->recv_connected = GNUNET_NO;
1902 ps->recv_active = GNUNET_NO; 1920 ps->recv_active = GNUNET_NO;
1903 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint); 1921 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1904 //curl_easy_cleanup(ps->recv_endpoint);
1905 //ps->recv_endpoint=NULL;
1906 } 1922 }
1907 } 1923 }
1908 else 1924 else
@@ -1941,8 +1957,6 @@ curl_handle_finished (struct Plugin *plugin)
1941 ps->send_connected = GNUNET_NO; 1957 ps->send_connected = GNUNET_NO;
1942 ps->send_active = GNUNET_NO; 1958 ps->send_active = GNUNET_NO;
1943 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint); 1959 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1944 //curl_easy_cleanup(ps->send_endpoint);
1945 //ps->send_endpoint =NULL;
1946 } 1960 }
1947 if (msg->easy_handle == ps->recv_endpoint) 1961 if (msg->easy_handle == ps->recv_endpoint)
1948 { 1962 {
@@ -1957,9 +1971,8 @@ curl_handle_finished (struct Plugin *plugin)
1957 ps->recv_connected = GNUNET_NO; 1971 ps->recv_connected = GNUNET_NO;
1958 ps->recv_active = GNUNET_NO; 1972 ps->recv_active = GNUNET_NO;
1959 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint); 1973 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1960 //curl_easy_cleanup(ps->recv_endpoint);
1961 //ps->recv_endpoint=NULL;
1962 } 1974 }
1975 plugin->current_connections--;
1963 } 1976 }
1964 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO)) 1977 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1965 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR); 1978 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
@@ -2121,10 +2134,11 @@ send_check_connections (struct Plugin *plugin,
2121 CURLMcode mret; 2134 CURLMcode mret;
2122 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT; 2135 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2123 2136
2124 if (ps->direction == OUTBOUND) 2137 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2125 { 2138 {
2126 /* RECV DIRECTION */ 2139 /* RECV DIRECTION */
2127 /* Check if session is connected to receive data, otherwise connect to peer */ 2140 /* Check if session is connected to receive data, otherwise connect to peer */
2141
2128 if (ps->recv_connected == GNUNET_NO) 2142 if (ps->recv_connected == GNUNET_NO)
2129 { 2143 {
2130 int fresh = GNUNET_NO; 2144 int fresh = GNUNET_NO;
@@ -2139,7 +2153,7 @@ send_check_connections (struct Plugin *plugin,
2139 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint); 2153 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2140#endif 2154#endif
2141#if BUILD_HTTPS 2155#if BUILD_HTTPS
2142 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); 2156 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2143 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0); 2157 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2144 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0); 2158 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2145#endif 2159#endif
@@ -2170,11 +2184,12 @@ send_check_connections (struct Plugin *plugin,
2170 return GNUNET_SYSERR; 2184 return GNUNET_SYSERR;
2171 } 2185 }
2172 } 2186 }
2173 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK) 2187 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2174 { 2188 {
2175 GNUNET_SCHEDULER_cancel(plugin->http_curl_task); 2189 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2176 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK; 2190 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2177 } 2191 }
2192 plugin->current_connections ++;
2178 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin); 2193 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2179 } 2194 }
2180 2195
@@ -2218,7 +2233,7 @@ send_check_connections (struct Plugin *plugin,
2218 } 2233 }
2219 } 2234 }
2220 /* not connected, initiate connection */ 2235 /* not connected, initiate connection */
2221 if (ps->send_connected==GNUNET_NO) 2236 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2222 { 2237 {
2223 int fresh = GNUNET_NO; 2238 int fresh = GNUNET_NO;
2224 if (NULL == ps->send_endpoint) 2239 if (NULL == ps->send_endpoint)
@@ -2279,6 +2294,7 @@ send_check_connections (struct Plugin *plugin,
2279 GNUNET_SCHEDULER_cancel(plugin->http_curl_task); 2294 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2280 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK; 2295 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2281 } 2296 }
2297 plugin->current_connections++;
2282 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin); 2298 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2283 return GNUNET_YES; 2299 return GNUNET_YES;
2284 } 2300 }
@@ -2990,6 +3006,7 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2990 struct GNUNET_TRANSPORT_PluginFunctions *api; 3006 struct GNUNET_TRANSPORT_PluginFunctions *api;
2991 struct GNUNET_TIME_Relative gn_timeout; 3007 struct GNUNET_TIME_Relative gn_timeout;
2992 long long unsigned int port; 3008 long long unsigned int port;
3009 unsigned long long tneigh;
2993 char * component_name; 3010 char * component_name;
2994#if BUILD_HTTPS 3011#if BUILD_HTTPS
2995 char * key_file = NULL; 3012 char * key_file = NULL;
@@ -3028,6 +3045,21 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3028 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey), 3045 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3029 &plugin->my_ascii_hash_ident); 3046 &plugin->my_ascii_hash_ident);
3030 3047
3048
3049 if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3050 {
3051 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3052 "TRANSPORT",
3053 "NEIGHBOUR_LIMIT",
3054 &tneigh);
3055 }
3056 else
3057 {
3058 tneigh = -1;
3059 }
3060 plugin->max_connect_per_transport = tneigh;
3061
3062
3031 /* Use IPv6? */ 3063 /* Use IPv6? */
3032 if (GNUNET_CONFIGURATION_have_value (env->cfg, 3064 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3033 component_name, "USE_IPv6")) 3065 component_name, "USE_IPv6"))
@@ -3239,11 +3271,10 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3239#endif 3271#endif
3240 MHD_USE_IPv6, 3272 MHD_USE_IPv6,
3241 port, 3273 port,
3242 &mhd_accept_cb, 3274 &mhd_accept_cb, plugin,
3243 plugin , &mhd_access_cb, plugin, 3275 &mhd_access_cb, plugin,
3244 MHD_OPTION_SOCK_ADDR, tmp, 3276 MHD_OPTION_SOCK_ADDR, tmp,
3245 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32, 3277 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3246 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
3247#if BUILD_HTTPS 3278#if BUILD_HTTPS
3248 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init, 3279 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3249 MHD_OPTION_HTTPS_MEM_KEY, plugin->key, 3280 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
@@ -3251,7 +3282,7 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3251#endif 3282#endif
3252 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout, 3283 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3253 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE), 3284 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3254 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL, 3285 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3255 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log, 3286 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3256 MHD_OPTION_END); 3287 MHD_OPTION_END);
3257 } 3288 }
@@ -3268,11 +3299,10 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3268#endif 3299#endif
3269 MHD_NO_FLAG, 3300 MHD_NO_FLAG,
3270 port, 3301 port,
3271 &mhd_accept_cb, 3302 &mhd_accept_cb, plugin ,
3272 plugin , &mhd_access_cb, plugin, 3303 &mhd_access_cb, plugin,
3273 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address, 3304 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3274 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32, 3305 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3275 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
3276#if BUILD_HTTPS 3306#if BUILD_HTTPS
3277 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init, 3307 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3278 MHD_OPTION_HTTPS_MEM_KEY, plugin->key, 3308 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
@@ -3280,7 +3310,7 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3280#endif 3310#endif
3281 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout, 3311 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3282 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE), 3312 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3283 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL, 3313 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3284 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log, 3314 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3285 MHD_OPTION_END); 3315 MHD_OPTION_END);
3286 } 3316 }