-fix URIs
[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     (void) memcpy (&pkey, data + (cnt * GNUNET_TESTING_HOSTKEYFILESIZE),
76                    GNUNET_TESTING_HOSTKEYFILESIZE);
77     GNUNET_CRYPTO_eddsa_key_get_public (&pkey, &id.public_key);
78     PRINTF ("Key %u: %s\n", cnt, GNUNET_i2s_full (&id));
79   }
80   result = GNUNET_OK;
81   GNUNET_DISK_file_unmap (map);
82   GNUNET_DISK_file_close (f);
83 }
84
85
86 int main (int argc, char *argv[])
87 {
88   struct GNUNET_GETOPT_CommandLineOption option[] = {
89     {'n', "num-keys", "COUNT",
90      gettext_noop ("list COUNT number of keys"),
91      GNUNET_YES, &GNUNET_GETOPT_set_uint, &nkeys},
92     {'s', "skip", "COUNT",
93      gettext_noop ("skip COUNT number of keys in the beginning"),
94      GNUNET_YES, &GNUNET_GETOPT_set_uint, &nskip},
95     GNUNET_GETOPT_OPTION_END
96   };
97   int ret;
98
99   result = GNUNET_SYSERR;
100   nkeys = 10;
101   ret = 
102       GNUNET_PROGRAM_run (argc, argv, "list-keys", "Lists the peer IDs corresponding to the given keys file\n",
103                           option, &run, NULL);
104   if (GNUNET_OK != ret)
105     return 1;
106   if (GNUNET_SYSERR == result)
107     return 1;
108   return 0;
109 }