- doc
[oweals/gnunet.git] / src / mesh / gnunet-mesh.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file mesh/gnunet-mesh.c
23  * @brief Print information about mesh tunnels and peers.
24  * @author Bartlomiej Polot
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_mesh_service.h"
29 #include "mesh.h"
30
31
32 /**
33  * Option -m.
34  */
35 static int monitor_connections;
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 int echo;
76
77 /**
78  * Time of last echo request.
79  */
80 struct GNUNET_TIME_Absolute echo_time;
81
82 /**
83  * Task for next echo request.
84  */
85 GNUNET_SCHEDULER_TaskIdentifier echo_task;
86
87 /**
88  * Peer to connect to.
89  */
90 static char *target_id;
91
92 /**
93  * Port to connect to
94  */
95 static uint32_t target_port;
96
97 /**
98  * Data pending in netcat mode.
99  */
100 size_t data_size;
101
102
103 /**
104  * Mesh handle.
105  */
106 static struct GNUNET_MESH_Handle *mh;
107
108 /**
109  * Channel handle.
110  */
111 static struct GNUNET_MESH_Channel *ch;
112
113 /**
114  * Shutdown task handle.
115  */
116 GNUNET_SCHEDULER_TaskIdentifier sd;
117
118
119
120 static void
121 listen_stdio (void);
122
123
124
125 /**
126  * Task run in monitor mode when the user presses CTRL-C to abort.
127  * Stops monitoring activity.
128  *
129  * @param cls Closure (unused).
130  * @param tc scheduler context
131  */
132 static void
133 shutdown_task (void *cls,
134                const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
137   if (NULL != ch)
138   {
139     GNUNET_MESH_channel_destroy (ch);
140     ch = NULL;
141   }
142   if (NULL != mh)
143   {
144     GNUNET_MESH_disconnect (mh);
145         mh = NULL;
146   }
147 }
148
149
150 /**
151  * Function called to notify a client about the connection
152  * begin ready to queue more data.  "buf" will be
153  * NULL and "size" zero if the connection was closed for
154  * writing in the meantime.
155  *
156  * FIXME
157  *
158  * @param cls closure
159  * @param size number of bytes available in buf
160  * @param buf where the callee should write the message
161  * @return number of bytes written to buf
162  */
163 size_t
164 data_ready (void *cls, size_t size, void *buf)
165 {
166   struct GNUNET_MessageHeader *msg;
167   size_t total_size;
168
169   if (NULL == buf || 0 == size)
170   {
171     GNUNET_SCHEDULER_shutdown();
172     return 0;
173   }
174
175   total_size = data_size + sizeof (struct GNUNET_MessageHeader);
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
177   GNUNET_assert (size >= total_size);
178
179   msg = buf;
180   msg->size = htons (total_size);
181   msg->type = htons (GNUNET_MESSAGE_TYPE_MESH_CLI);
182   memcpy (&msg[1], cls, data_size);
183   if (GNUNET_NO == echo)
184   {
185     listen_stdio ();
186   }
187   else
188   {
189     echo_time = GNUNET_TIME_absolute_get ();
190   }
191
192   return total_size;
193 }
194
195
196 /**
197  * Task run in monitor mode when the user presses CTRL-C to abort.
198  * Stops monitoring activity.
199  *
200  * @param cls Closure (unused).
201  * @param tc scheduler context
202  */
203 static void
204 read_stdio (void *cls,
205             const struct GNUNET_SCHEDULER_TaskContext *tc)
206 {
207   static char buf[60000];
208
209   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
210   {
211     return;
212   }
213
214   data_size = read (0, buf, 60000);
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
216   if (data_size < 1)
217   {
218     GNUNET_SCHEDULER_shutdown();
219     return;
220   }
221   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
222                                      GNUNET_TIME_UNIT_FOREVER_REL,
223                                      data_size
224                                      + sizeof (struct GNUNET_MessageHeader),
225                                      &data_ready, buf);
226 }
227
228
229 /**
230  * Start listening to stdin
231  */
232 static void
233 listen_stdio (void)
234 {
235   struct GNUNET_NETWORK_FDSet *rs;
236
237   rs = GNUNET_NETWORK_fdset_create ();
238   GNUNET_NETWORK_fdset_set_native (rs, 0);
239   GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
240                                GNUNET_TIME_UNIT_FOREVER_REL,
241                                rs, NULL,
242                                &read_stdio, NULL);
243   GNUNET_NETWORK_fdset_destroy (rs);
244 }
245
246
247 /**
248  * Function called whenever a channel is destroyed.  Should clean up
249  * any associated state.
250  *
251  * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
252  *
253  * @param cls closure (set from #GNUNET_MESH_connect)
254  * @param channel connection to the other end (henceforth invalid)
255  * @param channel_ctx place where local state associated
256  *                   with the channel is stored
257  */
258 static void
259 channel_ended (void *cls,
260                const struct GNUNET_MESH_Channel *channel,
261                void *channel_ctx)
262 {
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
264   GNUNET_break (channel == ch);
265   ch = NULL;
266   GNUNET_SCHEDULER_shutdown ();
267 }
268
269
270 /**
271  * Method called whenever another peer has added us to a channel
272  * the other peer initiated.
273  * Only called (once) upon reception of data with a message type which was
274  * subscribed to in #GNUNET_MESH_connect.
275  *
276  * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
277  * this case the handler MUST return NULL.
278  *
279  * @param cls closure
280  * @param channel new handle to the channel
281  * @param initiator peer that started the channel
282  * @param port Port this channel is for.
283  * @param options MeshOption flag field, with all active option bits set to 1.
284  *
285  * @return initial channel context for the channel
286  *         (can be NULL -- that's not an error)
287  */
288 static void *
289 channel_incoming (void *cls,
290                   struct GNUNET_MESH_Channel * channel,
291                   const struct GNUNET_PeerIdentity * initiator,
292                   uint32_t port, enum GNUNET_MESH_ChannelOption options)
293 {
294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295               "Incoming channel %p on port %u\n",
296               channel, port);
297   if (NULL != ch)
298   {
299     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
300     return NULL;
301   }
302   if (0 == listen_port)
303   {
304     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not listening to channels\n");
305     return NULL;
306   }
307   ch = channel;
308   if (GNUNET_NO == echo)
309   {
310     listen_stdio ();
311     return NULL;
312   }
313   data_size = 0;
314   return NULL;
315 }
316
317 /**
318  * @brief Send an echo request to the remote peer.
319  *
320  * @param cls Closure (NULL).
321  * @param tc Task context.
322  */
323 static void
324 send_echo (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
325 {
326   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
327                                      GNUNET_TIME_UNIT_FOREVER_REL,
328                                      sizeof (struct GNUNET_MessageHeader),
329                                      &data_ready, NULL);
330 }
331
332
333
334 /**
335  * Call MESH's monitor API, get info of one connection.
336  *
337  * @param cls Closure (unused).
338  * @param tc TaskContext
339  */
340 static void
341 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 {
343   struct GNUNET_PeerIdentity pid;
344   enum GNUNET_MESH_ChannelOption opt;
345
346   GNUNET_assert (NULL == ch);
347
348   if (GNUNET_OK !=
349       GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
350                                                   strlen (target_id),
351                                                   &pid.public_key))
352   {
353     FPRINTF (stderr,
354              _("Invalid target `%s'\n"),
355              target_id);
356     GNUNET_SCHEDULER_shutdown ();
357     return;
358   }
359   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
360   opt = GNUNET_MESH_OPTION_DEFAULT | GNUNET_MESH_OPTION_RELIABLE;
361   ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port, opt);
362   if (GNUNET_NO == echo)
363     listen_stdio ();
364   else
365     GNUNET_SCHEDULER_add_now (send_echo, NULL);
366 }
367
368
369 /**
370  * Function called whenever a message is received.
371  *
372  * Each time the function must call #GNUNET_MESH_receive_done on the channel
373  * in order to receive the next message. This doesn't need to be immediate:
374  * can be delayed if some processing is done on the message.
375  *
376  * @param cls Closure (set from #GNUNET_MESH_connect).
377  * @param channel Connection to the other end.
378  * @param channel_ctx Place to store local state associated with the channel.
379  * @param message The actual message.
380  * @return #GNUNET_OK to keep the channel open,
381  *         #GNUNET_SYSERR to close it (signal serious error).
382  */
383 static int
384 data_callback (void *cls,
385                struct GNUNET_MESH_Channel *channel,
386                void **channel_ctx,
387                const struct GNUNET_MessageHeader *message)
388 {
389   uint16_t len;
390   ssize_t done;
391   uint16_t off;
392   const char *buf;
393   GNUNET_break (ch == channel);
394
395   if (GNUNET_YES == echo)
396   {
397     if (0 != listen_port)
398     {
399       /* Just listening to echo incoming messages*/
400       GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
401                                         GNUNET_TIME_UNIT_FOREVER_REL,
402                                         sizeof (struct GNUNET_MessageHeader),
403                                         &data_ready, NULL);
404       return GNUNET_OK;
405     }
406     else
407     {
408       struct GNUNET_TIME_Relative latency;
409
410       latency = GNUNET_TIME_absolute_get_duration (echo_time);
411       echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
412       FPRINTF (stdout, "time: %s\n",
413                GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
414       echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
415                                                 &send_echo, NULL);
416     }
417   }
418
419   len = ntohs (message->size) - sizeof (*message);
420   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
421   buf = (const char *) &message[1];
422   off = 0;
423   while (off < len)
424   {
425     done = write (1, &buf[off], len - off);
426     if (done <= 0)
427     {
428       if (-1 == done)
429         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
430                              "write");
431       return GNUNET_SYSERR;
432     }
433     off += done;
434   }
435   return GNUNET_OK;
436 }
437
438
439 /**
440  * Method called to retrieve information about all peers in MESH, called
441  * once per peer.
442  *
443  * After last peer has been reported, an additional call with NULL is done.
444  *
445  * @param cls Closure.
446  * @param peer Peer, or NULL on "EOF".
447  * @param tunnel Do we have a tunnel towards this peer?
448  * @param n_paths Number of known paths towards this peer.
449  * @param best_path How long is the best path?
450  *                  (0 = unknown, 1 = ourselves, 2 = neighbor)
451  */
452 static void
453 peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
454                 int tunnel, unsigned int n_paths, unsigned int best_path)
455 {
456   if (NULL == peer)
457   {
458     if (GNUNET_YES != monitor_connections)
459     {
460       GNUNET_SCHEDULER_shutdown();
461     }
462     return;
463   }
464   FPRINTF (stdout, "%s tunnel: %c, paths: %u\n",
465            GNUNET_i2s_full (peer), tunnel ? 'Y' : 'N', n_paths);
466 }
467
468 /**
469  * Method called to retrieve information about a specific peer
470  * known to the service.
471  *
472  * @param cls Closure.
473  * @param peer Peer ID.
474  * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
475  * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
476  * @param n_paths Number of paths known towards peer.
477  * @param paths Array of PEER_IDs representing all paths to reach the peer.
478  *              Each path starts with the local peer.
479  *              Each path ends with the destination peer (given in @c peer).
480  */
481 void
482 peer_callback (void *cls,
483                const struct GNUNET_PeerIdentity *peer,
484                int tunnel,
485                int neighbor,
486                unsigned int n_paths,
487                struct GNUNET_PeerIdentity *paths)
488 {
489 }
490
491
492 /**
493  * Method called to retrieve information about all tunnels in MESH.
494  *
495  * @param cls Closure.
496  * @param peer Destination peer.
497  * @param channels Number of channels.
498  * @param connections Number of connections.
499  * @param estate Encryption state.
500  * @param cstate Connectivity state.
501  */
502 void
503 tunnels_callback (void *cls,
504                   const struct GNUNET_PeerIdentity *peer,
505                   unsigned int channels,
506                   unsigned int connections,
507                   uint16_t estate,
508                   uint16_t cstate)
509 {
510   if (NULL == peer)
511   {
512     if (GNUNET_YES != monitor_connections)
513     {
514       GNUNET_SCHEDULER_shutdown();
515     }
516     return;
517   }
518   FPRINTF (stdout, "%s [ENC: %u, CON: %u] CHs: %u, CONNs: %u\n",
519            GNUNET_i2s_full (peer), estate, cstate, channels, connections);
520 }
521
522
523 /**
524  * Method called to retrieve information about a specific tunnel the mesh peer
525  * has established, o`r is trying to establish.
526  *
527  * @param cls Closure.
528  * @param peer Peer towards whom the tunnel is directed.
529  * @param n_channels Number of channels.
530  * @param n_connections Number of connections.
531  * @param channels Channels.
532  * @param connections Connections.
533  * @param estate Encryption status.
534  * @param cstate Connectivity status.
535  */
536 void
537 tunnel_callback (void *cls,
538                  const struct GNUNET_PeerIdentity *peer,
539                  unsigned int n_channels,
540                  unsigned int n_connections,
541                  uint32_t *channels,
542                  struct GNUNET_MeshHash *connections,
543                  unsigned int estate,
544                  unsigned int cstate)
545 {
546   unsigned int i;
547
548   if (NULL != peer)
549   {
550     FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
551     FPRINTF (stdout, "- %u channels\n", n_channels);
552     for (i = 0; i < n_channels; i++)
553       FPRINTF (stdout, "   %u\n", channels[i]);
554     FPRINTF (stdout, "- %u connections\n", n_connections);
555     for (i = 0; i < n_connections; i++)
556       FPRINTF (stdout, "   %s\n", GM_h2s (&connections[i]));
557     FPRINTF (stdout, "- enc state: %u\n", estate);
558     FPRINTF (stdout, "- con state: %u\n", cstate);
559   }
560   if (GNUNET_YES != monitor_connections)
561   {
562     GNUNET_SCHEDULER_shutdown();
563   }
564   return;
565
566 }
567
568
569 /**
570  * Call MESH's meta API, get all peers known to a peer.
571  *
572  * @param cls Closure (unused).
573  * @param tc TaskContext
574  */
575 static void
576 get_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
577 {
578   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
579   {
580     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
581     return;
582   }
583   GNUNET_MESH_get_peers (mh, &peers_callback, NULL);
584 }
585
586
587 /**
588  * Call MESH's monitor API, get info of one peer.
589  *
590  * @param cls Closure (unused).
591  * @param tc TaskContext
592  */
593 static void
594 show_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
595 {
596   struct GNUNET_PeerIdentity pid;
597
598   if (GNUNET_OK !=
599     GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
600                                                 strlen (peer_id),
601                                                 &pid.public_key))
602   {
603     fprintf (stderr,
604              _("Invalid peer ID `%s'\n"),
605              peer_id);
606     GNUNET_SCHEDULER_shutdown();
607     return;
608   }
609   GNUNET_MESH_get_peer (mh, &pid, peer_callback, NULL);
610 }
611
612 /**
613  * Call MESH's meta API, get all tunnels known to a peer.
614  *
615  * @param cls Closure (unused).
616  * @param tc TaskContext
617  */
618 static void
619 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
620 {
621   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
622   {
623     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
624     return;
625   }
626   GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
627 }
628
629
630 /**
631  * Call MESH's monitor API, get info of one tunnel.
632  *
633  * @param cls Closure (unused).
634  * @param tc TaskContext
635  */
636 static void
637 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
638 {
639   struct GNUNET_PeerIdentity pid;
640
641   if (GNUNET_OK !=
642       GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
643                                                   strlen (tunnel_id),
644                                                   &pid.public_key))
645   {
646     fprintf (stderr,
647              _("Invalid tunnel owner `%s'\n"),
648              tunnel_id);
649     GNUNET_SCHEDULER_shutdown();
650     return;
651   }
652   GNUNET_MESH_get_tunnel (mh, &pid, tunnel_callback, NULL);
653 }
654
655
656 /**
657  * Call MESH's monitor API, get info of one channel.
658  *
659  * @param cls Closure (unused).
660  * @param tc TaskContext
661  */
662 static void
663 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
664 {
665
666 }
667
668
669 /**
670  * Call MESH's monitor API, get info of one connection.
671  *
672  * @param cls Closure (unused).
673  * @param tc TaskContext
674  */
675 static void
676 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
677 {
678
679 }
680
681
682 /**
683  * Main function that will be run by the scheduler.
684  *
685  * @param cls closure
686  * @param args remaining command-line arguments
687  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
688  * @param cfg configuration
689  */
690 static void
691 run (void *cls, char *const *args, const char *cfgfile,
692      const struct GNUNET_CONFIGURATION_Handle *cfg)
693 {
694   GNUNET_MESH_InboundChannelNotificationHandler *newch = NULL;
695   GNUNET_MESH_ChannelEndHandler *endch = NULL;
696   static const struct GNUNET_MESH_MessageHandler handlers[] = {
697     {&data_callback, GNUNET_MESSAGE_TYPE_MESH_CLI, 0},
698     {NULL, 0, 0} /* FIXME add option to monitor msg types */
699   };
700   static uint32_t *ports = NULL;
701   /* FIXME add option to monitor apps */
702
703   target_id = args[0];
704   target_port = args[0] && args[1] ? atoi(args[1]) : 0;
705   if ( (0 != (request_peers | request_tunnels)
706         || 0 != monitor_connections
707         || NULL != tunnel_id
708         || NULL != conn_id
709         || NULL != channel_id)
710        && target_id != NULL)
711   {
712     FPRINTF (stderr,
713              _("You must NOT give a TARGET"
714                "when using 'request all' options\n"));
715     return;
716   }
717
718   if (NULL != target_id)
719   {
720     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721                 "Creating channel to %s\n",
722                 target_id);
723     GNUNET_SCHEDULER_add_now (&create_channel, NULL);
724     endch = &channel_ended;
725   }
726   else if (0 != listen_port)
727   {
728     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
729     newch = &channel_incoming;
730     endch = &channel_ended;
731     ports = GNUNET_malloc (sizeof (uint32_t) * 2);
732     ports[0] = listen_port;
733   }
734   else if (NULL != peer_id)
735   {
736     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n");
737     GNUNET_SCHEDULER_add_now (&show_peer, NULL);
738   }
739   else if (NULL != tunnel_id)
740   {
741     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
742     GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
743   }
744   else if (NULL != channel_id)
745   {
746     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
747     GNUNET_SCHEDULER_add_now (&show_channel, NULL);
748   }
749   else if (NULL != conn_id)
750   {
751     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
752     GNUNET_SCHEDULER_add_now (&show_connection, NULL);
753   }
754   else if (GNUNET_YES == request_peers)
755   {
756     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n");
757     GNUNET_SCHEDULER_add_now (&get_peers, NULL);
758   }
759   else if (GNUNET_YES == request_tunnels)
760   {
761     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
762     GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
763   }
764   else
765   {
766     FPRINTF (stderr, "No action requested\n");
767     return;
768   }
769
770   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh\n");
771   mh = GNUNET_MESH_connect (cfg,
772                             NULL, /* cls */
773                             newch, /* new channel */
774                             endch, /* cleaner */
775                             handlers,
776                             ports);
777   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
778   if (NULL == mh)
779     GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
780   else
781     sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
782                                        shutdown_task, NULL);
783
784 }
785
786
787 /**
788  * The main function to obtain peer information.
789  *
790  * @param argc number of arguments from the command line
791  * @param argv command line arguments
792  * @return 0 ok, 1 on error
793  */
794 int
795 main (int argc, char *const *argv)
796 {
797   int res;
798   const char helpstr[] = "Create channels and retreive info about meshs status.";
799   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
800 //     {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
801 //      gettext_noop ("provide information about a particular channel"),
802 //      GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
803     {'C', "connection", "CONNECTION_ID",
804      gettext_noop ("provide information about a particular connection"),
805      GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
806     {'e', "echo", NULL,
807      gettext_noop ("activate echo mode"),
808      GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
809 //     {'m', "monitor", NULL,
810 //      gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
811 //      GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
812     {'o', "open-port", NULL,
813      gettext_noop ("port to listen to (default; 0)"),
814      GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
815     {'p', "peer", "PEER_ID",
816     gettext_noop ("provide information about all peers"),
817     GNUNET_NO, &GNUNET_GETOPT_set_string, &peer_id},
818     {'P', "peers", NULL,
819       gettext_noop ("provide information about all peers"),
820       GNUNET_NO, &GNUNET_GETOPT_set_one, &request_peers},
821     {'t', "tunnel", "TUNNEL_ID",
822      gettext_noop ("provide information about a particular tunnel"),
823      GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
824     {'T', "tunnels", NULL,
825      gettext_noop ("provide information about all tunnels"),
826      GNUNET_NO, &GNUNET_GETOPT_set_one, &request_tunnels},
827
828     GNUNET_GETOPT_OPTION_END
829   };
830
831   monitor_connections = GNUNET_NO;
832
833   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
834     return 2;
835
836   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
837                             gettext_noop (helpstr),
838                             options, &run, NULL);
839
840   GNUNET_free ((void *) argv);
841
842   if (GNUNET_OK == res)
843     return 0;
844   else
845     return 1;
846 }
847
848 /* end of gnunet-mesh.c */