first step to remove plibc
[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 it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14
15  You should have received a copy of the GNU Affero General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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
33 /**
34  * Timeout for a name resolution
35  */
36 #define RESOLUTION_TIMEOUT \
37   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, const struct GNUNET_PeerIdentity *key, void *value)
336 {
337   struct MonitoredPeer *m = value;
338
339   GNUNET_assert (
340     GNUNET_OK ==
341     GNUNET_CONTAINER_multipeermap_remove (monitored_peers, key, value));
342   GNUNET_free_non_null (m->address);
343   GNUNET_free (value);
344   return GNUNET_OK;
345 }
346
347
348 /**
349  * Task run in monitor mode when the user presses CTRL-C to abort.
350  * Stops monitoring activity.
351  *
352  * @param cls NULL
353  */
354 static void
355 shutdown_task (void *cls)
356 {
357   struct GNUNET_TIME_Relative duration;
358   struct ValidationResolutionContext *cur;
359   struct ValidationResolutionContext *next;
360   struct PeerResolutionContext *rc;
361
362   if (NULL != op_timeout)
363   {
364     GNUNET_SCHEDULER_cancel (op_timeout);
365     op_timeout = NULL;
366   }
367   if (NULL != pic)
368   {
369     GNUNET_TRANSPORT_monitor_peers_cancel (pic);
370     pic = NULL;
371   }
372   if (NULL != pm)
373   {
374     GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
375     pm = NULL;
376   }
377
378   next = vc_head;
379   for (cur = next; NULL != cur; cur = next)
380   {
381     next = cur->next;
382
383     GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
384     GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, cur);
385     GNUNET_free (cur->transport);
386     GNUNET_HELLO_address_free (cur->addrcp);
387     GNUNET_free (cur);
388   }
389   while (NULL != (rc = rc_head))
390   {
391     GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
392     GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
393     GNUNET_free (rc->transport);
394     GNUNET_free (rc->addrcp);
395     GNUNET_free (rc);
396   }
397   if (NULL != handle)
398   {
399     GNUNET_TRANSPORT_core_disconnect (handle);
400     handle = NULL;
401   }
402   if (benchmark_send)
403   {
404     duration = GNUNET_TIME_absolute_get_duration (start_time);
405     fprintf (stdout,
406              _ ("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
407              1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
408              traffic_sent,
409              GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
410   }
411   if (benchmark_receive)
412   {
413     duration = GNUNET_TIME_absolute_get_duration (start_time);
414     fprintf (stdout,
415              _ ("Received %llu bytes/s (%llu bytes in %s)\n"),
416              1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
417              traffic_received,
418              GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
419   }
420
421   if (NULL != monitored_peers)
422   {
423     GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
424     GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
425     monitored_peers = NULL;
426   }
427   if (NULL != monitored_plugins)
428   {
429     GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
430     GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
431     monitored_plugins = NULL;
432   }
433   if (NULL != blacklist)
434   {
435     GNUNET_TRANSPORT_blacklist_cancel (blacklist);
436     blacklist = NULL;
437     ret = 0;
438   }
439 }
440
441
442 /**
443  * We are done, shut down.
444  */
445 static void
446 operation_timeout (void *cls)
447 {
448   struct PeerResolutionContext *cur;
449   struct PeerResolutionContext *next;
450
451   op_timeout = NULL;
452   if ((benchmark_send) || (benchmark_receive))
453   {
454     fprintf (stdout, _ ("Failed to connect to `%s'\n"), GNUNET_i2s_full (&pid));
455     GNUNET_SCHEDULER_shutdown ();
456     ret = 1;
457     return;
458   }
459   if (iterate_connections)
460   {
461     next = rc_head;
462     while (NULL != (cur = next))
463     {
464       next = cur->next;
465       fprintf (stdout,
466                _ ("Failed to resolve address for peer `%s'\n"),
467                GNUNET_i2s (&cur->addrcp->peer));
468
469       GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, cur);
470       GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
471       GNUNET_free (cur->transport);
472       GNUNET_free (cur->addrcp);
473       GNUNET_free (cur);
474     }
475     fprintf (stdout,
476              "%s",
477              _ ("Failed to list connections, timeout occurred\n"));
478     GNUNET_SCHEDULER_shutdown ();
479     ret = 1;
480     return;
481   }
482 }
483
484
485 /**
486  * Function called to notify a client about the socket
487  * begin ready to queue more data.  Sends another message.
488  *
489  * @param cls closure with the message queue
490  */
491 static void
492 do_send (void *cls)
493 {
494   struct GNUNET_MQ_Handle *mq = cls;
495   struct GNUNET_MessageHeader *m;
496   struct GNUNET_MQ_Envelope *env;
497
498   env = GNUNET_MQ_msg_extra (m, BLOCKSIZE * 1024, GNUNET_MESSAGE_TYPE_DUMMY);
499   memset (&m[1], 52, BLOCKSIZE * 1024 - sizeof (struct GNUNET_MessageHeader));
500   traffic_sent += BLOCKSIZE * 1024;
501   GNUNET_MQ_notify_sent (env, &do_send, mq);
502   if (verbosity > 0)
503     fprintf (stdout,
504              _ ("Transmitting %u bytes\n"),
505              (unsigned int) BLOCKSIZE * 1024);
506   GNUNET_MQ_send (mq, env);
507 }
508
509
510 /**
511  * Function called to notify transport users that another
512  * peer connected to us.
513  *
514  * @param cls closure
515  * @param peer the peer that connected
516  * @param mq message queue for sending to @a peer
517  */
518 static void *
519 notify_connect (void *cls,
520                 const struct GNUNET_PeerIdentity *peer,
521                 struct GNUNET_MQ_Handle *mq)
522 {
523   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
524     return NULL;
525   ret = 0;
526   if (! benchmark_send)
527     return NULL;
528   if (NULL != op_timeout)
529   {
530     GNUNET_SCHEDULER_cancel (op_timeout);
531     op_timeout = NULL;
532   }
533   if (verbosity > 0)
534     fprintf (
535       stdout,
536       _ (
537         "Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
538       GNUNET_i2s (peer),
539       BLOCKSIZE);
540   start_time = GNUNET_TIME_absolute_get ();
541   do_send (mq);
542   return mq;
543 }
544
545
546 /**
547  * Function called to notify transport users that another
548  * peer disconnected from us.
549  *
550  * @param cls closure
551  * @param peer the peer that disconnected
552  * @param internal_cls what we returned from #notify_connect()
553  */
554 static void
555 notify_disconnect (void *cls,
556                    const struct GNUNET_PeerIdentity *peer,
557                    void *internal_cls)
558 {
559   if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
560     return;
561   if (NULL == internal_cls)
562     return; /* not about target peer */
563   if (! benchmark_send)
564     return; /* not transmitting */
565   fprintf (stdout,
566            _ ("Disconnected from peer `%s' while benchmarking\n"),
567            GNUNET_i2s (&pid));
568 }
569
570
571 /**
572  * Function called to notify transport users that another
573  * peer connected to us.
574  *
575  * @param cls closure
576  * @param peer the peer that connected
577  * @param mq for sending messages to @a peer
578  * @return NULL
579  */
580 static void *
581 monitor_notify_connect (void *cls,
582                         const struct GNUNET_PeerIdentity *peer,
583                         struct GNUNET_MQ_Handle *mq)
584 {
585   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
586   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
587
588   monitor_connect_counter++;
589   fprintf (stdout,
590            _ ("%24s: %-17s %4s   (%u connections in total)\n"),
591            now_str,
592            _ ("Connected to"),
593            GNUNET_i2s (peer),
594            monitor_connect_counter);
595   return NULL;
596 }
597
598
599 /**
600  * Function called to notify transport users that another
601  * peer disconnected from us.
602  *
603  * @param cls closure
604  * @param peer the peer that disconnected
605  * @param internal_cls what we returned from #monitor_notify_connect()
606  */
607 static void
608 monitor_notify_disconnect (void *cls,
609                            const struct GNUNET_PeerIdentity *peer,
610                            void *internal_cls)
611 {
612   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
613   const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
614
615   GNUNET_assert (monitor_connect_counter > 0);
616   monitor_connect_counter--;
617
618   fprintf (stdout,
619            _ ("%24s: %-17s %4s   (%u connections in total)\n"),
620            now_str,
621            _ ("Disconnected from"),
622            GNUNET_i2s (peer),
623            monitor_connect_counter);
624 }
625
626
627 /**
628  * Function called by the transport for each received message.
629  *
630  * @param cls closure
631  * @param message the message
632  * @return #GNUNET_OK
633  */
634 static int
635 check_dummy (void *cls, const struct GNUNET_MessageHeader *message)
636 {
637   return GNUNET_OK; /* all messages are fine */
638 }
639
640
641 /**
642  * Function called by the transport for each received message.
643  *
644  * @param cls closure
645  * @param message the message
646  */
647 static void
648 handle_dummy (void *cls, const struct GNUNET_MessageHeader *message)
649 {
650   if (! benchmark_receive)
651     return;
652   if (verbosity > 0)
653     fprintf (stdout,
654              _ ("Received %u bytes\n"),
655              (unsigned int) ntohs (message->size));
656   if (0 == traffic_received)
657     start_time = GNUNET_TIME_absolute_get ();
658   traffic_received += ntohs (message->size);
659 }
660
661
662 /**
663  * Convert address to a printable format.
664  *
665  * @param address the address
666  * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
667  *                to try to use reverse DNS
668  * @param state state the peer is in
669  * @param state_timeout when will the peer's state expire
670  */
671 static void
672 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
673                       int numeric,
674                       enum GNUNET_TRANSPORT_PeerState state,
675                       struct GNUNET_TIME_Absolute state_timeout);
676
677
678 static void
679 print_info (const struct GNUNET_PeerIdentity *id,
680             const char *transport,
681             const char *addr,
682             enum GNUNET_TRANSPORT_PeerState state,
683             struct GNUNET_TIME_Absolute state_timeout)
684 {
685
686   if (((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
687       (GNUNET_YES == monitor_connections))
688   {
689     fprintf (stdout,
690              _ ("Peer `%s': %s %s in state `%s' until %s\n"),
691              GNUNET_i2s (id),
692              (NULL == transport) ? "<none>" : transport,
693              (NULL == transport) ? "<none>" : addr,
694              GNUNET_TRANSPORT_ps2s (state),
695              GNUNET_STRINGS_absolute_time_to_string (state_timeout));
696   }
697   else if ((GNUNET_YES == iterate_connections) &&
698            (GNUNET_TRANSPORT_is_connected (state)))
699   {
700     /* Only connected peers, skip state */
701     fprintf (stdout,
702              _ ("Peer `%s': %s %s\n"),
703              GNUNET_i2s (id),
704              transport,
705              addr);
706   }
707 }
708
709
710 /**
711  * Function called with a textual representation of an address.  This
712  * function will be called several times with different possible
713  * textual representations, and a last time with @a address being NULL
714  * to signal the end of the iteration.  Note that @a address NULL
715  * always is the last call, regardless of the value in @a res.
716  *
717  * @param cls closure
718  * @param address NULL on end of iteration,
719  *        otherwise 0-terminated printable UTF-8 string,
720  *        in particular an empty string if @a res is #GNUNET_NO
721  * @param res result of the address to string conversion:
722  *        if #GNUNET_OK: conversion successful
723  *        if #GNUNET_NO: address was invalid (or not supported)
724  *        if #GNUNET_SYSERR: communication error (IPC error)
725  */
726 static void
727 process_peer_string (void *cls, const char *address, int res)
728 {
729   struct PeerResolutionContext *rc = cls;
730
731   if (NULL != address)
732   {
733     if (GNUNET_SYSERR == res)
734     {
735       fprintf (
736         stderr,
737         "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
738         GNUNET_i2s (&rc->addrcp->peer),
739         rc->addrcp->transport_name,
740         (unsigned int) rc->addrcp->address_length);
741       print_info (&rc->addrcp->peer,
742                   rc->transport,
743                   NULL,
744                   rc->state,
745                   rc->state_timeout);
746       rc->printed = GNUNET_YES;
747       return;
748     }
749     if (GNUNET_OK == res)
750     {
751       print_info (&rc->addrcp->peer,
752                   rc->transport,
753                   address,
754                   rc->state,
755                   rc->state_timeout);
756       rc->printed = GNUNET_YES;
757       return; /* Wait for done call */
758     }
759     /* GNUNET_NO == res: ignore, was simply not supported */
760     return;
761   }
762   /* NULL == address, last call, we are done */
763
764   rc->asc = NULL;
765   GNUNET_assert (address_resolutions > 0);
766   address_resolutions--;
767   if (GNUNET_NO == rc->printed)
768   {
769     if (numeric == GNUNET_NO)
770     {
771       /* Failed to resolve address, try numeric lookup
772          (note: this should not be needed, as transport
773          should fallback to numeric conversion if DNS takes
774          too long) */
775       resolve_peer_address (rc->addrcp,
776                             GNUNET_YES,
777                             rc->state,
778                             rc->state_timeout);
779     }
780     else
781     {
782       print_info (&rc->addrcp->peer,
783                   rc->transport,
784                   NULL,
785                   rc->state,
786                   rc->state_timeout);
787     }
788   }
789   GNUNET_free (rc->transport);
790   GNUNET_free (rc->addrcp);
791   GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
792   GNUNET_free (rc);
793   if ((0 == address_resolutions) && (iterate_connections))
794   {
795     if (NULL != op_timeout)
796     {
797       GNUNET_SCHEDULER_cancel (op_timeout);
798       op_timeout = NULL;
799     }
800     ret = 0;
801     GNUNET_SCHEDULER_shutdown ();
802   }
803 }
804
805
806 /**
807  * Convert address to a printable format and print it
808  * together with the given state data.
809  *
810  * @param address the address
811  * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
812  *                to try to use reverse DNS
813  * @param state state the peer is in
814  * @param state_timeout when will the peer's state expire
815  */
816 static void
817 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
818                       int numeric,
819                       enum GNUNET_TRANSPORT_PeerState state,
820                       struct GNUNET_TIME_Absolute state_timeout)
821 {
822   struct PeerResolutionContext *rc;
823
824   rc = GNUNET_new (struct PeerResolutionContext);
825   GNUNET_CONTAINER_DLL_insert (rc_head, rc_tail, rc);
826   address_resolutions++;
827   rc->transport = GNUNET_strdup (address->transport_name);
828   rc->addrcp = GNUNET_HELLO_address_copy (address);
829   rc->printed = GNUNET_NO;
830   rc->state = state;
831   rc->state_timeout = state_timeout;
832   /* Resolve address to string */
833   rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
834                                                 address,
835                                                 numeric,
836                                                 RESOLUTION_TIMEOUT,
837                                                 &process_peer_string,
838                                                 rc);
839 }
840
841
842 /**
843  * Function called with information about a peers during a one shot iteration
844  *
845  * @param cls closure
846  * @param peer identity of the peer, NULL for final callback when operation done
847  * @param address binary address used to communicate with this peer,
848  *  NULL on disconnect or when done
849  * @param state current state this peer is in
850  * @param state_timeout time out for the current state
851  */
852 static void
853 process_peer_iteration_cb (void *cls,
854                            const struct GNUNET_PeerIdentity *peer,
855                            const struct GNUNET_HELLO_Address *address,
856                            enum GNUNET_TRANSPORT_PeerState state,
857                            struct GNUNET_TIME_Absolute state_timeout)
858 {
859   if (NULL == peer)
860   {
861     /* done */
862     pic = NULL;
863     return;
864   }
865
866   if ((GNUNET_NO == iterate_all) &&
867       (GNUNET_NO == GNUNET_TRANSPORT_is_connected (state)))
868     return; /* Display only connected peers */
869
870   if (NULL != op_timeout)
871     GNUNET_SCHEDULER_cancel (op_timeout);
872   op_timeout =
873     GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL);
874
875   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876               "Received address for peer `%s': %s\n",
877               GNUNET_i2s (peer),
878               address ? address->transport_name : "");
879
880   if (NULL != address)
881     resolve_peer_address (address, numeric, state, state_timeout);
882   else
883     print_info (peer, NULL, NULL, state, state_timeout);
884 }
885
886
887 /**
888  * Context for address resolution by #plugin_monitoring_cb().
889  */
890 struct PluginMonitorAddress
891 {
892
893   /**
894    * Ongoing resolution request.
895    */
896   struct GNUNET_TRANSPORT_AddressToStringContext *asc;
897
898   /**
899    * Resolved address as string.
900    */
901   char *str;
902
903   /**
904    * Last event we got and did not yet print because
905    * @e str was NULL (address not yet resolved).
906    */
907   struct GNUNET_TRANSPORT_SessionInfo si;
908 };
909
910
911 /**
912  * Print information about a plugin monitoring event.
913  *
914  * @param addr out internal context
915  * @param info the monitoring information
916  */
917 static void
918 print_plugin_event_info (struct PluginMonitorAddress *addr,
919                          const struct GNUNET_TRANSPORT_SessionInfo *info)
920 {
921   const char *state;
922
923   switch (info->state)
924   {
925   case GNUNET_TRANSPORT_SS_INIT:
926     state = "INIT";
927     break;
928   case GNUNET_TRANSPORT_SS_HANDSHAKE:
929     state = "HANDSHAKE";
930     break;
931   case GNUNET_TRANSPORT_SS_UP:
932     state = "UP";
933     break;
934   case GNUNET_TRANSPORT_SS_UPDATE:
935     state = "UPDATE";
936     break;
937   case GNUNET_TRANSPORT_SS_DONE:
938     state = "DONE";
939     break;
940   default:
941     state = "UNKNOWN";
942     break;
943   }
944   fprintf (stdout,
945            "%s: state %s timeout in %s @ %s%s\n",
946            GNUNET_i2s (&info->address->peer),
947            state,
948            GNUNET_STRINGS_relative_time_to_string (
949              GNUNET_TIME_absolute_get_remaining (info->session_timeout),
950              GNUNET_YES),
951            addr->str,
952            (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
953   fprintf (stdout,
954            "%s: queue has %3u messages and %6u bytes\n",
955            GNUNET_i2s (&info->address->peer),
956            info->num_msg_pending,
957            info->num_bytes_pending);
958   if (0 !=
959       GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
960     fprintf (stdout,
961              "%s: receiving blocked until %s\n",
962              GNUNET_i2s (&info->address->peer),
963              GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
964 }
965
966
967 /**
968  * Function called with a textual representation of an address.  This
969  * function will be called several times with different possible
970  * textual representations, and a last time with @a address being NULL
971  * to signal the end of the iteration.  Note that @a address NULL
972  * always is the last call, regardless of the value in @a res.
973  *
974  * @param cls closure
975  * @param address NULL on end of iteration,
976  *        otherwise 0-terminated printable UTF-8 string,
977  *        in particular an empty string if @a res is #GNUNET_NO
978  * @param res result of the address to string conversion:
979  *        if #GNUNET_OK: conversion successful
980  *        if #GNUNET_NO: address was invalid (or not supported)
981  *        if #GNUNET_SYSERR: communication error (IPC error)
982  */
983 static void
984 address_cb (void *cls, const char *address, int res)
985 {
986   struct PluginMonitorAddress *addr = cls;
987
988   if (NULL == address)
989   {
990     addr->asc = NULL;
991     return;
992   }
993   if (NULL != addr->str)
994     return;
995   addr->str = GNUNET_strdup (address);
996   print_plugin_event_info (addr, &addr->si);
997 }
998
999
1000 /**
1001  * Function called by the plugin with information about the
1002  * current sessions managed by the plugin (for monitoring).
1003  *
1004  * @param cls closure (NULL)
1005  * @param session session handle this information is about,
1006  *        NULL to indicate that we are "in sync" (initial
1007  *        iteration complete)
1008  * @param session_ctx storage location where the application
1009  *        can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1010  *        and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1011  * @param info information about the state of the session,
1012  *        NULL if @a session is also NULL and we are
1013  *        merely signalling that the initial iteration is over;
1014  *        NULL with @a session being non-NULL if the monitor
1015  *        was being cancelled while sessions were active
1016  */
1017 static void
1018 plugin_monitoring_cb (void *cls,
1019                       struct GNUNET_TRANSPORT_PluginSession *session,
1020                       void **session_ctx,
1021                       const struct GNUNET_TRANSPORT_SessionInfo *info)
1022 {
1023   struct PluginMonitorAddress *addr;
1024
1025   if ((NULL == info) && (NULL == session))
1026     return; /* in sync with transport service */
1027   addr = *session_ctx;
1028   if (NULL == info)
1029   {
1030     if (NULL != addr)
1031     {
1032       if (NULL != addr->asc)
1033       {
1034         GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1035         addr->asc = NULL;
1036       }
1037       GNUNET_free_non_null (addr->str);
1038       GNUNET_free (addr);
1039       *session_ctx = NULL;
1040     }
1041     return; /* shutdown */
1042   }
1043   if (0 !=
1044       memcmp (&info->address->peer, &pid, sizeof (struct GNUNET_PeerIdentity)))
1045     return; /* filtered */
1046   if (NULL == addr)
1047   {
1048     addr = GNUNET_new (struct PluginMonitorAddress);
1049     addr->asc =
1050       GNUNET_TRANSPORT_address_to_string (cfg,
1051                                           info->address,
1052                                           numeric,
1053                                           GNUNET_TIME_UNIT_FOREVER_REL,
1054                                           &address_cb,
1055                                           addr);
1056     *session_ctx = addr;
1057   }
1058   if (NULL == addr->str)
1059     addr->si = *info;
1060   else
1061     print_plugin_event_info (addr, info);
1062   if (GNUNET_TRANSPORT_SS_DONE == info->state)
1063   {
1064     if (NULL != addr->asc)
1065     {
1066       GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1067       addr->asc = NULL;
1068     }
1069     GNUNET_free_non_null (addr->str);
1070     GNUNET_free (addr);
1071     *session_ctx = NULL;
1072   }
1073 }
1074
1075
1076 /**
1077  * Function called with information about a peers
1078  *
1079  * @param cls closure, NULL
1080  * @param peer identity of the peer, NULL for final callback when operation done
1081  * @param address binary address used to communicate with this peer,
1082  *  NULL on disconnect or when done
1083  * @param state current state this peer is in
1084  * @param state_timeout time out for the current state
1085  */
1086 static void
1087 process_peer_monitoring_cb (void *cls,
1088                             const struct GNUNET_PeerIdentity *peer,
1089                             const struct GNUNET_HELLO_Address *address,
1090                             enum GNUNET_TRANSPORT_PeerState state,
1091                             struct GNUNET_TIME_Absolute state_timeout)
1092 {
1093   struct MonitoredPeer *m;
1094
1095   if (NULL == peer)
1096   {
1097     fprintf (stdout,
1098              "%s",
1099              _ (
1100                "Monitor disconnected from transport service. Reconnecting.\n"));
1101     return;
1102   }
1103
1104   if (NULL != op_timeout)
1105     GNUNET_SCHEDULER_cancel (op_timeout);
1106   op_timeout =
1107     GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL);
1108
1109   if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1110   {
1111     m = GNUNET_new (struct MonitoredPeer);
1112     GNUNET_CONTAINER_multipeermap_put (
1113       monitored_peers,
1114       peer,
1115       m,
1116       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1117   }
1118   else
1119   {
1120     if ((m->state == state) &&
1121         (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1122         (NULL == address) && (NULL == m->address))
1123     {
1124       return; /* No real change */
1125     }
1126     if ((m->state == state) && (NULL != address) && (NULL != m->address) &&
1127         (0 == GNUNET_HELLO_address_cmp (m->address, address)))
1128       return; /* No real change */
1129   }
1130
1131   if (NULL != m->address)
1132   {
1133     GNUNET_free (m->address);
1134     m->address = NULL;
1135   }
1136   if (NULL != address)
1137     m->address = GNUNET_HELLO_address_copy (address);
1138   m->state = state;
1139   m->state_timeout = state_timeout;
1140
1141   if (NULL != address)
1142     resolve_peer_address (m->address, numeric, m->state, m->state_timeout);
1143   else
1144     print_info (peer, NULL, NULL, m->state, m->state_timeout);
1145 }
1146
1147
1148 /**
1149  * Function called with the transport service checking if we
1150  * want to blacklist a peer. Return #GNUNET_SYSERR for the
1151  * peer that we should disconnect from.
1152  *
1153  * @param cls NULL
1154  * @param cpid peer to check blacklisting for
1155  * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1156  */
1157 static int
1158 blacklist_cb (void *cls, const struct GNUNET_PeerIdentity *cpid)
1159 {
1160   if (0 == memcmp (cpid, &pid, sizeof (struct GNUNET_PeerIdentity)))
1161     return GNUNET_SYSERR;
1162   return GNUNET_OK;
1163 }
1164
1165
1166 /**
1167  * Main function that will be run by the scheduler.
1168  *
1169  * @param cls closure
1170  * @param args remaining command-line arguments
1171  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1172  * @param mycfg configuration
1173  */
1174 static void
1175 run (void *cls,
1176      char *const *args,
1177      const char *cfgfile,
1178      const struct GNUNET_CONFIGURATION_Handle *mycfg)
1179 {
1180   static struct GNUNET_PeerIdentity zero_pid;
1181   int counter = 0;
1182   ret = 1;
1183
1184   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1185
1186   counter = benchmark_send + benchmark_receive + iterate_connections +
1187             monitor_connections + monitor_connects + do_disconnect +
1188             monitor_plugins;
1189
1190   if (1 < counter)
1191   {
1192     fprintf (
1193       stderr,
1194       _ (
1195         "Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1196       "disconnect",
1197       "benchmark send",
1198       "benchmark receive",
1199       "information",
1200       "monitor",
1201       "events",
1202       "plugins");
1203     return;
1204   }
1205   if (0 == counter)
1206   {
1207     fprintf (
1208       stderr,
1209       _ (
1210         "No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1211       "disconnect",
1212       "benchmark send",
1213       "benchmark receive",
1214       "information",
1215       "monitor",
1216       "events",
1217       "plugins");
1218     return;
1219   }
1220
1221   if (do_disconnect) /* -D: Disconnect from peer */
1222   {
1223     if (0 == memcmp (&zero_pid, &pid, sizeof (pid)))
1224     {
1225       fprintf (stderr,
1226                _ ("Option `%s' makes no sense without option `%s'.\n"),
1227                "-D",
1228                "-p");
1229       ret = 1;
1230       return;
1231     }
1232     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_cb, NULL);
1233     if (NULL == blacklist)
1234     {
1235       fprintf (stderr,
1236                "%s",
1237                _ (
1238                  "Failed to connect to transport service for disconnection\n"));
1239       ret = 1;
1240       return;
1241     }
1242     fprintf (stdout,
1243              "%s",
1244              _ ("Blacklisting request in place, stop with CTRL-C\n"));
1245   }
1246   else if (benchmark_send) /* -s: Benchmark sending */
1247   {
1248     if (0 == memcmp (&zero_pid, &pid, sizeof (pid)))
1249     {
1250       fprintf (stderr,
1251                _ ("Option `%s' makes no sense without option `%s'.\n"),
1252                "-s",
1253                "-p");
1254       ret = 1;
1255       return;
1256     }
1257     handle = GNUNET_TRANSPORT_core_connect (cfg,
1258                                             NULL,
1259                                             NULL,
1260                                             NULL,
1261                                             &notify_connect,
1262                                             &notify_disconnect,
1263                                             NULL);
1264     if (NULL == handle)
1265     {
1266       fprintf (stderr, "%s", _ ("Failed to connect to transport service\n"));
1267       ret = 1;
1268       return;
1269     }
1270     start_time = GNUNET_TIME_absolute_get ();
1271     op_timeout =
1272       GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL);
1273   }
1274   else if (benchmark_receive) /* -b: Benchmark receiving */
1275   {
1276     struct GNUNET_MQ_MessageHandler handlers[] =
1277       {GNUNET_MQ_hd_var_size (dummy,
1278                               GNUNET_MESSAGE_TYPE_DUMMY,
1279                               struct GNUNET_MessageHeader,
1280                               NULL),
1281        GNUNET_MQ_handler_end ()};
1282
1283     handle = GNUNET_TRANSPORT_core_connect (cfg,
1284                                             NULL,
1285                                             handlers,
1286                                             NULL,
1287                                             NULL,
1288                                             NULL,
1289                                             NULL);
1290     if (NULL == handle)
1291     {
1292       fprintf (stderr, "%s", _ ("Failed to connect to transport service\n"));
1293       ret = 1;
1294       return;
1295     }
1296     if (verbosity > 0)
1297       fprintf (stdout, "%s", _ ("Starting to receive benchmark data\n"));
1298     start_time = GNUNET_TIME_absolute_get ();
1299   }
1300   else if (iterate_connections) /* -i: List information about peers once */
1301   {
1302     pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1303                                           &pid,
1304                                           GNUNET_YES,
1305                                           &process_peer_iteration_cb,
1306                                           (void *) cfg);
1307     op_timeout =
1308       GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL);
1309   }
1310   else if (monitor_connections) /* -m: List information about peers continuously
1311                                  */
1312   {
1313     monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1314     pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1315                                           &pid,
1316                                           GNUNET_NO,
1317                                           &process_peer_monitoring_cb,
1318                                           NULL);
1319   }
1320   else if (monitor_plugins) /* -P: List information about plugins continuously
1321                              */
1322   {
1323     monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1324     pm = GNUNET_TRANSPORT_monitor_plugins (cfg, &plugin_monitoring_cb, NULL);
1325   }
1326   else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1327   {
1328     monitor_connect_counter = 0;
1329     handle = GNUNET_TRANSPORT_core_connect (cfg,
1330                                             NULL,
1331                                             NULL,
1332                                             NULL,
1333                                             &monitor_notify_connect,
1334                                             &monitor_notify_disconnect,
1335                                             NULL);
1336     if (NULL == handle)
1337     {
1338       fprintf (stderr, "%s", _ ("Failed to connect to transport service\n"));
1339       ret = 1;
1340       return;
1341     }
1342     ret = 0;
1343   }
1344   else
1345   {
1346     GNUNET_break (0);
1347     return;
1348   }
1349
1350   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1351 }
1352
1353
1354 int
1355 main (int argc, char *const *argv)
1356 {
1357   int res;
1358   struct GNUNET_GETOPT_CommandLineOption options[] =
1359     {GNUNET_GETOPT_option_flag (
1360        'a',
1361        "all",
1362        gettext_noop (
1363          "print information for all peers (instead of only connected peers)"),
1364        &iterate_all),
1365      GNUNET_GETOPT_option_flag (
1366        'b',
1367        "benchmark",
1368        gettext_noop (
1369          "measure how fast we are receiving data from all peers (until CTRL-C)"),
1370        &benchmark_receive),
1371      GNUNET_GETOPT_option_flag ('D',
1372                                 "disconnect",
1373                                 gettext_noop ("disconnect from a peer"),
1374                                 &do_disconnect),
1375      GNUNET_GETOPT_option_flag (
1376        'i',
1377        "information",
1378        gettext_noop (
1379          "provide information about all current connections (once)"),
1380        &iterate_connections),
1381      GNUNET_GETOPT_option_flag (
1382        'm',
1383        "monitor",
1384        gettext_noop (
1385          "provide information about all current connections (continuously)"),
1386        &monitor_connections),
1387      GNUNET_GETOPT_option_flag (
1388        'e',
1389        "events",
1390        gettext_noop (
1391          "provide information about all connects and disconnect events (continuously)"),
1392        &monitor_connects),
1393      GNUNET_GETOPT_option_flag ('n',
1394                                 "numeric",
1395                                 gettext_noop ("do not resolve hostnames"),
1396                                 &numeric),
1397      GNUNET_GETOPT_option_base32_auto ('p',
1398                                        "peer",
1399                                        "PEER",
1400                                        gettext_noop ("peer identity"),
1401                                        &pid),
1402      GNUNET_GETOPT_option_flag ('P',
1403                                 "plugins",
1404                                 gettext_noop ("monitor plugin sessions"),
1405                                 &monitor_plugins),
1406      GNUNET_GETOPT_option_flag (
1407        's',
1408        "send",
1409        gettext_noop (
1410          "send data for benchmarking to the other peer (until CTRL-C)"),
1411        &benchmark_send),
1412      GNUNET_GETOPT_option_verbose (&verbosity),
1413      GNUNET_GETOPT_OPTION_END};
1414
1415   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1416     return 2;
1417
1418   res =
1419     GNUNET_PROGRAM_run (argc,
1420                         argv,
1421                         "gnunet-transport",
1422                         gettext_noop ("Direct access to transport service."),
1423                         options,
1424                         &run,
1425                         NULL);
1426   GNUNET_free ((void *) argv);
1427   if (GNUNET_OK == res)
1428     return ret;
1429   return 1;
1430 }
1431
1432 /* end of gnunet-transport.c */