remember direct route to be released on direct disconnect
[oweals/gnunet.git] / src / dv / gnunet-dv.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 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  * @file dv/gnunet-dv.c
22  * @brief DV monitoring command line tool
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_dv_service.h"
28
29 /**
30  * Handle to DV service.
31  */
32 static struct GNUNET_DV_ServiceHandle *sh;
33
34 /**
35  * Was verbose specified?
36  */
37 static int verbose;
38
39
40 /**
41  * Function called if DV starts to be able to talk to a peer.
42  *
43  * @param cls closure
44  * @param peer newly connected peer
45  * @param distance distance to the peer
46  */
47 static void
48 connect_cb (void *cls,
49             const struct GNUNET_PeerIdentity *peer,
50             uint32_t distance)
51 {
52   fprintf (stderr, "Connect: %s at %u\n",
53            GNUNET_i2s (peer),
54            (unsigned int) distance);
55 }
56
57
58 /**
59  * Function called if DV distance to a peer is changed.
60  *
61  * @param cls closure
62  * @param peer connected peer
63  * @param distance new distance to the peer
64  */
65 static void
66 change_cb (void *cls,
67            const struct GNUNET_PeerIdentity *peer,
68            uint32_t distance)
69 {
70   fprintf (stderr, "Change: %s at %u\n",
71            GNUNET_i2s (peer),
72            (unsigned int) distance);
73 }
74
75
76 /**
77  * Function called if DV is no longer able to talk to a peer.
78  *
79  * @param cls closure
80  * @param peer peer that disconnected
81  */
82 static void
83 disconnect_cb (void *cls,
84                const struct GNUNET_PeerIdentity *peer)
85 {
86   fprintf (stderr, "Disconnect: %s\n",
87            GNUNET_i2s (peer));
88 }
89
90
91 /**
92  * Function called if DV receives a message for this peer.
93  *
94  * @param cls closure
95  * @param sender sender of the message
96  * @param distance how far did the message travel
97  * @param msg actual message payload
98  */
99 static void
100 message_cb (void *cls,
101             const struct GNUNET_PeerIdentity *sender,
102             uint32_t distance,
103             const struct GNUNET_MessageHeader *msg)
104 {
105   if (verbose)
106     fprintf (stderr, "Message: %s at %u sends %u bytes of type %u\n",
107              GNUNET_i2s (sender),
108              (unsigned int) distance,
109              (unsigned int) ntohs (msg->size),
110              (unsigned int) ntohs (msg->type));
111 }
112
113
114 /**
115  * Task run on shutdown.
116  *
117  * @param cls NULL
118  * @param tc unused
119  */
120 static void
121 shutdown_task (void *cls,
122                const struct GNUNET_SCHEDULER_TaskContext *tc)
123 {
124   GNUNET_DV_service_disconnect (sh);
125   sh = NULL;
126 }
127
128
129 /**
130  * Main function that will be run by the scheduler.
131  *
132  * @param cls closure
133  * @param args remaining command-line arguments
134  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
135  * @param cfg configuration
136  */
137 static void
138 run (void *cls, char *const *args, const char *cfgfile,
139      const struct GNUNET_CONFIGURATION_Handle *cfg)
140 {
141   sh = GNUNET_DV_service_connect (cfg, NULL,
142                                   &connect_cb,
143                                   &change_cb,
144                                   &disconnect_cb,
145                                   &message_cb);
146   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
147                                 &shutdown_task, NULL);
148 }
149
150
151 /**
152  * The main function.
153  *
154  * @param argc number of arguments from the command line
155  * @param argv command line arguments
156  * @return 0 ok, 1 on error
157  */
158 int
159 main (int argc, char *const *argv)
160 {
161   int res;
162
163   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
164     {'V', "verbose", NULL,
165      gettext_noop ("verbose output"),
166      0, &GNUNET_GETOPT_set_one, &verbose},
167     GNUNET_GETOPT_OPTION_END
168   };
169
170   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
171     return 2;
172
173   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-dv",
174                             gettext_noop ("Print information about DV state"),
175                             options, &run,
176                             NULL);
177   GNUNET_free ((void *) argv);
178
179   if (GNUNET_OK != res)
180     return 1;
181   return 0;
182 }
183
184 /* end of gnunet-dv.c */