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