GNUnet  0.19.5
gnunet-dht-monitor.c
Go to the documentation of this file.
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 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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
27 #include "gnunet_dht_service.h"
28 
32 static unsigned int block_type;
33 
37 static char *query_key;
38 
42 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
43 
47 static int verbose;
48 
53 
57 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58 
63 
67 static unsigned int result_count;
68 
72 static int ret;
73 
77 static struct GNUNET_SCHEDULER_Task *tt;
78 
79 
85 static void
86 cleanup_task (void *cls)
87 {
88  if (verbose)
89  fprintf (stderr, "%s", "Cleaning up!\n");
90  if (NULL != monitor_handle)
91  {
93  monitor_handle = NULL;
94  }
95  if (NULL != dht_handle)
96  {
98  dht_handle = NULL;
99  }
100  if (NULL != tt)
101  {
103  tt = NULL;
104  }
105 }
106 
107 
113 static void
114 timeout_task (void *cls)
115 {
116  tt = NULL;
118 }
119 
120 
131 static void
132 get_callback (void *cls,
134  enum GNUNET_BLOCK_Type type,
135  uint32_t hop_count,
136  uint32_t desired_replication_level,
137  const struct GNUNET_HashCode *key)
138 {
139  fprintf (stdout,
140  "GET #%u: type %d, key `%s'\n",
141  result_count,
142  (int) type,
143  GNUNET_h2s_full (key));
144  result_count++;
145 }
146 
147 
163 static void
164 get_resp_callback (void *cls,
165  enum GNUNET_BLOCK_Type type,
166  const struct GNUNET_PeerIdentity *trunc_peer,
167  const struct GNUNET_DHT_PathElement *get_path,
168  unsigned int get_path_length,
169  const struct GNUNET_DHT_PathElement *put_path,
170  unsigned int put_path_length,
171  struct GNUNET_TIME_Absolute exp,
172  const struct GNUNET_HashCode *key,
173  const void *data,
174  size_t size)
175 {
176  fprintf (stdout,
178  ? "RESPONSE #%u (%s): type %d, key `%s', data `%.*s'\n"
179  : "RESPONSE #%u (%s): type %d, key `%s'\n",
180  result_count,
182  (int) type,
184  (unsigned int) size,
185  (char *) data);
186  result_count++;
187 }
188 
189 
206 static void
207 put_callback (void *cls,
209  enum GNUNET_BLOCK_Type type,
210  uint32_t hop_count,
211  uint32_t desired_replication_level,
212  const struct GNUNET_PeerIdentity *trunc_peer,
213  unsigned int path_length,
214  const struct GNUNET_DHT_PathElement *path,
215  struct GNUNET_TIME_Absolute exp,
216  const struct GNUNET_HashCode *key,
217  const void *data,
218  size_t size)
219 {
220  fprintf (stdout,
222  ? "PUT %u (%s): type %d, key `%s', data `%.*s'\n"
223  : "PUT %u (%s): type %d, key `%s'\n",
224  result_count,
226  (int) type,
228  (unsigned int) size,
229  (char *) data);
230  result_count++;
231 }
232 
233 
242 static void
243 run (void *cls,
244  char *const *args,
245  const char *cfgfile,
246  const struct GNUNET_CONFIGURATION_Handle *c)
247 {
248  struct GNUNET_HashCode *key;
249  struct GNUNET_HashCode hc;
250 
251  cfg = c;
252 
253  if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
254  {
255  fprintf (stderr, "%s", _ ("Failed to connect to DHT service!\n"));
256  ret = 1;
257  return;
258  }
259  if (GNUNET_BLOCK_TYPE_ANY == block_type) /* Type of data not set */
261  if (NULL != query_key)
262  {
263  key = &hc;
266  }
267  else
268  {
269  key = NULL;
270  }
271  if (verbose)
272  fprintf (stderr,
273  "Monitoring for %s\n",
275  GNUNET_NO));
279  block_type,
280  key,
281  &get_callback,
283  &put_callback,
284  NULL);
285 }
286 
287 
295 int
296 main (int argc, char *const *argv)
297 {
300  "key",
301  "KEY",
302  gettext_noop ("the query key"),
303  &query_key),
304 
306  "type",
307  "TYPE",
308  gettext_noop ("the type of data to look for"),
309  &block_type),
310 
312  'T',
313  "timeout",
314  "TIMEOUT",
315  gettext_noop ("how long should the monitor command run"),
316  &timeout_request),
317 
319  "verbose",
320  gettext_noop (
321  "be verbose (print progress information)"),
322  &verbose),
323 
325  };
326 
327 
328  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
329  return 2;
330 
331  return (GNUNET_OK ==
332  GNUNET_PROGRAM_run (argc,
333  argv,
334  "gnunet-dht-monitor",
335  gettext_noop (
336  "Prints all packets that go through the DHT."),
337  options,
338  &run,
339  NULL))
340  ? ret
341  : 1;
342 }
343 
344 
345 /* end of gnunet-dht-monitor.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
@ GNUNET_BLOCK_TYPE_TEST
Block for testing.
#define gettext_noop(String)
Definition: gettext.h:70
static void put_callback(void *cls, enum GNUNET_DHT_RouteOption options, enum GNUNET_BLOCK_Type type, uint32_t hop_count, uint32_t desired_replication_level, const struct GNUNET_PeerIdentity *trunc_peer, unsigned int path_length, const struct GNUNET_DHT_PathElement *path, struct GNUNET_TIME_Absolute exp, const struct GNUNET_HashCode *key, const void *data, size_t size)
Callback called on each PUT request going through the DHT.
static int verbose
Be verbose.
static struct GNUNET_TIME_Relative timeout_request
User supplied timeout value (in seconds)
static void timeout_task(void *cls)
We hit a timeout.
static unsigned int result_count
Count of messages received.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Global handle of the configuration.
static void cleanup_task(void *cls)
Stop monitoring request and start shutdown.
static int ret
Global status value.
static char * query_key
The key to be monitored.
static void get_callback(void *cls, enum GNUNET_DHT_RouteOption options, enum GNUNET_BLOCK_Type type, uint32_t hop_count, uint32_t desired_replication_level, const struct GNUNET_HashCode *key)
Callback called on each GET request going through the DHT.
static unsigned int block_type
The type of the query.
static struct GNUNET_DHT_Handle * dht_handle
Handle to the DHT.
static struct GNUNET_SCHEDULER_Task * tt
Task scheduled to handle timeout.
static void get_resp_callback(void *cls, enum GNUNET_BLOCK_Type type, const struct GNUNET_PeerIdentity *trunc_peer, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_length, struct GNUNET_TIME_Absolute exp, const struct GNUNET_HashCode *key, const void *data, size_t size)
Callback called on each GET reply going through the DHT.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run by the scheduler.
static struct GNUNET_DHT_MonitorHandle * monitor_handle
Handle for the get request.
int main(int argc, char *const *argv)
Entry point for gnunet-dht-monitor.
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t data
The data value.
API to the DHT service.
GNUNET_DHT_RouteOption
Options for routing.
struct GNUNET_DHT_MonitorHandle * GNUNET_DHT_monitor_start(struct GNUNET_DHT_Handle *handle, enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode *key, GNUNET_DHT_MonitorGetCB get_cb, GNUNET_DHT_MonitorGetRespCB get_resp_cb, GNUNET_DHT_MonitorPutCB put_cb, void *cb_cls)
Start monitoring the local DHT service.
Definition: dht_api.c:1270
struct GNUNET_DHT_Handle * GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int ht_len)
Initialize the connection with the DHT service.
Definition: dht_api.c:1039
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:1062
void GNUNET_DHT_monitor_stop(struct GNUNET_DHT_MonitorHandle *mh)
Stop monitoring.
Definition: dht_api.c:1302
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time).
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
#define GNUNET_CRYPTO_hash_from_string(enc, result)
Convert ASCII encoding back to struct GNUNET_HashCode
@ GNUNET_OK
@ GNUNET_NO
const char * GNUNET_h2s_full(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:562
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1334
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1272
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1222
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:616
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:569
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Connection to the DHT service.
Definition: dht_api.c:237
Handle to a monitoring request.
Definition: dht_api.c:151
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
Definition of a command line option.
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition: scheduler.c:136
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model