GNUnet 0.28.1-dev.4-47-g3e168ca2d
 
Loading...
Searching...
No Matches
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
32static unsigned int block_type;
33
37static char *query_key;
38
42static int raw_key;
43
49static struct GNUNET_TIME_Relative timeout_request = { 60 * 1000 * 1000 };
50
54static int verbose;
55
60
64static const struct GNUNET_CONFIGURATION_Handle *cfg;
65
70
74static unsigned int result_count;
75
79static int ret;
80
85
86
92static void
93cleanup_task (void *cls)
94{
95 if (verbose)
96 fprintf (stderr, "%s", "Cleaning up!\n");
97 if (NULL != monitor_handle)
98 {
100 monitor_handle = NULL;
101 }
102 if (NULL != dht_handle)
103 {
105 dht_handle = NULL;
106 }
107 if (NULL != tt)
108 {
110 tt = NULL;
111 }
112}
113
114
120static void
121timeout_task (void *cls)
122{
123 tt = NULL;
125}
126
127
138static void
139get_callback (void *cls,
142 uint32_t hop_count,
143 uint32_t desired_replication_level,
144 const struct GNUNET_HashCode *key)
145{
146 fprintf (stdout,
147 "GET #%u: type %d, key `%s'\n",
149 (int) type,
151 result_count++;
152}
153
154
170static void
173 const struct GNUNET_PeerIdentity *trunc_peer,
174 const struct GNUNET_DHT_PathElement *get_path,
175 unsigned int get_path_length,
176 const struct GNUNET_DHT_PathElement *put_path,
177 unsigned int put_path_length,
178 struct GNUNET_TIME_Absolute exp,
179 const struct GNUNET_HashCode *key,
180 const void *data,
181 size_t size)
182{
183 fprintf (stdout,
185 ? "RESPONSE #%u (%s): type %d, key `%s', data `%.*s'\n"
186 : "RESPONSE #%u (%s): type %d, key `%s'\n",
189 (int) type,
191 (unsigned int) size,
192 (char *) data);
193 result_count++;
194}
195
196
213static void
214put_callback (void *cls,
217 uint32_t hop_count,
218 uint32_t desired_replication_level,
219 const struct GNUNET_PeerIdentity *trunc_peer,
220 unsigned int path_length,
221 const struct GNUNET_DHT_PathElement *path,
222 struct GNUNET_TIME_Absolute exp,
223 const struct GNUNET_HashCode *key,
224 const void *data,
225 size_t size)
226{
227 fprintf (stdout,
229 ? "PUT %u (%s): type %d, key `%s', data `%.*s'\n"
230 : "PUT %u (%s): type %d, key `%s'\n",
233 (int) type,
235 (unsigned int) size,
236 (char *) data);
237 result_count++;
238}
239
240
249static void
250run (void *cls,
251 char *const *args,
252 const char *cfgfile,
253 const struct GNUNET_CONFIGURATION_Handle *c)
254{
255 struct GNUNET_HashCode *key;
256 struct GNUNET_HashCode hc;
257
258 cfg = c;
259
260 /* Resolve the key before connecting, so that a bad key does not leak
261 the DHT handle (and leave the scheduler with nothing to shut down). */
262 if (NULL != query_key)
263 {
264 key = &hc;
266 {
267 if (raw_key)
268 {
269 fprintf (stderr,
270 _ ("`%s' is not a valid key\n"),
271 query_key);
272 ret = 1;
273 return;
274 }
276 }
277 }
278 else
279 {
280 key = NULL;
281 }
282 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
283 {
284 fprintf (stderr, "%s", _ ("Failed to connect to DHT service!\n"));
285 ret = 1;
286 return;
287 }
288 if (GNUNET_BLOCK_TYPE_ANY == block_type) /* Type of data not set */
290 if (verbose)
291 fprintf (stderr,
292 "Monitoring for %s\n",
294 GNUNET_NO));
299 key,
303 NULL);
304}
305
306
314int
315main (int argc, char *const *argv)
316{
319 "key",
320 "KEY",
322 "the query key, or a string to hash into one if it does not parse as a key (see -K)"),
323 &query_key),
324
326 "raw-key",
328 "fail instead of hashing -k when it is not a valid key"),
329 &raw_key),
330
332 "type",
333 "TYPE",
334 gettext_noop ("the type of data to look for"),
335 &block_type),
336
338 'T',
339 "timeout",
340 "TIMEOUT",
342 "how long should the monitor command run (default: 60 s, use \"forever\" to run until interrupted)"),
344
346 "verbose",
348 "be verbose (print progress information)"),
349 &verbose),
350
352 };
353
354
355 return (GNUNET_OK ==
357 argc,
358 argv,
359 "gnunet-dht-monitor",
361 "Prints all packets that go through the DHT."),
362 options,
363 &run,
364 NULL))
365 ? ret
366 : 1;
367}
368
369
370/* end of gnunet-dht-monitor.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
static struct GNUNET_SCHEDULER_Task * timeout_task
Task to be run on timeout.
Definition gnunet-arm.c:123
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 raw_key
Require query_key to be the key itself rather than its preimage?
static int verbose
Be verbose.
static struct GNUNET_TIME_Relative timeout_request
User supplied timeout value.
static unsigned int result_count
Count of messages received.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Global handle of the configuration.
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.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_SCHEDULER_Task * cleanup_task
Cleanup task.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_TEST
Block for testing.
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
API to the DHT service.
GNUNET_DHT_RouteOption
Options for routing.
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:1036
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:1271
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition dht_api.c:1058
void GNUNET_DHT_monitor_stop(struct GNUNET_DHT_MonitorHandle *handle)
Stop monitoring.
Definition dht_api.c:1303
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).
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
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:40
#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).
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, 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:406
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:572
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:1345
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
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:1283
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:610
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition strings.c:671
static unsigned int size
Size of the "table".
Definition peer.c:68
#define _(String)
GNU gettext support macro.
Definition platform.h:179
Connection to the DHT service.
Definition dht_api.c:235
Handle to a monitoring request.
Definition dht_api.c:149
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:141
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.