0297b4624a1fa2689a2630686b24e4691596dc3b
[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 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
44 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
45
46 /**
47  * Benchmarking block size in KB
48  */
49 #define BLOCKSIZE 4
50
51
52 /**
53  * Which peer should we connect to?
54  */
55 static char *cpid;
56
57 /**
58  * Handle to transport service.
59  */
60 static struct GNUNET_TRANSPORT_Handle *handle;
61
62 /**
63  * Option -s.
64  */
65 static int benchmark_send;
66
67 /**
68  * Option -b.
69  */
70 static int benchmark_receive;
71
72 /**
73  * Option -l.
74  */
75 static int benchmark_receive;
76
77 /**
78  * Option -i.
79  */
80 static int iterate_connections;
81
82 /**
83  * Option -t.
84  */
85 static int test_configuration;
86
87 /**
88  * Option -c.
89  */
90 static int monitor_connects;
91
92 /**
93  * Option -m.
94  */
95 static int monitor_connections;
96
97 /**
98  * Option -C.
99  */
100 static int try_connect;
101
102 /**
103  * Option -n.
104  */
105 static int numeric;
106
107 /**
108  * Global return value (0 success).
109  */
110 static int ret;
111
112 /**
113  * Current number of connections in monitor mode
114  */
115 static int monitor_connect_counter;
116
117 /**
118  * Number of bytes of traffic we received so far.
119  */
120 static unsigned long long traffic_received;
121
122 /**
123  * Number of bytes of traffic we sent so far.
124  */
125 static unsigned long long traffic_sent;
126
127 /**
128  * Starting time of transmitting/receiving data.
129  */
130 static struct GNUNET_TIME_Absolute start_time;
131
132 /**
133  * Handle for current transmission request.
134  */
135 static struct GNUNET_TRANSPORT_TransmitHandle *th;
136
137 /**
138  *
139  */
140 struct GNUNET_TRANSPORT_PeerIterateContext *pic;
141
142 /**
143  * Identity of the peer we transmit to / connect to.
144  * (equivalent to 'cpid' string).
145  */
146 static struct GNUNET_PeerIdentity pid;
147
148 /**
149  * Task scheduled for cleanup / termination of the process.
150  */
151 static GNUNET_SCHEDULER_TaskIdentifier end;
152
153 /**
154  * Task for operation timeout
155  */
156 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
157
158
159 static struct GNUNET_CONTAINER_MultiHashMap *peers;
160
161 /**
162  * Selected level of verbosity.
163  */
164 static int verbosity;
165
166 /**
167  * Resolver process handle.
168  */
169 struct GNUNET_OS_Process *resolver;
170
171 /**
172  * Number of tasks running that still need the resolver.
173  */
174 static unsigned int resolver_users;
175
176 /**
177  * Number of address resolutions pending
178  */
179 static unsigned int address_resolutions;
180
181 /**
182  * Address resolutions pending in progress
183  */
184 static unsigned int address_resolution_in_progress;
185
186 /**
187  * Context for a plugin test.
188  */
189 struct TestContext
190 {
191
192   /**
193    * Handle to the active NAT test.
194    */
195   struct GNUNET_NAT_Test *tst;
196
197   /**
198    * Task identifier for the timeout.
199    */
200   GNUNET_SCHEDULER_TaskIdentifier tsk;
201
202   /**
203    * Name of plugin under test.
204    */
205   const char *name;
206
207 };
208
209
210 /**
211  * Task run in monitor mode when the user presses CTRL-C to abort.
212  * Stops monitoring activity.
213  *
214  * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
215  * @param tc scheduler context
216  */
217 static void
218 shutdown_task (void *cls,
219                const struct GNUNET_SCHEDULER_TaskContext *tc)
220 {
221   struct GNUNET_TIME_Relative duration;
222   end = GNUNET_SCHEDULER_NO_TASK;
223   if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
224   {
225       GNUNET_SCHEDULER_cancel (op_timeout);
226       op_timeout = GNUNET_SCHEDULER_NO_TASK;
227   }
228   if (NULL != pic)
229   {
230       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pic);
231       pic = NULL;
232   }
233   if (NULL != th)
234   {
235     GNUNET_TRANSPORT_notify_transmit_ready_cancel(th);
236     th = NULL;
237   }
238   if (NULL != handle)
239   {
240     GNUNET_TRANSPORT_disconnect(handle);
241     handle = NULL;
242   }
243   if (NULL != peers)
244   {
245     GNUNET_CONTAINER_multihashmap_destroy (peers);
246     peers = NULL;
247   }
248   if (benchmark_send)
249   {
250     duration = GNUNET_TIME_absolute_get_duration (start_time);
251     FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
252              1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
253              GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
254   }
255   if (benchmark_receive)
256   {
257     duration = GNUNET_TIME_absolute_get_duration (start_time);
258     FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
259              1000 * traffic_received / (1 + duration.rel_value),
260              traffic_received,
261              GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
262   }
263 }
264
265
266
267 static void
268 operation_timeout (void *cls,
269                const struct GNUNET_SCHEDULER_TaskContext *tc)
270 {
271   op_timeout = GNUNET_SCHEDULER_NO_TASK;
272   if ((try_connect) || (benchmark_send) || (benchmark_receive))
273   {
274       FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_h2s_full (&pid.hashPubKey));
275       if (GNUNET_SCHEDULER_NO_TASK != end)
276         GNUNET_SCHEDULER_cancel (end);
277       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
278       ret = 1;
279       return;
280   }
281 }
282
283
284
285 /**
286  * Display the result of the test.
287  *
288  * @param tc test context
289  * @param result GNUNET_YES on success
290  */
291 static void
292 display_test_result (struct TestContext *tc, int result)
293 {
294   if (GNUNET_YES != result)
295   {
296     FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
297   }
298   else
299   {
300     FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
301   }
302   if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
303   {
304     GNUNET_SCHEDULER_cancel (tc->tsk);
305     tc->tsk = GNUNET_SCHEDULER_NO_TASK;
306   }
307   if (NULL != tc->tst)
308   {
309     GNUNET_NAT_test_stop (tc->tst);
310     tc->tst = NULL;
311   }
312   GNUNET_free (tc);
313   resolver_users--;
314   if ((0 == resolver_users) && (NULL != resolver))
315   {
316     GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
317     GNUNET_OS_process_destroy (resolver);
318     resolver = NULL;
319   }
320 }
321
322
323 /**
324  * Function called by NAT on success.
325  * Clean up and update GUI (with success).
326  *
327  * @param cls test context
328  * @param success currently always GNUNET_OK
329  */
330 static void
331 result_callback (void *cls, int success)
332 {
333   struct TestContext *tc = cls;
334
335   display_test_result (tc, success);
336 }
337
338
339 /**
340  * Function called if NAT failed to confirm success.
341  * Clean up and update GUI (with failure).
342  *
343  * @param cls test context
344  * @param tc scheduler callback
345  */
346 static void
347 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348 {
349   struct TestContext *tstc = cls;
350
351   tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
352   display_test_result (tstc, GNUNET_NO);
353 }
354
355
356 /**
357  * Test our plugin's configuration (NAT traversal, etc.).
358  *
359  * @param cfg configuration to test
360  */
361 static void
362 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
363 {
364   char *plugins;
365   char *tok;
366   unsigned long long bnd_port;
367   unsigned long long adv_port;
368   struct TestContext *tc;
369
370   if (GNUNET_OK !=
371       GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
372                                              &plugins))
373   {
374     FPRINTF (stderr,
375              "%s",
376              _
377              ("No transport plugins configured, peer will never communicate\n"));
378     ret = 4;
379     return;
380   }
381   for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
382   {
383     char section[12 + strlen (tok)];
384
385     GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
386     if (GNUNET_OK !=
387         GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
388     {
389       FPRINTF (stderr,
390                _("No port configured for plugin `%s', cannot test it\n"), tok);
391       continue;
392     }
393     if (GNUNET_OK !=
394         GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
395                                                &adv_port))
396       adv_port = bnd_port;
397     if (NULL == resolver)
398       resolver =
399           GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
400                                    "gnunet-service-resolver", NULL);
401     resolver_users++;
402     GNUNET_RESOLVER_connect (cfg);
403     tc = GNUNET_malloc (sizeof (struct TestContext));
404     tc->name = GNUNET_strdup (tok);
405     tc->tst =
406         GNUNET_NAT_test_start (cfg,
407                                (0 ==
408                                 strcasecmp (tok,
409                                             "udp")) ? GNUNET_NO : GNUNET_YES,
410                                (uint16_t) bnd_port, (uint16_t) adv_port,
411                                &result_callback, tc);
412     if (NULL == tc->tst)
413     {
414       display_test_result (tc, GNUNET_SYSERR);
415       continue;
416     }
417     tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
418   }
419   GNUNET_free (plugins);
420 }
421
422 /**
423  * Function called to notify a client about the socket
424  * begin ready to queue more data.  "buf" will be
425  * NULL and "size" zero if the socket was closed for
426  * writing in the meantime.
427  *
428  * @param cls closure
429  * @param size number of bytes available in buf
430  * @param buf where the callee should write the message
431  * @return number of bytes written to buf
432  */
433 static size_t
434 transmit_data (void *cls, size_t size, void *buf)
435 {
436   struct GNUNET_MessageHeader *m = buf;
437
438   if ((NULL == buf) && (0 == size))
439   {
440     th = NULL;
441     return 0;
442   }
443
444   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
445   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
446   m->size = ntohs (size);
447   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
448   memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
449   traffic_sent += size;
450   th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, BLOCKSIZE * 1024, 0,
451                                                GNUNET_TIME_UNIT_FOREVER_REL,
452                                                &transmit_data, NULL);
453   if (verbosity > 0)
454     FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
455              GNUNET_i2s (&pid));
456   return size;
457 }
458
459
460 /**
461  * Function called to notify transport users that another
462  * peer connected to us.
463  *
464  * @param cls closure
465  * @param peer the peer that connected
466  * @param ats performance data
467  * @param ats_count number of entries in ats (excluding 0-termination)
468  */
469 static void
470 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
471                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
472 {
473   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
474     return;
475   ret = 0;
476   if (try_connect)
477   {
478       /* all done, terminate instantly */
479       FPRINTF (stdout, _("Successfully connected to `%s'\n"), GNUNET_h2s_full (&peer->hashPubKey));
480       ret = 0;
481
482       if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
483       {
484         GNUNET_SCHEDULER_cancel (op_timeout);
485         op_timeout = GNUNET_SCHEDULER_NO_TASK;
486       }
487
488       if (GNUNET_SCHEDULER_NO_TASK != end)
489         GNUNET_SCHEDULER_cancel (end);
490       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
491       return;
492   }
493   if (benchmark_send)
494   {
495     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
496     {
497       GNUNET_SCHEDULER_cancel (op_timeout);
498       op_timeout = GNUNET_SCHEDULER_NO_TASK;
499     }
500     if (verbosity > 0)
501       FPRINTF (stdout, _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
502           GNUNET_i2s (&pid), BLOCKSIZE);
503     start_time = GNUNET_TIME_absolute_get ();
504     if (NULL == th)
505       th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
506                                                    BLOCKSIZE * 1024, 0,
507                                                    GNUNET_TIME_UNIT_FOREVER_REL,
508                                                    &transmit_data, NULL);
509     else
510       GNUNET_break (0);
511     return;
512   }
513 }
514
515
516 /**
517  * Function called to notify transport users that another
518  * peer disconnected from us.
519  *
520  * @param cls closure
521  * @param peer the peer that disconnected
522  */
523 static void
524 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
525 {
526   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
527     return;
528
529   if (NULL != th)
530   {
531     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
532     th = NULL;
533   }
534   if (benchmark_send)
535   {
536       FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"), GNUNET_i2s (&pid));
537       if (GNUNET_SCHEDULER_NO_TASK != end)
538         GNUNET_SCHEDULER_cancel (end);
539       return;
540   }
541 }
542
543 /**
544  * Function called to notify transport users that another
545  * peer connected to us.
546  *
547  * @param cls closure
548  * @param peer the peer that connected
549  * @param ats performance data
550  * @param ats_count number of entries in ats (excluding 0-termination)
551  */
552 static void
553 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
554                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
555 {
556   monitor_connect_counter ++;
557   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
558   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
559
560   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
561            now_str,
562            _("Connected to"),
563            GNUNET_i2s (peer),
564            monitor_connect_counter);
565 }
566
567
568 /**
569  * Function called to notify transport users that another
570  * peer disconnected from us.
571  *
572  * @param cls closure
573  * @param peer the peer that disconnected
574  */
575 static void
576 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
577 {
578   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
579   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
580
581   GNUNET_assert (monitor_connect_counter > 0);
582   monitor_connect_counter --;
583
584   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
585            now_str,
586            _("Disconnected from"),
587            GNUNET_i2s (peer),
588            monitor_connect_counter);
589 }
590
591
592
593 /**
594  * Function called by the transport for each received message.
595  *
596  * @param cls closure
597  * @param peer (claimed) identity of the other peer
598  * @param message the message
599  * @param ats performance data
600  * @param ats_count number of entries in ats
601  */
602 static void
603 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
604                 const struct GNUNET_MessageHeader *message,
605                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
606 {
607   if (benchmark_receive)
608   {
609     if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
610       return;
611     if (verbosity > 0)
612       FPRINTF (stdout, _("Received %u bytes from %s\n"),
613                (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
614
615     if (traffic_received == 0)
616       start_time = GNUNET_TIME_absolute_get ();
617     traffic_received += ntohs (message->size);
618     return;
619   }
620 }
621
622 struct ResolutionContext
623 {
624   struct GNUNET_HELLO_Address *addrcp;
625
626   int printed;
627 };
628
629
630 static void
631 process_string (void *cls, const char *address)
632 {
633   struct ResolutionContext *rc = cls;
634   struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
635
636   if (address != NULL)
637   {
638     FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
639     rc->printed = GNUNET_YES;
640   }
641   else
642   {
643     /* done */
644     GNUNET_assert (address_resolutions > 0);
645     address_resolutions --;
646     if (GNUNET_NO == rc->printed)
647       FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
648     GNUNET_free (rc->addrcp);
649     GNUNET_free (rc);
650     if ((0 == address_resolutions) && (iterate_connections))
651     {
652         if (GNUNET_SCHEDULER_NO_TASK != end)
653           GNUNET_SCHEDULER_cancel (end);
654         ret = 0;
655         end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
656     }
657   }
658 }
659
660 /**
661  * Function to call with a binary address
662  *
663  * @param cls closure
664  * @param peer identity of the peer
665  * @param address binary address (NULL on disconnect)
666  */
667 static void
668 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
669                  const struct GNUNET_HELLO_Address *address)
670 {
671   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
672   struct ResolutionContext *rc;
673
674   if (peer == NULL)
675   {
676     /* done */
677     address_resolution_in_progress = GNUNET_NO;
678     pic = NULL;
679     return;
680   }
681   if (address == NULL)
682   {
683     FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
684     return;
685   }
686
687   rc = GNUNET_malloc(sizeof (struct ResolutionContext));
688   rc->addrcp = GNUNET_HELLO_address_copy(address);
689   rc->printed = GNUNET_NO;
690
691   GNUNET_assert (NULL != rc);
692   address_resolutions ++;
693   /* Resolve address to string */
694   GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
695                                       RESOLUTION_TIMEOUT, &process_string,
696                                       rc);
697 }
698
699
700 static void
701 testservice_task (void *cls,
702                   const struct GNUNET_SCHEDULER_TaskContext *tc)
703 {
704   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
705   int counter = 0;
706   ret = 1;
707
708   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
709   {
710       FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
711       return;
712   }
713
714   if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
715   {
716       FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
717       return;
718   }
719
720   counter = benchmark_send + benchmark_receive + iterate_connections +
721             monitor_connections + monitor_connects + try_connect;
722
723   if (1 < counter)
724   {
725     FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
726              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
727     return;
728   }
729   if (0 == counter)
730   {
731     FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
732              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
733     return;
734   }
735
736   if (try_connect) /* -C: Connect to peer */
737   {
738     if (NULL == cpid)
739     {
740       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
741                "-C", "-p");
742       ret = 1;
743       return;
744     }
745     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
746                                        &notify_receive,
747                                        &notify_connect,
748                                        &notify_disconnect);
749     if (NULL == handle)
750     {
751         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
752         ret = 1;
753         return;
754     }
755     GNUNET_TRANSPORT_try_connect (handle, &pid);
756     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
757                                                &operation_timeout, NULL);
758
759   }
760   else if (benchmark_send) /* -s: Benchmark sending */
761   {
762     if (NULL == cpid)
763     {
764       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
765                "-s", "-p");
766       ret = 1;
767       return;
768     }
769     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
770                                        &notify_receive,
771                                        &notify_connect,
772                                        &notify_disconnect);
773     if (NULL == handle)
774     {
775         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
776         ret = 1;
777         return;
778     }
779     GNUNET_TRANSPORT_try_connect (handle, &pid);
780     start_time = GNUNET_TIME_absolute_get ();
781     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
782                                                &operation_timeout, NULL);
783   }
784   else if (benchmark_receive) /* -b: Benchmark receiving */
785   {
786     handle =
787         GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
788                                   NULL, NULL);
789     if (NULL == handle)
790     {
791         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
792         ret = 1;
793         return;
794     }
795     if (verbosity > 0)
796       FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
797     start_time = GNUNET_TIME_absolute_get ();
798
799   }
800   else if (iterate_connections) /* -i: List all active addresses once */
801   {
802     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
803     address_resolution_in_progress = GNUNET_YES;
804     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
805                                                 GNUNET_YES,
806                                                 TIMEOUT,
807                                                 &process_address, (void *) cfg);
808   }
809   else if (monitor_connections) /* -m: List all active addresses continously */
810   {
811     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
812     address_resolution_in_progress = GNUNET_YES;
813     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
814                                                 GNUNET_NO,
815                                                 TIMEOUT,
816                                                 &process_address, (void *) cfg);
817   }
818   else if (monitor_connects) /* -e : Monitor (dis)connect events continously */
819   {
820     monitor_connect_counter = 0;
821     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
822                                        &monitor_notify_connect,
823                                        &monitor_notify_disconnect);
824     if (NULL == handle)
825     {
826       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
827       ret = 1;
828       return;
829     }
830     ret = 0;
831   }
832   else
833   {
834     GNUNET_break (0);
835     return;
836   }
837
838   end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
839                                       &shutdown_task,
840                                       NULL);
841
842 }
843
844
845 /**
846  * Main function that will be run by the scheduler.
847  *
848  * @param cls closure
849  * @param args remaining command-line arguments
850  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
851  * @param cfg configuration
852  */
853 static void
854 run (void *cls, char *const *args, const char *cfgfile,
855      const struct GNUNET_CONFIGURATION_Handle *cfg)
856 {
857   if (test_configuration)
858   {
859     do_test_configuration (cfg);
860     return;
861   }
862
863   GNUNET_CLIENT_service_test ("transport", cfg,
864       GNUNET_TIME_UNIT_SECONDS,
865       &testservice_task,
866       (void *) cfg);
867 }
868
869
870 int
871 main (int argc, char *const *argv)
872 {
873   int res;
874   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
875     {'b', "benchmark", NULL,
876      gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
877      0, &GNUNET_GETOPT_set_one, &benchmark_receive},
878     {'C', "connect", NULL,
879      gettext_noop ("connect to a peer"),
880      0, &GNUNET_GETOPT_set_one, &try_connect},
881     {'i', "information", NULL,
882      gettext_noop ("provide information about all current connections (once)"),
883      0, &GNUNET_GETOPT_set_one, &iterate_connections},
884     {'m', "monitor", NULL,
885      gettext_noop ("provide information about all current connections (continuously)"),
886      0, &GNUNET_GETOPT_set_one, &monitor_connections},
887     {'e', "events", NULL,
888      gettext_noop ("provide information about all connects and disconnect events (continuously)"),
889      0, &GNUNET_GETOPT_set_one, &monitor_connects},
890     {'n', "numeric", NULL,
891      gettext_noop ("do not resolve hostnames"),
892      0, &GNUNET_GETOPT_set_one, &numeric},
893      {'p', "peer", "PEER",
894       gettext_noop ("peer identity"),
895       1, &GNUNET_GETOPT_set_string, &cpid},
896     {'s', "send", NULL,
897      gettext_noop
898      ("send data for benchmarking to the other peer (until CTRL-C)"),
899      0, &GNUNET_GETOPT_set_one, &benchmark_send},
900     {'t', "test", NULL,
901      gettext_noop ("test transport configuration (involves external server)"),
902      0, &GNUNET_GETOPT_set_one, &test_configuration},
903     GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
904     GNUNET_GETOPT_OPTION_END
905   };
906
907   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
908     return 2;
909
910   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
911                               gettext_noop
912                               ("Direct access to transport service."), options,
913                               &run, NULL);
914   GNUNET_free ((void *) argv);
915   if (GNUNET_OK == res)
916     return ret;
917   return 1;
918 }
919
920
921 /* end of gnunet-transport.c */