aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-dht-monitor.c
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2012-04-17 13:49:06 +0000
committerBart Polot <bart@net.in.tum.de>2012-04-17 13:49:06 +0000
commitf47b063560515b6138b89e4d07fed09fac69eacf (patch)
tree093f99b9d245b8901011a84ff027361d8664b2ce /src/dht/gnunet-dht-monitor.c
parent4fbe3a77a2353a5f4a6074ca56106f285147b68d (diff)
downloadgnunet-f47b063560515b6138b89e4d07fed09fac69eacf.tar.gz
gnunet-f47b063560515b6138b89e4d07fed09fac69eacf.zip
- fixed small bugs, added FIXME, added monitor client (to be tested)
Diffstat (limited to 'src/dht/gnunet-dht-monitor.c')
-rw-r--r--src/dht/gnunet-dht-monitor.c256
1 files changed, 256 insertions, 0 deletions
diff --git a/src/dht/gnunet-dht-monitor.c b/src/dht/gnunet-dht-monitor.c
new file mode 100644
index 000000000..ab2e1a240
--- /dev/null
+++ b/src/dht/gnunet-dht-monitor.c
@@ -0,0 +1,256 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012 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 dht/gnunet-dht-monitor.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Bartlomiej Polot
25 */
26#include "platform.h"
27#include "gnunet_dht_service.h"
28
29/**
30 * The type of the query
31 */
32static unsigned int block_type;
33
34/**
35 * The key to be monitored
36 */
37static char *query_key;
38
39/**
40 * User supplied timeout value (in seconds)
41 */
42static unsigned long long timeout_request = 5;
43
44/**
45 * Be verbose
46 */
47static int verbose;
48
49/**
50* Handle to the DHT
51 */
52static struct GNUNET_DHT_Handle *dht_handle;
53
54/**
55 * Global handle of the configuration
56 */
57static const struct GNUNET_CONFIGURATION_Handle *cfg;
58
59/**
60 * Handle for the get request
61 */
62static struct GNUNET_DHT_MonitorHandle *monitor_handle;
63
64/**
65 * Count of messages received
66 */
67static unsigned int result_count;
68
69/**
70 * Global status value
71 */
72static int ret;
73
74
75/**
76 * Function called on shutdown, disconnects from DHT if necessary.
77 *
78 * @param cls closure (unused)
79 * @param tc Task Context
80 */
81static void
82shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83{
84 if (verbose)
85 FPRINTF (stderr, "%s", "Shutting down!\n");
86 if (dht_handle != NULL)
87 {
88 GNUNET_DHT_disconnect (dht_handle);
89 dht_handle = NULL;
90 }
91}
92
93
94/**
95 * Stop monitoring request and start shutdown
96 *
97 * @param cls closure (unused)
98 * @param tc Task Context
99 */
100static void
101cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102{
103 if (verbose)
104 FPRINTF (stderr, "%s", "Cleaning up!\n");
105 if (monitor_handle != NULL)
106 {
107 GNUNET_DHT_monitor_stop (monitor_handle);
108 monitor_handle = NULL;
109 }
110 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
111}
112
113
114/**
115 * Callback called on each request going through the DHT.
116 *
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.
129 */
130void
131monitor_callback (void *cls,
132 uint16_t mtype,
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,
142 const void *data,
143 size_t size)
144{
145 FPRINTF (stdout, "Result %d, type %d:\n%.*s\n",
146 result_count,
147 type,
148 (unsigned int) size,
149 (char *) data);
150 result_count++;
151}
152
153
154/**
155 * Main function that will be run by the scheduler.
156 *
157 * @param cls closure
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
161 */
162static void
163run (void *cls, char *const *args, const char *cfgfile,
164 const struct GNUNET_CONFIGURATION_Handle *c)
165{
166 struct GNUNET_TIME_Relative timeout;
167 GNUNET_HashCode *key;
168
169 cfg = c;
170
171 dht_handle = GNUNET_DHT_connect (cfg, 1);
172
173 if (dht_handle == NULL)
174 {
175 if (verbose)
176 FPRINTF (stderr, "%s", "Couldn't connect to DHT service!\n");
177 ret = 1;
178 return;
179 }
180 else if (verbose)
181 FPRINTF (stderr, "%s", "Connected to DHT service!\n");
182
183 if (block_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
184 block_type = GNUNET_BLOCK_TYPE_TEST;
185
186 if (query_key != NULL) {
187 key = GNUNET_malloc (sizeof(GNUNET_HashCode));
188 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
189 }
190 else
191 key = NULL;
192
193 if (0 != timeout_request)
194 {
195 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
196 timeout_request);
197 if (verbose)
198 FPRINTF (stderr, "Monitoring for %llus\n", timeout_request);
199 }
200 else
201 {
202 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
203 if (verbose)
204 FPRINTF (stderr, "Monitoring indefinitely (close with Ctrl+C)\n");
205 }
206
207 GNUNET_SCHEDULER_add_delayed (timeout, &cleanup_task, NULL);
208 if (verbose)
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);
212 if (verbose)
213 FPRINTF (stderr, "MONITOR started!\n");
214 GNUNET_free_non_null (key);
215
216}
217
218
219/**
220 * gnunet-dht-get command line options
221 */
222static struct GNUNET_GETOPT_CommandLineOption options[] = {
223 {'k', "key", "KEY",
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
236};
237
238
239/**
240 * Entry point for gnunet-dht-monitor
241 *
242 * @param argc number of arguments from the command line
243 * @param argv command line arguments
244 * @return 0 ok, 1 on error
245 */
246int
247main (int argc, char *const *argv)
248{
249 return (GNUNET_OK ==
250 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
251 gettext_noop
252 ("Prints all packets that go through the DHT."),
253 options, &run, NULL)) ? ret : 1;
254}
255
256/* end of gnunet-dht-monitor.c */