list-keys.c 2.8 KB

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