2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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-get-peer.c
22 * @brief search for peers close to key using the DHT
23 * @author Christian Grothoff
24 * @author Nathan Evans
27 #include "gnunet_dht_service.h"
28 #include "gnunet_hello_lib.h"
31 * The key for the query
33 static char *query_key;
36 * User supplied timeout value (in seconds)
38 static unsigned long long timeout_request = 5;
41 * When this request should really die
43 struct GNUNET_TIME_Absolute absolute_timeout;
53 static struct GNUNET_DHT_Handle *dht_handle;
56 * Global handle of the scheduler
58 static struct GNUNET_SCHEDULER_Handle *sched;
61 * Global handle of the configuration
63 static const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Handle for the request
68 static struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
71 * Count of results found
73 static unsigned int result_count;
81 shutdown_task (void *cls,
82 const struct GNUNET_SCHEDULER_TaskContext *tc)
84 if (dht_handle != NULL)
86 GNUNET_DHT_disconnect (dht_handle);
90 _("Found %u peers\n"),
96 cleanup_task (void *cls,
97 const struct GNUNET_SCHEDULER_TaskContext *tc)
99 if (find_peer_handle != NULL)
101 GNUNET_DHT_find_peer_stop (find_peer_handle);
102 find_peer_handle = NULL;
104 GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
108 * Iterator called on each result obtained from a find peer
111 * @param cls closure (NULL)
112 * @param hello the response message, a HELLO
115 find_peer_processor (void *cls,
116 const struct GNUNET_HELLO_Message *hello)
118 struct GNUNET_PeerIdentity peer;
120 if (GNUNET_OK == GNUNET_HELLO_get_id(hello, &peer))
125 _("Found peer `%s'\n"),
132 * Main function that will be run by the scheduler.
135 * @param s the scheduler to use
136 * @param args remaining command-line arguments
137 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
138 * @param c configuration
142 struct GNUNET_SCHEDULER_Handle *s,
144 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
146 struct GNUNET_TIME_Relative timeout;
151 if (query_key == NULL)
154 fprintf (stderr, "Must provide key for DHT GET!\n");
159 dht_handle = GNUNET_DHT_connect (sched, cfg, 1);
161 if (dht_handle == NULL)
164 fprintf (stderr, "Couldn't connect to DHT service!\n");
169 fprintf (stderr, "Connected to DHT service!\n");
171 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
174 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
175 absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout);
178 fprintf (stderr, "Issuing FIND PEER request for %s!\n", query_key);
180 find_peer_handle = GNUNET_DHT_find_peer_start (dht_handle,
184 &find_peer_processor,
186 if (NULL == find_peer_handle)
188 GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
191 GNUNET_SCHEDULER_add_delayed (sched,
192 GNUNET_TIME_absolute_get_remaining
194 &cleanup_task, NULL);
199 * gnunet-dht-get command line options
201 static struct GNUNET_GETOPT_CommandLineOption options[] = {
203 gettext_noop ("the query key"),
204 1, &GNUNET_GETOPT_set_string, &query_key},
205 {'T', "timeout", "TIMEOUT",
206 gettext_noop ("how long to execute this query before giving up?"),
207 1, &GNUNET_GETOPT_set_ulong, &timeout_request},
208 {'V', "verbose", NULL,
209 gettext_noop ("be verbose (print progress information)"),
210 0, &GNUNET_GETOPT_set_one, &verbose},
211 GNUNET_GETOPT_OPTION_END
216 * Entry point for gnunet-dht-get-peer
218 * @param argc number of arguments from the command line
219 * @param argv command line arguments
220 * @return 0 ok, 1 on error
223 main (int argc, char *const *argv)
226 GNUNET_PROGRAM_run (argc,
228 "gnunet-dht-get-peer",
230 ("Issue a GET PEER request to the GNUnet DHT, print results."),
231 options, &run, NULL)) ? ret : 1;
234 /* end of gnunet-dht-get-peer */