6f5323c65c7488c9a4e673cc97d0ee8eef213f27
[oweals/gnunet.git] / src / dv / gnunet-dv.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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 /**
19  * @file dv/gnunet-dv.c
20  * @brief DV monitoring command line tool
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_dv_service.h"
26
27 /**
28  * Handle to DV service.
29  */
30 static struct GNUNET_DV_ServiceHandle *sh;
31
32 /**
33  * Was verbose specified?
34  */
35 static unsigned int verbose;
36
37
38 /**
39  * Function called if DV starts to be able to talk to a peer.
40  *
41  * @param cls closure
42  * @param peer newly connected peer
43  * @param distance distance to the peer
44  * @param network the network the next hop is located in
45  */
46 static void
47 connect_cb (void *cls,
48             const struct GNUNET_PeerIdentity *peer,
49             uint32_t distance,
50             enum GNUNET_NetworkType network)
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  * @param network network used on first hop to peer
65  */
66 static void
67 change_cb (void *cls,
68            const struct GNUNET_PeerIdentity *peer,
69            uint32_t distance,
70            enum GNUNET_NetworkType network)
71 {
72   fprintf (stderr, "Change: %s at %u\n",
73            GNUNET_i2s (peer),
74            (unsigned int) distance);
75 }
76
77
78 /**
79  * Function called if DV is no longer able to talk to a peer.
80  *
81  * @param cls closure
82  * @param peer peer that disconnected
83  */
84 static void
85 disconnect_cb (void *cls,
86                const struct GNUNET_PeerIdentity *peer)
87 {
88   fprintf (stderr, "Disconnect: %s\n",
89            GNUNET_i2s (peer));
90 }
91
92
93 /**
94  * Function called if DV receives a message for this peer.
95  *
96  * @param cls closure
97  * @param sender sender of the message
98  * @param distance how far did the message travel
99  * @param msg actual message payload
100  */
101 static void
102 message_cb (void *cls,
103             const struct GNUNET_PeerIdentity *sender,
104             uint32_t distance,
105             const struct GNUNET_MessageHeader *msg)
106 {
107   if (verbose)
108     fprintf (stderr, "Message: %s at %u sends %u bytes of type %u\n",
109              GNUNET_i2s (sender),
110              (unsigned int) distance,
111              (unsigned int) ntohs (msg->size),
112              (unsigned int) ntohs (msg->type));
113 }
114
115
116 /**
117  * Task run on shutdown.
118  *
119  * @param cls NULL
120  */
121 static void
122 shutdown_task (void *cls)
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_shutdown (&shutdown_task, NULL);
147 }
148
149
150 /**
151  * The main function.
152  *
153  * @param argc number of arguments from the command line
154  * @param argv command line arguments
155  * @return 0 ok, 1 on error
156  */
157 int
158 main (int argc, char *const *argv)
159 {
160   int res;
161
162   struct GNUNET_GETOPT_CommandLineOption options[] = {
163
164     GNUNET_GETOPT_option_verbose (&verbose),
165
166     GNUNET_GETOPT_OPTION_END
167   };
168
169   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
170     return 2;
171
172   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-dv",
173                             gettext_noop ("Print information about DV state"),
174                             options, &run,
175                             NULL);
176   GNUNET_free ((void *) argv);
177
178   if (GNUNET_OK != res)
179     return 1;
180   return 0;
181 }
182
183 /* end of gnunet-dv.c */