summaryrefslogtreecommitdiff
path: root/src/testing/list-keys.c
blob: 39a48d071a9a0cb3c8a8d9d4df1e30894d270dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_testing_lib.h"

static unsigned int nkeys;
static unsigned int nskip;
static int result;

/**
 * Main run function.
 *
 * @param cls NULL
 * @param args arguments passed to GNUNET_PROGRAM_run
 * @param cfgfile the path to configuration file
 * @param cfg the configuration file handle
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *config)
{
  char *idfile;
  struct GNUNET_DISK_FileHandle *f;
  void *data;
  struct GNUNET_DISK_MapHandle *map;
  struct GNUNET_CRYPTO_EddsaPrivateKey pkey;
  struct GNUNET_PeerIdentity id;
  unsigned int cnt;
  uint64_t fsize;
  unsigned int nmax;

  if ((NULL == args) || (NULL == args[0]))
  {
    fprintf (stderr, "Need the hostkey file\n");
    return;
  }
  idfile = args[0];
  if (GNUNET_OK !=
      GNUNET_DISK_file_size (idfile, &fsize, GNUNET_YES, GNUNET_YES))
  {
    GNUNET_break (0);
    return;
  }
  if (0 != (fsize % GNUNET_TESTING_HOSTKEYFILESIZE))
  {
    fprintf (stderr, _ ("Incorrect hostkey file format: %s\n"), idfile);
    return;
  }
  f = GNUNET_DISK_file_open (idfile,
                             GNUNET_DISK_OPEN_READ,
                             GNUNET_DISK_PERM_NONE);
  if (NULL == f)
  {
    GNUNET_break (0);
    return;
  }
  data = GNUNET_DISK_file_map (f, &map, GNUNET_DISK_MAP_TYPE_READ, fsize);
  if (NULL == data)
  {
    GNUNET_break (0);
    GNUNET_DISK_file_close (f);
    return;
  }
  nmax = fsize / GNUNET_TESTING_HOSTKEYFILESIZE;
  for (cnt = nskip; cnt < (nskip + nkeys); cnt++)
  {
    if (nskip + cnt >= nmax)
    {
      printf ("Max keys %u reached\n", nmax);
      break;
    }
    GNUNET_memcpy (&pkey,
                   data + (cnt * GNUNET_TESTING_HOSTKEYFILESIZE),
                   GNUNET_TESTING_HOSTKEYFILESIZE);
    GNUNET_CRYPTO_eddsa_key_get_public (&pkey, &id.public_key);
    printf ("Key %u: %s\n", cnt, GNUNET_i2s_full (&id));
  }
  result = GNUNET_OK;
  GNUNET_DISK_file_unmap (map);
  GNUNET_DISK_file_close (f);
}


int
main (int argc, char *argv[])
{
  struct GNUNET_GETOPT_CommandLineOption option[] =
    {GNUNET_GETOPT_option_uint ('n',
                                "num-keys",
                                "COUNT",
                                gettext_noop ("list COUNT number of keys"),
                                &nkeys),
     GNUNET_GETOPT_OPTION_END};
  int ret;

  result = GNUNET_SYSERR;
  nkeys = 10;
  ret =
    GNUNET_PROGRAM_run (argc,
                        argv,
                        "list-keys",
                        "Lists the peer IDs corresponding to the given keys file\n",
                        option,
                        &run,
                        NULL);
  if (GNUNET_OK != ret)
    return 1;
  if (GNUNET_SYSERR == result)
    return 1;
  return 0;
}