312859695cded03a414c8d01bdb6ab4244ec03d2
[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   if (NULL != peers)
224   {
225     GNUNET_CONTAINER_multihashmap_destroy (peers);
226     peers = NULL;
227   }
228 }
229
230 /**
231  * Display the result of the test.
232  *
233  * @param tc test context
234  * @param result GNUNET_YES on success
235  */
236 static void
237 display_test_result (struct TestContext *tc, int result)
238 {
239   if (GNUNET_YES != result)
240   {
241     FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
242   }
243   else
244   {
245     FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
246   }
247   if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
248   {
249     GNUNET_SCHEDULER_cancel (tc->tsk);
250     tc->tsk = GNUNET_SCHEDULER_NO_TASK;
251   }
252   if (NULL != tc->tst)
253   {
254     GNUNET_NAT_test_stop (tc->tst);
255     tc->tst = NULL;
256   }
257   GNUNET_free (tc);
258   resolver_users--;
259   if ((0 == resolver_users) && (NULL != resolver))
260   {
261     GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
262     GNUNET_OS_process_destroy (resolver);
263     resolver = NULL;
264   }
265 }
266
267
268 /**
269  * Function called by NAT on success.
270  * Clean up and update GUI (with success).
271  *
272  * @param cls test context
273  * @param success currently always GNUNET_OK
274  */
275 static void
276 result_callback (void *cls, int success)
277 {
278   struct TestContext *tc = cls;
279
280   display_test_result (tc, success);
281 }
282
283
284 /**
285  * Function called if NAT failed to confirm success.
286  * Clean up and update GUI (with failure).
287  *
288  * @param cls test context
289  * @param tc scheduler callback
290  */
291 static void
292 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
293 {
294   struct TestContext *tstc = cls;
295
296   tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
297   display_test_result (tstc, GNUNET_NO);
298 }
299
300
301 /**
302  * Test our plugin's configuration (NAT traversal, etc.).
303  *
304  * @param cfg configuration to test
305  */
306 static void
307 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
308 {
309   char *plugins;
310   char *tok;
311   unsigned long long bnd_port;
312   unsigned long long adv_port;
313   struct TestContext *tc;
314
315   if (GNUNET_OK !=
316       GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
317                                              &plugins))
318   {
319     FPRINTF (stderr,
320              "%s",
321              _
322              ("No transport plugins configured, peer will never communicate\n"));
323     ret = 4;
324     return;
325   }
326   for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
327   {
328     char section[12 + strlen (tok)];
329
330     GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
331     if (GNUNET_OK !=
332         GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
333     {
334       FPRINTF (stderr,
335                _("No port configured for plugin `%s', cannot test it\n"), tok);
336       continue;
337     }
338     if (GNUNET_OK !=
339         GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
340                                                &adv_port))
341       adv_port = bnd_port;
342     if (NULL == resolver)
343       resolver =
344           GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
345                                    "gnunet-service-resolver", NULL);
346     resolver_users++;
347     GNUNET_RESOLVER_connect (cfg);
348     tc = GNUNET_malloc (sizeof (struct TestContext));
349     tc->name = GNUNET_strdup (tok);
350     tc->tst =
351         GNUNET_NAT_test_start (cfg,
352                                (0 ==
353                                 strcasecmp (tok,
354                                             "udp")) ? GNUNET_NO : GNUNET_YES,
355                                (uint16_t) bnd_port, (uint16_t) adv_port,
356                                &result_callback, tc);
357     if (NULL == tc->tst)
358     {
359       display_test_result (tc, GNUNET_SYSERR);
360       continue;
361     }
362     tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
363   }
364   GNUNET_free (plugins);
365 }
366
367
368 /**
369  * Shutdown, print statistics.
370  */
371 static void
372 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
373 {
374   struct GNUNET_TIME_Relative duration;
375
376   if (NULL != th)
377   {
378     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
379     th = NULL;
380   }
381   GNUNET_TRANSPORT_disconnect (handle);
382   if (benchmark_receive)
383   {
384     duration = GNUNET_TIME_absolute_get_duration (start_time);
385     FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
386              1000 * traffic_received / (1 + duration.rel_value),
387              traffic_received,
388              GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
389   }
390   if (benchmark_send)
391   {
392     duration = GNUNET_TIME_absolute_get_duration (start_time);
393     FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
394              1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
395              GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
396   }
397 }
398
399
400 /**
401  * Function called to notify a client about the socket
402  * begin ready to queue more data.  "buf" will be
403  * NULL and "size" zero if the socket was closed for
404  * writing in the meantime.
405  *
406  * @param cls closure
407  * @param size number of bytes available in buf
408  * @param buf where the callee should write the message
409  * @return number of bytes written to buf
410  */
411 static size_t
412 transmit_data (void *cls, size_t size, void *buf)
413 {
414   struct GNUNET_MessageHeader *m = buf;
415
416   if ((NULL == buf) && (0 == size))
417   {
418     th = NULL;
419     return 0;
420   }
421
422   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
423   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
424   m->size = ntohs (size);
425   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
426   memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
427   traffic_sent += size;
428   th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, 32 * 1024, 0,
429                                                GNUNET_TIME_UNIT_FOREVER_REL,
430                                                &transmit_data, NULL);
431   if (verbosity > 0)
432     FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
433              GNUNET_i2s (&pid));
434   return size;
435 }
436
437
438 /**
439  * Function called to notify transport users that another
440  * peer connected to us.
441  *
442  * @param cls closure
443  * @param peer the peer that connected
444  * @param ats performance data
445  * @param ats_count number of entries in ats (excluding 0-termination)
446  */
447 static void
448 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
449                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
450 {
451   if (verbosity > 0)
452     FPRINTF (stdout, _("Connected to %s\n"), GNUNET_i2s (peer));
453   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
454     return;
455   ret = 0;
456   if (try_connect)
457   {
458       /* all done, terminate instantly */
459       if (verbosity > 0)
460         FPRINTF (stdout, _("Successfully connected to %s\n"), GNUNET_i2s (peer));
461       GNUNET_SCHEDULER_cancel (end);
462       end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
463   }
464   else if (benchmark_send)
465   {
466     start_time = GNUNET_TIME_absolute_get ();
467     if (NULL == th)
468       th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer, 32 * 1024, 0,
469                                                  GNUNET_TIME_UNIT_FOREVER_REL,
470                                                  &transmit_data, NULL);
471   }
472   else
473   {
474     /* all done, terminate instantly */
475     GNUNET_SCHEDULER_cancel (end);
476     end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
477   }
478 }
479
480
481 /**
482  * Function called to notify transport users that another
483  * peer disconnected from us.
484  *
485  * @param cls closure
486  * @param peer the peer that disconnected
487  */
488 static void
489 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
490 {
491   if (verbosity > 0)
492     FPRINTF (stdout, _("Disconnected from %s\n"), GNUNET_i2s (peer));
493   if (NULL != th)
494   {
495     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
496     th = NULL;
497   }
498
499 }
500
501 /**
502  * Function called to notify transport users that another
503  * peer connected to us.
504  *
505  * @param cls closure
506  * @param peer the peer that connected
507  * @param ats performance data
508  * @param ats_count number of entries in ats (excluding 0-termination)
509  */
510 static void
511 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
512                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
513 {
514   monitor_connect_counter ++;
515   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
516   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
517
518   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
519            now_str,
520            _("Connected to"),
521            GNUNET_i2s (peer),
522            monitor_connect_counter);
523 }
524
525
526 /**
527  * Function called to notify transport users that another
528  * peer disconnected from us.
529  *
530  * @param cls closure
531  * @param peer the peer that disconnected
532  */
533 static void
534 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
535 {
536   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
537   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
538
539   GNUNET_assert (monitor_connect_counter > 0);
540   monitor_connect_counter --;
541
542   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
543            now_str,
544            _("Disconnected from"),
545            GNUNET_i2s (peer),
546            monitor_connect_counter);
547 }
548
549
550
551 /**
552  * Function called by the transport for each received message.
553  *
554  * @param cls closure
555  * @param peer (claimed) identity of the other peer
556  * @param message the message
557  * @param ats performance data
558  * @param ats_count number of entries in ats
559  */
560 static void
561 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
562                 const struct GNUNET_MessageHeader *message,
563                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
564 {
565   if (!benchmark_receive)
566     return;
567   if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
568     return;
569   if (verbosity > 0)
570     FPRINTF (stdout, _("Received %u bytes from %s\n"),
571              (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
572   if (traffic_received == 0)
573     start_time = GNUNET_TIME_absolute_get ();
574   traffic_received += ntohs (message->size);
575 }
576
577 struct ResolutionContext
578 {
579   struct GNUNET_HELLO_Address *addrcp;
580
581   int printed;
582 };
583
584
585 static void
586 process_string (void *cls, const char *address)
587 {
588   struct ResolutionContext *rc = cls;
589   struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
590
591   if (address != NULL)
592   {
593     FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
594     rc->printed = GNUNET_YES;
595   }
596   else
597   {
598     /* done */
599     GNUNET_assert (address_resolutions > 0);
600     address_resolutions --;
601     if (GNUNET_NO == rc->printed)
602       FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
603     GNUNET_free (rc->addrcp);
604     GNUNET_free (rc);
605     if (0 == address_resolutions)
606     {
607         if (GNUNET_SCHEDULER_NO_TASK != end)
608           GNUNET_SCHEDULER_cancel (end);
609         end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
610     }
611   }
612 }
613
614 /**
615  * Function to call with a binary address
616  *
617  * @param cls closure
618  * @param peer identity of the peer
619  * @param address binary address (NULL on disconnect)
620  */
621 static void
622 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
623                  const struct GNUNET_HELLO_Address *address)
624 {
625   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
626   struct ResolutionContext *rc;
627
628   if (peer == NULL)
629   {
630     /* done */
631     address_resolution_in_progress = GNUNET_NO;
632     pic = NULL;
633     return;
634   }
635   if (address == NULL)
636   {
637     FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
638     return;
639   }
640
641   rc = GNUNET_malloc(sizeof (struct ResolutionContext));
642   rc->addrcp = GNUNET_HELLO_address_copy(address);
643   rc->printed = GNUNET_NO;
644
645   GNUNET_assert (NULL != rc);
646   address_resolutions ++;
647   /* Resolve address to string */
648   GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
649                                       RESOLUTION_TIMEOUT, &process_string,
650                                       rc);
651 }
652
653
654 static void
655 testservice_task (void *cls,
656                   const struct GNUNET_SCHEDULER_TaskContext *tc)
657 {
658   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
659   int counter = 0;
660   int try_connect = 0;
661
662
663   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
664   {
665       FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
666       return;
667   }
668
669   if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
670   {
671       FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
672       return;
673   }
674
675   counter = benchmark_send + benchmark_receive + iterate_connections +
676             monitor_connections + monitor_connects + try_connect;
677
678   if (1 < counter)
679   {
680     FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
681              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
682     return;
683   }
684   if (0 == counter)
685   {
686     FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
687              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
688     return;
689   }
690
691   end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
692                                       &shutdown_task,
693                                       NULL);
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     /*
736     end = GNUNET_SCHEDULER_add_delayed (benchmark_send ?
737                                         GNUNET_TIME_UNIT_FOREVER_REL :
738                                         GNUNET_TIME_UNIT_SECONDS,
739                                         &do_disconnect,
740                                         NULL);*/
741   }
742   else if (benchmark_receive) /* Benchmark receiving */
743   {
744     handle =
745         GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
746                                   &notify_connect, &notify_disconnect);
747     if (NULL == handle)
748     {
749         FPRINTF (stderr, _("Failed to connect to transport service\n"));
750         return;
751     }
752     GNUNET_TRANSPORT_try_connect (handle, &pid);
753     start_time = GNUNET_TIME_absolute_get ();
754     /*
755     end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
756                                       &do_disconnect, NULL);*/
757   }
758   else if (iterate_connections) /* -i: List all active addresses once */
759   {
760     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
761     address_resolution_in_progress = GNUNET_YES;
762     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
763                                                 GNUNET_YES,
764                                                 TIMEOUT,
765                                                 &process_address, (void *) cfg);
766   }
767   else if (monitor_connections) /* -m: List all active addresses continously */
768   {
769     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
770     address_resolution_in_progress = GNUNET_YES;
771     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
772                                                 GNUNET_NO,
773                                                 TIMEOUT,
774                                                 &process_address, (void *) cfg);
775   }
776   else if (monitor_connects) /* -e : Monitor (dis)connect events continously */
777   {
778     monitor_connect_counter = 0;
779     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
780                                        &monitor_notify_connect,
781                                        &monitor_notify_disconnect);
782     if (NULL == handle)
783     {
784       FPRINTF (stderr, _("Failed to connect to transport service\n"));
785       return;
786     }
787   }
788   else
789   {
790     GNUNET_break (0);
791     return;
792   }
793 }
794
795
796 /**
797  * Main function that will be run by the scheduler.
798  *
799  * @param cls closure
800  * @param args remaining command-line arguments
801  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
802  * @param cfg configuration
803  */
804 static void
805 run (void *cls, char *const *args, const char *cfgfile,
806      const struct GNUNET_CONFIGURATION_Handle *cfg)
807 {
808   if (test_configuration)
809   {
810     do_test_configuration (cfg);
811   }
812
813   GNUNET_CLIENT_service_test ("transport", cfg,
814       GNUNET_TIME_UNIT_SECONDS,
815       &testservice_task,
816       (void *) cfg);
817 }
818
819
820 int
821 main (int argc, char *const *argv)
822 {
823   int res;
824   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
825     {'b', "benchmark", NULL,
826      gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
827      0, &GNUNET_GETOPT_set_one, &benchmark_receive},
828     {'C', "connect", NULL,
829      gettext_noop ("connect to a peer"),
830      0, &GNUNET_GETOPT_set_one, &try_connect},
831     {'i', "information", NULL,
832      gettext_noop ("provide information about all current connections (once)"),
833      0, &GNUNET_GETOPT_set_one, &iterate_connections},
834     {'m', "monitor", NULL,
835      gettext_noop ("provide information about all current connections (continuously)"),
836      0, &GNUNET_GETOPT_set_one, &monitor_connections},
837     {'e', "events", NULL,
838      gettext_noop ("provide information about all connects and disconnect events (continuously)"),
839      0, &GNUNET_GETOPT_set_one, &monitor_connects},
840     {'n', "numeric", NULL,
841      gettext_noop ("do not resolve hostnames"),
842      0, &GNUNET_GETOPT_set_one, &numeric},
843      {'p', "peer", "PEER",
844       gettext_noop ("peer identity"),
845       1, &GNUNET_GETOPT_set_string, &cpid},
846     {'s', "send", NULL,
847      gettext_noop
848      ("send data for benchmarking to the other peer (until CTRL-C)"),
849      0, &GNUNET_GETOPT_set_one, &benchmark_send},
850     {'t', "test", NULL,
851      gettext_noop ("test transport configuration (involves external server)"),
852      0, &GNUNET_GETOPT_set_one, &test_configuration},
853     GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
854     GNUNET_GETOPT_OPTION_END
855   };
856
857   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
858     return 2;
859
860   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
861                               gettext_noop
862                               ("Direct access to transport service."), options,
863                               &run, NULL);
864   GNUNET_free ((void *) argv);
865   if (GNUNET_OK == res)
866     return ret;
867   return 1;
868 }
869
870
871 /* end of gnunet-transport.c */