-fix
[oweals/gnunet.git] / src / gns / gnunet-gns-lookup.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 gns/gnunet-gns-lookup.c
22  * @brief search for records in GNS
23  * @author Martin Schanzenbach
24  */
25 #include "platform.h"
26 #include "gnunet_gns_service.h"
27
28 /**
29  * The type of the query
30  */
31 static unsigned int query_type;
32
33 /**
34  * Desired replication level
35  */
36 static unsigned int replication = 5;
37
38 /**
39  * The key for the query
40  */
41 static char *query_key;
42
43 /**
44  * User supplied timeout value (in seconds)
45  */
46 static unsigned long long timeout_request = 5;
47
48 /**
49  * When this request should really die
50  */
51 struct GNUNET_TIME_Absolute absolute_timeout;
52
53 /**
54  * Be verbose
55  */
56 static int verbose;
57
58 /**
59  * Handle to the GNS
60  */
61 static struct GNUNET_GNS_Handle *gns_handle;
62
63 /**
64  * Global handle of the configuration
65  */
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67
68 /**
69  * Handle for the lookup request
70  */
71 static struct GNUNET_GNS_LookupHandle *lookup_handle;
72
73 /**
74  * Global status value
75  */
76 static int ret;
77
78
79 static void
80 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 {
82   if (gns_handle != NULL)
83   {
84     GNUNET_GNS_disconnect (gns_handle);
85     gns_handle = NULL;
86   }
87 }
88
89
90 static void
91 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 {
93   if (lookup_handle != NULL)
94   {
95     GNUNET_GNS_lookup_stop (lookup_handle);
96     lookup_handle = NULL;
97   }
98   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
99 }
100
101
102 /**
103  * Iterator called on each result obtained for a GNS
104  * operation that expects a reply
105  *
106  * @param cls closure
107  * @param exp when will this value expire
108  * @param key key of the result
109  * @param get_path peers on reply path (or NULL if not recorded)
110  * @param get_path_length number of entries in get_path
111  * @param put_path peers on the PUT path (or NULL if not recorded)
112  * @param put_path_length number of entries in get_path
113  * @param type type of the result
114  * @param size number of bytes in data
115  * @param data pointer to the result data
116  */
117 static void
118 lookup_result_iterator (void *cls,
119                         const char * name,
120                         const struct GNUNET_GNS_Record *record,
121                         unsigned int num_records)
122 {
123   FPRINTF (stdout, "%d results for %s\n", num_records, name);
124 }
125
126
127 /**
128  * Main function that will be run by the scheduler.
129  *
130  * @param cls closure
131  * @param args remaining command-line arguments
132  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
133  * @param c configuration
134  */
135 static void
136 run (void *cls, char *const *args, const char *cfgfile,
137      const struct GNUNET_CONFIGURATION_Handle *c)
138 {
139   struct GNUNET_TIME_Relative timeout;
140
141   cfg = c;
142
143   if (query_key == NULL)
144   {
145     if (verbose)
146       FPRINTF (stderr, "%s",  "Must provide key for GNS lookup!\n");
147     ret = 1;
148     return;
149   }
150
151   gns_handle = GNUNET_GNS_connect (cfg, 1);
152
153   if (gns_handle == NULL)
154   {
155     if (verbose)
156       FPRINTF (stderr, "%s",  "Couldn't connect to GNS service!\n");
157     ret = 1;
158     return;
159   }
160   else if (verbose)
161     FPRINTF (stderr, "%s",  "Connected to GNS service!\n");
162
163   timeout =
164       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
165   absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout);
166
167   if (verbose)
168     FPRINTF (stderr, "Issuing lookup request for %s!\n", query_key);
169   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
170                                 (absolute_timeout), &cleanup_task, NULL);
171   lookup_handle =
172       GNUNET_GNS_lookup_start (gns_handle, timeout, query_key,
173                                0/*GNS_RecordType*/,
174                                &lookup_result_iterator,
175                                NULL);
176
177 }
178
179
180 /**
181  * gnunet-dht-get command line options
182  */
183 static struct GNUNET_GETOPT_CommandLineOption options[] = {
184   {'k', "key", "KEY",
185    gettext_noop ("the query key"),
186    1, &GNUNET_GETOPT_set_string, &query_key},
187   {'r', "replication", "LEVEL",
188    gettext_noop ("how many parallel requests (replicas) to create"),
189    1, &GNUNET_GETOPT_set_uint, &replication},
190   {'t', "type", "TYPE",
191    gettext_noop ("the type of data to look for"),
192    1, &GNUNET_GETOPT_set_uint, &query_type},
193   {'T', "timeout", "TIMEOUT",
194    gettext_noop ("how long to execute this query before giving up?"),
195    1, &GNUNET_GETOPT_set_ulong, &timeout_request},
196   {'V', "verbose", NULL,
197    gettext_noop ("be verbose (print progress information)"),
198    0, &GNUNET_GETOPT_set_one, &verbose},
199   GNUNET_GETOPT_OPTION_END
200 };
201
202
203 /**
204  * Entry point for gnunet-gns-lookup
205  *
206  * @param argc number of arguments from the command line
207  * @param argv command line arguments
208  * @return 0 ok, 1 on error
209  */
210 int
211 main (int argc, char *const *argv)
212 {
213   return (GNUNET_OK ==
214           GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-get",
215                               gettext_noop
216                               ("Issue a request to the GNUnet Naming System, prints results."),
217                               options, &run, NULL)) ? ret : 1;
218 }
219
220 /* end of gnunet-gns-lookup.c */