tolerate additional IPv4 address now available for gnunet.org
[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  * Main run function.
11  *
12  * @param cls NULL
13  * @param args arguments passed to GNUNET_PROGRAM_run
14  * @param cfgfile the path to configuration file
15  * @param cfg the configuration file handle
16  */
17 static void
18 run (void *cls, char *const *args, 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
31   if ((NULL == args) || (NULL == args[0]))
32   {
33     FPRINTF (stderr, "Need the hostkey file\n");
34     return;
35   }
36   idfile = args[0];
37   if (GNUNET_OK !=
38       GNUNET_DISK_file_size (idfile, &fsize, GNUNET_YES, GNUNET_YES))
39   {
40     GNUNET_break (0);
41     return;
42   }
43   if (0 != (fsize % GNUNET_TESTING_HOSTKEYFILESIZE))
44   {
45     FPRINTF (stderr,
46              _("Incorrect hostkey file format: %s\n"), idfile);
47     return;
48   }
49   f = GNUNET_DISK_file_open (idfile, GNUNET_DISK_OPEN_READ,
50                              GNUNET_DISK_PERM_NONE);
51   if (NULL == f)
52   {
53     GNUNET_break (0);
54     return;
55   }
56   data = GNUNET_DISK_file_map (f, &map, GNUNET_DISK_MAP_TYPE_READ, fsize);
57   if (NULL == data)
58   {
59     GNUNET_break (0);
60     GNUNET_DISK_file_close (f);
61     return;
62   }
63   nmax = fsize / GNUNET_TESTING_HOSTKEYFILESIZE;
64   for (cnt = nskip; cnt < (nskip + nkeys); cnt++)
65   {
66     if (nskip + cnt >= nmax)
67     {
68       PRINTF ("Max keys %u reached\n", nmax);
69       break;
70     }
71     GNUNET_memcpy (&pkey,
72                    data + (cnt * GNUNET_TESTING_HOSTKEYFILESIZE),
73                    GNUNET_TESTING_HOSTKEYFILESIZE);
74     GNUNET_CRYPTO_eddsa_key_get_public (&pkey, &id.public_key);
75     PRINTF ("Key %u: %s\n", cnt, GNUNET_i2s_full (&id));
76   }
77   result = GNUNET_OK;
78   GNUNET_DISK_file_unmap (map);
79   GNUNET_DISK_file_close (f);
80 }
81
82
83 int main (int argc, char *argv[])
84 {
85   struct GNUNET_GETOPT_CommandLineOption option[] = {
86     GNUNET_GETOPT_option_uint ('n',
87                                    "num-keys",
88                                    "COUNT",
89                                    gettext_noop ("list COUNT number of keys"),
90                                    &nkeys),
91     GNUNET_GETOPT_OPTION_END
92   };
93   int ret;
94
95   result = GNUNET_SYSERR;
96   nkeys = 10;
97   ret =
98       GNUNET_PROGRAM_run (argc, argv, "list-keys", "Lists the peer IDs corresponding to the given keys file\n",
99                           option, &run, NULL);
100   if (GNUNET_OK != ret)
101     return 1;
102   if (GNUNET_SYSERR == result)
103     return 1;
104   return 0;
105 }