aboutsummaryrefslogtreecommitdiff
path: root/src/gnsrecord/gnunet-gnsrecord-tvg.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2020-05-10 15:21:47 +0200
committerMartin Schanzenbach <mschanzenbach@posteo.de>2020-05-10 15:21:47 +0200
commitf6c7437bf0a0371b2004b64ec24a68abb3d8e352 (patch)
tree622cd365822147dd237d01b70cd8288ebad09cf3 /src/gnsrecord/gnunet-gnsrecord-tvg.c
parent63c371cfbd8c6297a17dab9190ad650e93ad9053 (diff)
downloadgnunet-f6c7437bf0a0371b2004b64ec24a68abb3d8e352.tar.gz
gnunet-f6c7437bf0a0371b2004b64ec24a68abb3d8e352.zip
add gnsrecord tvg
Diffstat (limited to 'src/gnsrecord/gnunet-gnsrecord-tvg.c')
-rw-r--r--src/gnsrecord/gnunet-gnsrecord-tvg.c191
1 files changed, 191 insertions, 0 deletions
diff --git a/src/gnsrecord/gnunet-gnsrecord-tvg.c b/src/gnsrecord/gnunet-gnsrecord-tvg.c
new file mode 100644
index 000000000..e0ac5881a
--- /dev/null
+++ b/src/gnsrecord/gnunet-gnsrecord-tvg.c
@@ -0,0 +1,191 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file util/gnunet-gns-tvg.c
23 * @brief Generate test vectors for GNS.
24 * @author Martin Schanzenbach
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_signatures.h"
29#include "gnunet_gns_service.h"
30#include "gnunet_gnsrecord_lib.h"
31#include "gnunet_dnsparser_lib.h"
32#include "gnunet_testing_lib.h"
33#include <inttypes.h>
34
35#define TEST_RECORD_LABEL "test"
36#define TEST_RECORD_A "1.2.3.4"
37#define TEST_RRCOUNT 2
38
39static void
40print_record(const struct GNUNET_GNSRECORD_Data *rd)
41{
42 char *data_enc;
43 char *string_v;
44 string_v = GNUNET_GNSRECORD_value_to_string (rd->record_type,
45 rd->data,
46 rd->data_size);
47 fprintf (stdout,
48 "EXPIRATION: %"PRIu64"\n", rd->expiration_time);
49 fprintf (stdout,
50 "DATA_SIZE: %"PRIu64"\n", rd->data_size);
51 fprintf (stdout,
52 "TYPE: %d\n", rd->record_type);
53 fprintf (stdout,
54 "FLAGS: %d\n", rd->flags);
55 GNUNET_STRINGS_base64_encode (rd->data,
56 rd->data_size,
57 &data_enc);
58 fprintf (stdout,
59 "DATA (base64):\n%s\n",
60 data_enc);
61 fprintf (stdout,
62 "DATA (Human readable):\n%s\n\n", string_v);
63 GNUNET_free (string_v);
64
65 GNUNET_free (data_enc);
66}
67
68/**
69 * Main function that will be run.
70 *
71 * @param cls closure
72 * @param args remaining command-line arguments
73 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
74 * @param cfg configuration
75 */
76static void
77run (void *cls,
78 char *const *args,
79 const char *cfgfile,
80 const struct GNUNET_CONFIGURATION_Handle *cfg)
81{
82 struct GNUNET_GNSRECORD_Data rd[2];
83 struct GNUNET_TIME_Absolute exp_abs = GNUNET_TIME_absolute_get();
84 struct GNUNET_GNSRECORD_Block *rrblock;
85 char *bdata;
86 struct GNUNET_CRYPTO_EcdsaPrivateKey id_priv;
87 struct GNUNET_CRYPTO_EcdsaPublicKey id_pub;
88 struct GNUNET_CRYPTO_EcdsaPrivateKey pkey_data_p;
89 struct GNUNET_CRYPTO_EcdsaPublicKey pkey_data;
90 void *data;
91 size_t data_size;
92 char *rdata;
93 size_t rdata_size;
94
95 GNUNET_CRYPTO_ecdsa_key_create (&id_priv);
96 GNUNET_CRYPTO_ecdsa_key_get_public (&id_priv,
97 &id_pub);
98
99 GNUNET_CRYPTO_ecdsa_key_create (&pkey_data_p);
100 GNUNET_CRYPTO_ecdsa_key_get_public (&pkey_data_p,
101 &pkey_data);
102 fprintf (stdout,
103 "Label: %s\nRRCOUNT: %d\n\n", TEST_RECORD_LABEL, TEST_RRCOUNT);
104 memset (rd, 0, sizeof (struct GNUNET_GNSRECORD_Data) * 2);
105 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A, TEST_RECORD_A, &data, &data_size));
106 rd[0].data = data;
107 rd[0].data_size = data_size;
108 rd[0].expiration_time = exp_abs.abs_value_us;
109 rd[0].record_type = GNUNET_DNSPARSER_TYPE_A;
110 fprintf (stdout, "Record #0\n");
111 print_record (&rd[0]);
112
113 rd[1].data = &pkey_data;
114 rd[1].data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
115 rd[1].expiration_time = exp_abs.abs_value_us;
116 rd[1].record_type = GNUNET_GNSRECORD_TYPE_PKEY;
117 rd[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
118 fprintf (stdout, "Record #1\n");
119 print_record (&rd[1]);
120
121 rdata_size = GNUNET_GNSRECORD_records_get_size (2,
122 rd);
123 rdata = GNUNET_malloc (rdata_size);
124 GNUNET_GNSRECORD_records_serialize (2,
125 rd,
126 rdata_size,
127 rdata);
128 char* data_enc;
129 GNUNET_STRINGS_base64_encode (rdata,
130 rdata_size,
131 &data_enc);
132 fprintf(stdout, "RDATA:\n%s\n\n", data_enc);
133 GNUNET_free (data_enc);
134 rrblock = GNUNET_GNSRECORD_block_create (&id_priv,
135 exp_abs,
136 TEST_RECORD_LABEL,
137 rd,
138 TEST_RRCOUNT);
139 size_t bdata_size = ntohl (rrblock->purpose.size) -
140 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) -
141 sizeof(struct GNUNET_TIME_AbsoluteNBO);
142 size_t rrblock_size = ntohl (rrblock->purpose.size) +
143 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
144 sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
145
146 bdata = (char*)&rrblock[1];
147 GNUNET_STRINGS_base64_encode (bdata,
148 bdata_size,
149 &data_enc);
150 fprintf(stdout, "BDATA:\n%s\n\n", data_enc);
151 GNUNET_free (data_enc);
152 GNUNET_STRINGS_base64_encode (rrblock,
153 rrblock_size,
154 &data_enc);
155 fprintf(stdout, "RRBLOCK:\n%s\n", data_enc);
156 GNUNET_free (data_enc);
157
158}
159
160
161/**
162 * The main function of the test vector generation tool.
163 *
164 * @param argc number of arguments from the command line
165 * @param argv command line arguments
166 * @return 0 ok, 1 on error
167 */
168int
169main (int argc,
170 char *const *argv)
171{
172 const struct GNUNET_GETOPT_CommandLineOption options[] = {
173 GNUNET_GETOPT_OPTION_END
174 };
175
176 GNUNET_assert (GNUNET_OK ==
177 GNUNET_log_setup ("gnunet-gns-tvg",
178 "INFO",
179 NULL));
180 if (GNUNET_OK !=
181 GNUNET_PROGRAM_run (argc, argv,
182 "gnunet-gns-tvg",
183 "Generate test vectors for GNS",
184 options,
185 &run, NULL))
186 return 1;
187 return 0;
188}
189
190
191/* end of gnunet-gns-tvg.c */