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;
234 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
241 int copied_elements = 0;
242 int contains_signature = 0;
246 name_len = strlen(name) + 1;
248 /* count records to copy */
251 if (lnc->record_type != 0)
253 /* special record type needed */
254 for (c = 0; c < rd_count; c ++)
255 if (rd[c].record_type == lnc->record_type)
256 copied_elements++; /* found matching record */
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records with type %u for name `%s' in zone `%s'\n",
258 copied_elements, lnc->record_type, lnc->name, GNUNET_h2s(lnc->zone));
259 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
261 for (c = 0; c < rd_count; c ++)
263 if (rd[c].record_type == lnc->record_type)
265 /* found matching record */
266 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
273 copied_elements = rd_count;
274 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
282 expire = GNUNET_TIME_UNIT_ZERO_ABS;
285 rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
286 char rd_ser[rd_ser_len];
287 GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
289 if (rd_selected != rd)
290 GNUNET_free (rd_selected);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records for name `%s' in zone `%s'\n",
293 copied_elements, lnc->name, GNUNET_h2s(lnc->zone));
295 if ((copied_elements == rd_count) && (NULL != signature))
296 contains_signature = GNUNET_YES;
298 contains_signature = GNUNET_NO;
300 r_size = sizeof (struct LookupNameResponseMessage) +
301 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
306 lnr_msg = GNUNET_malloc (r_size);
307 lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
308 lnr_msg->gns_header.header.size = ntohs (r_size);
309 lnr_msg->gns_header.r_id = htonl (lnc->request_id);
310 lnr_msg->rd_count = htons (copied_elements);
311 lnr_msg->rd_len = htons (rd_ser_len);
312 lnr_msg->name_len = htons (name_len);
313 lnr_msg->contains_sig = htons (contains_signature);
314 lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
316 if (zone_key != NULL)
317 lnr_msg->public_key = (*zone_key);
319 memset(&lnr_msg->public_key, '\0', sizeof (lnr_msg->public_key));
320 if (GNUNET_YES == contains_signature)
321 lnr_msg->signature = *signature;
323 memset (&lnr_msg->signature, '\0', sizeof (lnr_msg->signature));
325 name_tmp = (char *) &lnr_msg[1];
326 rd_tmp = &name_tmp[name_len];
328 memcpy (name_tmp, name, name_len);
329 memcpy (rd_tmp, rd_ser, rd_ser_len);
331 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
333 GNUNET_free (lnr_msg);
336 static void handle_lookup_name (void *cls,
337 struct GNUNET_SERVER_Client * client,
338 const struct GNUNET_MessageHeader * message)
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
341 struct LookupNameContext lnc;
342 struct GNUNET_NAMESTORE_Client *nc;
349 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
352 GNUNET_SERVER_receive_done (client, GNUNET_OK);
356 nc = client_lookup(client);
360 GNUNET_SERVER_receive_done (client, GNUNET_OK);
364 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
365 rid = ntohl (ln_msg->gns_header.r_id);
366 name_len = ntohl (ln_msg->name_len);
367 type = ntohl (ln_msg->record_type);
369 if ((name_len == 0) || (name_len > 256))
372 GNUNET_SERVER_receive_done (client, GNUNET_OK);
376 name = (char *) &ln_msg[1];
377 if (name[name_len -1] != '\0')
380 GNUNET_SERVER_receive_done (client, GNUNET_OK);
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up all records for name `%s' in zone `%s'\n", name, GNUNET_h2s(&ln_msg->zone));
387 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));
389 /* do the actual lookup */
390 lnc.request_id = rid;
392 lnc.record_type = type;
394 lnc.zone = &ln_msg->zone;
395 res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
397 GNUNET_SERVER_receive_done (client, GNUNET_OK);
400 static void handle_record_put (void *cls,
401 struct GNUNET_SERVER_Client * client,
402 const struct GNUNET_MessageHeader * message)
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
405 struct GNUNET_NAMESTORE_Client *nc;
406 struct GNUNET_TIME_Absolute expire;
407 struct GNUNET_CRYPTO_RsaSignature *signature;
408 struct RecordPutResponseMessage rpr_msg;
417 int res = GNUNET_SYSERR;
419 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
422 GNUNET_SERVER_receive_done (client, GNUNET_OK);
426 nc = client_lookup (client);
430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
434 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
436 rid = ntohl (rp_msg->gns_header.r_id);
437 msg_size = ntohs (rp_msg->gns_header.header.size);
438 name_len = ntohs (rp_msg->name_len);
439 rd_count = ntohs (rp_msg->rd_count);
440 rd_ser_len = ntohs(rp_msg->rd_len);
442 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
449 if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
452 GNUNET_SERVER_receive_done (client, GNUNET_OK);
456 msg_size_exp = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
457 if (msg_size != msg_size_exp)
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
461 GNUNET_SERVER_receive_done (client, GNUNET_OK);
464 if ((name_len == 0) || (name_len > 256))
467 GNUNET_SERVER_receive_done (client, GNUNET_OK);
471 name = (char *) &rp_msg[1];
473 if (name[name_len -1] != '\0')
476 GNUNET_SERVER_receive_done (client, GNUNET_OK);
480 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
481 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
483 rd_ser = &name[name_len];
484 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
485 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
486 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;
514 rpr_msg.op_result = htonl (res);
515 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
517 GNUNET_SERVER_receive_done (client, GNUNET_OK);
520 struct CreateRecordContext
522 struct GNUNET_NAMESTORE_RecordData *rd;
523 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
524 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
525 struct GNUNET_TIME_Absolute expire;
532 handle_create_record_it (void *cls,
533 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey,
534 struct GNUNET_TIME_Absolute expire,
536 unsigned int rd_count,
537 const struct GNUNET_NAMESTORE_RecordData *rd,
538 const struct GNUNET_CRYPTO_RsaSignature *signature)
540 struct CreateRecordContext * crc = cls;
541 struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
542 struct GNUNET_NAMESTORE_RecordData *rd_new = NULL;
544 int exist = GNUNET_SYSERR;
545 int update = GNUNET_NO;
547 int rd_count_new = 0;
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, crc->name);
551 for (c = 0; c < rd_count; c++)
554 if ((crc->rd->record_type == rd[c].record_type) &&
555 (crc->rd->data_size == rd[c].data_size) &&
556 (0 == memcmp (crc->rd->data, rd[c].data, rd[c].data_size)))
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing records for `%s' to update expiration date!\n", crc->name);
560 if (crc->rd->expiration.abs_value != rd[c].expiration.abs_value)
566 if (exist == GNUNET_SYSERR)
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "NO existing records for `%s' to update!\n", crc->name);
569 if (exist == GNUNET_SYSERR)
571 rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
572 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
573 rd_count_new = rd_count + 1;
574 rd_new[rd_count] = *(crc->rd);
575 signature_new = GNUNET_NAMESTORE_create_signature (crc->pkey, crc->name, rd_new, rd_count+1);
577 if (NULL == signature_new)
584 else if (update == GNUNET_NO)
586 /* Exact same record already exists */
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No update for %s' record required!\n", crc->name);
591 else if (update == GNUNET_YES)
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing records for `%s'!\n", crc->name);
595 rd_new = GNUNET_malloc ((rd_count) * sizeof (struct GNUNET_NAMESTORE_RecordData));
596 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
597 rd_count_new = rd_count;
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration from %llu to %llu!\n", rd_new[exist].expiration.abs_value, crc->rd->expiration.abs_value);
599 rd_new[exist].expiration = crc->rd->expiration;
600 signature_new = GNUNET_NAMESTORE_create_signature (crc->pkey, crc->name, rd_new, rd_count_new);
601 if (NULL == signature_new)
609 /* Database operation */
610 GNUNET_assert ((rd_new != NULL) && (rd_count_new > 0));
611 res = GSN_database->put_records(GSN_database->cls,
612 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
615 rd_count_new, rd_new,
617 GNUNET_break (GNUNET_OK == res);
621 GNUNET_free_non_null (rd_new);
622 GNUNET_free_non_null (signature_new);
626 /* failed to create the record */
627 crc->res = GNUNET_SYSERR;
630 /* database operations OK */
631 if (GNUNET_YES == update)
632 /* we updated an existing record */
633 crc->res = GNUNET_NO;
635 /* we created a new record */
636 crc->res = GNUNET_YES;
639 /* identical entry existed, so we did nothing */
640 crc->res = GNUNET_NO;
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name `%s' %u\n", crc->name, res);
650 static void handle_record_create (void *cls,
651 struct GNUNET_SERVER_Client * client,
652 const struct GNUNET_MessageHeader * message)
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
655 struct GNUNET_NAMESTORE_Client *nc;
656 struct CreateRecordContext crc;
657 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
658 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
659 struct RecordCreateResponseMessage rcr_msg;
660 GNUNET_HashCode pubkey_hash;
672 int res = GNUNET_SYSERR;
674 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
677 GNUNET_SERVER_receive_done (client, GNUNET_OK);
681 nc = client_lookup(client);
685 GNUNET_SERVER_receive_done (client, GNUNET_OK);
689 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
690 rid = ntohl (rp_msg->gns_header.r_id);
691 name_len = ntohs (rp_msg->name_len);
692 msg_size = ntohs (message->size);
693 rd_count = ntohs (rp_msg->rd_count);
694 rd_ser_len = ntohs (rp_msg->rd_len);
695 key_len = ntohs (rp_msg->pkey_len);
696 msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
698 if (msg_size != msg_size_exp)
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
702 GNUNET_SERVER_receive_done (client, GNUNET_OK);
706 if ((name_len == 0) || (name_len > 256))
709 GNUNET_SERVER_receive_done (client, GNUNET_OK);
713 pkey_tmp = (char *) &rp_msg[1];
714 name_tmp = &pkey_tmp[key_len];
715 rd_ser = &name_tmp[name_len];
717 if (name_tmp[name_len -1] != '\0')
720 GNUNET_SERVER_receive_done (client, GNUNET_OK);
724 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
725 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
726 if ((res != GNUNET_OK) || (rd_count != 1))
732 /* Extracting and converting private key */
733 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
734 GNUNET_assert (pkey != NULL);
735 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
736 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
745 /* Get existing records for name */
746 res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
747 if (res != GNUNET_SYSERR)
749 GNUNET_CRYPTO_rsa_key_free(pkey);
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
754 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
755 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
756 rcr_msg.gns_header.r_id = htonl (rid);
757 if ((GNUNET_OK == res) && (crc.res == GNUNET_YES))
758 rcr_msg.op_result = htonl (GNUNET_YES);
759 else if ((GNUNET_OK == res) && (crc.res == GNUNET_NO))
760 rcr_msg.op_result = htonl (GNUNET_NO);
762 rcr_msg.op_result = htonl (GNUNET_SYSERR);
763 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
765 GNUNET_SERVER_receive_done (client, GNUNET_OK);
769 struct RemoveRecordContext
771 struct GNUNET_NAMESTORE_RecordData *rd;
772 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
777 handle_record_remove_it (void *cls,
778 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
779 struct GNUNET_TIME_Absolute expire,
781 unsigned int rd_count,
782 const struct GNUNET_NAMESTORE_RecordData *rd,
783 const struct GNUNET_CRYPTO_RsaSignature *signature)
785 struct RemoveRecordContext *rrc = cls;
786 unsigned int rd_count_new = rd_count -1;
787 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
790 int found = GNUNET_NO;
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
796 /* Could not find record to remove */
801 /* Find record to remove */
803 for (c = 0; c < rd_count; c++)
805 if ((rd[c].expiration.abs_value == rrc->rd->expiration.abs_value) &&
806 (rd[c].flags == rrc->rd->flags) &&
807 (rd[c].record_type == rrc->rd->record_type) &&
808 (rd[c].data_size == rrc->rd->data_size) &&
809 (0 == memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size)))
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
821 if ((c2 != rd_count_new) || (found == GNUNET_NO))
823 /* Could not find record to remove */
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
829 /* Create new signature */
830 struct GNUNET_CRYPTO_RsaSignature * new_signature;
831 new_signature = GNUNET_NAMESTORE_create_signature (rrc->pkey, name, rd_new, rd_count_new);
833 if (new_signature == NULL)
835 /* Signature failed */
841 res = GSN_database->put_records(GSN_database->cls,
845 rd_count_new, rd_new,
847 GNUNET_free (new_signature);
849 if (GNUNET_OK != res)
851 /* Could put records into database */
859 static void handle_record_remove (void *cls,
860 struct GNUNET_SERVER_Client * client,
861 const struct GNUNET_MessageHeader * message)
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
864 struct GNUNET_NAMESTORE_Client *nc;
865 struct RecordRemoveResponseMessage rrr_msg;
866 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
867 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
868 GNUNET_HashCode pubkey_hash;
869 char * pkey_tmp = NULL;
870 char * name_tmp = NULL;
871 char * rd_ser = NULL;
874 size_t rd_ser_len = 0;
876 size_t msg_size_exp = 0;
880 int res = GNUNET_SYSERR;
882 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
889 nc = client_lookup(client);
893 GNUNET_SERVER_receive_done (client, GNUNET_OK);
897 struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
898 rid = ntohl (rr_msg->gns_header.r_id);
899 name_len = ntohs (rr_msg->name_len);
900 rd_ser_len = ntohs (rr_msg->rd_len);
901 rd_count = ntohs (rr_msg->rd_count);
902 key_len = ntohs (rr_msg->pkey_len);
903 msg_size = ntohs (message->size);
905 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
908 GNUNET_SERVER_receive_done (client, GNUNET_OK);
912 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
915 GNUNET_SERVER_receive_done (client, GNUNET_OK);
919 msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
920 if (msg_size != msg_size_exp)
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
924 GNUNET_SERVER_receive_done (client, GNUNET_OK);
928 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
931 GNUNET_SERVER_receive_done (client, GNUNET_OK);
935 pkey_tmp = (char *) &rr_msg[1];
936 name_tmp = &pkey_tmp[key_len];
937 rd_ser = &name_tmp[name_len];
940 if ((name_len == 0) || (name_len > 256))
943 GNUNET_SERVER_receive_done (client, GNUNET_OK);
947 if (name_tmp[name_len -1] != '\0')
950 GNUNET_SERVER_receive_done (client, GNUNET_OK);
954 /* Extracting and converting private key */
955 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
956 GNUNET_assert (pkey != NULL);
957 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
958 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
960 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
961 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
962 if ((res != GNUNET_OK) || (rd_count != 1))
968 struct RemoveRecordContext rrc;
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
974 /* Database operation */
975 res = GSN_database->iterate_records (GSN_database->cls,
979 handle_record_remove_it, &rrc);
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
982 name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
988 rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
989 rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
990 rrr_msg.gns_header.r_id = rr_msg->gns_header.r_id;
991 rrr_msg.op_result = htonl (res);
992 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
994 GNUNET_CRYPTO_rsa_key_free (pkey);
996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1000 struct ZoneToNameCtx
1002 struct GNUNET_NAMESTORE_Client *nc;
1007 handle_zone_to_name_it (void *cls,
1008 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1009 struct GNUNET_TIME_Absolute expire,
1011 unsigned int rd_count,
1012 const struct GNUNET_NAMESTORE_RecordData *rd,
1013 const struct GNUNET_CRYPTO_RsaSignature *signature)
1015 struct ZoneToNameCtx * ztn_ctx = cls;
1016 struct ZoneToNameResponseMessage *ztnr_msg;
1017 int16_t res = GNUNET_SYSERR;
1018 uint16_t name_len = 0;
1019 uint16_t rd_ser_len = 0 ;
1020 int32_t contains_sig = 0;
1021 size_t msg_size = 0;
1023 char *rd_ser = NULL;
1028 if ((zone_key != NULL) && (name != NULL))
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found results: name is `%s', has %u records\n", name, rd_count);
1033 name_len = strlen (name);
1037 /* no result found */
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found no results\n");
1045 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1046 rd_ser = GNUNET_malloc (rd_ser_len);
1047 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1052 if (signature != NULL)
1053 contains_sig = GNUNET_YES;
1055 contains_sig = GNUNET_NO;
1059 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len + contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
1060 ztnr_msg = GNUNET_malloc (msg_size);
1062 name_tmp = (char *) &ztnr_msg[1];
1063 rd_tmp = &name_tmp[name_len];
1064 sig_tmp = &rd_tmp[rd_ser_len];
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_TO_NAME_RESPONSE");
1067 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1068 ztnr_msg->gns_header.header.size = htons (msg_size);
1069 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1070 ztnr_msg->res = htons (res);
1071 ztnr_msg->rd_len = htons (rd_ser_len);
1072 ztnr_msg->rd_count = htons (rd_count);
1073 ztnr_msg->name_len = htons (name_len);
1074 ztnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
1075 if (zone_key != NULL)
1076 ztnr_msg->zone_key = *zone_key;
1078 memset (&ztnr_msg->zone_key, '\0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1080 memcpy (name_tmp, name, name_len);
1082 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);
1083 memcpy (rd_tmp, rd_ser, rd_ser_len);
1084 memcpy (sig_tmp, signature, contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature));
1086 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client, (const struct GNUNET_MessageHeader *) ztnr_msg, GNUNET_NO);
1087 GNUNET_free (ztnr_msg);
1088 GNUNET_free_non_null (rd_ser);
1092 static void handle_zone_to_name (void *cls,
1093 struct GNUNET_SERVER_Client * client,
1094 const struct GNUNET_MessageHeader * message)
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_TO_NAME");
1097 struct GNUNET_NAMESTORE_Client *nc;
1098 struct ZoneToNameCtx ztn_ctx;
1099 size_t msg_size = 0;
1104 if (ntohs (message->size) != sizeof (struct ZoneToNameMessage))
1106 GNUNET_break_op (0);
1107 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1111 nc = client_lookup(client);
1114 GNUNET_break_op (0);
1115 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1119 struct ZoneToNameMessage *ztn_msg = (struct ZoneToNameMessage *) message;
1121 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1123 GNUNET_break_op (0);
1124 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1128 rid = ntohl (ztn_msg->gns_header.r_id);
1133 char * z_tmp = strdup (GNUNET_h2s (&ztn_msg->zone));
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up name for zone `%s' in zone `%s'\n",
1136 GNUNET_h2s (&ztn_msg->value_zone));
1137 GNUNET_free (z_tmp);
1139 res = GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
1141 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1144 struct ZoneIterationProcResult
1147 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
1150 struct GNUNET_CRYPTO_RsaSignature signature;
1151 struct GNUNET_TIME_Absolute expire;
1161 void zone_iteration_proc (void *cls,
1162 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1163 struct GNUNET_TIME_Absolute expire,
1165 unsigned int rd_count,
1166 const struct GNUNET_NAMESTORE_RecordData *rd,
1167 const struct GNUNET_CRYPTO_RsaSignature *signature)
1169 struct ZoneIterationProcResult *zipr = cls;
1171 if (zone_key != NULL)
1173 zipr->zone_key = *zone_key;
1174 zipr->have_zone_key = GNUNET_YES;
1177 zipr->have_zone_key = GNUNET_NO;
1179 zipr->expire = expire;
1183 memcpy (zipr->name, name, strlen(name) + 1);
1184 zipr->have_name = GNUNET_YES;
1187 zipr->have_name = GNUNET_NO;
1189 if (signature != NULL)
1191 zipr->signature = *signature;
1192 zipr->have_signature = GNUNET_YES;
1195 zipr->have_signature = GNUNET_NO;
1197 if ((rd_count > 0) && (rd != NULL))
1199 len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1200 zipr->rd_ser = GNUNET_malloc (len);
1201 GNUNET_NAMESTORE_records_serialize(rd_count, rd, len, zipr->rd_ser);
1202 zipr->rd_ser_len = len;
1206 static void handle_iteration_start (void *cls,
1207 struct GNUNET_SERVER_Client * client,
1208 const struct GNUNET_MessageHeader * message)
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1212 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
1213 struct GNUNET_NAMESTORE_Client *nc;
1214 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1215 struct ZoneIterationResponseMessage zir_msg;
1216 struct ZoneIterationProcResult zipr;
1219 nc = client_lookup(client);
1222 GNUNET_break_op (0);
1223 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1227 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
1228 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1231 zi->zone = zis_msg->zone;
1233 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1235 res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
1238 /* GNUNET_OK on success */
1242 /* GNUNET_SYSERR on error */
1245 /* GNUNET_NO if there were no results, */
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
1254 zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1255 zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
1256 zir_msg.gns_header.r_id = htonl(zi->request_id);
1258 GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1260 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1263 static void handle_iteration_stop (void *cls,
1264 struct GNUNET_SERVER_Client * client,
1265 const struct GNUNET_MessageHeader * message)
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1269 struct GNUNET_NAMESTORE_Client *nc;
1270 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1271 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1274 nc = client_lookup(client);
1277 GNUNET_break_op (0);
1278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1282 rid = ntohl (zis_msg->gns_header.r_id);
1283 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1285 if (zi->request_id == rid)
1290 GNUNET_break_op (0);
1291 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1295 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
1299 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1302 static void handle_iteration_next (void *cls,
1303 struct GNUNET_SERVER_Client * client,
1304 const struct GNUNET_MessageHeader * message)
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1308 struct GNUNET_NAMESTORE_Client *nc;
1309 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1310 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1314 nc = client_lookup(client);
1317 GNUNET_break_op (0);
1318 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1322 rid = ntohl (zis_msg->gns_header.r_id);
1323 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1325 if (zi->request_id == rid)
1330 GNUNET_break_op (0);
1331 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1336 res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
1342 * Process template requests.
1344 * @param cls closure
1345 * @param server the initialized server
1346 * @param cfg configuration to use
1349 run (void *cls, struct GNUNET_SERVER_Handle *server,
1350 const struct GNUNET_CONFIGURATION_Handle *cfg)
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1356 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1357 {&handle_start, NULL,
1358 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1359 {&handle_lookup_name, NULL,
1360 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1361 {&handle_record_put, NULL,
1362 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1363 {&handle_record_create, NULL,
1364 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1365 {&handle_record_remove, NULL,
1366 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1367 {&handle_zone_to_name, NULL,
1368 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, 0},
1369 {&handle_iteration_start, NULL,
1370 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1371 {&handle_iteration_next, NULL,
1372 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1373 {&handle_iteration_stop, NULL,
1374 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, 0},
1380 /* Loading database plugin */
1382 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1386 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1387 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1388 if (GSN_database == NULL)
1389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1391 GNUNET_free (database);
1393 /* Configuring server handles */
1394 GNUNET_SERVER_add_handlers (server, handlers);
1395 snc = GNUNET_SERVER_notification_context_create (server, 16);
1396 GNUNET_SERVER_disconnect_notify (server,
1397 &client_disconnect_notification,
1400 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1407 * The main function for the template service.
1409 * @param argc number of arguments from the command line
1410 * @param argv command line arguments
1411 * @return 0 ok, 1 on error
1414 main (int argc, char *const *argv)
1416 return (GNUNET_OK ==
1417 GNUNET_SERVICE_run (argc, argv, "namestore",
1418 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1421 /* end of gnunet-service-namestore.c */