2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file namestore/gnunet-service-namestore.c
23 * @brief namestore for the GNUnet naming system
24 * @author Matthias Wachs
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_namestore_plugin.h"
31 #include "gnunet_signatures.h"
32 #include "namestore.h"
37 * A namestore operation.
39 struct GNUNET_NAMESTORE_ZoneIteration
41 struct GNUNET_NAMESTORE_ZoneIteration *next;
42 struct GNUNET_NAMESTORE_ZoneIteration *prev;
44 struct GNUNET_NAMESTORE_Client * client;
57 struct GNUNET_NAMESTORE_Client
59 struct GNUNET_NAMESTORE_Client *next;
60 struct GNUNET_NAMESTORE_Client *prev;
62 struct GNUNET_SERVER_Client * client;
64 struct GNUNET_NAMESTORE_ZoneIteration *op_head;
65 struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
71 * Configuration handle.
73 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
75 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
78 * Our notification context.
80 static struct GNUNET_SERVER_NotificationContext *snc;
82 static char *db_lib_name;
84 static struct GNUNET_NAMESTORE_Client *client_head;
85 static struct GNUNET_NAMESTORE_Client *client_tail;
89 * Task run during shutdown.
95 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
99 struct GNUNET_NAMESTORE_ZoneIteration * no;
100 struct GNUNET_NAMESTORE_ZoneIteration * tmp;
101 struct GNUNET_NAMESTORE_Client * nc;
102 struct GNUNET_NAMESTORE_Client * next;
104 GNUNET_SERVER_notification_context_destroy (snc);
107 for (nc = client_head; nc != NULL; nc = next)
110 for (no = nc->op_head; no != NULL; no = tmp)
113 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
117 GNUNET_SERVER_client_drop(nc->client);
118 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
122 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
123 GNUNET_free (db_lib_name);
126 static struct GNUNET_NAMESTORE_Client *
127 client_lookup (struct GNUNET_SERVER_Client *client)
129 struct GNUNET_NAMESTORE_Client * nc;
131 GNUNET_assert (NULL != client);
133 for (nc = client_head; nc != NULL; nc = nc->next)
135 if (client == nc->client)
142 * Called whenever a client is disconnected. Frees our
143 * resources associated with that client.
146 * @param client identification of the client
149 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
151 struct GNUNET_NAMESTORE_ZoneIteration * no;
152 struct GNUNET_NAMESTORE_Client * nc;
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
158 nc = client_lookup (client);
160 if ((NULL == client) || (NULL == nc))
163 for (no = nc->op_head; no != NULL; no = no->next)
165 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
169 GNUNET_SERVER_client_drop(nc->client);
170 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
176 static void handle_start (void *cls,
177 struct GNUNET_SERVER_Client * client,
178 const struct GNUNET_MessageHeader * message)
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
182 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
184 GNUNET_SERVER_notification_context_add (snc, client);
185 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
186 GNUNET_SERVER_client_keep (client);
187 GNUNET_SERVER_receive_done (client, GNUNET_OK);
190 struct LookupNameContext
192 struct GNUNET_NAMESTORE_Client *nc;
194 uint32_t record_type;
195 GNUNET_HashCode *zone;
199 void drop_iterator (void *cls,
200 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
201 struct GNUNET_TIME_Absolute expire,
204 const struct GNUNET_NAMESTORE_RecordData *rd,
205 const struct GNUNET_CRYPTO_RsaSignature *signature)
207 GNUNET_HashCode zone_hash;
209 if (NULL != zone_key)
211 GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_hash);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting zone `%s'\n", GNUNET_h2s (&zone_hash));
213 GSN_database->delete_zone (GSN_database->cls, &zone_hash);
217 (*stop) = GNUNET_YES;
223 handle_lookup_name_it (void *cls,
224 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
225 struct GNUNET_TIME_Absolute expire,
227 unsigned int rd_count,
228 const struct GNUNET_NAMESTORE_RecordData *rd,
229 const struct GNUNET_CRYPTO_RsaSignature *signature)
232 struct LookupNameContext *lnc = cls;
233 struct LookupNameResponseMessage *lnr_msg;
235 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
236 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
243 int copied_elements = 0;
244 int contains_signature = 0;
248 name_len = strlen(name) + 1;
250 /* count records to copy */
253 if (lnc->record_type != 0)
255 /* special record type needed */
256 for (c = 0; c < rd_count; c ++)
257 if (rd[c].record_type == lnc->record_type)
258 copied_elements++; /* found matching record */
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records with type %u for name `%s' in zone `%s'\n",
260 copied_elements, lnc->record_type, lnc->name, GNUNET_h2s(lnc->zone));
261 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
263 for (c = 0; c < rd_count; c ++)
265 if (rd[c].record_type == lnc->record_type)
267 /* found matching record */
268 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
275 copied_elements = rd_count;
276 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
284 expire = GNUNET_TIME_UNIT_ZERO_ABS;
287 rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
288 char rd_ser[rd_ser_len];
289 GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
291 if (rd_selected != rd)
292 GNUNET_free (rd_selected);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records for name `%s' in zone `%s'\n",
295 copied_elements, lnc->name, GNUNET_h2s(lnc->zone));
297 if ((copied_elements == rd_count) && (signature != NULL))
298 contains_signature = GNUNET_YES;
300 contains_signature = GNUNET_NO;
302 r_size = sizeof (struct LookupNameResponseMessage) +
303 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
308 lnr_msg = GNUNET_malloc (r_size);
309 lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
310 lnr_msg->gns_header.header.size = ntohs (r_size);
311 lnr_msg->gns_header.r_id = htonl (lnc->request_id);
312 lnr_msg->rd_count = htons (copied_elements);
313 lnr_msg->rd_len = htons (rd_ser_len);
314 lnr_msg->name_len = htons (name_len);
315 lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
316 lnr_msg->contains_sig = htons (contains_signature);
317 if (GNUNET_YES == contains_signature)
318 lnr_msg->signature = *signature;
320 memset (&lnr_msg->signature, '\0', sizeof (lnr_msg->signature));
322 zone_key_tmp = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
323 name_tmp = (char *) &zone_key_tmp[1];
324 rd_tmp = &name_tmp[name_len];
326 memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
327 memcpy (name_tmp, name, name_len);
328 memcpy (rd_tmp, rd_ser, rd_ser_len);
330 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
332 GNUNET_free (lnr_msg);
335 static void handle_lookup_name (void *cls,
336 struct GNUNET_SERVER_Client * client,
337 const struct GNUNET_MessageHeader * message)
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
340 struct LookupNameContext lnc;
341 struct GNUNET_NAMESTORE_Client *nc;
348 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
355 nc = client_lookup(client);
359 GNUNET_SERVER_receive_done (client, GNUNET_OK);
363 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
364 rid = ntohl (ln_msg->gns_header.r_id);
365 name_len = ntohl (ln_msg->name_len);
366 type = ntohl (ln_msg->record_type);
368 if ((name_len == 0) || (name_len > 256))
371 GNUNET_SERVER_receive_done (client, GNUNET_OK);
375 name = (char *) &ln_msg[1];
376 if (name[name_len -1] != '\0')
379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up all records for name `%s' in zone `%s'\n", name, GNUNET_h2s(&ln_msg->zone));
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up records with type %u for name `%s' in zone `%s'\n", type, name, GNUNET_h2s(&ln_msg->zone));
388 /* do the actual lookup */
389 lnc.request_id = rid;
391 lnc.record_type = type;
393 lnc.zone = &ln_msg->zone;
394 res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
396 GNUNET_SERVER_receive_done (client, GNUNET_OK);
399 static void handle_record_put (void *cls,
400 struct GNUNET_SERVER_Client * client,
401 const struct GNUNET_MessageHeader * message)
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
404 struct GNUNET_NAMESTORE_Client *nc;
405 struct GNUNET_TIME_Absolute expire;
406 struct GNUNET_CRYPTO_RsaSignature *signature;
407 struct RecordPutResponseMessage rpr_msg;
416 int res = GNUNET_SYSERR;
418 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
425 nc = client_lookup (client);
429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
433 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
435 rid = ntohl (rp_msg->gns_header.r_id);
436 msg_size = ntohs (rp_msg->gns_header.header.size);
437 name_len = ntohs (rp_msg->name_len);
438 rd_count = ntohs (rp_msg->rd_count);
439 rd_ser_len = ntohs(rp_msg->rd_len);
441 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
444 GNUNET_SERVER_receive_done (client, GNUNET_OK);
448 if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
451 GNUNET_SERVER_receive_done (client, GNUNET_OK);
455 msg_size_exp = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
456 if (msg_size != msg_size_exp)
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
463 if ((name_len == 0) || (name_len > 256))
466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
470 name = (char *) &rp_msg[1];
472 if (name[name_len -1] != '\0')
475 GNUNET_SERVER_receive_done (client, GNUNET_OK);
479 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
480 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
482 rd_ser = &name[name_len];
483 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
484 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
485 if (res != GNUNET_OK)
492 GNUNET_HashCode zone_hash;
493 GNUNET_CRYPTO_hash (&rp_msg->public_key, sizeof (rp_msg->public_key), &zone_hash);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting %u record for name `%s' in zone `%s'\n", rd_count, name, GNUNET_h2s(&zone_hash));
497 /* Database operation */
498 res = GSN_database->put_records(GSN_database->cls,
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
506 name, (res == GNUNET_OK) ? "OK" : "FAIL");
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
511 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
512 rpr_msg.gns_header.header.size = htons (sizeof (struct RecordPutResponseMessage));
513 rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
515 if (GNUNET_OK == res)
516 rpr_msg.op_result = htons (GNUNET_OK);
518 rpr_msg.op_result = htons (GNUNET_NO);
519 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
521 GNUNET_SERVER_receive_done (client, GNUNET_OK);
524 struct CreateRecordContext
526 struct GNUNET_NAMESTORE_RecordData *rd;
527 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
528 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
529 struct GNUNET_TIME_Absolute expire;
536 handle_create_record_it (void *cls,
537 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey,
538 struct GNUNET_TIME_Absolute expire,
540 unsigned int rd_count,
541 const struct GNUNET_NAMESTORE_RecordData *rd,
542 const struct GNUNET_CRYPTO_RsaSignature *signature)
544 struct CreateRecordContext * crc = cls;
545 struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
546 struct GNUNET_NAMESTORE_RecordData *rd_new = NULL;
548 int exist = GNUNET_SYSERR;
549 int update = GNUNET_NO;
551 int rd_count_new = 0;
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, crc->name);
555 for (c = 0; c < rd_count; c++)
558 if ((crc->rd->record_type == rd[c].record_type) &&
559 (crc->rd->data_size == rd[c].data_size) &&
560 (0 == memcmp (crc->rd->data, rd[c].data, rd[c].data_size)))
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing records for `%s' to update expiration date!\n", crc->name);
564 if (crc->rd->expiration.abs_value != rd[c].expiration.abs_value)
570 if (exist == GNUNET_SYSERR)
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "NO existing records for `%s' to update!\n", crc->name);
573 if (exist == GNUNET_SYSERR)
575 rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
576 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
577 rd_count_new = rd_count + 1;
578 rd_new[rd_count] = *(crc->rd);
579 signature_new = GNUNET_NAMESTORE_create_signature (crc->pkey, crc->name, rd_new, rd_count+1);
581 if (NULL == signature_new)
588 else if (update == GNUNET_NO)
590 /* Exact same record already exists */
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No update for %s' record required!\n", crc->name);
595 else if (update == GNUNET_YES)
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing records for `%s'!\n", crc->name);
599 rd_new = GNUNET_malloc ((rd_count) * sizeof (struct GNUNET_NAMESTORE_RecordData));
600 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
601 rd_count_new = rd_count;
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration from %llu to %llu!\n", rd_new[exist].expiration.abs_value, crc->rd->expiration.abs_value);
603 rd_new[exist].expiration = crc->rd->expiration;
604 signature_new = GNUNET_NAMESTORE_create_signature (crc->pkey, crc->name, rd_new, rd_count_new);
605 if (NULL == signature_new)
613 /* Database operation */
614 GNUNET_assert ((rd_new != NULL) && (rd_count_new > 0));
615 res = GSN_database->put_records(GSN_database->cls,
616 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
619 rd_count_new, rd_new,
621 GNUNET_break (GNUNET_OK == res);
625 GNUNET_free_non_null (rd_new);
626 GNUNET_free_non_null (signature_new);
630 /* failed to create the record */
631 crc->res = GNUNET_SYSERR;
634 /* database operations OK */
635 if (GNUNET_YES == update)
636 /* we updated an existing record */
637 crc->res = GNUNET_NO;
639 /* we created a new record */
640 crc->res = GNUNET_YES;
643 /* identical entry existed, so we did nothing */
644 crc->res = GNUNET_NO;
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name `%s' %u\n", crc->name, res);
654 static void handle_record_create (void *cls,
655 struct GNUNET_SERVER_Client * client,
656 const struct GNUNET_MessageHeader * message)
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
659 struct GNUNET_NAMESTORE_Client *nc;
660 struct CreateRecordContext crc;
661 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
662 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
663 struct RecordCreateResponseMessage rcr_msg;
664 GNUNET_HashCode pubkey_hash;
676 int res = GNUNET_SYSERR;
678 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
681 GNUNET_SERVER_receive_done (client, GNUNET_OK);
685 nc = client_lookup(client);
689 GNUNET_SERVER_receive_done (client, GNUNET_OK);
693 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
694 rid = ntohl (rp_msg->gns_header.r_id);
695 name_len = ntohs (rp_msg->name_len);
696 msg_size = ntohs (message->size);
697 rd_count = ntohs (rp_msg->rd_count);
698 rd_ser_len = ntohs (rp_msg->rd_len);
699 key_len = ntohs (rp_msg->pkey_len);
700 msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
702 if (msg_size != msg_size_exp)
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
710 if ((name_len == 0) || (name_len > 256))
713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
717 pkey_tmp = (char *) &rp_msg[1];
718 name_tmp = &pkey_tmp[key_len];
719 rd_ser = &name_tmp[name_len];
721 if (name_tmp[name_len -1] != '\0')
724 GNUNET_SERVER_receive_done (client, GNUNET_OK);
728 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
729 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
730 if ((res != GNUNET_OK) || (rd_count != 1))
736 /* Extracting and converting private key */
737 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
738 GNUNET_assert (pkey != NULL);
739 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
740 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
749 /* Get existing records for name */
750 res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
751 if (res != GNUNET_SYSERR)
753 GNUNET_CRYPTO_rsa_key_free(pkey);
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
758 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
759 rcr_msg.gns_header.r_id = htonl (rid);
760 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
761 if ((GNUNET_OK == res) && (crc.res == GNUNET_YES))
762 rcr_msg.op_result = htons (GNUNET_YES);
763 else if ((GNUNET_OK == res) && (crc.res == GNUNET_NO))
764 rcr_msg.op_result = htons (GNUNET_NO);
766 rcr_msg.op_result = htons (GNUNET_SYSERR);
767 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
769 GNUNET_SERVER_receive_done (client, GNUNET_OK);
773 struct RemoveRecordContext
775 struct GNUNET_NAMESTORE_RecordData *rd;
776 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
781 handle_record_remove_it (void *cls,
782 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
783 struct GNUNET_TIME_Absolute expire,
785 unsigned int rd_count,
786 const struct GNUNET_NAMESTORE_RecordData *rd,
787 const struct GNUNET_CRYPTO_RsaSignature *signature)
789 struct RemoveRecordContext *rrc = cls;
790 unsigned int rd_count_new = rd_count -1;
791 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
794 int found = GNUNET_NO;
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
800 /* Could not find record to remove */
805 /* Find record to remove */
807 for (c = 0; c < rd_count; c++)
809 if ((rd[c].expiration.abs_value == rrc->rd->expiration.abs_value) &&
810 (rd[c].flags == rrc->rd->flags) &&
811 (rd[c].record_type == rrc->rd->record_type) &&
812 (rd[c].data_size == rrc->rd->data_size) &&
813 (0 == memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size)))
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
825 if ((c2 != rd_count_new) || (found == GNUNET_NO))
827 /* Could not find record to remove */
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
833 /* Create new signature */
834 struct GNUNET_CRYPTO_RsaSignature * new_signature;
835 new_signature = GNUNET_NAMESTORE_create_signature (rrc->pkey, name, rd_new, rd_count_new);
837 if (new_signature == NULL)
839 /* Signature failed */
845 res = GSN_database->put_records(GSN_database->cls,
849 rd_count_new, rd_new,
851 GNUNET_free (new_signature);
853 if (GNUNET_OK != res)
855 /* Could put records into database */
863 static void handle_record_remove (void *cls,
864 struct GNUNET_SERVER_Client * client,
865 const struct GNUNET_MessageHeader * message)
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
868 struct GNUNET_NAMESTORE_Client *nc;
869 struct RecordRemoveResponseMessage rrr_msg;
870 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
871 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
872 GNUNET_HashCode pubkey_hash;
873 char * pkey_tmp = NULL;
874 char * name_tmp = NULL;
875 char * rd_ser = NULL;
878 size_t rd_ser_len = 0;
880 size_t msg_size_exp = 0;
884 int res = GNUNET_SYSERR;
886 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
893 nc = client_lookup(client);
897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
901 struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
902 rid = ntohl (rr_msg->gns_header.r_id);
903 name_len = ntohs (rr_msg->name_len);
904 rd_ser_len = ntohs (rr_msg->rd_len);
905 rd_count = ntohs (rr_msg->rd_count);
906 key_len = ntohs (rr_msg->pkey_len);
907 msg_size = ntohs (message->size);
909 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
912 GNUNET_SERVER_receive_done (client, GNUNET_OK);
916 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
919 GNUNET_SERVER_receive_done (client, GNUNET_OK);
923 msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
924 if (msg_size != msg_size_exp)
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
928 GNUNET_SERVER_receive_done (client, GNUNET_OK);
932 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
935 GNUNET_SERVER_receive_done (client, GNUNET_OK);
939 pkey_tmp = (char *) &rr_msg[1];
940 name_tmp = &pkey_tmp[key_len];
941 rd_ser = &name_tmp[name_len];
944 if ((name_len == 0) || (name_len > 256))
947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
951 if (name_tmp[name_len -1] != '\0')
954 GNUNET_SERVER_receive_done (client, GNUNET_OK);
958 /* Extracting and converting private key */
959 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
960 GNUNET_assert (pkey != NULL);
961 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
962 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
964 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
965 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
966 if ((res != GNUNET_OK) || (rd_count != 1))
972 struct RemoveRecordContext rrc;
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
978 /* Database operation */
979 res = GSN_database->iterate_records (GSN_database->cls,
983 handle_record_remove_it, &rrc);
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
986 name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
992 rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
993 rrr_msg.gns_header.r_id = rr_msg->gns_header.r_id;
994 rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
995 rrr_msg.op_result = htons (res);
996 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
998 GNUNET_CRYPTO_rsa_key_free (pkey);
1000 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1004 struct ZoneToNameCtx
1006 struct GNUNET_NAMESTORE_Client *nc;
1011 handle_zone_to_name_it (void *cls,
1012 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1013 struct GNUNET_TIME_Absolute expire,
1015 unsigned int rd_count,
1016 const struct GNUNET_NAMESTORE_RecordData *rd,
1017 const struct GNUNET_CRYPTO_RsaSignature *signature)
1019 struct ZoneToNameCtx * ztn_ctx = cls;
1020 struct ZoneToNameResponseMessage *ztnr_msg;
1021 int16_t res = GNUNET_SYSERR;
1022 uint16_t name_len = 0;
1023 uint16_t rd_ser_len = 0 ;
1024 int32_t contains_sig = 0;
1025 size_t msg_size = 0;
1027 char *rd_ser = NULL;
1032 if ((zone_key != NULL) && (name != NULL))
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found results: name is `%s', has %u records\n", name, rd_count);
1037 name_len = strlen (name);
1041 /* no result found */
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found no results\n");
1049 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1050 rd_ser = GNUNET_malloc (rd_ser_len);
1051 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1056 if (signature != NULL)
1057 contains_sig = GNUNET_YES;
1059 contains_sig = GNUNET_NO;
1063 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len + contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
1064 ztnr_msg = GNUNET_malloc (msg_size);
1066 name_tmp = (char *) &ztnr_msg[1];
1067 rd_tmp = &name_tmp[name_len];
1068 sig_tmp = &rd_tmp[rd_ser_len];
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_TO_NAME_RESPONSE");
1071 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1072 ztnr_msg->gns_header.header.size = htons (msg_size);
1073 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1074 ztnr_msg->res = htons (res);
1075 ztnr_msg->rd_len = htons (rd_ser_len);
1076 ztnr_msg->rd_count = htons (rd_count);
1077 ztnr_msg->name_len = htons (name_len);
1078 ztnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
1079 if (zone_key != NULL)
1080 ztnr_msg->zone_key = *zone_key;
1082 memset (&ztnr_msg->zone_key, '\0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1084 memcpy (name_tmp, name, name_len);
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name is `%s', has %u records, rd ser len %u msg_size %u\n", name, rd_count, rd_ser_len, msg_size);
1087 memcpy (rd_tmp, rd_ser, rd_ser_len);
1088 memcpy (sig_tmp, signature, contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature));
1090 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client, (const struct GNUNET_MessageHeader *) ztnr_msg, GNUNET_NO);
1091 GNUNET_free (ztnr_msg);
1092 GNUNET_free_non_null (rd_ser);
1096 static void handle_zone_to_name (void *cls,
1097 struct GNUNET_SERVER_Client * client,
1098 const struct GNUNET_MessageHeader * message)
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_TO_NAME");
1101 struct GNUNET_NAMESTORE_Client *nc;
1102 struct ZoneToNameCtx ztn_ctx;
1103 size_t msg_size = 0;
1108 if (ntohs (message->size) != sizeof (struct ZoneToNameMessage))
1110 GNUNET_break_op (0);
1111 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1115 nc = client_lookup(client);
1118 GNUNET_break_op (0);
1119 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1123 struct ZoneToNameMessage *ztn_msg = (struct ZoneToNameMessage *) message;
1125 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1127 GNUNET_break_op (0);
1128 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1132 rid = ntohl (ztn_msg->gns_header.r_id);
1137 char * z_tmp = strdup (GNUNET_h2s (&ztn_msg->zone));
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up name for zone `%s' in zone `%s'\n",
1140 GNUNET_h2s (&ztn_msg->value_zone));
1141 GNUNET_free (z_tmp);
1143 res = GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
1145 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1148 struct ZoneIterationProcResult
1151 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
1154 struct GNUNET_CRYPTO_RsaSignature signature;
1155 struct GNUNET_TIME_Absolute expire;
1165 void zone_iteration_proc (void *cls,
1166 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1167 struct GNUNET_TIME_Absolute expire,
1169 unsigned int rd_count,
1170 const struct GNUNET_NAMESTORE_RecordData *rd,
1171 const struct GNUNET_CRYPTO_RsaSignature *signature)
1173 struct ZoneIterationProcResult *zipr = cls;
1175 if (zone_key != NULL)
1177 zipr->zone_key = *zone_key;
1178 zipr->have_zone_key = GNUNET_YES;
1181 zipr->have_zone_key = GNUNET_NO;
1183 zipr->expire = expire;
1187 memcpy (zipr->name, name, strlen(name) + 1);
1188 zipr->have_name = GNUNET_YES;
1191 zipr->have_name = GNUNET_NO;
1193 if (signature != NULL)
1195 zipr->signature = *signature;
1196 zipr->have_signature = GNUNET_YES;
1199 zipr->have_signature = GNUNET_NO;
1201 if ((rd_count > 0) && (rd != NULL))
1203 len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1204 zipr->rd_ser = GNUNET_malloc (len);
1205 GNUNET_NAMESTORE_records_serialize(rd_count, rd, len, zipr->rd_ser);
1206 zipr->rd_ser_len = len;
1210 static void handle_iteration_start (void *cls,
1211 struct GNUNET_SERVER_Client * client,
1212 const struct GNUNET_MessageHeader * message)
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1216 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
1217 struct GNUNET_NAMESTORE_Client *nc;
1218 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1219 struct ZoneIterationResponseMessage zir_msg;
1220 struct ZoneIterationProcResult zipr;
1223 nc = client_lookup(client);
1226 GNUNET_break_op (0);
1227 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1231 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
1232 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1235 zi->zone = zis_msg->zone;
1237 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1239 res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
1242 /* GNUNET_OK on success */
1246 /* GNUNET_SYSERR on error */
1249 /* GNUNET_NO if there were no results, */
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
1258 zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1259 zir_msg.gns_header.r_id = htonl(zi->request_id);
1260 zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
1263 GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1266 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1269 static void handle_iteration_stop (void *cls,
1270 struct GNUNET_SERVER_Client * client,
1271 const struct GNUNET_MessageHeader * message)
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1275 struct GNUNET_NAMESTORE_Client *nc;
1276 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1277 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1280 nc = client_lookup(client);
1283 GNUNET_break_op (0);
1284 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1288 rid = ntohl (zis_msg->gns_header.r_id);
1289 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1291 if (zi->request_id == rid)
1296 GNUNET_break_op (0);
1297 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1301 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
1305 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1308 static void handle_iteration_next (void *cls,
1309 struct GNUNET_SERVER_Client * client,
1310 const struct GNUNET_MessageHeader * message)
1312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1314 struct GNUNET_NAMESTORE_Client *nc;
1315 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1316 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1320 nc = client_lookup(client);
1323 GNUNET_break_op (0);
1324 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1328 rid = ntohl (zis_msg->gns_header.r_id);
1329 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1331 if (zi->request_id == rid)
1336 GNUNET_break_op (0);
1337 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1342 res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
1348 * Process template requests.
1350 * @param cls closure
1351 * @param server the initialized server
1352 * @param cfg configuration to use
1355 run (void *cls, struct GNUNET_SERVER_Handle *server,
1356 const struct GNUNET_CONFIGURATION_Handle *cfg)
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1362 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1363 {&handle_start, NULL,
1364 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1365 {&handle_lookup_name, NULL,
1366 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1367 {&handle_record_put, NULL,
1368 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1369 {&handle_record_create, NULL,
1370 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1371 {&handle_record_remove, NULL,
1372 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1373 {&handle_zone_to_name, NULL,
1374 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, 0},
1375 {&handle_iteration_start, NULL,
1376 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1377 {&handle_iteration_next, NULL,
1378 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1379 {&handle_iteration_stop, NULL,
1380 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, 0},
1386 /* Loading database plugin */
1388 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1392 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1393 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1394 if (GSN_database == NULL)
1395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1397 GNUNET_free (database);
1399 /* Configuring server handles */
1400 GNUNET_SERVER_add_handlers (server, handlers);
1401 snc = GNUNET_SERVER_notification_context_create (server, 16);
1402 GNUNET_SERVER_disconnect_notify (server,
1403 &client_disconnect_notification,
1406 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1413 * The main function for the template service.
1415 * @param argc number of arguments from the command line
1416 * @param argv command line arguments
1417 * @return 0 ok, 1 on error
1420 main (int argc, char *const *argv)
1422 return (GNUNET_OK ==
1423 GNUNET_SERVICE_run (argc, argv, "namestore",
1424 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1427 /* end of gnunet-service-namestore.c */