loglevel MESSAGE for the incoming connection message + type
[oweals/gnunet.git] / src / cadet / gnunet-cadet.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 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 cadet/gnunet-cadet.c
23  * @brief Print information about cadet tunnels and peers.
24  * @author Bartlomiej Polot
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_cadet_service.h"
30 #include "cadet.h"
31
32
33 /**
34  * Option -P.
35  */
36 static int request_peers;
37
38 /**
39  * Option --peer
40  */
41 static char *peer_id;
42
43 /**
44  * Option -T.
45  */
46 static int request_tunnels;
47
48 /**
49  * Option --tunnel
50  */
51 static char *tunnel_id;
52
53 /**
54  * Option --connection
55  */
56 static char *conn_id;
57
58 /**
59  * Option --channel
60  */
61 static char *channel_id;
62
63 /**
64  * Port to listen on (-o).
65  */
66 static char *listen_port;
67
68 /**
69  * Request echo service
70  */
71 static int echo;
72
73 /**
74  * Request a debug dump
75  */
76 static int dump;
77
78 /**
79  * Time of last echo request.
80  */
81 static struct GNUNET_TIME_Absolute echo_time;
82
83 /**
84  * Task for next echo request.
85  */
86 static struct GNUNET_SCHEDULER_Task *echo_task;
87
88 /**
89  * Peer to connect to.
90  */
91 static char *target_id;
92
93 /**
94  * Port to connect to
95  */
96 static char *target_port = "default";
97
98 /**
99  * Cadet handle.
100  */
101 static struct GNUNET_CADET_Handle *mh;
102
103 /**
104  * Channel handle.
105  */
106 static struct GNUNET_CADET_Channel *ch;
107
108 /**
109  * HashCode of the given port string
110  */
111 static struct GNUNET_HashCode porthash;
112
113 /**
114  * Data structure for ongoing reception of incoming virtual circuits.
115  */
116 struct GNUNET_CADET_Port *lp;
117
118 /**
119  * Task for reading from stdin.
120  */
121 static struct GNUNET_SCHEDULER_Task *rd_task;
122
123 /**
124  * Task for main job.
125  */
126 static struct GNUNET_SCHEDULER_Task *job;
127
128
129 /**
130  * Wait for input on STDIO and send it out over the #ch.
131  */
132 static void
133 listen_stdio (void);
134
135
136 /**
137  * Convert encryption status to human readable string.
138  *
139  * @param status Encryption status.
140  *
141  * @return Human readable string.
142  */
143 static const char *
144 enc_2s (uint16_t status)
145 {
146   switch (status)
147   {
148     case 0:
149       return "NULL ";
150     case 1:
151       return "KSENT";
152     case 2:
153       return "KRECV";
154     case 3:
155       return "READY";
156     default:
157       return "";
158   }
159 }
160
161
162 /**
163  * Convert connection status to human readable string.
164  *
165  * @param status Connection status.
166  *
167  * @return Human readable string.
168  */
169 static const char *
170 conn_2s (uint16_t status)
171 {
172   switch (status)
173   {
174     case 0:
175       return "NEW  ";
176     case 1:
177       return "SRCH ";
178     case 2:
179       return "WAIT ";
180     case 3:
181       return "READY";
182     case 4:
183       return "SHUTD";
184     default:
185       return "";
186   }
187 }
188
189
190
191 /**
192  * Task to shut down this application.
193  *
194  * @param cls Closure (unused).
195  */
196 static void
197 shutdown_task (void *cls)
198 {
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Shutdown\n");
201   if (NULL != ch)
202   {
203     GNUNET_CADET_channel_destroy (ch);
204     ch = NULL;
205   }
206   if (NULL != mh)
207   {
208     GNUNET_CADET_disconnect (mh);
209     mh = NULL;
210   }
211   if (NULL != rd_task)
212   {
213     GNUNET_SCHEDULER_cancel (rd_task);
214     rd_task = NULL;
215   }
216   if (NULL != echo_task)
217   {
218     GNUNET_SCHEDULER_cancel (echo_task);
219     echo_task = NULL;
220   }
221   if (NULL != job)
222   {
223     GNUNET_SCHEDULER_cancel (job);
224     job = NULL;
225   }
226 }
227
228
229 /**
230  * Task run in stdio mode, after some data is available at stdin.
231  *
232  * @param cls Closure (unused).
233  */
234 static void
235 read_stdio (void *cls)
236 {
237   struct GNUNET_MQ_Envelope *env;
238   struct GNUNET_MessageHeader *msg;
239   char buf[60000];
240   ssize_t data_size;
241
242   rd_task = NULL;
243   data_size = read (0,
244                     buf,
245                     60000);
246   if (data_size < 1)
247   {
248     GNUNET_SCHEDULER_shutdown();
249     return;
250   }
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252               "Read %u bytes from stdio\n",
253               (unsigned int) data_size);
254   env = GNUNET_MQ_msg_extra (msg,
255                              data_size,
256                              GNUNET_MESSAGE_TYPE_CADET_CLI);
257   GNUNET_memcpy (&msg[1],
258                  buf,
259                  data_size);
260   GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
261                   env);
262   if (GNUNET_NO == echo)
263   {
264     listen_stdio ();
265   }
266   else
267   {
268     echo_time = GNUNET_TIME_absolute_get ();
269   }
270 }
271
272
273 /**
274  * Wait for input on STDIO and send it out over the #ch.
275  */
276 static void
277 listen_stdio ()
278 {
279   struct GNUNET_NETWORK_FDSet *rs;
280
281   /* FIXME: why use 'rs' here, seems overly complicated... */
282   rs = GNUNET_NETWORK_fdset_create ();
283   GNUNET_NETWORK_fdset_set_native (rs,
284                                    0); /* STDIN */
285   rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
286                                          GNUNET_TIME_UNIT_FOREVER_REL,
287                                          rs,
288                                          NULL,
289                                          &read_stdio,
290                                          NULL);
291   GNUNET_NETWORK_fdset_destroy (rs);
292 }
293
294
295 /**
296  * Function called whenever a channel is destroyed.  Should clean up
297  * any associated state.
298  *
299  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
300  *
301  * @param cls closure
302  * @param channel connection to the other end (henceforth invalid)
303  */
304 static void
305 channel_ended (void *cls,
306                const struct GNUNET_CADET_Channel *channel)
307 {
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309               "Channel ended!\n");
310   GNUNET_assert (channel == ch);
311   ch = NULL;
312   GNUNET_SCHEDULER_shutdown ();
313 }
314
315
316 /**
317  * Method called whenever another peer has added us to a channel
318  * the other peer initiated.
319  * Only called (once) upon reception of data with a message type which was
320  * subscribed to in #GNUNET_CADET_connect.
321  *
322  * A call to #GNUNET_CADET_channel_destroy causes the channel to be ignored.
323  * In this case the handler MUST return NULL.
324  *
325  * @param cls closure
326  * @param channel new handle to the channel
327  * @param initiator peer that started the channel
328  * @return initial channel context for the channel, we use @a channel
329  */
330 static void *
331 channel_incoming (void *cls,
332                   struct GNUNET_CADET_Channel *channel,
333                   const struct GNUNET_PeerIdentity *initiator)
334 {
335   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
336               "Incoming connection from %s\n",
337               GNUNET_i2s_full (initiator));
338   GNUNET_assert (NULL == ch);
339   GNUNET_assert (NULL != lp);
340   GNUNET_CADET_close_port (lp);
341   lp = NULL;
342   ch = channel;
343   if (GNUNET_NO == echo)
344     listen_stdio ();
345   return channel;
346 }
347
348
349 /**
350  * @brief Send an echo request to the remote peer.
351  *
352  * @param cls Closure (NULL).
353  */
354 static void
355 send_echo (void *cls)
356 {
357   struct GNUNET_MQ_Envelope *env;
358   struct GNUNET_MessageHeader *msg;
359
360   echo_task = NULL;
361   if (NULL == ch)
362     return;
363   env = GNUNET_MQ_msg (msg,
364                        GNUNET_MESSAGE_TYPE_CADET_CLI);
365   GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
366                   env);
367 }
368
369
370 /**
371  * Call CADET's monitor API, request debug dump on the service.
372  *
373  * @param cls Closure (unused).
374  */
375 static void
376 request_dump (void *cls)
377 {
378   GNUNET_CADET_request_dump (mh);
379   GNUNET_SCHEDULER_shutdown ();
380 }
381
382
383 /**
384  * Function called whenever a message is received.
385  *
386  * Each time the function must call #GNUNET_CADET_receive_done on the channel
387  * in order to receive the next message. This doesn't need to be immediate:
388  * can be delayed if some processing is done on the message.
389  *
390  * @param cls should match #ch
391  * @param message The actual message.
392  * @return #GNUNET_OK to keep the channel open,
393  *         #GNUNET_SYSERR to close it (signal serious error).
394  */
395 static int
396 check_data (void *cls,
397             const struct GNUNET_MessageHeader *message)
398 {
399   return GNUNET_OK; /* all is well-formed */
400 }
401
402
403 /**
404  * Function called whenever a message is received.
405  *
406  * Each time the function must call #GNUNET_CADET_receive_done on the channel
407  * in order to receive the next message. This doesn't need to be immediate:
408  * can be delayed if some processing is done on the message.
409  *
410  * @param cls NULL
411  * @param message The actual message.
412  */
413 static void
414 handle_data (void *cls,
415              const struct GNUNET_MessageHeader *message)
416 {
417   size_t payload_size = ntohs (message->size) - sizeof (*message);
418   uint16_t len;
419   ssize_t done;
420   uint16_t off;
421   const char *buf;
422
423   GNUNET_CADET_receive_done (ch);
424   if (GNUNET_YES == echo)
425   {
426     if (NULL != listen_port)
427     {
428       struct GNUNET_MQ_Envelope *env;
429       struct GNUNET_MessageHeader *msg;
430
431       env = GNUNET_MQ_msg_extra (msg,
432                                  payload_size,
433                                  GNUNET_MESSAGE_TYPE_CADET_CLI);
434       GNUNET_memcpy (&msg[1],
435                      &message[1],
436                      payload_size);
437       GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
438                       env);
439       return;
440     }
441     else
442     {
443       struct GNUNET_TIME_Relative latency;
444
445       latency = GNUNET_TIME_absolute_get_duration (echo_time);
446       echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
447       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
448                   "time: %s\n",
449                   GNUNET_STRINGS_relative_time_to_string (latency,
450                                                           GNUNET_NO));
451       echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
452                                                 &send_echo,
453                                                 NULL);
454     }
455   }
456
457   len = ntohs (message->size) - sizeof (*message);
458   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459               "Got %u bytes\n",
460               len);
461   buf = (const char *) &message[1];
462   off = 0;
463   while (off < len)
464   {
465     done = write (1,
466                   &buf[off],
467                   len - off);
468     if (done <= 0)
469     {
470       if (-1 == done)
471         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
472                              "write");
473       GNUNET_SCHEDULER_shutdown ();
474       return;
475     }
476     off += done;
477   }
478 }
479
480
481 /**
482  * Method called to retrieve information about all peers in CADET, called
483  * once per peer.
484  *
485  * After last peer has been reported, an additional call with NULL is done.
486  *
487  * @param cls Closure.
488  * @param peer Peer, or NULL on "EOF".
489  * @param tunnel Do we have a tunnel towards this peer?
490  * @param n_paths Number of known paths towards this peer.
491  * @param best_path How long is the best path?
492  *                  (0 = unknown, 1 = ourselves, 2 = neighbor)
493  */
494 static void
495 peers_callback (void *cls,
496                 const struct GNUNET_PeerIdentity *peer,
497                 int tunnel,
498                 unsigned int n_paths,
499                 unsigned int best_path)
500 {
501   if (NULL == peer)
502   {
503     GNUNET_SCHEDULER_shutdown();
504     return;
505   }
506   FPRINTF (stdout,
507            "%s tunnel: %c, paths: %u\n",
508            GNUNET_i2s_full (peer),
509            tunnel ? 'Y' : 'N',
510            n_paths);
511 }
512
513
514 /**
515  * Method called to retrieve information about a specific peer
516  * known to the service.
517  *
518  * @param cls Closure.
519  * @param peer Peer ID.
520  * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
521  * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
522  * @param n_paths Number of paths known towards peer.
523  * @param paths Array of PEER_IDs representing all paths to reach the peer.
524  *              Each path starts with the local peer.
525  *              Each path ends with the destination peer (given in @c peer).
526  */
527 static void
528 peer_callback (void *cls,
529                const struct GNUNET_PeerIdentity *peer,
530                int tunnel,
531                int neighbor,
532                unsigned int n_paths,
533                const struct GNUNET_PeerIdentity *paths)
534 {
535   unsigned int i;
536   const struct GNUNET_PeerIdentity *p;
537
538   FPRINTF (stdout,
539            "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n",
540            GNUNET_i2s_full (peer),
541            tunnel ? "Y" : "N",
542            neighbor ? "Y" : "N",
543            n_paths);
544   p = paths;
545   for (i = 0; i < n_paths && NULL != p;)
546   {
547     FPRINTF (stdout,
548              "%s ",
549              GNUNET_i2s (p));
550     if (0 == memcmp (p,
551                      peer,
552                      sizeof (*p)))
553     {
554       FPRINTF (stdout, "\n");
555       i++;
556     }
557     p++;
558   }
559
560   GNUNET_SCHEDULER_shutdown();
561 }
562
563
564 /**
565  * Method called to retrieve information about all tunnels in CADET.
566  *
567  * @param cls Closure.
568  * @param peer Destination peer.
569  * @param channels Number of channels.
570  * @param connections Number of connections.
571  * @param estate Encryption state.
572  * @param cstate Connectivity state.
573  */
574 static void
575 tunnels_callback (void *cls,
576                   const struct GNUNET_PeerIdentity *peer,
577                   unsigned int channels,
578                   unsigned int connections,
579                   uint16_t estate,
580                   uint16_t cstate)
581 {
582   if (NULL == peer)
583   {
584     GNUNET_SCHEDULER_shutdown();
585     return;
586   }
587   FPRINTF (stdout,
588            "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n",
589            GNUNET_i2s_full (peer),
590            enc_2s (estate),
591            conn_2s (cstate),
592            channels,
593            connections);
594 }
595
596
597 /**
598  * Method called to retrieve information about a specific tunnel the cadet peer
599  * has established, o`r is trying to establish.
600  *
601  * @param cls Closure.
602  * @param peer Peer towards whom the tunnel is directed.
603  * @param n_channels Number of channels.
604  * @param n_connections Number of connections.
605  * @param channels Channels.
606  * @param connections Connections.
607  * @param estate Encryption status.
608  * @param cstate Connectivity status.
609  */
610 static void
611 tunnel_callback (void *cls,
612                  const struct GNUNET_PeerIdentity *peer,
613                  unsigned int n_channels,
614                  unsigned int n_connections,
615                  const struct GNUNET_CADET_ChannelTunnelNumber *channels,
616                  const struct GNUNET_CADET_ConnectionTunnelIdentifier *connections,
617                  unsigned int estate,
618                  unsigned int cstate)
619 {
620   unsigned int i;
621
622   if (NULL != peer)
623   {
624     FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
625     FPRINTF (stdout, "\t%u channels\n", n_channels);
626     for (i = 0; i < n_channels; i++)
627       FPRINTF (stdout, "\t\t%X\n", ntohl (channels[i].cn));
628     FPRINTF (stdout, "\t%u connections\n", n_connections);
629     for (i = 0; i < n_connections; i++)
630       FPRINTF (stdout, "\t\t%s\n", GNUNET_sh2s (&connections[i].connection_of_tunnel));
631     FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate));
632     FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate));
633   }
634   GNUNET_SCHEDULER_shutdown ();
635 }
636
637
638 /**
639  * Call CADET's meta API, get all peers known to a peer.
640  *
641  * @param cls Closure (unused).
642  */
643 static void
644 get_peers (void *cls)
645 {
646   job = NULL;
647   GNUNET_CADET_get_peers (mh, &peers_callback, NULL);
648 }
649
650
651 /**
652  * Call CADET's monitor API, get info of one peer.
653  *
654  * @param cls Closure (unused).
655  */
656 static void
657 show_peer (void *cls)
658 {
659   struct GNUNET_PeerIdentity pid;
660
661   job = NULL;
662   if (GNUNET_OK !=
663       GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
664                                                   strlen (peer_id),
665                                                   &pid.public_key))
666     {
667     fprintf (stderr,
668              _("Invalid peer ID `%s'\n"),
669              peer_id);
670     GNUNET_SCHEDULER_shutdown();
671     return;
672   }
673   GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL);
674 }
675
676
677 /**
678  * Call CADET's meta API, get all tunnels known to a peer.
679  *
680  * @param cls Closure (unused).
681  */
682 static void
683 get_tunnels (void *cls)
684 {
685   job = NULL;
686   GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL);
687 }
688
689
690 /**
691  * Call CADET's monitor API, get info of one tunnel.
692  *
693  * @param cls Closure (unused).
694  */
695 static void
696 show_tunnel (void *cls)
697 {
698   struct GNUNET_PeerIdentity pid;
699
700   if (GNUNET_OK !=
701       GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
702                                                   strlen (tunnel_id),
703                                                   &pid.public_key))
704   {
705     fprintf (stderr,
706              _("Invalid tunnel owner `%s'\n"),
707              tunnel_id);
708     GNUNET_SCHEDULER_shutdown ();
709     return;
710   }
711   GNUNET_CADET_get_tunnel (mh,
712                            &pid,
713                            &tunnel_callback,
714                            NULL);
715 }
716
717
718 /**
719  * Call CADET's monitor API, get info of one channel.
720  *
721  * @param cls Closure (unused).
722  */
723 static void
724 show_channel (void *cls)
725 {
726   job = NULL;
727   GNUNET_break (0);
728 }
729
730
731 /**
732  * Call CADET's monitor API, get info of one connection.
733  *
734  * @param cls Closure (unused).
735  */
736 static void
737 show_connection (void *cls)
738 {
739   job = NULL;
740   GNUNET_break (0);
741 }
742
743
744 /**
745  * Main function that will be run by the scheduler.
746  *
747  * @param cls closure
748  * @param args remaining command-line arguments
749  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
750  * @param cfg configuration
751  */
752 static void
753 run (void *cls,
754      char *const *args,
755      const char *cfgfile,
756      const struct GNUNET_CONFIGURATION_Handle *cfg)
757 {
758   struct GNUNET_MQ_MessageHandler handlers[] = {
759     GNUNET_MQ_hd_var_size (data,
760                            GNUNET_MESSAGE_TYPE_CADET_CLI,
761                            struct GNUNET_MessageHeader,
762                            NULL),
763     GNUNET_MQ_handler_end ()
764   };
765
766   /* FIXME add option to monitor apps */
767
768   target_id = args[0];
769   if (target_id && args[1])
770     target_port = args[1];
771
772   if ( (0 != (request_peers | request_tunnels)
773         || NULL != tunnel_id
774         || NULL != conn_id
775         || NULL != channel_id)
776        && target_id != NULL)
777   {
778     FPRINTF (stderr,
779              _("You must NOT give a TARGET "
780                "when using 'request all' options\n"));
781     return;
782   }
783
784   if (GNUNET_YES == dump)
785   {
786     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787                 "requesting debug dump\n");
788     job = GNUNET_SCHEDULER_add_now (&request_dump,
789                                     NULL);
790   }
791   else if (NULL != peer_id)
792   {
793     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794                 "Show peer\n");
795     job = GNUNET_SCHEDULER_add_now (&show_peer,
796                                     NULL);
797   }
798   else if (NULL != tunnel_id)
799   {
800     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801                 "Show tunnel\n");
802     job = GNUNET_SCHEDULER_add_now (&show_tunnel,
803                                     NULL);
804   }
805   else if (NULL != channel_id)
806   {
807     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808                 "Show channel\n");
809     job = GNUNET_SCHEDULER_add_now (&show_channel,
810                                     NULL);
811   }
812   else if (NULL != conn_id)
813   {
814     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815                 "Show connection\n");
816     job = GNUNET_SCHEDULER_add_now (&show_connection,
817                                     NULL);
818   }
819   else if (GNUNET_YES == request_peers)
820   {
821     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822                 "Show all peers\n");
823     job = GNUNET_SCHEDULER_add_now (&get_peers,
824                                     NULL);
825   }
826   else if (GNUNET_YES == request_tunnels)
827   {
828     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829                 "Show all tunnels\n");
830     job = GNUNET_SCHEDULER_add_now (&get_tunnels,
831                                     NULL);
832   }
833
834   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835               "Connecting to CADET service\n");
836   mh = GNUNET_CADET_connecT (cfg);
837   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
838                                  NULL);
839   if (NULL == mh)
840   {
841     GNUNET_SCHEDULER_shutdown ();
842     return;
843   }
844   if (NULL != listen_port)
845   {
846     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847                 "Opening CADET listen port\n");
848     GNUNET_CRYPTO_hash (listen_port,
849                         strlen (listen_port),
850                         &porthash);
851     lp = GNUNET_CADET_open_porT (mh,
852                                  &porthash,
853                                  &channel_incoming,
854                                  NULL,
855                                  NULL /* window changes */,
856                                  &channel_ended,
857                                  handlers);
858   }
859   if (NULL != target_id)
860   {
861     struct GNUNET_PeerIdentity pid;
862     enum GNUNET_CADET_ChannelOption opt;
863
864     if (GNUNET_OK !=
865         GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
866                                                     strlen (target_id),
867                                                     &pid.public_key))
868     {
869       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
870                   _("Invalid target `%s'\n"),
871                   target_id);
872       GNUNET_SCHEDULER_shutdown ();
873       return;
874     }
875     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876                 "Connecting to `%s:%s'\n",
877                 target_id,
878                 target_port);
879     opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
880     GNUNET_CRYPTO_hash (target_port,
881                         strlen(target_port),
882                         &porthash);
883     ch = GNUNET_CADET_channel_creatE (mh,
884                                       NULL,
885                                       &pid,
886                                       &porthash,
887                                       opt,
888                                       NULL /* window changes */,
889                                       &channel_ended,
890                                       handlers);
891     if (GNUNET_YES == echo)
892     {
893       echo_task = GNUNET_SCHEDULER_add_now (&send_echo,
894                                             NULL);
895     }
896     else
897     {
898       listen_stdio ();
899     }
900   }
901
902   if ( (NULL == lp) &&
903        (NULL == job) &&
904        (NULL == ch) )
905   {
906     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
907                 _("No action requested\n"));
908     GNUNET_SCHEDULER_shutdown ();
909     return;
910   }
911 }
912
913
914 /**
915  * The main function to obtain peer information.
916  *
917  * @param argc number of arguments from the command line
918  * @param argv command line arguments
919  * @return 0 ok, 1 on error
920  */
921 int
922 main (int argc,
923       char *const *argv)
924 {
925   int res;
926   const char helpstr[] = "Create channels and retreive info about cadets status.";
927   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
928     {'C', "connection", "CONNECTION_ID",
929      gettext_noop ("provide information about a particular connection"),
930      GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
931     {'e', "echo", NULL,
932      gettext_noop ("activate echo mode"),
933      GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
934     {'d', "dump", NULL,
935      gettext_noop ("dump debug information to STDERR"),
936      GNUNET_NO, &GNUNET_GETOPT_set_one, &dump},
937     {'o', "open-port", "PORT",
938      gettext_noop ("port to listen to"),
939      GNUNET_YES, &GNUNET_GETOPT_set_string, &listen_port},
940     {'p', "peer", "PEER_ID",
941      gettext_noop ("provide information about a patricular peer"),
942      GNUNET_YES, &GNUNET_GETOPT_set_string, &peer_id},
943     {'P', "peers", NULL,
944       gettext_noop ("provide information about all peers"),
945       GNUNET_NO, &GNUNET_GETOPT_set_one, &request_peers},
946     {'t', "tunnel", "TUNNEL_ID",
947      gettext_noop ("provide information about a particular tunnel"),
948      GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
949     {'T', "tunnels", NULL,
950      gettext_noop ("provide information about all tunnels"),
951      GNUNET_NO, &GNUNET_GETOPT_set_one, &request_tunnels},
952
953     GNUNET_GETOPT_OPTION_END
954   };
955
956   if (GNUNET_OK !=
957       GNUNET_STRINGS_get_utf8_args (argc, argv,
958                                     &argc, &argv))
959     return 2;
960
961   res = GNUNET_PROGRAM_run (argc, argv,
962                             "gnunet-cadet (OPTIONS | TARGET PORT)",
963                             gettext_noop (helpstr),
964                             options, &run, NULL);
965
966   GNUNET_free ((void *) argv);
967
968   if (GNUNET_OK == res)
969     return 0;
970   return 1;
971 }
972
973 /* end of gnunet-cadet.c */