diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-03-11 22:47:43 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-03-11 22:47:43 +0000 |
commit | 9f2d6b1be912f09e47c30b2ac7b79a9c5e7bdecc (patch) | |
tree | 6bda4694e539ba83eaf6369bfc36f5f3189c1500 /src/peerinfo-tool | |
parent | 5a60ea398453342651cae558c37bbe2d0ef2ab36 (diff) | |
download | gnunet-9f2d6b1be912f09e47c30b2ac7b79a9c5e7bdecc.tar.gz gnunet-9f2d6b1be912f09e47c30b2ac7b79a9c5e7bdecc.zip |
-cleaning up get uri code
Diffstat (limited to 'src/peerinfo-tool')
-rw-r--r-- | src/peerinfo-tool/gnunet-peerinfo.c | 157 |
1 files changed, 93 insertions, 64 deletions
diff --git a/src/peerinfo-tool/gnunet-peerinfo.c b/src/peerinfo-tool/gnunet-peerinfo.c index 915e86bc2..7867191b7 100644 --- a/src/peerinfo-tool/gnunet-peerinfo.c +++ b/src/peerinfo-tool/gnunet-peerinfo.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2001, 2002, 2003, 2004, 2006, 2009, 2010 Christian Grothoff (and other contributing authors) | 3 | (C) 2001-2012 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -72,6 +72,20 @@ struct PrintContext | |||
72 | 72 | ||
73 | 73 | ||
74 | /** | 74 | /** |
75 | * Context used for building our own URI. | ||
76 | */ | ||
77 | struct GetUriContext | ||
78 | { | ||
79 | /** | ||
80 | * Final URI. | ||
81 | */ | ||
82 | char *uri; | ||
83 | |||
84 | struct PrintContext *pc; | ||
85 | }; | ||
86 | |||
87 | |||
88 | /** | ||
75 | * FIXME. | 89 | * FIXME. |
76 | */ | 90 | */ |
77 | struct GNUNET_PEERINFO_HelloAddressParsingContext | 91 | struct GNUNET_PEERINFO_HelloAddressParsingContext |
@@ -197,6 +211,9 @@ dump_pc (struct PrintContext *pc) | |||
197 | } | 211 | } |
198 | 212 | ||
199 | 213 | ||
214 | /* ************************* list all known addresses **************** */ | ||
215 | |||
216 | |||
200 | /** | 217 | /** |
201 | * Function to call with a human-readable format of an address | 218 | * Function to call with a human-readable format of an address |
202 | * | 219 | * |
@@ -222,9 +239,9 @@ process_resolved_address (void *cls, const char *address) | |||
222 | 239 | ||
223 | 240 | ||
224 | /** | 241 | /** |
225 | * Iterator callback to go over all addresses. | 242 | * Iterator callback to go over all addresses and count them. |
226 | * | 243 | * |
227 | * @param cls closure | 244 | * @param cls 'struct PrintContext' with 'off' to increment |
228 | * @param address the address | 245 | * @param address the address |
229 | * @param expiration expiration time | 246 | * @param expiration expiration time |
230 | * @return GNUNET_OK to keep the address and continue | 247 | * @return GNUNET_OK to keep the address and continue |
@@ -302,23 +319,28 @@ print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer, | |||
302 | } | 319 | } |
303 | 320 | ||
304 | 321 | ||
322 | /* ************************* GET URI ************************** */ | ||
305 | 323 | ||
306 | 324 | ||
307 | 325 | /** | |
308 | 326 | * Function that is called on each address of this peer. | |
309 | 327 | * Expands the corresponding URI string. | |
310 | 328 | * | |
311 | 329 | * @param cls the 'GetUriContext' | |
312 | 330 | * @param address address to add | |
313 | 331 | * @param expiration expiration time for the address | |
314 | 332 | * @return GNUNET_OK (continue iteration). | |
333 | */ | ||
315 | static int | 334 | static int |
316 | compose_uri (void *cls, const struct GNUNET_HELLO_Address *address, | 335 | compose_uri (void *cls, const struct GNUNET_HELLO_Address *address, |
317 | struct GNUNET_TIME_Absolute expiration) | 336 | struct GNUNET_TIME_Absolute expiration) |
318 | { | 337 | { |
319 | struct PrintContext *pc = cls; | 338 | struct GetUriContext *guc = cls; |
320 | struct GNUNET_TRANSPORT_PluginFunctions *papi; | 339 | struct GNUNET_TRANSPORT_PluginFunctions *papi; |
321 | static const char *addr; | 340 | const char *addr; |
341 | char *ret; | ||
342 | struct tm *t; | ||
343 | time_t seconds; | ||
322 | 344 | ||
323 | papi = GPI_plugins_find (address->transport_name); | 345 | papi = GPI_plugins_find (address->transport_name); |
324 | if (papi == NULL) | 346 | if (papi == NULL) |
@@ -326,64 +348,71 @@ compose_uri (void *cls, const struct GNUNET_HELLO_Address *address, | |||
326 | /* Not an error - we might just not have the right plugin. */ | 348 | /* Not an error - we might just not have the right plugin. */ |
327 | return GNUNET_OK; | 349 | return GNUNET_OK; |
328 | } | 350 | } |
329 | 351 | if (NULL == papi->address_to_string) | |
330 | addr = papi->address_to_string (papi->cls, address->address, address->address_length); | ||
331 | if (addr != NULL) | ||
332 | { | 352 | { |
333 | ssize_t l = strlen (addr); | 353 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
334 | if (l > 0) | 354 | "URI conversion not implemented for plugin `%s'\n", |
335 | { | 355 | address->transport_name); |
336 | struct tm *t; | 356 | return GNUNET_OK; |
337 | time_t seconds; | ||
338 | int s; | ||
339 | seconds = expiration.abs_value / 1000; | ||
340 | t = gmtime(&seconds); | ||
341 | pc->uri = GNUNET_realloc (pc->uri, pc->uri_len + 1 + 14 + 1 + strlen (address->transport_name) + 1 + l + 1 /* 0 */); | ||
342 | s = sprintf (&pc->uri[pc->uri_len], "!%04u%02u%02u%02u%02u%02u!%s!%s", | ||
343 | t->tm_year, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, | ||
344 | address->transport_name, addr); | ||
345 | if (s > 0) | ||
346 | pc->uri_len += s; | ||
347 | } | ||
348 | } | 357 | } |
358 | addr = papi->address_to_string (papi->cls, address->address, address->address_length); | ||
359 | if ( (addr == NULL) || (strlen(addr) == 0) ) | ||
360 | return GNUNET_OK; | ||
361 | seconds = expiration.abs_value / 1000; | ||
362 | t = gmtime (&seconds); | ||
363 | GNUNET_asprintf (&ret, | ||
364 | "%s!%04u%02u%02u%02u%02u%02u!%s!%s", | ||
365 | guc->uri, | ||
366 | t->tm_year, | ||
367 | t->tm_mon, | ||
368 | t->tm_mday, | ||
369 | t->tm_hour, | ||
370 | t->tm_min, | ||
371 | t->tm_sec, | ||
372 | address->transport_name, | ||
373 | addr); | ||
374 | GNUNET_free (guc->uri); | ||
375 | guc->uri = ret; | ||
349 | return GNUNET_OK; | 376 | return GNUNET_OK; |
350 | } | 377 | } |
351 | 378 | ||
352 | 379 | ||
353 | /** | 380 | /** |
354 | * Print information about the peer. | 381 | * Print URI of the peer. |
382 | * | ||
383 | * @param cls the 'struct GetUriContext' | ||
384 | * @param peer identity of the peer (unused) | ||
385 | * @param hello addresses of the peer | ||
386 | * @param err_msg error message | ||
355 | */ | 387 | */ |
356 | static void | 388 | static void |
357 | print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer, | 389 | print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer, |
358 | const struct GNUNET_HELLO_Message *hello, const char *err_msg) | 390 | const struct GNUNET_HELLO_Message *hello, |
391 | const char *err_msg) | ||
359 | { | 392 | { |
360 | struct GNUNET_CRYPTO_HashAsciiEncoded enc; | 393 | struct GetUriContext *guc = cls; |
361 | struct PrintContext *pc = cls; | ||
362 | 394 | ||
363 | if (peer == NULL) | 395 | if (peer == NULL) |
364 | { | 396 | { |
365 | if (err_msg != NULL) | 397 | if (err_msg != NULL) |
366 | FPRINTF (stderr, "%s", _("Error in communication with PEERINFO service\n")); | 398 | FPRINTF (stderr, |
367 | GNUNET_free (pc->uri); | 399 | _("Error in communication with PEERINFO service: %s\n"), |
368 | return; | 400 | err_msg); |
369 | } | 401 | } |
370 | if ((be_quiet) || (NULL == hello)) | 402 | else |
371 | { | 403 | { |
372 | GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc); | 404 | if (NULL != hello) |
373 | printf ("%s\n", (const char *) &enc); | 405 | GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, guc); |
374 | if (be_quiet && get_uri != GNUNET_YES) | 406 | printf ("%s\n", (const char *) guc->uri); |
375 | return; | ||
376 | } | 407 | } |
377 | pc->peer = *peer; | 408 | GNUNET_free (guc->uri); |
378 | if (NULL != hello) | 409 | GNUNET_free (guc); |
379 | { | 410 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); |
380 | GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc); | ||
381 | GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, pc); | ||
382 | } | ||
383 | printf ("%s\n", pc->uri); | ||
384 | } | 411 | } |
385 | 412 | ||
386 | 413 | ||
414 | /* ************************* import HELLO by URI ********************* */ | ||
415 | |||
387 | 416 | ||
388 | static size_t | 417 | static size_t |
389 | add_addr_to_hello (void *cls, size_t max, void *buffer) | 418 | add_addr_to_hello (void *cls, size_t max, void *buffer) |
@@ -592,6 +621,9 @@ parse_hello (const struct GNUNET_CONFIGURATION_Handle *c, | |||
592 | } | 621 | } |
593 | 622 | ||
594 | 623 | ||
624 | /* ************************ Main state machine ********************* */ | ||
625 | |||
626 | |||
595 | /** | 627 | /** |
596 | * Main state machine that goes over all options and | 628 | * Main state machine that goes over all options and |
597 | * runs the next requested function. | 629 | * runs the next requested function. |
@@ -671,6 +703,8 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
671 | } | 703 | } |
672 | GNUNET_free (fn); | 704 | GNUNET_free (fn); |
673 | GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key); | 705 | GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key); |
706 | fprintf (stderr, "PK: `%s\n", | ||
707 | GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key)); | ||
674 | GNUNET_CRYPTO_rsa_key_free (priv); | 708 | GNUNET_CRYPTO_rsa_key_free (priv); |
675 | GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey); | 709 | GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey); |
676 | } | 710 | } |
@@ -725,25 +759,20 @@ state_machine (void *cls, | |||
725 | } | 759 | } |
726 | if (GNUNET_YES == get_uri) | 760 | if (GNUNET_YES == get_uri) |
727 | { | 761 | { |
728 | struct PrintContext *pc; | 762 | struct GetUriContext *guc; |
729 | char *pkey; | 763 | char *pkey; |
730 | ssize_t l; | ||
731 | ssize_t pl; | ||
732 | 764 | ||
733 | // FIXME... | 765 | guc = GNUNET_malloc (sizeof (struct GetUriContext)); |
734 | pc = GNUNET_malloc (sizeof (struct PrintContext)); | ||
735 | pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key); | 766 | pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key); |
736 | pl = strlen ("gnunet://hello/"); | 767 | GNUNET_asprintf (&guc->uri, |
737 | l = strlen (pkey) + pl; | 768 | "gnunet://hello/%s", |
738 | pc->uri = GNUNET_malloc (l + 1); | 769 | pkey); |
739 | strcpy (pc->uri, "gnunet://hello/"); | 770 | GNUNET_free (pkey); |
740 | strcpy (&pc->uri[pl], pkey); | ||
741 | pc->uri_len = l; | ||
742 | |||
743 | GPI_plugins_load (cfg); | 771 | GPI_plugins_load (cfg); |
744 | pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity, | 772 | pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity, |
745 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), | 773 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), |
746 | &print_my_uri, pc); | 774 | &print_my_uri, guc); |
775 | get_uri = GNUNET_NO; | ||
747 | return; | 776 | return; |
748 | } | 777 | } |
749 | GNUNET_SCHEDULER_shutdown (); | 778 | GNUNET_SCHEDULER_shutdown (); |