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 STAT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
60 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
65 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
70 static struct GNUNET_PeerIdentity my_identity;
75 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
81 struct GNUNET_SCHEDULER_Handle *sched;
84 * Our statistics handle.
86 struct GNUNET_STATISTICS_Handle *stats;
92 const struct GNUNET_CONFIGURATION_Handle *cfg;
95 * Number of neighbours we'd like to have.
97 static uint32_t max_connect_per_transport;
100 * Environment for this plugin.
102 static struct GNUNET_TRANSPORT_PluginEnvironment env;
105 *handle for the api provided by this plugin
107 static struct GNUNET_TRANSPORT_PluginFunctions *api;
110 * ID of the task controlling the testcase timeout
112 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
115 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
117 static unsigned int timeout_count;
118 static unsigned int recieved;
119 const struct GNUNET_PeerIdentity * p;
122 * Did the test pass or fail?
131 task_send (void *cls,
132 const struct GNUNET_SCHEDULER_TaskContext *tc)
134 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
135 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
138 struct GNUNET_MessageHeader * msg = cls;
139 unsigned int len = ntohs(msg->size);
140 const char * msgc = (const char *) msg;
142 api->send(api->cls,p,msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, NULL, NULL);
147 * Initialize Environment for this plugin
149 static struct GNUNET_TIME_Relative
151 const struct GNUNET_PeerIdentity * peer,
152 const struct GNUNET_MessageHeader * message,
154 struct Session *session,
155 const char *sender_address,
156 uint16_t sender_address_len)
158 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));
162 recieved = GNUNET_YES;
164 void * c = (void *) message;
166 ti_send =GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_send, c);
167 return GNUNET_TIME_UNIT_ZERO;
171 notify_address (void *cls,
175 struct GNUNET_TIME_Relative expires)
181 * Simple example test that invokes
182 * the check_address function of the plugin.
184 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
185 * isn't enabled (eg. FreeBSD > 4)[TESTING]
191 /* if (stat_get_handle != NULL)
193 GNUNET_STATISTICS_get_cancel(stat_get_handle);
196 /* if ( NULL!=stats )GNUNET_STATISTICS_destroy (stats, GNUNET_YES); */
197 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
198 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
199 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
202 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
204 GNUNET_SCHEDULER_shutdown(sched);
205 /* FIXME: */ fail = GNUNET_NO;
210 setup_plugin_environment ()
215 env.my_identity = &my_identity;
217 env.receive = &receive;
218 env.notify_address = ¬ify_address;
219 env.max_connections = max_connect_per_transport;
224 process_stat (void *cls,
225 const char *subsystem,
230 stat_get_handle = NULL;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin failed \n");
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin not failed \n");
250 cont_func (void *cls, int success)
252 stat_get_handle = NULL;
259 * Task that checks if we should try to download a hostlist.
260 * If so, we initiate the download, otherwise we schedule
261 * this task again for a later time.
264 task_timeout (void *cls,
265 const struct GNUNET_SCHEDULER_TaskContext *tc)
267 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
268 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
271 if ( timeout_count > 1 )
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n", timeout_count);
280 /* stat_get_handle = GNUNET_STATISTICS_get (stats,
282 gettext_noop("shutdown"),
283 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
288 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_timeout, NULL);
293 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
297 /* in real-world cases, this would probably get this data differently
298 as this fread() stuff is exactly what the library already would do
299 by default internally */
300 retcode = fread(ptr, size, nmemb, stream);
302 fprintf(stderr, "*** We read %d bytes from file\n", (int) retcode);
317 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
321 struct CBC * cbc = ptr;
323 len = strlen(cbc->buf);
325 if (( cbc->pos == len) && (len < (size * nmemb)))
327 memcpy(stream, cbc->buf, len+1);
334 static int execute (char * url)
343 struct stat file_info;
348 char *file = "curl.c";
354 const char * txt = "Hello World!";
355 memcpy(cbc.buf,txt,strlen(txt)+1);
356 //fprintf(stderr,"%s %u\n",src,strlen(src));
358 /* get the file size of the local file */
359 hd = open(file, O_RDONLY) ;
360 fstat(hd, &file_info);
363 /* get a FILE * of the same file, could also be made with
364 fdopen() from the previous descriptor, but hey this is just
366 hd_src = fopen(file, "rb");
367 //printf("size: %u \n", (curl_off_t) file_info.st_size);
369 /* get a curl handle */
370 curl_handle = curl_easy_init();
371 if( NULL == curl_handle)
373 printf("easy_init failed \n");
376 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
377 //curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, ©Buffer);
378 //curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, &cbc);
379 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
380 curl_easy_setopt (curl_handle, CURLOPT_READDATA, &cbc);
381 curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L);
382 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
383 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
384 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) strlen(txt));
386 //curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
388 multi_handle = curl_multi_init();
389 curl_multi_add_handle(multi_handle, curl_handle);
391 while(CURLM_CALL_MULTI_PERFORM ==
392 curl_multi_perform(multi_handle, &still_running));
396 struct timeval timeout;
397 int rc; /* select() return code */
408 /* set a suitable timeout to play around with */
412 /* get file descriptors from the transfers */
413 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
415 /* In a real-world program you OF COURSE check the return code of the
416 function calls. On success, the value of maxfd is guaranteed to be
417 greater or equal than -1. We call select(maxfd + 1, ...), specially in
418 case of (maxfd == -1), we call select(0, ...), which is basically equal
421 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
429 /* timeout, do something else */
432 /* one or more of curl's file descriptors say there's data to read
434 while(CURLM_CALL_MULTI_PERFORM ==
435 curl_multi_perform(multi_handle, &still_running));
440 /* See how the transfers went */
441 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
442 if (msg->msg == CURLMSG_DONE) {
445 /* Find out which handle this message is about */
446 for (idx=0; idx<1; idx++) {
447 found = (msg->easy_handle == curl_handle);
454 printf("HTTP transfer completed with status %d\n", msg->data.result);
460 curl_multi_cleanup(multi_handle);
462 curl_easy_cleanup(curl_handle);
464 fclose(hd_src); /* close the local file */
471 * Task that checks if we should try to download a hostlist.
472 * If so, we initiate the download, otherwise we schedule
473 * this task again for a later time.
476 task_download (void *cls,
477 const struct GNUNET_SCHEDULER_TaskContext *tc)
479 ti_download = GNUNET_SCHEDULER_NO_TASK;
480 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
483 execute ("http://localhost:12389/Q7BO0ELEHAT8JNENQ90112G0TACH2H2HIR4IJ2JQ28U6FV14CK44EVP26FVAEALO7HIRJFLFE6709RP6IITM64B0FU7J4RA8KPNDKN8");
493 * @param s scheduler to use
494 * @param c configuration to use
498 struct GNUNET_SCHEDULER_Handle *s,
500 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
506 unsigned long long tneigh;
508 /* settings up statistics */
509 /* stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513 _("Failed to retrieve statistics handle\n"));
519 /* parse configuration */
521 GNUNET_CONFIGURATION_get_value_number (c,
526 GNUNET_CONFIGURATION_get_value_filename (c,
528 "HOSTKEY", &keyfile)))
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
532 ("Transport service is lacking key configuration settings. Exiting.\n"));
533 GNUNET_SCHEDULER_shutdown (s);
536 max_connect_per_transport = (uint32_t) tneigh;
537 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
538 GNUNET_free (keyfile);
539 if (my_private_key == NULL)
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
543 ("Transport service could not access hostkey. Exiting.\n"));
544 GNUNET_SCHEDULER_shutdown (s);
547 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
548 GNUNET_CRYPTO_hash (&my_public_key,
549 sizeof (my_public_key), &my_identity.hashPubKey);
551 /* load plugins... */
552 setup_plugin_environment ();
553 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
554 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
555 api = GNUNET_PLUGIN_load (libname, &env);
556 GNUNET_free (libname);
559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
560 _("Failed to load transport plugin for http\n"));
565 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_timeout, NULL);
566 //ti_download = GNUNET_SCHEDULER_add_now (sched, &task_download, NULL);
574 * The main function for the transport service.
576 * @param argc number of arguments from the command line
577 * @param argv command line arguments
578 * @return 0 ok, 1 on error
581 main (int argc, char *const *argv)
584 static struct GNUNET_GETOPT_CommandLineOption options[] = {
585 GNUNET_GETOPT_OPTION_END
588 char *const argv_prog[] = {
589 "test_plugin_transport_http",
591 "test_plugin_transport_data_http.conf",
600 GNUNET_log_setup ("test_plugin_transport_http",
607 /* GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting statistics service\n");
608 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
609 "gnunet-service-statistics",
611 "-c", "test_plugin_transport_data_http.conf", NULL);
616 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to start statistics service\n");
622 GNUNET_PROGRAM_run (5,
624 "test_plugin_transport_http",
625 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
627 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
630 /* if (0 != PLIBC_KILL (pid, SIGTERM))
632 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "Failed to kill statistics service");
635 if (GNUNET_OS_process_wait(pid) != GNUNET_OK)
636 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
637 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Killed statistics service\n");*/
641 /* end of test_plugin_transport_http.c */