c701970d16b48e65ca9c25dde6f0f971c5c195ad
[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 on success.
541  * Clean up and update GUI (with success).
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, int success, const char *emsg)
549 {
550   struct TestContext *tc = cls;
551   display_test_result (tc, success);
552 }
553
554 /**
555  * Function called if NAT failed to confirm success.
556  * Clean up and update GUI (with failure).
557  *
558  * @param cls test context
559  * @param tc scheduler callback
560  */
561 static void
562 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
563 {
564   struct TestContext *tstc = cls;
565
566   tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
567   display_test_result (tstc, NAT_TEST_TIMEOUT);
568 }
569
570
571 static void
572 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
573                             const struct GNUNET_HELLO_Address *address,
574                             int numeric,
575                             struct GNUNET_TIME_Absolute last_validation,
576                             struct GNUNET_TIME_Absolute valid_until,
577                             struct GNUNET_TIME_Absolute next_validation,
578                             enum GNUNET_TRANSPORT_ValidationState state);
579
580
581 static void
582 process_validation_string (void *cls, const char *address)
583 {
584   struct ValidationResolutionContext *vc = cls;
585   char *s_valid;
586   char *s_last;
587   char *s_next;
588
589   if (address != NULL )
590   {
591     if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
592       s_valid = GNUNET_strdup("never");
593     else
594       s_valid = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
595
596     if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
597       s_last = GNUNET_strdup("never");
598     else
599       s_last = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
600
601     if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
602       s_next = GNUNET_strdup("never");
603     else
604       s_next = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
605
606     FPRINTF (stdout,
607         _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
608         GNUNET_i2s (&vc->id), address,
609         (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
610         "Valid until    : ", s_valid,
611         "Last validation: ",s_last,
612         "Next validation: ", s_next);
613     GNUNET_free (s_valid);
614     GNUNET_free (s_last);
615     GNUNET_free (s_next);
616     vc->printed = GNUNET_YES;
617   }
618   else
619   {
620     /* done */
621     GNUNET_assert(address_resolutions > 0);
622     address_resolutions--;
623     if (GNUNET_NO == vc->printed)
624     {
625       if (numeric == GNUNET_NO)
626       {
627         /* Failed to resolve address, try numeric lookup */
628         resolve_validation_address (&vc->id, vc->addrcp, GNUNET_NO,
629            vc->last_validation, vc->valid_until, vc->next_validation,
630            vc->state);
631       }
632       else
633       {
634         FPRINTF (stdout, _("Peer `%s' %s `%s' \n"),
635             GNUNET_i2s (&vc->id), "<unable to resolve address>",
636             GNUNET_TRANSPORT_vs2s (vc->state));
637       }
638     }
639     GNUNET_free (vc->transport);
640     GNUNET_free (vc->addrcp);
641     GNUNET_CONTAINER_DLL_remove(vc_head, vc_tail, vc);
642     GNUNET_free(vc);
643     if ((0 == address_resolutions) && (iterate_validation))
644     {
645       if (GNUNET_SCHEDULER_NO_TASK != end)
646       {
647         GNUNET_SCHEDULER_cancel (end);
648         end = GNUNET_SCHEDULER_NO_TASK;
649       }
650       if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
651       {
652         GNUNET_SCHEDULER_cancel (op_timeout);
653         op_timeout = GNUNET_SCHEDULER_NO_TASK;
654       }
655       ret = 0;
656       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
657     }
658   }
659 }
660
661
662
663 static void
664 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
665     const struct GNUNET_HELLO_Address *address, int numeric,
666     struct GNUNET_TIME_Absolute last_validation,
667     struct GNUNET_TIME_Absolute valid_until,
668     struct GNUNET_TIME_Absolute next_validation,
669     enum GNUNET_TRANSPORT_ValidationState state)
670 {
671   struct ValidationResolutionContext *vc;
672
673   vc = GNUNET_new (struct ValidationResolutionContext);
674   GNUNET_assert(NULL != vc);
675   GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
676   address_resolutions++;
677
678   vc->id = (*id);
679   vc->transport = GNUNET_strdup(address->transport_name);
680   vc->addrcp = GNUNET_HELLO_address_copy (address);
681   vc->printed = GNUNET_NO;
682   vc->state = state;
683   vc->last_validation = last_validation;
684   vc->valid_until = valid_until;
685   vc->next_validation = next_validation;
686
687   /* Resolve address to string */
688   vc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
689       RESOLUTION_TIMEOUT, &process_validation_string, vc);
690 }
691
692
693 static void
694 process_validation_cb (void *cls,
695                        const struct GNUNET_PeerIdentity *peer,
696                        const struct GNUNET_HELLO_Address *address,
697                        struct GNUNET_TIME_Absolute last_validation,
698                        struct GNUNET_TIME_Absolute valid_until,
699                        struct GNUNET_TIME_Absolute next_validation,
700                        enum GNUNET_TRANSPORT_ValidationState state)
701 {
702   if ((NULL == peer) && (NULL == address))
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       &result_callback, head);
738   if (NULL == head->tst)
739   {
740     display_test_result (head, NAT_TEST_FAILED_TO_START);
741     return;
742   }
743   head->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, head);
744 }
745
746
747 /**
748  * Test our plugin's configuration (NAT traversal, etc.).
749  *
750  * @param cfg configuration to test
751  */
752 static void
753 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
754 {
755   char *plugins;
756   char *tok;
757   unsigned long long bnd_port;
758   unsigned long long adv_port;
759   struct TestContext *tc;
760   char *binary;
761
762   if (GNUNET_OK
763       != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
764           &plugins))
765   {
766     FPRINTF (stderr, "%s", _
767     ("No transport plugins configured, peer will never communicate\n") );
768     ret = 4;
769     return;
770   }
771
772   for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
773   {
774     char section[12 + strlen (tok)];
775     GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
776     if (GNUNET_OK
777         != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
778             &bnd_port))
779     {
780       FPRINTF (stderr,
781           _("No port configured for plugin `%s', cannot test it\n"), tok);
782       continue;
783     }
784     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
785             "ADVERTISED_PORT", &adv_port))
786       adv_port = bnd_port;
787
788     tc = GNUNET_new (struct TestContext);
789     tc->name = GNUNET_strdup (tok);
790     tc->adv_port = adv_port;
791     tc->bnd_port = bnd_port;
792     GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
793   }
794   GNUNET_free(plugins);
795
796   if ((NULL != head) && (NULL == resolver))
797   {
798     binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
799     resolver = GNUNET_OS_start_process (GNUNET_YES,
800                                         GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
801                                         NULL, NULL, NULL,
802                                         binary,
803                                         "gnunet-service-resolver", NULL );
804     if (NULL == resolver)
805     {
806       FPRINTF (stderr, _("Failed to start resolver!\n"));
807       return;
808     }
809
810     GNUNET_free(binary);
811     GNUNET_RESOLVER_connect (cfg);
812     run_nat_test ();
813   }
814 }
815
816 /**
817  * Function called to notify a client about the socket
818  * begin ready to queue more data.  @a buf will be
819  * NULL and @a size zero if the socket was closed for
820  * writing in the meantime.
821  *
822  * @param cls closure
823  * @param size number of bytes available in @a buf
824  * @param buf where the callee should write the message
825  * @return number of bytes written to @a buf
826  */
827 static size_t
828 transmit_data (void *cls, size_t size, void *buf)
829 {
830   struct GNUNET_MessageHeader *m = buf;
831
832   if ((NULL == buf) || (0 == size))
833   {
834     th = NULL;
835     return 0;
836   }
837
838   GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
839   GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
840   m->size = ntohs (size);
841   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
842   memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
843   traffic_sent += size;
844   th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
845                                                BLOCKSIZE * 1024,
846                                                GNUNET_TIME_UNIT_FOREVER_REL,
847                                                &transmit_data, NULL );
848   if (verbosity > 0)
849     FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
850         GNUNET_i2s (&pid));
851   return size;
852 }
853
854 /**
855  * Function called to notify transport users that another
856  * peer connected to us.
857  *
858  * @param cls closure
859  * @param peer the peer that connected
860  */
861 static void
862 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
863 {
864   if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
865     return;
866   ret = 0;
867   if (try_connect)
868   {
869     /* all done, terminate instantly */
870     FPRINTF (stdout, _("Successfully connected to `%s'\n"),
871         GNUNET_i2s_full (peer));
872     ret = 0;
873
874     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
875     {
876       GNUNET_SCHEDULER_cancel (op_timeout);
877       op_timeout = GNUNET_SCHEDULER_NO_TASK;
878     }
879
880     if (GNUNET_SCHEDULER_NO_TASK != end)
881       GNUNET_SCHEDULER_cancel (end);
882     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
883     return;
884   }
885   if (benchmark_send)
886   {
887     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
888     {
889       GNUNET_SCHEDULER_cancel (op_timeout);
890       op_timeout = GNUNET_SCHEDULER_NO_TASK;
891     }
892     if (verbosity > 0)
893       FPRINTF (stdout,
894           _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
895           GNUNET_i2s (&pid), BLOCKSIZE);
896     start_time = GNUNET_TIME_absolute_get ();
897     if (NULL == th)
898       th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
899                                                    BLOCKSIZE * 1024,
900                                                    GNUNET_TIME_UNIT_FOREVER_REL,
901                                                    &transmit_data,
902                                                    NULL);
903     else
904       GNUNET_break(0);
905     return;
906   }
907 }
908
909 /**
910  * Function called to notify transport users that another
911  * peer disconnected from us.
912  *
913  * @param cls closure
914  * @param peer the peer that disconnected
915  */
916 static void
917 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
918 {
919   if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
920     return;
921
922   if (try_disconnect)
923   {
924     /* all done, terminate instantly */
925     FPRINTF (stdout, _("Successfully disconnected from `%s'\n"),
926         GNUNET_i2s_full (peer));
927     ret = 0;
928
929     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
930     {
931       GNUNET_SCHEDULER_cancel (op_timeout);
932       op_timeout = GNUNET_SCHEDULER_NO_TASK;
933     }
934
935     if (GNUNET_SCHEDULER_NO_TASK != end)
936       GNUNET_SCHEDULER_cancel (end);
937     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
938     return;
939   }
940
941   if (NULL != th)
942   {
943     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
944     th = NULL;
945   }
946   if (benchmark_send)
947   {
948     FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
949         GNUNET_i2s (&pid));
950     if (GNUNET_SCHEDULER_NO_TASK != end)
951       GNUNET_SCHEDULER_cancel (end);
952     return;
953   }
954 }
955
956 /**
957  * Function called to notify transport users that another
958  * peer connected to us.
959  *
960  * @param cls closure
961  * @param peer the peer that connected
962  */
963 static void
964 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
965 {
966   monitor_connect_counter++;
967   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
968   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
969
970   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"), now_str,
971       _("Connected to"), GNUNET_i2s (peer), monitor_connect_counter);
972 }
973
974 /**
975  * Function called to notify transport users that another
976  * peer disconnected from us.
977  *
978  * @param cls closure
979  * @param peer the peer that disconnected
980  */
981 static void
982 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
983 {
984   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
985   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
986
987   GNUNET_assert(monitor_connect_counter > 0);
988   monitor_connect_counter--;
989
990   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"), now_str,
991       _("Disconnected from"), GNUNET_i2s (peer), monitor_connect_counter);
992 }
993
994 /**
995  * Function called by the transport for each received message.
996  *
997  * @param cls closure
998  * @param peer (claimed) identity of the other peer
999  * @param message the message
1000  */
1001 static void
1002 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
1003     const struct GNUNET_MessageHeader *message)
1004 {
1005   if (benchmark_receive)
1006   {
1007     if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1008       return;
1009     if (verbosity > 0)
1010       FPRINTF (stdout, _("Received %u bytes from %s\n"),
1011           (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
1012
1013     if (traffic_received == 0)
1014       start_time = GNUNET_TIME_absolute_get ();
1015     traffic_received += ntohs (message->size);
1016     return;
1017   }
1018 }
1019
1020
1021 static void
1022 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1023                       const struct GNUNET_HELLO_Address *address,
1024                       int numeric,
1025                       enum GNUNET_TRANSPORT_PeerState state,
1026                       struct GNUNET_TIME_Absolute state_timeout);
1027
1028
1029 static void
1030 print_info (const struct GNUNET_PeerIdentity *id,
1031             const char *transport,
1032             const char *addr,
1033             enum GNUNET_TRANSPORT_PeerState state,
1034             struct GNUNET_TIME_Absolute state_timeout)
1035 {
1036
1037   if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1038        (GNUNET_YES == monitor_connections) )
1039   {
1040     FPRINTF (stdout, _("Peer `%s': %s %s in state `%s' until %s\n"),
1041         GNUNET_i2s (id),
1042         (NULL == transport) ? "<none>" : transport,
1043         (NULL == transport) ? "<none>" : addr,
1044         GNUNET_TRANSPORT_ps2s (state),
1045         GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1046   }
1047   else if ( (GNUNET_YES == iterate_connections) &&
1048              (GNUNET_TRANSPORT_is_connected(state)) )
1049   {
1050     /* Only connected peers, skip state */
1051     FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (id), transport, addr);
1052   }
1053 }
1054
1055
1056 static void
1057 process_peer_string (void *cls, const char *address)
1058 {
1059   struct PeerResolutionContext *rc = cls;
1060
1061   if (address != NULL )
1062   {
1063     print_info (&rc->id, rc->transport, address, rc->state, rc->state_timeout);
1064     rc->printed = GNUNET_YES;
1065   }
1066   else
1067   {
1068     /* done */
1069     GNUNET_assert(address_resolutions > 0);
1070     address_resolutions--;
1071     if (GNUNET_NO == rc->printed)
1072     {
1073       if (numeric == GNUNET_NO)
1074       {
1075         /* Failed to resolve address, try numeric lookup */
1076         resolve_peer_address (&rc->id, rc->addrcp, GNUNET_YES,
1077             rc->state, rc->state_timeout);
1078       }
1079       else
1080       {
1081         print_info (&rc->id, rc->transport, "<unable to resolve address>",
1082             rc->state, rc->state_timeout);
1083       }
1084     }
1085     GNUNET_free (rc->transport);
1086     GNUNET_free (rc->addrcp);
1087     GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, rc);
1088     GNUNET_free(rc);
1089     if ((0 == address_resolutions) && (iterate_connections))
1090     {
1091       if (GNUNET_SCHEDULER_NO_TASK != end)
1092       {
1093         GNUNET_SCHEDULER_cancel (end);
1094         end = GNUNET_SCHEDULER_NO_TASK;
1095       }
1096       if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1097       {
1098         GNUNET_SCHEDULER_cancel (op_timeout);
1099         op_timeout = GNUNET_SCHEDULER_NO_TASK;
1100       }
1101       ret = 0;
1102       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1103     }
1104   }
1105 }
1106
1107
1108 static void
1109 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1110                       const struct GNUNET_HELLO_Address *address,
1111                       int numeric,
1112                       enum GNUNET_TRANSPORT_PeerState state,
1113                       struct GNUNET_TIME_Absolute state_timeout)
1114 {
1115   struct PeerResolutionContext *rc;
1116
1117   rc = GNUNET_new (struct PeerResolutionContext);
1118   GNUNET_assert(NULL != rc);
1119   GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
1120   address_resolutions++;
1121
1122   rc->id = (*id);
1123   rc->transport = GNUNET_strdup(address->transport_name);
1124   rc->addrcp = GNUNET_HELLO_address_copy (address);
1125   rc->printed = GNUNET_NO;
1126   rc->state = state;
1127   rc->state_timeout = state_timeout;
1128   /* Resolve address to string */
1129   rc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
1130       RESOLUTION_TIMEOUT, &process_peer_string, rc);
1131 }
1132
1133
1134 /**
1135  * Function called with information about a peers during a one shot iteration
1136  *
1137  * @param cls closure
1138  * @param peer identity of the peer, NULL for final callback when operation done
1139  * @param address binary address used to communicate with this peer,
1140  *  NULL on disconnect or when done
1141  * @param state current state this peer is in
1142  * @param state_timeout time out for the current state
1143  *
1144  */
1145 static void
1146 process_peer_iteration_cb (void *cls,
1147                            const struct GNUNET_PeerIdentity *peer,
1148                            const struct GNUNET_HELLO_Address *address,
1149                            enum GNUNET_TRANSPORT_PeerState state,
1150                            struct GNUNET_TIME_Absolute state_timeout)
1151 {
1152   if (NULL == peer)
1153   {
1154     if (monitor_connections)
1155     {
1156       FPRINTF (stdout,
1157                _("Monitor disconnected from transport service. Reconnecting.\n"));
1158       return;
1159     }
1160     /* done */
1161     address_resolution_in_progress = GNUNET_NO;
1162     pic = NULL;
1163     if (GNUNET_SCHEDULER_NO_TASK != end)
1164       GNUNET_SCHEDULER_cancel (end);
1165     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1166     return;
1167   }
1168
1169   if ((GNUNET_NO == iterate_all) && (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)) )
1170       return; /* Display only connected peers */
1171
1172   if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1173     GNUNET_SCHEDULER_cancel (op_timeout);
1174   op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1175       NULL );
1176
1177   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1178              "Received address for peer `%s': %s\n",
1179              GNUNET_i2s (peer), address->transport_name);
1180
1181   if (NULL != address)
1182     resolve_peer_address (peer, address, numeric, state, state_timeout);
1183   else
1184     print_info (peer, NULL, NULL, state, state_timeout);
1185 }
1186
1187
1188 /**
1189  * Function called with information about a peers
1190  *
1191  * @param cls closure
1192  * @param peer identity of the peer, NULL for final callback when operation done
1193  * @param address binary address used to communicate with this peer,
1194  *  NULL on disconnect or when done
1195  * @param state current state this peer is in
1196  * @param state_timeout time out for the current state
1197  *
1198  */
1199 static void
1200 process_peer_monitoring_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
1201     const struct GNUNET_HELLO_Address *address,
1202     enum GNUNET_TRANSPORT_PeerState state,
1203     struct GNUNET_TIME_Absolute state_timeout)
1204 {
1205   struct MonitoredPeer *m;
1206
1207   if (peer == NULL )
1208   {
1209     /* done */
1210     address_resolution_in_progress = GNUNET_NO;
1211     pic = NULL;
1212     if (GNUNET_SCHEDULER_NO_TASK != end)
1213       GNUNET_SCHEDULER_cancel (end);
1214     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1215     return;
1216   }
1217
1218   if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1219     GNUNET_SCHEDULER_cancel (op_timeout);
1220   op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1221       NULL );
1222
1223   if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1224   {
1225     m = GNUNET_new (struct MonitoredPeer);
1226     GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
1227         m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1228   }
1229   else
1230   {
1231     if ( (m->state == state) &&
1232       (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1233       ((NULL == address) && (NULL == m->address)) )
1234     {
1235       return; /* No real change */
1236     }
1237     if ( (m->state == state) && ((NULL != address) && (NULL != m->address)) &&
1238         (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1239       return; /* No real change */
1240   }
1241
1242   if (NULL != m->address)
1243   {
1244     GNUNET_free (m->address);
1245     m->address = NULL;
1246   }
1247   if (NULL != address)
1248     m->address = GNUNET_HELLO_address_copy (address);
1249   m->state = state;
1250   m->state_timeout = state_timeout;
1251
1252   if (NULL != address)
1253     resolve_peer_address (peer, m->address, numeric, m->state, m->state_timeout);
1254   else
1255     print_info (peer, NULL, NULL, m->state, m->state_timeout);
1256 }
1257
1258 static void
1259 try_connect_cb (void *cls, const int result)
1260 {
1261   static int retries = 0;
1262   if (GNUNET_OK == result)
1263   {
1264     tc_handle = NULL;
1265     return;
1266   }
1267   retries++;
1268   if (retries < 10)
1269     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1270         NULL );
1271   else
1272   {
1273     FPRINTF (stderr, "%s",
1274         _("Failed to send connect request to transport service\n") );
1275     if (GNUNET_SCHEDULER_NO_TASK != end)
1276       GNUNET_SCHEDULER_cancel (end);
1277     ret = 1;
1278     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1279     return;
1280   }
1281 }
1282
1283 static void
1284 try_disconnect_cb (void *cls, const int result)
1285 {
1286   static int retries = 0;
1287   if (GNUNET_OK == result)
1288   {
1289     tc_handle = NULL;
1290     return;
1291   }
1292   retries++;
1293   if (retries < 10)
1294     tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1295         NULL );
1296   else
1297   {
1298     FPRINTF (stderr, "%s",
1299         _("Failed to send connect request to transport service\n") );
1300     if (GNUNET_SCHEDULER_NO_TASK != end)
1301       GNUNET_SCHEDULER_cancel (end);
1302     ret = 1;
1303     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1304     return;
1305   }
1306 }
1307
1308 /**
1309  * Function called with the result of the check if the 'transport'
1310  * service is running.
1311  *
1312  * @param cls closure with our configuration
1313  * @param result #GNUNET_YES if transport is running
1314  */
1315 static void
1316 testservice_task (void *cls, int result)
1317 {
1318   int counter = 0;
1319   ret = 1;
1320
1321   if (GNUNET_YES != result)
1322   {
1323     FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1324     return;
1325   }
1326
1327   if ((NULL != cpid)
1328       && (GNUNET_OK
1329           != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
1330               &pid.public_key)))
1331   {
1332     FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
1333     return;
1334   }
1335
1336   counter = benchmark_send + benchmark_receive + iterate_connections
1337       + monitor_connections + monitor_connects + try_connect + try_disconnect +
1338       + iterate_validation + monitor_validation;
1339
1340   if (1 < counter)
1341   {
1342     FPRINTF (stderr,
1343         _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
1344         "connect", "benchmark send", "benchmark receive", "information",
1345         "monitor", "events");
1346     return;
1347   }
1348   if (0 == counter)
1349   {
1350     FPRINTF (stderr,
1351         _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
1352         "connect", "benchmark send", "benchmark receive", "information",
1353         "monitor", "events");
1354     return;
1355   }
1356
1357   if (try_connect) /* -C: Connect to peer */
1358   {
1359     if (NULL == cpid)
1360     {
1361       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1362           "-C", "-p");
1363       ret = 1;
1364       return;
1365     }
1366     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1367         &notify_connect, &notify_disconnect);
1368     if (NULL == handle)
1369     {
1370       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1371       ret = 1;
1372       return;
1373     }
1374     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1375         NULL );
1376     if (NULL == tc_handle)
1377     {
1378       FPRINTF (stderr, "%s",
1379           _("Failed to send request to transport service\n") );
1380       ret = 1;
1381       return;
1382     }
1383     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1384         NULL );
1385
1386   }
1387   else if (try_disconnect) /* -D: Disconnect from peer */
1388   {
1389     if (NULL == cpid)
1390     {
1391       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1392           "-D", "-p");
1393       ret = 1;
1394       return;
1395     }
1396     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1397         &notify_connect, &notify_disconnect);
1398     if (NULL == handle)
1399     {
1400       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1401       ret = 1;
1402       return;
1403     }
1404     tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1405         NULL );
1406     if (NULL == tc_handle)
1407     {
1408       FPRINTF (stderr, "%s",
1409           _("Failed to send request to transport service\n") );
1410       ret = 1;
1411       return;
1412     }
1413     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1414         NULL );
1415
1416   }
1417   else if (benchmark_send) /* -s: Benchmark sending */
1418   {
1419     if (NULL == cpid)
1420     {
1421       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1422           "-s", "-p");
1423       ret = 1;
1424       return;
1425     }
1426     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1427         &notify_connect, &notify_disconnect);
1428     if (NULL == handle)
1429     {
1430       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1431       ret = 1;
1432       return;
1433     }
1434     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1435         NULL );
1436     if (NULL == tc_handle)
1437     {
1438       FPRINTF (stderr, "%s",
1439           _("Failed to send request to transport service\n") );
1440       ret = 1;
1441       return;
1442     }
1443     start_time = GNUNET_TIME_absolute_get ();
1444     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1445         NULL );
1446   }
1447   else if (benchmark_receive) /* -b: Benchmark receiving */
1448   {
1449     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive, NULL,
1450         NULL );
1451     if (NULL == handle)
1452     {
1453       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1454       ret = 1;
1455       return;
1456     }
1457     if (verbosity > 0)
1458       FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n") );
1459     start_time = GNUNET_TIME_absolute_get ();
1460
1461   }
1462   else if (iterate_connections) /* -i: List information about peers once */
1463   {
1464     address_resolution_in_progress = GNUNET_YES;
1465     pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1466         GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1467     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1468         NULL );
1469   }
1470   else if (monitor_connections) /* -m: List information about peers continuously */
1471   {
1472     monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1473     address_resolution_in_progress = GNUNET_YES;
1474     pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1475         GNUNET_NO, TIMEOUT, &process_peer_monitoring_cb, (void *) cfg);
1476   }
1477   else if (iterate_validation) /* -d: Print information about validations */
1478   {
1479     vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1480         (NULL == cpid) ? NULL : &pid,
1481         GNUNET_YES, TIMEOUT, &process_validation_cb, (void *) cfg);
1482   }
1483   else if (monitor_validation) /* -f: Print information about validations continuously */
1484   {
1485     vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1486         (NULL == cpid) ? NULL : &pid,
1487         GNUNET_NO, TIMEOUT, &process_validation_cb, (void *) cfg);
1488   }
1489   else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1490   {
1491     monitor_connect_counter = 0;
1492     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1493         &monitor_notify_connect, &monitor_notify_disconnect);
1494     if (NULL == handle)
1495     {
1496       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1497       ret = 1;
1498       return;
1499     }
1500     ret = 0;
1501   }
1502   else
1503   {
1504     GNUNET_break(0);
1505     return;
1506   }
1507
1508   end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1509       &shutdown_task, NULL );
1510
1511 }
1512
1513 /**
1514  * Main function that will be run by the scheduler.
1515  *
1516  * @param cls closure
1517  * @param args remaining command-line arguments
1518  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1519  * @param mycfg configuration
1520  */
1521 static void
1522 run (void *cls, char * const *args, const char *cfgfile,
1523     const struct GNUNET_CONFIGURATION_Handle *mycfg)
1524 {
1525   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1526   if (test_configuration)
1527   {
1528     do_test_configuration (cfg);
1529     return;
1530   }
1531   GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1532       &testservice_task, (void *) cfg);
1533 }
1534
1535 int
1536 main (int argc, char * const *argv)
1537 {
1538   int res;
1539   static const struct GNUNET_GETOPT_CommandLineOption options[] =
1540       {
1541           { 'a', "all", NULL,
1542               gettext_noop ("print information for all peers (instead of only connected peers )"),
1543               0, &GNUNET_GETOPT_set_one, &iterate_all },
1544           { 'b', "benchmark", NULL,
1545               gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1546               0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1547           { 'C', "connect",
1548               NULL, gettext_noop ("connect to a peer"), 0,
1549               &GNUNET_GETOPT_set_one, &try_connect },
1550           { 'D', "disconnect",
1551               NULL, gettext_noop ("disconnect to a peer"), 0,
1552               &GNUNET_GETOPT_set_one, &try_disconnect },
1553           { 'd', "validation", NULL,
1554               gettext_noop ("print information for all pending validations "),
1555               0, &GNUNET_GETOPT_set_one, &iterate_validation },
1556           { 'f', "monitorvalidation", NULL,
1557               gettext_noop ("print information for all pending validations continously"),
1558               0, &GNUNET_GETOPT_set_one, &monitor_validation },
1559           { 'i', "information", NULL,
1560               gettext_noop ("provide information about all current connections (once)"),
1561               0, &GNUNET_GETOPT_set_one, &iterate_connections },
1562           { 'm', "monitor", NULL,
1563               gettext_noop ("provide information about all current connections (continuously)"),
1564               0, &GNUNET_GETOPT_set_one, &monitor_connections },
1565           { 'e', "events", NULL,
1566               gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1567               0, &GNUNET_GETOPT_set_one, &monitor_connects }, { 'n', "numeric",
1568               NULL, gettext_noop ("do not resolve hostnames"), 0,
1569               &GNUNET_GETOPT_set_one, &numeric }, { 'p', "peer", "PEER",
1570               gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1571               &cpid }, { 's', "send", NULL, gettext_noop
1572           ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1573               &GNUNET_GETOPT_set_one, &benchmark_send },
1574           { 't', "test", NULL,
1575               gettext_noop ("test transport configuration (involves external server)"),
1576               0, &GNUNET_GETOPT_set_one, &test_configuration },
1577               GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1578               GNUNET_GETOPT_OPTION_END };
1579
1580   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1581     return 2;
1582
1583   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport", gettext_noop
1584   ("Direct access to transport service."), options, &run, NULL );
1585   GNUNET_free((void * ) argv);
1586   if (GNUNET_OK == res)
1587     return ret;
1588   return 1;
1589 }
1590
1591 /* end of gnunet-transport.c */