gnunet-namecache.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2012, 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file gnunet-namecache.c
  19. * @brief command line tool to inspect the name cache
  20. * @author Christian Grothoff
  21. *
  22. * TODO:
  23. * - test
  24. */
  25. #include "platform.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_dnsparser_lib.h"
  28. #include "gnunet_identity_service.h"
  29. #include "gnunet_gnsrecord_lib.h"
  30. #include "gnunet_namecache_service.h"
  31. /**
  32. * Handle to the namecache.
  33. */
  34. static struct GNUNET_NAMECACHE_Handle *ns;
  35. /**
  36. * Queue entry for the 'query' operation.
  37. */
  38. static struct GNUNET_NAMECACHE_QueueEntry *qe;
  39. /**
  40. * Name (label) of the records to list.
  41. */
  42. static char *name;
  43. /**
  44. * Public key of the zone to look in.
  45. */
  46. static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
  47. /**
  48. * Public key of the zone to look in, in ASCII.
  49. */
  50. static char *pkey;
  51. /**
  52. * Global return value
  53. */
  54. static int ret;
  55. /**
  56. * Task run on shutdown. Cleans up everything.
  57. *
  58. * @param cls unused
  59. * @param tc scheduler context
  60. */
  61. static void
  62. do_shutdown (void *cls,
  63. const struct GNUNET_SCHEDULER_TaskContext *tc)
  64. {
  65. if (NULL != qe)
  66. {
  67. GNUNET_NAMECACHE_cancel (qe);
  68. qe = NULL;
  69. }
  70. if (NULL != ns)
  71. {
  72. GNUNET_NAMECACHE_disconnect (ns);
  73. ns = NULL;
  74. }
  75. }
  76. /**
  77. * Process a record that was stored in the namecache in a block.
  78. *
  79. * @param cls closure, NULL
  80. * @param rd_len number of entries in @a rd array
  81. * @param rd array of records with data to store
  82. */
  83. static void
  84. display_records_from_block (void *cls,
  85. unsigned int rd_len,
  86. const struct GNUNET_GNSRECORD_Data *rd)
  87. {
  88. const char *typestring;
  89. char *s;
  90. unsigned int i;
  91. if (0 == rd_len)
  92. {
  93. FPRINTF (stdout,
  94. _("No records found for `%s'"),
  95. name);
  96. return;
  97. }
  98. FPRINTF (stdout,
  99. "%s:\n",
  100. name);
  101. for (i=0;i<rd_len;i++)
  102. {
  103. typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
  104. s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  105. rd[i].data,
  106. rd[i].data_size);
  107. if (NULL == s)
  108. {
  109. FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
  110. (unsigned int) rd[i].record_type);
  111. continue;
  112. }
  113. FPRINTF (stdout,
  114. "\t%s: %s\n",
  115. typestring,
  116. s);
  117. GNUNET_free (s);
  118. }
  119. FPRINTF (stdout, "%s", "\n");
  120. }
  121. /**
  122. * Display block obtained from listing (by name).
  123. *
  124. * @param cls NULL
  125. * @param block NULL if not found
  126. */
  127. static void
  128. handle_block (void *cls,
  129. const struct GNUNET_GNSRECORD_Block *block)
  130. {
  131. qe = NULL;
  132. if (NULL == block)
  133. {
  134. fprintf (stderr,
  135. "No matching block found\n");
  136. }
  137. else if (GNUNET_OK !=
  138. GNUNET_GNSRECORD_block_decrypt (block,
  139. &pubkey,
  140. name,
  141. &display_records_from_block,
  142. NULL))
  143. {
  144. fprintf (stderr,
  145. "Failed to decrypt block!\n");
  146. }
  147. GNUNET_SCHEDULER_shutdown ();
  148. }
  149. /**
  150. * Main function that will be run.
  151. *
  152. * @param cls closure
  153. * @param args remaining command-line arguments
  154. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  155. * @param cfg configuration
  156. */
  157. static void
  158. run (void *cls, char *const *args, const char *cfgfile,
  159. const struct GNUNET_CONFIGURATION_Handle *cfg)
  160. {
  161. struct GNUNET_HashCode dhash;
  162. if (NULL == pkey)
  163. {
  164. fprintf (stderr,
  165. _("You must specify which zone should be accessed\n"));
  166. return;
  167. }
  168. if (GNUNET_OK !=
  169. GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
  170. strlen (pkey),
  171. &pubkey))
  172. {
  173. fprintf (stderr,
  174. _("Invalid public key for reverse lookup `%s'\n"),
  175. pkey);
  176. GNUNET_SCHEDULER_shutdown ();
  177. return;
  178. }
  179. if (NULL == name)
  180. {
  181. fprintf (stderr,
  182. _("You must specify a name\n"));
  183. return;
  184. }
  185. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  186. &do_shutdown,
  187. NULL);
  188. ns = GNUNET_NAMECACHE_connect (cfg);
  189. GNUNET_GNSRECORD_query_from_public_key (&pubkey,
  190. name,
  191. &dhash);
  192. qe = GNUNET_NAMECACHE_lookup_block (ns,
  193. &dhash,
  194. &handle_block,
  195. NULL);
  196. }
  197. /**
  198. * The main function for gnunet-namecache.
  199. *
  200. * @param argc number of arguments from the command line
  201. * @param argv command line arguments
  202. * @return 0 ok, 1 on error
  203. */
  204. int
  205. main (int argc, char *const *argv)
  206. {
  207. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  208. {'n', "name", "NAME",
  209. gettext_noop ("name of the record to add/delete/display"), 1,
  210. &GNUNET_GETOPT_set_string, &name},
  211. {'z', "zone", "PKEY",
  212. gettext_noop ("spezifies the public key of the zone to look in"), 1,
  213. &GNUNET_GETOPT_set_string, &pkey},
  214. GNUNET_GETOPT_OPTION_END
  215. };
  216. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  217. return 2;
  218. GNUNET_log_setup ("gnunet-namecache", "WARNING", NULL);
  219. if (GNUNET_OK !=
  220. GNUNET_PROGRAM_run (argc, argv, "gnunet-namecache",
  221. _("GNUnet zone manipulation tool"),
  222. options,
  223. &run, NULL))
  224. {
  225. GNUNET_free ((void*) argv);
  226. return 1;
  227. }
  228. GNUNET_free ((void*) argv);
  229. return ret;
  230. }
  231. /* end of gnunet-namecache.c */