From b79a1b59f07258abc25a86f7347d954e61ea019a Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Thu, 6 May 2010 11:52:54 +0000 Subject: [PATCH] --- src/transport/plugin_transport_http.c | 132 +++++++++++++++------ src/transport/test_plugin_transport_http.c | 12 +- 2 files changed, 104 insertions(+), 40 deletions(-) diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c index b01fa4241..7569c65c7 100644 --- a/src/transport/plugin_transport_http.c +++ b/src/transport/plugin_transport_http.c @@ -47,6 +47,8 @@ #define HTTP_TIMEOUT 600 +#define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt(c, a, b); if (ret != CURLE_OK) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret)); } while (0); + /** * Text of the response sent back after the last bytes of a PUT * request have been received (just to formally obey the HTTP @@ -161,12 +163,25 @@ static GNUNET_SCHEDULER_TaskIdentifier http_task_v4; */ static GNUNET_SCHEDULER_TaskIdentifier http_task_v6; +static char * hd_src ; + /** * Curl multi for managing client operations. */ static CURLM *curl_multi; +static char * get_url( const struct GNUNET_PeerIdentity * target) +{ + return strdup("http://localhost:12389"); +} + +static size_t curl_read_function( void *ptr, size_t size, size_t nmemb, void *stream) +{ + // strcpy ("Testmessa") + return 0; +} + /** * Function that can be used by the transport service to transmit * a message using the plugin. @@ -195,21 +210,68 @@ static CURLM *curl_multi; */ static ssize_t http_plugin_send (void *cls, - const struct GNUNET_PeerIdentity * - target, - const char *msgbuf, - size_t msgbuf_size, - unsigned int priority, - struct GNUNET_TIME_Relative timeout, - struct Session *session, - const void *addr, - size_t addrlen, - int force_address, - GNUNET_TRANSPORT_TransmitContinuation - cont, void *cont_cls) + const struct GNUNET_PeerIdentity * target, + const char *msgbuf, + size_t msgbuf_size, + unsigned int priority, + struct GNUNET_TIME_Relative timeout, + struct Session *session, + const void *addr, + size_t addrlen, + int force_address, + GNUNET_TRANSPORT_TransmitContinuation cont, + void *cont_cls) { + char * peer_url = get_url( target ); + CURL *curl; + CURLcode ret; + int bytes_sent = 0; /* struct Plugin *plugin = cls; */ + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending %u bytes (`%s') to `%s'\n",msgbuf_size, msgbuf,GNUNET_i2s(target)); + /* Insert code to send using cURL */ + curl = curl_easy_init (); + + CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1); + CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4); + + /* setting put options */ + CURL_EASY_SETOPT (curl, CURLOPT_UPLOAD, 1L); + CURL_EASY_SETOPT (curl, CURLOPT_PUT, 1L); + CURL_EASY_SETOPT (curl, CURLOPT_READDATA, msgbuf); + + + /* no need to abort if the above failed */ + CURL_EASY_SETOPT (curl, + CURLOPT_URL, + peer_url); + if (ret != CURLE_OK) + { + /* clean_up (); */ + return 0; + } + CURL_EASY_SETOPT (curl, + CURLOPT_FAILONERROR, + 1); +#if 0 + CURL_EASY_SETOPT (curl, + CURLOPT_VERBOSE, + 1); +#endif + CURL_EASY_SETOPT (curl, + CURLOPT_BUFFERSIZE, + GNUNET_SERVER_MAX_MESSAGE_SIZE); + if (0 == strncmp (peer_url, "http", 4)) + CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet"); + CURL_EASY_SETOPT (curl, + CURLOPT_CONNECTTIMEOUT, + 60L); + CURL_EASY_SETOPT (curl, + CURLOPT_TIMEOUT, + 60L); + + GNUNET_free(peer_url); return bytes_sent; } @@ -510,34 +572,26 @@ libgnunet_plugin_transport_http_init (void *cls) } if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0)) { - http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG, - port, - &acceptPolicyCallback, - NULL, &accessHandlerCallback, NULL, - MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16, - MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1, - MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, - MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024), - MHD_OPTION_END); http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6, - port, - &acceptPolicyCallback, - NULL, &accessHandlerCallback, NULL, - MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16, - MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1, - MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, - MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024), - MHD_OPTION_END); + port, + &acceptPolicyCallback, + NULL, &accessHandlerCallback, NULL, + MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16, + MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1, + MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, + MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024), + MHD_OPTION_END); + http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG, + port, + &acceptPolicyCallback, + NULL, &accessHandlerCallback, NULL, + MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16, + MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1, + MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, + MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024), + MHD_OPTION_END); } - - if ((http_daemon_v4 != NULL) && (http_daemon_v6 != NULL)) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv4 and IPv6 enabled\n",port); - if ((http_daemon_v4 != NULL) && (http_daemon_v6 == NULL)) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv4 enabled\n",port); - if ((http_daemon_v4 == NULL) && (http_daemon_v6 != NULL)) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 enabled\n",port); - curl_multi = curl_multi_init (); if (http_daemon_v4 != NULL) @@ -545,6 +599,10 @@ libgnunet_plugin_transport_http_init (void *cls) if (http_daemon_v6 != NULL) http_task_v6 = prepare_daemon (http_daemon_v6); + if ((http_daemon_v4 == NULL) || (http_daemon_v6 != NULL)) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u\n",port); + + if (NULL == plugin->env->stats) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, diff --git a/src/transport/test_plugin_transport_http.c b/src/transport/test_plugin_transport_http.c index 1c8c9727a..eb27f3ee0 100644 --- a/src/transport/test_plugin_transport_http.c +++ b/src/transport/test_plugin_transport_http.c @@ -46,6 +46,7 @@ */ #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) + /** * Our public key. */ @@ -139,7 +140,7 @@ shutdown_clean () api)); if (my_private_key != NULL) GNUNET_CRYPTO_rsa_key_free (my_private_key); - + GNUNET_SCHEDULER_shutdown(sched); return; } @@ -225,7 +226,6 @@ run (void *cls, stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg); env.stats = stats; - /* max_connect_per_transport = (uint32_t) tneigh; my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); GNUNET_free (keyfile); @@ -240,7 +240,7 @@ run (void *cls, } GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key); GNUNET_CRYPTO_hash (&my_public_key, - sizeof (my_public_key), &my_identity.hashPubKey);*/ + sizeof (my_public_key), &my_identity.hashPubKey); /* load plugins... */ setup_plugin_environment (); @@ -263,6 +263,12 @@ run (void *cls, } fail = GNUNET_NO; + + char * test_message = "Hello World!"; + size_t size = strlen(test_message) +1; + + /* Testing to send */ + api->send(NULL, &my_identity,test_message,size,0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, NULL, NULL); shutdown_clean (); return; } -- 2.25.1