GNUnet  0.10.x
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  {
92  GNUNET_DHT_monitor_stop (monitor_handle);
93  monitor_handle = NULL;
94  }
95  if (NULL != dht_handle)
96  {
97  GNUNET_DHT_disconnect (dht_handle);
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 
121 
134 static void
135 get_callback (void *cls,
137  enum GNUNET_BLOCK_Type type,
138  uint32_t hop_count,
139  uint32_t desired_replication_level,
140  unsigned int path_length,
141  const struct GNUNET_PeerIdentity *path,
142  const struct GNUNET_HashCode * key)
143 {
144  FPRINTF (stdout,
145  "GET #%u: type %d, key `%s'\n",
146  result_count,
147  (int) type,
148  GNUNET_h2s_full(key));
149  result_count++;
150 }
151 
152 
167 static void
168 get_resp_callback (void *cls,
169  enum GNUNET_BLOCK_Type type,
170  const struct GNUNET_PeerIdentity *get_path,
171  unsigned int get_path_length,
172  const struct GNUNET_PeerIdentity *put_path,
173  unsigned int put_path_length,
174  struct GNUNET_TIME_Absolute exp,
175  const struct GNUNET_HashCode * key,
176  const void *data,
177  size_t size)
178 {
179  FPRINTF (stdout,
180  (GNUNET_BLOCK_TYPE_TEST == type)
181  ? "RESPONSE #%u (%s): type %d, key `%s', data `%.*s'\n"
182  : "RESPONSE #%u (%s): type %d, key `%s'\n",
183  result_count,
185  (int) type,
186  GNUNET_h2s_full (key),
187  (unsigned int) size,
188  (char *) data);
189  result_count++;
190 }
191 
192 
208 static void
209 put_callback (void *cls,
211  enum GNUNET_BLOCK_Type type,
212  uint32_t hop_count,
213  uint32_t desired_replication_level,
214  unsigned int path_length,
215  const struct GNUNET_PeerIdentity *path,
216  struct GNUNET_TIME_Absolute exp,
217  const struct GNUNET_HashCode * key,
218  const void *data,
219  size_t size)
220 {
221  FPRINTF (stdout,
222  (GNUNET_BLOCK_TYPE_TEST == type)
223  ? "PUT %u (%s): type %d, key `%s', data `%.*s'\n"
224  : "PUT %u (%s): type %d, key `%s'\n",
225  result_count,
227  (int) type,
228  GNUNET_h2s_full(key),
229  (unsigned int) size,
230  (char *) data);
231  result_count++;
232 }
233 
234 
243 static void
244 run (void *cls,
245  char *const *args,
246  const char *cfgfile,
247  const struct GNUNET_CONFIGURATION_Handle *c)
248 {
249  struct GNUNET_HashCode *key;
250  struct GNUNET_HashCode hc;
251 
252  cfg = c;
253 
254  if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
255  {
256  FPRINTF (stderr, "%s",
257  _("Failed to connect to DHT service!\n"));
258  ret = 1;
259  return;
260  }
261  if (GNUNET_BLOCK_TYPE_ANY == block_type) /* Type of data not set */
263  if (NULL != query_key)
264  {
265  key = &hc;
266  if (GNUNET_OK !=
268  GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
269  }
270  else
271  {
272  key = NULL;
273  }
274  if (verbose)
275  FPRINTF (stderr,
276  "Monitoring for %s\n",
278  tt = GNUNET_SCHEDULER_add_delayed (timeout_request,
279  &timeout_task,
280  NULL);
282  NULL);
283  monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
284  block_type,
285  key,
286  &get_callback,
288  &put_callback,
289  NULL);
290 }
291 
299 int
300 main (int argc, char *const *argv)
301 {
302  struct GNUNET_GETOPT_CommandLineOption options[] = {
303 
305  "key",
306  "KEY",
307  gettext_noop ("the query key"),
308  &query_key),
309 
311  "type",
312  "TYPE",
313  gettext_noop ("the type of data to look for"),
314  &block_type),
315 
317  "timeout",
318  "TIMEOUT",
319  gettext_noop ("how long should the monitor command run"),
320  &timeout_request),
321 
323  "verbose",
324  gettext_noop ("be verbose (print progress information)"),
325  &verbose),
326 
328  };
329 
330 
331  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
332  return 2;
333 
334  return (GNUNET_OK ==
335  GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-monitor",
337  ("Prints all packets that go through the DHT."),
338  options, &run, NULL)) ? ret : 1;
339 }
340 
341 /* end of gnunet-dht-monitor.c */
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)...
static int verbose
Be verbose.
int main(int argc, char *const *argv)
Entry point for gnunet-dht-monitor.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Global handle of the configuration.
Any type of block, used as a wildcard when searching.
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
#define FPRINTF
Definition: plibc.h:683
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1293
static void timeout_task(void *cls)
We hit a timeout.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
static struct GNUNET_SCHEDULER_Task * tt
Task scheduled to handle timeout.
void GNUNET_DHT_monitor_stop(struct GNUNET_DHT_MonitorHandle *mh)
Stop monitoring.
Definition: dht_api.c:1244
static struct GNUNET_DHT_Handle * dht_handle
Handle to the DHT.
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, unsigned int path_length, const struct GNUNET_PeerIdentity *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.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static struct GNUNET_TIME_Relative timeout_request
User supplied timeout value (in seconds)
Definition of a command line option.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
Connection to the DHT service.
Definition: dht_api.c:205
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.
static char * query_key
The key to be monitored.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
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.
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:1246
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:1205
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
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:727
static unsigned int result_count
Count of messages received.
A 512-bit hashcode.
GNUNET_DHT_RouteOption
Options for routing.
static void get_resp_callback(void *cls, enum GNUNET_BLOCK_Type type, const struct GNUNET_PeerIdentity *get_path, unsigned int get_path_length, const struct GNUNET_PeerIdentity *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.
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:67
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:895
const char * GNUNET_h2s_full(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:923
static int ret
Global status value.
static struct GNUNET_DHT_MonitorHandle * monitor_handle
Handle for the get request.
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:85
Block for testing.
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.
Entry in list of pending tasks.
Definition: scheduler.c:134
static void cleanup_task(void *cls)
Stop monitoring request and start shutdown.
int 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, parse options).
Definition: program.c:361
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
static unsigned int block_type
The type of the query.
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.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:792
uint32_t data
The data value.
#define GNUNET_CRYPTO_hash_from_string(enc, result)
Convert ASCII encoding back to struct GNUNET_HashCode
Handle to a monitoring request.
Definition: dht_api.c:152
Time for relative time used by GNUnet, in microseconds.
#define gettext_noop(String)
Definition: gettext.h:69
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, unsigned int path_length, const struct GNUNET_PeerIdentity *path, const struct GNUNET_HashCode *key)
Callback called on each GET request going through the DHT.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965