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