diff options
Diffstat (limited to 'src/transport/plugin_transport_http.c')
-rw-r--r-- | src/transport/plugin_transport_http.c | 127 |
1 files changed, 125 insertions, 2 deletions
diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c index a0711660b..7ede6da3c 100644 --- a/src/transport/plugin_transport_http.c +++ b/src/transport/plugin_transport_http.c | |||
@@ -32,8 +32,10 @@ | |||
32 | #include "gnunet_statistics_service.h" | 32 | #include "gnunet_statistics_service.h" |
33 | #include "gnunet_transport_service.h" | 33 | #include "gnunet_transport_service.h" |
34 | #include "plugin_transport.h" | 34 | #include "plugin_transport.h" |
35 | #include "microhttpd.h" | ||
35 | 36 | ||
36 | #define DEBUG_HTTP GNUNET_YES | 37 | #define VERBOSE GNUNET_YES |
38 | #define DEBUG GNUNET_YES | ||
37 | 39 | ||
38 | /** | 40 | /** |
39 | * After how long do we expire an address that we | 41 | * After how long do we expire an address that we |
@@ -42,6 +44,7 @@ | |||
42 | */ | 44 | */ |
43 | #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6) | 45 | #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6) |
44 | 46 | ||
47 | #define HTTP_TIMEOUT 600 | ||
45 | 48 | ||
46 | /** | 49 | /** |
47 | * Encapsulation of all of the state of the plugin. | 50 | * Encapsulation of all of the state of the plugin. |
@@ -130,6 +133,11 @@ struct Plugin | |||
130 | }; | 133 | }; |
131 | 134 | ||
132 | /** | 135 | /** |
136 | * Daemon for listening for new connections. | ||
137 | */ | ||
138 | static struct MHD_Daemon *http_daemon; | ||
139 | |||
140 | /** | ||
133 | * Function that can be used by the transport service to transmit | 141 | * Function that can be used by the transport service to transmit |
134 | * a message using the plugin. | 142 | * a message using the plugin. |
135 | * | 143 | * |
@@ -247,6 +255,46 @@ http_plugin_address_suggested (void *cls, | |||
247 | return GNUNET_OK; | 255 | return GNUNET_OK; |
248 | } | 256 | } |
249 | 257 | ||
258 | /** | ||
259 | * Check if we are allowed to connect to the given IP. | ||
260 | */ | ||
261 | static int | ||
262 | acceptPolicyCallback (void *cls, | ||
263 | const struct sockaddr *addr, socklen_t addr_len) | ||
264 | { | ||
265 | return MHD_YES; | ||
266 | } | ||
267 | |||
268 | /** | ||
269 | * Process GET or PUT request received via MHD. For | ||
270 | * GET, queue response that will send back our pending | ||
271 | * messages. For PUT, process incoming data and send | ||
272 | * to GNUnet core. In either case, check if a session | ||
273 | * already exists and create a new one if not. | ||
274 | */ | ||
275 | static int | ||
276 | accessHandlerCallback (void *cls, | ||
277 | struct MHD_Connection *session, | ||
278 | const char *url, | ||
279 | const char *method, | ||
280 | const char *version, | ||
281 | const char *upload_data, | ||
282 | size_t * upload_data_size, void **httpSessionCache) | ||
283 | { | ||
284 | return MHD_YES; | ||
285 | } | ||
286 | |||
287 | /** | ||
288 | * MHD is done handling a request. Cleanup | ||
289 | * the respective transport state. | ||
290 | */ | ||
291 | static void | ||
292 | requestCompletedCallback (void *unused, | ||
293 | struct MHD_Connection *session, | ||
294 | void **httpSessionCache) | ||
295 | { | ||
296 | |||
297 | } | ||
250 | 298 | ||
251 | /** | 299 | /** |
252 | * Entry point for the plugin. | 300 | * Entry point for the plugin. |
@@ -257,6 +305,8 @@ libgnunet_plugin_transport_http_init (void *cls) | |||
257 | struct GNUNET_TRANSPORT_PluginEnvironment *env = cls; | 305 | struct GNUNET_TRANSPORT_PluginEnvironment *env = cls; |
258 | struct GNUNET_TRANSPORT_PluginFunctions *api; | 306 | struct GNUNET_TRANSPORT_PluginFunctions *api; |
259 | struct Plugin *plugin; | 307 | struct Plugin *plugin; |
308 | long long unsigned int port; | ||
309 | int use_ipv6; | ||
260 | 310 | ||
261 | plugin = GNUNET_malloc (sizeof (struct Plugin)); | 311 | plugin = GNUNET_malloc (sizeof (struct Plugin)); |
262 | plugin->env = env; | 312 | plugin->env = env; |
@@ -267,7 +317,73 @@ libgnunet_plugin_transport_http_init (void *cls) | |||
267 | api->disconnect = &http_plugin_disconnect; | 317 | api->disconnect = &http_plugin_disconnect; |
268 | api->address_pretty_printer = &http_plugin_address_pretty_printer; | 318 | api->address_pretty_printer = &http_plugin_address_pretty_printer; |
269 | api->check_address = &http_plugin_address_suggested; | 319 | api->check_address = &http_plugin_address_suggested; |
270 | return api; | 320 | |
321 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n"); | ||
322 | /* Reading port number from config file */ | ||
323 | if ((GNUNET_OK != | ||
324 | GNUNET_CONFIGURATION_get_value_number (env->cfg, | ||
325 | "transport-http", | ||
326 | "PORT", | ||
327 | &port)) || | ||
328 | (port > 65535) ) | ||
329 | { | ||
330 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
331 | "http", | ||
332 | _ | ||
333 | ("Require valid port number for service `%s' in configuration!\n"), | ||
334 | "transport-http"); | ||
335 | return NULL; | ||
336 | } | ||
337 | use_ipv6 = GNUNET_YES; | ||
338 | use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-http","USE_IPV6"); | ||
339 | if ((http_daemon == NULL) && (port != 0)) | ||
340 | { | ||
341 | if ( use_ipv6 == GNUNET_YES) | ||
342 | { | ||
343 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon could not started, http plugin not working\n"); | ||
344 | http_daemon = MHD_start_daemon (MHD_USE_IPv6, | ||
345 | port, | ||
346 | &acceptPolicyCallback, | ||
347 | NULL, &accessHandlerCallback, NULL, | ||
348 | MHD_OPTION_CONNECTION_TIMEOUT, | ||
349 | (unsigned int) HTTP_TIMEOUT, | ||
350 | MHD_OPTION_CONNECTION_MEMORY_LIMIT, | ||
351 | (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE, | ||
352 | MHD_OPTION_CONNECTION_LIMIT, | ||
353 | (unsigned int) 128, | ||
354 | MHD_OPTION_PER_IP_CONNECTION_LIMIT, | ||
355 | (unsigned int) 8, | ||
356 | MHD_OPTION_NOTIFY_COMPLETED, | ||
357 | &requestCompletedCallback, NULL, | ||
358 | MHD_OPTION_END); | ||
359 | } | ||
360 | else | ||
361 | { | ||
362 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port); | ||
363 | http_daemon = MHD_start_daemon (MHD_NO_FLAG, | ||
364 | port, | ||
365 | &acceptPolicyCallback, | ||
366 | NULL, &accessHandlerCallback, NULL, | ||
367 | MHD_OPTION_CONNECTION_TIMEOUT, | ||
368 | (unsigned int) HTTP_TIMEOUT, | ||
369 | MHD_OPTION_CONNECTION_MEMORY_LIMIT, | ||
370 | (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE, | ||
371 | MHD_OPTION_CONNECTION_LIMIT, | ||
372 | (unsigned int) 128, | ||
373 | MHD_OPTION_PER_IP_CONNECTION_LIMIT, | ||
374 | (unsigned int) 8, | ||
375 | MHD_OPTION_NOTIFY_COMPLETED, | ||
376 | &requestCompletedCallback, NULL, | ||
377 | MHD_OPTION_END); | ||
378 | } | ||
379 | } | ||
380 | if ( NULL != http_daemon ) | ||
381 | return api; | ||
382 | else | ||
383 | { | ||
384 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Starting MHD on port %u with IPv6 disabled\n",port); | ||
385 | return NULL; | ||
386 | } | ||
271 | } | 387 | } |
272 | 388 | ||
273 | 389 | ||
@@ -280,6 +396,13 @@ libgnunet_plugin_transport_http_done (void *cls) | |||
280 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | 396 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; |
281 | struct Plugin *plugin = api->cls; | 397 | struct Plugin *plugin = api->cls; |
282 | 398 | ||
399 | if (http_daemon != NULL) | ||
400 | { | ||
401 | MHD_stop_daemon (http_daemon); | ||
402 | http_daemon = NULL; | ||
403 | } | ||
404 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n"); | ||
405 | |||
283 | GNUNET_free (plugin); | 406 | GNUNET_free (plugin); |
284 | GNUNET_free (api); | 407 | GNUNET_free (api); |
285 | return NULL; | 408 | return NULL; |