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