edc253dae409b2646b072454bf4c2321f35f7f2d
[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, 20)
43 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
44 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45
46 /**
47  * Benchmarking block size in KB
48  */
49 #define BLOCKSIZE 4
50
51 /**
52  * Which peer should we connect to?
53  */
54 static char *cpid;
55
56 /**
57  * Handle to transport service.
58  */
59 static struct GNUNET_TRANSPORT_Handle *handle;
60
61 /**
62  * Configuration handle
63  */
64 static struct GNUNET_CONFIGURATION_Handle *cfg;
65
66 /**
67  * Try_connect handle
68  */
69 struct GNUNET_TRANSPORT_TryConnectHandle * tc_handle;
70
71 /**
72  * Option -s.
73  */
74 static int benchmark_send;
75
76 /**
77  * Option -b.
78  */
79 static int benchmark_receive;
80
81 /**
82  * Option -l.
83  */
84 static int benchmark_receive;
85
86 /**
87  * Option -i.
88  */
89 static int iterate_connections;
90
91 /**
92  * Option -d.
93  */
94 static int iterate_validation;
95
96 /**
97  * Option -a.
98  */
99 static int iterate_all;
100
101 /**
102  * Option -t.
103  */
104 static int test_configuration;
105
106 /**
107  * Option -c.
108  */
109 static int monitor_connects;
110
111 /**
112  * Option -m.
113  */
114 static int monitor_connections;
115
116 /**
117  * Option -f.
118  */
119 static int monitor_validation;
120
121 /**
122  * Option -C.
123  */
124 static int try_connect;
125
126 /**
127  * Option -D.
128  */
129 static int try_disconnect;
130
131 /**
132  * Option -n.
133  */
134 static int numeric;
135
136 /**
137  * Global return value (0 success).
138  */
139 static int ret;
140
141 /**
142  * Current number of connections in monitor mode
143  */
144 static int monitor_connect_counter;
145
146 /**
147  * Number of bytes of traffic we received so far.
148  */
149 static unsigned long long traffic_received;
150
151 /**
152  * Number of bytes of traffic we sent so far.
153  */
154 static unsigned long long traffic_sent;
155
156 /**
157  * Starting time of transmitting/receiving data.
158  */
159 static struct GNUNET_TIME_Absolute start_time;
160
161 /**
162  * Handle for current transmission request.
163  */
164 static struct GNUNET_TRANSPORT_TransmitHandle *th;
165
166 /**
167  * Map storing information about monitored peers
168  */
169 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
170
171 /**
172  *
173  */
174 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
175
176 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
177
178 /**
179  * Identity of the peer we transmit to / connect to.
180  * (equivalent to 'cpid' string).
181  */
182 static struct GNUNET_PeerIdentity pid;
183
184 /**
185  * Task scheduled for cleanup / termination of the process.
186  */
187 static GNUNET_SCHEDULER_TaskIdentifier end;
188
189 /**
190  * Task for operation timeout
191  */
192 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
193
194 /**
195  * Selected level of verbosity.
196  */
197 static int verbosity;
198
199 /**
200  * Resolver process handle.
201  */
202 struct GNUNET_OS_Process *resolver;
203
204 /**
205  * Number of address resolutions pending
206  */
207 static unsigned int address_resolutions;
208
209 /**
210  * Address resolutions pending in progress
211  */
212 static unsigned int address_resolution_in_progress;
213
214 /**
215  * DLL for NAT Test Contexts: head
216  */
217 struct TestContext *head;
218
219 /**
220  * DLL for NAT Test Contexts: tail
221  */
222 struct TestContext *tail;
223
224 /**
225  * Context for a plugin test.
226  */
227 struct TestContext
228 {
229   /**
230    * Previous in DLL
231    */
232   struct TestContext *prev;
233
234   /**
235    * Next in DLL
236    */
237   struct TestContext *next;
238
239   /**
240    * Handle to the active NAT test.
241    */
242   struct GNUNET_NAT_Test *tst;
243
244   /**
245    * Task identifier for the timeout.
246    */
247   GNUNET_SCHEDULER_TaskIdentifier tsk;
248
249   /**
250    * Name of plugin under test.
251    */
252   char *name;
253
254   /**
255    * Bound port
256    */
257   unsigned long long bnd_port;
258
259   /**
260    * Advertised ports
261    */
262   unsigned long long adv_port;
263
264 };
265
266 enum TestResult
267 {
268   /* NAT returned success */
269   NAT_TEST_SUCCESS = GNUNET_OK,
270
271   /* NAT returned failure  */
272   NAT_TEST_FAIL = GNUNET_NO,
273
274   /* NAT returned failure while running test */
275   NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
276
277   /* We could not start the test */
278   NAT_TEST_FAILED_TO_START = 2,
279
280   /* We had a timeout while running the test */
281   NAT_TEST_TIMEOUT = 3,
282 };
283
284 static struct ValidationResolutionContext *vc_head;
285 static struct ValidationResolutionContext *vc_tail;
286
287 struct ValidationResolutionContext
288 {
289   struct ValidationResolutionContext *next;
290   struct ValidationResolutionContext *prev;
291
292   struct GNUNET_PeerIdentity id;
293   struct GNUNET_HELLO_Address *addrcp;
294   struct GNUNET_TIME_Absolute last_validation;
295   struct GNUNET_TIME_Absolute valid_until;
296   struct GNUNET_TIME_Absolute next_validation;
297   enum GNUNET_TRANSPORT_ValidationState state;
298
299   struct GNUNET_TRANSPORT_AddressToStringContext *asc;
300
301   char *transport;
302   int printed;
303 };
304
305 struct MonitoredPeer
306 {
307   enum GNUNET_TRANSPORT_PeerState state;
308   struct GNUNET_TIME_Absolute state_timeout;
309   struct GNUNET_HELLO_Address *address;
310 };
311
312
313 static int
314 destroy_it (void *cls,
315             const struct GNUNET_PeerIdentity *key,
316             void *value)
317 {
318   struct MonitoredPeer *m = value;
319
320   GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
321                                                                     key, value));
322   GNUNET_free_non_null (m->address);
323   GNUNET_free (value);
324   return GNUNET_OK;
325 }
326
327
328 /**
329  * Task run in monitor mode when the user presses CTRL-C to abort.
330  * Stops monitoring activity.
331  *
332  * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
333  * @param tc scheduler context
334  */
335 static void
336 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 {
338   struct GNUNET_TIME_Relative duration;
339   struct ValidationResolutionContext *cur;
340   struct ValidationResolutionContext *next;
341   end = GNUNET_SCHEDULER_NO_TASK;
342   if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
343   {
344     GNUNET_SCHEDULER_cancel (op_timeout);
345     op_timeout = GNUNET_SCHEDULER_NO_TASK;
346   }
347   if (NULL != tc_handle)
348   {
349     GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
350     tc_handle = NULL;
351   }
352   if (NULL != pic)
353   {
354     GNUNET_TRANSPORT_monitor_peers_cancel (pic);
355     pic = NULL;
356   }
357   if (NULL != vic)
358   {
359     GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
360     vic = NULL;
361   }
362
363   next = vc_head;
364   for (cur = next; NULL != cur; cur = next)
365   {
366     next = cur->next;
367
368     GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
369     GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, cur);
370     GNUNET_free (cur->transport);
371     GNUNET_HELLO_address_free (cur->addrcp);
372     GNUNET_free (cur);
373   }
374
375   if (NULL != th)
376   {
377     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
378     th = NULL;
379   }
380   if (NULL != handle)
381   {
382     GNUNET_TRANSPORT_disconnect (handle);
383     handle = NULL;
384   }
385   if (benchmark_send)
386   {
387     duration = GNUNET_TIME_absolute_get_duration (start_time);
388     FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
389         1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
390         traffic_sent,
391         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
392   }
393   if (benchmark_receive)
394   {
395     duration = GNUNET_TIME_absolute_get_duration (start_time);
396     FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
397         1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
398         traffic_received,
399         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
400   }
401
402   if (NULL != monitored_peers)
403   {
404     GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
405     GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
406     monitored_peers = NULL;
407   }
408 }
409
410 static struct PeerResolutionContext *rc_head;
411 static struct PeerResolutionContext *rc_tail;
412
413 struct PeerResolutionContext
414 {
415   struct PeerResolutionContext *next;
416   struct PeerResolutionContext *prev;
417   struct GNUNET_PeerIdentity id;
418   struct GNUNET_HELLO_Address *addrcp;
419   struct GNUNET_TRANSPORT_AddressToStringContext *asc;
420   enum GNUNET_TRANSPORT_PeerState state;
421   struct GNUNET_TIME_Absolute state_timeout;
422   char *transport;
423   int printed;
424 };
425
426
427
428 static void
429 operation_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
430 {
431   struct PeerResolutionContext *cur;
432   struct PeerResolutionContext *next;
433   op_timeout = GNUNET_SCHEDULER_NO_TASK;
434   if ((try_connect) || (benchmark_send) || (benchmark_receive))
435   {
436     FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_i2s_full (&pid));
437     if (GNUNET_SCHEDULER_NO_TASK != end)
438       GNUNET_SCHEDULER_cancel (end);
439     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
440     ret = 1;
441     return;
442   }
443   if (iterate_connections)
444   {
445     next = rc_head;
446     while (NULL != (cur = next))
447     {
448       next = cur->next;
449       FPRINTF (stdout, _("Failed to resolve address for peer `%s'\n"),
450           GNUNET_i2s (&cur->addrcp->peer));
451
452       GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
453       GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
454       GNUNET_free(cur->transport);
455       GNUNET_free(cur->addrcp);
456       GNUNET_free(cur);
457
458     }
459     FPRINTF (stdout, "%s", _("Failed to list connections, timeout occured\n") );
460     if (GNUNET_SCHEDULER_NO_TASK != end)
461       GNUNET_SCHEDULER_cancel (end);
462     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
463     ret = 1;
464     return;
465   }
466
467 }
468
469 static void
470 run_nat_test ();
471
472 /**
473  * Display the result of the test.
474  *
475  * @param tc test context
476  * @param result #GNUNET_YES on success
477  */
478 static void
479 display_test_result (struct TestContext *tc, enum TestResult result)
480 {
481   switch (result) {
482     case NAT_TEST_FAIL:
483       FPRINTF (stderr, _("Configuration for plugin `%s' did not work!\n"),
484           tc->name);
485       break;
486     case NAT_TEST_SUCCESS:
487       FPRINTF (stderr, _("Configuration for plugin `%s' did work!\n"),
488           tc->name);
489       break;
490     case NAT_TEST_INTERNAL_FAIL:
491       FPRINTF (stderr, _("Internal NAT error while running test for plugin `%s'\n"),
492           tc->name);
493       break;
494     case NAT_TEST_FAILED_TO_START:
495       FPRINTF (stderr, _("Failed to start NAT test for plugin `%s'\n"),
496           tc->name);
497       break;
498     case NAT_TEST_TIMEOUT:
499       FPRINTF (stderr, _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
500           tc->name);
501       break;
502     default:
503       break;
504   }
505
506   if (GNUNET_YES != result)
507   {
508     FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
509   }
510   else
511   {
512     FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
513   }
514   if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
515   {
516     GNUNET_SCHEDULER_cancel (tc->tsk);
517     tc->tsk = GNUNET_SCHEDULER_NO_TASK;
518   }
519   if (NULL != tc->tst)
520   {
521     GNUNET_NAT_test_stop (tc->tst);
522     tc->tst = NULL;
523   }
524
525   GNUNET_CONTAINER_DLL_remove (head, tail, tc);
526   GNUNET_free (tc->name);
527   GNUNET_free (tc);
528
529   if ((NULL == head) && (NULL != resolver))
530   {
531     GNUNET_break(0 == GNUNET_OS_process_kill (resolver, GNUNET_TERM_SIG));
532     GNUNET_OS_process_destroy (resolver);
533     resolver = NULL;
534   }
535   if (NULL != head)
536     run_nat_test ();
537 }
538
539 /**
540  * Function called by NAT to report the outcome of the nat-test.
541  * Clean up and update GUI.
542  *
543  * @param cls test context
544  * @param success currently always #GNUNET_OK
545  * @param emsg error message, NULL on success
546  */
547 static void
548 result_callback (void *cls, enum GNUNET_NAT_FailureCode result)
549 {
550   struct TestContext *tc = cls;
551   display_test_result (tc, result);
552 }
553
554 static void
555 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
556                             const struct GNUNET_HELLO_Address *address,
557                             int numeric,
558                             struct GNUNET_TIME_Absolute last_validation,
559                             struct GNUNET_TIME_Absolute valid_until,
560                             struct GNUNET_TIME_Absolute next_validation,
561                             enum GNUNET_TRANSPORT_ValidationState state);
562
563
564 static void
565 process_validation_string (void *cls, const char *address, int res)
566 {
567   struct ValidationResolutionContext *vc = cls;
568   char *s_valid;
569   char *s_last;
570   char *s_next;
571
572   if (address != NULL )
573   {
574     if (GNUNET_SYSERR == res)
575     {
576       FPRINTF (stderr, "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
577           GNUNET_i2s (&vc->id),
578           vc->addrcp->transport_name,
579           vc->addrcp->address_length);
580     }
581     if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
582       s_valid = GNUNET_strdup("never");
583     else
584       s_valid = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
585
586     if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
587       s_last = GNUNET_strdup("never");
588     else
589       s_last = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
590
591     if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
592       s_next = GNUNET_strdup("never");
593     else
594       s_next = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
595
596     FPRINTF (stdout,
597         _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
598         GNUNET_i2s (&vc->id),
599         (GNUNET_OK == res) ? address : "<invalid address>",
600         (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
601         "Valid until    : ", s_valid,
602         "Last validation: ",s_last,
603         "Next validation: ", s_next);
604     GNUNET_free (s_valid);
605     GNUNET_free (s_last);
606     GNUNET_free (s_next);
607     vc->printed = GNUNET_YES;
608   }
609   else
610   {
611     /* done */
612
613     GNUNET_assert(address_resolutions > 0);
614     address_resolutions--;
615     if ((GNUNET_SYSERR == res) && (GNUNET_NO == vc->printed))
616     {
617       if (numeric == GNUNET_NO)
618       {
619         /* Failed to resolve address, try numeric lookup */
620         resolve_validation_address (&vc->id, vc->addrcp, GNUNET_NO,
621            vc->last_validation, vc->valid_until, vc->next_validation,
622            vc->state);
623       }
624       else
625       {
626         FPRINTF (stdout, _("Peer `%s' %s `%s' \n"),
627             GNUNET_i2s (&vc->id), "<unable to resolve address>",
628             GNUNET_TRANSPORT_vs2s (vc->state));
629       }
630     }
631     GNUNET_free (vc->transport);
632     GNUNET_free (vc->addrcp);
633     GNUNET_CONTAINER_DLL_remove(vc_head, vc_tail, vc);
634     GNUNET_free(vc);
635     if ((0 == address_resolutions) && (iterate_validation))
636     {
637       if (GNUNET_SCHEDULER_NO_TASK != end)
638       {
639         GNUNET_SCHEDULER_cancel (end);
640         end = GNUNET_SCHEDULER_NO_TASK;
641       }
642       if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
643       {
644         GNUNET_SCHEDULER_cancel (op_timeout);
645         op_timeout = GNUNET_SCHEDULER_NO_TASK;
646       }
647       ret = 0;
648       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
649     }
650   }
651 }
652
653
654
655 static void
656 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
657     const struct GNUNET_HELLO_Address *address, int numeric,
658     struct GNUNET_TIME_Absolute last_validation,
659     struct GNUNET_TIME_Absolute valid_until,
660     struct GNUNET_TIME_Absolute next_validation,
661     enum GNUNET_TRANSPORT_ValidationState state)
662 {
663   struct ValidationResolutionContext *vc;
664
665   vc = GNUNET_new (struct ValidationResolutionContext);
666   GNUNET_assert(NULL != vc);
667   GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
668   address_resolutions++;
669
670   vc->id = (*id);
671   vc->transport = GNUNET_strdup(address->transport_name);
672   vc->addrcp = GNUNET_HELLO_address_copy (address);
673   vc->printed = GNUNET_NO;
674   vc->state = state;
675   vc->last_validation = last_validation;
676   vc->valid_until = valid_until;
677   vc->next_validation = next_validation;
678
679   /* Resolve address to string */
680   vc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
681       RESOLUTION_TIMEOUT, &process_validation_string, vc);
682 }
683
684
685 static void
686 process_validation_cb (void *cls,
687                        const struct GNUNET_PeerIdentity *peer,
688                        const struct GNUNET_HELLO_Address *address,
689                        struct GNUNET_TIME_Absolute last_validation,
690                        struct GNUNET_TIME_Absolute valid_until,
691                        struct GNUNET_TIME_Absolute next_validation,
692                        enum GNUNET_TRANSPORT_ValidationState state)
693 {
694   if ((NULL == peer) && (NULL == address))
695   {
696     if (monitor_validation)
697     {
698       FPRINTF (stdout,
699                "%s",
700                _("Monitor disconnected from transport service. Reconnecting.\n"));
701       return;
702     }
703
704     /* done */
705     vic = NULL;
706     if (GNUNET_SCHEDULER_NO_TASK != end)
707       GNUNET_SCHEDULER_cancel (end);
708     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
709     return;
710   }
711   if ((NULL == peer) || (NULL == address))
712   {
713     /* invalid response */
714     vic = NULL;
715     if (GNUNET_SCHEDULER_NO_TASK != end)
716       GNUNET_SCHEDULER_cancel (end);
717     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
718     return;
719   }
720   resolve_validation_address (peer, address,
721      numeric, last_validation,
722      valid_until, next_validation, state);
723 }
724
725
726 static void
727 run_nat_test ()
728 {
729   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
730       "Running test for plugin `%s' using bind port %u and advertised port %u \n",
731       head->name, (uint16_t) head->bnd_port, (uint16_t) head->adv_port);
732
733   head->tst = GNUNET_NAT_test_start (cfg,
734       (0 == strcasecmp (head->name, "udp")) ? GNUNET_NO : GNUNET_YES,
735       (uint16_t) head->bnd_port,
736       (uint16_t) head->adv_port,
737       TIMEOUT,
738       &result_callback, head);
739 }
740
741
742 /**
743  * Test our plugin's configuration (NAT traversal, etc.).
744  *
745  * @param cfg configuration to test
746  */
747 static void
748 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
749 {
750   char *plugins;
751   char *tok;
752   unsigned long long bnd_port;
753   unsigned long long adv_port;
754   struct TestContext *tc;
755   char *binary;
756
757   if (GNUNET_OK
758       != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
759           &plugins))
760   {
761     FPRINTF (stderr, "%s", _
762     ("No transport plugins configured, peer will never communicate\n") );
763     ret = 4;
764     return;
765   }
766
767   for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
768   {
769     char section[12 + strlen (tok)];
770     GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
771     if (GNUNET_OK
772         != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
773             &bnd_port))
774     {
775       FPRINTF (stderr,
776           _("No port configured for plugin `%s', cannot test it\n"), tok);
777       continue;
778     }
779     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
780             "ADVERTISED_PORT", &adv_port))
781       adv_port = bnd_port;
782
783     tc = GNUNET_new (struct TestContext);
784     tc->name = GNUNET_strdup (tok);
785     tc->adv_port = adv_port;
786     tc->bnd_port = bnd_port;
787     GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
788   }
789   GNUNET_free(plugins);
790
791   if ((NULL != head) && (NULL == resolver))
792   {
793     binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
794     resolver = GNUNET_OS_start_process (GNUNET_YES,
795                                         GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
796                                         NULL, NULL, NULL,
797                                         binary,
798                                         "gnunet-service-resolver", NULL );
799     if (NULL == resolver)
800     {
801       FPRINTF (stderr, _("Failed to start resolver!\n"));
802       return;
803     }
804
805     GNUNET_free(binary);
806     GNUNET_RESOLVER_connect (cfg);
807     run_nat_test ();
808   }
809 }
810
811 /**
812  * Function called to notify a client about the socket
813  * begin ready to queue more data.  @a buf will be
814  * NULL and @a size zero if the socket was closed for
815  * writing in the meantime.
816  *
817  * @param cls closure
818  * @param size number of bytes available in @a buf
819  * @param buf where the callee should write the message
820  * @return number of bytes written to @a buf
821  */
822 static size_t
823 transmit_data (void *cls, size_t size, void *buf)
824 {
825   struct GNUNET_MessageHeader *m = buf;
826
827   if ((NULL == buf) || (0 == size))
828   {
829     th = NULL;
830     return 0;
831   }
832
833   GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
834   GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
835   m->size = ntohs (size);
836   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
837   memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
838   traffic_sent += size;
839   th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
840                                                BLOCKSIZE * 1024,
841                                                GNUNET_TIME_UNIT_FOREVER_REL,
842                                                &transmit_data, NULL );
843   if (verbosity > 0)
844     FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
845         GNUNET_i2s (&pid));
846   return size;
847 }
848
849
850 /**
851  * Function called to notify transport users that another
852  * peer connected to us.
853  *
854  * @param cls closure
855  * @param peer the peer that connected
856  */
857 static void
858 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
859 {
860   if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
861     return;
862   ret = 0;
863   if (try_connect)
864   {
865     /* all done, terminate instantly */
866     FPRINTF (stdout, _("Successfully connected to `%s'\n"),
867         GNUNET_i2s_full (peer));
868     ret = 0;
869
870     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
871     {
872       GNUNET_SCHEDULER_cancel (op_timeout);
873       op_timeout = GNUNET_SCHEDULER_NO_TASK;
874     }
875
876     if (GNUNET_SCHEDULER_NO_TASK != end)
877       GNUNET_SCHEDULER_cancel (end);
878     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
879     return;
880   }
881   if (benchmark_send)
882   {
883     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
884     {
885       GNUNET_SCHEDULER_cancel (op_timeout);
886       op_timeout = GNUNET_SCHEDULER_NO_TASK;
887     }
888     if (verbosity > 0)
889       FPRINTF (stdout,
890           _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
891           GNUNET_i2s (&pid), BLOCKSIZE);
892     start_time = GNUNET_TIME_absolute_get ();
893     if (NULL == th)
894       th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
895                                                    BLOCKSIZE * 1024,
896                                                    GNUNET_TIME_UNIT_FOREVER_REL,
897                                                    &transmit_data,
898                                                    NULL);
899     else
900       GNUNET_break(0);
901     return;
902   }
903 }
904
905 /**
906  * Function called to notify transport users that another
907  * peer disconnected from us.
908  *
909  * @param cls closure
910  * @param peer the peer that disconnected
911  */
912 static void
913 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
914 {
915   if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
916     return;
917
918   if (try_disconnect)
919   {
920     /* all done, terminate instantly */
921     FPRINTF (stdout, _("Successfully disconnected from `%s'\n"),
922         GNUNET_i2s_full (peer));
923     ret = 0;
924
925     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
926     {
927       GNUNET_SCHEDULER_cancel (op_timeout);
928       op_timeout = GNUNET_SCHEDULER_NO_TASK;
929     }
930
931     if (GNUNET_SCHEDULER_NO_TASK != end)
932       GNUNET_SCHEDULER_cancel (end);
933     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
934     return;
935   }
936
937   if (NULL != th)
938   {
939     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
940     th = NULL;
941   }
942   if (benchmark_send)
943   {
944     FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
945         GNUNET_i2s (&pid));
946     if (GNUNET_SCHEDULER_NO_TASK != end)
947       GNUNET_SCHEDULER_cancel (end);
948     return;
949   }
950 }
951
952 /**
953  * Function called to notify transport users that another
954  * peer connected to us.
955  *
956  * @param cls closure
957  * @param peer the peer that connected
958  */
959 static void
960 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
961 {
962   monitor_connect_counter++;
963   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
964   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
965
966   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"), now_str,
967       _("Connected to"), GNUNET_i2s (peer), monitor_connect_counter);
968 }
969
970
971 /**
972  * Function called to notify transport users that another
973  * peer disconnected from us.
974  *
975  * @param cls closure
976  * @param peer the peer that disconnected
977  */
978 static void
979 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
980 {
981   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
982   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
983
984   GNUNET_assert(monitor_connect_counter > 0);
985   monitor_connect_counter--;
986
987   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"), now_str,
988       _("Disconnected from"), GNUNET_i2s (peer), monitor_connect_counter);
989 }
990
991
992 /**
993  * Function called by the transport for each received message.
994  *
995  * @param cls closure
996  * @param peer (claimed) identity of the other peer
997  * @param message the message
998  */
999 static void
1000 notify_receive (void *cls,
1001                 const struct GNUNET_PeerIdentity *peer,
1002                 const struct GNUNET_MessageHeader *message)
1003 {
1004   if (benchmark_receive)
1005   {
1006     if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1007       return;
1008     if (verbosity > 0)
1009       FPRINTF (stdout, _("Received %u bytes from %s\n"),
1010           (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
1011
1012     if (traffic_received == 0)
1013       start_time = GNUNET_TIME_absolute_get ();
1014     traffic_received += ntohs (message->size);
1015     return;
1016   }
1017 }
1018
1019
1020 static void
1021 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1022                       const struct GNUNET_HELLO_Address *address,
1023                       int numeric,
1024                       enum GNUNET_TRANSPORT_PeerState state,
1025                       struct GNUNET_TIME_Absolute state_timeout);
1026
1027
1028 static void
1029 print_info (const struct GNUNET_PeerIdentity *id,
1030             const char *transport,
1031             const char *addr,
1032             enum GNUNET_TRANSPORT_PeerState state,
1033             struct GNUNET_TIME_Absolute state_timeout)
1034 {
1035
1036   if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1037        (GNUNET_YES == monitor_connections) )
1038   {
1039     FPRINTF (stdout, _("Peer `%s': %s %s in state `%s' until %s\n"),
1040         GNUNET_i2s (id),
1041         (NULL == transport) ? "<none>" : transport,
1042         (NULL == transport) ? "<none>" : addr,
1043         GNUNET_TRANSPORT_ps2s (state),
1044         GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1045   }
1046   else if ( (GNUNET_YES == iterate_connections) &&
1047              (GNUNET_TRANSPORT_is_connected(state)) )
1048   {
1049     /* Only connected peers, skip state */
1050     FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (id), transport, addr);
1051   }
1052 }
1053
1054
1055 static void
1056 process_peer_string (void *cls, const char *address, int res)
1057 {
1058   struct PeerResolutionContext *rc = cls;
1059
1060   if (GNUNET_SYSERR == res)
1061   {
1062     FPRINTF (stderr, "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
1063         GNUNET_i2s (&rc->id),
1064         rc->addrcp->transport_name,
1065         rc->addrcp->address_length);
1066     print_info (&rc->id, rc->transport, NULL, rc->state, rc->state_timeout);
1067     rc->printed = GNUNET_YES;
1068     return;
1069   }
1070
1071   if ((GNUNET_OK == res) && (address != NULL))
1072   {
1073     print_info (&rc->id, rc->transport, address, rc->state, rc->state_timeout);
1074     rc->printed = GNUNET_YES;
1075     return; /* Wait for done call */
1076   }
1077
1078   if (NULL == address)
1079   {
1080     /* done */
1081     GNUNET_assert(address_resolutions > 0);
1082     address_resolutions--;
1083     if (GNUNET_NO == rc->printed)
1084     {
1085       if (numeric == GNUNET_NO)
1086       {
1087         /* Failed to resolve address, try numeric lookup */
1088         resolve_peer_address (&rc->id, rc->addrcp, GNUNET_YES,
1089             rc->state, rc->state_timeout);
1090       }
1091       else
1092       {
1093         print_info (&rc->id, rc->transport, NULL,
1094             rc->state, rc->state_timeout);
1095       }
1096     }
1097     GNUNET_free (rc->transport);
1098     GNUNET_free (rc->addrcp);
1099     GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, rc);
1100     GNUNET_free(rc);
1101     if ((0 == address_resolutions) && (iterate_connections))
1102     {
1103       if (GNUNET_SCHEDULER_NO_TASK != end)
1104       {
1105         GNUNET_SCHEDULER_cancel (end);
1106         end = GNUNET_SCHEDULER_NO_TASK;
1107       }
1108       if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1109       {
1110         GNUNET_SCHEDULER_cancel (op_timeout);
1111         op_timeout = GNUNET_SCHEDULER_NO_TASK;
1112       }
1113       ret = 0;
1114       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1115     }
1116   }
1117 }
1118
1119
1120 static void
1121 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1122                       const struct GNUNET_HELLO_Address *address,
1123                       int numeric,
1124                       enum GNUNET_TRANSPORT_PeerState state,
1125                       struct GNUNET_TIME_Absolute state_timeout)
1126 {
1127   struct PeerResolutionContext *rc;
1128
1129   rc = GNUNET_new (struct PeerResolutionContext);
1130   GNUNET_assert(NULL != rc);
1131   GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
1132   address_resolutions++;
1133
1134   rc->id = (*id);
1135   rc->transport = GNUNET_strdup(address->transport_name);
1136   rc->addrcp = GNUNET_HELLO_address_copy (address);
1137   rc->printed = GNUNET_NO;
1138   rc->state = state;
1139   rc->state_timeout = state_timeout;
1140   /* Resolve address to string */
1141   rc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
1142       RESOLUTION_TIMEOUT, &process_peer_string, rc);
1143 }
1144
1145
1146 /**
1147  * Function called with information about a peers during a one shot iteration
1148  *
1149  * @param cls closure
1150  * @param peer identity of the peer, NULL for final callback when operation done
1151  * @param address binary address used to communicate with this peer,
1152  *  NULL on disconnect or when done
1153  * @param state current state this peer is in
1154  * @param state_timeout time out for the current state
1155  */
1156 static void
1157 process_peer_iteration_cb (void *cls,
1158                            const struct GNUNET_PeerIdentity *peer,
1159                            const struct GNUNET_HELLO_Address *address,
1160                            enum GNUNET_TRANSPORT_PeerState state,
1161                            struct GNUNET_TIME_Absolute state_timeout)
1162 {
1163   if (NULL == peer)
1164   {
1165     /* done */
1166     address_resolution_in_progress = GNUNET_NO;
1167     pic = NULL;
1168     if (GNUNET_SCHEDULER_NO_TASK != end)
1169       GNUNET_SCHEDULER_cancel (end);
1170     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1171     return;
1172   }
1173
1174   if ( (GNUNET_NO == iterate_all) &&
1175        (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)) )
1176       return; /* Display only connected peers */
1177
1178   if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1179     GNUNET_SCHEDULER_cancel (op_timeout);
1180   op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1181                                              NULL);
1182
1183   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1184              "Received address for peer `%s': %s\n",
1185              GNUNET_i2s (peer), address->transport_name);
1186
1187   if (NULL != address)
1188     resolve_peer_address (peer, address, numeric, state, state_timeout);
1189   else
1190     print_info (peer, NULL, NULL, state, state_timeout);
1191 }
1192
1193
1194 /**
1195  * Function called with information about a peers
1196  *
1197  * @param cls closure
1198  * @param peer identity of the peer, NULL for final callback when operation done
1199  * @param address binary address used to communicate with this peer,
1200  *  NULL on disconnect or when done
1201  * @param state current state this peer is in
1202  * @param state_timeout time out for the current state
1203  *
1204  */
1205 static void
1206 process_peer_monitoring_cb (void *cls,
1207                             const struct GNUNET_PeerIdentity *peer,
1208                             const struct GNUNET_HELLO_Address *address,
1209                             enum GNUNET_TRANSPORT_PeerState state,
1210                             struct GNUNET_TIME_Absolute state_timeout)
1211 {
1212   struct MonitoredPeer *m;
1213
1214   if (NULL == peer)
1215   {
1216     FPRINTF (stdout,
1217              "%s",
1218              _("Monitor disconnected from transport service. Reconnecting.\n"));
1219     return;
1220   }
1221
1222   if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1223     GNUNET_SCHEDULER_cancel (op_timeout);
1224   op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1225                                              NULL);
1226
1227   if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1228   {
1229     m = GNUNET_new (struct MonitoredPeer);
1230     GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
1231         m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1232   }
1233   else
1234   {
1235     if ( (m->state == state) &&
1236       (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1237       ((NULL == address) && (NULL == m->address)) )
1238     {
1239       return; /* No real change */
1240     }
1241     if ( (m->state == state) && ((NULL != address) && (NULL != m->address)) &&
1242         (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1243       return; /* No real change */
1244   }
1245
1246   if (NULL != m->address)
1247   {
1248     GNUNET_free (m->address);
1249     m->address = NULL;
1250   }
1251   if (NULL != address)
1252     m->address = GNUNET_HELLO_address_copy (address);
1253   m->state = state;
1254   m->state_timeout = state_timeout;
1255
1256   if (NULL != address)
1257     resolve_peer_address (peer, m->address, numeric, m->state, m->state_timeout);
1258   else
1259     print_info (peer, NULL, NULL, m->state, m->state_timeout);
1260 }
1261
1262 static void
1263 try_connect_cb (void *cls, const int result)
1264 {
1265   static int retries = 0;
1266   if (GNUNET_OK == result)
1267   {
1268     tc_handle = NULL;
1269     return;
1270   }
1271   retries++;
1272   if (retries < 10)
1273     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1274         NULL );
1275   else
1276   {
1277     FPRINTF (stderr, "%s",
1278         _("Failed to send connect request to transport service\n") );
1279     if (GNUNET_SCHEDULER_NO_TASK != end)
1280       GNUNET_SCHEDULER_cancel (end);
1281     ret = 1;
1282     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1283     return;
1284   }
1285 }
1286
1287 static void
1288 try_disconnect_cb (void *cls, const int result)
1289 {
1290   static int retries = 0;
1291   if (GNUNET_OK == result)
1292   {
1293     tc_handle = NULL;
1294     return;
1295   }
1296   retries++;
1297   if (retries < 10)
1298     tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1299         NULL );
1300   else
1301   {
1302     FPRINTF (stderr, "%s",
1303         _("Failed to send connect request to transport service\n") );
1304     if (GNUNET_SCHEDULER_NO_TASK != end)
1305       GNUNET_SCHEDULER_cancel (end);
1306     ret = 1;
1307     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1308     return;
1309   }
1310 }
1311
1312 /**
1313  * Function called with the result of the check if the 'transport'
1314  * service is running.
1315  *
1316  * @param cls closure with our configuration
1317  * @param result #GNUNET_YES if transport is running
1318  */
1319 static void
1320 testservice_task (void *cls, int result)
1321 {
1322   int counter = 0;
1323   ret = 1;
1324
1325   if (GNUNET_YES != result)
1326   {
1327     FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1328     return;
1329   }
1330
1331   if ((NULL != cpid)
1332       && (GNUNET_OK
1333           != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
1334               &pid.public_key)))
1335   {
1336     FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
1337     return;
1338   }
1339
1340   counter = benchmark_send + benchmark_receive + iterate_connections
1341       + monitor_connections + monitor_connects + try_connect + try_disconnect +
1342       + iterate_validation + monitor_validation;
1343
1344   if (1 < counter)
1345   {
1346     FPRINTF (stderr,
1347         _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
1348         "connect", "benchmark send", "benchmark receive", "information",
1349         "monitor", "events");
1350     return;
1351   }
1352   if (0 == counter)
1353   {
1354     FPRINTF (stderr,
1355         _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
1356         "connect", "benchmark send", "benchmark receive", "information",
1357         "monitor", "events");
1358     return;
1359   }
1360
1361   if (try_connect) /* -C: Connect to peer */
1362   {
1363     if (NULL == cpid)
1364     {
1365       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1366           "-C", "-p");
1367       ret = 1;
1368       return;
1369     }
1370     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1371         &notify_connect, &notify_disconnect);
1372     if (NULL == handle)
1373     {
1374       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1375       ret = 1;
1376       return;
1377     }
1378     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1379         NULL );
1380     if (NULL == tc_handle)
1381     {
1382       FPRINTF (stderr, "%s",
1383           _("Failed to send request to transport service\n") );
1384       ret = 1;
1385       return;
1386     }
1387     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1388         NULL );
1389
1390   }
1391   else if (try_disconnect) /* -D: Disconnect from peer */
1392   {
1393     if (NULL == cpid)
1394     {
1395       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1396           "-D", "-p");
1397       ret = 1;
1398       return;
1399     }
1400     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1401         &notify_connect, &notify_disconnect);
1402     if (NULL == handle)
1403     {
1404       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1405       ret = 1;
1406       return;
1407     }
1408     tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1409         NULL );
1410     if (NULL == tc_handle)
1411     {
1412       FPRINTF (stderr, "%s",
1413           _("Failed to send request to transport service\n") );
1414       ret = 1;
1415       return;
1416     }
1417     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1418         NULL );
1419
1420   }
1421   else if (benchmark_send) /* -s: Benchmark sending */
1422   {
1423     if (NULL == cpid)
1424     {
1425       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1426           "-s", "-p");
1427       ret = 1;
1428       return;
1429     }
1430     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1431         &notify_connect, &notify_disconnect);
1432     if (NULL == handle)
1433     {
1434       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1435       ret = 1;
1436       return;
1437     }
1438     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1439         NULL );
1440     if (NULL == tc_handle)
1441     {
1442       FPRINTF (stderr, "%s",
1443           _("Failed to send request to transport service\n") );
1444       ret = 1;
1445       return;
1446     }
1447     start_time = GNUNET_TIME_absolute_get ();
1448     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1449         NULL );
1450   }
1451   else if (benchmark_receive) /* -b: Benchmark receiving */
1452   {
1453     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive, NULL,
1454         NULL );
1455     if (NULL == handle)
1456     {
1457       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1458       ret = 1;
1459       return;
1460     }
1461     if (verbosity > 0)
1462       FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n") );
1463     start_time = GNUNET_TIME_absolute_get ();
1464
1465   }
1466   else if (iterate_connections) /* -i: List information about peers once */
1467   {
1468     address_resolution_in_progress = GNUNET_YES;
1469     pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1470         GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1471     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1472         NULL );
1473   }
1474   else if (monitor_connections) /* -m: List information about peers continuously */
1475   {
1476     monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1477     address_resolution_in_progress = GNUNET_YES;
1478     pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1479         GNUNET_NO, TIMEOUT, &process_peer_monitoring_cb, (void *) cfg);
1480   }
1481   else if (iterate_validation) /* -d: Print information about validations */
1482   {
1483     vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1484         (NULL == cpid) ? NULL : &pid,
1485         GNUNET_YES, TIMEOUT, &process_validation_cb, (void *) cfg);
1486   }
1487   else if (monitor_validation) /* -f: Print information about validations continuously */
1488   {
1489     vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1490         (NULL == cpid) ? NULL : &pid,
1491         GNUNET_NO, TIMEOUT, &process_validation_cb, (void *) cfg);
1492   }
1493   else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1494   {
1495     monitor_connect_counter = 0;
1496     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1497                                        &monitor_notify_connect,
1498                                        &monitor_notify_disconnect);
1499     if (NULL == handle)
1500     {
1501       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1502       ret = 1;
1503       return;
1504     }
1505     ret = 0;
1506   }
1507   else
1508   {
1509     GNUNET_break(0);
1510     return;
1511   }
1512
1513   end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1514       &shutdown_task, NULL );
1515
1516 }
1517
1518 /**
1519  * Main function that will be run by the scheduler.
1520  *
1521  * @param cls closure
1522  * @param args remaining command-line arguments
1523  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1524  * @param mycfg configuration
1525  */
1526 static void
1527 run (void *cls, char * const *args, const char *cfgfile,
1528     const struct GNUNET_CONFIGURATION_Handle *mycfg)
1529 {
1530   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1531   if (test_configuration)
1532   {
1533     do_test_configuration (cfg);
1534     return;
1535   }
1536   GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1537       &testservice_task, (void *) cfg);
1538 }
1539
1540 int
1541 main (int argc, char * const *argv)
1542 {
1543   int res;
1544   static const struct GNUNET_GETOPT_CommandLineOption options[] =
1545       {
1546           { 'a', "all", NULL,
1547               gettext_noop ("print information for all peers (instead of only connected peers )"),
1548               0, &GNUNET_GETOPT_set_one, &iterate_all },
1549           { 'b', "benchmark", NULL,
1550               gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1551               0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1552           { 'C', "connect",
1553               NULL, gettext_noop ("connect to a peer"), 0,
1554               &GNUNET_GETOPT_set_one, &try_connect },
1555           { 'D', "disconnect",
1556               NULL, gettext_noop ("disconnect to a peer"), 0,
1557               &GNUNET_GETOPT_set_one, &try_disconnect },
1558           { 'd', "validation", NULL,
1559               gettext_noop ("print information for all pending validations "),
1560               0, &GNUNET_GETOPT_set_one, &iterate_validation },
1561           { 'f', "monitorvalidation", NULL,
1562               gettext_noop ("print information for all pending validations continously"),
1563               0, &GNUNET_GETOPT_set_one, &monitor_validation },
1564           { 'i', "information", NULL,
1565               gettext_noop ("provide information about all current connections (once)"),
1566               0, &GNUNET_GETOPT_set_one, &iterate_connections },
1567           { 'm', "monitor", NULL,
1568               gettext_noop ("provide information about all current connections (continuously)"),
1569               0, &GNUNET_GETOPT_set_one, &monitor_connections },
1570           { 'e', "events", NULL,
1571               gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1572               0, &GNUNET_GETOPT_set_one, &monitor_connects }, { 'n', "numeric",
1573               NULL, gettext_noop ("do not resolve hostnames"), 0,
1574               &GNUNET_GETOPT_set_one, &numeric }, { 'p', "peer", "PEER",
1575               gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1576               &cpid }, { 's', "send", NULL, gettext_noop
1577           ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1578               &GNUNET_GETOPT_set_one, &benchmark_send },
1579           { 't', "test", NULL,
1580               gettext_noop ("test transport configuration (involves external server)"),
1581               0, &GNUNET_GETOPT_set_one, &test_configuration },
1582               GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1583               GNUNET_GETOPT_OPTION_END };
1584
1585   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1586     return 2;
1587
1588   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport", gettext_noop
1589   ("Direct access to transport service."), options, &run, NULL );
1590   GNUNET_free((void * ) argv);
1591   if (GNUNET_OK == res)
1592     return ret;
1593   return 1;
1594 }
1595
1596 /* end of gnunet-transport.c */