aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gnunet-gns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gns/gnunet-gns.c')
-rw-r--r--src/gns/gnunet-gns.c198
1 files changed, 164 insertions, 34 deletions
diff --git a/src/gns/gnunet-gns.c b/src/gns/gnunet-gns.c
index c1944c1e2..881b88810 100644
--- a/src/gns/gnunet-gns.c
+++ b/src/gns/gnunet-gns.c
@@ -21,15 +21,20 @@
21 * @file gnunet-gns.c 21 * @file gnunet-gns.c
22 * @brief command line tool to access distributed GNS 22 * @brief command line tool to access distributed GNS
23 * @author Christian Grothoff 23 * @author Christian Grothoff
24 *
25 */ 24 */
26#include "platform.h" 25#include "platform.h"
27#include <gnunet_util_lib.h> 26#include <gnunet_util_lib.h>
28#include <gnunet_dnsparser_lib.h> 27#include <gnunet_dnsparser_lib.h>
28#include <gnunet_identity_service.h>
29#include <gnunet_namestore_service.h> 29#include <gnunet_namestore_service.h>
30#include <gnunet_gns_service.h> 30#include <gnunet_gns_service.h>
31 31
32/** 32/**
33 * Configuration we are using.
34 */
35static const struct GNUNET_CONFIGURATION_Handle *cfg;
36
37/**
33 * Handle to GNS service. 38 * Handle to GNS service.
34 */ 39 */
35static struct GNUNET_GNS_Handle *gns; 40static struct GNUNET_GNS_Handle *gns;
@@ -45,6 +50,16 @@ static char *lookup_name;
45static char *lookup_type; 50static char *lookup_type;
46 51
47/** 52/**
53 * Identity of the zone to use for the lookup (-z option)
54 */
55static char *zone_ego_name;
56
57/**
58 * Public key of the zone to use for the lookup (-p option)
59 */
60static char *public_key;
61
62/**
48 * raw output 63 * raw output
49 */ 64 */
50static int raw; 65static int raw;
@@ -59,6 +74,11 @@ static int rtype;
59 */ 74 */
60static struct GNUNET_GNS_LookupRequest *lookup_request; 75static struct GNUNET_GNS_LookupRequest *lookup_request;
61 76
77/**
78 * Handle to the identity service.
79 */
80static struct GNUNET_IDENTITY_Handle *identity;
81
62 82
63/** 83/**
64 * Task run on shutdown. Cleans up everything. 84 * Task run on shutdown. Cleans up everything.
@@ -70,6 +90,11 @@ static void
70do_shutdown (void *cls, 90do_shutdown (void *cls,
71 const struct GNUNET_SCHEDULER_TaskContext *tc) 91 const struct GNUNET_SCHEDULER_TaskContext *tc)
72{ 92{
93 if (NULL != identity)
94 {
95 GNUNET_IDENTITY_disconnect (identity);
96 identity = NULL;
97 }
73 if (NULL != lookup_request) 98 if (NULL != lookup_request)
74 { 99 {
75 GNUNET_GNS_lookup_cancel (lookup_request); 100 GNUNET_GNS_lookup_cancel (lookup_request);
@@ -128,43 +153,17 @@ process_lookup_result (void *cls, uint32_t rd_count,
128 153
129 154
130/** 155/**
131 * Main function that will be run. 156 * Perform the actual resolution, starting with the zone
157 * identified by the given public key.
132 * 158 *
133 * @param cls closure 159 * @param pkey public key to use for the zone
134 * @param args remaining command-line arguments
135 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
136 * @param cfg configuration
137 */ 160 */
138static void 161static void
139run (void *cls, char *const *args, const char *cfgfile, 162lookup_with_public_key (const struct GNUNET_CRYPTO_EccPublicKey *pkey)
140 const struct GNUNET_CONFIGURATION_Handle *cfg)
141{ 163{
142 char *keyfile; 164 char *keyfile;
143 struct GNUNET_CRYPTO_EccPrivateKey *key;
144 struct GNUNET_CRYPTO_EccPublicKey pkey;
145 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key; 165 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
146 166
147 gns = GNUNET_GNS_connect (cfg);
148 if (NULL == gns)
149 {
150 fprintf (stderr,
151 _("Failed to connect to GNS\n"));
152 return;
153 }
154 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
155 "ZONEKEY", &keyfile))
156 {
157 fprintf (stderr,
158 "Need zone to perform lookup in!\n");
159 GNUNET_SCHEDULER_shutdown ();
160 return;
161 }
162 /* FIXME: use identity service and/or allow user to specify public key! */
163 key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
164 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
165 GNUNET_free (key);
166 GNUNET_free (keyfile);
167
168 if (GNUNET_OK != 167 if (GNUNET_OK !=
169 GNUNET_CONFIGURATION_get_value_filename (cfg, "gns", 168 GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
170 "SHORTEN_ZONEKEY", &keyfile)) 169 "SHORTEN_ZONEKEY", &keyfile))
@@ -187,7 +186,7 @@ run (void *cls, char *const *args, const char *cfgfile,
187 { 186 {
188 lookup_request = GNUNET_GNS_lookup (gns, 187 lookup_request = GNUNET_GNS_lookup (gns,
189 lookup_name, 188 lookup_name,
190 &pkey, 189 pkey,
191 rtype, 190 rtype,
192 GNUNET_NO, /* Use DHT */ 191 GNUNET_NO, /* Use DHT */
193 shorten_key, 192 shorten_key,
@@ -201,10 +200,135 @@ run (void *cls, char *const *args, const char *cfgfile,
201 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 200 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
202 return; 201 return;
203 } 202 }
204 if (NULL != shorten_key) 203 GNUNET_free_non_null (shorten_key);
205 GNUNET_free (shorten_key); 204}
205
206
207/**
208 * Method called to inform about the egos of this peer.
209 *
210 * When used with #GNUNET_IDENTITY_connect, this function is
211 * initially called for all egos and then again whenever a
212 * ego's name changes or if it is deleted. At the end of
213 * the initial pass over all egos, the function is once called
214 * with 'NULL' for @a ego. That does NOT mean that the callback won't
215 * be invoked in the future or that there was an error.
216 *
217 * If the @a name matches the `zone_ego_name`, we found the zone
218 * for our computation and will begin resolving against that zone.
219 * If we have iterated over all egos and not found the name, we
220 * terminate the program with an error message.
221 *
222 * @param cls closure (NULL, unused)
223 * @param ego ego handle
224 * @param ego_ctx context for application to store data for this ego
225 * (during the lifetime of this process, initially NULL)
226 * @param name name assigned by the user for this ego,
227 * NULL if the user just deleted the ego and it
228 * must thus no longer be used
229 */
230static void
231identity_cb (void *cls,
232 struct GNUNET_IDENTITY_Ego *ego,
233 void **ctx,
234 const char *name)
235{
236 struct GNUNET_CRYPTO_EccPublicKey pkey;
237
238 if ( (NULL != zone_ego_name) &&
239 (NULL != name) &&
240 (0 == strcmp (name,
241 zone_ego_name)) )
242 {
243 GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
244 lookup_with_public_key (&pkey);
245 GNUNET_free (zone_ego_name);
246 zone_ego_name = NULL;
247 GNUNET_IDENTITY_disconnect (identity);
248 identity = NULL;
249 return;
250 }
251 if ( (NULL == ego) &&
252 (NULL != identity) &&
253 (NULL != zone_ego_name) )
254 {
255 fprintf (stderr,
256 _("Ego `%s' not found\n"),
257 zone_ego_name);
258 GNUNET_free (zone_ego_name);
259 zone_ego_name = NULL;
260 GNUNET_IDENTITY_disconnect (identity);
261 identity = NULL;
262 GNUNET_SCHEDULER_shutdown ();
263 return;
264 }
265}
266
267
268/**
269 * Main function that will be run.
270 *
271 * @param cls closure
272 * @param args remaining command-line arguments
273 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
274 * @param c configuration
275 */
276static void
277run (void *cls, char *const *args, const char *cfgfile,
278 const struct GNUNET_CONFIGURATION_Handle *c)
279{
280 struct GNUNET_CRYPTO_EccPublicKey pkey;
281
282 cfg = c;
283 gns = GNUNET_GNS_connect (cfg);
284 if (NULL == gns)
285 {
286 fprintf (stderr,
287 _("Failed to connect to GNS\n"));
288 return;
289 }
206 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 290 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
207 &do_shutdown, NULL); 291 &do_shutdown, NULL);
292 if (NULL != public_key)
293 {
294 if (GNUNET_OK !=
295 GNUNET_CRYPTO_ecc_public_key_from_string (public_key,
296 strlen (public_key),
297 &pkey))
298 {
299 fprintf (stderr,
300 _("Public key `%s' is not well-formed\n"),
301 public_key);
302 GNUNET_SCHEDULER_shutdown ();
303 return;
304 }
305 lookup_with_public_key (&pkey);
306 return;
307 }
308 if (NULL != zone_ego_name)
309 {
310 identity = GNUNET_IDENTITY_connect (cfg,
311 &identity_cb,
312 NULL);
313 return;
314 }
315 if ( (NULL != lookup_name) &&
316 (strlen (lookup_name) > 4) &&
317 (0 == strcmp (".zkey",
318 &lookup_name[strlen (lookup_name) - 4])) )
319 {
320 /* no zone required, use 'anonymous' zone */
321 GNUNET_CRYPTO_ecc_key_get_public (GNUNET_CRYPTO_ecc_key_get_anonymous (),
322 &pkey);
323 lookup_with_public_key (&pkey);
324 }
325 else
326 {
327 fprintf (stderr,
328 _("I need a zone (`-p' or `-z' option) to resolve this name\n"));
329 GNUNET_SCHEDULER_shutdown ();
330 return;
331 }
208} 332}
209 333
210 334
@@ -228,6 +352,12 @@ main (int argc, char *const *argv)
228 {'r', "raw", NULL, 352 {'r', "raw", NULL,
229 gettext_noop ("No unneeded output"), 0, 353 gettext_noop ("No unneeded output"), 0,
230 &GNUNET_GETOPT_set_one, &raw}, 354 &GNUNET_GETOPT_set_one, &raw},
355 {'p', "public-key", "PKEY",
356 gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
357 &GNUNET_GETOPT_set_string, &public_key},
358 {'z', "zone", "NAME",
359 gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
360 &GNUNET_GETOPT_set_string, &zone_ego_name},
231 GNUNET_GETOPT_OPTION_END 361 GNUNET_GETOPT_OPTION_END
232 }; 362 };
233 int ret; 363 int ret;