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