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