diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2013-10-28 14:02:20 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2013-10-28 14:02:20 +0000 |
commit | 39dceb2cfe324e3f1b44958e3cf4cb936ab76881 (patch) | |
tree | be84fba9fed2e859805475b6bff5a8bd3915e854 /src/namestore | |
parent | 50a4192766dd96f383020f9b2989b047155e4db3 (diff) | |
download | gnunet-39dceb2cfe324e3f1b44958e3cf4cb936ab76881.tar.gz gnunet-39dceb2cfe324e3f1b44958e3cf4cb936ab76881.zip |
extended plugin api to support lookup function
added new index to both plugins
implemented lookup functionality in both plugins
namestore uses lookup function
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/gnunet-service-namestore.c | 16 | ||||
-rw-r--r-- | src/namestore/namestore.h | 5 | ||||
-rw-r--r-- | src/namestore/namestore_api.c | 14 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_postgres.c | 51 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_sqlite.c | 59 |
5 files changed, 128 insertions, 17 deletions
diff --git a/src/namestore/gnunet-service-namestore.c b/src/namestore/gnunet-service-namestore.c index 0fce443c7..2353eadec 100644 --- a/src/namestore/gnunet-service-namestore.c +++ b/src/namestore/gnunet-service-namestore.c | |||
@@ -623,7 +623,6 @@ handle_record_lookup (void *cls, | |||
623 | uint32_t name_len; | 623 | uint32_t name_len; |
624 | size_t src_size; | 624 | size_t src_size; |
625 | size_t res_size; | 625 | size_t res_size; |
626 | int offset; | ||
627 | int res; | 626 | int res; |
628 | 627 | ||
629 | if (ntohs (message->size) < sizeof (struct LabelLookupMessage)) | 628 | if (ntohs (message->size) < sizeof (struct LabelLookupMessage)) |
@@ -664,15 +663,8 @@ handle_record_lookup (void *cls, | |||
664 | rlc.rd_ser_len = 0; | 663 | rlc.rd_ser_len = 0; |
665 | rlc.res_rd = NULL; | 664 | rlc.res_rd = NULL; |
666 | 665 | ||
667 | offset = 0; | 666 | res = GSN_database->lookup_records (GSN_database->cls, |
668 | do | 667 | &ll_msg->zone, name_tmp, &lookup_it, &rlc); |
669 | { | ||
670 | /* changee this call */ | ||
671 | res = GSN_database->iterate_records (GSN_database->cls, | ||
672 | &ll_msg->zone, offset, &lookup_it, &rlc); | ||
673 | offset++; | ||
674 | } | ||
675 | while ((GNUNET_NO == rlc.found) && (GNUNET_OK == res)); | ||
676 | 668 | ||
677 | res_size = sizeof (struct LabelLookupResponseMessage) + name_len + rlc.rd_ser_len; | 669 | res_size = sizeof (struct LabelLookupResponseMessage) + name_len + rlc.rd_ser_len; |
678 | llr_msg = GNUNET_malloc (res_size); | 670 | llr_msg = GNUNET_malloc (res_size); |
@@ -684,6 +676,10 @@ handle_record_lookup (void *cls, | |||
684 | llr_msg->rd_count = htons (rlc.res_rd_count); | 676 | llr_msg->rd_count = htons (rlc.res_rd_count); |
685 | llr_msg->rd_len = htons (rlc.rd_ser_len); | 677 | llr_msg->rd_len = htons (rlc.rd_ser_len); |
686 | res_name = (char *) &llr_msg[1]; | 678 | res_name = (char *) &llr_msg[1]; |
679 | if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res)) | ||
680 | llr_msg->found = ntohs (GNUNET_YES); | ||
681 | else | ||
682 | llr_msg->found = ntohs (GNUNET_NO); | ||
687 | memcpy (&llr_msg[1], name_tmp, name_len); | 683 | memcpy (&llr_msg[1], name_tmp, name_len); |
688 | memcpy (&res_name[name_len], rlc.res_rd, rlc.rd_ser_len); | 684 | memcpy (&res_name[name_len], rlc.res_rd, rlc.rd_ser_len); |
689 | 685 | ||
diff --git a/src/namestore/namestore.h b/src/namestore/namestore.h index f403d589c..6bde8ab41 100644 --- a/src/namestore/namestore.h +++ b/src/namestore/namestore.h | |||
@@ -168,9 +168,10 @@ struct LabelLookupResponseMessage | |||
168 | uint16_t rd_count GNUNET_PACKED; | 168 | uint16_t rd_count GNUNET_PACKED; |
169 | 169 | ||
170 | /** | 170 | /** |
171 | * always zero (for alignment) | 171 | * Was the label found in the database?? |
172 | * GNUNET_YES or GNUNET_NO | ||
172 | */ | 173 | */ |
173 | uint16_t reserved GNUNET_PACKED; | 174 | uint16_t found GNUNET_PACKED; |
174 | 175 | ||
175 | /** | 176 | /** |
176 | * The private key of the authority. | 177 | * The private key of the authority. |
diff --git a/src/namestore/namestore_api.c b/src/namestore/namestore_api.c index 303b05c98..c05386908 100644 --- a/src/namestore/namestore_api.c +++ b/src/namestore/namestore_api.c | |||
@@ -300,6 +300,7 @@ handle_lookup_result (struct GNUNET_NAMESTORE_QueueEntry *qe, | |||
300 | size_t name_len; | 300 | size_t name_len; |
301 | size_t rd_len; | 301 | size_t rd_len; |
302 | unsigned int rd_count; | 302 | unsigned int rd_count; |
303 | int found; | ||
303 | 304 | ||
304 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 305 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
305 | "Received `%s'\n", | 306 | "Received `%s'\n", |
@@ -309,7 +310,7 @@ handle_lookup_result (struct GNUNET_NAMESTORE_QueueEntry *qe, | |||
309 | rd_count = ntohs (msg->rd_count); | 310 | rd_count = ntohs (msg->rd_count); |
310 | msg_len = ntohs (msg->gns_header.header.size); | 311 | msg_len = ntohs (msg->gns_header.header.size); |
311 | name_len = ntohs (msg->name_len); | 312 | name_len = ntohs (msg->name_len); |
312 | GNUNET_break (0 == ntohs (msg->reserved)); | 313 | found = ntohs (msg->found); |
313 | exp_msg_len = sizeof (struct LabelLookupResponseMessage) + name_len + rd_len; | 314 | exp_msg_len = sizeof (struct LabelLookupResponseMessage) + name_len + rd_len; |
314 | if (msg_len != exp_msg_len) | 315 | if (msg_len != exp_msg_len) |
315 | { | 316 | { |
@@ -323,6 +324,17 @@ handle_lookup_result (struct GNUNET_NAMESTORE_QueueEntry *qe, | |||
323 | GNUNET_break (0); | 324 | GNUNET_break (0); |
324 | return GNUNET_SYSERR; | 325 | return GNUNET_SYSERR; |
325 | } | 326 | } |
327 | if (GNUNET_NO == found) | ||
328 | { | ||
329 | /* label was not in namestore */ | ||
330 | if (NULL != qe->proc) | ||
331 | qe->proc (qe->proc_cls, | ||
332 | &msg->private_key, | ||
333 | name, | ||
334 | 0, NULL); | ||
335 | return GNUNET_OK; | ||
336 | } | ||
337 | |||
326 | rd_tmp = &name[name_len]; | 338 | rd_tmp = &name[name_len]; |
327 | { | 339 | { |
328 | struct GNUNET_GNSRECORD_Data rd[rd_count]; | 340 | struct GNUNET_GNSRECORD_Data rd[rd_count]; |
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c index e84413400..cf52e0bac 100644 --- a/src/namestore/plugin_namestore_postgres.c +++ b/src/namestore/plugin_namestore_postgres.c | |||
@@ -86,8 +86,9 @@ create_indices (PGconn * dbh) | |||
86 | GNUNET_POSTGRES_exec (dbh, | 86 | GNUNET_POSTGRES_exec (dbh, |
87 | "CREATE INDEX ir_pkey_iter ON ns097records (zone_private_key,rvalue)")) || | 87 | "CREATE INDEX ir_pkey_iter ON ns097records (zone_private_key,rvalue)")) || |
88 | (GNUNET_OK != | 88 | (GNUNET_OK != |
89 | GNUNET_POSTGRES_exec (dbh, | 89 | GNUNET_POSTGRES_exec (dbh, "CREATE INDEX it_iter ON ns097records (rvalue)")) || |
90 | "CREATE INDEX it_iter ON ns097records (rvalue)")) ) | 90 | (GNUNET_OK != |
91 | GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_label ON ns097records (label)")) ) | ||
91 | LOG (GNUNET_ERROR_TYPE_ERROR, | 92 | LOG (GNUNET_ERROR_TYPE_ERROR, |
92 | _("Failed to create indices\n")); | 93 | _("Failed to create indices\n")); |
93 | } | 94 | } |
@@ -178,7 +179,12 @@ database_setup (struct Plugin *plugin) | |||
178 | GNUNET_POSTGRES_prepare (plugin->dbh, | 179 | GNUNET_POSTGRES_prepare (plugin->dbh, |
179 | "iterate_all_zones", | 180 | "iterate_all_zones", |
180 | "SELECT record_count,record_data,label,zone_private_key" | 181 | "SELECT record_count,record_data,label,zone_private_key" |
181 | " FROM ns097records ORDER BY rvalue LIMIT 1 OFFSET $1", 1))) | 182 | " FROM ns097records ORDER BY rvalue LIMIT 1 OFFSET $1", 1)) || |
183 | (GNUNET_OK != | ||
184 | GNUNET_POSTGRES_prepare (plugin->dbh, | ||
185 | "lookup_label", | ||
186 | "SELECT record_count,record_data,label,zone_private_key" | ||
187 | " FROM ns097records WHERE records zone_private_key=$1 AND label=$2", 2))) | ||
182 | { | 188 | { |
183 | PQfinish (plugin->dbh); | 189 | PQfinish (plugin->dbh); |
184 | plugin->dbh = NULL; | 190 | plugin->dbh = NULL; |
@@ -370,6 +376,44 @@ get_record_and_call_iterator (struct Plugin *plugin, | |||
370 | 376 | ||
371 | 377 | ||
372 | /** | 378 | /** |
379 | * Lookup records in the datastore for which we are the authority. | ||
380 | * | ||
381 | * @param cls closure (internal context for the plugin) | ||
382 | * @param zone private key of the zone | ||
383 | * @param label name of the record in the zone | ||
384 | * @param iter function to call with the result | ||
385 | * @param iter_cls closure for @a iter | ||
386 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR | ||
387 | */ | ||
388 | static int | ||
389 | namestore_postgres_lookup_records (void *cls, | ||
390 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, | ||
391 | GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls) | ||
392 | { | ||
393 | struct Plugin *plugin = cls; | ||
394 | const char *paramValues[] = { | ||
395 | (const char *) zone, | ||
396 | label | ||
397 | }; | ||
398 | int paramLengths[] = { | ||
399 | sizeof (*zone), | ||
400 | strlen (label) | ||
401 | }; | ||
402 | const int paramFormats[] = { 1, 1 }; | ||
403 | PGresult *res; | ||
404 | |||
405 | res = PQexecPrepared (plugin->dbh, | ||
406 | "lookup_label", 2, | ||
407 | paramValues, paramLengths, paramFormats, | ||
408 | 1); | ||
409 | return get_record_and_call_iterator (plugin, | ||
410 | res, | ||
411 | zone, | ||
412 | iter, iter_cls); | ||
413 | } | ||
414 | |||
415 | |||
416 | /** | ||
373 | * Iterate over the results for a particular key and zone in the | 417 | * Iterate over the results for a particular key and zone in the |
374 | * datastore. Will return at most one result to the iterator. | 418 | * datastore. Will return at most one result to the iterator. |
375 | * | 419 | * |
@@ -515,6 +559,7 @@ libgnunet_plugin_namestore_postgres_init (void *cls) | |||
515 | api->store_records = &namestore_postgres_store_records; | 559 | api->store_records = &namestore_postgres_store_records; |
516 | api->iterate_records = &namestore_postgres_iterate_records; | 560 | api->iterate_records = &namestore_postgres_iterate_records; |
517 | api->zone_to_name = &namestore_postgres_zone_to_name; | 561 | api->zone_to_name = &namestore_postgres_zone_to_name; |
562 | api->lookup_records = &namestore_postgres_lookup_records; | ||
518 | LOG (GNUNET_ERROR_TYPE_INFO, | 563 | LOG (GNUNET_ERROR_TYPE_INFO, |
519 | _("Postgres database running\n")); | 564 | _("Postgres database running\n")); |
520 | return api; | 565 | return api; |
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c index 2cf604dbd..ca965d610 100644 --- a/src/namestore/plugin_namestore_sqlite.c +++ b/src/namestore/plugin_namestore_sqlite.c | |||
@@ -97,6 +97,10 @@ struct Plugin | |||
97 | */ | 97 | */ |
98 | sqlite3_stmt *zone_to_name; | 98 | sqlite3_stmt *zone_to_name; |
99 | 99 | ||
100 | /** | ||
101 | * Precompiled SQL to lookup records based on label. | ||
102 | */ | ||
103 | sqlite3_stmt *lookup_label; | ||
100 | }; | 104 | }; |
101 | 105 | ||
102 | 106 | ||
@@ -278,7 +282,12 @@ database_setup (struct Plugin *plugin) | |||
278 | (plugin->dbh, | 282 | (plugin->dbh, |
279 | "SELECT record_count,record_data,label,zone_private_key" | 283 | "SELECT record_count,record_data,label,zone_private_key" |
280 | " FROM ns097records ORDER BY rvalue LIMIT 1 OFFSET ?", | 284 | " FROM ns097records ORDER BY rvalue LIMIT 1 OFFSET ?", |
281 | &plugin->iterate_all_zones) != SQLITE_OK) | 285 | &plugin->iterate_all_zones) != SQLITE_OK) || |
286 | (sq_prepare | ||
287 | (plugin->dbh, | ||
288 | "SELECT record_count,record_data,label,zone_private_key" | ||
289 | " FROM ns097records WHERE zone_private_key=? AND label=?", | ||
290 | &plugin->lookup_label) != SQLITE_OK) | ||
282 | ) | 291 | ) |
283 | { | 292 | { |
284 | LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling"); | 293 | LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling"); |
@@ -309,6 +318,8 @@ database_shutdown (struct Plugin *plugin) | |||
309 | sqlite3_finalize (plugin->iterate_all_zones); | 318 | sqlite3_finalize (plugin->iterate_all_zones); |
310 | if (NULL != plugin->zone_to_name) | 319 | if (NULL != plugin->zone_to_name) |
311 | sqlite3_finalize (plugin->zone_to_name); | 320 | sqlite3_finalize (plugin->zone_to_name); |
321 | if (NULL != plugin->zone_to_name) | ||
322 | sqlite3_finalize (plugin->lookup_label); | ||
312 | result = sqlite3_close (plugin->dbh); | 323 | result = sqlite3_close (plugin->dbh); |
313 | if (result == SQLITE_BUSY) | 324 | if (result == SQLITE_BUSY) |
314 | { | 325 | { |
@@ -536,6 +547,51 @@ get_record_and_call_iterator (struct Plugin *plugin, | |||
536 | return ret; | 547 | return ret; |
537 | } | 548 | } |
538 | 549 | ||
550 | /** | ||
551 | * Lookup records in the datastore for which we are the authority. | ||
552 | * | ||
553 | * @param cls closure (internal context for the plugin) | ||
554 | * @param zone private key of the zone | ||
555 | * @param label name of the record in the zone | ||
556 | * @param iter function to call with the result | ||
557 | * @param iter_cls closure for @a iter | ||
558 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR | ||
559 | */ | ||
560 | static int | ||
561 | namestore_sqlite_lookup_records (void *cls, | ||
562 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, | ||
563 | GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls) | ||
564 | { | ||
565 | struct Plugin *plugin = cls; | ||
566 | sqlite3_stmt *stmt; | ||
567 | int err; | ||
568 | |||
569 | if (NULL == zone) | ||
570 | { | ||
571 | return GNUNET_SYSERR; | ||
572 | } | ||
573 | else | ||
574 | { | ||
575 | stmt = plugin->lookup_label; | ||
576 | err = ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1, | ||
577 | zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), | ||
578 | SQLITE_STATIC)) || | ||
579 | (SQLITE_OK != sqlite3_bind_text (stmt, 2, | ||
580 | label, -1, SQLITE_STATIC)) ); | ||
581 | } | ||
582 | if (err) | ||
583 | { | ||
584 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
585 | "sqlite3_bind_XXXX"); | ||
586 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
587 | LOG_SQLITE (plugin, | ||
588 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
589 | "sqlite3_reset"); | ||
590 | return GNUNET_SYSERR; | ||
591 | } | ||
592 | return get_record_and_call_iterator (plugin, stmt, zone, iter, iter_cls); | ||
593 | } | ||
594 | |||
539 | 595 | ||
540 | /** | 596 | /** |
541 | * Iterate over the results for a particular key and zone in the | 597 | * Iterate over the results for a particular key and zone in the |
@@ -658,6 +714,7 @@ libgnunet_plugin_namestore_sqlite_init (void *cls) | |||
658 | api->store_records = &namestore_sqlite_store_records; | 714 | api->store_records = &namestore_sqlite_store_records; |
659 | api->iterate_records = &namestore_sqlite_iterate_records; | 715 | api->iterate_records = &namestore_sqlite_iterate_records; |
660 | api->zone_to_name = &namestore_sqlite_zone_to_name; | 716 | api->zone_to_name = &namestore_sqlite_zone_to_name; |
717 | api->lookup_records = &namestore_sqlite_lookup_records; | ||
661 | LOG (GNUNET_ERROR_TYPE_INFO, | 718 | LOG (GNUNET_ERROR_TYPE_INFO, |
662 | _("Sqlite database running\n")); | 719 | _("Sqlite database running\n")); |
663 | return api; | 720 | return api; |