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