-increase timeout
[oweals/gnunet.git] / src / transport / gnunet-transport.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20
21 /**
22  * @file src/transport/gnunet-transport.c
23  * @brief Tool to help configure, measure and control the transport subsystem.
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  *
27  * This utility can be used to test if a transport mechanism for
28  * GNUnet is properly configured.
29  */
30
31 #include "platform.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_nat_lib.h"
37
38 /**
39  * How long do we wait for the NAT test to report success?
40  * Should match NAT_SERVER_TIMEOUT in 'nat_test.c'.
41  */
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43
44 /**
45  * Which peer should we connect to?
46  */
47 static char *cpid;
48
49 /**
50  * Handle to transport service.
51  */
52 static struct GNUNET_TRANSPORT_Handle *handle;
53
54 /**
55  * Option -s.
56  */
57 static int benchmark_send;
58
59 /**
60  * Option -b.
61  */
62 static int benchmark_receive;
63
64 /**
65  * Option -l.
66  */
67 static int benchmark_receive;
68
69 /**
70  * Option -i.
71  */
72 static int iterate_connections;
73
74 /**
75  * Option -t.
76  */
77 static int test_configuration;
78
79 /**
80  * Option -m.
81  */
82 static int monitor_connections;
83
84 /**
85  * Option -n.
86  */
87 static int numeric;
88
89 /**
90  * Global return value (0 success).
91  */
92 static int ret;
93
94 /**
95  * Number of bytes of traffic we received so far.
96  */
97 static unsigned long long traffic_received;
98
99 /**
100  * Number of bytes of traffic we sent so far.
101  */
102 static unsigned long long traffic_sent;
103
104 /**
105  * Starting time of transmitting/receiving data.
106  */
107 static struct GNUNET_TIME_Absolute start_time;
108
109 /**
110  * Handle for current transmission request.
111  */
112 static struct GNUNET_TRANSPORT_TransmitHandle *th;
113
114 /**
115  * Identity of the peer we transmit to / connect to.
116  * (equivalent to 'cpid' string).
117  */
118 static struct GNUNET_PeerIdentity pid;
119
120 /**
121  * Task scheduled for cleanup / termination of the process.
122  */
123 static GNUNET_SCHEDULER_TaskIdentifier end;
124
125 /**
126  * Selected level of verbosity.
127  */
128 static int verbosity;
129
130 /**
131  * Resolver process handle.
132  */
133 struct GNUNET_OS_Process *resolver;
134
135 /**
136  * Number of tasks running that still need the resolver.
137  */
138 static unsigned int resolver_users;
139
140
141 /**
142  * Context for a plugin test.
143  */
144 struct TestContext
145 {
146
147   /**
148    * Handle to the active NAT test.
149    */
150   struct GNUNET_NAT_Test *tst;
151
152   /**
153    * Task identifier for the timeout.
154    */
155   GNUNET_SCHEDULER_TaskIdentifier tsk;
156
157   /**
158    * Name of plugin under test.
159    */
160   const char *name;
161
162 };
163
164
165 /**
166  * Display the result of the test.
167  *
168  * @param tc test context
169  * @param result GNUNET_YES on success
170  */
171 static void
172 display_test_result (struct TestContext *tc, int result)
173 {
174   if (GNUNET_YES != result)
175   {
176     FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
177   }
178   else
179   {
180     FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
181   }
182   if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
183   {
184     GNUNET_SCHEDULER_cancel (tc->tsk);
185     tc->tsk = GNUNET_SCHEDULER_NO_TASK;
186   }
187   if (NULL != tc->tst)
188   {
189     GNUNET_NAT_test_stop (tc->tst);
190     tc->tst = NULL;
191   }
192   GNUNET_free (tc);
193   resolver_users--;
194   if ((0 == resolver_users) && (NULL != resolver))
195   {
196     GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
197     GNUNET_OS_process_close (resolver);
198     resolver = NULL;
199   }
200 }
201
202
203 /**
204  * Function called by NAT on success.
205  * Clean up and update GUI (with success).
206  *
207  * @param cls test context
208  * @param success currently always GNUNET_OK
209  */
210 static void
211 result_callback (void *cls, int success)
212 {
213   struct TestContext *tc = cls;
214
215   display_test_result (tc, success);
216 }
217
218
219 /**
220  * Function called if NAT failed to confirm success.
221  * Clean up and update GUI (with failure).
222  *
223  * @param cls test context
224  * @param tc scheduler callback
225  */
226 static void
227 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 {
229   struct TestContext *tstc = cls;
230
231   tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
232   display_test_result (tstc, GNUNET_NO);
233 }
234
235
236 /**
237  * Test our plugin's configuration (NAT traversal, etc.).
238  *
239  * @param cfg configuration to test
240  */
241 static void
242 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
243 {
244   char *plugins;
245   char *tok;
246   unsigned long long bnd_port;
247   unsigned long long adv_port;
248   struct TestContext *tc;
249
250   if (GNUNET_OK !=
251       GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
252                                              &plugins))
253   {
254     FPRINTF (stderr,
255              "%s",
256              _
257              ("No transport plugins configured, peer will never communicate\n"));
258     ret = 4;
259     return;
260   }
261   for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
262   {
263     char section[12 + strlen (tok)];
264
265     GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
266     if (GNUNET_OK !=
267         GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
268     {
269       FPRINTF (stderr,
270                _("No port configured for plugin `%s', cannot test it\n"), tok);
271       continue;
272     }
273     if (GNUNET_OK !=
274         GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
275                                                &adv_port))
276       adv_port = bnd_port;
277     if (NULL == resolver)
278       resolver =
279           GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-resolver",
280                                    "gnunet-service-resolver", NULL);
281     resolver_users++;
282     GNUNET_RESOLVER_connect (cfg);
283     tc = GNUNET_malloc (sizeof (struct TestContext));
284     tc->name = GNUNET_strdup (tok);
285     tc->tst =
286         GNUNET_NAT_test_start (cfg,
287                                (0 ==
288                                 strcasecmp (tok,
289                                             "udp")) ? GNUNET_NO : GNUNET_YES,
290                                (uint16_t) bnd_port, (uint16_t) adv_port,
291                                &result_callback, tc);
292     if (NULL == tc->tst)
293     {
294       display_test_result (tc, GNUNET_SYSERR);
295       continue;
296     }
297     tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
298   }
299   GNUNET_free (plugins);
300 }
301
302
303 /**
304  * Shutdown, print statistics.
305  */
306 static void
307 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
308 {
309   struct GNUNET_TIME_Relative duration;
310
311   if (NULL != th)
312   {
313     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
314     th = NULL;
315   }
316   GNUNET_TRANSPORT_disconnect (handle);
317   if (benchmark_receive)
318   {
319     duration = GNUNET_TIME_absolute_get_duration (start_time);
320     FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %llu ms)\n"),
321              1000 * traffic_received / (1 + duration.rel_value),
322              traffic_received, (unsigned long long) duration.rel_value);
323   }
324   if (benchmark_send)
325   {
326     duration = GNUNET_TIME_absolute_get_duration (start_time);
327     FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %llu ms)\n"),
328              1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
329              (unsigned long long) duration.rel_value);
330   }
331 }
332
333
334 /**
335  * Function called to notify a client about the socket
336  * begin ready to queue more data.  "buf" will be
337  * NULL and "size" zero if the socket was closed for
338  * writing in the meantime.
339  *
340  * @param cls closure
341  * @param size number of bytes available in buf
342  * @param buf where the callee should write the message
343  * @return number of bytes written to buf
344  */
345 static size_t
346 transmit_data (void *cls, size_t size, void *buf)
347 {
348   struct GNUNET_MessageHeader *m = buf;
349
350   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
351   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
352   m->size = ntohs (size);
353   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
354   memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
355   traffic_sent += size;
356   th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, 32 * 1024, 0,
357                                                GNUNET_TIME_UNIT_FOREVER_REL,
358                                                &transmit_data, NULL);
359   if (verbosity > 0)
360     FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
361              GNUNET_i2s (&pid));
362   return size;
363 }
364
365
366 /**
367  * Function called to notify transport users that another
368  * peer connected to us.
369  *
370  * @param cls closure
371  * @param peer the peer that connected
372  * @param ats performance data
373  * @param ats_count number of entries in ats (excluding 0-termination)
374  */
375 static void
376 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
377                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
378 {
379   if (verbosity > 0)
380     FPRINTF (stdout, _("Connected to %s\n"), GNUNET_i2s (peer));
381   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
382     return;
383   ret = 0;
384   if (benchmark_send)
385   {
386     start_time = GNUNET_TIME_absolute_get ();
387     th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer, 32 * 1024, 0,
388                                                  GNUNET_TIME_UNIT_FOREVER_REL,
389                                                  &transmit_data, NULL);
390   }
391   else
392   {
393     /* all done, terminate instantly */
394     GNUNET_SCHEDULER_cancel (end);
395     end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
396   }
397 }
398
399
400 /**
401  * Function called to notify transport users that another
402  * peer disconnected from us.
403  *
404  * @param cls closure
405  * @param peer the peer that disconnected
406  */
407 static void
408 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
409 {
410   if (verbosity > 0)
411     FPRINTF (stdout, _("Disconnected from %s\n"), GNUNET_i2s (peer));
412   if ((0 == memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity))) &&
413       (NULL != th))
414   {
415     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
416     th = NULL;
417     GNUNET_SCHEDULER_cancel (end);
418     end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
419   }
420 }
421
422
423 /**
424  * Function called by the transport for each received message.
425  *
426  * @param cls closure
427  * @param peer (claimed) identity of the other peer
428  * @param message the message
429  * @param ats performance data
430  * @param ats_count number of entries in ats
431  */
432 static void
433 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
434                 const struct GNUNET_MessageHeader *message,
435                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
436 {
437   if (!benchmark_receive)
438     return;
439   if (verbosity > 0)
440     FPRINTF (stdout, _("Received %u bytes from %s\n"),
441              (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
442   if (traffic_received == 0)
443     start_time = GNUNET_TIME_absolute_get ();
444   traffic_received += ntohs (message->size);
445 }
446
447 void
448 process_string (void *cls, const char *address)
449 {
450   struct GNUNET_HELLO_Address *addrcp = cls;
451
452   if ((address != NULL))
453   {
454     FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
455   }
456   else
457   {
458     /* done */
459     GNUNET_free (addrcp);
460   }
461 }
462
463 /**
464  * Function to call with a binary address
465  *
466  * @param cls closure
467  * @param peer identity of the peer
468  * @param address binary address (NULL on disconnect)
469  */
470 static void
471 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
472                  const struct GNUNET_HELLO_Address *address)
473 {
474   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
475
476   if (peer == NULL)
477   {
478     /* done */
479     return;
480   }
481
482   if (address == NULL)
483   {
484     FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
485     return;
486   }
487
488   /* Resolve address to string */
489   GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
490                                       GNUNET_TIME_UNIT_MINUTES, &process_string,
491                                       GNUNET_HELLO_address_copy(address));
492 }
493
494
495 /**
496  * Task run in monitor mode when the user presses CTRL-C to abort.
497  * Stops monitoring activity.
498  * 
499  * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
500  * @param tc scheduler context
501  */
502 static void
503 shutdown_task (void *cls,
504                const struct GNUNET_SCHEDULER_TaskContext *tc)
505 {
506   struct GNUNET_TRANSPORT_PeerIterateContext *pic = cls;
507
508   GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pic);  
509 }
510
511
512
513 /**
514  * Main function that will be run by the scheduler.
515  *
516  * @param cls closure
517  * @param args remaining command-line arguments
518  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
519  * @param cfg configuration
520  */
521 static void
522 run (void *cls, char *const *args, const char *cfgfile,
523      const struct GNUNET_CONFIGURATION_Handle *cfg)
524 {
525   if (test_configuration)
526   {
527     do_test_configuration (cfg);
528   }
529   if (benchmark_send && (NULL == cpid))
530   {
531     FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
532              "-s", "-C");
533     return;
534   }
535   if (NULL != cpid)
536   {
537     ret = 1;
538     if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
539     {
540       FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
541       return;
542     }
543     handle =
544         GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
545                                   &notify_connect, &notify_disconnect);
546     GNUNET_TRANSPORT_try_connect (handle, &pid);
547     end =
548         GNUNET_SCHEDULER_add_delayed (benchmark_send ?
549                                       GNUNET_TIME_UNIT_FOREVER_REL :
550                                       GNUNET_TIME_UNIT_SECONDS, &do_disconnect,
551                                       NULL);
552   }
553   else if (benchmark_receive)
554   {
555     handle =
556         GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
557                                   &notify_connect, &notify_disconnect);
558     GNUNET_TRANSPORT_try_connect (handle, &pid);
559     end =
560         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
561                                       &do_disconnect, NULL);
562   }
563   if (iterate_connections)
564   {
565     GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL, GNUNET_YES,
566                                                 GNUNET_TIME_UNIT_MINUTES,
567                                                 &process_address, (void *) cfg);
568   }
569   if (monitor_connections)
570   {
571     struct GNUNET_TRANSPORT_PeerIterateContext *pic;
572
573     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL, GNUNET_NO,
574                                                       GNUNET_TIME_UNIT_FOREVER_REL,
575                                                       &process_address, (void *) cfg);
576     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
577                                   &shutdown_task,
578                                   pic);
579   }
580 }
581
582
583 int
584 main (int argc, char *const *argv)
585 {
586   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
587     {'b', "benchmark", NULL,
588      gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
589      0, &GNUNET_GETOPT_set_one, &benchmark_receive},
590     {'C', "connect", "PEER",
591      gettext_noop ("try to connect to the given peer"),
592      1, &GNUNET_GETOPT_set_string, &cpid},
593     {'i', "information", NULL,
594      gettext_noop ("provide information about all current connections (once)"),
595      0, &GNUNET_GETOPT_set_one, &iterate_connections},
596     {'m', "monitor", NULL,
597      gettext_noop ("provide information about all current connections (continuously)"),
598      0, &GNUNET_GETOPT_set_one, &monitor_connections},
599     {'n', "numeric", NULL,
600      gettext_noop ("do not resolve hostnames"),
601      0, &GNUNET_GETOPT_set_one, &numeric},
602     {'s', "send", NULL,
603      gettext_noop
604      ("send data for benchmarking to the other peer (until CTRL-C)"),
605      0, &GNUNET_GETOPT_set_one, &benchmark_send},
606     {'t', "test", NULL,
607      gettext_noop ("test transport configuration (involves external server)"),
608      0, &GNUNET_GETOPT_set_one, &test_configuration},
609     GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
610     GNUNET_GETOPT_OPTION_END
611   };
612   return (GNUNET_OK ==
613           GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
614                               gettext_noop
615                               ("Direct access to transport service."), options,
616                               &run, NULL)) ? ret : 1;
617 }
618
619
620 /* end of gnunet-transport.c */