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