aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2014-01-08 21:29:32 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2014-01-08 21:29:32 +0000
commit1c10ce20375a5dffb575cea7fd330a805ef3d7a1 (patch)
treeee46615716a1a292e3b341084c21851e97680404 /src/testing
parent7ab6edba583d8d09d2c830c9c5b6fe155d31b408 (diff)
downloadgnunet-1c10ce20375a5dffb575cea7fd330a805ef3d7a1.tar.gz
gnunet-1c10ce20375a5dffb575cea7fd330a805ef3d7a1.zip
- utility to list peer ids from a given hostkeys file
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/Makefile.am9
-rw-r--r--src/testing/list-keys.c109
2 files changed, 118 insertions, 0 deletions
diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am
index f27b46ed0..8657d42cb 100644
--- a/src/testing/Makefile.am
+++ b/src/testing/Makefile.am
@@ -30,6 +30,9 @@ libgnunettesting_la_LDFLAGS = \
30bin_PROGRAMS = \ 30bin_PROGRAMS = \
31 gnunet-testing 31 gnunet-testing
32 32
33noinst_PROGRAMS = \
34 list-keys
35
33gnunet_testing_SOURCES = \ 36gnunet_testing_SOURCES = \
34 gnunet-testing.c 37 gnunet-testing.c
35gnunet_testing_LDADD = \ 38gnunet_testing_LDADD = \
@@ -39,6 +42,12 @@ gnunet_testing_LDADD = \
39gnunet_testing_DEPENDENCIES = \ 42gnunet_testing_DEPENDENCIES = \
40 libgnunettesting.la 43 libgnunettesting.la
41 44
45list_keys_SOURCES = \
46 list-keys.c
47list_keys_LDADD = \
48 $(top_builddir)/src/util/libgnunetutil.la \
49 $(GN_LIBINTL)
50
42 51
43check_PROGRAMS = \ 52check_PROGRAMS = \
44 test_testing_portreservation \ 53 test_testing_portreservation \
diff --git a/src/testing/list-keys.c b/src/testing/list-keys.c
new file mode 100644
index 000000000..1c469adcc
--- /dev/null
+++ b/src/testing/list-keys.c
@@ -0,0 +1,109 @@
1#include "platform.h"
2#include "gnunet_util_lib.h"
3#include "gnunet_testing_lib.h"
4
5static unsigned int nkeys;
6static unsigned int nskip;
7static 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 */
21static void
22run (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
86int 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}