aboutsummaryrefslogtreecommitdiff
path: root/src/gnsrecord
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/gnsrecord
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/gnsrecord')
-rw-r--r--src/gnsrecord/gnsrecord.c139
-rw-r--r--src/gnsrecord/gnsrecord_crypto.c484
-rw-r--r--src/gnsrecord/gnsrecord_misc.c210
-rw-r--r--src/gnsrecord/gnsrecord_serialization.c253
-rw-r--r--src/gnsrecord/perf_gnsrecord_crypto.c106
-rw-r--r--src/gnsrecord/plugin_gnsrecord_dns.c1167
-rw-r--r--src/gnsrecord/test_gnsrecord_block_expiration.c31
-rw-r--r--src/gnsrecord/test_gnsrecord_crypto.c123
-rw-r--r--src/gnsrecord/test_gnsrecord_serialization.c159
9 files changed, 1342 insertions, 1330 deletions
diff --git a/src/gnsrecord/gnsrecord.c b/src/gnsrecord/gnsrecord.c
index a4e8624e8..7672a7b00 100644
--- a/src/gnsrecord/gnsrecord.c
+++ b/src/gnsrecord/gnsrecord.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file gnsrecord/gnsrecord.c 22 * @file gnsrecord/gnsrecord.c
@@ -32,14 +32,13 @@
32#include "gnunet_gnsrecord_plugin.h" 32#include "gnunet_gnsrecord_plugin.h"
33#include "gnunet_tun_lib.h" 33#include "gnunet_tun_lib.h"
34 34
35#define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) 35#define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord", __VA_ARGS__)
36 36
37 37
38/** 38/**
39 * Handle for a plugin. 39 * Handle for a plugin.
40 */ 40 */
41struct Plugin 41struct Plugin {
42{
43 /** 42 /**
44 * Name of the shared library. 43 * Name of the shared library.
45 */ 44 */
@@ -76,20 +75,20 @@ static int once;
76 * @param lib_ret the plugin API 75 * @param lib_ret the plugin API
77 */ 76 */
78static void 77static void
79add_plugin (void *cls, 78add_plugin(void *cls,
80 const char *library_name, 79 const char *library_name,
81 void *lib_ret) 80 void *lib_ret)
82{ 81{
83 struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret; 82 struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret;
84 struct Plugin *plugin; 83 struct Plugin *plugin;
85 84
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 85 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
87 "Loading block plugin `%s'\n", 86 "Loading block plugin `%s'\n",
88 library_name); 87 library_name);
89 plugin = GNUNET_new (struct Plugin); 88 plugin = GNUNET_new(struct Plugin);
90 plugin->api = api; 89 plugin->api = api;
91 plugin->library_name = GNUNET_strdup (library_name); 90 plugin->library_name = GNUNET_strdup(library_name);
92 GNUNET_array_append (gns_plugins, num_plugins, plugin); 91 GNUNET_array_append(gns_plugins, num_plugins, plugin);
93} 92}
94 93
95 94
@@ -97,13 +96,13 @@ add_plugin (void *cls,
97 * Loads all plugins (lazy initialization). 96 * Loads all plugins (lazy initialization).
98 */ 97 */
99static void 98static void
100init () 99init()
101{ 100{
102 if (1 == once) 101 if (1 == once)
103 return; 102 return;
104 once = 1; 103 once = 1;
105 GNUNET_PLUGIN_load_all ("libgnunet_plugin_gnsrecord_", NULL, 104 GNUNET_PLUGIN_load_all("libgnunet_plugin_gnsrecord_", NULL,
106 &add_plugin, NULL); 105 &add_plugin, NULL);
107} 106}
108 107
109 108
@@ -111,20 +110,20 @@ init ()
111 * Dual function to #init(). 110 * Dual function to #init().
112 */ 111 */
113void __attribute__ ((destructor)) 112void __attribute__ ((destructor))
114GNSRECORD_fini () 113GNSRECORD_fini()
115{ 114{
116 struct Plugin *plugin; 115 struct Plugin *plugin;
117 116
118 for (unsigned int i = 0; i < num_plugins; i++) 117 for (unsigned int i = 0; i < num_plugins; i++)
119 { 118 {
120 plugin = gns_plugins[i]; 119 plugin = gns_plugins[i];
121 GNUNET_break (NULL == 120 GNUNET_break(NULL ==
122 GNUNET_PLUGIN_unload (plugin->library_name, 121 GNUNET_PLUGIN_unload(plugin->library_name,
123 plugin->api)); 122 plugin->api));
124 GNUNET_free (plugin->library_name); 123 GNUNET_free(plugin->library_name);
125 GNUNET_free (plugin); 124 GNUNET_free(plugin);
126 } 125 }
127 GNUNET_free_non_null (gns_plugins); 126 GNUNET_free_non_null(gns_plugins);
128 gns_plugins = NULL; 127 gns_plugins = NULL;
129 once = 0; 128 once = 0;
130 num_plugins = 0; 129 num_plugins = 0;
@@ -140,23 +139,23 @@ GNSRECORD_fini ()
140 * @return NULL on error, otherwise human-readable representation of the value 139 * @return NULL on error, otherwise human-readable representation of the value
141 */ 140 */
142char * 141char *
143GNUNET_GNSRECORD_value_to_string (uint32_t type, 142GNUNET_GNSRECORD_value_to_string(uint32_t type,
144 const void *data, 143 const void *data,
145 size_t data_size) 144 size_t data_size)
146{ 145{
147 struct Plugin *plugin; 146 struct Plugin *plugin;
148 char *ret; 147 char *ret;
149 148
150 init (); 149 init();
151 for (unsigned int i = 0; i < num_plugins; i++) 150 for (unsigned int i = 0; i < num_plugins; i++)
152 { 151 {
153 plugin = gns_plugins[i]; 152 plugin = gns_plugins[i];
154 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, 153 if (NULL != (ret = plugin->api->value_to_string(plugin->api->cls,
155 type, 154 type,
156 data, 155 data,
157 data_size))) 156 data_size)))
158 return ret; 157 return ret;
159 } 158 }
160 return NULL; 159 return NULL;
161} 160}
162 161
@@ -172,24 +171,24 @@ GNUNET_GNSRECORD_value_to_string (uint32_t type,
172 * @return #GNUNET_OK on success 171 * @return #GNUNET_OK on success
173 */ 172 */
174int 173int
175GNUNET_GNSRECORD_string_to_value (uint32_t type, 174GNUNET_GNSRECORD_string_to_value(uint32_t type,
176 const char *s, 175 const char *s,
177 void **data, 176 void **data,
178 size_t *data_size) 177 size_t *data_size)
179{ 178{
180 struct Plugin *plugin; 179 struct Plugin *plugin;
181 180
182 init (); 181 init();
183 for (unsigned int i = 0; i < num_plugins; i++) 182 for (unsigned int i = 0; i < num_plugins; i++)
184 { 183 {
185 plugin = gns_plugins[i]; 184 plugin = gns_plugins[i];
186 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, 185 if (GNUNET_OK == plugin->api->string_to_value(plugin->api->cls,
187 type, 186 type,
188 s, 187 s,
189 data, 188 data,
190 data_size)) 189 data_size))
191 return GNUNET_OK; 190 return GNUNET_OK;
192 } 191 }
193 return GNUNET_SYSERR; 192 return GNUNET_SYSERR;
194} 193}
195 194
@@ -201,22 +200,22 @@ GNUNET_GNSRECORD_string_to_value (uint32_t type,
201 * @return corresponding number, UINT32_MAX on error 200 * @return corresponding number, UINT32_MAX on error
202 */ 201 */
203uint32_t 202uint32_t
204GNUNET_GNSRECORD_typename_to_number (const char *dns_typename) 203GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
205{ 204{
206 struct Plugin *plugin; 205 struct Plugin *plugin;
207 uint32_t ret; 206 uint32_t ret;
208 207
209 if (0 == strcasecmp (dns_typename, 208 if (0 == strcasecmp(dns_typename,
210 "ANY")) 209 "ANY"))
211 return GNUNET_GNSRECORD_TYPE_ANY; 210 return GNUNET_GNSRECORD_TYPE_ANY;
212 init (); 211 init();
213 for (unsigned int i = 0; i < num_plugins; i++) 212 for (unsigned int i = 0; i < num_plugins; i++)
214 { 213 {
215 plugin = gns_plugins[i]; 214 plugin = gns_plugins[i];
216 if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls, 215 if (UINT32_MAX != (ret = plugin->api->typename_to_number(plugin->api->cls,
217 dns_typename))) 216 dns_typename)))
218 return ret; 217 return ret;
219 } 218 }
220 return UINT32_MAX; 219 return UINT32_MAX;
221} 220}
222 221
@@ -228,21 +227,21 @@ GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
228 * @return corresponding typestring, NULL on error 227 * @return corresponding typestring, NULL on error
229 */ 228 */
230const char * 229const char *
231GNUNET_GNSRECORD_number_to_typename (uint32_t type) 230GNUNET_GNSRECORD_number_to_typename(uint32_t type)
232{ 231{
233 struct Plugin *plugin; 232 struct Plugin *plugin;
234 const char * ret; 233 const char * ret;
235 234
236 if (GNUNET_GNSRECORD_TYPE_ANY == type) 235 if (GNUNET_GNSRECORD_TYPE_ANY == type)
237 return "ANY"; 236 return "ANY";
238 init (); 237 init();
239 for (unsigned int i = 0; i < num_plugins; i++) 238 for (unsigned int i = 0; i < num_plugins; i++)
240 { 239 {
241 plugin = gns_plugins[i]; 240 plugin = gns_plugins[i];
242 if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls, 241 if (NULL != (ret = plugin->api->number_to_typename(plugin->api->cls,
243 type))) 242 type)))
244 return ret; 243 return ret;
245 } 244 }
246 return NULL; 245 return NULL;
247} 246}
248 247
diff --git a/src/gnsrecord/gnsrecord_crypto.c b/src/gnsrecord/gnsrecord_crypto.c
index 2785a4a59..6eda1e962 100644
--- a/src/gnsrecord/gnsrecord_crypto.c
+++ b/src/gnsrecord/gnsrecord_crypto.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file gnsrecord/gnsrecord_crypto.c 22 * @file gnsrecord/gnsrecord_crypto.c
@@ -35,7 +35,7 @@
35#include "gnunet_tun_lib.h" 35#include "gnunet_tun_lib.h"
36 36
37 37
38#define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) 38#define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord", __VA_ARGS__)
39 39
40 40
41/** 41/**
@@ -47,24 +47,24 @@
47 * @param pub public key to use for KDF 47 * @param pub public key to use for KDF
48 */ 48 */
49static void 49static void
50derive_block_aes_key (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, 50derive_block_aes_key(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
51 struct GNUNET_CRYPTO_SymmetricSessionKey *skey, 51 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
52 const char *label, 52 const char *label,
53 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub) 53 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
54{ 54{
55 static const char ctx_key[] = "gns-aes-ctx-key"; 55 static const char ctx_key[] = "gns-aes-ctx-key";
56 static const char ctx_iv[] = "gns-aes-ctx-iv"; 56 static const char ctx_iv[] = "gns-aes-ctx-iv";
57 57
58 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), 58 GNUNET_CRYPTO_kdf(skey, sizeof(struct GNUNET_CRYPTO_SymmetricSessionKey),
59 pub, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), 59 pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
60 label, strlen (label), 60 label, strlen(label),
61 ctx_key, strlen (ctx_key), 61 ctx_key, strlen(ctx_key),
62 NULL, 0); 62 NULL, 0);
63 GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector), 63 GNUNET_CRYPTO_kdf(iv, sizeof(struct GNUNET_CRYPTO_SymmetricInitializationVector),
64 pub, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), 64 pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
65 label, strlen (label), 65 label, strlen(label),
66 ctx_iv, strlen (ctx_iv), 66 ctx_iv, strlen(ctx_iv),
67 NULL, 0); 67 NULL, 0);
68} 68}
69 69
70 70
@@ -80,15 +80,15 @@ derive_block_aes_key (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
80 * @return NULL on error (block too large) 80 * @return NULL on error (block too large)
81 */ 81 */
82struct GNUNET_GNSRECORD_Block * 82struct GNUNET_GNSRECORD_Block *
83block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, 83block_create(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
84 const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey, 84 const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
85 struct GNUNET_TIME_Absolute expire, 85 struct GNUNET_TIME_Absolute expire,
86 const char *label, 86 const char *label,
87 const struct GNUNET_GNSRECORD_Data *rd, 87 const struct GNUNET_GNSRECORD_Data *rd,
88 unsigned int rd_count) 88 unsigned int rd_count)
89{ 89{
90 ssize_t payload_len = GNUNET_GNSRECORD_records_get_size (rd_count, 90 ssize_t payload_len = GNUNET_GNSRECORD_records_get_size(rd_count,
91 rd); 91 rd);
92 struct GNUNET_GNSRECORD_Block *block; 92 struct GNUNET_GNSRECORD_Block *block;
93 struct GNUNET_CRYPTO_EcdsaPrivateKey *dkey; 93 struct GNUNET_CRYPTO_EcdsaPrivateKey *dkey;
94 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 94 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
@@ -98,80 +98,80 @@ block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
98 struct GNUNET_TIME_Absolute now; 98 struct GNUNET_TIME_Absolute now;
99 99
100 if (payload_len < 0) 100 if (payload_len < 0)
101 { 101 {
102 GNUNET_break (0); 102 GNUNET_break(0);
103 return NULL; 103 return NULL;
104 } 104 }
105 if (payload_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE) 105 if (payload_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE)
106 { 106 {
107 GNUNET_break (0); 107 GNUNET_break(0);
108 return NULL; 108 return NULL;
109 } 109 }
110 /* convert relative to absolute times */ 110 /* convert relative to absolute times */
111 now = GNUNET_TIME_absolute_get (); 111 now = GNUNET_TIME_absolute_get();
112 for (unsigned int i=0;i<rd_count;i++) 112 for (unsigned int i = 0; i < rd_count; i++)
113 {
114 rdc[i] = rd[i];
115 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
116 { 113 {
117 struct GNUNET_TIME_Relative t; 114 rdc[i] = rd[i];
115 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
116 {
117 struct GNUNET_TIME_Relative t;
118 118
119 /* encrypted blocks must never have relative expiration times, convert! */ 119 /* encrypted blocks must never have relative expiration times, convert! */
120 rdc[i].flags &= ~GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; 120 rdc[i].flags &= ~GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
121 t.rel_value_us = rdc[i].expiration_time; 121 t.rel_value_us = rdc[i].expiration_time;
122 rdc[i].expiration_time = GNUNET_TIME_absolute_add (now, t).abs_value_us; 122 rdc[i].expiration_time = GNUNET_TIME_absolute_add(now, t).abs_value_us;
123 }
123 } 124 }
124 }
125 /* serialize */ 125 /* serialize */
126 rd_count_nbo = htonl (rd_count); 126 rd_count_nbo = htonl(rd_count);
127 { 127 {
128 char payload[sizeof (uint32_t) + payload_len]; 128 char payload[sizeof(uint32_t) + payload_len];
129 129
130 GNUNET_memcpy (payload, 130 GNUNET_memcpy(payload,
131 &rd_count_nbo, 131 &rd_count_nbo,
132 sizeof (uint32_t)); 132 sizeof(uint32_t));
133 GNUNET_assert (payload_len == 133 GNUNET_assert(payload_len ==
134 GNUNET_GNSRECORD_records_serialize (rd_count, 134 GNUNET_GNSRECORD_records_serialize(rd_count,
135 rdc, 135 rdc,
136 payload_len, 136 payload_len,
137 &payload[sizeof (uint32_t)])); 137 &payload[sizeof(uint32_t)]));
138 block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) + 138 block = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Block) +
139 sizeof (uint32_t) + 139 sizeof(uint32_t) +
140 payload_len); 140 payload_len);
141 block->purpose.size = htonl (sizeof (uint32_t) + 141 block->purpose.size = htonl(sizeof(uint32_t) +
142 payload_len + 142 payload_len +
143 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + 143 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
144 sizeof (struct GNUNET_TIME_AbsoluteNBO)); 144 sizeof(struct GNUNET_TIME_AbsoluteNBO));
145 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN); 145 block->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
146 block->expiration_time = GNUNET_TIME_absolute_hton (expire); 146 block->expiration_time = GNUNET_TIME_absolute_hton(expire);
147 /* encrypt and sign */ 147 /* encrypt and sign */
148 dkey = GNUNET_CRYPTO_ecdsa_private_key_derive (key, 148 dkey = GNUNET_CRYPTO_ecdsa_private_key_derive(key,
149 label, 149 label,
150 "gns"); 150 "gns");
151 GNUNET_CRYPTO_ecdsa_key_get_public (dkey, 151 GNUNET_CRYPTO_ecdsa_key_get_public(dkey,
152 &block->derived_key); 152 &block->derived_key);
153 derive_block_aes_key (&iv, 153 derive_block_aes_key(&iv,
154 &skey, 154 &skey,
155 label, 155 label,
156 pkey); 156 pkey);
157 GNUNET_break (payload_len + sizeof (uint32_t) == 157 GNUNET_break(payload_len + sizeof(uint32_t) ==
158 GNUNET_CRYPTO_symmetric_encrypt (payload, 158 GNUNET_CRYPTO_symmetric_encrypt(payload,
159 payload_len + sizeof (uint32_t), 159 payload_len + sizeof(uint32_t),
160 &skey, 160 &skey,
161 &iv, 161 &iv,
162 &block[1])); 162 &block[1]));
163 } 163 }
164 if (GNUNET_OK != 164 if (GNUNET_OK !=
165 GNUNET_CRYPTO_ecdsa_sign (dkey, 165 GNUNET_CRYPTO_ecdsa_sign(dkey,
166 &block->purpose, 166 &block->purpose,
167 &block->signature)) 167 &block->signature))
168 { 168 {
169 GNUNET_break (0); 169 GNUNET_break(0);
170 GNUNET_free (dkey); 170 GNUNET_free(dkey);
171 GNUNET_free (block); 171 GNUNET_free(block);
172 return NULL; 172 return NULL;
173 } 173 }
174 GNUNET_free (dkey); 174 GNUNET_free(dkey);
175 return block; 175 return block;
176} 176}
177 177
@@ -187,30 +187,29 @@ block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
187 * @return NULL on error (block too large) 187 * @return NULL on error (block too large)
188 */ 188 */
189struct GNUNET_GNSRECORD_Block * 189struct GNUNET_GNSRECORD_Block *
190GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, 190GNUNET_GNSRECORD_block_create(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
191 struct GNUNET_TIME_Absolute expire, 191 struct GNUNET_TIME_Absolute expire,
192 const char *label, 192 const char *label,
193 const struct GNUNET_GNSRECORD_Data *rd, 193 const struct GNUNET_GNSRECORD_Data *rd,
194 unsigned int rd_count) 194 unsigned int rd_count)
195{ 195{
196 struct GNUNET_CRYPTO_EcdsaPublicKey pkey; 196 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
197 197
198 GNUNET_CRYPTO_ecdsa_key_get_public (key, 198 GNUNET_CRYPTO_ecdsa_key_get_public(key,
199 &pkey); 199 &pkey);
200 return block_create (key, 200 return block_create(key,
201 &pkey, 201 &pkey,
202 expire, 202 expire,
203 label, 203 label,
204 rd, 204 rd,
205 rd_count); 205 rd_count);
206} 206}
207 207
208 208
209/** 209/**
210 * Line in cache mapping private keys to public keys. 210 * Line in cache mapping private keys to public keys.
211 */ 211 */
212struct KeyCacheLine 212struct KeyCacheLine {
213{
214 /** 213 /**
215 * A private key. 214 * A private key.
216 */ 215 */
@@ -220,7 +219,6 @@ struct KeyCacheLine
220 * Associated public key. 219 * Associated public key.
221 */ 220 */
222 struct GNUNET_CRYPTO_EcdsaPublicKey pkey; 221 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
223
224}; 222};
225 223
226 224
@@ -237,33 +235,33 @@ struct KeyCacheLine
237 * @return NULL on error (block too large) 235 * @return NULL on error (block too large)
238 */ 236 */
239struct GNUNET_GNSRECORD_Block * 237struct GNUNET_GNSRECORD_Block *
240GNUNET_GNSRECORD_block_create2 (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, 238GNUNET_GNSRECORD_block_create2(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
241 struct GNUNET_TIME_Absolute expire, 239 struct GNUNET_TIME_Absolute expire,
242 const char *label, 240 const char *label,
243 const struct GNUNET_GNSRECORD_Data *rd, 241 const struct GNUNET_GNSRECORD_Data *rd,
244 unsigned int rd_count) 242 unsigned int rd_count)
245{ 243{
246#define CSIZE 64 244#define CSIZE 64
247 static struct KeyCacheLine cache[CSIZE]; 245 static struct KeyCacheLine cache[CSIZE];
248 struct KeyCacheLine *line; 246 struct KeyCacheLine *line;
249 247
250 line = &cache[(*(unsigned int *) key) % CSIZE]; 248 line = &cache[(*(unsigned int *)key) % CSIZE];
251 if (0 != memcmp (&line->key, 249 if (0 != memcmp(&line->key,
252 key, 250 key,
253 sizeof (*key))) 251 sizeof(*key)))
254 { 252 {
255 /* cache miss, recompute */ 253 /* cache miss, recompute */
256 line->key = *key; 254 line->key = *key;
257 GNUNET_CRYPTO_ecdsa_key_get_public (key, 255 GNUNET_CRYPTO_ecdsa_key_get_public(key,
258 &line->pkey); 256 &line->pkey);
259 } 257 }
260#undef CSIZE 258#undef CSIZE
261 return block_create (key, 259 return block_create(key,
262 &line->pkey, 260 &line->pkey,
263 expire, 261 expire,
264 label, 262 label,
265 rd, 263 rd,
266 rd_count); 264 rd_count);
267} 265}
268 266
269 267
@@ -276,12 +274,12 @@ GNUNET_GNSRECORD_block_create2 (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
276 * @return #GNUNET_OK if the signature is valid 274 * @return #GNUNET_OK if the signature is valid
277 */ 275 */
278int 276int
279GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block) 277GNUNET_GNSRECORD_block_verify(const struct GNUNET_GNSRECORD_Block *block)
280{ 278{
281 return GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, 279 return GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN,
282 &block->purpose, 280 &block->purpose,
283 &block->signature, 281 &block->signature,
284 &block->derived_key); 282 &block->derived_key);
285} 283}
286 284
287 285
@@ -297,124 +295,124 @@ GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block)
297 * not well-formed 295 * not well-formed
298 */ 296 */
299int 297int
300GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block, 298GNUNET_GNSRECORD_block_decrypt(const struct GNUNET_GNSRECORD_Block *block,
301 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, 299 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
302 const char *label, 300 const char *label,
303 GNUNET_GNSRECORD_RecordCallback proc, 301 GNUNET_GNSRECORD_RecordCallback proc,
304 void *proc_cls) 302 void *proc_cls)
305{ 303{
306 size_t payload_len = ntohl (block->purpose.size) - 304 size_t payload_len = ntohl(block->purpose.size) -
307 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) - 305 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) -
308 sizeof (struct GNUNET_TIME_AbsoluteNBO); 306 sizeof(struct GNUNET_TIME_AbsoluteNBO);
309 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 307 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
310 struct GNUNET_CRYPTO_SymmetricSessionKey skey; 308 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
311 309
312 if (ntohl (block->purpose.size) < 310 if (ntohl(block->purpose.size) <
313 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + 311 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
314 sizeof (struct GNUNET_TIME_AbsoluteNBO)) 312 sizeof(struct GNUNET_TIME_AbsoluteNBO))
315 { 313 {
316 GNUNET_break_op (0); 314 GNUNET_break_op(0);
317 return GNUNET_SYSERR; 315 return GNUNET_SYSERR;
318 } 316 }
319 derive_block_aes_key (&iv, 317 derive_block_aes_key(&iv,
320 &skey, 318 &skey,
321 label, 319 label,
322 zone_key); 320 zone_key);
323 { 321 {
324 char payload[payload_len]; 322 char payload[payload_len];
325 uint32_t rd_count; 323 uint32_t rd_count;
326 324
327 GNUNET_break (payload_len == 325 GNUNET_break(payload_len ==
328 GNUNET_CRYPTO_symmetric_decrypt (&block[1], payload_len, 326 GNUNET_CRYPTO_symmetric_decrypt(&block[1], payload_len,
329 &skey, &iv, 327 &skey, &iv,
330 payload)); 328 payload));
331 GNUNET_memcpy (&rd_count, 329 GNUNET_memcpy(&rd_count,
332 payload, 330 payload,
333 sizeof (uint32_t)); 331 sizeof(uint32_t));
334 rd_count = ntohl (rd_count); 332 rd_count = ntohl(rd_count);
335 if (rd_count > 2048) 333 if (rd_count > 2048)
336 { 334 {
337 /* limit to sane value */ 335 /* limit to sane value */
338 GNUNET_break_op (0); 336 GNUNET_break_op(0);
339 return GNUNET_SYSERR; 337 return GNUNET_SYSERR;
340 } 338 }
341 { 339 {
342 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)]; 340 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
343 unsigned int j; 341 unsigned int j;
344 struct GNUNET_TIME_Absolute now; 342 struct GNUNET_TIME_Absolute now;
345 343
346 if (GNUNET_OK != 344 if (GNUNET_OK !=
347 GNUNET_GNSRECORD_records_deserialize (payload_len - sizeof (uint32_t), 345 GNUNET_GNSRECORD_records_deserialize(payload_len - sizeof(uint32_t),
348 &payload[sizeof (uint32_t)], 346 &payload[sizeof(uint32_t)],
349 rd_count, 347 rd_count,
350 rd)) 348 rd))
351 {
352 GNUNET_break_op (0);
353 return GNUNET_SYSERR;
354 }
355 /* hide expired records */
356 now = GNUNET_TIME_absolute_get ();
357 j = 0;
358 for (unsigned int i=0;i<rd_count;i++)
359 {
360 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
361 { 349 {
362 /* encrypted blocks must never have relative expiration times, skip! */ 350 GNUNET_break_op(0);
363 GNUNET_break_op (0); 351 return GNUNET_SYSERR;
364 continue;
365 } 352 }
366 353 /* hide expired records */
367 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) 354 now = GNUNET_TIME_absolute_get();
355 j = 0;
356 for (unsigned int i = 0; i < rd_count; i++)
368 { 357 {
369 int include_record = GNUNET_YES; 358 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
370 /* Shadow record, figure out if we have a not expired active record */ 359 {
371 for (unsigned int k=0;k<rd_count;k++) 360 /* encrypted blocks must never have relative expiration times, skip! */
372 { 361 GNUNET_break_op(0);
373 if (k == i)
374 continue; 362 continue;
375 if (rd[i].expiration_time < now.abs_value_us) 363 }
376 include_record = GNUNET_NO; /* Shadow record is expired */ 364
377 if ( (rd[k].record_type == rd[i].record_type) && 365 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD))
378 (rd[k].expiration_time >= now.abs_value_us) &&
379 (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) )
380 { 366 {
381 include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */ 367 int include_record = GNUNET_YES;
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 368 /* Shadow record, figure out if we have a not expired active record */
383 "Ignoring shadow record\n"); 369 for (unsigned int k = 0; k < rd_count; k++)
384 break; 370 {
371 if (k == i)
372 continue;
373 if (rd[i].expiration_time < now.abs_value_us)
374 include_record = GNUNET_NO; /* Shadow record is expired */
375 if ((rd[k].record_type == rd[i].record_type) &&
376 (rd[k].expiration_time >= now.abs_value_us) &&
377 (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
378 {
379 include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */
380 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
381 "Ignoring shadow record\n");
382 break;
383 }
384 }
385 if (GNUNET_YES == include_record)
386 {
387 rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW_RECORD; /* Remove Flag */
388 if (j != i)
389 rd[j] = rd[i];
390 j++;
391 }
392 }
393 else if (rd[i].expiration_time >= now.abs_value_us)
394 {
395 /* Include this record */
396 if (j != i)
397 rd[j] = rd[i];
398 j++;
399 }
400 else
401 {
402 struct GNUNET_TIME_Absolute at;
403
404 at.abs_value_us = rd[i].expiration_time;
405 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
406 "Excluding record that expired %s (%llu ago)\n",
407 GNUNET_STRINGS_absolute_time_to_string(at),
408 (unsigned long long)rd[i].expiration_time - now.abs_value_us);
385 } 409 }
386 }
387 if (GNUNET_YES == include_record)
388 {
389 rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW_RECORD; /* Remove Flag */
390 if (j != i)
391 rd[j] = rd[i];
392 j++;
393 }
394 }
395 else if (rd[i].expiration_time >= now.abs_value_us)
396 {
397 /* Include this record */
398 if (j != i)
399 rd[j] = rd[i];
400 j++;
401 } 410 }
402 else
403 {
404 struct GNUNET_TIME_Absolute at;
405
406 at.abs_value_us = rd[i].expiration_time;
407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
408 "Excluding record that expired %s (%llu ago)\n",
409 GNUNET_STRINGS_absolute_time_to_string (at),
410 (unsigned long long) rd[i].expiration_time - now.abs_value_us);
411 }
412 }
413 rd_count = j; 411 rd_count = j;
414 if (NULL != proc) 412 if (NULL != proc)
415 proc (proc_cls, 413 proc(proc_cls,
416 rd_count, 414 rd_count,
417 (0 != rd_count) ? rd : NULL); 415 (0 != rd_count) ? rd : NULL);
418 } 416 }
419 } 417 }
420 return GNUNET_OK; 418 return GNUNET_OK;
@@ -429,17 +427,17 @@ GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block,
429 * @param query hash to use for the query 427 * @param query hash to use for the query
430 */ 428 */
431void 429void
432GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 430GNUNET_GNSRECORD_query_from_private_key(const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
433 const char *label, 431 const char *label,
434 struct GNUNET_HashCode *query) 432 struct GNUNET_HashCode *query)
435{ 433{
436 struct GNUNET_CRYPTO_EcdsaPublicKey pub; 434 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
437 435
438 GNUNET_CRYPTO_ecdsa_key_get_public (zone, 436 GNUNET_CRYPTO_ecdsa_key_get_public(zone,
439 &pub); 437 &pub);
440 GNUNET_GNSRECORD_query_from_public_key (&pub, 438 GNUNET_GNSRECORD_query_from_public_key(&pub,
441 label, 439 label,
442 query); 440 query);
443} 441}
444 442
445 443
@@ -451,19 +449,19 @@ GNUNET_GNSRECORD_query_from_private_key (const struct GNUNET_CRYPTO_EcdsaPrivate
451 * @param query hash to use for the query 449 * @param query hash to use for the query
452 */ 450 */
453void 451void
454GNUNET_GNSRECORD_query_from_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, 452GNUNET_GNSRECORD_query_from_public_key(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
455 const char *label, 453 const char *label,
456 struct GNUNET_HashCode *query) 454 struct GNUNET_HashCode *query)
457{ 455{
458 struct GNUNET_CRYPTO_EcdsaPublicKey pd; 456 struct GNUNET_CRYPTO_EcdsaPublicKey pd;
459 457
460 GNUNET_CRYPTO_ecdsa_public_key_derive (pub, 458 GNUNET_CRYPTO_ecdsa_public_key_derive(pub,
461 label, 459 label,
462 "gns", 460 "gns",
463 &pd); 461 &pd);
464 GNUNET_CRYPTO_hash (&pd, 462 GNUNET_CRYPTO_hash(&pd,
465 sizeof (pd), 463 sizeof(pd),
466 query); 464 query);
467} 465}
468 466
469 467
diff --git a/src/gnsrecord/gnsrecord_misc.c b/src/gnsrecord/gnsrecord_misc.c
index 50dd2086b..159f078ee 100644
--- a/src/gnsrecord/gnsrecord_misc.c
+++ b/src/gnsrecord/gnsrecord_misc.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file gnsrecord/gnsrecord_misc.c 22 * @file gnsrecord/gnsrecord_misc.c
@@ -35,7 +35,7 @@
35#include "gnunet_tun_lib.h" 35#include "gnunet_tun_lib.h"
36 36
37 37
38#define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) 38#define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord", __VA_ARGS__)
39 39
40/** 40/**
41 * Convert a UTF-8 string to UTF-8 lowercase 41 * Convert a UTF-8 string to UTF-8 lowercase
@@ -43,12 +43,12 @@
43 * @return converted result 43 * @return converted result
44 */ 44 */
45char * 45char *
46GNUNET_GNSRECORD_string_to_lowercase (const char *src) 46GNUNET_GNSRECORD_string_to_lowercase(const char *src)
47{ 47{
48 char *res; 48 char *res;
49 49
50 res = GNUNET_strdup (src); 50 res = GNUNET_strdup(src);
51 GNUNET_STRINGS_utf8_tolower (src, res); 51 GNUNET_STRINGS_utf8_tolower(src, res);
52 return res; 52 return res;
53} 53}
54 54
@@ -62,19 +62,19 @@ GNUNET_GNSRECORD_string_to_lowercase (const char *src)
62 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s 62 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s
63 */ 63 */
64const char * 64const char *
65GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z) 65GNUNET_GNSRECORD_z2s(const struct GNUNET_CRYPTO_EcdsaPublicKey *z)
66{ 66{
67 static char buf[sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) * 8]; 67 static char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) * 8];
68 char *end; 68 char *end;
69 69
70 end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z, 70 end = GNUNET_STRINGS_data_to_string((const unsigned char *)z,
71 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), 71 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
72 buf, sizeof (buf)); 72 buf, sizeof(buf));
73 if (NULL == end) 73 if (NULL == end)
74 { 74 {
75 GNUNET_break (0); 75 GNUNET_break(0);
76 return NULL; 76 return NULL;
77 } 77 }
78 *end = '\0'; 78 *end = '\0';
79 return buf; 79 return buf;
80} 80}
@@ -90,51 +90,51 @@ GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z)
90 * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not 90 * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not
91 */ 91 */
92int 92int
93GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a, 93GNUNET_GNSRECORD_records_cmp(const struct GNUNET_GNSRECORD_Data *a,
94 const struct GNUNET_GNSRECORD_Data *b) 94 const struct GNUNET_GNSRECORD_Data *b)
95{ 95{
96 LOG (GNUNET_ERROR_TYPE_DEBUG, 96 LOG(GNUNET_ERROR_TYPE_DEBUG,
97 "Comparing records\n"); 97 "Comparing records\n");
98 if (a->record_type != b->record_type) 98 if (a->record_type != b->record_type)
99 { 99 {
100 LOG (GNUNET_ERROR_TYPE_DEBUG, 100 LOG(GNUNET_ERROR_TYPE_DEBUG,
101 "Record type %lu != %lu\n", a->record_type, b->record_type); 101 "Record type %lu != %lu\n", a->record_type, b->record_type);
102 return GNUNET_NO; 102 return GNUNET_NO;
103 } 103 }
104 if ((a->expiration_time != b->expiration_time) && 104 if ((a->expiration_time != b->expiration_time) &&
105 ((a->expiration_time != 0) && (b->expiration_time != 0))) 105 ((a->expiration_time != 0) && (b->expiration_time != 0)))
106 { 106 {
107 LOG (GNUNET_ERROR_TYPE_DEBUG, 107 LOG(GNUNET_ERROR_TYPE_DEBUG,
108 "Expiration time %llu != %llu\n", 108 "Expiration time %llu != %llu\n",
109 a->expiration_time, 109 a->expiration_time,
110 b->expiration_time); 110 b->expiration_time);
111 return GNUNET_NO; 111 return GNUNET_NO;
112 } 112 }
113 if ((a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS) 113 if ((a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS)
114 != (b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS)) 114 != (b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS))
115 { 115 {
116 LOG (GNUNET_ERROR_TYPE_DEBUG, 116 LOG(GNUNET_ERROR_TYPE_DEBUG,
117 "Flags %lu (%lu) != %lu (%lu)\n", a->flags, 117 "Flags %lu (%lu) != %lu (%lu)\n", a->flags,
118 a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS, b->flags, 118 a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS, b->flags,
119 b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS); 119 b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS);
120 return GNUNET_NO; 120 return GNUNET_NO;
121 } 121 }
122 if (a->data_size != b->data_size) 122 if (a->data_size != b->data_size)
123 { 123 {
124 LOG (GNUNET_ERROR_TYPE_DEBUG, 124 LOG(GNUNET_ERROR_TYPE_DEBUG,
125 "Data size %lu != %lu\n", 125 "Data size %lu != %lu\n",
126 a->data_size, 126 a->data_size,
127 b->data_size); 127 b->data_size);
128 return GNUNET_NO; 128 return GNUNET_NO;
129 } 129 }
130 if (0 != memcmp (a->data, b->data, a->data_size)) 130 if (0 != memcmp(a->data, b->data, a->data_size))
131 { 131 {
132 LOG (GNUNET_ERROR_TYPE_DEBUG, 132 LOG(GNUNET_ERROR_TYPE_DEBUG,
133 "Data contents do not match\n"); 133 "Data contents do not match\n");
134 return GNUNET_NO; 134 return GNUNET_NO;
135 } 135 }
136 LOG (GNUNET_ERROR_TYPE_DEBUG, 136 LOG(GNUNET_ERROR_TYPE_DEBUG,
137 "Records are equal\n"); 137 "Records are equal\n");
138 return GNUNET_YES; 138 return GNUNET_YES;
139} 139}
140 140
@@ -149,8 +149,8 @@ GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
149 * @return absolute expiration time 149 * @return absolute expiration time
150 */ 150 */
151struct GNUNET_TIME_Absolute 151struct GNUNET_TIME_Absolute
152GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count, 152GNUNET_GNSRECORD_record_get_expiration_time(unsigned int rd_count,
153 const struct GNUNET_GNSRECORD_Data *rd) 153 const struct GNUNET_GNSRECORD_Data *rd)
154{ 154{
155 struct GNUNET_TIME_Absolute expire; 155 struct GNUNET_TIME_Absolute expire;
156 struct GNUNET_TIME_Absolute at; 156 struct GNUNET_TIME_Absolute at;
@@ -162,44 +162,44 @@ GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
162 return GNUNET_TIME_UNIT_ZERO_ABS; 162 return GNUNET_TIME_UNIT_ZERO_ABS;
163 expire = GNUNET_TIME_UNIT_FOREVER_ABS; 163 expire = GNUNET_TIME_UNIT_FOREVER_ABS;
164 for (unsigned int c = 0; c < rd_count; c++) 164 for (unsigned int c = 0; c < rd_count; c++)
165 {
166 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
167 { 165 {
168 rt.rel_value_us = rd[c].expiration_time; 166 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
169 at = GNUNET_TIME_relative_to_absolute (rt); 167 {
170 } 168 rt.rel_value_us = rd[c].expiration_time;
171 else 169 at = GNUNET_TIME_relative_to_absolute(rt);
172 { 170 }
173 at.abs_value_us = rd[c].expiration_time;
174 }
175
176 for (unsigned int c2 = 0; c2 < rd_count; c2++)
177 {
178 /* Check for shadow record */
179 if ( (c == c2) ||
180 (rd[c].record_type != rd[c2].record_type) ||
181 (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) )
182 continue;
183 /* We have a shadow record */
184 if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
185 {
186 rt_shadow.rel_value_us = rd[c2].expiration_time;
187 at_shadow = GNUNET_TIME_relative_to_absolute (rt_shadow);
188 }
189 else 171 else
190 { 172 {
191 at_shadow.abs_value_us = rd[c2].expiration_time; 173 at.abs_value_us = rd[c].expiration_time;
192 } 174 }
193 at = GNUNET_TIME_absolute_max (at, 175
194 at_shadow); 176 for (unsigned int c2 = 0; c2 < rd_count; c2++)
177 {
178 /* Check for shadow record */
179 if ((c == c2) ||
180 (rd[c].record_type != rd[c2].record_type) ||
181 (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
182 continue;
183 /* We have a shadow record */
184 if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
185 {
186 rt_shadow.rel_value_us = rd[c2].expiration_time;
187 at_shadow = GNUNET_TIME_relative_to_absolute(rt_shadow);
188 }
189 else
190 {
191 at_shadow.abs_value_us = rd[c2].expiration_time;
192 }
193 at = GNUNET_TIME_absolute_max(at,
194 at_shadow);
195 }
196 expire = GNUNET_TIME_absolute_min(at,
197 expire);
195 } 198 }
196 expire = GNUNET_TIME_absolute_min (at, 199 LOG(GNUNET_ERROR_TYPE_DEBUG,
197 expire); 200 "Determined expiration time for block with %u records to be %s\n",
198 } 201 rd_count,
199 LOG (GNUNET_ERROR_TYPE_DEBUG, 202 GNUNET_STRINGS_absolute_time_to_string(expire));
200 "Determined expiration time for block with %u records to be %s\n",
201 rd_count,
202 GNUNET_STRINGS_absolute_time_to_string (expire));
203 return expire; 203 return expire;
204} 204}
205 205
@@ -211,14 +211,14 @@ GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
211 * #GNUNET_NO if not 211 * #GNUNET_NO if not
212 */ 212 */
213int 213int
214GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd) 214GNUNET_GNSRECORD_is_expired(const struct GNUNET_GNSRECORD_Data *rd)
215{ 215{
216 struct GNUNET_TIME_Absolute at; 216 struct GNUNET_TIME_Absolute at;
217 217
218 if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) 218 if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
219 return GNUNET_NO; 219 return GNUNET_NO;
220 at.abs_value_us = rd->expiration_time; 220 at.abs_value_us = rd->expiration_time;
221 return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ? GNUNET_YES : GNUNET_NO; 221 return (0 == GNUNET_TIME_absolute_get_remaining(at).rel_value_us) ? GNUNET_YES : GNUNET_NO;
222} 222}
223 223
224 224
@@ -233,17 +233,17 @@ GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd)
233 * key in an encoding suitable for DNS labels. 233 * key in an encoding suitable for DNS labels.
234 */ 234 */
235const char * 235const char *
236GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey) 236GNUNET_GNSRECORD_pkey_to_zkey(const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
237{ 237{
238 static char ret[128]; 238 static char ret[128];
239 char *pkeys; 239 char *pkeys;
240 240
241 pkeys = GNUNET_CRYPTO_ecdsa_public_key_to_string (pkey); 241 pkeys = GNUNET_CRYPTO_ecdsa_public_key_to_string(pkey);
242 GNUNET_snprintf (ret, 242 GNUNET_snprintf(ret,
243 sizeof (ret), 243 sizeof(ret),
244 "%s", 244 "%s",
245 pkeys); 245 pkeys);
246 GNUNET_free (pkeys); 246 GNUNET_free(pkeys);
247 return ret; 247 return ret;
248} 248}
249 249
@@ -258,13 +258,13 @@ GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
258 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax 258 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
259 */ 259 */
260int 260int
261GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey, 261GNUNET_GNSRECORD_zkey_to_pkey(const char *zkey,
262 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey) 262 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
263{ 263{
264 if (GNUNET_OK != 264 if (GNUNET_OK !=
265 GNUNET_CRYPTO_ecdsa_public_key_from_string (zkey, 265 GNUNET_CRYPTO_ecdsa_public_key_from_string(zkey,
266 strlen (zkey), 266 strlen(zkey),
267 pkey)) 267 pkey))
268 return GNUNET_SYSERR; 268 return GNUNET_SYSERR;
269 return GNUNET_OK; 269 return GNUNET_OK;
270} 270}
diff --git a/src/gnsrecord/gnsrecord_serialization.c b/src/gnsrecord/gnsrecord_serialization.c
index be1dc1889..9d2c858ef 100644
--- a/src/gnsrecord/gnsrecord_serialization.c
+++ b/src/gnsrecord/gnsrecord_serialization.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file gnsrecord/gnsrecord_serialization.c 22 * @file gnsrecord/gnsrecord_serialization.c
@@ -35,7 +35,7 @@
35#include "gnunet_tun_lib.h" 35#include "gnunet_tun_lib.h"
36 36
37 37
38#define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) 38#define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord", __VA_ARGS__)
39 39
40/** 40/**
41 * Set to 1 to check that all records are well-formed (can be converted 41 * Set to 1 to check that all records are well-formed (can be converted
@@ -49,9 +49,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
49/** 49/**
50 * Internal format of a record in the serialized form. 50 * Internal format of a record in the serialized form.
51 */ 51 */
52struct NetworkRecord 52struct NetworkRecord {
53{
54
55 /** 53 /**
56 * Expiration time for the DNS record; relative or absolute depends 54 * Expiration time for the DNS record; relative or absolute depends
57 * on @e flags, network byte order. 55 * on @e flags, network byte order.
@@ -72,7 +70,6 @@ struct NetworkRecord
72 * Flags for the record, network byte order. 70 * Flags for the record, network byte order.
73 */ 71 */
74 uint32_t flags GNUNET_PACKED; 72 uint32_t flags GNUNET_PACKED;
75
76}; 73};
77 74
78GNUNET_NETWORK_STRUCT_END 75GNUNET_NETWORK_STRUCT_END
@@ -87,44 +84,44 @@ GNUNET_NETWORK_STRUCT_END
87 * @return the required size to serialize, -1 on error 84 * @return the required size to serialize, -1 on error
88 */ 85 */
89ssize_t 86ssize_t
90GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, 87GNUNET_GNSRECORD_records_get_size(unsigned int rd_count,
91 const struct GNUNET_GNSRECORD_Data *rd) 88 const struct GNUNET_GNSRECORD_Data *rd)
92{ 89{
93 size_t ret; 90 size_t ret;
94 91
95 if (0 == rd_count) 92 if (0 == rd_count)
96 return 0; 93 return 0;
97 94
98 ret = sizeof (struct NetworkRecord) * rd_count; 95 ret = sizeof(struct NetworkRecord) * rd_count;
99 for (unsigned int i=0;i<rd_count;i++) 96 for (unsigned int i = 0; i < rd_count; i++)
100 {
101 if ((ret + rd[i].data_size) < ret)
102 { 97 {
103 GNUNET_break (0); 98 if ((ret + rd[i].data_size) < ret)
104 return -1; 99 {
105 } 100 GNUNET_break(0);
106 ret += rd[i].data_size; 101 return -1;
102 }
103 ret += rd[i].data_size;
107#if DEBUG_GNSRECORDS 104#if DEBUG_GNSRECORDS
108 {
109 char *str;
110
111 str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
112 rd[i].data,
113 rd[i].data_size);
114 if (NULL == str)
115 { 105 {
116 GNUNET_break_op (0); 106 char *str;
117 return -1; 107
108 str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type,
109 rd[i].data,
110 rd[i].data_size);
111 if (NULL == str)
112 {
113 GNUNET_break_op(0);
114 return -1;
115 }
116 GNUNET_free(str);
118 } 117 }
119 GNUNET_free (str);
120 }
121#endif 118#endif
122 } 119 }
123 if (ret > SSIZE_MAX) 120 if (ret > SSIZE_MAX)
124 { 121 {
125 GNUNET_break (0); 122 GNUNET_break(0);
126 return -1; 123 return -1;
127 } 124 }
128 //Do not pad PKEY 125 //Do not pad PKEY
129 if (GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) 126 if (GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type)
130 return ret; 127 return ret;
@@ -140,7 +137,7 @@ GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
140 ret |= ret >> 8; 137 ret |= ret >> 8;
141 ret |= ret >> 16; 138 ret |= ret >> 16;
142 ret++; 139 ret++;
143 return (ssize_t) ret; 140 return (ssize_t)ret;
144} 141}
145 142
146 143
@@ -154,65 +151,65 @@ GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
154 * @return the size of serialized records, -1 if records do not fit 151 * @return the size of serialized records, -1 if records do not fit
155 */ 152 */
156ssize_t 153ssize_t
157GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, 154GNUNET_GNSRECORD_records_serialize(unsigned int rd_count,
158 const struct GNUNET_GNSRECORD_Data *rd, 155 const struct GNUNET_GNSRECORD_Data *rd,
159 size_t dest_size, 156 size_t dest_size,
160 char *dest) 157 char *dest)
161{ 158{
162 struct NetworkRecord rec; 159 struct NetworkRecord rec;
163 size_t off; 160 size_t off;
164 161
165 off = 0; 162 off = 0;
166 for (unsigned int i=0;i<rd_count;i++) 163 for (unsigned int i = 0; i < rd_count; i++)
167 {
168 LOG (GNUNET_ERROR_TYPE_DEBUG,
169 "Serializing record %u with flags %d and expiration time %llu\n",
170 i,
171 rd[i].flags,
172 (unsigned long long) rd[i].expiration_time);
173 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
174 rec.data_size = htonl ((uint32_t) rd[i].data_size);
175 rec.record_type = htonl (rd[i].record_type);
176 rec.flags = htonl (rd[i].flags);
177 if ( (off + sizeof (rec) > dest_size) ||
178 (off + sizeof (rec) < off) )
179 {
180 GNUNET_break (0);
181 return -1;
182 }
183 GNUNET_memcpy (&dest[off],
184 &rec,
185 sizeof (rec));
186 off += sizeof (rec);
187 if ( (off + rd[i].data_size > dest_size) ||
188 (off + rd[i].data_size < off) )
189 { 164 {
190 GNUNET_break (0); 165 LOG(GNUNET_ERROR_TYPE_DEBUG,
191 return -1; 166 "Serializing record %u with flags %d and expiration time %llu\n",
192 } 167 i,
193 GNUNET_memcpy (&dest[off], 168 rd[i].flags,
194 rd[i].data, 169 (unsigned long long)rd[i].expiration_time);
195 rd[i].data_size); 170 rec.expiration_time = GNUNET_htonll(rd[i].expiration_time);
196 off += rd[i].data_size; 171 rec.data_size = htonl((uint32_t)rd[i].data_size);
172 rec.record_type = htonl(rd[i].record_type);
173 rec.flags = htonl(rd[i].flags);
174 if ((off + sizeof(rec) > dest_size) ||
175 (off + sizeof(rec) < off))
176 {
177 GNUNET_break(0);
178 return -1;
179 }
180 GNUNET_memcpy(&dest[off],
181 &rec,
182 sizeof(rec));
183 off += sizeof(rec);
184 if ((off + rd[i].data_size > dest_size) ||
185 (off + rd[i].data_size < off))
186 {
187 GNUNET_break(0);
188 return -1;
189 }
190 GNUNET_memcpy(&dest[off],
191 rd[i].data,
192 rd[i].data_size);
193 off += rd[i].data_size;
197#if DEBUG_GNSRECORDS 194#if DEBUG_GNSRECORDS
198 {
199 char *str;
200
201 str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
202 rd[i].data,
203 rd[i].data_size);
204 if (NULL == str)
205 { 195 {
206 GNUNET_break_op (0); 196 char *str;
207 return -1; 197
198 str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type,
199 rd[i].data,
200 rd[i].data_size);
201 if (NULL == str)
202 {
203 GNUNET_break_op(0);
204 return -1;
205 }
206 GNUNET_free(str);
208 } 207 }
209 GNUNET_free (str);
210 }
211#endif 208#endif
212 } 209 }
213 memset (&dest[off], 210 memset(&dest[off],
214 0, 211 0,
215 dest_size-off); 212 dest_size - off);
216 return dest_size; 213 return dest_size;
217} 214}
218 215
@@ -227,60 +224,60 @@ GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
227 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 224 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
228 */ 225 */
229int 226int
230GNUNET_GNSRECORD_records_deserialize (size_t len, 227GNUNET_GNSRECORD_records_deserialize(size_t len,
231 const char *src, 228 const char *src,
232 unsigned int rd_count, 229 unsigned int rd_count,
233 struct GNUNET_GNSRECORD_Data *dest) 230 struct GNUNET_GNSRECORD_Data *dest)
234{ 231{
235 struct NetworkRecord rec; 232 struct NetworkRecord rec;
236 size_t off; 233 size_t off;
237 234
238 off = 0; 235 off = 0;
239 for (unsigned int i=0;i<rd_count;i++) 236 for (unsigned int i = 0; i < rd_count; i++)
240 {
241 if ( (off + sizeof (rec) > len) ||
242 (off + sizeof (rec) < off) )
243 { 237 {
244 GNUNET_break_op (0); 238 if ((off + sizeof(rec) > len) ||
245 return GNUNET_SYSERR; 239 (off + sizeof(rec) < off))
246 } 240 {
247 GNUNET_memcpy (&rec, 241 GNUNET_break_op(0);
248 &src[off], 242 return GNUNET_SYSERR;
249 sizeof (rec)); 243 }
250 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time); 244 GNUNET_memcpy(&rec,
251 dest[i].data_size = ntohl ((uint32_t) rec.data_size); 245 &src[off],
252 dest[i].record_type = ntohl (rec.record_type); 246 sizeof(rec));
253 dest[i].flags = ntohl (rec.flags); 247 dest[i].expiration_time = GNUNET_ntohll(rec.expiration_time);
254 off += sizeof (rec); 248 dest[i].data_size = ntohl((uint32_t)rec.data_size);
255 if ( (off + dest[i].data_size > len) || 249 dest[i].record_type = ntohl(rec.record_type);
256 (off + dest[i].data_size < off) ) 250 dest[i].flags = ntohl(rec.flags);
257 { 251 off += sizeof(rec);
258 GNUNET_break_op (0); 252 if ((off + dest[i].data_size > len) ||
259 return GNUNET_SYSERR; 253 (off + dest[i].data_size < off))
260 } 254 {
261 dest[i].data = &src[off]; 255 GNUNET_break_op(0);
262 off += dest[i].data_size; 256 return GNUNET_SYSERR;
257 }
258 dest[i].data = &src[off];
259 off += dest[i].data_size;
263#if GNUNET_EXTRA_LOGGING 260#if GNUNET_EXTRA_LOGGING
264 {
265 char *str;
266
267 str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type,
268 dest[i].data,
269 dest[i].data_size);
270 if (NULL == str)
271 { 261 {
272 GNUNET_break_op (0); 262 char *str;
273 return GNUNET_SYSERR; 263
264 str = GNUNET_GNSRECORD_value_to_string(dest[i].record_type,
265 dest[i].data,
266 dest[i].data_size);
267 if (NULL == str)
268 {
269 GNUNET_break_op(0);
270 return GNUNET_SYSERR;
271 }
272 GNUNET_free(str);
274 } 273 }
275 GNUNET_free (str);
276 }
277#endif 274#endif
278 LOG (GNUNET_ERROR_TYPE_DEBUG, 275 LOG(GNUNET_ERROR_TYPE_DEBUG,
279 "Deserialized record %u with flags %d and expiration time %llu\n", 276 "Deserialized record %u with flags %d and expiration time %llu\n",
280 i, 277 i,
281 dest[i].flags, 278 dest[i].flags,
282 (unsigned long long) dest[i].expiration_time); 279 (unsigned long long)dest[i].expiration_time);
283 } 280 }
284 return GNUNET_OK; 281 return GNUNET_OK;
285} 282}
286 283
diff --git a/src/gnsrecord/perf_gnsrecord_crypto.c b/src/gnsrecord/perf_gnsrecord_crypto.c
index a7cff9d48..a4fffd238 100644
--- a/src/gnsrecord/perf_gnsrecord_crypto.c
+++ b/src/gnsrecord/perf_gnsrecord_crypto.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file gnsrecord/test_gnsrecord_crypto.c 21 * @file gnsrecord/test_gnsrecord_crypto.c
22 * @brief testcase for block creation, verification and decryption 22 * @brief testcase for block creation, verification and decryption
@@ -43,29 +43,29 @@
43 43
44 44
45static struct GNUNET_GNSRECORD_Data * 45static struct GNUNET_GNSRECORD_Data *
46create_record (int count) 46create_record(int count)
47{ 47{
48 struct GNUNET_GNSRECORD_Data *rd; 48 struct GNUNET_GNSRECORD_Data *rd;
49 49
50 rd = GNUNET_new_array (count, 50 rd = GNUNET_new_array(count,
51 struct GNUNET_GNSRECORD_Data); 51 struct GNUNET_GNSRECORD_Data);
52 for (unsigned int c = 0; c < count; c++) 52 for (unsigned int c = 0; c < count; c++)
53 { 53 {
54 rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000; 54 rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
55 rd[c].record_type = TEST_RECORD_TYPE; 55 rd[c].record_type = TEST_RECORD_TYPE;
56 rd[c].data_size = TEST_RECORD_DATALEN; 56 rd[c].data_size = TEST_RECORD_DATALEN;
57 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN); 57 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
58 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); 58 memset((char *)rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
59 } 59 }
60 return rd; 60 return rd;
61} 61}
62 62
63 63
64static void 64static void
65run (void *cls, 65run(void *cls,
66 char *const *args, 66 char *const *args,
67 const char *cfgfile, 67 const char *cfgfile,
68 const struct GNUNET_CONFIGURATION_Handle *cfg) 68 const struct GNUNET_CONFIGURATION_Handle *cfg)
69{ 69{
70 struct GNUNET_GNSRECORD_Block *block; 70 struct GNUNET_GNSRECORD_Block *block;
71 struct GNUNET_HashCode query; 71 struct GNUNET_HashCode query;
@@ -75,45 +75,45 @@ run (void *cls,
75 struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey; 75 struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
76 struct GNUNET_TIME_Absolute expire; 76 struct GNUNET_TIME_Absolute expire;
77 77
78 (void) cls; 78 (void)cls;
79 (void) args; 79 (void)args;
80 (void) cfgfile; 80 (void)cfgfile;
81 (void) cfg; 81 (void)cfg;
82 expire = GNUNET_TIME_absolute_get(); 82 expire = GNUNET_TIME_absolute_get();
83 privkey = GNUNET_CRYPTO_ecdsa_key_create (); 83 privkey = GNUNET_CRYPTO_ecdsa_key_create();
84 GNUNET_assert (NULL != privkey); 84 GNUNET_assert(NULL != privkey);
85 85
86 /* test block creation */ 86 /* test block creation */
87 s_name = "DUMMY.dummy.gnunet"; 87 s_name = "DUMMY.dummy.gnunet";
88 s_rd = create_record (RECORDS); 88 s_rd = create_record(RECORDS);
89 start_time = GNUNET_TIME_absolute_get (); 89 start_time = GNUNET_TIME_absolute_get();
90 for (unsigned int i=0;i<ROUNDS;i++) 90 for (unsigned int i = 0; i < ROUNDS; i++)
91 { 91 {
92 GNUNET_assert (NULL != (block = 92 GNUNET_assert(NULL != (block =
93 GNUNET_GNSRECORD_block_create2 (privkey, 93 GNUNET_GNSRECORD_block_create2(privkey,
94 expire, 94 expire,
95 s_name, 95 s_name,
96 s_rd, 96 s_rd,
97 RECORDS))); 97 RECORDS)));
98 GNUNET_GNSRECORD_query_from_private_key (privkey, 98 GNUNET_GNSRECORD_query_from_private_key(privkey,
99 s_name, 99 s_name,
100 &query); 100 &query);
101 GNUNET_free (block); 101 GNUNET_free(block);
102 } 102 }
103 fprintf (stderr, 103 fprintf(stderr,
104 "Took %s to produce %u GNS blocks for the DHT\n", 104 "Took %s to produce %u GNS blocks for the DHT\n",
105 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start_time), 105 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start_time),
106 GNUNET_YES), 106 GNUNET_YES),
107 ROUNDS); 107 ROUNDS);
108 for (unsigned int i=0;i<RECORDS;i++) 108 for (unsigned int i = 0; i < RECORDS; i++)
109 GNUNET_free ((void *) s_rd[i].data); 109 GNUNET_free((void *)s_rd[i].data);
110 GNUNET_free (s_rd); 110 GNUNET_free(s_rd);
111 GNUNET_free (privkey); 111 GNUNET_free(privkey);
112} 112}
113 113
114 114
115int 115int
116main (int argc, char *argv[]) 116main(int argc, char *argv[])
117{ 117{
118 static char *const argvx[] = { 118 static char *const argvx[] = {
119 "perf-gnsrecord-crypto", 119 "perf-gnsrecord-crypto",
@@ -124,12 +124,12 @@ main (int argc, char *argv[])
124 }; 124 };
125 125
126 if (GNUNET_OK != 126 if (GNUNET_OK !=
127 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, 127 GNUNET_PROGRAM_run((sizeof(argvx) / sizeof(char *)) - 1,
128 argvx, 128 argvx,
129 "perf-gnsrecord-crypto", 129 "perf-gnsrecord-crypto",
130 "nohelp", options, 130 "nohelp", options,
131 &run, 131 &run,
132 NULL)) 132 NULL))
133 return 1; 133 return 1;
134 return 0; 134 return 0;
135} 135}
diff --git a/src/gnsrecord/plugin_gnsrecord_dns.c b/src/gnsrecord/plugin_gnsrecord_dns.c
index 3f62d3d77..8c39603e3 100644
--- a/src/gnsrecord/plugin_gnsrecord_dns.c
+++ b/src/gnsrecord/plugin_gnsrecord_dns.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file gnsrecord/plugin_gnsrecord_dns.c 22 * @file gnsrecord/plugin_gnsrecord_dns.c
@@ -39,222 +39,234 @@
39 * @return NULL on error, otherwise human-readable representation of the value 39 * @return NULL on error, otherwise human-readable representation of the value
40 */ 40 */
41static char * 41static char *
42dns_value_to_string (void *cls, 42dns_value_to_string(void *cls,
43 uint32_t type, 43 uint32_t type,
44 const void *data, 44 const void *data,
45 size_t data_size) 45 size_t data_size)
46{ 46{
47 char *result; 47 char *result;
48 char tmp[INET6_ADDRSTRLEN]; 48 char tmp[INET6_ADDRSTRLEN];
49 49
50 switch (type) 50 switch (type)
51 {
52 case GNUNET_DNSPARSER_TYPE_A:
53 if (data_size != sizeof (struct in_addr))
54 return NULL;
55 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
56 return NULL;
57 return GNUNET_strdup (tmp);
58 case GNUNET_DNSPARSER_TYPE_NS: {
59 char *ns;
60 size_t off;
61
62 off = 0;
63 ns = GNUNET_DNSPARSER_parse_name (data, data_size, &off);
64 if ((NULL == ns) || (off != data_size))
65 { 51 {
66 GNUNET_break_op (0); 52 case GNUNET_DNSPARSER_TYPE_A:
67 GNUNET_free_non_null (ns); 53 if (data_size != sizeof(struct in_addr))
68 return NULL; 54 return NULL;
55 if (NULL == inet_ntop(AF_INET, data, tmp, sizeof(tmp)))
56 return NULL;
57 return GNUNET_strdup(tmp);
58
59 case GNUNET_DNSPARSER_TYPE_NS: {
60 char *ns;
61 size_t off;
62
63 off = 0;
64 ns = GNUNET_DNSPARSER_parse_name(data, data_size, &off);
65 if ((NULL == ns) || (off != data_size))
66 {
67 GNUNET_break_op(0);
68 GNUNET_free_non_null(ns);
69 return NULL;
70 }
71 return ns;
69 } 72 }
70 return ns; 73
71 } 74 case GNUNET_DNSPARSER_TYPE_CNAME: {
72 case GNUNET_DNSPARSER_TYPE_CNAME: { 75 char *cname;
73 char *cname; 76 size_t off;
74 size_t off; 77
75 78 off = 0;
76 off = 0; 79 cname = GNUNET_DNSPARSER_parse_name(data, data_size, &off);
77 cname = GNUNET_DNSPARSER_parse_name (data, data_size, &off); 80 if ((NULL == cname) || (off != data_size))
78 if ((NULL == cname) || (off != data_size)) 81 {
79 { 82 GNUNET_break_op(0);
80 GNUNET_break_op (0); 83 GNUNET_free_non_null(cname);
81 GNUNET_free_non_null (cname); 84 return NULL;
82 return NULL; 85 }
86 return cname;
83 } 87 }
84 return cname; 88
85 } 89 case GNUNET_DNSPARSER_TYPE_SOA: {
86 case GNUNET_DNSPARSER_TYPE_SOA: { 90 struct GNUNET_DNSPARSER_SoaRecord *soa;
87 struct GNUNET_DNSPARSER_SoaRecord *soa; 91 size_t off;
88 size_t off; 92
89 93 off = 0;
90 off = 0; 94 soa = GNUNET_DNSPARSER_parse_soa(data, data_size, &off);
91 soa = GNUNET_DNSPARSER_parse_soa (data, data_size, &off); 95 if ((NULL == soa) || (off != data_size))
92 if ((NULL == soa) || (off != data_size)) 96 {
93 { 97 GNUNET_break_op(0);
94 GNUNET_break_op (0); 98 if (NULL != soa)
95 if (NULL != soa) 99 GNUNET_DNSPARSER_free_soa(soa);
96 GNUNET_DNSPARSER_free_soa (soa); 100 return NULL;
97 return NULL; 101 }
102 GNUNET_asprintf(&result,
103 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
104 soa->rname,
105 soa->mname,
106 soa->serial,
107 soa->refresh,
108 soa->retry,
109 soa->expire,
110 soa->minimum_ttl);
111 GNUNET_DNSPARSER_free_soa(soa);
112 return result;
98 } 113 }
99 GNUNET_asprintf (&result, 114
100 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu", 115 case GNUNET_DNSPARSER_TYPE_PTR: {
101 soa->rname, 116 char *ptr;
102 soa->mname, 117 size_t off;
103 soa->serial, 118
104 soa->refresh, 119 off = 0;
105 soa->retry, 120 ptr = GNUNET_DNSPARSER_parse_name(data, data_size, &off);
106 soa->expire, 121 if ((NULL == ptr) || (off != data_size))
107 soa->minimum_ttl); 122 {
108 GNUNET_DNSPARSER_free_soa (soa); 123 GNUNET_break_op(0);
109 return result; 124 GNUNET_free_non_null(ptr);
110 } 125 return NULL;
111 case GNUNET_DNSPARSER_TYPE_PTR: { 126 }
112 char *ptr; 127 return ptr;
113 size_t off;
114
115 off = 0;
116 ptr = GNUNET_DNSPARSER_parse_name (data, data_size, &off);
117 if ((NULL == ptr) || (off != data_size))
118 {
119 GNUNET_break_op (0);
120 GNUNET_free_non_null (ptr);
121 return NULL;
122 } 128 }
123 return ptr; 129
124 } 130 case GNUNET_DNSPARSER_TYPE_CERT: {
125 case GNUNET_DNSPARSER_TYPE_CERT: { 131 struct GNUNET_DNSPARSER_CertRecord *cert;
126 struct GNUNET_DNSPARSER_CertRecord *cert; 132 size_t off;
127 size_t off; 133 char *base64;
128 char *base64; 134 int len;
129 int len; 135
130 136 off = 0;
131 off = 0; 137 cert = GNUNET_DNSPARSER_parse_cert(data, data_size, &off);
132 cert = GNUNET_DNSPARSER_parse_cert (data, data_size, &off); 138 if ((NULL == cert) || (off != data_size))
133 if ((NULL == cert) || (off != data_size)) 139 {
134 { 140 GNUNET_break_op(0);
135 GNUNET_break_op (0); 141 GNUNET_DNSPARSER_free_cert(cert);
136 GNUNET_DNSPARSER_free_cert (cert); 142 return NULL;
137 return NULL; 143 }
144 len = GNUNET_STRINGS_base64_encode(cert->certificate_data,
145 cert->certificate_size,
146 &base64);
147 GNUNET_asprintf(&result,
148 "%u %u %u %.*s",
149 cert->cert_type,
150 cert->cert_tag,
151 cert->algorithm,
152 len,
153 base64);
154 GNUNET_free(base64);
155 GNUNET_DNSPARSER_free_cert(cert);
156 return result;
138 } 157 }
139 len = GNUNET_STRINGS_base64_encode (cert->certificate_data, 158
140 cert->certificate_size, 159 case GNUNET_DNSPARSER_TYPE_MX: {
141 &base64); 160 struct GNUNET_DNSPARSER_MxRecord *mx;
142 GNUNET_asprintf (&result, 161 size_t off;
143 "%u %u %u %.*s", 162
144 cert->cert_type, 163 off = 0;
145 cert->cert_tag, 164 mx = GNUNET_DNSPARSER_parse_mx(data, data_size, &off);
146 cert->algorithm, 165 if ((NULL == mx) || (off != data_size))
147 len, 166 {
148 base64); 167 GNUNET_break_op(0);
149 GNUNET_free (base64); 168 GNUNET_DNSPARSER_free_mx(mx);
150 GNUNET_DNSPARSER_free_cert (cert); 169 return NULL;
151 return result; 170 }
152 } 171 GNUNET_asprintf(&result,
153 case GNUNET_DNSPARSER_TYPE_MX: { 172 "%u,%s",
154 struct GNUNET_DNSPARSER_MxRecord *mx; 173 (unsigned int)mx->preference,
155 size_t off; 174 mx->mxhost);
156 175 GNUNET_DNSPARSER_free_mx(mx);
157 off = 0; 176 return result;
158 mx = GNUNET_DNSPARSER_parse_mx (data, data_size, &off);
159 if ((NULL == mx) || (off != data_size))
160 {
161 GNUNET_break_op (0);
162 GNUNET_DNSPARSER_free_mx (mx);
163 return NULL;
164 } 177 }
165 GNUNET_asprintf (&result, 178
166 "%u,%s", 179 case GNUNET_DNSPARSER_TYPE_TXT:
167 (unsigned int) mx->preference, 180 return GNUNET_strndup(data, data_size);
168 mx->mxhost); 181
169 GNUNET_DNSPARSER_free_mx (mx); 182 case GNUNET_DNSPARSER_TYPE_AAAA:
170 return result; 183 if (data_size != sizeof(struct in6_addr))
171 } 184 return NULL;
172 case GNUNET_DNSPARSER_TYPE_TXT: 185 if (NULL == inet_ntop(AF_INET6, data, tmp, sizeof(tmp)))
173 return GNUNET_strndup (data, data_size); 186 return NULL;
174 case GNUNET_DNSPARSER_TYPE_AAAA: 187 return GNUNET_strdup(tmp);
175 if (data_size != sizeof (struct in6_addr)) 188
176 return NULL; 189 case GNUNET_DNSPARSER_TYPE_SRV: {
177 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp))) 190 struct GNUNET_DNSPARSER_SrvRecord *srv;
178 return NULL; 191 size_t off;
179 return GNUNET_strdup (tmp); 192
180 case GNUNET_DNSPARSER_TYPE_SRV: { 193 off = 0;
181 struct GNUNET_DNSPARSER_SrvRecord *srv; 194 srv = GNUNET_DNSPARSER_parse_srv(data, data_size, &off);
182 size_t off; 195 if ((NULL == srv) || (off != data_size))
183 196 {
184 off = 0; 197 GNUNET_break_op(0);
185 srv = GNUNET_DNSPARSER_parse_srv (data, data_size, &off); 198 if (NULL != srv)
186 if ((NULL == srv) || (off != data_size)) 199 GNUNET_DNSPARSER_free_srv(srv);
187 { 200 return NULL;
188 GNUNET_break_op (0); 201 }
189 if (NULL != srv) 202 GNUNET_asprintf(&result,
190 GNUNET_DNSPARSER_free_srv (srv); 203 "%d %d %d %s",
191 return NULL; 204 srv->priority,
205 srv->weight,
206 srv->port,
207 srv->target);
208 GNUNET_DNSPARSER_free_srv(srv);
209 return result;
192 } 210 }
193 GNUNET_asprintf (&result, 211
194 "%d %d %d %s", 212 case GNUNET_DNSPARSER_TYPE_TLSA: {
195 srv->priority, 213 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
196 srv->weight, 214 char *tlsa_str;
197 srv->port, 215 char *hex;
198 srv->target); 216
199 GNUNET_DNSPARSER_free_srv (srv); 217 if (data_size < sizeof(struct GNUNET_TUN_DnsTlsaRecord))
200 return result; 218 return NULL; /* malformed */
201 } 219 tlsa = data;
202 case GNUNET_DNSPARSER_TYPE_TLSA: { 220 hex =
203 const struct GNUNET_TUN_DnsTlsaRecord *tlsa; 221 GNUNET_DNSPARSER_bin_to_hex(&tlsa[1],
204 char *tlsa_str; 222 data_size -
205 char *hex; 223 sizeof(struct GNUNET_TUN_DnsTlsaRecord));
206 224 if (0 == GNUNET_asprintf(&tlsa_str,
207 if (data_size < sizeof (struct GNUNET_TUN_DnsTlsaRecord)) 225 "%u %u %u %s",
208 return NULL; /* malformed */ 226 (unsigned int)tlsa->usage,
209 tlsa = data; 227 (unsigned int)tlsa->selector,
210 hex = 228 (unsigned int)tlsa->matching_type,
211 GNUNET_DNSPARSER_bin_to_hex (&tlsa[1], 229 hex))
212 data_size - 230 {
213 sizeof (struct GNUNET_TUN_DnsTlsaRecord)); 231 GNUNET_free(hex);
214 if (0 == GNUNET_asprintf (&tlsa_str, 232 GNUNET_free(tlsa_str);
215 "%u %u %u %s", 233 return NULL;
216 (unsigned int) tlsa->usage, 234 }
217 (unsigned int) tlsa->selector, 235 GNUNET_free(hex);
218 (unsigned int) tlsa->matching_type, 236 return tlsa_str;
219 hex))
220 {
221 GNUNET_free (hex);
222 GNUNET_free (tlsa_str);
223 return NULL;
224 } 237 }
225 GNUNET_free (hex); 238
226 return tlsa_str; 239 case GNUNET_DNSPARSER_TYPE_CAA: { //RFC6844
227 } 240 const struct GNUNET_DNSPARSER_CaaRecord *caa;
228 case GNUNET_DNSPARSER_TYPE_CAA: { //RFC6844 241 char tag[15]; // between 1 and 15 bytes
229 const struct GNUNET_DNSPARSER_CaaRecord *caa; 242 char value[data_size];
230 char tag[15]; // between 1 and 15 bytes 243 char *caa_str;
231 char value[data_size]; 244 if (data_size < sizeof(struct GNUNET_DNSPARSER_CaaRecord))
232 char *caa_str; 245 return NULL; /* malformed */
233 if (data_size < sizeof (struct GNUNET_DNSPARSER_CaaRecord)) 246 caa = data;
234 return NULL; /* malformed */ 247 if ((1 > caa->tag_len) || (15 < caa->tag_len))
235 caa = data; 248 return NULL; /* malformed */
236 if ((1 > caa->tag_len) || (15 < caa->tag_len)) 249 memset(tag, 0, sizeof(tag));
237 return NULL; /* malformed */ 250 memset(value, 0, data_size);
238 memset (tag, 0, sizeof (tag)); 251 memcpy(tag, &caa[1], caa->tag_len);
239 memset (value, 0, data_size); 252 memcpy(value,
240 memcpy (tag, &caa[1], caa->tag_len); 253 (char *)&caa[1] + caa->tag_len,
241 memcpy (value, 254 data_size - caa->tag_len - 2);
242 (char *) &caa[1] + caa->tag_len, 255 if (0 == GNUNET_asprintf(&caa_str,
243 data_size - caa->tag_len - 2); 256 "%u %s %s",
244 if (0 == GNUNET_asprintf (&caa_str, 257 (unsigned int)caa->flags,
245 "%u %s %s", 258 tag,
246 (unsigned int) caa->flags, 259 value))
247 tag, 260 {
248 value)) 261 GNUNET_free(caa_str);
249 { 262 return NULL;
250 GNUNET_free (caa_str); 263 }
264 return caa_str;
265 }
266
267 default:
251 return NULL; 268 return NULL;
252 } 269 }
253 return caa_str;
254 }
255 default:
256 return NULL;
257 }
258} 270}
259 271
260 272
@@ -265,27 +277,26 @@ dns_value_to_string (void *cls,
265 * @return the value, 0 if not found 277 * @return the value, 0 if not found
266 */ 278 */
267static unsigned int 279static unsigned int
268rfc4398_mnemonic_to_value (const char *mnemonic) 280rfc4398_mnemonic_to_value(const char *mnemonic)
269{ 281{
270 static struct 282 static struct {
271 {
272 const char *mnemonic; 283 const char *mnemonic;
273 unsigned int val; 284 unsigned int val;
274 } table[] = {{"PKIX", 1}, 285 } table[] = { { "PKIX", 1 },
275 {"SPKI", 2}, 286 { "SPKI", 2 },
276 {"PGP", 3}, 287 { "PGP", 3 },
277 {"IPKIX", 4}, 288 { "IPKIX", 4 },
278 {"ISPKI", 5}, 289 { "ISPKI", 5 },
279 {"IPGP", 6}, 290 { "IPGP", 6 },
280 {"ACPKIX", 7}, 291 { "ACPKIX", 7 },
281 {"IACPKIX", 8}, 292 { "IACPKIX", 8 },
282 {"URI", 253}, 293 { "URI", 253 },
283 {"OID", 254}, 294 { "OID", 254 },
284 {NULL, 0}}; 295 { NULL, 0 } };
285 unsigned int i; 296 unsigned int i;
286 297
287 for (i = 0; NULL != table[i].mnemonic; i++) 298 for (i = 0; NULL != table[i].mnemonic; i++)
288 if (0 == strcasecmp (mnemonic, table[i].mnemonic)) 299 if (0 == strcasecmp(mnemonic, table[i].mnemonic))
289 return table[i].val; 300 return table[i].val;
290 return 0; 301 return 0;
291} 302}
@@ -298,25 +309,24 @@ rfc4398_mnemonic_to_value (const char *mnemonic)
298 * @return the value, 0 if not found 309 * @return the value, 0 if not found
299 */ 310 */
300static unsigned int 311static unsigned int
301rfc4034_mnemonic_to_value (const char *mnemonic) 312rfc4034_mnemonic_to_value(const char *mnemonic)
302{ 313{
303 static struct 314 static struct {
304 {
305 const char *mnemonic; 315 const char *mnemonic;
306 unsigned int val; 316 unsigned int val;
307 } table[] = {{"RSAMD5", 1}, 317 } table[] = { { "RSAMD5", 1 },
308 {"DH", 2}, 318 { "DH", 2 },
309 {"DSA", 3}, 319 { "DSA", 3 },
310 {"ECC", 4}, 320 { "ECC", 4 },
311 {"RSASHA1", 5}, 321 { "RSASHA1", 5 },
312 {"INDIRECT", 252}, 322 { "INDIRECT", 252 },
313 {"PRIVATEDNS", 253}, 323 { "PRIVATEDNS", 253 },
314 {"PRIVATEOID", 254}, 324 { "PRIVATEOID", 254 },
315 {NULL, 0}}; 325 { NULL, 0 } };
316 unsigned int i; 326 unsigned int i;
317 327
318 for (i = 0; NULL != table[i].mnemonic; i++) 328 for (i = 0; NULL != table[i].mnemonic; i++)
319 if (0 == strcasecmp (mnemonic, table[i].mnemonic)) 329 if (0 == strcasecmp(mnemonic, table[i].mnemonic))
320 return table[i].val; 330 return table[i].val;
321 return 0; 331 return 0;
322} 332}
@@ -334,11 +344,11 @@ rfc4034_mnemonic_to_value (const char *mnemonic)
334 * @return #GNUNET_OK on success 344 * @return #GNUNET_OK on success
335 */ 345 */
336static int 346static int
337dns_string_to_value (void *cls, 347dns_string_to_value(void *cls,
338 uint32_t type, 348 uint32_t type,
339 const char *s, 349 const char *s,
340 void **data, 350 void **data,
341 size_t *data_size) 351 size_t *data_size)
342{ 352{
343 struct in_addr value_a; 353 struct in_addr value_a;
344 struct in6_addr value_aaaa; 354 struct in6_addr value_aaaa;
@@ -347,341 +357,353 @@ dns_string_to_value (void *cls,
347 if (NULL == s) 357 if (NULL == s)
348 return GNUNET_SYSERR; 358 return GNUNET_SYSERR;
349 switch (type) 359 switch (type)
350 {
351 case GNUNET_DNSPARSER_TYPE_A:
352 if (1 != inet_pton (AF_INET, s, &value_a))
353 {
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 _ ("Unable to parse IPv4 address `%s'\n"),
356 s);
357 return GNUNET_SYSERR;
358 }
359 *data = GNUNET_new (struct in_addr);
360 GNUNET_memcpy (*data, &value_a, sizeof (value_a));
361 *data_size = sizeof (value_a);
362 return GNUNET_OK;
363 case GNUNET_DNSPARSER_TYPE_NS: {
364 char nsbuf[256];
365 size_t off;
366
367 off = 0;
368 if (GNUNET_OK !=
369 GNUNET_DNSPARSER_builder_add_name (nsbuf, sizeof (nsbuf), &off, s))
370 {
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 _ ("Failed to serialize NS record with value `%s'\n"),
373 s);
374 return GNUNET_SYSERR;
375 }
376 *data_size = off;
377 *data = GNUNET_malloc (off);
378 GNUNET_memcpy (*data, nsbuf, off);
379 return GNUNET_OK;
380 }
381 case GNUNET_DNSPARSER_TYPE_CNAME: {
382 char cnamebuf[256];
383 size_t off;
384
385 off = 0;
386 if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_name (cnamebuf,
387 sizeof (cnamebuf),
388 &off,
389 s))
390 {
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 _ ("Failed to serialize CNAME record with value `%s'\n"),
393 s);
394 return GNUNET_SYSERR;
395 }
396 *data_size = off;
397 *data = GNUNET_malloc (off);
398 GNUNET_memcpy (*data, cnamebuf, off);
399 return GNUNET_OK;
400 }
401 case GNUNET_DNSPARSER_TYPE_CERT: {
402 char *sdup;
403 const char *typep;
404 const char *keyp;
405 const char *algp;
406 const char *certp;
407 unsigned int type;
408 unsigned int key;
409 unsigned int alg;
410 size_t cert_size;
411 char *cert_data;
412 struct GNUNET_DNSPARSER_CertRecord cert;
413
414 sdup = GNUNET_strdup (s);
415 typep = strtok (sdup, " ");
416 if ((NULL == typep) ||
417 ((0 == (type = rfc4398_mnemonic_to_value (typep))) &&
418 ((1 != sscanf (typep, "%u", &type)) || (type > UINT16_MAX))))
419 {
420 GNUNET_free (sdup);
421 return GNUNET_SYSERR;
422 }
423 keyp = strtok (NULL, " ");
424 if ((NULL == keyp) || (1 != sscanf (keyp, "%u", &key)) ||
425 (key > UINT16_MAX))
426 { 360 {
427 GNUNET_free (sdup); 361 case GNUNET_DNSPARSER_TYPE_A:
428 return GNUNET_SYSERR; 362 if (1 != inet_pton(AF_INET, s, &value_a))
429 } 363 {
430 alg = 0; 364 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
431 algp = strtok (NULL, " "); 365 _("Unable to parse IPv4 address `%s'\n"),
432 if ((NULL == algp) || 366 s);
433 ((0 == (type = rfc4034_mnemonic_to_value (typep))) && 367 return GNUNET_SYSERR;
434 ((1 != sscanf (algp, "%u", &alg)) || (alg > UINT8_MAX)))) 368 }
435 { 369 *data = GNUNET_new(struct in_addr);
436 GNUNET_free (sdup); 370 GNUNET_memcpy(*data, &value_a, sizeof(value_a));
437 return GNUNET_SYSERR; 371 *data_size = sizeof(value_a);
438 } 372 return GNUNET_OK;
439 certp = strtok (NULL, " "); 373
440 if ((NULL == certp) || (0 == strlen (certp))) 374 case GNUNET_DNSPARSER_TYPE_NS: {
441 { 375 char nsbuf[256];
442 GNUNET_free (sdup);
443 return GNUNET_SYSERR;
444 }
445 cert_size = GNUNET_STRINGS_base64_decode (certp,
446 strlen (certp),
447 (void **) &cert_data);
448 GNUNET_free (sdup);
449 cert.cert_type = type;
450 cert.cert_tag = key;
451 cert.algorithm = alg;
452 cert.certificate_size = cert_size;
453 cert.certificate_data = cert_data;
454 {
455 char certbuf[cert_size + sizeof (struct GNUNET_TUN_DnsCertRecord)];
456 size_t off; 376 size_t off;
457 377
458 off = 0; 378 off = 0;
459 if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_cert (certbuf, 379 if (GNUNET_OK !=
460 sizeof (certbuf), 380 GNUNET_DNSPARSER_builder_add_name(nsbuf, sizeof(nsbuf), &off, s))
461 &off, 381 {
462 &cert)) 382 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
463 { 383 _("Failed to serialize NS record with value `%s'\n"),
464 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 384 s);
465 _ ("Failed to serialize CERT record with %u bytes\n"), 385 return GNUNET_SYSERR;
466 (unsigned int) cert_size); 386 }
467 GNUNET_free (cert_data);
468 return GNUNET_SYSERR;
469 }
470 *data_size = off; 387 *data_size = off;
471 *data = GNUNET_malloc (off); 388 *data = GNUNET_malloc(off);
472 GNUNET_memcpy (*data, certbuf, off); 389 GNUNET_memcpy(*data, nsbuf, off);
473 } 390 return GNUNET_OK;
474 GNUNET_free (cert_data);
475 return GNUNET_OK;
476 }
477 case GNUNET_DNSPARSER_TYPE_SOA: {
478 struct GNUNET_DNSPARSER_SoaRecord soa;
479 char soabuf[540];
480 char soa_rname[253 + 1];
481 char soa_mname[253 + 1];
482 unsigned int soa_serial;
483 unsigned int soa_refresh;
484 unsigned int soa_retry;
485 unsigned int soa_expire;
486 unsigned int soa_min;
487 size_t off;
488
489 if (7 != sscanf (s,
490 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
491 soa_rname,
492 soa_mname,
493 &soa_serial,
494 &soa_refresh,
495 &soa_retry,
496 &soa_expire,
497 &soa_min))
498 {
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 _ ("Unable to parse SOA record `%s'\n"),
501 s);
502 return GNUNET_SYSERR;
503 }
504 soa.mname = soa_mname;
505 soa.rname = soa_rname;
506 soa.serial = (uint32_t) soa_serial;
507 soa.refresh = (uint32_t) soa_refresh;
508 soa.retry = (uint32_t) soa_retry;
509 soa.expire = (uint32_t) soa_expire;
510 soa.minimum_ttl = (uint32_t) soa_min;
511 off = 0;
512 if (GNUNET_OK !=
513 GNUNET_DNSPARSER_builder_add_soa (soabuf, sizeof (soabuf), &off, &soa))
514 {
515 GNUNET_log (
516 GNUNET_ERROR_TYPE_ERROR,
517 _ ("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
518 soa_mname,
519 soa_rname);
520 return GNUNET_SYSERR;
521 } 391 }
522 *data_size = off; 392
523 *data = GNUNET_malloc (off); 393 case GNUNET_DNSPARSER_TYPE_CNAME: {
524 GNUNET_memcpy (*data, soabuf, off); 394 char cnamebuf[256];
525 return GNUNET_OK; 395 size_t off;
526 } 396
527 case GNUNET_DNSPARSER_TYPE_PTR: { 397 off = 0;
528 char ptrbuf[256]; 398 if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_name(cnamebuf,
529 size_t off; 399 sizeof(cnamebuf),
530 400 &off,
531 off = 0; 401 s))
532 if (GNUNET_OK != 402 {
533 GNUNET_DNSPARSER_builder_add_name (ptrbuf, sizeof (ptrbuf), &off, s)) 403 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
534 { 404 _("Failed to serialize CNAME record with value `%s'\n"),
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 405 s);
536 _ ("Failed to serialize PTR record with value `%s'\n"), 406 return GNUNET_SYSERR;
537 s); 407 }
538 return GNUNET_SYSERR; 408 *data_size = off;
409 *data = GNUNET_malloc(off);
410 GNUNET_memcpy(*data, cnamebuf, off);
411 return GNUNET_OK;
539 } 412 }
540 *data_size = off; 413
541 *data = GNUNET_malloc (off); 414 case GNUNET_DNSPARSER_TYPE_CERT: {
542 GNUNET_memcpy (*data, ptrbuf, off); 415 char *sdup;
543 return GNUNET_OK; 416 const char *typep;
544 } 417 const char *keyp;
545 case GNUNET_DNSPARSER_TYPE_MX: { 418 const char *algp;
546 struct GNUNET_DNSPARSER_MxRecord mx; 419 const char *certp;
547 char mxbuf[258]; 420 unsigned int type;
548 char mxhost[253 + 1]; 421 unsigned int key;
549 unsigned int mx_pref; 422 unsigned int alg;
550 size_t off; 423 size_t cert_size;
551 424 char *cert_data;
552 if (2 != sscanf (s, "%u,%253s", &mx_pref, mxhost)) 425 struct GNUNET_DNSPARSER_CertRecord cert;
553 { 426
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 427 sdup = GNUNET_strdup(s);
555 _ ("Unable to parse MX record `%s'\n"), 428 typep = strtok(sdup, " ");
556 s); 429 if ((NULL == typep) ||
557 return GNUNET_SYSERR; 430 ((0 == (type = rfc4398_mnemonic_to_value(typep))) &&
431 ((1 != sscanf(typep, "%u", &type)) || (type > UINT16_MAX))))
432 {
433 GNUNET_free(sdup);
434 return GNUNET_SYSERR;
435 }
436 keyp = strtok(NULL, " ");
437 if ((NULL == keyp) || (1 != sscanf(keyp, "%u", &key)) ||
438 (key > UINT16_MAX))
439 {
440 GNUNET_free(sdup);
441 return GNUNET_SYSERR;
442 }
443 alg = 0;
444 algp = strtok(NULL, " ");
445 if ((NULL == algp) ||
446 ((0 == (type = rfc4034_mnemonic_to_value(typep))) &&
447 ((1 != sscanf(algp, "%u", &alg)) || (alg > UINT8_MAX))))
448 {
449 GNUNET_free(sdup);
450 return GNUNET_SYSERR;
451 }
452 certp = strtok(NULL, " ");
453 if ((NULL == certp) || (0 == strlen(certp)))
454 {
455 GNUNET_free(sdup);
456 return GNUNET_SYSERR;
457 }
458 cert_size = GNUNET_STRINGS_base64_decode(certp,
459 strlen(certp),
460 (void **)&cert_data);
461 GNUNET_free(sdup);
462 cert.cert_type = type;
463 cert.cert_tag = key;
464 cert.algorithm = alg;
465 cert.certificate_size = cert_size;
466 cert.certificate_data = cert_data;
467 {
468 char certbuf[cert_size + sizeof(struct GNUNET_TUN_DnsCertRecord)];
469 size_t off;
470
471 off = 0;
472 if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_cert(certbuf,
473 sizeof(certbuf),
474 &off,
475 &cert))
476 {
477 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
478 _("Failed to serialize CERT record with %u bytes\n"),
479 (unsigned int)cert_size);
480 GNUNET_free(cert_data);
481 return GNUNET_SYSERR;
482 }
483 *data_size = off;
484 *data = GNUNET_malloc(off);
485 GNUNET_memcpy(*data, certbuf, off);
486 }
487 GNUNET_free(cert_data);
488 return GNUNET_OK;
558 } 489 }
559 mx.preference = (uint16_t) mx_pref;
560 mx.mxhost = mxhost;
561 off = 0;
562 490
563 if (GNUNET_OK != 491 case GNUNET_DNSPARSER_TYPE_SOA: {
564 GNUNET_DNSPARSER_builder_add_mx (mxbuf, sizeof (mxbuf), &off, &mx)) 492 struct GNUNET_DNSPARSER_SoaRecord soa;
565 { 493 char soabuf[540];
566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 494 char soa_rname[253 + 1];
567 _ ("Failed to serialize MX record with hostname `%s'\n"), 495 char soa_mname[253 + 1];
568 mxhost); 496 unsigned int soa_serial;
569 return GNUNET_SYSERR; 497 unsigned int soa_refresh;
498 unsigned int soa_retry;
499 unsigned int soa_expire;
500 unsigned int soa_min;
501 size_t off;
502
503 if (7 != sscanf(s,
504 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
505 soa_rname,
506 soa_mname,
507 &soa_serial,
508 &soa_refresh,
509 &soa_retry,
510 &soa_expire,
511 &soa_min))
512 {
513 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
514 _("Unable to parse SOA record `%s'\n"),
515 s);
516 return GNUNET_SYSERR;
517 }
518 soa.mname = soa_mname;
519 soa.rname = soa_rname;
520 soa.serial = (uint32_t)soa_serial;
521 soa.refresh = (uint32_t)soa_refresh;
522 soa.retry = (uint32_t)soa_retry;
523 soa.expire = (uint32_t)soa_expire;
524 soa.minimum_ttl = (uint32_t)soa_min;
525 off = 0;
526 if (GNUNET_OK !=
527 GNUNET_DNSPARSER_builder_add_soa(soabuf, sizeof(soabuf), &off, &soa))
528 {
529 GNUNET_log(
530 GNUNET_ERROR_TYPE_ERROR,
531 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
532 soa_mname,
533 soa_rname);
534 return GNUNET_SYSERR;
535 }
536 *data_size = off;
537 *data = GNUNET_malloc(off);
538 GNUNET_memcpy(*data, soabuf, off);
539 return GNUNET_OK;
570 } 540 }
571 *data_size = off; 541
572 *data = GNUNET_malloc (off); 542 case GNUNET_DNSPARSER_TYPE_PTR: {
573 GNUNET_memcpy (*data, mxbuf, off); 543 char ptrbuf[256];
574 return GNUNET_OK; 544 size_t off;
575 } 545
576 case GNUNET_DNSPARSER_TYPE_SRV: { 546 off = 0;
577 struct GNUNET_DNSPARSER_SrvRecord srv; 547 if (GNUNET_OK !=
578 char srvbuf[270]; 548 GNUNET_DNSPARSER_builder_add_name(ptrbuf, sizeof(ptrbuf), &off, s))
579 char srvtarget[253 + 1]; 549 {
580 unsigned int priority; 550 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
581 unsigned int weight; 551 _("Failed to serialize PTR record with value `%s'\n"),
582 unsigned int port; 552 s);
583 size_t off; 553 return GNUNET_SYSERR;
584 554 }
585 if (4 != sscanf (s, "%u %u %u %253s", &priority, &weight, &port, srvtarget)) 555 *data_size = off;
586 { 556 *data = GNUNET_malloc(off);
587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 557 GNUNET_memcpy(*data, ptrbuf, off);
588 _ ("Unable to parse SRV record `%s'\n"), 558 return GNUNET_OK;
589 s);
590 return GNUNET_SYSERR;
591 } 559 }
592 srv.priority = (uint16_t) priority; 560
593 srv.weight = (uint16_t) weight; 561 case GNUNET_DNSPARSER_TYPE_MX: {
594 srv.port = (uint16_t) port; 562 struct GNUNET_DNSPARSER_MxRecord mx;
595 srv.target = srvtarget; 563 char mxbuf[258];
596 off = 0; 564 char mxhost[253 + 1];
597 if (GNUNET_OK != 565 unsigned int mx_pref;
598 GNUNET_DNSPARSER_builder_add_srv (srvbuf, sizeof (srvbuf), &off, &srv)) 566 size_t off;
599 { 567
600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 568 if (2 != sscanf(s, "%u,%253s", &mx_pref, mxhost))
601 _ ("Failed to serialize SRV record with target `%s'\n"), 569 {
602 srvtarget); 570 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
603 return GNUNET_SYSERR; 571 _("Unable to parse MX record `%s'\n"),
572 s);
573 return GNUNET_SYSERR;
574 }
575 mx.preference = (uint16_t)mx_pref;
576 mx.mxhost = mxhost;
577 off = 0;
578
579 if (GNUNET_OK !=
580 GNUNET_DNSPARSER_builder_add_mx(mxbuf, sizeof(mxbuf), &off, &mx))
581 {
582 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
583 _("Failed to serialize MX record with hostname `%s'\n"),
584 mxhost);
585 return GNUNET_SYSERR;
586 }
587 *data_size = off;
588 *data = GNUNET_malloc(off);
589 GNUNET_memcpy(*data, mxbuf, off);
590 return GNUNET_OK;
604 } 591 }
605 *data_size = off; 592
606 *data = GNUNET_malloc (off); 593 case GNUNET_DNSPARSER_TYPE_SRV: {
607 GNUNET_memcpy (*data, srvbuf, off); 594 struct GNUNET_DNSPARSER_SrvRecord srv;
608 return GNUNET_OK; 595 char srvbuf[270];
609 } 596 char srvtarget[253 + 1];
610 case GNUNET_DNSPARSER_TYPE_TXT: 597 unsigned int priority;
611 *data = GNUNET_strdup (s); 598 unsigned int weight;
612 *data_size = strlen (s); 599 unsigned int port;
613 return GNUNET_OK; 600 size_t off;
614 case GNUNET_DNSPARSER_TYPE_AAAA: 601
615 if (1 != inet_pton (AF_INET6, s, &value_aaaa)) 602 if (4 != sscanf(s, "%u %u %u %253s", &priority, &weight, &port, srvtarget))
616 { 603 {
617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 604 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
618 _ ("Unable to parse IPv6 address `%s'\n"), 605 _("Unable to parse SRV record `%s'\n"),
619 s); 606 s);
620 return GNUNET_SYSERR; 607 return GNUNET_SYSERR;
608 }
609 srv.priority = (uint16_t)priority;
610 srv.weight = (uint16_t)weight;
611 srv.port = (uint16_t)port;
612 srv.target = srvtarget;
613 off = 0;
614 if (GNUNET_OK !=
615 GNUNET_DNSPARSER_builder_add_srv(srvbuf, sizeof(srvbuf), &off, &srv))
616 {
617 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
618 _("Failed to serialize SRV record with target `%s'\n"),
619 srvtarget);
620 return GNUNET_SYSERR;
621 }
622 *data_size = off;
623 *data = GNUNET_malloc(off);
624 GNUNET_memcpy(*data, srvbuf, off);
625 return GNUNET_OK;
621 } 626 }
622 *data = GNUNET_new (struct in6_addr); 627
623 *data_size = sizeof (struct in6_addr); 628 case GNUNET_DNSPARSER_TYPE_TXT:
624 GNUNET_memcpy (*data, &value_aaaa, sizeof (value_aaaa)); 629 *data = GNUNET_strdup(s);
625 return GNUNET_OK; 630 *data_size = strlen(s);
626 case GNUNET_DNSPARSER_TYPE_TLSA: { 631 return GNUNET_OK;
627 unsigned int usage; 632
628 unsigned int selector; 633 case GNUNET_DNSPARSER_TYPE_AAAA:
629 unsigned int matching_type; 634 if (1 != inet_pton(AF_INET6, s, &value_aaaa))
630 size_t slen = strlen (s) + 1; 635 {
631 char hex[slen]; 636 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
632 637 _("Unable to parse IPv6 address `%s'\n"),
633 if (4 != sscanf (s, "%u %u %u %s", &usage, &selector, &matching_type, hex)) 638 s);
634 { 639 return GNUNET_SYSERR;
635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 640 }
636 _ ("Unable to parse TLSA record string `%s'\n"), 641 *data = GNUNET_new(struct in6_addr);
637 s); 642 *data_size = sizeof(struct in6_addr);
638 *data_size = 0; 643 GNUNET_memcpy(*data, &value_aaaa, sizeof(value_aaaa));
639 return GNUNET_SYSERR; 644 return GNUNET_OK;
645
646 case GNUNET_DNSPARSER_TYPE_TLSA: {
647 unsigned int usage;
648 unsigned int selector;
649 unsigned int matching_type;
650 size_t slen = strlen(s) + 1;
651 char hex[slen];
652
653 if (4 != sscanf(s, "%u %u %u %s", &usage, &selector, &matching_type, hex))
654 {
655 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
656 _("Unable to parse TLSA record string `%s'\n"),
657 s);
658 *data_size = 0;
659 return GNUNET_SYSERR;
660 }
661
662 *data_size = sizeof(struct GNUNET_TUN_DnsTlsaRecord) + strlen(hex) / 2;
663 *data = tlsa = GNUNET_malloc(*data_size);
664 tlsa->usage = (uint8_t)usage;
665 tlsa->selector = (uint8_t)selector;
666 tlsa->matching_type = (uint8_t)matching_type;
667 if (strlen(hex) / 2 != GNUNET_DNSPARSER_hex_to_bin(hex, &tlsa[1]))
668 {
669 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
670 _("Unable to parse TLSA record string `%s'\n"),
671 s);
672 GNUNET_free(*data);
673 *data = NULL;
674 *data_size = 0;
675 return GNUNET_SYSERR;
676 }
677 return GNUNET_OK;
640 } 678 }
641 679
642 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (hex) / 2; 680 case GNUNET_DNSPARSER_TYPE_CAA: { //RFC6844
643 *data = tlsa = GNUNET_malloc (*data_size); 681 struct GNUNET_DNSPARSER_CaaRecord *caa;
644 tlsa->usage = (uint8_t) usage; 682 unsigned int flags;
645 tlsa->selector = (uint8_t) selector; 683 char tag[15]; //Max tag length 15
646 tlsa->matching_type = (uint8_t) matching_type; 684 char value[strlen(s) + 1]; //Should be more than enough
647 if (strlen (hex) / 2 != GNUNET_DNSPARSER_hex_to_bin (hex, &tlsa[1])) 685
648 { 686 if (3 != sscanf(s, "%u %s %[^\n]", &flags, tag, value))
649 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 687 {
650 _ ("Unable to parse TLSA record string `%s'\n"), 688 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
651 s); 689 _("Unable to parse CAA record string `%s'\n"),
652 GNUNET_free (*data); 690 s);
653 *data = NULL; 691 *data_size = 0;
654 *data_size = 0; 692 return GNUNET_SYSERR;
655 return GNUNET_SYSERR; 693 }
694 *data_size = sizeof(struct GNUNET_DNSPARSER_CaaRecord) + strlen(tag) +
695 strlen(value);
696 *data = caa = GNUNET_malloc(*data_size);
697 caa->flags = flags;
698 memcpy(&caa[1], tag, strlen(tag));
699 caa->tag_len = strlen(tag);
700 memcpy((char *)&caa[1] + caa->tag_len, value, strlen(value));
701 return GNUNET_OK;
656 } 702 }
657 return GNUNET_OK; 703
658 } 704 default:
659 case GNUNET_DNSPARSER_TYPE_CAA: { //RFC6844
660 struct GNUNET_DNSPARSER_CaaRecord *caa;
661 unsigned int flags;
662 char tag[15]; //Max tag length 15
663 char value[strlen (s) + 1]; //Should be more than enough
664
665 if (3 != sscanf (s, "%u %s %[^\n]", &flags, tag, value))
666 {
667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
668 _ ("Unable to parse CAA record string `%s'\n"),
669 s);
670 *data_size = 0;
671 return GNUNET_SYSERR; 705 return GNUNET_SYSERR;
672 } 706 }
673 *data_size = sizeof (struct GNUNET_DNSPARSER_CaaRecord) + strlen (tag) +
674 strlen (value);
675 *data = caa = GNUNET_malloc (*data_size);
676 caa->flags = flags;
677 memcpy (&caa[1], tag, strlen (tag));
678 caa->tag_len = strlen (tag);
679 memcpy ((char *) &caa[1] + caa->tag_len, value, strlen (value));
680 return GNUNET_OK;
681 }
682 default:
683 return GNUNET_SYSERR;
684 }
685} 707}
686 708
687 709
@@ -689,23 +711,22 @@ dns_string_to_value (void *cls,
689 * Mapping of record type numbers to human-readable 711 * Mapping of record type numbers to human-readable
690 * record type names. 712 * record type names.
691 */ 713 */
692static struct 714static struct {
693{
694 const char *name; 715 const char *name;
695 uint32_t number; 716 uint32_t number;
696} name_map[] = {{"A", GNUNET_DNSPARSER_TYPE_A}, 717} name_map[] = { { "A", GNUNET_DNSPARSER_TYPE_A },
697 {"NS", GNUNET_DNSPARSER_TYPE_NS}, 718 { "NS", GNUNET_DNSPARSER_TYPE_NS },
698 {"CNAME", GNUNET_DNSPARSER_TYPE_CNAME}, 719 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
699 {"SOA", GNUNET_DNSPARSER_TYPE_SOA}, 720 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
700 {"PTR", GNUNET_DNSPARSER_TYPE_PTR}, 721 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
701 {"MX", GNUNET_DNSPARSER_TYPE_MX}, 722 { "MX", GNUNET_DNSPARSER_TYPE_MX },
702 {"TXT", GNUNET_DNSPARSER_TYPE_TXT}, 723 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
703 {"AAAA", GNUNET_DNSPARSER_TYPE_AAAA}, 724 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
704 {"SRV", GNUNET_DNSPARSER_TYPE_SRV}, 725 { "SRV", GNUNET_DNSPARSER_TYPE_SRV },
705 {"TLSA", GNUNET_DNSPARSER_TYPE_TLSA}, 726 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
706 {"CERT", GNUNET_DNSPARSER_TYPE_CERT}, 727 { "CERT", GNUNET_DNSPARSER_TYPE_CERT },
707 {"CAA", GNUNET_DNSPARSER_TYPE_CAA}, 728 { "CAA", GNUNET_DNSPARSER_TYPE_CAA },
708 {NULL, UINT32_MAX}}; 729 { NULL, UINT32_MAX } };
709 730
710 731
711/** 732/**
@@ -716,13 +737,13 @@ static struct
716 * @return corresponding number, UINT32_MAX on error 737 * @return corresponding number, UINT32_MAX on error
717 */ 738 */
718static uint32_t 739static uint32_t
719dns_typename_to_number (void *cls, const char *dns_typename) 740dns_typename_to_number(void *cls, const char *dns_typename)
720{ 741{
721 unsigned int i; 742 unsigned int i;
722 743
723 i = 0; 744 i = 0;
724 while ((NULL != name_map[i].name) && 745 while ((NULL != name_map[i].name) &&
725 (0 != strcasecmp (dns_typename, name_map[i].name))) 746 (0 != strcasecmp(dns_typename, name_map[i].name)))
726 i++; 747 i++;
727 return name_map[i].number; 748 return name_map[i].number;
728} 749}
@@ -736,7 +757,7 @@ dns_typename_to_number (void *cls, const char *dns_typename)
736 * @return corresponding typestring, NULL on error 757 * @return corresponding typestring, NULL on error
737 */ 758 */
738static const char * 759static const char *
739dns_number_to_typename (void *cls, uint32_t type) 760dns_number_to_typename(void *cls, uint32_t type)
740{ 761{
741 unsigned int i; 762 unsigned int i;
742 763
@@ -754,11 +775,11 @@ dns_number_to_typename (void *cls, uint32_t type)
754 * @return the exported block API 775 * @return the exported block API
755 */ 776 */
756void * 777void *
757libgnunet_plugin_gnsrecord_dns_init (void *cls) 778libgnunet_plugin_gnsrecord_dns_init(void *cls)
758{ 779{
759 struct GNUNET_GNSRECORD_PluginFunctions *api; 780 struct GNUNET_GNSRECORD_PluginFunctions *api;
760 781
761 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions); 782 api = GNUNET_new(struct GNUNET_GNSRECORD_PluginFunctions);
762 api->value_to_string = &dns_value_to_string; 783 api->value_to_string = &dns_value_to_string;
763 api->string_to_value = &dns_string_to_value; 784 api->string_to_value = &dns_string_to_value;
764 api->typename_to_number = &dns_typename_to_number; 785 api->typename_to_number = &dns_typename_to_number;
@@ -774,11 +795,11 @@ libgnunet_plugin_gnsrecord_dns_init (void *cls)
774 * @return NULL 795 * @return NULL
775 */ 796 */
776void * 797void *
777libgnunet_plugin_gnsrecord_dns_done (void *cls) 798libgnunet_plugin_gnsrecord_dns_done(void *cls)
778{ 799{
779 struct GNUNET_GNSRECORD_PluginFunctions *api = cls; 800 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
780 801
781 GNUNET_free (api); 802 GNUNET_free(api);
782 return NULL; 803 return NULL;
783} 804}
784 805
diff --git a/src/gnsrecord/test_gnsrecord_block_expiration.c b/src/gnsrecord/test_gnsrecord_block_expiration.c
index 1a9dbc105..24aa3d921 100644
--- a/src/gnsrecord/test_gnsrecord_block_expiration.c
+++ b/src/gnsrecord/test_gnsrecord_block_expiration.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file gnsrecord/test_gnsrecord_crypto.c 21 * @file gnsrecord/test_gnsrecord_crypto.c
22 * @brief testcase for block creation, verification and decryption 22 * @brief testcase for block creation, verification and decryption
@@ -44,17 +44,17 @@ static int res;
44 44
45 45
46static void 46static void
47run (void *cls, char *const *args, const char *cfgfile, 47run(void *cls, char *const *args, const char *cfgfile,
48 const struct GNUNET_CONFIGURATION_Handle *cfg) 48 const struct GNUNET_CONFIGURATION_Handle *cfg)
49{ 49{
50 struct GNUNET_GNSRECORD_Data rd[2]; 50 struct GNUNET_GNSRECORD_Data rd[2];
51 struct GNUNET_TIME_Absolute expiration_abs; 51 struct GNUNET_TIME_Absolute expiration_abs;
52 struct GNUNET_TIME_Absolute expiration_abs_shadow; 52 struct GNUNET_TIME_Absolute expiration_abs_shadow;
53 53
54 expiration_abs.abs_value_us = GNUNET_TIME_absolute_get().abs_value_us + 54 expiration_abs.abs_value_us = GNUNET_TIME_absolute_get().abs_value_us +
55 GNUNET_TIME_UNIT_SECONDS.rel_value_us; 55 GNUNET_TIME_UNIT_SECONDS.rel_value_us;
56 expiration_abs_shadow.abs_value_us = GNUNET_TIME_absolute_get().abs_value_us + 56 expiration_abs_shadow.abs_value_us = GNUNET_TIME_absolute_get().abs_value_us +
57 GNUNET_TIME_UNIT_MINUTES.rel_value_us; 57 GNUNET_TIME_UNIT_MINUTES.rel_value_us;
58 58
59 /* create record */ 59 /* create record */
60 rd[0].expiration_time = expiration_abs.abs_value_us; 60 rd[0].expiration_time = expiration_abs.abs_value_us;
@@ -62,42 +62,41 @@ run (void *cls, char *const *args, const char *cfgfile,
62 rd[0].data_size = TEST_RECORD_DATALEN; 62 rd[0].data_size = TEST_RECORD_DATALEN;
63 rd[0].data = GNUNET_malloc(TEST_RECORD_DATALEN); 63 rd[0].data = GNUNET_malloc(TEST_RECORD_DATALEN);
64 rd[0].flags = GNUNET_GNSRECORD_RF_NONE; 64 rd[0].flags = GNUNET_GNSRECORD_RF_NONE;
65 memset ((char *) rd[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); 65 memset((char *)rd[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
66 66
67 rd[1].expiration_time = expiration_abs.abs_value_us; 67 rd[1].expiration_time = expiration_abs.abs_value_us;
68 rd[1].record_type = TEST_RECORD_TYPE; 68 rd[1].record_type = TEST_RECORD_TYPE;
69 rd[1].data_size = TEST_RECORD_DATALEN; 69 rd[1].data_size = TEST_RECORD_DATALEN;
70 rd[1].data = GNUNET_malloc(TEST_RECORD_DATALEN); 70 rd[1].data = GNUNET_malloc(TEST_RECORD_DATALEN);
71 rd[1].flags = GNUNET_GNSRECORD_RF_NONE; 71 rd[1].flags = GNUNET_GNSRECORD_RF_NONE;
72 memset ((char *) rd[1].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); 72 memset((char *)rd[1].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
73 73
74 GNUNET_assert (expiration_abs.abs_value_us == GNUNET_GNSRECORD_record_get_expiration_time(2, rd).abs_value_us); 74 GNUNET_assert(expiration_abs.abs_value_us == GNUNET_GNSRECORD_record_get_expiration_time(2, rd).abs_value_us);
75 75
76 rd[1].expiration_time = expiration_abs_shadow.abs_value_us; 76 rd[1].expiration_time = expiration_abs_shadow.abs_value_us;
77 rd[1].record_type = TEST_RECORD_TYPE; 77 rd[1].record_type = TEST_RECORD_TYPE;
78 rd[1].data_size = TEST_RECORD_DATALEN; 78 rd[1].data_size = TEST_RECORD_DATALEN;
79 rd[1].data = GNUNET_malloc(TEST_RECORD_DATALEN); 79 rd[1].data = GNUNET_malloc(TEST_RECORD_DATALEN);
80 rd[1].flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD; 80 rd[1].flags = GNUNET_GNSRECORD_RF_SHADOW_RECORD;
81 memset ((char *) rd[1].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); 81 memset((char *)rd[1].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
82 82
83 GNUNET_assert (expiration_abs_shadow.abs_value_us == GNUNET_GNSRECORD_record_get_expiration_time(2, rd).abs_value_us); 83 GNUNET_assert(expiration_abs_shadow.abs_value_us == GNUNET_GNSRECORD_record_get_expiration_time(2, rd).abs_value_us);
84 res = 0; 84 res = 0;
85} 85}
86 86
87 87
88int 88int
89main (int argc, char *argv[]) 89main(int argc, char *argv[])
90{ 90{
91 static char *const argvx[] = { "test-gnsrecord-crypto", 91 static char *const argvx[] = { "test-gnsrecord-crypto",
92 NULL 92 NULL };
93 };
94 static struct GNUNET_GETOPT_CommandLineOption options[] = { 93 static struct GNUNET_GETOPT_CommandLineOption options[] = {
95 GNUNET_GETOPT_OPTION_END 94 GNUNET_GETOPT_OPTION_END
96 }; 95 };
97 96
98 res = 1; 97 res = 1;
99 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx, "test-namestore-api", 98 GNUNET_PROGRAM_run((sizeof(argvx) / sizeof(char *)) - 1, argvx, "test-namestore-api",
100 "nohelp", options, &run, &res); 99 "nohelp", options, &run, &res);
101 return res; 100 return res;
102} 101}
103 102
diff --git a/src/gnsrecord/test_gnsrecord_crypto.c b/src/gnsrecord/test_gnsrecord_crypto.c
index 35cc5d34b..6be870e17 100644
--- a/src/gnsrecord/test_gnsrecord_crypto.c
+++ b/src/gnsrecord/test_gnsrecord_crypto.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file gnsrecord/test_gnsrecord_crypto.c 21 * @file gnsrecord/test_gnsrecord_crypto.c
22 * @brief testcase for block creation, verification and decryption 22 * @brief testcase for block creation, verification and decryption
@@ -51,54 +51,53 @@ static int res;
51 51
52 52
53static struct GNUNET_GNSRECORD_Data * 53static struct GNUNET_GNSRECORD_Data *
54create_record (int count) 54create_record(int count)
55{ 55{
56 struct GNUNET_GNSRECORD_Data *rd; 56 struct GNUNET_GNSRECORD_Data *rd;
57 57
58 rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data); 58 rd = GNUNET_new_array(count, struct GNUNET_GNSRECORD_Data);
59 for (unsigned int c = 0; c < count; c++) 59 for (unsigned int c = 0; c < count; c++)
60 { 60 {
61 rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000; 61 rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
62 rd[c].record_type = TEST_RECORD_TYPE; 62 rd[c].record_type = TEST_RECORD_TYPE;
63 rd[c].data_size = TEST_RECORD_DATALEN; 63 rd[c].data_size = TEST_RECORD_DATALEN;
64 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN); 64 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
65 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); 65 memset((char *)rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
66 } 66 }
67 return rd; 67 return rd;
68} 68}
69 69
70 70
71static void 71static void
72rd_decrypt_cb (void *cls, 72rd_decrypt_cb(void *cls,
73 unsigned int rd_count, 73 unsigned int rd_count,
74 const struct GNUNET_GNSRECORD_Data *rd) 74 const struct GNUNET_GNSRECORD_Data *rd)
75{ 75{
76 char rd_cmp_data[TEST_RECORD_DATALEN]; 76 char rd_cmp_data[TEST_RECORD_DATALEN];
77 77
78 GNUNET_assert (RECORDS == rd_count); 78 GNUNET_assert(RECORDS == rd_count);
79 GNUNET_assert (NULL != rd); 79 GNUNET_assert(NULL != rd);
80 memset (rd_cmp_data, 80 memset(rd_cmp_data,
81 'a', 81 'a',
82 TEST_RECORD_DATALEN); 82 TEST_RECORD_DATALEN);
83 for (unsigned int c = 0; c < rd_count; c++) 83 for (unsigned int c = 0; c < rd_count; c++)
84 { 84 {
85 GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type); 85 GNUNET_assert(TEST_RECORD_TYPE == rd[c].record_type);
86 GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size); 86 GNUNET_assert(TEST_RECORD_DATALEN == rd[c].data_size);
87 GNUNET_assert (0 == memcmp (&rd_cmp_data, 87 GNUNET_assert(0 == memcmp(&rd_cmp_data,
88 rd[c].data, 88 rd[c].data,
89 TEST_RECORD_DATALEN)); 89 TEST_RECORD_DATALEN));
90 } 90 }
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 91 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
92 "Block was decrypted successfully \n"); 92 "Block was decrypted successfully \n");
93 res = 0; 93 res = 0;
94
95} 94}
96 95
97static void 96static void
98run (void *cls, 97run(void *cls,
99 char *const *args, 98 char *const *args,
100 const char *cfgfile, 99 const char *cfgfile,
101 const struct GNUNET_CONFIGURATION_Handle *cfg) 100 const struct GNUNET_CONFIGURATION_Handle *cfg)
102{ 101{
103 struct GNUNET_GNSRECORD_Block *block; 102 struct GNUNET_GNSRECORD_Block *block;
104 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey; 103 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
@@ -106,48 +105,48 @@ run (void *cls,
106 struct GNUNET_HashCode query_priv; 105 struct GNUNET_HashCode query_priv;
107 struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get(); 106 struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get();
108 107
109 privkey = GNUNET_CRYPTO_ecdsa_key_create (); 108 privkey = GNUNET_CRYPTO_ecdsa_key_create();
110 GNUNET_assert (NULL != privkey); 109 GNUNET_assert(NULL != privkey);
111 /* get public key */ 110 /* get public key */
112 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, 111 GNUNET_CRYPTO_ecdsa_key_get_public(privkey,
113 &pubkey); 112 &pubkey);
114 113
115 /* test query derivation */ 114 /* test query derivation */
116 GNUNET_GNSRECORD_query_from_private_key (privkey, 115 GNUNET_GNSRECORD_query_from_private_key(privkey,
117 "testlabel",
118 &query_priv);
119 GNUNET_GNSRECORD_query_from_public_key (&pubkey,
120 "testlabel", 116 "testlabel",
121 &query_pub); 117 &query_priv);
122 GNUNET_assert (0 == memcmp (&query_priv, 118 GNUNET_GNSRECORD_query_from_public_key(&pubkey,
123 &query_pub, 119 "testlabel",
124 sizeof (struct GNUNET_HashCode))); 120 &query_pub);
121 GNUNET_assert(0 == memcmp(&query_priv,
122 &query_pub,
123 sizeof(struct GNUNET_HashCode)));
125 /* create record */ 124 /* create record */
126 s_name = "DUMMY.dummy.gnunet"; 125 s_name = "DUMMY.dummy.gnunet";
127 s_rd = create_record (RECORDS); 126 s_rd = create_record(RECORDS);
128 127
129 /* Create block */ 128 /* Create block */
130 GNUNET_assert (NULL != (block = 129 GNUNET_assert(NULL != (block =
131 GNUNET_GNSRECORD_block_create (privkey, 130 GNUNET_GNSRECORD_block_create(privkey,
132 expire, 131 expire,
133 s_name, 132 s_name,
134 s_rd, 133 s_rd,
135 RECORDS))); 134 RECORDS)));
136 GNUNET_assert (GNUNET_OK == 135 GNUNET_assert(GNUNET_OK ==
137 GNUNET_GNSRECORD_block_verify (block)); 136 GNUNET_GNSRECORD_block_verify(block));
138 GNUNET_assert (GNUNET_OK == 137 GNUNET_assert(GNUNET_OK ==
139 GNUNET_GNSRECORD_block_decrypt (block, 138 GNUNET_GNSRECORD_block_decrypt(block,
140 &pubkey, 139 &pubkey,
141 s_name, 140 s_name,
142 &rd_decrypt_cb, 141 &rd_decrypt_cb,
143 s_name)); 142 s_name));
144 GNUNET_free (block); 143 GNUNET_free(block);
145 GNUNET_free (privkey); 144 GNUNET_free(privkey);
146} 145}
147 146
148 147
149int 148int
150main (int argc, char *argv[]) 149main(int argc, char *argv[])
151{ 150{
152 static char *const argvx[] = { 151 static char *const argvx[] = {
153 "test-gnsrecord-crypto", 152 "test-gnsrecord-crypto",
@@ -158,11 +157,11 @@ main (int argc, char *argv[])
158 }; 157 };
159 158
160 res = 1; 159 res = 1;
161 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, 160 GNUNET_PROGRAM_run((sizeof(argvx) / sizeof(char *)) - 1,
162 argvx, 161 argvx,
163 "test-gnsrecord-crypto", 162 "test-gnsrecord-crypto",
164 "nohelp", options, 163 "nohelp", options,
165 &run, &res); 164 &run, &res);
166 return res; 165 return res;
167} 166}
168 167
diff --git a/src/gnsrecord/test_gnsrecord_serialization.c b/src/gnsrecord/test_gnsrecord_serialization.c
index f2ee1a479..34dcff5cb 100644
--- a/src/gnsrecord/test_gnsrecord_serialization.c
+++ b/src/gnsrecord/test_gnsrecord_serialization.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file gnsrecord/test_gnsrecord_serialization.c 21 * @file gnsrecord/test_gnsrecord_serialization.c
22 * @brief testcase for gnsrecord_serialization.c 22 * @brief testcase for gnsrecord_serialization.c
@@ -26,16 +26,16 @@
26#include "gnunet_gnsrecord_lib.h" 26#include "gnunet_gnsrecord_lib.h"
27#include "gnunet_dnsparser_lib.h" 27#include "gnunet_dnsparser_lib.h"
28 28
29#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) 29#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 100)
30 30
31static int res; 31static int res;
32 32
33 33
34static void 34static void
35run (void *cls, 35run(void *cls,
36 char *const *args, 36 char *const *args,
37 const char *cfgfile, 37 const char *cfgfile,
38 const struct GNUNET_CONFIGURATION_Handle *cfg) 38 const struct GNUNET_CONFIGURATION_Handle *cfg)
39{ 39{
40 size_t len; 40 size_t len;
41 int c; 41 int c;
@@ -44,112 +44,111 @@ run (void *cls,
44 size_t data_len; 44 size_t data_len;
45 struct GNUNET_GNSRECORD_Data src[rd_count]; 45 struct GNUNET_GNSRECORD_Data src[rd_count];
46 46
47 memset(src, '\0', rd_count * sizeof (struct GNUNET_GNSRECORD_Data)); 47 memset(src, '\0', rd_count * sizeof(struct GNUNET_GNSRECORD_Data));
48 48
49 data_len = 0; 49 data_len = 0;
50 for (c = 0; c < rd_count; c++) 50 for (c = 0; c < rd_count; c++)
51 { 51 {
52 src[c].record_type = GNUNET_DNSPARSER_TYPE_TXT; 52 src[c].record_type = GNUNET_DNSPARSER_TYPE_TXT;
53 src[c].data_size = data_len; 53 src[c].data_size = data_len;
54 src[c].data = GNUNET_malloc (data_len); 54 src[c].data = GNUNET_malloc(data_len);
55 55
56 /* Setting data to data_len * record_type */ 56 /* Setting data to data_len * record_type */
57 memset ((char *) src[c].data, 'a', data_len); 57 memset((char *)src[c].data, 'a', data_len);
58 data_len += 10; 58 data_len += 10;
59 } 59 }
60 res = 0; 60 res = 0;
61 61
62 len = GNUNET_GNSRECORD_records_get_size (rd_count, src); 62 len = GNUNET_GNSRECORD_records_get_size(rd_count, src);
63 char rd_ser[len]; 63 char rd_ser[len];
64 GNUNET_assert (len == 64 GNUNET_assert(len ==
65 GNUNET_GNSRECORD_records_serialize (rd_count, 65 GNUNET_GNSRECORD_records_serialize(rd_count,
66 src, 66 src,
67 len, 67 len,
68 rd_ser)); 68 rd_ser));
69 69
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 70 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
71 "Serialized data len: %u\n", 71 "Serialized data len: %u\n",
72 (unsigned int) len); 72 (unsigned int)len);
73 73
74 GNUNET_assert (rd_ser != NULL); 74 GNUNET_assert(rd_ser != NULL);
75 { 75 {
76 struct GNUNET_GNSRECORD_Data dst[rd_count]; 76 struct GNUNET_GNSRECORD_Data dst[rd_count];
77 GNUNET_assert (GNUNET_OK == 77 GNUNET_assert(GNUNET_OK ==
78 GNUNET_GNSRECORD_records_deserialize (len, 78 GNUNET_GNSRECORD_records_deserialize(len,
79 rd_ser, 79 rd_ser,
80 rd_count, 80 rd_count,
81 dst)); 81 dst));
82 82
83 GNUNET_assert (dst != NULL); 83 GNUNET_assert(dst != NULL);
84 84
85 for (c = 0; c < rd_count; c++) 85 for (c = 0; c < rd_count; c++)
86 {
87 if (src[c].data_size != dst[c].data_size)
88 {
89 GNUNET_break (0);
90 res = 1;
91 }
92 if (src[c].expiration_time != dst[c].expiration_time)
93 {
94 GNUNET_break (0);
95 res = 1;
96 }
97 if (src[c].flags != dst[c].flags)
98 {
99 GNUNET_break (0);
100 res = 1;
101 }
102 if (src[c].record_type != dst[c].record_type)
103 { 86 {
104 GNUNET_break (0); 87 if (src[c].data_size != dst[c].data_size)
105 res = 1; 88 {
106 } 89 GNUNET_break(0);
107 90 res = 1;
108 { 91 }
109 size_t data_size = src[c].data_size; 92 if (src[c].expiration_time != dst[c].expiration_time)
110 char data[data_size]; 93 {
94 GNUNET_break(0);
95 res = 1;
96 }
97 if (src[c].flags != dst[c].flags)
98 {
99 GNUNET_break(0);
100 res = 1;
101 }
102 if (src[c].record_type != dst[c].record_type)
103 {
104 GNUNET_break(0);
105 res = 1;
106 }
111 107
112 memset (data, 'a', data_size);
113 if (0 != memcmp (data, dst[c].data, data_size))
114 {
115 GNUNET_break (0);
116 res = 1;
117 }
118 if (0 != memcmp (data, src[c].data, data_size))
119 {
120 GNUNET_break (0);
121 res = 1;
122 }
123 if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size))
124 { 108 {
125 GNUNET_break (0); 109 size_t data_size = src[c].data_size;
126 res = 1; 110 char data[data_size];
111
112 memset(data, 'a', data_size);
113 if (0 != memcmp(data, dst[c].data, data_size))
114 {
115 GNUNET_break(0);
116 res = 1;
117 }
118 if (0 != memcmp(data, src[c].data, data_size))
119 {
120 GNUNET_break(0);
121 res = 1;
122 }
123 if (0 != memcmp(src[c].data, dst[c].data, src[c].data_size))
124 {
125 GNUNET_break(0);
126 res = 1;
127 }
127 } 128 }
128 } 129 }
129 } 130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
131 } 131 }
132 132
133 for (c = 0; c < rd_count; c++) 133 for (c = 0; c < rd_count; c++)
134 { 134 {
135 GNUNET_free ((void *)src[c].data); 135 GNUNET_free((void *)src[c].data);
136 } 136 }
137} 137}
138 138
139 139
140int 140int
141main (int argcx, char *argvx[]) 141main(int argcx, char *argvx[])
142{ 142{
143 static char *const argv[] = { "test_gnsrecord_serialization", 143 static char *const argv[] = { "test_gnsrecord_serialization",
144 NULL 144 NULL };
145 };
146 static struct GNUNET_GETOPT_CommandLineOption options[] = { 145 static struct GNUNET_GETOPT_CommandLineOption options[] = {
147 GNUNET_GETOPT_OPTION_END 146 GNUNET_GETOPT_OPTION_END
148 }; 147 };
149 148
150 res = 1; 149 res = 1;
151 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_record_serialization", 150 GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *)) - 1, argv, "test_namestore_record_serialization",
152 "nohelp", options, &run, &res); 151 "nohelp", options, &run, &res);
153 return res; 152 return res;
154} 153}
155 154