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
45 #define PLUGIN libgnunet_plugin_transport_template
48 * How long until we give up on transmitting the message?
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
53 * How long until we give up on transmitting the message?
55 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
58 * How long between recieve and send?
60 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
63 * Message to send using http
76 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
81 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
86 static struct GNUNET_PeerIdentity my_identity;
91 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
97 struct GNUNET_SCHEDULER_Handle *sched;
100 * Our statistics handle.
102 struct GNUNET_STATISTICS_Handle *stats;
108 const struct GNUNET_CONFIGURATION_Handle *cfg;
111 * Number of neighbours we'd like to have.
113 static uint32_t max_connect_per_transport;
116 * Environment for this plugin.
118 static struct GNUNET_TRANSPORT_PluginEnvironment env;
121 *handle for the api provided by this plugin
123 static struct GNUNET_TRANSPORT_PluginFunctions *api;
126 * ID of the task controlling the testcase timeout
128 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
130 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
132 const struct GNUNET_PeerIdentity * p;
135 * Struct for plugin addresses
137 struct Plugin_Address
140 * Next field for linked list
142 struct Plugin_Address * next;
145 * buffer containing data to send
150 * amount of data to sent
155 struct Plugin_Address * addr_head;
158 * Did the test pass or fail?
160 static int fail_notify_address;
162 * Did the test pass or fail?
164 static int fail_notify_address_count;
167 * Did the test pass or fail?
169 static int fail_pretty_printer;
172 * Did the test pass or fail?
174 static int fail_pretty_printer_count;
177 * Did the test pass or fail?
179 static int fail_addr_to_str;
182 * Did the test pass or fail?
187 * Recieved message already returned to sender?
196 static CURLM *multi_handle;
199 * Test message to send
201 struct HTTP_Message * msg;
204 * The task sending data
206 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
214 curl_multi_cleanup(multi_handle);
216 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
218 GNUNET_SCHEDULER_cancel(sched,ti_send);
219 ti_send = GNUNET_SCHEDULER_NO_TASK;
222 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
224 GNUNET_SCHEDULER_cancel(sched,http_task_send);
225 http_task_send = GNUNET_SCHEDULER_NO_TASK;
228 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
230 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
231 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
234 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
236 GNUNET_SCHEDULER_shutdown(sched);
241 //GNUNET_free(msg->buf);
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
249 * Continuation called after plugin send message
252 * @result GNUNET_OK or GNUNET_SYSERR
254 static void task_send_cont (void *cls,
255 const struct GNUNET_PeerIdentity * target,
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
264 * Task sending recieved message back to peer
269 task_send (void *cls,
270 const struct GNUNET_SCHEDULER_TaskContext *tc)
272 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
273 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
276 if (GNUNET_YES==sent)
279 struct GNUNET_MessageHeader * msg = cls;
280 unsigned int len = ntohs(msg->size);
281 const char * msgc = (const char *) msg;
283 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
289 * Recieves messages from plugin, in real world transport
291 static struct GNUNET_TIME_Relative
293 const struct GNUNET_PeerIdentity * peer,
294 const struct GNUNET_MessageHeader * message,
296 struct Session *session,
297 const char *sender_address,
298 uint16_t sender_address_len)
300 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));
302 /* take recieved message and send it back to peer */
304 void * c = (void *) message;
305 ti_send =GNUNET_SCHEDULER_add_delayed (sched, WAIT_INTERVALL, &task_send, c);
307 return GNUNET_TIME_UNIT_ZERO;
312 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
315 struct HTTP_Message * cbc = ptr;
319 if (( cbc->pos == len) && (len < (size * nmemb)))
321 memcpy(stream, cbc->buf, len);
326 static size_t copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
328 struct HTTP_Message *cbc = ctx;
330 if (cbc->pos + size * nmemb > cbc->size)
331 return 0; /* overflow */
332 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
333 cbc->pos += size * nmemb;
337 static size_t send_prepare( void );
339 static void send_execute (void *cls,
340 const struct GNUNET_SCHEDULER_TaskContext *tc)
346 http_task_send = GNUNET_SCHEDULER_NO_TASK;
347 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
353 mret = curl_multi_perform (multi_handle, &running);
359 msg = curl_multi_info_read (multi_handle, &running);
360 GNUNET_break (msg != NULL);
363 /* get session for affected curl handle */
364 //cs = find_session_by_curlhandle (msg->easy_handle);
365 //GNUNET_assert ( cs != NULL );
370 if ( (msg->data.result != CURLE_OK) &&
371 (msg->data.result != CURLE_GOT_NOTHING) )
374 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
375 _("curl failed for `%s' at %s:%d: `%s'\n"),
376 "curl_multi_perform",
379 curl_easy_strerror (msg->data.result));
380 /* sending msg failed*/
381 //if ( NULL != cs->transmit_cont)
382 // cs->transmit_cont (NULL,&cs->sender,GNUNET_SYSERR);
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Send completed.\n");
388 //if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
389 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'", GNUNET_i2s(&cs->sender));
391 curl_easy_cleanup(curl_handle);
402 while ( (running > 0) );
405 while (mret == CURLM_CALL_MULTI_PERFORM);
410 * Function setting up file descriptors and scheduling task to run
411 * @param ses session to send data to
412 * @return bytes sent to peer
414 static size_t send_prepare( void )
420 struct GNUNET_NETWORK_FDSet *grs;
421 struct GNUNET_NETWORK_FDSet *gws;
429 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
430 if (mret != CURLM_OK)
432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
433 _("%s failed at %s:%d: `%s'\n"),
434 "curl_multi_fdset", __FILE__, __LINE__,
435 curl_multi_strerror (mret));
438 mret = curl_multi_timeout (multi_handle, &to);
439 if (mret != CURLM_OK)
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("%s failed at %s:%d: `%s'\n"),
443 "curl_multi_timeout", __FILE__, __LINE__,
444 curl_multi_strerror (mret));
448 grs = GNUNET_NETWORK_fdset_create ();
449 gws = GNUNET_NETWORK_fdset_create ();
450 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
451 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
452 http_task_send = GNUNET_SCHEDULER_add_select (sched,
453 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
454 GNUNET_SCHEDULER_NO_TASK,
455 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
460 GNUNET_NETWORK_fdset_destroy (gws);
461 GNUNET_NETWORK_fdset_destroy (grs);
463 /* FIXME: return bytes REALLY sent */
468 * function to send data to server
470 static int send_data(struct HTTP_Message *msg, char * url)
473 curl_handle = curl_easy_init();
474 if( NULL == curl_handle)
476 printf("easy_init failed \n");
477 return GNUNET_SYSERR;
479 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
480 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
481 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
482 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, ©Buffer);
483 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, msg);
484 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
485 curl_easy_setopt (curl_handle, CURLOPT_READDATA, msg);
486 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
487 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
488 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
490 curl_multi_add_handle(multi_handle, curl_handle);
498 * Network format for IPv4 addresses.
500 struct IPv4HttpAddress
503 * IPv4 address, in network byte order.
508 * Port number, in network byte order.
516 * Network format for IPv6 addresses.
518 struct IPv6HttpAddress
523 struct in6_addr ipv6_addr;
526 * Port number, in network byte order.
533 * Plugin notifies transport (aka testcase) about its addresses
536 notify_address (void *cls,
540 struct GNUNET_TIME_Relative expires)
542 char * address = NULL;
544 struct Plugin_Address * pl_addr;
545 struct Plugin_Address * cur;
547 if (addrlen == (sizeof (struct IPv4HttpAddress)))
549 address = GNUNET_malloc (INET_ADDRSTRLEN);
550 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
551 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
554 if (addrlen == (sizeof (struct IPv6HttpAddress)))
556 address = GNUNET_malloc (INET6_ADDRSTRLEN);
557 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
558 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transport plugin notification for address: `%s':%u\n"),address,port);
562 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
563 pl_addr->addrlen = addrlen;
564 pl_addr->addr = GNUNET_malloc(addrlen);
565 memcpy(pl_addr->addr,addr,addrlen);
566 pl_addr->next = NULL;
568 if ( NULL == addr_head)
575 while (NULL != cur->next)
582 fail_notify_address_count++;
583 fail_notify_address = GNUNET_NO;
587 * Setup plugin environment
590 setup_plugin_environment ()
595 env.my_identity = &my_identity;
597 env.receive = &receive;
598 env.notify_address = ¬ify_address;
599 env.max_connections = max_connect_per_transport;
604 * Task shutting down testcase if it a timeout occurs
607 task_timeout (void *cls,
608 const struct GNUNET_SCHEDULER_TaskContext *tc)
610 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
611 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
620 static void pretty_printer_cb (void *cls,
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
626 fail_pretty_printer_count++;
634 * @param s scheduler to use
635 * @param c configuration to use
639 struct GNUNET_SCHEDULER_Handle *s,
641 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
647 unsigned long long tneigh;
648 struct Plugin_Address * cur;
649 struct Plugin_Address * tmp;
650 const char * addr_str;
651 unsigned int count_str_addr;
652 unsigned int suggest_res;
655 fail_pretty_printer = GNUNET_YES;
656 fail_notify_address = GNUNET_YES;
657 fail_addr_to_str = GNUNET_YES;
660 /* parse configuration */
662 GNUNET_CONFIGURATION_get_value_number (c,
667 GNUNET_CONFIGURATION_get_value_filename (c,
669 "HOSTKEY", &keyfile)))
671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 ("Transport service is lacking key configuration settings. Exiting.\n"));
674 GNUNET_SCHEDULER_shutdown (s);
678 max_connect_per_transport = (uint32_t) tneigh;
679 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
680 GNUNET_free (keyfile);
681 if (my_private_key == NULL)
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684 _("Transport service could not access hostkey. Exiting.\n"));
685 GNUNET_SCHEDULER_shutdown (s);
689 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
690 GNUNET_CRYPTO_hash (&my_public_key,
691 sizeof (my_public_key), &my_identity.hashPubKey);
693 /* load plugins... */
694 setup_plugin_environment ();
695 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
696 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
697 api = GNUNET_PLUGIN_load (libname, &env);
698 GNUNET_free (libname);
701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
702 _("Failed to load transport plugin for http\n"));
707 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
709 /* testing plugin functionality */
710 GNUNET_assert (0!=fail_notify_address_count);
711 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
713 /* testing pretty printer with all addresses obtained from the plugin*/
714 while (addr_head != NULL)
718 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
719 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
720 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
722 GNUNET_assert (GNUNET_OK == suggest_res);
723 GNUNET_assert (NULL != addr_str);
726 tmp = addr_head->next;
727 GNUNET_free (addr_head->addr);
728 GNUNET_free (addr_head);
729 GNUNET_free ((char *) addr_str);
732 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
733 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
734 fail_pretty_printer=GNUNET_NO;
735 fail_addr_to_str=GNUNET_NO;
737 /* Suggesting addresses with wrong port*/
738 struct IPv4HttpAddress failing_addr;
739 failing_addr.ipv4_addr = INADDR_LOOPBACK;
740 failing_addr.u_port = 0;
741 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
742 GNUNET_assert (GNUNET_SYSERR == suggest_res);
744 /* Suggesting addresses with wrong size*/
745 failing_addr.ipv4_addr = INADDR_LOOPBACK;
746 failing_addr.u_port = 0;
747 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
748 GNUNET_assert (GNUNET_SYSERR == suggest_res);
750 /* Suggesting addresses with wrong address*/
751 failing_addr.ipv4_addr = 0;
752 failing_addr.u_port = 12389;
753 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
754 GNUNET_assert (GNUNET_SYSERR == suggest_res);
756 /* test sending to client */
757 multi_handle = curl_multi_init();
759 /*building messages */
760 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
763 msg->buf = GNUNET_malloc (2048);
765 res = send_data (msg, "http://localhost:12389/");
767 /* testing finished, shutting down */
768 if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) )
778 * The main function for the transport service.
780 * @param argc number of arguments from the command line
781 * @param argv command line arguments
782 * @return 0 ok, 1 on error
785 main (int argc, char *const *argv)
788 static struct GNUNET_GETOPT_CommandLineOption options[] = {
789 GNUNET_GETOPT_OPTION_END
792 char *const argv_prog[] = {
793 "test_plugin_transport_http",
795 "test_plugin_transport_data_http.conf",
804 GNUNET_log_setup ("test_plugin_transport_http",
813 GNUNET_PROGRAM_run (5,
815 "test_plugin_transport_http",
816 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
818 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
823 /* end of test_plugin_transport_http.c */