2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file dht/gnunet-dht-monitor.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Bartlomiej Polot
27 #include "gnunet_dht_service.h"
30 * The type of the query
32 static unsigned int block_type;
35 * The key to be monitored
37 static char *query_key;
40 * User supplied timeout value (in seconds)
42 static unsigned long long timeout_request = 5;
52 static struct GNUNET_DHT_Handle *dht_handle;
55 * Global handle of the configuration
57 static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 * Handle for the get request
62 static struct GNUNET_DHT_MonitorHandle *monitor_handle;
65 * Count of messages received
67 static unsigned int result_count;
76 * Function called on shutdown, disconnects from DHT if necessary.
78 * @param cls closure (unused)
79 * @param tc Task Context
82 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 FPRINTF (stderr, "%s", "Shutting down!\n");
86 if (dht_handle != NULL)
88 GNUNET_DHT_disconnect (dht_handle);
95 * Stop monitoring request and start shutdown
97 * @param cls closure (unused)
98 * @param tc Task Context
101 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 FPRINTF (stderr, "%s", "Cleaning up!\n");
105 if (monitor_handle != NULL)
107 GNUNET_DHT_monitor_stop (monitor_handle);
108 monitor_handle = NULL;
110 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
115 * Callback called on each request going through the DHT.
117 * @param cls Closure.
118 * @param mtype Type of the DHT message monitored.
119 * @param exp When will this value expire.
120 * @param key Key of the result/request.
121 * @param get_path Peers on reply path (or NULL if not recorded).
122 * @param get_path_length number of entries in get_path.
123 * @param put_path peers on the PUT path (or NULL if not recorded).
124 * @param put_path_length number of entries in get_path.
125 * @param desired_replication_level Desired replication level.
126 * @param type Type of the result/request.
127 * @param data Pointer to the result data.
128 * @param size Number of bytes in data.
131 monitor_callback (void *cls,
133 struct GNUNET_TIME_Absolute exp,
134 const GNUNET_HashCode * key,
135 const struct GNUNET_PeerIdentity *get_path,
136 unsigned int get_path_length,
137 const struct GNUNET_PeerIdentity *put_path,
138 unsigned int put_path_length,
139 uint32_t desired_replication_level,
140 enum GNUNET_DHT_RouteOption options,
141 enum GNUNET_BLOCK_Type type,
145 FPRINTF (stdout, "Result %d, type %d:\n%.*s\n",
155 * Main function that will be run by the scheduler.
158 * @param args remaining command-line arguments
159 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
160 * @param c configuration
163 run (void *cls, char *const *args, const char *cfgfile,
164 const struct GNUNET_CONFIGURATION_Handle *c)
166 struct GNUNET_TIME_Relative timeout;
167 GNUNET_HashCode *key;
171 dht_handle = GNUNET_DHT_connect (cfg, 1);
173 if (dht_handle == NULL)
176 FPRINTF (stderr, "%s", "Couldn't connect to DHT service!\n");
181 FPRINTF (stderr, "%s", "Connected to DHT service!\n");
183 if (block_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
184 block_type = GNUNET_BLOCK_TYPE_TEST;
186 if (query_key != NULL) {
187 key = GNUNET_malloc (sizeof(GNUNET_HashCode));
188 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
193 if (0 != timeout_request)
195 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
198 FPRINTF (stderr, "Monitoring for %llus\n", timeout_request);
202 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
204 FPRINTF (stderr, "Monitoring indefinitely (close with Ctrl+C)\n");
207 GNUNET_SCHEDULER_add_delayed (timeout, &cleanup_task, NULL);
209 FPRINTF (stderr, "Issuing MONITOR request for %s!\n", query_key);
210 monitor_handle = GNUNET_DHT_monitor_start (dht_handle, block_type, key,
211 &monitor_callback, NULL);
213 FPRINTF (stderr, "MONITOR started!\n");
214 GNUNET_free_non_null (key);
220 * gnunet-dht-get command line options
222 static struct GNUNET_GETOPT_CommandLineOption options[] = {
224 gettext_noop ("the query key"),
225 1, &GNUNET_GETOPT_set_string, &query_key},
226 {'t', "type", "TYPE",
227 gettext_noop ("the type of data to look for"),
228 1, &GNUNET_GETOPT_set_uint, &block_type},
229 {'T', "timeout", "TIMEOUT",
230 gettext_noop ("how long to execute? 0 = forever"),
231 1, &GNUNET_GETOPT_set_ulong, &timeout_request},
232 {'V', "verbose", NULL,
233 gettext_noop ("be verbose (print progress information)"),
234 0, &GNUNET_GETOPT_set_one, &verbose},
235 GNUNET_GETOPT_OPTION_END
240 * Entry point for gnunet-dht-monitor
242 * @param argc number of arguments from the command line
243 * @param argv command line arguments
244 * @return 0 ok, 1 on error
247 main (int argc, char *const *argv)
250 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
252 ("Prints all packets that go through the DHT."),
253 options, &run, NULL)) ? ret : 1;
256 /* end of gnunet-dht-monitor.c */