99724c0a4a3699cb7a145e13e1181a4c15a5b94b
[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, "%s", _("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     if (GNUNET_SCHEDULER_NO_TASK != end)
731       GNUNET_SCHEDULER_cancel (end);
732     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
733     return;
734   }
735   if (address == NULL)
736   {
737     FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
738     return;
739   }
740
741   if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
742         GNUNET_SCHEDULER_cancel (op_timeout);
743   op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
744                                              &operation_timeout, NULL);
745
746   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received address for peer `%s': %s\n",
747                 GNUNET_i2s (peer), address->transport_name);
748
749   rc = GNUNET_malloc(sizeof (struct ResolutionContext));
750   GNUNET_assert (NULL != rc);
751   GNUNET_CONTAINER_DLL_insert (rc_head, rc_tail, rc);
752   address_resolutions ++;
753
754   rc->addrcp = GNUNET_HELLO_address_copy(address);
755   rc->printed = GNUNET_NO;
756   /* Resolve address to string */
757   rc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
758                                       RESOLUTION_TIMEOUT, &process_string,
759                                       rc);
760
761 }
762
763 void try_connect_cb (void *cls,
764                      const int result)
765 {
766   static int retries = 0;
767   if (GNUNET_OK == result)
768   {
769       tc_handle = NULL;
770       return;
771   }
772   retries ++;
773   if (retries < 10)
774     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
775   else
776   {
777     FPRINTF (stderr, "%s", _("Failed to send connect request to transport service\n"));
778     if (GNUNET_SCHEDULER_NO_TASK != end)
779       GNUNET_SCHEDULER_cancel (end);
780     ret = 1;
781     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
782     return;
783   }
784 }
785
786
787 static void
788 testservice_task (void *cls,
789                   const struct GNUNET_SCHEDULER_TaskContext *tc)
790 {
791   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
792   int counter = 0;
793   ret = 1;
794
795   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
796   {
797       FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
798       return;
799   }
800
801   if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
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, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
813              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
814     return;
815   }
816   if (0 == counter)
817   {
818     FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
819              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
820     return;
821   }
822
823   if (try_connect) /* -C: Connect to peer */
824   {
825     if (NULL == cpid)
826     {
827       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
828                "-C", "-p");
829       ret = 1;
830       return;
831     }
832     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
833                                        &notify_receive,
834                                        &notify_connect,
835                                        &notify_disconnect);
836     if (NULL == handle)
837     {
838         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
839         ret = 1;
840         return;
841     }
842     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
843     if (NULL == tc_handle)
844     {
845         FPRINTF (stderr, "%s", _("Failed to send request to transport service\n"));
846         ret = 1;
847         return;
848     }
849     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
850                                                &operation_timeout, NULL);
851
852   }
853   else if (benchmark_send) /* -s: Benchmark sending */
854   {
855     if (NULL == cpid)
856     {
857       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
858                "-s", "-p");
859       ret = 1;
860       return;
861     }
862     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
863                                        &notify_receive,
864                                        &notify_connect,
865                                        &notify_disconnect);
866     if (NULL == handle)
867     {
868         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
869         ret = 1;
870         return;
871     }
872     tc_handle =  GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
873     if (NULL == tc_handle)
874     {
875         FPRINTF (stderr, "%s", _("Failed to send request to transport service\n"));
876         ret = 1;
877         return;
878     }
879     start_time = GNUNET_TIME_absolute_get ();
880     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
881                                                &operation_timeout, NULL);
882   }
883   else if (benchmark_receive) /* -b: Benchmark receiving */
884   {
885     handle =
886         GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
887                                   NULL, NULL);
888     if (NULL == handle)
889     {
890         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
891         ret = 1;
892         return;
893     }
894     if (verbosity > 0)
895       FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
896     start_time = GNUNET_TIME_absolute_get ();
897
898   }
899   else if (iterate_connections) /* -i: List all active addresses once */
900   {
901     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
902     address_resolution_in_progress = GNUNET_YES;
903     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
904                                                 (NULL == cpid) ? NULL : &pid,
905                                                 GNUNET_YES,
906                                                 TIMEOUT,
907                                                 &process_address, (void *) cfg);
908     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
909                                                &operation_timeout, NULL);
910   }
911   else if (monitor_connections) /* -m: List all active addresses continously */
912   {
913     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
914     address_resolution_in_progress = GNUNET_YES;
915     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
916                                                 (NULL == cpid) ? NULL : &pid,
917                                                 GNUNET_NO,
918                                                 TIMEOUT,
919                                                 &process_address, (void *) cfg);
920   }
921   else if (monitor_connects) /* -e : Monitor (dis)connect events continously */
922   {
923     monitor_connect_counter = 0;
924     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
925                                        &monitor_notify_connect,
926                                        &monitor_notify_disconnect);
927     if (NULL == handle)
928     {
929       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
930       ret = 1;
931       return;
932     }
933     ret = 0;
934   }
935   else
936   {
937     GNUNET_break (0);
938     return;
939   }
940
941   end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
942                                       &shutdown_task,
943                                       NULL);
944
945 }
946
947
948 /**
949  * Main function that will be run by the scheduler.
950  *
951  * @param cls closure
952  * @param args remaining command-line arguments
953  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
954  * @param cfg configuration
955  */
956 static void
957 run (void *cls, char *const *args, const char *cfgfile,
958      const struct GNUNET_CONFIGURATION_Handle *cfg)
959 {
960   if (test_configuration)
961   {
962     do_test_configuration (cfg);
963     return;
964   }
965
966   GNUNET_CLIENT_service_test ("transport", cfg,
967       GNUNET_TIME_UNIT_SECONDS,
968       &testservice_task,
969       (void *) cfg);
970 }
971
972
973 int
974 main (int argc, char *const *argv)
975 {
976   int res;
977   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
978     {'b', "benchmark", NULL,
979      gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
980      0, &GNUNET_GETOPT_set_one, &benchmark_receive},
981     {'C', "connect", NULL,
982      gettext_noop ("connect to a peer"),
983      0, &GNUNET_GETOPT_set_one, &try_connect},
984     {'i', "information", NULL,
985      gettext_noop ("provide information about all current connections (once)"),
986      0, &GNUNET_GETOPT_set_one, &iterate_connections},
987     {'m', "monitor", NULL,
988      gettext_noop ("provide information about all current connections (continuously)"),
989      0, &GNUNET_GETOPT_set_one, &monitor_connections},
990     {'e', "events", NULL,
991      gettext_noop ("provide information about all connects and disconnect events (continuously)"),
992      0, &GNUNET_GETOPT_set_one, &monitor_connects},
993     {'n', "numeric", NULL,
994      gettext_noop ("do not resolve hostnames"),
995      0, &GNUNET_GETOPT_set_one, &numeric},
996      {'p', "peer", "PEER",
997       gettext_noop ("peer identity"),
998       1, &GNUNET_GETOPT_set_string, &cpid},
999     {'s', "send", NULL,
1000      gettext_noop
1001      ("send data for benchmarking to the other peer (until CTRL-C)"),
1002      0, &GNUNET_GETOPT_set_one, &benchmark_send},
1003     {'t', "test", NULL,
1004      gettext_noop ("test transport configuration (involves external server)"),
1005      0, &GNUNET_GETOPT_set_one, &test_configuration},
1006     GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1007     GNUNET_GETOPT_OPTION_END
1008   };
1009
1010   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1011     return 2;
1012
1013   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
1014                               gettext_noop
1015                               ("Direct access to transport service."), options,
1016                               &run, NULL);
1017   GNUNET_free ((void *) argv);
1018   if (GNUNET_OK == res)
1019     return ret;
1020   return 1;
1021 }
1022
1023
1024 /* end of gnunet-transport.c */