gnunet-namecache.c 6.0 KB

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