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 gns/gnunet-gns-lookup.c
22 * @brief search for records in GNS
23 * @author Martin Schanzenbach
26 #include "gnunet_gns_service.h"
29 * The type of the query
31 static unsigned int query_type;
34 * Desired replication level
36 static unsigned int replication = 5;
39 * The key for the query
41 static char *query_key;
44 * User supplied timeout value (in seconds)
46 static unsigned long long timeout_request = 5;
49 * When this request should really die
51 struct GNUNET_TIME_Absolute absolute_timeout;
61 static struct GNUNET_GNS_Handle *gns_handle;
64 * Global handle of the configuration
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Handle for the lookup request
71 static struct GNUNET_GNS_LookupHandle *lookup_handle;
74 * Count of results found
76 static unsigned int result_count;
85 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 if (gns_handle != NULL)
89 GNUNET_GNS_disconnect (gns_handle);
96 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 if (lookup_handle != NULL)
100 GNUNET_GNS_lookup_stop (lookup_handle);
101 lookup_handle = NULL;
103 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
108 * Iterator called on each result obtained for a GNS
109 * operation that expects a reply
112 * @param exp when will this value expire
113 * @param key key of the result
114 * @param get_path peers on reply path (or NULL if not recorded)
115 * @param get_path_length number of entries in get_path
116 * @param put_path peers on the PUT path (or NULL if not recorded)
117 * @param put_path_length number of entries in get_path
118 * @param type type of the result
119 * @param size number of bytes in data
120 * @param data pointer to the result data
123 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
124 const GNUNET_HashCode * key,
125 const struct GNUNET_PeerIdentity *get_path,
126 unsigned int get_path_length,
127 const struct GNUNET_PeerIdentity *put_path,
128 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
129 size_t size, const void *data)
131 FPRINTF (stdout, "Result %d, type %d:\n%.*s\n", result_count, type,
132 (unsigned int) size, (char *) data);
138 * Main function that will be run by the scheduler.
141 * @param args remaining command-line arguments
142 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
143 * @param c configuration
146 run (void *cls, char *const *args, const char *cfgfile,
147 const struct GNUNET_CONFIGURATION_Handle *c)
149 struct GNUNET_TIME_Relative timeout;
154 if (query_key == NULL)
157 FPRINTF (stderr, "%s", "Must provide key for GNS lookup!\n");
162 lookup_handle = GNUNET_GNS_connect (cfg, 1);
164 if (lookup_handle == NULL)
167 FPRINTF (stderr, "%s", "Couldn't connect to GNS service!\n");
172 FPRINTF (stderr, "%s", "Connected to GNS service!\n");
174 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
175 query_type = GNUNET_BLOCK_TYPE_TEST;
177 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
180 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
181 absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout);
184 FPRINTF (stderr, "Issuing lookup request for %s!\n", query_key);
185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
186 (absolute_timeout), &cleanup_task, NULL);
188 GNUNET_GNS_lookup_start (lookup_handle, timeout, query_type, &key, replication,
189 GNUNET_DHT_RO_NONE, NULL, 0, &get_result_iterator,
196 * gnunet-dht-get command line options
198 static struct GNUNET_GETOPT_CommandLineOption options[] = {
200 gettext_noop ("the query key"),
201 1, &GNUNET_GETOPT_set_string, &query_key},
202 {'r', "replication", "LEVEL",
203 gettext_noop ("how many parallel requests (replicas) to create"),
204 1, &GNUNET_GETOPT_set_uint, &replication},
205 {'t', "type", "TYPE",
206 gettext_noop ("the type of data to look for"),
207 1, &GNUNET_GETOPT_set_uint, &query_type},
208 {'T', "timeout", "TIMEOUT",
209 gettext_noop ("how long to execute this query before giving up?"),
210 1, &GNUNET_GETOPT_set_ulong, &timeout_request},
211 {'V', "verbose", NULL,
212 gettext_noop ("be verbose (print progress information)"),
213 0, &GNUNET_GETOPT_set_one, &verbose},
214 GNUNET_GETOPT_OPTION_END
219 * Entry point for gnunet-gns-lookup
221 * @param argc number of arguments from the command line
222 * @param argv command line arguments
223 * @return 0 ok, 1 on error
226 main (int argc, char *const *argv)
229 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-get",
231 ("Issue a request to the GNUnet Naming System, prints results."),
232 options, &run, NULL)) ? ret : 1;
235 /* end of gnunet-gns-lookup.c */