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