aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_flat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/plugin_namestore_flat.c')
-rw-r--r--src/namestore/plugin_namestore_flat.c828
1 files changed, 410 insertions, 418 deletions
diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c
index 4e1d33907..083609359 100644
--- a/src/namestore/plugin_namestore_flat.c
+++ b/src/namestore/plugin_namestore_flat.c
@@ -1,22 +1,22 @@
1 /* 1/*
2 * This file is part of GNUnet 2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015, 2018, 2019 GNUnet e.V. 3 * Copyright (C) 2009-2015, 2018, 2019 GNUnet e.V.
4 * 4 *
5 * GNUnet is free software: you can redistribute it and/or modify it 5 * GNUnet is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Affero General Public License as published 6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, 7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your option) any later version. 8 * or (at your option) any later version.
9 * 9 *
10 * GNUnet is distributed in the hope that it will be useful, but 10 * GNUnet is distributed in the hope that it will be useful, but
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 namestore/plugin_namestore_flat.c 21 * @file namestore/plugin_namestore_flat.c
22 * @brief file-based namestore backend 22 * @brief file-based namestore backend
@@ -33,9 +33,7 @@
33/** 33/**
34 * Context for all functions in this plugin. 34 * Context for all functions in this plugin.
35 */ 35 */
36struct Plugin 36struct Plugin {
37{
38
39 const struct GNUNET_CONFIGURATION_Handle *cfg; 37 const struct GNUNET_CONFIGURATION_Handle *cfg;
40 38
41 /** 39 /**
@@ -47,12 +45,10 @@ struct Plugin
47 * HashMap 45 * HashMap
48 */ 46 */
49 struct GNUNET_CONTAINER_MultiHashMap *hm; 47 struct GNUNET_CONTAINER_MultiHashMap *hm;
50
51}; 48};
52 49
53 50
54struct FlatFileEntry 51struct FlatFileEntry {
55{
56 /** 52 /**
57 * Entry zone 53 * Entry zone
58 */ 54 */
@@ -77,7 +73,6 @@ struct FlatFileEntry
77 * Label 73 * Label
78 */ 74 */
79 char *label; 75 char *label;
80
81}; 76};
82 77
83 78
@@ -89,27 +84,27 @@ struct FlatFileEntry
89 * @param h[out] initialized hash 84 * @param h[out] initialized hash
90 */ 85 */
91static void 86static void
92hash_pkey_and_label (const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, 87hash_pkey_and_label(const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
93 const char *label, 88 const char *label,
94 struct GNUNET_HashCode *h) 89 struct GNUNET_HashCode *h)
95{ 90{
96 char *key; 91 char *key;
97 size_t label_len; 92 size_t label_len;
98 size_t key_len; 93 size_t key_len;
99 94
100 label_len = strlen (label); 95 label_len = strlen(label);
101 key_len = label_len + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 96 key_len = label_len + sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey);
102 key = GNUNET_malloc (key_len); 97 key = GNUNET_malloc(key_len);
103 GNUNET_memcpy (key, 98 GNUNET_memcpy(key,
104 label, 99 label,
105 label_len); 100 label_len);
106 GNUNET_memcpy (key + label_len, 101 GNUNET_memcpy(key + label_len,
107 pkey, 102 pkey,
108 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 103 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
109 GNUNET_CRYPTO_hash (key, 104 GNUNET_CRYPTO_hash(key,
110 key_len, 105 key_len,
111 h); 106 h);
112 GNUNET_free (key); 107 GNUNET_free(key);
113} 108}
114 109
115 110
@@ -122,7 +117,7 @@ hash_pkey_and_label (const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
122 * @return #GNUNET_OK on success 117 * @return #GNUNET_OK on success
123 */ 118 */
124static int 119static int
125database_setup (struct Plugin *plugin) 120database_setup(struct Plugin *plugin)
126{ 121{
127 char *flatdbfile; 122 char *flatdbfile;
128 char *record_data; 123 char *record_data;
@@ -141,188 +136,188 @@ database_setup (struct Plugin *plugin)
141 struct GNUNET_DISK_MapHandle *mh; 136 struct GNUNET_DISK_MapHandle *mh;
142 137
143 if (GNUNET_OK != 138 if (GNUNET_OK !=
144 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, 139 GNUNET_CONFIGURATION_get_value_filename(plugin->cfg,
145 "namestore-flat", 140 "namestore-flat",
146 "FILENAME", 141 "FILENAME",
147 &flatdbfile)) 142 &flatdbfile))
148 {
149 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
150 "namestore-flat",
151 "FILENAME");
152 return GNUNET_SYSERR;
153 }
154 if (GNUNET_OK !=
155 GNUNET_DISK_file_test (flatdbfile))
156 {
157 if (GNUNET_OK !=
158 GNUNET_DISK_directory_create_for_file (flatdbfile))
159 { 143 {
160 GNUNET_break (0); 144 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
161 GNUNET_free (flatdbfile); 145 "namestore-flat",
146 "FILENAME");
162 return GNUNET_SYSERR; 147 return GNUNET_SYSERR;
163 } 148 }
164 } 149 if (GNUNET_OK !=
150 GNUNET_DISK_file_test(flatdbfile))
151 {
152 if (GNUNET_OK !=
153 GNUNET_DISK_directory_create_for_file(flatdbfile))
154 {
155 GNUNET_break(0);
156 GNUNET_free(flatdbfile);
157 return GNUNET_SYSERR;
158 }
159 }
165 /* flatdbfile should be UTF-8-encoded. If it isn't, it's a bug */ 160 /* flatdbfile should be UTF-8-encoded. If it isn't, it's a bug */
166 plugin->fn = flatdbfile; 161 plugin->fn = flatdbfile;
167 162
168 /* Load data from file into hashmap */ 163 /* Load data from file into hashmap */
169 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10, 164 plugin->hm = GNUNET_CONTAINER_multihashmap_create(10,
170 GNUNET_NO); 165 GNUNET_NO);
171 fh = GNUNET_DISK_file_open (flatdbfile, 166 fh = GNUNET_DISK_file_open(flatdbfile,
172 GNUNET_DISK_OPEN_CREATE | 167 GNUNET_DISK_OPEN_CREATE |
173 GNUNET_DISK_OPEN_READWRITE, 168 GNUNET_DISK_OPEN_READWRITE,
174 GNUNET_DISK_PERM_USER_WRITE | 169 GNUNET_DISK_PERM_USER_WRITE |
175 GNUNET_DISK_PERM_USER_READ); 170 GNUNET_DISK_PERM_USER_READ);
176 if (NULL == fh) 171 if (NULL == fh)
177 { 172 {
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 173 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
179 _("Unable to initialize file: %s.\n"), 174 _("Unable to initialize file: %s.\n"),
180 flatdbfile); 175 flatdbfile);
181 return GNUNET_SYSERR; 176 return GNUNET_SYSERR;
182 } 177 }
183 if (GNUNET_SYSERR == 178 if (GNUNET_SYSERR ==
184 GNUNET_DISK_file_size (flatdbfile, 179 GNUNET_DISK_file_size(flatdbfile,
185 &size, 180 &size,
186 GNUNET_YES, 181 GNUNET_YES,
187 GNUNET_YES)) 182 GNUNET_YES))
188 { 183 {
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 184 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
190 _("Unable to get filesize: %s.\n"), 185 _("Unable to get filesize: %s.\n"),
191 flatdbfile); 186 flatdbfile);
192 GNUNET_DISK_file_close (fh); 187 GNUNET_DISK_file_close(fh);
193 return GNUNET_SYSERR; 188 return GNUNET_SYSERR;
194 } 189 }
195 if (size > SIZE_MAX) 190 if (size > SIZE_MAX)
196 { 191 {
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 192 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
198 _("File too big to map: %llu bytes.\n"), 193 _("File too big to map: %llu bytes.\n"),
199 (unsigned long long) size); 194 (unsigned long long)size);
200 GNUNET_DISK_file_close (fh); 195 GNUNET_DISK_file_close(fh);
201 return GNUNET_SYSERR; 196 return GNUNET_SYSERR;
202 } 197 }
203 if (0 == size) 198 if (0 == size)
204 { 199 {
205 GNUNET_DISK_file_close (fh); 200 GNUNET_DISK_file_close(fh);
206 return GNUNET_OK; 201 return GNUNET_OK;
207 } 202 }
208 buffer = GNUNET_DISK_file_map (fh, 203 buffer = GNUNET_DISK_file_map(fh,
209 &mh, 204 &mh,
210 GNUNET_DISK_MAP_TYPE_READ, 205 GNUNET_DISK_MAP_TYPE_READ,
211 size); 206 size);
212 if (NULL == buffer) 207 if (NULL == buffer)
213 { 208 {
214 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 209 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR,
215 "mmap"); 210 "mmap");
216 GNUNET_DISK_file_close (fh); 211 GNUNET_DISK_file_close(fh);
217 return GNUNET_SYSERR; 212 return GNUNET_SYSERR;
218 } 213 }
219 if ('\0' != buffer[size-1]) 214 if ('\0' != buffer[size - 1])
220 { 215 {
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 216 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
222 _("Namestore database file `%s' malformed\n"), 217 _("Namestore database file `%s' malformed\n"),
223 flatdbfile); 218 flatdbfile);
224 GNUNET_DISK_file_unmap (mh); 219 GNUNET_DISK_file_unmap(mh);
225 GNUNET_DISK_file_close (fh); 220 GNUNET_DISK_file_close(fh);
226 return GNUNET_SYSERR; 221 return GNUNET_SYSERR;
227 } 222 }
228 223
229 line = strtok (buffer, "\n"); 224 line = strtok(buffer, "\n");
230 while (NULL != line) 225 while (NULL != line)
231 {
232 zone_private_key = strtok (line, ",");
233 if (NULL == zone_private_key)
234 break;
235 rvalue = strtok (NULL, ",");
236 if (NULL == rvalue)
237 break;
238 record_count = strtok (NULL, ",");
239 if (NULL == record_count)
240 break;
241 record_data_b64 = strtok (NULL, ",");
242 if (NULL == record_data_b64)
243 break;
244 label = strtok (NULL, ",");
245 if (NULL == label)
246 break;
247 line = strtok (NULL, "\n");
248 entry = GNUNET_new (struct FlatFileEntry);
249 { 226 {
250 unsigned long long ll; 227 zone_private_key = strtok(line, ",");
251 228 if (NULL == zone_private_key)
252 if (1 != sscanf (rvalue, 229 break;
253 "%llu", 230 rvalue = strtok(NULL, ",");
254 &ll)) 231 if (NULL == rvalue)
255 { 232 break;
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 233 record_count = strtok(NULL, ",");
257 "Error parsing entry\n"); 234 if (NULL == record_count)
258 GNUNET_free (entry);
259 break; 235 break;
236 record_data_b64 = strtok(NULL, ",");
237 if (NULL == record_data_b64)
238 break;
239 label = strtok(NULL, ",");
240 if (NULL == label)
241 break;
242 line = strtok(NULL, "\n");
243 entry = GNUNET_new(struct FlatFileEntry);
244 {
245 unsigned long long ll;
246
247 if (1 != sscanf(rvalue,
248 "%llu",
249 &ll))
250 {
251 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
252 "Error parsing entry\n");
253 GNUNET_free(entry);
254 break;
255 }
256 entry->rvalue = (uint64_t)ll;
260 } 257 }
261 entry->rvalue = (uint64_t) ll; 258 {
262 } 259 unsigned int ui;
263 { 260
264 unsigned int ui; 261 if (1 != sscanf(record_count,
262 "%u",
263 &ui))
264 {
265 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
266 "Error parsing entry\n");
267 GNUNET_free(entry);
268 break;
269 }
270 entry->record_count = (uint32_t)ui;
271 }
272 entry->label = GNUNET_strdup(label);
273 record_data_size
274 = GNUNET_STRINGS_base64_decode(record_data_b64,
275 strlen(record_data_b64),
276 (void **)&record_data);
277 entry->record_data =
278 GNUNET_new_array(entry->record_count,
279 struct GNUNET_GNSRECORD_Data);
280 if (GNUNET_OK !=
281 GNUNET_GNSRECORD_records_deserialize(record_data_size,
282 record_data,
283 entry->record_count,
284 entry->record_data))
285 {
286 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
287 "Unable to deserialize record %s\n",
288 label);
289 GNUNET_free(entry->label);
290 GNUNET_free(entry);
291 GNUNET_free(record_data);
292 break;
293 }
294 GNUNET_free(record_data);
265 295
266 if (1 != sscanf (record_count,
267 "%u",
268 &ui))
269 { 296 {
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 297 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
271 "Error parsing entry\n"); 298
272 GNUNET_free (entry); 299 GNUNET_STRINGS_base64_decode(zone_private_key,
273 break; 300 strlen(zone_private_key),
301 (void**)&private_key);
302 entry->private_key = *private_key;
303 GNUNET_free(private_key);
274 } 304 }
275 entry->record_count = (uint32_t) ui; 305
276 } 306 hash_pkey_and_label(&entry->private_key,
277 entry->label = GNUNET_strdup (label); 307 label,
278 record_data_size 308 &hkey);
279 = GNUNET_STRINGS_base64_decode (record_data_b64, 309 if (GNUNET_OK !=
280 strlen (record_data_b64), 310 GNUNET_CONTAINER_multihashmap_put(plugin->hm,
281 (void **) &record_data); 311 &hkey,
282 entry->record_data = 312 entry,
283 GNUNET_new_array (entry->record_count, 313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
284 struct GNUNET_GNSRECORD_Data); 314 {
285 if (GNUNET_OK != 315 GNUNET_free(entry);
286 GNUNET_GNSRECORD_records_deserialize (record_data_size, 316 GNUNET_break(0);
287 record_data, 317 }
288 entry->record_count,
289 entry->record_data))
290 {
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 "Unable to deserialize record %s\n",
293 label);
294 GNUNET_free (entry->label);
295 GNUNET_free (entry);
296 GNUNET_free (record_data);
297 break;
298 }
299 GNUNET_free (record_data);
300
301 {
302 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
303
304 GNUNET_STRINGS_base64_decode (zone_private_key,
305 strlen (zone_private_key),
306 (void**)&private_key);
307 entry->private_key = *private_key;
308 GNUNET_free (private_key);
309 }
310
311 hash_pkey_and_label (&entry->private_key,
312 label,
313 &hkey);
314 if (GNUNET_OK !=
315 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
316 &hkey,
317 entry,
318 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
319 {
320 GNUNET_free (entry);
321 GNUNET_break (0);
322 } 318 }
323 } 319 GNUNET_DISK_file_unmap(mh);
324 GNUNET_DISK_file_unmap (mh); 320 GNUNET_DISK_file_close(fh);
325 GNUNET_DISK_file_close (fh);
326 return GNUNET_OK; 321 return GNUNET_OK;
327} 322}
328 323
@@ -335,9 +330,9 @@ database_setup (struct Plugin *plugin)
335 * @param value a `struct FlatFileEntry` 330 * @param value a `struct FlatFileEntry`
336 */ 331 */
337static int 332static int
338store_and_free_entries (void *cls, 333store_and_free_entries(void *cls,
339 const struct GNUNET_HashCode *key, 334 const struct GNUNET_HashCode *key,
340 void *value) 335 void *value)
341{ 336{
342 struct GNUNET_DISK_FileHandle *fh = cls; 337 struct GNUNET_DISK_FileHandle *fh = cls;
343 struct FlatFileEntry *entry = value; 338 struct FlatFileEntry *entry = value;
@@ -346,61 +341,61 @@ store_and_free_entries (void *cls,
346 char *record_data_b64; 341 char *record_data_b64;
347 ssize_t data_size; 342 ssize_t data_size;
348 343
349 (void) key; 344 (void)key;
350 GNUNET_STRINGS_base64_encode (&entry->private_key, 345 GNUNET_STRINGS_base64_encode(&entry->private_key,
351 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 346 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
352 &zone_private_key); 347 &zone_private_key);
353 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count, 348 data_size = GNUNET_GNSRECORD_records_get_size(entry->record_count,
354 entry->record_data); 349 entry->record_data);
355 if (data_size < 0) 350 if (data_size < 0)
356 { 351 {
357 GNUNET_break (0); 352 GNUNET_break(0);
358 GNUNET_free (zone_private_key); 353 GNUNET_free(zone_private_key);
359 return GNUNET_SYSERR; 354 return GNUNET_SYSERR;
360 } 355 }
361 if (data_size >= UINT16_MAX) 356 if (data_size >= UINT16_MAX)
362 { 357 {
363 GNUNET_break (0); 358 GNUNET_break(0);
364 GNUNET_free (zone_private_key); 359 GNUNET_free(zone_private_key);
365 return GNUNET_SYSERR; 360 return GNUNET_SYSERR;
366 } 361 }
367 { 362 {
368 char data[data_size]; 363 char data[data_size];
369 ssize_t ret; 364 ssize_t ret;
370 365
371 ret = GNUNET_GNSRECORD_records_serialize (entry->record_count, 366 ret = GNUNET_GNSRECORD_records_serialize(entry->record_count,
372 entry->record_data, 367 entry->record_data,
373 data_size, 368 data_size,
374 data); 369 data);
375 if ( (ret < 0) || 370 if ((ret < 0) ||
376 (data_size != ret) ) 371 (data_size != ret))
377 { 372 {
378 GNUNET_break (0); 373 GNUNET_break(0);
379 GNUNET_free (zone_private_key); 374 GNUNET_free(zone_private_key);
380 return GNUNET_SYSERR; 375 return GNUNET_SYSERR;
381 } 376 }
382 GNUNET_STRINGS_base64_encode (data, 377 GNUNET_STRINGS_base64_encode(data,
383 data_size, 378 data_size,
384 &record_data_b64); 379 &record_data_b64);
385 } 380 }
386 GNUNET_asprintf (&line, 381 GNUNET_asprintf(&line,
387 "%s,%llu,%u,%s,%s\n", 382 "%s,%llu,%u,%s,%s\n",
388 zone_private_key, 383 zone_private_key,
389 (unsigned long long) entry->rvalue, 384 (unsigned long long)entry->rvalue,
390 (unsigned int) entry->record_count, 385 (unsigned int)entry->record_count,
391 record_data_b64, 386 record_data_b64,
392 entry->label); 387 entry->label);
393 GNUNET_free (record_data_b64); 388 GNUNET_free(record_data_b64);
394 GNUNET_free (zone_private_key); 389 GNUNET_free(zone_private_key);
395 390
396 GNUNET_DISK_file_write (fh, 391 GNUNET_DISK_file_write(fh,
397 line, 392 line,
398 strlen (line)); 393 strlen(line));
399 394
400 GNUNET_free (line); 395 GNUNET_free(line);
401 GNUNET_free (entry->label); 396 GNUNET_free(entry->label);
402 GNUNET_free (entry->record_data); 397 GNUNET_free(entry->record_data);
403 GNUNET_free (entry); 398 GNUNET_free(entry);
404 return GNUNET_YES; 399 return GNUNET_YES;
405} 400}
406 401
@@ -411,33 +406,33 @@ store_and_free_entries (void *cls,
411 * @param plugin the plugin context (state for this module) 406 * @param plugin the plugin context (state for this module)
412 */ 407 */
413static void 408static void
414database_shutdown (struct Plugin *plugin) 409database_shutdown(struct Plugin *plugin)
415{ 410{
416 struct GNUNET_DISK_FileHandle *fh; 411 struct GNUNET_DISK_FileHandle *fh;
417 412
418 fh = GNUNET_DISK_file_open (plugin->fn, 413 fh = GNUNET_DISK_file_open(plugin->fn,
419 GNUNET_DISK_OPEN_CREATE | 414 GNUNET_DISK_OPEN_CREATE |
420 GNUNET_DISK_OPEN_TRUNCATE | 415 GNUNET_DISK_OPEN_TRUNCATE |
421 GNUNET_DISK_OPEN_READWRITE, 416 GNUNET_DISK_OPEN_READWRITE,
422 GNUNET_DISK_PERM_USER_WRITE | 417 GNUNET_DISK_PERM_USER_WRITE |
423 GNUNET_DISK_PERM_USER_READ); 418 GNUNET_DISK_PERM_USER_READ);
424 if (NULL == fh) 419 if (NULL == fh)
425 { 420 {
426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 421 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
427 _("Unable to initialize file: %s.\n"), 422 _("Unable to initialize file: %s.\n"),
428 plugin->fn); 423 plugin->fn);
429 return; 424 return;
430 } 425 }
431 426
432 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, 427 GNUNET_CONTAINER_multihashmap_iterate(plugin->hm,
433 &store_and_free_entries, 428 &store_and_free_entries,
434 fh); 429 fh);
435 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm); 430 GNUNET_CONTAINER_multihashmap_destroy(plugin->hm);
436 /* append 0-terminator */ 431 /* append 0-terminator */
437 GNUNET_DISK_file_write (fh, 432 GNUNET_DISK_file_write(fh,
438 "", 433 "",
439 1); 434 1);
440 GNUNET_DISK_file_close (fh); 435 GNUNET_DISK_file_close(fh);
441} 436}
442 437
443 438
@@ -453,57 +448,57 @@ database_shutdown (struct Plugin *plugin)
453 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 448 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
454 */ 449 */
455static int 450static int
456namestore_flat_store_records (void *cls, 451namestore_flat_store_records(void *cls,
457 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, 452 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
458 const char *label, 453 const char *label,
459 unsigned int rd_count, 454 unsigned int rd_count,
460 const struct GNUNET_GNSRECORD_Data *rd) 455 const struct GNUNET_GNSRECORD_Data *rd)
461{ 456{
462 struct Plugin *plugin = cls; 457 struct Plugin *plugin = cls;
463 uint64_t rvalue; 458 uint64_t rvalue;
464 struct GNUNET_HashCode hkey; 459 struct GNUNET_HashCode hkey;
465 struct FlatFileEntry *entry; 460 struct FlatFileEntry *entry;
466 461
467 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 462 rvalue = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
468 UINT64_MAX); 463 UINT64_MAX);
469 hash_pkey_and_label (zone_key, 464 hash_pkey_and_label(zone_key,
470 label, 465 label,
471 &hkey); 466 &hkey);
472 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, 467 GNUNET_CONTAINER_multihashmap_remove_all(plugin->hm,
473 &hkey); 468 &hkey);
474 if (0 == rd_count) 469 if (0 == rd_count)
475 { 470 {
476 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 471 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
477 "sqlite", 472 "sqlite",
478 "Record deleted\n"); 473 "Record deleted\n");
479 return GNUNET_OK; 474 return GNUNET_OK;
480 } 475 }
481 entry = GNUNET_new (struct FlatFileEntry); 476 entry = GNUNET_new(struct FlatFileEntry);
482 GNUNET_asprintf (&entry->label, 477 GNUNET_asprintf(&entry->label,
483 label, 478 label,
484 strlen (label)); 479 strlen(label));
485 GNUNET_memcpy (&entry->private_key, 480 GNUNET_memcpy(&entry->private_key,
486 zone_key, 481 zone_key,
487 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 482 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
488 entry->rvalue = rvalue; 483 entry->rvalue = rvalue;
489 entry->record_count = rd_count; 484 entry->record_count = rd_count;
490 entry->record_data = GNUNET_new_array (rd_count, 485 entry->record_data = GNUNET_new_array(rd_count,
491 struct GNUNET_GNSRECORD_Data); 486 struct GNUNET_GNSRECORD_Data);
492 for (unsigned int i = 0; i < rd_count; i++) 487 for (unsigned int i = 0; i < rd_count; i++)
493 { 488 {
494 entry->record_data[i].expiration_time = rd[i].expiration_time; 489 entry->record_data[i].expiration_time = rd[i].expiration_time;
495 entry->record_data[i].record_type = rd[i].record_type; 490 entry->record_data[i].record_type = rd[i].record_type;
496 entry->record_data[i].flags = rd[i].flags; 491 entry->record_data[i].flags = rd[i].flags;
497 entry->record_data[i].data_size = rd[i].data_size; 492 entry->record_data[i].data_size = rd[i].data_size;
498 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size); 493 entry->record_data[i].data = GNUNET_malloc(rd[i].data_size);
499 GNUNET_memcpy ((char*)entry->record_data[i].data, 494 GNUNET_memcpy((char*)entry->record_data[i].data,
500 rd[i].data, 495 rd[i].data,
501 rd[i].data_size); 496 rd[i].data_size);
502 } 497 }
503 return GNUNET_CONTAINER_multihashmap_put (plugin->hm, 498 return GNUNET_CONTAINER_multihashmap_put(plugin->hm,
504 &hkey, 499 &hkey,
505 entry, 500 entry,
506 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 501 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
507} 502}
508 503
509 504
@@ -518,36 +513,36 @@ namestore_flat_store_records (void *cls,
518 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR 513 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
519 */ 514 */
520static int 515static int
521namestore_flat_lookup_records (void *cls, 516namestore_flat_lookup_records(void *cls,
522 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 517 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
523 const char *label, 518 const char *label,
524 GNUNET_NAMESTORE_RecordIterator iter, 519 GNUNET_NAMESTORE_RecordIterator iter,
525 void *iter_cls) 520 void *iter_cls)
526{ 521{
527 struct Plugin *plugin = cls; 522 struct Plugin *plugin = cls;
528 struct FlatFileEntry *entry; 523 struct FlatFileEntry *entry;
529 struct GNUNET_HashCode hkey; 524 struct GNUNET_HashCode hkey;
530 525
531 if (NULL == zone) 526 if (NULL == zone)
532 { 527 {
533 GNUNET_break (0); 528 GNUNET_break(0);
534 return GNUNET_SYSERR; 529 return GNUNET_SYSERR;
535 } 530 }
536 hash_pkey_and_label (zone, 531 hash_pkey_and_label(zone,
537 label, 532 label,
538 &hkey); 533 &hkey);
539 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, 534 entry = GNUNET_CONTAINER_multihashmap_get(plugin->hm,
540 &hkey); 535 &hkey);
541 536
542 if (NULL == entry) 537 if (NULL == entry)
543 return GNUNET_NO; 538 return GNUNET_NO;
544 if (NULL != iter) 539 if (NULL != iter)
545 iter (iter_cls, 540 iter(iter_cls,
546 1, /* zero is illegal */ 541 1, /* zero is illegal */
547 &entry->private_key, 542 &entry->private_key,
548 entry->label, 543 entry->label,
549 entry->record_count, 544 entry->record_count,
550 entry->record_data); 545 entry->record_data);
551 return GNUNET_YES; 546 return GNUNET_YES;
552} 547}
553 548
@@ -555,8 +550,7 @@ namestore_flat_lookup_records (void *cls,
555/** 550/**
556 * Closure for #iterate_zones. 551 * Closure for #iterate_zones.
557 */ 552 */
558struct IterateContext 553struct IterateContext {
559{
560 /** 554 /**
561 * How many more records should we skip before returning results? 555 * How many more records should we skip before returning results?
562 */ 556 */
@@ -587,7 +581,6 @@ struct IterateContext
587 * Closure for @e iter. 581 * Closure for @e iter.
588 */ 582 */
589 void *iter_cls; 583 void *iter_cls;
590
591}; 584};
592 585
593 586
@@ -600,34 +593,34 @@ struct IterateContext
600 * @return #GNUNET_YES to continue the iteration 593 * @return #GNUNET_YES to continue the iteration
601 */ 594 */
602static int 595static int
603iterate_zones (void *cls, 596iterate_zones(void *cls,
604 const struct GNUNET_HashCode *key, 597 const struct GNUNET_HashCode *key,
605 void *value) 598 void *value)
606{ 599{
607 struct IterateContext *ic = cls; 600 struct IterateContext *ic = cls;
608 struct FlatFileEntry *entry = value; 601 struct FlatFileEntry *entry = value;
609 602
610 (void) key; 603 (void)key;
611 if (0 == ic->limit) 604 if (0 == ic->limit)
612 return GNUNET_NO; 605 return GNUNET_NO;
613 if ( (NULL != ic->zone) && 606 if ((NULL != ic->zone) &&
614 (0 != GNUNET_memcmp (&entry->private_key, 607 (0 != GNUNET_memcmp(&entry->private_key,
615 ic->zone)) ) 608 ic->zone)))
616 return GNUNET_YES; 609 return GNUNET_YES;
617 ic->pos++; 610 ic->pos++;
618 if (ic->offset > 0) 611 if (ic->offset > 0)
619 { 612 {
620 ic->offset--; 613 ic->offset--;
621 return GNUNET_YES; 614 return GNUNET_YES;
622 } 615 }
623 ic->iter (ic->iter_cls, 616 ic->iter(ic->iter_cls,
624 ic->pos, 617 ic->pos,
625 (NULL == ic->zone) 618 (NULL == ic->zone)
626 ? &entry->private_key 619 ? &entry->private_key
627 : ic->zone, 620 : ic->zone,
628 entry->label, 621 entry->label,
629 entry->record_count, 622 entry->record_count,
630 entry->record_data); 623 entry->record_data);
631 ic->limit--; 624 ic->limit--;
632 if (0 == ic->limit) 625 if (0 == ic->limit)
633 return GNUNET_NO; 626 return GNUNET_NO;
@@ -648,12 +641,12 @@ iterate_zones (void *cls,
648 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error 641 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
649 */ 642 */
650static int 643static int
651namestore_flat_iterate_records (void *cls, 644namestore_flat_iterate_records(void *cls,
652 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 645 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
653 uint64_t serial, 646 uint64_t serial,
654 uint64_t limit, 647 uint64_t limit,
655 GNUNET_NAMESTORE_RecordIterator iter, 648 GNUNET_NAMESTORE_RecordIterator iter,
656 void *iter_cls) 649 void *iter_cls)
657{ 650{
658 struct Plugin *plugin = cls; 651 struct Plugin *plugin = cls;
659 struct IterateContext ic; 652 struct IterateContext ic;
@@ -664,9 +657,9 @@ namestore_flat_iterate_records (void *cls,
664 ic.iter = iter; 657 ic.iter = iter;
665 ic.iter_cls = iter_cls; 658 ic.iter_cls = iter_cls;
666 ic.zone = zone; 659 ic.zone = zone;
667 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, 660 GNUNET_CONTAINER_multihashmap_iterate(plugin->hm,
668 &iterate_zones, 661 &iterate_zones,
669 &ic); 662 &ic);
670 return (0 == ic.limit) ? GNUNET_OK : GNUNET_NO; 663 return (0 == ic.limit) ? GNUNET_OK : GNUNET_NO;
671} 664}
672 665
@@ -674,8 +667,7 @@ namestore_flat_iterate_records (void *cls,
674/** 667/**
675 * Closure for #zone_to_name. 668 * Closure for #zone_to_name.
676 */ 669 */
677struct ZoneToNameContext 670struct ZoneToNameContext {
678{
679 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone; 671 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone;
680 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone; 672 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone;
681 GNUNET_NAMESTORE_RecordIterator iter; 673 GNUNET_NAMESTORE_RecordIterator iter;
@@ -686,35 +678,35 @@ struct ZoneToNameContext
686 678
687 679
688static int 680static int
689zone_to_name (void *cls, 681zone_to_name(void *cls,
690 const struct GNUNET_HashCode *key, 682 const struct GNUNET_HashCode *key,
691 void *value) 683 void *value)
692{ 684{
693 struct ZoneToNameContext *ztn = cls; 685 struct ZoneToNameContext *ztn = cls;
694 struct FlatFileEntry *entry = value; 686 struct FlatFileEntry *entry = value;
695 687
696 (void) key; 688 (void)key;
697 if (0 != GNUNET_memcmp (&entry->private_key, 689 if (0 != GNUNET_memcmp(&entry->private_key,
698 ztn->zone)) 690 ztn->zone))
699 return GNUNET_YES; 691 return GNUNET_YES;
700 692
701 for (unsigned int i = 0; i < entry->record_count; i++) 693 for (unsigned int i = 0; i < entry->record_count; i++)
702 {
703 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
704 continue;
705 if (0 == memcmp (ztn->value_zone,
706 entry->record_data[i].data,
707 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
708 { 694 {
709 ztn->iter (ztn->iter_cls, 695 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
710 i + 1, /* zero is illegal! */ 696 continue;
711 &entry->private_key, 697 if (0 == memcmp(ztn->value_zone,
712 entry->label, 698 entry->record_data[i].data,
713 entry->record_count, 699 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)))
714 entry->record_data); 700 {
715 ztn->result_found = GNUNET_YES; 701 ztn->iter(ztn->iter_cls,
702 i + 1, /* zero is illegal! */
703 &entry->private_key,
704 entry->label,
705 entry->record_count,
706 entry->record_data);
707 ztn->result_found = GNUNET_YES;
708 }
716 } 709 }
717 }
718 return GNUNET_YES; 710 return GNUNET_YES;
719} 711}
720 712
@@ -731,11 +723,11 @@ zone_to_name (void *cls,
731 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 723 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
732 */ 724 */
733static int 725static int
734namestore_flat_zone_to_name (void *cls, 726namestore_flat_zone_to_name(void *cls,
735 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 727 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
736 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, 728 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
737 GNUNET_NAMESTORE_RecordIterator iter, 729 GNUNET_NAMESTORE_RecordIterator iter,
738 void *iter_cls) 730 void *iter_cls)
739{ 731{
740 struct Plugin *plugin = cls; 732 struct Plugin *plugin = cls;
741 struct ZoneToNameContext ztn = { 733 struct ZoneToNameContext ztn = {
@@ -746,12 +738,12 @@ namestore_flat_zone_to_name (void *cls,
746 .result_found = GNUNET_NO 738 .result_found = GNUNET_NO
747 }; 739 };
748 740
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 741 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
750 "Performing reverse lookup for `%s'\n", 742 "Performing reverse lookup for `%s'\n",
751 GNUNET_GNSRECORD_z2s (value_zone)); 743 GNUNET_GNSRECORD_z2s(value_zone));
752 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, 744 GNUNET_CONTAINER_multihashmap_iterate(plugin->hm,
753 &zone_to_name, 745 &zone_to_name,
754 &ztn); 746 &ztn);
755 return ztn.result_found; 747 return ztn.result_found;
756} 748}
757 749
@@ -763,7 +755,7 @@ namestore_flat_zone_to_name (void *cls,
763 * @return NULL on error, otherwise the plugin context 755 * @return NULL on error, otherwise the plugin context
764 */ 756 */
765void * 757void *
766libgnunet_plugin_namestore_flat_init (void *cls) 758libgnunet_plugin_namestore_flat_init(void *cls)
767{ 759{
768 static struct Plugin plugin; 760 static struct Plugin plugin;
769 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 761 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
@@ -771,23 +763,23 @@ libgnunet_plugin_namestore_flat_init (void *cls)
771 763
772 if (NULL != plugin.cfg) 764 if (NULL != plugin.cfg)
773 return NULL; /* can only initialize once! */ 765 return NULL; /* can only initialize once! */
774 memset (&plugin, 766 memset(&plugin,
775 0, 767 0,
776 sizeof (struct Plugin)); 768 sizeof(struct Plugin));
777 plugin.cfg = cfg; 769 plugin.cfg = cfg;
778 if (GNUNET_OK != database_setup (&plugin)) 770 if (GNUNET_OK != database_setup(&plugin))
779 { 771 {
780 database_shutdown (&plugin); 772 database_shutdown(&plugin);
781 return NULL; 773 return NULL;
782 } 774 }
783 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); 775 api = GNUNET_new(struct GNUNET_NAMESTORE_PluginFunctions);
784 api->cls = &plugin; 776 api->cls = &plugin;
785 api->store_records = &namestore_flat_store_records; 777 api->store_records = &namestore_flat_store_records;
786 api->iterate_records = &namestore_flat_iterate_records; 778 api->iterate_records = &namestore_flat_iterate_records;
787 api->zone_to_name = &namestore_flat_zone_to_name; 779 api->zone_to_name = &namestore_flat_zone_to_name;
788 api->lookup_records = &namestore_flat_lookup_records; 780 api->lookup_records = &namestore_flat_lookup_records;
789 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 781 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
790 _("Flat file database running\n")); 782 _("Flat file database running\n"));
791 return api; 783 return api;
792} 784}
793 785
@@ -799,16 +791,16 @@ libgnunet_plugin_namestore_flat_init (void *cls)
799 * @return always NULL 791 * @return always NULL
800 */ 792 */
801void * 793void *
802libgnunet_plugin_namestore_flat_done (void *cls) 794libgnunet_plugin_namestore_flat_done(void *cls)
803{ 795{
804 struct GNUNET_NAMESTORE_PluginFunctions *api = cls; 796 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
805 struct Plugin *plugin = api->cls; 797 struct Plugin *plugin = api->cls;
806 798
807 database_shutdown (plugin); 799 database_shutdown(plugin);
808 plugin->cfg = NULL; 800 plugin->cfg = NULL;
809 GNUNET_free (api); 801 GNUNET_free(api);
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 802 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
811 "Flat file plugin is finished\n"); 803 "Flat file plugin is finished\n");
812 return NULL; 804 return NULL;
813} 805}
814 806