2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file transport/test_plugin_transport_http.c
22 * @brief testcase for plugin_transport_http.c
23 * @author Matthias Wachs
27 #include "gnunet_constants.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_plugin_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_service_lib.h"
37 #include "plugin_transport.h"
38 #include "gnunet_statistics_service.h"
39 #include "transport.h"
40 #include <curl/curl.h>
42 #define VERBOSE GNUNET_YES
43 #define DEBUG GNUNET_YES
44 #define DEBUG_CURL GNUNET_NO
46 #define PLUGIN libgnunet_plugin_transport_template
49 * How long until we give up on transmitting the message?
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
54 * How long until we give up on transmitting the message?
56 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
59 * How long between recieve and send?
61 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
64 * Message to send using http
77 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
82 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
87 static struct GNUNET_PeerIdentity my_identity;
92 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
98 struct GNUNET_SCHEDULER_Handle *sched;
101 * Our statistics handle.
103 struct GNUNET_STATISTICS_Handle *stats;
109 const struct GNUNET_CONFIGURATION_Handle *cfg;
112 * Number of neighbours we'd like to have.
114 static uint32_t max_connect_per_transport;
117 * Environment for this plugin.
119 static struct GNUNET_TRANSPORT_PluginEnvironment env;
122 *handle for the api provided by this plugin
124 static struct GNUNET_TRANSPORT_PluginFunctions *api;
127 * ID of the task controlling the testcase timeout
129 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
131 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
133 const struct GNUNET_PeerIdentity * p;
136 * Struct for plugin addresses
138 struct Plugin_Address
141 * Next field for linked list
143 struct Plugin_Address * next;
146 * buffer containing data to send
151 * amount of data to sent
157 * Struct for plugin addresses
162 * HTTP Header result for transfer
164 unsigned int http_result_code;
167 * amount of bytes we recieved
173 struct Plugin_Address * addr_head;
176 * Did the test pass or fail?
178 static int fail_notify_address;
180 * Did the test pass or fail?
182 static int fail_notify_address_count;
185 * Did the test pass or fail?
187 static int fail_pretty_printer;
190 * Did the test pass or fail?
192 static int fail_pretty_printer_count;
195 * Did the test pass or fail?
197 static int fail_addr_to_str;
200 * Did the test pass or fail?
202 static int fail_send_data;
203 static struct HTTP_Transfer no_ident;
208 * Did the test pass or fail?
213 * Recieved message already returned to sender?
222 static CURLM *multi_handle;
225 * Test message to send
227 struct HTTP_Message * msg;
230 * The task sending data
232 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
240 curl_multi_cleanup(multi_handle);
242 if (NULL != curl_handle)
243 curl_easy_cleanup (curl_handle);
246 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
248 GNUNET_SCHEDULER_cancel(sched,ti_send);
249 ti_send = GNUNET_SCHEDULER_NO_TASK;
252 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
254 GNUNET_SCHEDULER_cancel(sched,http_task_send);
255 http_task_send = GNUNET_SCHEDULER_NO_TASK;
258 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
260 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
261 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
264 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
266 GNUNET_SCHEDULER_shutdown(sched);
268 GNUNET_free(msg->buf);
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
277 * Continuation called after plugin send message
280 * @result GNUNET_OK or GNUNET_SYSERR
282 static void task_send_cont (void *cls,
283 const struct GNUNET_PeerIdentity * target,
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
292 * Task sending recieved message back to peer
297 task_send (void *cls,
298 const struct GNUNET_SCHEDULER_TaskContext *tc)
300 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
301 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
304 if (GNUNET_YES==sent)
307 struct GNUNET_MessageHeader * msg = cls;
308 unsigned int len = ntohs(msg->size);
309 const char * msgc = (const char *) msg;
311 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
317 * Recieves messages from plugin, in real world transport
319 static struct GNUNET_TIME_Relative
321 const struct GNUNET_PeerIdentity * peer,
322 const struct GNUNET_MessageHeader * message,
324 struct Session *session,
325 const char *sender_address,
326 uint16_t sender_address_len)
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase recieved new message from peer `%s' with type %u and length %u\n", GNUNET_i2s(peer),ntohs(message->type),ntohs(message->size));
330 /* take recieved message and send it back to peer */
332 void * c = (void *) message;
333 ti_send =GNUNET_SCHEDULER_add_delayed (sched, WAIT_INTERVALL, &task_send, c);
335 return GNUNET_TIME_UNIT_ZERO;
340 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
343 struct HTTP_Message * cbc = ptr;
347 if (( cbc->pos == len) && (len < (size * nmemb)))
349 memcpy(stream, cbc->buf, len);
354 static size_t copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
356 struct HTTP_Message *cbc = ctx;
358 if (cbc->pos + size * nmemb > cbc->size)
359 return 0; /* overflow */
360 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Send completed. %s\n",cbc->buf);
362 cbc->pos += size * nmemb;
366 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
369 unsigned int len = size * nmemb;
370 tmp = GNUNET_malloc ( len+1 );
372 //if (tmp[len-1]!='\0')
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: %u :`%s'\n",len,tmp);
379 static size_t send_prepare( void );
381 static void send_execute (void *cls,
382 const struct GNUNET_SCHEDULER_TaskContext *tc)
390 http_task_send = GNUNET_SCHEDULER_NO_TASK;
391 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
397 mret = curl_multi_perform (multi_handle, &running);
403 msg = curl_multi_info_read (multi_handle, &running);
404 GNUNET_break (msg != NULL);
407 /* get session for affected curl handle */
408 //cs = find_session_by_curlhandle (msg->easy_handle);
409 //GNUNET_assert ( cs != NULL );
414 if ( (msg->data.result != CURLE_OK) &&
415 (msg->data.result != CURLE_GOT_NOTHING) )
418 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
419 _("curl failed for `%s' at %s:%d: `%s'\n"),
420 "curl_multi_perform",
423 curl_easy_strerror (msg->data.result));
424 /* sending msg failed*/
425 fail_send_data = GNUNET_YES;
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send completed\n");
430 /* sending completed */
431 fail_send_data = GNUNET_NO;
435 curl_easy_cleanup(curl_handle);
444 while ( (running > 0) );
447 while (mret == CURLM_CALL_MULTI_PERFORM);
452 * Function setting up file descriptors and scheduling task to run
453 * @param ses session to send data to
454 * @return bytes sent to peer
456 static size_t send_prepare( void )
462 struct GNUNET_NETWORK_FDSet *grs;
463 struct GNUNET_NETWORK_FDSet *gws;
471 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
472 if (mret != CURLM_OK)
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
475 _("%s failed at %s:%d: `%s'\n"),
476 "curl_multi_fdset", __FILE__, __LINE__,
477 curl_multi_strerror (mret));
480 mret = curl_multi_timeout (multi_handle, &to);
481 if (mret != CURLM_OK)
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
484 _("%s failed at %s:%d: `%s'\n"),
485 "curl_multi_timeout", __FILE__, __LINE__,
486 curl_multi_strerror (mret));
490 grs = GNUNET_NETWORK_fdset_create ();
491 gws = GNUNET_NETWORK_fdset_create ();
492 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
493 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
494 http_task_send = GNUNET_SCHEDULER_add_select (sched,
495 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
496 GNUNET_SCHEDULER_NO_TASK,
497 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
502 GNUNET_NETWORK_fdset_destroy (gws);
503 GNUNET_NETWORK_fdset_destroy (grs);
505 /* FIXME: return bytes REALLY sent */
510 * function to send data to server
512 static int send_data(struct HTTP_Message *msg, struct HTTP_Transfer * result, char * url)
515 curl_handle = curl_easy_init();
516 if( NULL == curl_handle)
518 printf("easy_init failed \n");
519 return GNUNET_SYSERR;
522 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
524 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
525 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
526 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
527 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
528 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, ©Buffer);
529 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, msg);
530 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
531 curl_easy_setopt (curl_handle, CURLOPT_READDATA, msg);
532 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
533 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
534 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
536 curl_multi_add_handle(multi_handle, curl_handle);
544 * Network format for IPv4 addresses.
546 struct IPv4HttpAddress
549 * IPv4 address, in network byte order.
554 * Port number, in network byte order.
562 * Network format for IPv6 addresses.
564 struct IPv6HttpAddress
569 struct in6_addr ipv6_addr;
572 * Port number, in network byte order.
579 * Plugin notifies transport (aka testcase) about its addresses
582 notify_address (void *cls,
586 struct GNUNET_TIME_Relative expires)
588 char * address = NULL;
590 struct Plugin_Address * pl_addr;
591 struct Plugin_Address * cur;
593 if (addrlen == (sizeof (struct IPv4HttpAddress)))
595 address = GNUNET_malloc (INET_ADDRSTRLEN);
596 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
597 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
600 if (addrlen == (sizeof (struct IPv6HttpAddress)))
602 address = GNUNET_malloc (INET6_ADDRSTRLEN);
603 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
604 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transport plugin notification for address: `%s':%u\n"),address,port);
608 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
609 pl_addr->addrlen = addrlen;
610 pl_addr->addr = GNUNET_malloc(addrlen);
611 memcpy(pl_addr->addr,addr,addrlen);
612 pl_addr->next = NULL;
614 if ( NULL == addr_head)
621 while (NULL != cur->next)
628 fail_notify_address_count++;
629 fail_notify_address = GNUNET_NO;
633 * Setup plugin environment
636 setup_plugin_environment ()
641 env.my_identity = &my_identity;
643 env.receive = &receive;
644 env.notify_address = ¬ify_address;
645 env.max_connections = max_connect_per_transport;
650 * Task shutting down testcase if it a timeout occurs
653 task_timeout (void *cls,
654 const struct GNUNET_SCHEDULER_TaskContext *tc)
656 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
657 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
666 static void pretty_printer_cb (void *cls,
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
672 fail_pretty_printer_count++;
680 * @param s scheduler to use
681 * @param c configuration to use
685 struct GNUNET_SCHEDULER_Handle *s,
687 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
693 unsigned long long tneigh;
694 struct Plugin_Address * cur;
695 struct Plugin_Address * tmp;
696 const char * addr_str;
697 unsigned int count_str_addr;
698 unsigned int suggest_res;
701 fail_pretty_printer = GNUNET_YES;
702 fail_notify_address = GNUNET_YES;
703 fail_addr_to_str = GNUNET_YES;
704 fail_send_data = GNUNET_YES;
707 /* parse configuration */
709 GNUNET_CONFIGURATION_get_value_number (c,
714 GNUNET_CONFIGURATION_get_value_filename (c,
716 "HOSTKEY", &keyfile)))
718 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
720 ("Transport service is lacking key configuration settings. Exiting.\n"));
721 GNUNET_SCHEDULER_shutdown (s);
725 max_connect_per_transport = (uint32_t) tneigh;
726 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
727 GNUNET_free (keyfile);
728 if (my_private_key == NULL)
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731 _("Transport service could not access hostkey. Exiting.\n"));
732 GNUNET_SCHEDULER_shutdown (s);
736 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
737 GNUNET_CRYPTO_hash (&my_public_key,
738 sizeof (my_public_key), &my_identity.hashPubKey);
740 /* load plugins... */
741 setup_plugin_environment ();
742 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
743 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
744 api = GNUNET_PLUGIN_load (libname, &env);
745 GNUNET_free (libname);
748 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
749 _("Failed to load transport plugin for http\n"));
754 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
756 /* testing plugin functionality */
757 GNUNET_assert (0!=fail_notify_address_count);
758 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
760 /* testing pretty printer with all addresses obtained from the plugin*/
761 while (addr_head != NULL)
765 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
766 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
767 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
769 GNUNET_assert (GNUNET_OK == suggest_res);
770 GNUNET_assert (NULL != addr_str);
773 tmp = addr_head->next;
774 GNUNET_free (addr_head->addr);
775 GNUNET_free (addr_head);
776 GNUNET_free ((char *) addr_str);
779 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
780 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
781 fail_pretty_printer=GNUNET_NO;
782 fail_addr_to_str=GNUNET_NO;
784 /* Suggesting addresses with wrong port*/
785 struct IPv4HttpAddress failing_addr;
786 failing_addr.ipv4_addr = INADDR_LOOPBACK;
787 failing_addr.u_port = 0;
788 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
789 GNUNET_assert (GNUNET_SYSERR == suggest_res);
791 /* Suggesting addresses with wrong size*/
792 failing_addr.ipv4_addr = INADDR_LOOPBACK;
793 failing_addr.u_port = 0;
794 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
795 GNUNET_assert (GNUNET_SYSERR == suggest_res);
797 /* Suggesting addresses with wrong address*/
798 failing_addr.ipv4_addr = 0;
799 failing_addr.u_port = 12389;
800 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
801 GNUNET_assert (GNUNET_SYSERR == suggest_res);
803 /* test sending to client */
804 multi_handle = curl_multi_init();
806 /*building messages */
807 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
810 msg->buf = GNUNET_malloc (2048);
813 res = send_data (msg, &no_ident, "http://localhost:12389/");
815 /* testing finished, shutting down */
816 if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) && (fail_send_data == GNUNET_NO))
826 * The main function for the transport service.
828 * @param argc number of arguments from the command line
829 * @param argv command line arguments
830 * @return 0 ok, 1 on error
833 main (int argc, char *const *argv)
836 static struct GNUNET_GETOPT_CommandLineOption options[] = {
837 GNUNET_GETOPT_OPTION_END
840 char *const argv_prog[] = {
841 "test_plugin_transport_http",
843 "test_plugin_transport_data_http.conf",
852 GNUNET_log_setup ("test_plugin_transport_http",
861 GNUNET_PROGRAM_run (5,
863 "test_plugin_transport_http",
864 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
866 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
871 /* end of test_plugin_transport_http.c */