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