From eccba40f860a0b216814bbfca0f03c532f610b71 Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Mon, 31 May 2010 15:28:34 +0000 Subject: [PATCH] --- src/transport/plugin_transport_http.c | 87 +++++++++++++--- src/transport/test_plugin_transport_http.c | 116 ++++++++++++++++++--- 2 files changed, 179 insertions(+), 24 deletions(-) diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c index 98901835f..1b5f5ed11 100644 --- a/src/transport/plugin_transport_http.c +++ b/src/transport/plugin_transport_http.c @@ -330,7 +330,7 @@ static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *pe /** * Finds a http session in our linked list using libcurl handle as a key * Needed when sending data with libcurl to differentiate between sessions - * @param peer peeridentity + * @param handle peeridentity * @return http session corresponding to peer identity */ static struct Session * find_session_by_curlhandle( CURL* handle ) @@ -351,7 +351,7 @@ static struct Session * find_session_by_curlhandle( CURL* handle ) * Create a new session * * @param address address the peer is using - * @peer peer identity + * @param peer identity * @return created session object */ static struct Session * create_session (struct sockaddr_in *address, const struct GNUNET_PeerIdentity *peer) @@ -747,7 +747,7 @@ static int remove_http_message(struct Session * ses, struct HTTP_Message * msg) * Callback method used with libcurl * Method is called when libcurl needs to read data during sending * @param stream pointer where to write data - * @param size_t size of an individual element + * @param size size of an individual element * @param nmemb count of elements that can be written to the buffer * @param ptr source pointer, passed to the libcurl handle * @return bytes written to stream @@ -776,7 +776,7 @@ static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void * * Callback method used with libcurl * Method is called when libcurl needs to write data during sending * @param stream pointer where to write data -* @param size_t size of an individual element +* @param size size of an individual element * @param nmemb count of elements that can be written to the buffer * @param ptr destination pointer, passed to the libcurl handle * @return bytes read from stream @@ -796,7 +796,7 @@ static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void /** * Function setting up file descriptors and scheduling task to run - * @param ses session to send data to + * @param session session to send data to * @return bytes sent to peer */ static size_t send_prepare(struct Session* session ); @@ -991,7 +991,7 @@ static size_t send_prepare(struct Session* session ) * @param priority how important is the message * @param msgbuf the message to transmit * @param msgbuf_size number of bytes in 'msgbuf' - * @param timeout when should we time out + * @param to when should we time out * @param session which session must be used (or NULL for "any") * @param addr the address to use (can be NULL if the plugin * is "on its own" (i.e. re-use existing TCP connection)) @@ -1143,10 +1143,42 @@ http_plugin_address_pretty_printer (void *cls, GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_pretty_printer\n"); - + const struct IPv4HttpAddress *t4; + const struct IPv6HttpAddress *t6; + struct sockaddr_in a4; + struct sockaddr_in6 a6; + char * address; + char * ret; + unsigned int port; + + if (addrlen == sizeof (struct IPv6HttpAddress)) + { + address = GNUNET_malloc (INET6_ADDRSTRLEN); + t6 = addr; + a6.sin6_addr = t6->ipv6_addr; + inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN); + port = ntohs(t6->u6_port); + } + else if (addrlen == sizeof (struct IPv4HttpAddress)) + { + address = GNUNET_malloc (INET_ADDRSTRLEN); + t4 = addr; + a4.sin_addr.s_addr = t4->ipv4_addr; + inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN); + port = ntohs(t4->u_port); + } + else + { + /* invalid address */ + GNUNET_break_op (0); + asc (asc_cls, NULL); + return; + } - asc (asc_cls, NULL); + ret = GNUNET_malloc(strlen(address) +14); + GNUNET_asprintf(&ret,"http://%s:%u/",address,port); + GNUNET_free (address); + asc (asc_cls, ret); } @@ -1192,9 +1224,40 @@ http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_address_to_string\n"); - GNUNET_break (0); - return NULL; + const struct IPv4HttpAddress *t4; + const struct IPv6HttpAddress *t6; + struct sockaddr_in a4; + struct sockaddr_in6 a6; + char * address; + char * ret; + unsigned int port; + + if (addrlen == sizeof (struct IPv6HttpAddress)) + { + address = GNUNET_malloc (INET6_ADDRSTRLEN); + t6 = addr; + a6.sin6_addr = t6->ipv6_addr; + inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN); + port = ntohs(t6->u6_port); + } + else if (addrlen == sizeof (struct IPv4HttpAddress)) + { + address = GNUNET_malloc (INET_ADDRSTRLEN); + t4 = addr; + a4.sin_addr.s_addr = t4->ipv4_addr; + inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN); + port = ntohs(t4->u_port); + } + else + { + /* invalid address */ + return NULL; + } + + ret = GNUNET_malloc(strlen(address) +6); + GNUNET_asprintf(&ret,"%s:%u",address,port); + GNUNET_free (address); + return ret; } /** diff --git a/src/transport/test_plugin_transport_http.c b/src/transport/test_plugin_transport_http.c index 6e717470c..728ab7db3 100644 --- a/src/transport/test_plugin_transport_http.c +++ b/src/transport/test_plugin_transport_http.c @@ -116,11 +116,32 @@ static struct GNUNET_TRANSPORT_PluginFunctions *api; */ static GNUNET_SCHEDULER_TaskIdentifier ti_timeout; - static GNUNET_SCHEDULER_TaskIdentifier ti_send; const struct GNUNET_PeerIdentity * p; +/** + * Struct for plugin addresses + */ +struct Plugin_Address +{ + /** + * Next field for linked list + */ + struct Plugin_Address * next; + + /** + * buffer containing data to send + */ + void * addr; + + /** + * amount of data to sent + */ + size_t addrlen; +}; + +struct Plugin_Address * addr_head; /** * Did the test pass or fail? @@ -131,6 +152,21 @@ static int fail_notify_address; */ static int fail_notify_address_count; +/** + * Did the test pass or fail? + */ +static int fail_pretty_printer; + +/** + * Did the test pass or fail? + */ +static int fail_pretty_printer_count; + +/** + * Did the test pass or fail? + */ +static int fail_addr_to_str; + /** * Did the test pass or fail? */ @@ -158,7 +194,6 @@ shutdown_clean () GNUNET_SCHEDULER_cancel(sched,ti_timeout); ti_timeout = GNUNET_SCHEDULER_NO_TASK; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n"); GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api)); @@ -278,7 +313,8 @@ notify_address (void *cls, { char * address = NULL; unsigned int port; - + struct Plugin_Address * pl_addr; + struct Plugin_Address * cur; if (addrlen == (sizeof (struct IPv4HttpAddress))) { @@ -294,8 +330,28 @@ notify_address (void *cls, port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port); } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transport plugin notification for address: `%s':%u\n"),address,port); - fail_notify_address_count++; + pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) ); + pl_addr->addrlen = addrlen; + pl_addr->addr = GNUNET_malloc(addrlen); + memcpy(pl_addr->addr,addr,addrlen); + pl_addr->next = NULL; + + if ( NULL == addr_head) + { + addr_head = pl_addr; + } + else + { + cur = addr_head; + while (NULL != cur->next) + { + cur = cur->next; + } + cur->next = pl_addr; + } + + fail_notify_address_count++; fail_notify_address = GNUNET_NO; } @@ -333,6 +389,15 @@ task_timeout (void *cls, return; } +static void pretty_printer_cb (void *cls, + const char *address) +{ + if (NULL==address) + return; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned: `%s'\n",address); + fail_pretty_printer_count++; +} + /** * Runs the test. @@ -352,7 +417,16 @@ run (void *cls, cfg = c; char *keyfile; unsigned long long tneigh; - + struct Plugin_Address * cur; + struct Plugin_Address * tmp; + const char * addr_str; + unsigned int count_str_addr; + + fail_pretty_printer = GNUNET_YES; + fail_notify_address = GNUNET_YES; + fail_addr_to_str = GNUNET_YES; + addr_head = NULL; + count_str_addr = 0; /* parse configuration */ if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, @@ -403,18 +477,36 @@ run (void *cls, ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL); /* testing plugin functionality */ + GNUNET_assert (0!=fail_notify_address_count); GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count); - /* testing finished, shutting down */ + /* testing pretty printer with all addresses obtained from the plugin*/ + while (addr_head != NULL) + { + cur = addr_head; + + api->address_pretty_printer(NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL); + addr_str = api->address_to_string(NULL,cur->addr,cur->addrlen); + GNUNET_assert (NULL != addr_str); + count_str_addr++; + + tmp = addr_head->next; + GNUNET_free (addr_head->addr); + GNUNET_free (addr_head); + GNUNET_free ((char *) addr_str); + addr_head=tmp; + } + GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count); + GNUNET_assert (fail_pretty_printer_count==count_str_addr); + fail_pretty_printer=GNUNET_NO; + fail_addr_to_str=GNUNET_NO; - if (fail_notify_address == GNUNET_NO) + /* testing finished, shutting down */ + if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) ) fail = 0; - - - + else + fail = 1; shutdown_clean(); - - return; } -- 2.25.1