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