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