8624b09b42a2a3b2db06bc9e82322abe6aa955c4
[oweals/gnunet.git] / src / transport / gnunet-transport.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011-2014, 2016, 2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, 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 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_resolver_service.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_transport_core_service.h"
33
34 /**
35  * Timeout for a name resolution
36  */
37 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38
39 /**
40  * Timeout for an operation
41  */
42 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43
44
45 /**
46  * Context to store name resolutions for valiation
47  */
48 struct ValidationResolutionContext
49 {
50   /**
51    * Next in DLL
52    */
53   struct ValidationResolutionContext *next;
54
55   /**
56    * Previous in DLL
57    */
58   struct ValidationResolutionContext *prev;
59
60   /**
61    * Address to resolve
62    */
63   struct GNUNET_HELLO_Address *addrcp;
64
65   /**
66    * Time of last validation
67    */
68   struct GNUNET_TIME_Absolute last_validation;
69
70   /**
71    * Address is valid until
72    */
73   struct GNUNET_TIME_Absolute valid_until;
74
75   /**
76    * Time of next validation
77    */
78   struct GNUNET_TIME_Absolute next_validation;
79
80   /**
81    * Tranport conversion handle
82    */
83   struct GNUNET_TRANSPORT_AddressToStringContext *asc;
84
85   /**
86    * plugin name
87    */
88   char *transport;
89
90   /**
91    * was the entry printed
92    */
93   int printed;
94 };
95
96 /**
97  * Struct to store information about peers in monitor mode
98  */
99 struct MonitoredPeer
100 {
101   /**
102    * State of the peer
103    */
104   enum GNUNET_TRANSPORT_PeerState state;
105
106   /**
107    * Timeout
108    */
109   struct GNUNET_TIME_Absolute state_timeout;
110
111   /**
112    * The address to convert
113    */
114   struct GNUNET_HELLO_Address *address;
115 };
116
117 /**
118  * Context to store name resolutions for valiation
119  */
120 struct PeerResolutionContext
121 {
122   /**
123    * Next in DLL
124    */
125   struct PeerResolutionContext *next;
126
127   /**
128    * Prev in DLL
129    */
130   struct PeerResolutionContext *prev;
131
132   /**
133    * address to resolve
134    */
135   struct GNUNET_HELLO_Address *addrcp;
136
137   /**
138    * transport conversiion context
139    */
140   struct GNUNET_TRANSPORT_AddressToStringContext *asc;
141
142   /**
143    * peer state
144    */
145   enum GNUNET_TRANSPORT_PeerState state;
146
147   /**
148    * state timeout
149    */
150   struct GNUNET_TIME_Absolute state_timeout;
151
152   /**
153    * transport plugin
154    */
155   char *transport;
156
157   /**
158    * was the entry printed
159    */
160   int printed;
161 };
162
163
164 /**
165  * Benchmarking block size in KB
166  */
167 #define BLOCKSIZE 4
168
169 /**
170  * Handle to transport service.
171  */
172 static struct GNUNET_TRANSPORT_CoreHandle *handle;
173
174 /**
175  * Configuration handle
176  */
177 static struct GNUNET_CONFIGURATION_Handle *cfg;
178
179 /**
180  * Blacklisting handle
181  */
182 struct GNUNET_TRANSPORT_Blacklist *blacklist;
183
184 /**
185  * Option -s.
186  */
187 static int benchmark_send;
188
189 /**
190  * Option -b.
191  */
192 static int benchmark_receive;
193
194 /**
195  * Option -l.
196  */
197 static int benchmark_receive;
198
199 /**
200  * Option -i.
201  */
202 static int iterate_connections;
203
204 /**
205  * Option -a.
206  */
207 static int iterate_all;
208
209 /**
210  * Option -c.
211  */
212 static int monitor_connects;
213
214 /**
215  * Option -m.
216  */
217 static int monitor_connections;
218
219 /**
220  * Option -P.
221  */
222 static int monitor_plugins;
223
224 /**
225  * Option -D.
226  */
227 static int do_disconnect;
228
229 /**
230  * Option -n.
231  */
232 static int numeric;
233
234 /**
235  * Global return value (0 success).
236  */
237 static int ret;
238
239 /**
240  * Current number of connections in monitor mode
241  */
242 static int monitor_connect_counter;
243
244 /**
245  * Number of bytes of traffic we received so far.
246  */
247 static unsigned long long traffic_received;
248
249 /**
250  * Number of bytes of traffic we sent so far.
251  */
252 static unsigned long long traffic_sent;
253
254 /**
255  * Starting time of transmitting/receiving data.
256  */
257 static struct GNUNET_TIME_Absolute start_time;
258
259 /**
260  * Map storing information about monitored peers
261  */
262 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
263
264 /**
265  * Map storing information about monitored plugins's sessions.
266  */
267 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_plugins;
268
269 /**
270  * Handle if we are monitoring peers at the transport level.
271  */
272 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
273
274 /**
275  * Handle if we are monitoring plugin session activity.
276  */
277 static struct GNUNET_TRANSPORT_PluginMonitor *pm;
278
279 /**
280  * Identity of the peer we transmit to / connect to.
281  * ('-p' command-line option).
282  */
283 static struct GNUNET_PeerIdentity pid;
284
285 /**
286  * Task for operation timeout
287  */
288 static struct GNUNET_SCHEDULER_Task *op_timeout;
289
290 /**
291  * Selected level of verbosity.
292  */
293 static unsigned int verbosity;
294
295 /**
296  * Resolver process handle.
297  */
298 struct GNUNET_OS_Process *resolver;
299
300 /**
301  * Number of address resolutions pending
302  */
303 static unsigned int address_resolutions;
304
305 /**
306  * DLL: head of validation resolution entries
307  */
308 static struct ValidationResolutionContext *vc_head;
309
310 /**
311  * DLL: tail of validation resolution entries
312  */
313 static struct ValidationResolutionContext *vc_tail;
314
315 /**
316  * DLL: head of resolution entries
317  */
318 static struct PeerResolutionContext *rc_head;
319
320 /**
321  * DLL: head of resolution entries
322  */
323 static struct PeerResolutionContext *rc_tail;
324
325
326 /**
327  * Function called to release data stored in the #monitored_peers map.
328  *
329  * @param cls unused
330  * @param key the peer identity
331  * @param value a `struct MonitoredPeer` to release
332  * @return #GNUNET_OK (continue to iterate)
333  */
334 static int
335 destroy_it (void *cls,
336             const struct GNUNET_PeerIdentity *key,
337             void *value)
338 {
339   struct MonitoredPeer *m = value;
340
341   GNUNET_assert (GNUNET_OK ==
342                  GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
343                                                        key,
344                                                        value));
345   GNUNET_free_non_null (m->address);
346   GNUNET_free (value);
347   return GNUNET_OK;
348 }
349
350
351 /**
352  * Task run in monitor mode when the user presses CTRL-C to abort.
353  * Stops monitoring activity.
354  *
355  * @param cls NULL
356  */
357 static void
358 shutdown_task (void *cls)
359 {
360   struct GNUNET_TIME_Relative duration;
361   struct ValidationResolutionContext *cur;
362   struct ValidationResolutionContext *next;
363   struct PeerResolutionContext *rc;
364
365   if (NULL != op_timeout)
366   {
367     GNUNET_SCHEDULER_cancel (op_timeout);
368     op_timeout = NULL;
369   }
370   if (NULL != pic)
371   {
372     GNUNET_TRANSPORT_monitor_peers_cancel (pic);
373     pic = NULL;
374   }
375   if (NULL != pm)
376   {
377     GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
378     pm = NULL;
379   }
380
381   next = vc_head;
382   for (cur = next; NULL != cur; cur = next)
383   {
384     next = cur->next;
385
386     GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
387     GNUNET_CONTAINER_DLL_remove (vc_head,
388                                  vc_tail,
389                                  cur);
390     GNUNET_free (cur->transport);
391     GNUNET_HELLO_address_free (cur->addrcp);
392     GNUNET_free (cur);
393   }
394   while (NULL != (rc = rc_head))
395   {
396     GNUNET_CONTAINER_DLL_remove (rc_head,
397                                  rc_tail,
398                                  rc);
399     GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
400     GNUNET_free (rc->transport);
401     GNUNET_free (rc->addrcp);
402     GNUNET_free (rc);
403   }
404   if (NULL != handle)
405   {
406     GNUNET_TRANSPORT_core_disconnect (handle);
407     handle = NULL;
408   }
409   if (benchmark_send)
410   {
411     duration = GNUNET_TIME_absolute_get_duration (start_time);
412     FPRINTF (stdout,
413              _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
414              1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
415              traffic_sent,
416              GNUNET_STRINGS_relative_time_to_string (duration,
417                                                      GNUNET_YES));
418   }
419   if (benchmark_receive)
420   {
421     duration = GNUNET_TIME_absolute_get_duration (start_time);
422     FPRINTF (stdout,
423              _("Received %llu bytes/s (%llu bytes in %s)\n"),
424              1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
425              traffic_received,
426              GNUNET_STRINGS_relative_time_to_string (duration,
427                                                      GNUNET_YES));
428   }
429
430   if (NULL != monitored_peers)
431   {
432     GNUNET_CONTAINER_multipeermap_iterate (monitored_peers,
433                                            &destroy_it,
434                                            NULL);
435     GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
436     monitored_peers = NULL;
437   }
438   if (NULL != monitored_plugins)
439   {
440     GNUNET_break (0 ==
441                   GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
442     GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
443     monitored_plugins = NULL;
444   }
445   if (NULL != blacklist)
446   {
447     GNUNET_TRANSPORT_blacklist_cancel (blacklist);
448     blacklist = NULL;
449     ret = 0;
450   }
451 }
452
453
454 /**
455  * We are done, shut down.
456  */
457 static void
458 operation_timeout (void *cls)
459 {
460   struct PeerResolutionContext *cur;
461   struct PeerResolutionContext *next;
462
463   op_timeout = NULL;
464   if ((benchmark_send) || (benchmark_receive))
465   {
466     FPRINTF (stdout,
467              _("Failed to connect to `%s'\n"),
468              GNUNET_i2s_full (&pid));
469     GNUNET_SCHEDULER_shutdown ();
470     ret = 1;
471     return;
472   }
473   if (iterate_connections)
474   {
475     next = rc_head;
476     while (NULL != (cur = next))
477     {
478       next = cur->next;
479       FPRINTF (stdout,
480                _("Failed to resolve address for peer `%s'\n"),
481                GNUNET_i2s (&cur->addrcp->peer));
482
483       GNUNET_CONTAINER_DLL_remove(rc_head,
484                                   rc_tail,
485                                   cur);
486       GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
487       GNUNET_free (cur->transport);
488       GNUNET_free (cur->addrcp);
489       GNUNET_free (cur);
490
491     }
492     FPRINTF (stdout,
493              "%s",
494              _("Failed to list connections, timeout occured\n"));
495     GNUNET_SCHEDULER_shutdown ();
496     ret = 1;
497     return;
498   }
499 }
500
501
502 /**
503  * Function called to notify a client about the socket
504  * begin ready to queue more data.  Sends another message.
505  *
506  * @param cls closure with the message queue
507  */
508 static void
509 do_send (void *cls)
510 {
511   struct GNUNET_MQ_Handle *mq = cls;
512   struct GNUNET_MessageHeader *m;
513   struct GNUNET_MQ_Envelope *env;
514
515   env = GNUNET_MQ_msg_extra (m,
516                              BLOCKSIZE * 1024,
517                               GNUNET_MESSAGE_TYPE_DUMMY);
518   memset (&m[1],
519           52,
520           BLOCKSIZE * 1024 - sizeof(struct GNUNET_MessageHeader));
521   traffic_sent += BLOCKSIZE * 1024;
522   GNUNET_MQ_notify_sent (env,
523                          &do_send,
524                          mq);
525   if (verbosity > 0)
526     FPRINTF (stdout,
527              _("Transmitting %u bytes\n"),
528              (unsigned int) BLOCKSIZE * 1024);
529   GNUNET_MQ_send (mq,
530                   env);
531 }
532
533
534 /**
535  * Function called to notify transport users that another
536  * peer connected to us.
537  *
538  * @param cls closure
539  * @param peer the peer that connected
540  * @param mq message queue for sending to @a peer
541  */
542 static void *
543 notify_connect (void *cls,
544                 const struct GNUNET_PeerIdentity *peer,
545                 struct GNUNET_MQ_Handle *mq)
546 {
547   if (0 != memcmp (&pid,
548                    peer,
549                    sizeof(struct GNUNET_PeerIdentity)))
550     return NULL;
551   ret = 0;
552   if (! benchmark_send)
553     return NULL;
554   if (NULL != op_timeout)
555   {
556     GNUNET_SCHEDULER_cancel (op_timeout);
557     op_timeout = NULL;
558   }
559   if (verbosity > 0)
560     FPRINTF (stdout,
561              _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
562              GNUNET_i2s (peer),
563              BLOCKSIZE);
564   start_time = GNUNET_TIME_absolute_get ();
565   do_send (mq);
566   return mq;
567 }
568
569
570 /**
571  * Function called to notify transport users that another
572  * peer disconnected from us.
573  *
574  * @param cls closure
575  * @param peer the peer that disconnected
576  * @param internal_cls what we returned from #notify_connect()
577  */
578 static void
579 notify_disconnect (void *cls,
580                    const struct GNUNET_PeerIdentity *peer,
581                    void *internal_cls)
582 {
583   if (0 != memcmp (&pid,
584                    peer,
585                    sizeof(struct GNUNET_PeerIdentity)))
586     return;
587   if (NULL == internal_cls)
588     return; /* not about target peer */
589   if (! benchmark_send)
590     return; /* not transmitting */
591   FPRINTF (stdout,
592            _("Disconnected from peer `%s' while benchmarking\n"),
593            GNUNET_i2s (&pid));
594 }
595
596
597 /**
598  * Function called to notify transport users that another
599  * peer connected to us.
600  *
601  * @param cls closure
602  * @param peer the peer that connected
603  * @param mq for sending messages to @a peer
604  * @return NULL
605  */
606 static void *
607 monitor_notify_connect (void *cls,
608                         const struct GNUNET_PeerIdentity *peer,
609                         struct GNUNET_MQ_Handle *mq)
610 {
611   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
612   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
613
614   monitor_connect_counter++;
615   FPRINTF (stdout,
616            _("%24s: %-17s %4s   (%u connections in total)\n"),
617            now_str,
618            _("Connected to"),
619            GNUNET_i2s (peer),
620            monitor_connect_counter);
621   return NULL;
622 }
623
624
625 /**
626  * Function called to notify transport users that another
627  * peer disconnected from us.
628  *
629  * @param cls closure
630  * @param peer the peer that disconnected
631  * @param internal_cls what we returned from #monitor_notify_connect()
632  */
633 static void
634 monitor_notify_disconnect (void *cls,
635                            const struct GNUNET_PeerIdentity *peer,
636                            void *internal_cls)
637 {
638   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
639   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
640
641   GNUNET_assert(monitor_connect_counter > 0);
642   monitor_connect_counter--;
643
644   FPRINTF (stdout,
645            _("%24s: %-17s %4s   (%u connections in total)\n"),
646            now_str,
647            _("Disconnected from"),
648            GNUNET_i2s (peer),
649            monitor_connect_counter);
650 }
651
652
653 /**
654  * Function called by the transport for each received message.
655  *
656  * @param cls closure
657  * @param message the message
658  * @return #GNUNET_OK
659  */
660 static int
661 check_dummy (void *cls,
662              const struct GNUNET_MessageHeader *message)
663 {
664   return GNUNET_OK; /* all messages are fine */
665 }
666
667
668 /**
669  * Function called by the transport for each received message.
670  *
671  * @param cls closure
672  * @param message the message
673  */
674 static void
675 handle_dummy (void *cls,
676               const struct GNUNET_MessageHeader *message)
677 {
678   if (! benchmark_receive)
679     return;
680   if (verbosity > 0)
681     FPRINTF (stdout,
682              _("Received %u bytes\n"),
683              (unsigned int) ntohs (message->size));
684   if (0 == traffic_received)
685     start_time = GNUNET_TIME_absolute_get ();
686   traffic_received += ntohs (message->size);
687 }
688
689
690 /**
691  * Convert address to a printable format.
692  *
693  * @param address the address
694  * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
695  *                to try to use reverse DNS
696  * @param state state the peer is in
697  * @param state_timeout when will the peer's state expire
698  */
699 static void
700 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
701                       int numeric,
702                       enum GNUNET_TRANSPORT_PeerState state,
703                       struct GNUNET_TIME_Absolute state_timeout);
704
705
706 static void
707 print_info (const struct GNUNET_PeerIdentity *id,
708             const char *transport,
709             const char *addr,
710             enum GNUNET_TRANSPORT_PeerState state,
711             struct GNUNET_TIME_Absolute state_timeout)
712 {
713
714   if ( ((GNUNET_YES == iterate_connections) &&
715         (GNUNET_YES == iterate_all)) ||
716        (GNUNET_YES == monitor_connections))
717   {
718     FPRINTF (stdout,
719              _("Peer `%s': %s %s in state `%s' until %s\n"),
720              GNUNET_i2s (id),
721              (NULL == transport) ? "<none>" : transport,
722              (NULL == transport) ? "<none>" : addr,
723              GNUNET_TRANSPORT_ps2s (state),
724              GNUNET_STRINGS_absolute_time_to_string (state_timeout));
725   }
726   else if ( (GNUNET_YES == iterate_connections) &&
727             (GNUNET_TRANSPORT_is_connected(state)) )
728   {
729     /* Only connected peers, skip state */
730     FPRINTF (stdout,
731              _("Peer `%s': %s %s\n"),
732              GNUNET_i2s (id),
733              transport,
734              addr);
735   }
736 }
737
738
739 /**
740  * Function called with a textual representation of an address.  This
741  * function will be called several times with different possible
742  * textual representations, and a last time with @a address being NULL
743  * to signal the end of the iteration.  Note that @a address NULL
744  * always is the last call, regardless of the value in @a res.
745  *
746  * @param cls closure
747  * @param address NULL on end of iteration,
748  *        otherwise 0-terminated printable UTF-8 string,
749  *        in particular an empty string if @a res is #GNUNET_NO
750  * @param res result of the address to string conversion:
751  *        if #GNUNET_OK: conversion successful
752  *        if #GNUNET_NO: address was invalid (or not supported)
753  *        if #GNUNET_SYSERR: communication error (IPC error)
754  */
755 static void
756 process_peer_string (void *cls,
757                      const char *address,
758                      int res)
759 {
760   struct PeerResolutionContext *rc = cls;
761
762   if (NULL != address)
763   {
764     if (GNUNET_SYSERR == res)
765     {
766       FPRINTF (stderr,
767                "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
768                GNUNET_i2s (&rc->addrcp->peer),
769                rc->addrcp->transport_name,
770                (unsigned int) rc->addrcp->address_length);
771       print_info (&rc->addrcp->peer,
772                   rc->transport,
773                   NULL,
774                   rc->state,
775                   rc->state_timeout);
776       rc->printed = GNUNET_YES;
777       return;
778     }
779     if (GNUNET_OK == res)
780     {
781       print_info (&rc->addrcp->peer,
782                   rc->transport,
783                   address,
784                   rc->state,
785                   rc->state_timeout);
786       rc->printed = GNUNET_YES;
787       return; /* Wait for done call */
788     }
789     /* GNUNET_NO == res: ignore, was simply not supported */
790     return;
791   }
792   /* NULL == address, last call, we are done */
793
794   rc->asc = NULL;
795   GNUNET_assert (address_resolutions > 0);
796   address_resolutions--;
797   if (GNUNET_NO == rc->printed)
798   {
799     if (numeric == GNUNET_NO)
800     {
801       /* Failed to resolve address, try numeric lookup
802          (note: this should not be needed, as transport
803          should fallback to numeric conversion if DNS takes
804          too long) */
805       resolve_peer_address (rc->addrcp,
806                             GNUNET_YES,
807                             rc->state,
808                             rc->state_timeout);
809     }
810     else
811     {
812       print_info (&rc->addrcp->peer,
813                   rc->transport,
814                   NULL,
815                   rc->state,
816                   rc->state_timeout);
817     }
818   }
819   GNUNET_free (rc->transport);
820   GNUNET_free (rc->addrcp);
821   GNUNET_CONTAINER_DLL_remove (rc_head,
822                                rc_tail,
823                                rc);
824   GNUNET_free (rc);
825   if ((0 == address_resolutions) && (iterate_connections))
826   {
827     if (NULL != op_timeout)
828     {
829       GNUNET_SCHEDULER_cancel (op_timeout);
830       op_timeout = NULL;
831     }
832     ret = 0;
833     GNUNET_SCHEDULER_shutdown ();
834   }
835 }
836
837
838 /**
839  * Convert address to a printable format and print it
840  * together with the given state data.
841  *
842  * @param address the address
843  * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
844  *                to try to use reverse DNS
845  * @param state state the peer is in
846  * @param state_timeout when will the peer's state expire
847  */
848 static void
849 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
850                       int numeric,
851                       enum GNUNET_TRANSPORT_PeerState state,
852                       struct GNUNET_TIME_Absolute state_timeout)
853 {
854   struct PeerResolutionContext *rc;
855
856   rc = GNUNET_new (struct PeerResolutionContext);
857   GNUNET_CONTAINER_DLL_insert (rc_head,
858                                rc_tail,
859                                rc);
860   address_resolutions++;
861   rc->transport = GNUNET_strdup (address->transport_name);
862   rc->addrcp = GNUNET_HELLO_address_copy (address);
863   rc->printed = GNUNET_NO;
864   rc->state = state;
865   rc->state_timeout = state_timeout;
866   /* Resolve address to string */
867   rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
868                                                 address,
869                                                 numeric,
870                                                 RESOLUTION_TIMEOUT,
871                                                 &process_peer_string,
872                                                 rc);
873 }
874
875
876 /**
877  * Function called with information about a peers during a one shot iteration
878  *
879  * @param cls closure
880  * @param peer identity of the peer, NULL for final callback when operation done
881  * @param address binary address used to communicate with this peer,
882  *  NULL on disconnect or when done
883  * @param state current state this peer is in
884  * @param state_timeout time out for the current state
885  */
886 static void
887 process_peer_iteration_cb (void *cls,
888                            const struct GNUNET_PeerIdentity *peer,
889                            const struct GNUNET_HELLO_Address *address,
890                            enum GNUNET_TRANSPORT_PeerState state,
891                            struct GNUNET_TIME_Absolute state_timeout)
892 {
893   if (NULL == peer)
894   {
895     /* done */
896     pic = NULL;
897     return;
898   }
899
900   if ( (GNUNET_NO == iterate_all) &&
901        (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
902       return; /* Display only connected peers */
903
904   if (NULL != op_timeout)
905     GNUNET_SCHEDULER_cancel (op_timeout);
906   op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
907                                              &operation_timeout,
908                                              NULL);
909
910   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911               "Received address for peer `%s': %s\n",
912               GNUNET_i2s (peer),
913               address ? address->transport_name : "");
914
915   if (NULL != address)
916     resolve_peer_address (address,
917                           numeric,
918                           state,
919                           state_timeout);
920   else
921     print_info (peer,
922                 NULL,
923                 NULL,
924                 state,
925                 state_timeout);
926 }
927
928
929 /**
930  * Context for address resolution by #plugin_monitoring_cb().
931  */
932 struct PluginMonitorAddress
933 {
934
935   /**
936    * Ongoing resolution request.
937    */
938   struct GNUNET_TRANSPORT_AddressToStringContext *asc;
939
940   /**
941    * Resolved address as string.
942    */
943   char *str;
944
945   /**
946    * Last event we got and did not yet print because
947    * @e str was NULL (address not yet resolved).
948    */
949   struct GNUNET_TRANSPORT_SessionInfo si;
950 };
951
952
953 /**
954  * Print information about a plugin monitoring event.
955  *
956  * @param addr out internal context
957  * @param info the monitoring information
958  */
959 static void
960 print_plugin_event_info (struct PluginMonitorAddress *addr,
961                          const struct GNUNET_TRANSPORT_SessionInfo *info)
962 {
963   const char *state;
964
965   switch (info->state)
966   {
967   case GNUNET_TRANSPORT_SS_INIT:
968     state = "INIT";
969     break;
970   case GNUNET_TRANSPORT_SS_HANDSHAKE:
971     state = "HANDSHAKE";
972     break;
973   case GNUNET_TRANSPORT_SS_UP:
974     state = "UP";
975     break;
976   case GNUNET_TRANSPORT_SS_UPDATE:
977     state = "UPDATE";
978     break;
979   case GNUNET_TRANSPORT_SS_DONE:
980     state = "DONE";
981     break;
982   default:
983     state = "UNKNOWN";
984     break;
985   }
986   fprintf (stdout,
987            "%s: state %s timeout in %s @ %s%s\n",
988            GNUNET_i2s (&info->address->peer),
989            state,
990            GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
991                                                    GNUNET_YES),
992            addr->str,
993            (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
994   fprintf (stdout,
995            "%s: queue has %3u messages and %6u bytes\n",
996            GNUNET_i2s (&info->address->peer),
997            info->num_msg_pending,
998            info->num_bytes_pending);
999   if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1000     fprintf (stdout,
1001              "%s: receiving blocked until %s\n",
1002              GNUNET_i2s (&info->address->peer),
1003              GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1004 }
1005
1006
1007 /**
1008  * Function called with a textual representation of an address.  This
1009  * function will be called several times with different possible
1010  * textual representations, and a last time with @a address being NULL
1011  * to signal the end of the iteration.  Note that @a address NULL
1012  * always is the last call, regardless of the value in @a res.
1013  *
1014  * @param cls closure
1015  * @param address NULL on end of iteration,
1016  *        otherwise 0-terminated printable UTF-8 string,
1017  *        in particular an empty string if @a res is #GNUNET_NO
1018  * @param res result of the address to string conversion:
1019  *        if #GNUNET_OK: conversion successful
1020  *        if #GNUNET_NO: address was invalid (or not supported)
1021  *        if #GNUNET_SYSERR: communication error (IPC error)
1022  */
1023 static void
1024 address_cb (void *cls,
1025             const char *address,
1026             int res)
1027 {
1028   struct PluginMonitorAddress *addr = cls;
1029
1030   if (NULL == address)
1031   {
1032     addr->asc = NULL;
1033     return;
1034   }
1035   if (NULL != addr->str)
1036     return;
1037   addr->str = GNUNET_strdup (address);
1038   print_plugin_event_info (addr,
1039                            &addr->si);
1040 }
1041
1042
1043 /**
1044  * Function called by the plugin with information about the
1045  * current sessions managed by the plugin (for monitoring).
1046  *
1047  * @param cls closure (NULL)
1048  * @param session session handle this information is about,
1049  *        NULL to indicate that we are "in sync" (initial
1050  *        iteration complete)
1051  * @param session_ctx storage location where the application
1052  *        can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1053  *        and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1054  * @param info information about the state of the session,
1055  *        NULL if @a session is also NULL and we are
1056  *        merely signalling that the initial iteration is over;
1057  *        NULL with @a session being non-NULL if the monitor
1058  *        was being cancelled while sessions were active
1059  */
1060 static void
1061 plugin_monitoring_cb (void *cls,
1062                       struct GNUNET_TRANSPORT_PluginSession *session,
1063                       void **session_ctx,
1064                       const struct GNUNET_TRANSPORT_SessionInfo *info)
1065 {
1066   struct PluginMonitorAddress *addr;
1067
1068   if ( (NULL == info) &&
1069        (NULL == session) )
1070     return; /* in sync with transport service */
1071   addr = *session_ctx;
1072   if (NULL == info)
1073   {
1074     if (NULL != addr)
1075     {
1076       if (NULL != addr->asc)
1077       {
1078         GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1079         addr->asc = NULL;
1080       }
1081       GNUNET_free_non_null (addr->str);
1082       GNUNET_free (addr);
1083       *session_ctx = NULL;
1084     }
1085     return; /* shutdown */
1086   }
1087   if (0 != memcmp (&info->address->peer,
1088                    &pid,
1089                    sizeof (struct GNUNET_PeerIdentity)))
1090     return; /* filtered */
1091   if (NULL == addr)
1092   {
1093     addr = GNUNET_new (struct PluginMonitorAddress);
1094     addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1095                                                     info->address,
1096                                                     numeric,
1097                                                     GNUNET_TIME_UNIT_FOREVER_REL,
1098                                                     &address_cb,
1099                                                     addr);
1100     *session_ctx = addr;
1101   }
1102   if (NULL == addr->str)
1103     addr->si = *info;
1104   else
1105     print_plugin_event_info (addr,
1106                              info);
1107   if (GNUNET_TRANSPORT_SS_DONE == info->state)
1108   {
1109     if (NULL != addr->asc)
1110     {
1111       GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1112       addr->asc = NULL;
1113     }
1114     GNUNET_free_non_null (addr->str);
1115     GNUNET_free (addr);
1116     *session_ctx = NULL;
1117   }
1118 }
1119
1120
1121 /**
1122  * Function called with information about a peers
1123  *
1124  * @param cls closure, NULL
1125  * @param peer identity of the peer, NULL for final callback when operation done
1126  * @param address binary address used to communicate with this peer,
1127  *  NULL on disconnect or when done
1128  * @param state current state this peer is in
1129  * @param state_timeout time out for the current state
1130  */
1131 static void
1132 process_peer_monitoring_cb (void *cls,
1133                             const struct GNUNET_PeerIdentity *peer,
1134                             const struct GNUNET_HELLO_Address *address,
1135                             enum GNUNET_TRANSPORT_PeerState state,
1136                             struct GNUNET_TIME_Absolute state_timeout)
1137 {
1138   struct MonitoredPeer *m;
1139
1140   if (NULL == peer)
1141   {
1142     FPRINTF (stdout,
1143              "%s",
1144              _("Monitor disconnected from transport service. Reconnecting.\n"));
1145     return;
1146   }
1147
1148   if (NULL != op_timeout)
1149     GNUNET_SCHEDULER_cancel (op_timeout);
1150   op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1151                                              &operation_timeout,
1152                                              NULL);
1153
1154   if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1155                                                       peer)))
1156   {
1157     m = GNUNET_new (struct MonitoredPeer);
1158     GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1159                                        peer,
1160                                        m,
1161                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1162   }
1163   else
1164   {
1165     if ( (m->state == state) &&
1166          (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1167          (NULL == address) &&
1168          (NULL == m->address) )
1169     {
1170       return; /* No real change */
1171     }
1172     if ( (m->state == state) &&
1173          (NULL != address) &&
1174          (NULL != m->address) &&
1175          (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1176       return; /* No real change */
1177   }
1178
1179   if (NULL != m->address)
1180   {
1181     GNUNET_free (m->address);
1182     m->address = NULL;
1183   }
1184   if (NULL != address)
1185     m->address = GNUNET_HELLO_address_copy (address);
1186   m->state = state;
1187   m->state_timeout = state_timeout;
1188
1189   if (NULL != address)
1190     resolve_peer_address (m->address,
1191                           numeric,
1192                           m->state,
1193                           m->state_timeout);
1194   else
1195     print_info (peer,
1196                 NULL,
1197                 NULL,
1198                 m->state,
1199                 m->state_timeout);
1200 }
1201
1202
1203 /**
1204  * Function called with the transport service checking if we
1205  * want to blacklist a peer. Return #GNUNET_SYSERR for the
1206  * peer that we should disconnect from.
1207  *
1208  * @param cls NULL
1209  * @param cpid peer to check blacklisting for
1210  * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1211  */
1212 static int
1213 blacklist_cb (void *cls,
1214               const struct GNUNET_PeerIdentity *cpid)
1215 {
1216   if (0 == memcmp (cpid,
1217                    &pid,
1218                    sizeof (struct GNUNET_PeerIdentity)))
1219     return GNUNET_SYSERR;
1220   return GNUNET_OK;
1221 }
1222
1223
1224 /**
1225  * Main function that will be run by the scheduler.
1226  *
1227  * @param cls closure
1228  * @param args remaining command-line arguments
1229  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1230  * @param mycfg configuration
1231  */
1232 static void
1233 run (void *cls,
1234      char * const *args,
1235      const char *cfgfile,
1236      const struct GNUNET_CONFIGURATION_Handle *mycfg)
1237 {
1238   static struct GNUNET_PeerIdentity zero_pid;
1239   int counter = 0;
1240   ret = 1;
1241
1242   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1243
1244   counter = benchmark_send + benchmark_receive + iterate_connections
1245       + monitor_connections + monitor_connects + do_disconnect +
1246       monitor_plugins;
1247
1248   if (1 < counter)
1249   {
1250     FPRINTF (stderr,
1251              _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1252              "disconnect",
1253              "benchmark send",
1254              "benchmark receive",
1255              "information",
1256              "monitor",
1257              "events",
1258              "plugins");
1259     return;
1260   }
1261   if (0 == counter)
1262   {
1263     FPRINTF (stderr,
1264              _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1265              "disconnect",
1266              "benchmark send",
1267              "benchmark receive",
1268              "information",
1269              "monitor",
1270              "events",
1271              "plugins");
1272     return;
1273   }
1274
1275   if (do_disconnect) /* -D: Disconnect from peer */
1276   {
1277     if (0 == memcmp (&zero_pid,
1278                      &pid,
1279                      sizeof (pid)))
1280     {
1281       FPRINTF (stderr,
1282                _("Option `%s' makes no sense without option `%s'.\n"),
1283                "-D", "-p");
1284       ret = 1;
1285       return;
1286     }
1287     blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1288                                             &blacklist_cb,
1289                                             NULL);
1290     if (NULL == blacklist)
1291     {
1292       FPRINTF (stderr,
1293                "%s",
1294                _("Failed to connect to transport service for disconnection\n"));
1295       ret = 1;
1296       return;
1297     }
1298     FPRINTF (stdout,
1299              "%s",
1300              _("Blacklisting request in place, stop with CTRL-C\n"));
1301   }
1302   else if (benchmark_send) /* -s: Benchmark sending */
1303   {
1304     if (0 == memcmp (&zero_pid,
1305                      &pid,
1306                      sizeof (pid)))
1307     {
1308       FPRINTF (stderr,
1309                _("Option `%s' makes no sense without option `%s'.\n"),
1310                "-s", "-p");
1311       ret = 1;
1312       return;
1313     }
1314     handle = GNUNET_TRANSPORT_core_connect (cfg,
1315                                             NULL,
1316                                             NULL,
1317                                             NULL,
1318                                             &notify_connect,
1319                                             &notify_disconnect,
1320                                             NULL);
1321     if (NULL == handle)
1322     {
1323       FPRINTF (stderr,
1324                "%s",
1325                _("Failed to connect to transport service\n"));
1326       ret = 1;
1327       return;
1328     }
1329     start_time = GNUNET_TIME_absolute_get ();
1330     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1331                                                &operation_timeout,
1332                                                NULL);
1333   }
1334   else if (benchmark_receive) /* -b: Benchmark receiving */
1335   {
1336     struct GNUNET_MQ_MessageHandler handlers[] = {
1337       GNUNET_MQ_hd_var_size (dummy,
1338                              GNUNET_MESSAGE_TYPE_DUMMY,
1339                              struct GNUNET_MessageHeader,
1340                              NULL),
1341       GNUNET_MQ_handler_end ()
1342     };
1343
1344     handle = GNUNET_TRANSPORT_core_connect (cfg,
1345                                             NULL,
1346                                             handlers,
1347                                             NULL,
1348                                             NULL,
1349                                             NULL,
1350                                             NULL);
1351     if (NULL == handle)
1352     {
1353       FPRINTF (stderr,
1354                "%s",
1355                _("Failed to connect to transport service\n"));
1356       ret = 1;
1357       return;
1358     }
1359     if (verbosity > 0)
1360       FPRINTF (stdout,
1361                "%s",
1362                _("Starting to receive benchmark data\n"));
1363     start_time = GNUNET_TIME_absolute_get ();
1364
1365   }
1366   else if (iterate_connections) /* -i: List information about peers once */
1367   {
1368     pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1369                                           &pid,
1370                                           GNUNET_YES,
1371                                           &process_peer_iteration_cb,
1372                                           (void *) cfg);
1373     op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1374                                                &operation_timeout,
1375                                                NULL);
1376   }
1377   else if (monitor_connections) /* -m: List information about peers continuously */
1378   {
1379     monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1380                                                             GNUNET_NO);
1381     pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1382                                           &pid,
1383                                           GNUNET_NO,
1384                                           &process_peer_monitoring_cb,
1385                                           NULL);
1386   }
1387   else if (monitor_plugins) /* -P: List information about plugins continuously */
1388   {
1389     monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1390     pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1391                                            &plugin_monitoring_cb,
1392                                            NULL);
1393   }
1394   else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1395   {
1396     monitor_connect_counter = 0;
1397     handle = GNUNET_TRANSPORT_core_connect (cfg,
1398                                             NULL,
1399                                             NULL,
1400                                             NULL,
1401                                             &monitor_notify_connect,
1402                                             &monitor_notify_disconnect,
1403                                             NULL);
1404     if (NULL == handle)
1405     {
1406       FPRINTF (stderr,
1407                "%s",
1408                _("Failed to connect to transport service\n"));
1409       ret = 1;
1410       return;
1411     }
1412     ret = 0;
1413   }
1414   else
1415   {
1416     GNUNET_break(0);
1417     return;
1418   }
1419
1420   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1421                                  NULL);
1422 }
1423
1424
1425 int
1426 main (int argc,
1427       char * const *argv)
1428 {
1429   int res;
1430   struct GNUNET_GETOPT_CommandLineOption options[] = {
1431     GNUNET_GETOPT_OPTION_SET_ONE ('a',
1432                                   "all",
1433                                   gettext_noop ("print information for all peers (instead of only connected peers)"),
1434                                   &iterate_all),
1435     GNUNET_GETOPT_OPTION_SET_ONE ('b',
1436                                   "benchmark",
1437                                   gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1438                                   &benchmark_receive),
1439     GNUNET_GETOPT_OPTION_SET_ONE ('D',
1440                                   "disconnect",
1441                                   gettext_noop ("disconnect from a peer"),
1442                                   &do_disconnect),
1443     GNUNET_GETOPT_OPTION_SET_ONE ('i',
1444                                   "information",
1445                                   gettext_noop ("provide information about all current connections (once)"),
1446                                   &iterate_connections),
1447     GNUNET_GETOPT_OPTION_SET_ONE ('m',
1448                                   "monitor",
1449                                   gettext_noop ("provide information about all current connections (continuously)"),
1450                                   &monitor_connections),
1451     GNUNET_GETOPT_OPTION_SET_ONE ('e',
1452                                   "events",
1453                                   gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1454                                   &monitor_connects),
1455     GNUNET_GETOPT_OPTION_SET_ONE ('n',
1456                                   "numeric",
1457                                   gettext_noop ("do not resolve hostnames"),
1458                                   &numeric),
1459     GNUNET_GETOPT_OPTION_SET_BASE32_AUTO ('p',
1460                                           "peer",
1461                                           "PEER",
1462                                           gettext_noop ("peer identity"),
1463                                           &pid),
1464     GNUNET_GETOPT_OPTION_SET_ONE ('P',
1465                                   "plugins",
1466                                   gettext_noop ("monitor plugin sessions"),
1467                                   &monitor_plugins),
1468     GNUNET_GETOPT_OPTION_SET_ONE ('s',
1469                                   "send",
1470                                   gettext_noop
1471       ("send data for benchmarking to the other peer (until CTRL-C)"),
1472                                   &benchmark_send),
1473     GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1474     GNUNET_GETOPT_OPTION_END
1475   };
1476
1477   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1478     return 2;
1479
1480   res = GNUNET_PROGRAM_run (argc, argv,
1481                             "gnunet-transport",
1482                             gettext_noop ("Direct access to transport service."),
1483                             options,
1484                             &run, NULL);
1485   GNUNET_free ((void *) argv);
1486   if (GNUNET_OK == res)
1487     return ret;
1488   return 1;
1489 }
1490
1491 /* end of gnunet-transport.c */