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, 3)
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_download;
118 static unsigned int timeout_count;
121 * Did the test pass or fail?
130 * Initialize Environment for this plugin
132 static struct GNUNET_TIME_Relative
134 const struct GNUNET_PeerIdentity * peer,
135 const struct GNUNET_MessageHeader * message,
137 struct Session *session,
138 const char *sender_address,
139 uint16_t sender_address_len)
142 return GNUNET_TIME_UNIT_ZERO;
146 notify_address (void *cls,
150 struct GNUNET_TIME_Relative expires)
156 * Simple example test that invokes
157 * the check_address function of the plugin.
159 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
160 * isn't enabled (eg. FreeBSD > 4)[TESTING]
166 /* if (stat_get_handle != NULL)
168 GNUNET_STATISTICS_get_cancel(stat_get_handle);
171 /* if ( NULL!=stats )GNUNET_STATISTICS_destroy (stats, GNUNET_YES); */
172 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
173 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
174 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
177 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
179 GNUNET_SCHEDULER_shutdown(sched);
180 /* FIXME: */ fail = GNUNET_NO;
185 setup_plugin_environment ()
190 env.my_identity = &my_identity;
192 env.receive = &receive;
193 env.notify_address = ¬ify_address;
194 env.max_connections = max_connect_per_transport;
199 process_stat (void *cls,
200 const char *subsystem,
205 stat_get_handle = NULL;
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin failed \n");
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin not failed \n");
225 cont_func (void *cls, int success)
227 stat_get_handle = NULL;
232 * Task that checks if we should try to download a hostlist.
233 * If so, we initiate the download, otherwise we schedule
234 * this task again for a later time.
237 task_timeout (void *cls,
238 const struct GNUNET_SCHEDULER_TaskContext *tc)
240 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
241 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
244 if ( timeout_count > 15 )
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n", timeout_count);
253 /* stat_get_handle = GNUNET_STATISTICS_get (stats,
255 gettext_noop("shutdown"),
256 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
261 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_timeout, NULL);
266 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
270 /* in real-world cases, this would probably get this data differently
271 as this fread() stuff is exactly what the library already would do
272 by default internally */
273 retcode = fread(ptr, size, nmemb, stream);
275 fprintf(stderr, "*** We read %d bytes from file\n", (int) retcode);
290 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
294 struct CBC * cbc = ptr;
296 len = strlen(cbc->buf);
298 if (( cbc->pos == len) && (len < (size * nmemb)))
300 memcpy(stream, cbc->buf, len+1);
307 static int execute (char * url)
316 struct stat file_info;
321 char *file = "curl.c";
327 const char * txt = "Hello World!";
328 memcpy(cbc.buf,txt,strlen(txt)+1);
329 //fprintf(stderr,"%s %u\n",src,strlen(src));
331 /* get the file size of the local file */
332 hd = open(file, O_RDONLY) ;
333 fstat(hd, &file_info);
336 /* get a FILE * of the same file, could also be made with
337 fdopen() from the previous descriptor, but hey this is just
339 hd_src = fopen(file, "rb");
340 //printf("size: %u \n", (curl_off_t) file_info.st_size);
342 /* get a curl handle */
343 curl_handle = curl_easy_init();
344 if( NULL == curl_handle)
346 printf("easy_init failed \n");
349 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
350 //curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, ©Buffer);
351 //curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, &cbc);
352 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
353 curl_easy_setopt (curl_handle, CURLOPT_READDATA, &cbc);
354 curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L);
355 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
356 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
357 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) strlen(txt));
359 //curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
361 multi_handle = curl_multi_init();
362 curl_multi_add_handle(multi_handle, curl_handle);
364 while(CURLM_CALL_MULTI_PERFORM ==
365 curl_multi_perform(multi_handle, &still_running));
369 struct timeval timeout;
370 int rc; /* select() return code */
381 /* set a suitable timeout to play around with */
385 /* get file descriptors from the transfers */
386 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
388 /* In a real-world program you OF COURSE check the return code of the
389 function calls. On success, the value of maxfd is guaranteed to be
390 greater or equal than -1. We call select(maxfd + 1, ...), specially in
391 case of (maxfd == -1), we call select(0, ...), which is basically equal
394 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
402 /* timeout, do something else */
405 /* one or more of curl's file descriptors say there's data to read
407 while(CURLM_CALL_MULTI_PERFORM ==
408 curl_multi_perform(multi_handle, &still_running));
413 /* See how the transfers went */
414 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
415 if (msg->msg == CURLMSG_DONE) {
418 /* Find out which handle this message is about */
419 for (idx=0; idx<1; idx++) {
420 found = (msg->easy_handle == curl_handle);
427 printf("HTTP transfer completed with status %d\n", msg->data.result);
433 curl_multi_cleanup(multi_handle);
435 curl_easy_cleanup(curl_handle);
437 fclose(hd_src); /* close the local file */
444 * Task that checks if we should try to download a hostlist.
445 * If so, we initiate the download, otherwise we schedule
446 * this task again for a later time.
449 task_download (void *cls,
450 const struct GNUNET_SCHEDULER_TaskContext *tc)
452 ti_download = GNUNET_SCHEDULER_NO_TASK;
453 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
456 execute ("http://localhost:12389/Q7BO0ELEHAT8JNENQ90112G0TACH2H2HIR4IJ2JQ28U6FV14CK44EVP26FVAEALO7HIRJFLFE6709RP6IITM64B0FU7J4RA8KPNDKN8");
466 * @param s scheduler to use
467 * @param c configuration to use
471 struct GNUNET_SCHEDULER_Handle *s,
473 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
479 unsigned long long tneigh;
481 /* settings up statistics */
482 /* stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486 _("Failed to retrieve statistics handle\n"));
492 /* parse configuration */
494 GNUNET_CONFIGURATION_get_value_number (c,
499 GNUNET_CONFIGURATION_get_value_filename (c,
501 "HOSTKEY", &keyfile)))
503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
505 ("Transport service is lacking key configuration settings. Exiting.\n"));
506 GNUNET_SCHEDULER_shutdown (s);
509 max_connect_per_transport = (uint32_t) tneigh;
510 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
511 GNUNET_free (keyfile);
512 if (my_private_key == NULL)
514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
516 ("Transport service could not access hostkey. Exiting.\n"));
517 GNUNET_SCHEDULER_shutdown (s);
520 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
521 GNUNET_CRYPTO_hash (&my_public_key,
522 sizeof (my_public_key), &my_identity.hashPubKey);
524 /* load plugins... */
525 setup_plugin_environment ();
526 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
527 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
528 api = GNUNET_PLUGIN_load (libname, &env);
529 GNUNET_free (libname);
532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
533 _("Failed to load transport plugin for http\n"));
538 ti_timeout = GNUNET_SCHEDULER_add_now (sched, &task_timeout, NULL);
539 //ti_download = GNUNET_SCHEDULER_add_now (sched, &task_download, NULL);
547 * The main function for the transport service.
549 * @param argc number of arguments from the command line
550 * @param argv command line arguments
551 * @return 0 ok, 1 on error
554 main (int argc, char *const *argv)
557 static struct GNUNET_GETOPT_CommandLineOption options[] = {
558 GNUNET_GETOPT_OPTION_END
561 char *const argv_prog[] = {
562 "test_plugin_transport_http",
564 "test_plugin_transport_data_http.conf",
573 GNUNET_log_setup ("test_plugin_transport_http",
580 /* GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting statistics service\n");
581 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
582 "gnunet-service-statistics",
584 "-c", "test_plugin_transport_data_http.conf", NULL);
589 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to start statistics service\n");
595 GNUNET_PROGRAM_run (5,
597 "test_plugin_transport_http",
598 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
600 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
603 /* if (0 != PLIBC_KILL (pid, SIGTERM))
605 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "Failed to kill statistics service");
608 if (GNUNET_OS_process_wait(pid) != GNUNET_OK)
609 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
610 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Killed statistics service\n");*/
614 /* end of test_plugin_transport_http.c */