list-keys.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "platform.h"
  2. #include "gnunet_util_lib.h"
  3. #include "gnunet_testing_lib.h"
  4. static unsigned int nkeys;
  5. static unsigned int nskip;
  6. static int result;
  7. /**
  8. * Main run function.
  9. *
  10. * @param cls NULL
  11. * @param args arguments passed to GNUNET_PROGRAM_run
  12. * @param cfgfile the path to configuration file
  13. * @param cfg the configuration file handle
  14. */
  15. static void
  16. run (void *cls, char *const *args, const char *cfgfile,
  17. const struct GNUNET_CONFIGURATION_Handle *config)
  18. {
  19. char *idfile;
  20. struct GNUNET_DISK_FileHandle *f;
  21. void *data;
  22. struct GNUNET_DISK_MapHandle *map;
  23. struct GNUNET_CRYPTO_EddsaPrivateKey pkey;
  24. struct GNUNET_PeerIdentity id;
  25. unsigned int cnt;
  26. uint64_t fsize;
  27. unsigned int nmax;
  28. if ((NULL == args) || (NULL == args[0]))
  29. {
  30. FPRINTF (stderr, "Need the hostkey file\n");
  31. return;
  32. }
  33. idfile = args[0];
  34. if (GNUNET_OK !=
  35. GNUNET_DISK_file_size (idfile, &fsize, GNUNET_YES, GNUNET_YES))
  36. {
  37. GNUNET_break (0);
  38. return;
  39. }
  40. if (0 != (fsize % GNUNET_TESTING_HOSTKEYFILESIZE))
  41. {
  42. FPRINTF (stderr,
  43. _("Incorrect hostkey file format: %s\n"), idfile);
  44. return;
  45. }
  46. f = GNUNET_DISK_file_open (idfile, GNUNET_DISK_OPEN_READ,
  47. GNUNET_DISK_PERM_NONE);
  48. if (NULL == f)
  49. {
  50. GNUNET_break (0);
  51. return;
  52. }
  53. data = GNUNET_DISK_file_map (f, &map, GNUNET_DISK_MAP_TYPE_READ, fsize);
  54. if (NULL == data)
  55. {
  56. GNUNET_break (0);
  57. GNUNET_DISK_file_close (f);
  58. return;
  59. }
  60. nmax = fsize / GNUNET_TESTING_HOSTKEYFILESIZE;
  61. for (cnt = nskip; cnt < (nskip + nkeys); cnt++)
  62. {
  63. if (nskip + cnt >= nmax)
  64. {
  65. PRINTF ("Max keys %u reached\n", nmax);
  66. break;
  67. }
  68. GNUNET_memcpy (&pkey,
  69. data + (cnt * GNUNET_TESTING_HOSTKEYFILESIZE),
  70. GNUNET_TESTING_HOSTKEYFILESIZE);
  71. GNUNET_CRYPTO_eddsa_key_get_public (&pkey, &id.public_key);
  72. PRINTF ("Key %u: %s\n", cnt, GNUNET_i2s_full (&id));
  73. }
  74. result = GNUNET_OK;
  75. GNUNET_DISK_file_unmap (map);
  76. GNUNET_DISK_file_close (f);
  77. }
  78. int main (int argc, char *argv[])
  79. {
  80. struct GNUNET_GETOPT_CommandLineOption option[] = {
  81. GNUNET_GETOPT_option_uint ('n',
  82. "num-keys",
  83. "COUNT",
  84. gettext_noop ("list COUNT number of keys"),
  85. &nkeys),
  86. GNUNET_GETOPT_OPTION_END
  87. };
  88. int ret;
  89. result = GNUNET_SYSERR;
  90. nkeys = 10;
  91. ret =
  92. GNUNET_PROGRAM_run (argc, argv, "list-keys", "Lists the peer IDs corresponding to the given keys file\n",
  93. option, &run, NULL);
  94. if (GNUNET_OK != ret)
  95. return 1;
  96. if (GNUNET_SYSERR == result)
  97. return 1;
  98. return 0;
  99. }