4e251a009256fc6a3ae85f58d72235fdc34bca1c
[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, 30)
43 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
44 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
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
277
278 static void
279 operation_timeout (void *cls,
280                const struct GNUNET_SCHEDULER_TaskContext *tc)
281 {
282   op_timeout = GNUNET_SCHEDULER_NO_TASK;
283   if ((try_connect) || (benchmark_send) || (benchmark_receive))
284   {
285       FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_h2s_full (&pid.hashPubKey));
286       if (GNUNET_SCHEDULER_NO_TASK != end)
287         GNUNET_SCHEDULER_cancel (end);
288       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
289       ret = 1;
290       return;
291   }
292 }
293
294
295
296 /**
297  * Display the result of the test.
298  *
299  * @param tc test context
300  * @param result GNUNET_YES on success
301  */
302 static void
303 display_test_result (struct TestContext *tc, int result)
304 {
305   if (GNUNET_YES != result)
306   {
307     FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
308   }
309   else
310   {
311     FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
312   }
313   if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
314   {
315     GNUNET_SCHEDULER_cancel (tc->tsk);
316     tc->tsk = GNUNET_SCHEDULER_NO_TASK;
317   }
318   if (NULL != tc->tst)
319   {
320     GNUNET_NAT_test_stop (tc->tst);
321     tc->tst = NULL;
322   }
323   GNUNET_free (tc);
324   resolver_users--;
325   if ((0 == resolver_users) && (NULL != resolver))
326   {
327     GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
328     GNUNET_OS_process_destroy (resolver);
329     resolver = NULL;
330   }
331 }
332
333
334 /**
335  * Function called by NAT on success.
336  * Clean up and update GUI (with success).
337  *
338  * @param cls test context
339  * @param success currently always GNUNET_OK
340  */
341 static void
342 result_callback (void *cls, int success)
343 {
344   struct TestContext *tc = cls;
345
346   display_test_result (tc, success);
347 }
348
349
350 /**
351  * Function called if NAT failed to confirm success.
352  * Clean up and update GUI (with failure).
353  *
354  * @param cls test context
355  * @param tc scheduler callback
356  */
357 static void
358 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
359 {
360   struct TestContext *tstc = cls;
361
362   tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
363   display_test_result (tstc, GNUNET_NO);
364 }
365
366
367 /**
368  * Test our plugin's configuration (NAT traversal, etc.).
369  *
370  * @param cfg configuration to test
371  */
372 static void
373 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
374 {
375   char *plugins;
376   char *tok;
377   unsigned long long bnd_port;
378   unsigned long long adv_port;
379   struct TestContext *tc;
380
381   if (GNUNET_OK !=
382       GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
383                                              &plugins))
384   {
385     FPRINTF (stderr,
386              "%s",
387              _
388              ("No transport plugins configured, peer will never communicate\n"));
389     ret = 4;
390     return;
391   }
392   for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
393   {
394     char section[12 + strlen (tok)];
395
396     GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
397     if (GNUNET_OK !=
398         GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
399     {
400       FPRINTF (stderr,
401                _("No port configured for plugin `%s', cannot test it\n"), tok);
402       continue;
403     }
404     if (GNUNET_OK !=
405         GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
406                                                &adv_port))
407       adv_port = bnd_port;
408     if (NULL == resolver)
409       resolver =
410           GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
411                                    "gnunet-service-resolver", NULL);
412     resolver_users++;
413     GNUNET_RESOLVER_connect (cfg);
414     tc = GNUNET_malloc (sizeof (struct TestContext));
415     tc->name = GNUNET_strdup (tok);
416     tc->tst =
417         GNUNET_NAT_test_start (cfg,
418                                (0 ==
419                                 strcasecmp (tok,
420                                             "udp")) ? GNUNET_NO : GNUNET_YES,
421                                (uint16_t) bnd_port, (uint16_t) adv_port,
422                                &result_callback, tc);
423     if (NULL == tc->tst)
424     {
425       display_test_result (tc, GNUNET_SYSERR);
426       continue;
427     }
428     tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
429   }
430   GNUNET_free (plugins);
431 }
432
433 /**
434  * Function called to notify a client about the socket
435  * begin ready to queue more data.  "buf" will be
436  * NULL and "size" zero if the socket was closed for
437  * writing in the meantime.
438  *
439  * @param cls closure
440  * @param size number of bytes available in buf
441  * @param buf where the callee should write the message
442  * @return number of bytes written to buf
443  */
444 static size_t
445 transmit_data (void *cls, size_t size, void *buf)
446 {
447   struct GNUNET_MessageHeader *m = buf;
448
449   if ((NULL == buf) && (0 == size))
450   {
451     th = NULL;
452     return 0;
453   }
454
455   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
456   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
457   m->size = ntohs (size);
458   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
459   memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
460   traffic_sent += size;
461   th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, BLOCKSIZE * 1024, 0,
462                                                GNUNET_TIME_UNIT_FOREVER_REL,
463                                                &transmit_data, NULL);
464   if (verbosity > 0)
465     FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
466              GNUNET_i2s (&pid));
467   return size;
468 }
469
470
471 /**
472  * Function called to notify transport users that another
473  * peer connected to us.
474  *
475  * @param cls closure
476  * @param peer the peer that connected
477  * @param ats performance data
478  * @param ats_count number of entries in ats (excluding 0-termination)
479  */
480 static void
481 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
482                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
483 {
484   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
485     return;
486   ret = 0;
487   if (try_connect)
488   {
489       /* all done, terminate instantly */
490       FPRINTF (stdout, _("Successfully connected to `%s'\n"), GNUNET_h2s_full (&peer->hashPubKey));
491       ret = 0;
492
493       if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
494       {
495         GNUNET_SCHEDULER_cancel (op_timeout);
496         op_timeout = GNUNET_SCHEDULER_NO_TASK;
497       }
498
499       if (GNUNET_SCHEDULER_NO_TASK != end)
500         GNUNET_SCHEDULER_cancel (end);
501       end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
502       return;
503   }
504   if (benchmark_send)
505   {
506     if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
507     {
508       GNUNET_SCHEDULER_cancel (op_timeout);
509       op_timeout = GNUNET_SCHEDULER_NO_TASK;
510     }
511     if (verbosity > 0)
512       FPRINTF (stdout, _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
513           GNUNET_i2s (&pid), BLOCKSIZE);
514     start_time = GNUNET_TIME_absolute_get ();
515     if (NULL == th)
516       th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
517                                                    BLOCKSIZE * 1024, 0,
518                                                    GNUNET_TIME_UNIT_FOREVER_REL,
519                                                    &transmit_data, NULL);
520     else
521       GNUNET_break (0);
522     return;
523   }
524 }
525
526
527 /**
528  * Function called to notify transport users that another
529  * peer disconnected from us.
530  *
531  * @param cls closure
532  * @param peer the peer that disconnected
533  */
534 static void
535 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
536 {
537   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
538     return;
539
540   if (NULL != th)
541   {
542     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
543     th = NULL;
544   }
545   if (benchmark_send)
546   {
547       FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"), GNUNET_i2s (&pid));
548       if (GNUNET_SCHEDULER_NO_TASK != end)
549         GNUNET_SCHEDULER_cancel (end);
550       return;
551   }
552 }
553
554 /**
555  * Function called to notify transport users that another
556  * peer connected to us.
557  *
558  * @param cls closure
559  * @param peer the peer that connected
560  * @param ats performance data
561  * @param ats_count number of entries in ats (excluding 0-termination)
562  */
563 static void
564 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
565                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
566 {
567   monitor_connect_counter ++;
568   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
569   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
570
571   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
572            now_str,
573            _("Connected to"),
574            GNUNET_i2s (peer),
575            monitor_connect_counter);
576 }
577
578
579 /**
580  * Function called to notify transport users that another
581  * peer disconnected from us.
582  *
583  * @param cls closure
584  * @param peer the peer that disconnected
585  */
586 static void
587 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
588 {
589   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
590   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
591
592   GNUNET_assert (monitor_connect_counter > 0);
593   monitor_connect_counter --;
594
595   FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
596            now_str,
597            _("Disconnected from"),
598            GNUNET_i2s (peer),
599            monitor_connect_counter);
600 }
601
602
603
604 /**
605  * Function called by the transport for each received message.
606  *
607  * @param cls closure
608  * @param peer (claimed) identity of the other peer
609  * @param message the message
610  * @param ats performance data
611  * @param ats_count number of entries in ats
612  */
613 static void
614 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
615                 const struct GNUNET_MessageHeader *message,
616                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
617 {
618   if (benchmark_receive)
619   {
620     if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
621       return;
622     if (verbosity > 0)
623       FPRINTF (stdout, _("Received %u bytes from %s\n"),
624                (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
625
626     if (traffic_received == 0)
627       start_time = GNUNET_TIME_absolute_get ();
628     traffic_received += ntohs (message->size);
629     return;
630   }
631 }
632
633 struct ResolutionContext
634 {
635   struct GNUNET_HELLO_Address *addrcp;
636
637   int printed;
638 };
639
640
641 static void
642 process_string (void *cls, const char *address)
643 {
644   struct ResolutionContext *rc = cls;
645   struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
646
647   if (address != NULL)
648   {
649     FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
650     rc->printed = GNUNET_YES;
651   }
652   else
653   {
654     /* done */
655     GNUNET_assert (address_resolutions > 0);
656     address_resolutions --;
657     if (GNUNET_NO == rc->printed)
658       FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
659     GNUNET_free (rc->addrcp);
660     GNUNET_free (rc);
661     if ((0 == address_resolutions) && (iterate_connections))
662     {
663         if (GNUNET_SCHEDULER_NO_TASK != end)
664           GNUNET_SCHEDULER_cancel (end);
665         ret = 0;
666         end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
667     }
668   }
669 }
670
671 /**
672  * Function to call with a binary address
673  *
674  * @param cls closure
675  * @param peer identity of the peer
676  * @param address binary address (NULL on disconnect)
677  */
678 static void
679 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
680                  const struct GNUNET_HELLO_Address *address)
681 {
682   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
683   struct ResolutionContext *rc;
684
685   if (peer == NULL)
686   {
687     /* done */
688     address_resolution_in_progress = GNUNET_NO;
689     pic = NULL;
690     return;
691   }
692   if (address == NULL)
693   {
694     FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
695     return;
696   }
697
698   rc = GNUNET_malloc(sizeof (struct ResolutionContext));
699   rc->addrcp = GNUNET_HELLO_address_copy(address);
700   rc->printed = GNUNET_NO;
701
702   GNUNET_assert (NULL != rc);
703   address_resolutions ++;
704   /* Resolve address to string */
705   GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
706                                       RESOLUTION_TIMEOUT, &process_string,
707                                       rc);
708 }
709
710 void try_connect_cb (void *cls,
711                      const int result)
712 {
713   static int retries = 0;
714   if (GNUNET_OK == result)
715   {
716       tc_handle = NULL;
717       return;
718   }
719   retries ++;
720   if (retries < 10)
721     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
722   else
723   {
724     FPRINTF (stderr, "%s", _("Failed to send connect request to transport service\n"));
725     if (GNUNET_SCHEDULER_NO_TASK != end)
726       GNUNET_SCHEDULER_cancel (end);
727     ret = 1;
728     end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
729     return;
730   }
731 }
732
733
734 static void
735 testservice_task (void *cls,
736                   const struct GNUNET_SCHEDULER_TaskContext *tc)
737 {
738   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
739   int counter = 0;
740   ret = 1;
741
742   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
743   {
744       FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
745       return;
746   }
747
748   if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
749   {
750       FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
751       return;
752   }
753
754   counter = benchmark_send + benchmark_receive + iterate_connections +
755             monitor_connections + monitor_connects + try_connect;
756
757   if (1 < counter)
758   {
759     FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
760              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
761     return;
762   }
763   if (0 == counter)
764   {
765     FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
766              "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
767     return;
768   }
769
770   if (try_connect) /* -C: Connect to peer */
771   {
772     if (NULL == cpid)
773     {
774       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
775                "-C", "-p");
776       ret = 1;
777       return;
778     }
779     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
780                                        &notify_receive,
781                                        &notify_connect,
782                                        &notify_disconnect);
783     if (NULL == handle)
784     {
785         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
786         ret = 1;
787         return;
788     }
789     tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
790     if (NULL == tc_handle)
791     {
792         FPRINTF (stderr, "%s", _("Failed to send request to transport service\n"));
793         ret = 1;
794         return;
795     }
796     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
797                                                &operation_timeout, NULL);
798
799   }
800   else if (benchmark_send) /* -s: Benchmark sending */
801   {
802     if (NULL == cpid)
803     {
804       FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
805                "-s", "-p");
806       ret = 1;
807       return;
808     }
809     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
810                                        &notify_receive,
811                                        &notify_connect,
812                                        &notify_disconnect);
813     if (NULL == handle)
814     {
815         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
816         ret = 1;
817         return;
818     }
819     tc_handle =  GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
820     if (NULL == tc_handle)
821     {
822         FPRINTF (stderr, "%s", _("Failed to send request to transport service\n"));
823         ret = 1;
824         return;
825     }
826     start_time = GNUNET_TIME_absolute_get ();
827     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
828                                                &operation_timeout, NULL);
829   }
830   else if (benchmark_receive) /* -b: Benchmark receiving */
831   {
832     handle =
833         GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
834                                   NULL, NULL);
835     if (NULL == handle)
836     {
837         FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
838         ret = 1;
839         return;
840     }
841     if (verbosity > 0)
842       FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
843     start_time = GNUNET_TIME_absolute_get ();
844
845   }
846   else if (iterate_connections) /* -i: List all active addresses once */
847   {
848     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
849     address_resolution_in_progress = GNUNET_YES;
850     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
851                                                 (NULL == cpid) ? NULL : &pid,
852                                                 GNUNET_YES,
853                                                 TIMEOUT,
854                                                 &process_address, (void *) cfg);
855   }
856   else if (monitor_connections) /* -m: List all active addresses continously */
857   {
858     peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
859     address_resolution_in_progress = GNUNET_YES;
860     pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
861                                                 (NULL == cpid) ? NULL : &pid,
862                                                 GNUNET_NO,
863                                                 TIMEOUT,
864                                                 &process_address, (void *) cfg);
865   }
866   else if (monitor_connects) /* -e : Monitor (dis)connect events continously */
867   {
868     monitor_connect_counter = 0;
869     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
870                                        &monitor_notify_connect,
871                                        &monitor_notify_disconnect);
872     if (NULL == handle)
873     {
874       FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
875       ret = 1;
876       return;
877     }
878     ret = 0;
879   }
880   else
881   {
882     GNUNET_break (0);
883     return;
884   }
885
886   end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
887                                       &shutdown_task,
888                                       NULL);
889
890 }
891
892
893 /**
894  * Main function that will be run by the scheduler.
895  *
896  * @param cls closure
897  * @param args remaining command-line arguments
898  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
899  * @param cfg configuration
900  */
901 static void
902 run (void *cls, char *const *args, const char *cfgfile,
903      const struct GNUNET_CONFIGURATION_Handle *cfg)
904 {
905   if (test_configuration)
906   {
907     do_test_configuration (cfg);
908     return;
909   }
910
911   GNUNET_CLIENT_service_test ("transport", cfg,
912       GNUNET_TIME_UNIT_SECONDS,
913       &testservice_task,
914       (void *) cfg);
915 }
916
917
918 int
919 main (int argc, char *const *argv)
920 {
921   int res;
922   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
923     {'b', "benchmark", NULL,
924      gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
925      0, &GNUNET_GETOPT_set_one, &benchmark_receive},
926     {'C', "connect", NULL,
927      gettext_noop ("connect to a peer"),
928      0, &GNUNET_GETOPT_set_one, &try_connect},
929     {'i', "information", NULL,
930      gettext_noop ("provide information about all current connections (once)"),
931      0, &GNUNET_GETOPT_set_one, &iterate_connections},
932     {'m', "monitor", NULL,
933      gettext_noop ("provide information about all current connections (continuously)"),
934      0, &GNUNET_GETOPT_set_one, &monitor_connections},
935     {'e', "events", NULL,
936      gettext_noop ("provide information about all connects and disconnect events (continuously)"),
937      0, &GNUNET_GETOPT_set_one, &monitor_connects},
938     {'n', "numeric", NULL,
939      gettext_noop ("do not resolve hostnames"),
940      0, &GNUNET_GETOPT_set_one, &numeric},
941      {'p', "peer", "PEER",
942       gettext_noop ("peer identity"),
943       1, &GNUNET_GETOPT_set_string, &cpid},
944     {'s', "send", NULL,
945      gettext_noop
946      ("send data for benchmarking to the other peer (until CTRL-C)"),
947      0, &GNUNET_GETOPT_set_one, &benchmark_send},
948     {'t', "test", NULL,
949      gettext_noop ("test transport configuration (involves external server)"),
950      0, &GNUNET_GETOPT_set_one, &test_configuration},
951     GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
952     GNUNET_GETOPT_OPTION_END
953   };
954
955   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
956     return 2;
957
958   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
959                               gettext_noop
960                               ("Direct access to transport service."), options,
961                               &run, NULL);
962   GNUNET_free ((void *) argv);
963   if (GNUNET_OK == res)
964     return ret;
965   return 1;
966 }
967
968
969 /* end of gnunet-transport.c */