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"
34 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
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;
61 struct GNUNET_NAMESTORE_Client
63 struct GNUNET_NAMESTORE_Client *next;
64 struct GNUNET_NAMESTORE_Client *prev;
66 struct GNUNET_SERVER_Client * client;
68 struct GNUNET_NAMESTORE_ZoneIteration *op_head;
69 struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
72 struct GNUNET_NAMESTORE_CryptoContainer
77 struct GNUNET_CRYPTO_RsaPrivateKey *privkey;
78 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
83 * Configuration handle.
85 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
90 struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
95 static char *zonefile_directory;
97 static char *db_lib_name;
101 * Our notification context.
103 static struct GNUNET_SERVER_NotificationContext *snc;
105 static struct GNUNET_NAMESTORE_Client *client_head;
106 static struct GNUNET_NAMESTORE_Client *client_tail;
108 struct GNUNET_CONTAINER_MultiHashMap *zonekeys;
112 * Write zonefile to disk
113 * @param file where to write
116 * @return GNUNET_OK on success, GNUNET_SYSERR on fail
119 int write_key_to_file (const char *filename, struct GNUNET_NAMESTORE_CryptoContainer *c)
121 struct GNUNET_CRYPTO_RsaPrivateKey *ret = c->privkey;
122 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *enc;
123 struct GNUNET_DISK_FileHandle *fd;
125 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
127 GNUNET_HashCode zone;
128 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
129 struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
131 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(filename);
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135 _("File zone `%s' but corrupt content already exists, failed to write! \n"), GNUNET_h2s (&zone));
136 return GNUNET_SYSERR;
139 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
140 GNUNET_CRYPTO_hash(&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone);
141 GNUNET_CRYPTO_rsa_key_free(privkey);
143 if (0 == memcmp (&zone, &c->zone, sizeof(zone)))
145 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
146 _("File zone `%s' containing this key already exists\n"), GNUNET_h2s (&zone));
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152 _("File zone `%s' but different zone key already exists, failed to write! \n"), GNUNET_h2s (&zone));
156 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS, GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
161 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
163 /* must exist but not be accessible, fail for good! */
164 if (0 != ACCESS (filename, R_OK))
165 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
167 GNUNET_break (0); /* what is going on!? */
168 return GNUNET_SYSERR;
171 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
172 return GNUNET_SYSERR;
175 if (GNUNET_YES != GNUNET_DISK_file_lock (fd, 0, sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded), GNUNET_YES))
176 return GNUNET_SYSERR;
177 enc = GNUNET_CRYPTO_rsa_encode_key (ret);
178 GNUNET_assert (enc != NULL);
179 GNUNET_assert (ntohs (enc->len) == GNUNET_DISK_file_write (fd, enc, ntohs (enc->len)));
181 GNUNET_DISK_file_sync (fd);
182 if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded)))
183 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
184 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187 _("Stored zonekey for zone `%s' in file `%s'\n"),GNUNET_h2s(&c->zone), c->filename);
191 int zone_to_disk_it (void *cls,
192 const GNUNET_HashCode * key,
195 struct GNUNET_NAMESTORE_CryptoContainer * c = value;
197 if (c->filename != NULL)
198 write_key_to_file(c->filename, c);
201 GNUNET_asprintf(&c->filename, "%s/%s.zone", zonefile_directory, GNUNET_h2s_full (&c->zone));
202 write_key_to_file(c->filename, c);
205 GNUNET_CONTAINER_multihashmap_remove (zonekeys, key, value);;
206 GNUNET_CRYPTO_rsa_key_free(c->privkey);
207 GNUNET_free (c->pubkey);
208 GNUNET_free(c->filename);
215 struct GNUNET_TIME_Absolute
216 get_block_expiration_time (unsigned int rd_count, const struct GNUNET_NAMESTORE_RecordData *rd)
219 struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get_forever();
221 return GNUNET_TIME_absolute_get_zero();
222 for (c = 0; c < rd_count; c++)
224 if (rd[c].expiration.abs_value < expire.abs_value)
225 expire = rd[c].expiration;
231 * Task run during shutdown.
237 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
240 struct GNUNET_NAMESTORE_ZoneIteration * no;
241 struct GNUNET_NAMESTORE_ZoneIteration * tmp;
242 struct GNUNET_NAMESTORE_Client * nc;
243 struct GNUNET_NAMESTORE_Client * next;
245 GNUNET_SERVER_notification_context_destroy (snc);
248 GNUNET_CONTAINER_multihashmap_iterate(zonekeys, &zone_to_disk_it, NULL);
249 GNUNET_CONTAINER_multihashmap_destroy(zonekeys);
251 for (nc = client_head; nc != NULL; nc = next)
254 for (no = nc->op_head; no != NULL; no = tmp)
256 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
260 GNUNET_SERVER_client_drop(nc->client);
261 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
265 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
266 GNUNET_free (db_lib_name);
267 GNUNET_free_non_null(zonefile_directory);
270 static struct GNUNET_NAMESTORE_Client *
271 client_lookup (struct GNUNET_SERVER_Client *client)
273 struct GNUNET_NAMESTORE_Client * nc;
275 GNUNET_assert (NULL != client);
277 for (nc = client_head; nc != NULL; nc = nc->next)
279 if (client == nc->client)
286 * Called whenever a client is disconnected. Frees our
287 * resources associated with that client.
290 * @param client identification of the client
293 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
295 struct GNUNET_NAMESTORE_ZoneIteration * no;
296 struct GNUNET_NAMESTORE_Client * nc;
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
302 nc = client_lookup (client);
304 if ((NULL == client) || (NULL == nc))
307 for (no = nc->op_head; no != NULL; no = no->next)
309 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
314 GNUNET_SERVER_client_drop(nc->client);
315 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
322 static void handle_start (void *cls,
323 struct GNUNET_SERVER_Client * client,
324 const struct GNUNET_MessageHeader * message)
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
328 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
330 GNUNET_SERVER_notification_context_add (snc, client);
331 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
332 GNUNET_SERVER_client_keep (client);
333 GNUNET_SERVER_receive_done (client, GNUNET_OK);
336 struct LookupNameContext
338 struct GNUNET_NAMESTORE_Client *nc;
340 uint32_t record_type;
341 GNUNET_HashCode *zone;
345 void drop_iterator (void *cls,
346 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
347 struct GNUNET_TIME_Absolute expire,
350 const struct GNUNET_NAMESTORE_RecordData *rd,
351 const struct GNUNET_CRYPTO_RsaSignature *signature)
353 GNUNET_HashCode zone_hash;
355 if (NULL != zone_key)
357 GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_hash);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting zone `%s'\n", GNUNET_h2s (&zone_hash));
359 GSN_database->delete_zone (GSN_database->cls, &zone_hash);
363 (*stop) = GNUNET_YES;
369 handle_lookup_name_it (void *cls,
370 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
371 struct GNUNET_TIME_Absolute expire,
373 unsigned int rd_count,
374 const struct GNUNET_NAMESTORE_RecordData *rd,
375 const struct GNUNET_CRYPTO_RsaSignature *signature)
378 struct LookupNameContext *lnc = cls;
379 struct LookupNameResponseMessage *lnr_msg;
380 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
381 struct GNUNET_NAMESTORE_CryptoContainer *cc;
382 struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
383 struct GNUNET_TIME_Absolute e;
384 GNUNET_HashCode zone_key_hash;
391 int copied_elements = 0;
392 int contains_signature = GNUNET_NO;
393 int authoritative = GNUNET_NO;
397 name_len = strlen(name) + 1;
399 /* count records to copy */
402 if (lnc->record_type != 0)
404 /* special record type needed */
405 for (c = 0; c < rd_count; c ++)
406 if (rd[c].record_type == lnc->record_type)
407 copied_elements++; /* found matching record */
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records with type %u for name `%s' in zone `%s'\n",
409 copied_elements, lnc->record_type, lnc->name, GNUNET_h2s(lnc->zone));
410 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
412 for (c = 0; c < rd_count; c ++)
414 if (rd[c].record_type == lnc->record_type)
416 /* found matching record */
417 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
424 copied_elements = rd_count;
425 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
433 expire = GNUNET_TIME_UNIT_ZERO_ABS;
436 rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
437 char rd_ser[rd_ser_len];
438 GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records for name `%s' in zone `%s'\n",
441 copied_elements, lnc->name, GNUNET_h2s(lnc->zone));
443 if ((copied_elements == rd_count) && (NULL != signature))
444 contains_signature = GNUNET_YES; /* returning all records, so include signature */
446 contains_signature = GNUNET_NO; /* returning not all records, so do not include signature */
449 if ((NULL != zone_key) && (copied_elements == rd_count))
451 GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
452 if (GNUNET_CONTAINER_multihashmap_contains(zonekeys, &zone_key_hash))
454 cc = GNUNET_CONTAINER_multihashmap_get(zonekeys, &zone_key_hash);
455 e = get_block_expiration_time(rd_count, rd);
456 signature_new = GNUNET_NAMESTORE_create_signature(cc->privkey, e, name, rd, rd_count);
457 GNUNET_assert (signature_new != NULL);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating signature for name `%s' with %u records in zone `%s'\n",name, copied_elements, GNUNET_h2s(&zone_key_hash));
459 authoritative = GNUNET_YES;
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am not authoritative for name `%s' in zone `%s'\n",name, GNUNET_h2s(&zone_key_hash));
465 r_size = sizeof (struct LookupNameResponseMessage) +
466 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
471 lnr_msg = GNUNET_malloc (r_size);
472 lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
473 lnr_msg->gns_header.header.size = ntohs (r_size);
474 lnr_msg->gns_header.r_id = htonl (lnc->request_id);
475 lnr_msg->rd_count = htons (copied_elements);
476 lnr_msg->rd_len = htons (rd_ser_len);
477 lnr_msg->name_len = htons (name_len);
478 lnr_msg->expire = GNUNET_TIME_absolute_hton(get_block_expiration_time(copied_elements, rd_selected));
480 if (rd_selected != rd)
481 GNUNET_free (rd_selected);
483 if (zone_key != NULL)
484 lnr_msg->public_key = (*zone_key);
486 memset(&lnr_msg->public_key, '\0', sizeof (lnr_msg->public_key));
488 if (GNUNET_YES == authoritative)
489 { /* use new created signature */
490 lnr_msg->contains_sig = htons (GNUNET_YES);
491 GNUNET_assert (signature_new != NULL);
492 lnr_msg->signature = *signature_new;
493 GNUNET_free (signature_new);
495 else if (GNUNET_YES == contains_signature)
497 /* use existing signature */
498 lnr_msg->contains_sig = htons (GNUNET_YES);
499 GNUNET_assert (signature != NULL);
500 lnr_msg->signature = *signature;
504 /* use no signature */
505 memset (&lnr_msg->signature, '\0', sizeof (lnr_msg->signature));
508 name_tmp = (char *) &lnr_msg[1];
509 rd_tmp = &name_tmp[name_len];
511 memcpy (name_tmp, name, name_len);
512 memcpy (rd_tmp, rd_ser, rd_ser_len);
514 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
515 GNUNET_free (lnr_msg);
518 static void handle_lookup_name (void *cls,
519 struct GNUNET_SERVER_Client * client,
520 const struct GNUNET_MessageHeader * message)
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
523 struct LookupNameContext lnc;
524 struct GNUNET_NAMESTORE_Client *nc;
530 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
533 GNUNET_SERVER_receive_done (client, GNUNET_OK);
537 nc = client_lookup(client);
541 GNUNET_SERVER_receive_done (client, GNUNET_OK);
545 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
546 rid = ntohl (ln_msg->gns_header.r_id);
547 name_len = ntohl (ln_msg->name_len);
548 type = ntohl (ln_msg->record_type);
550 if ((name_len == 0) || (name_len > 256))
553 GNUNET_SERVER_receive_done (client, GNUNET_OK);
557 name = (char *) &ln_msg[1];
558 if (name[name_len -1] != '\0')
561 GNUNET_SERVER_receive_done (client, GNUNET_OK);
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up all records for name `%s' in zone `%s'\n", name, GNUNET_h2s(&ln_msg->zone));
568 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));
570 /* do the actual lookup */
571 lnc.request_id = rid;
573 lnc.record_type = type;
575 lnc.zone = &ln_msg->zone;
576 GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
578 GNUNET_SERVER_receive_done (client, GNUNET_OK);
581 static void handle_record_put (void *cls,
582 struct GNUNET_SERVER_Client * client,
583 const struct GNUNET_MessageHeader * message)
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
586 struct GNUNET_NAMESTORE_Client *nc;
587 struct GNUNET_TIME_Absolute expire;
588 struct GNUNET_CRYPTO_RsaSignature *signature;
589 struct RecordPutResponseMessage rpr_msg;
598 int res = GNUNET_SYSERR;
600 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
603 GNUNET_SERVER_receive_done (client, GNUNET_OK);
607 nc = client_lookup (client);
611 GNUNET_SERVER_receive_done (client, GNUNET_OK);
615 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
617 rid = ntohl (rp_msg->gns_header.r_id);
618 msg_size = ntohs (rp_msg->gns_header.header.size);
619 name_len = ntohs (rp_msg->name_len);
620 rd_count = ntohs (rp_msg->rd_count);
621 rd_ser_len = ntohs(rp_msg->rd_len);
623 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
626 GNUNET_SERVER_receive_done (client, GNUNET_OK);
630 if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
637 msg_size_exp = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
638 if (msg_size != msg_size_exp)
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
642 GNUNET_SERVER_receive_done (client, GNUNET_OK);
645 if ((name_len == 0) || (name_len > 256))
648 GNUNET_SERVER_receive_done (client, GNUNET_OK);
652 name = (char *) &rp_msg[1];
654 if (name[name_len -1] != '\0')
657 GNUNET_SERVER_receive_done (client, GNUNET_OK);
661 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
662 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
664 rd_ser = &name[name_len];
665 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
666 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
667 if (res != GNUNET_OK)
673 GNUNET_HashCode zone_hash;
674 GNUNET_CRYPTO_hash (&rp_msg->public_key, sizeof (rp_msg->public_key), &zone_hash);
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting %u record for name `%s' in zone `%s'\n", rd_count, name, GNUNET_h2s(&zone_hash));
678 /* Database operation */
679 res = GSN_database->put_records(GSN_database->cls,
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
687 name, (res == GNUNET_OK) ? "OK" : "FAIL");
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
692 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
693 rpr_msg.gns_header.header.size = htons (sizeof (struct RecordPutResponseMessage));
694 rpr_msg.gns_header.r_id = htonl (rid);
695 rpr_msg.op_result = htonl (res);
696 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
698 GNUNET_SERVER_receive_done (client, GNUNET_OK);
701 struct CreateRecordContext
703 struct GNUNET_NAMESTORE_RecordData *rd;
704 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
705 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
706 struct GNUNET_TIME_Absolute expire;
713 handle_create_record_it (void *cls,
714 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey,
715 struct GNUNET_TIME_Absolute expire,
717 unsigned int rd_count,
718 const struct GNUNET_NAMESTORE_RecordData *rd,
719 const struct GNUNET_CRYPTO_RsaSignature *signature)
721 struct CreateRecordContext * crc = cls;
722 struct GNUNET_NAMESTORE_RecordData *rd_new = NULL;
723 struct GNUNET_CRYPTO_RsaSignature dummy_signature;
724 struct GNUNET_TIME_Absolute block_expiration;
726 int exist = GNUNET_SYSERR;
727 int update = GNUNET_NO;
729 int rd_count_new = 0;
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, crc->name);
733 for (c = 0; c < rd_count; c++)
736 if ((crc->rd->record_type == rd[c].record_type) &&
737 (crc->rd->data_size == rd[c].data_size) &&
738 (0 == memcmp (crc->rd->data, rd[c].data, rd[c].data_size)))
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing records for `%s' to update expiration date!\n", crc->name);
742 if (crc->rd->expiration.abs_value != rd[c].expiration.abs_value)
748 if (exist == GNUNET_SYSERR)
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New record does not exist for name `%s'!\n", crc->name);
751 if (exist == GNUNET_SYSERR)
753 rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
754 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
755 rd_count_new = rd_count + 1;
756 rd_new[rd_count] = *(crc->rd);
758 else if (update == GNUNET_NO)
760 /* Exact same record already exists */
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No update for %s' record required!\n", crc->name);
765 else if (update == GNUNET_YES)
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing records for `%s'!\n", crc->name);
769 rd_new = GNUNET_malloc ((rd_count) * sizeof (struct GNUNET_NAMESTORE_RecordData));
770 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
771 rd_count_new = rd_count;
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration from %llu to %llu!\n", rd_new[exist].expiration.abs_value, crc->rd->expiration.abs_value);
773 rd_new[exist].expiration = crc->rd->expiration;
776 block_expiration = GNUNET_TIME_absolute_max(crc->expire, expire);
777 if (block_expiration.abs_value != expire.abs_value)
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updated block expiration time\n");
780 memset (&dummy_signature, '\0', sizeof (dummy_signature));
782 /* Database operation */
783 GNUNET_assert ((rd_new != NULL) && (rd_count_new > 0));
784 res = GSN_database->put_records(GSN_database->cls,
785 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
788 rd_count_new, rd_new,
790 GNUNET_break (GNUNET_OK == res);
791 if (res == GNUNET_OK)
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully put record for `%s' in database \n", crc->name);
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to put record for `%s' in database \n", crc->name);
798 GNUNET_free_non_null (rd_new);
802 /* failed to create the record */
803 crc->res = GNUNET_SYSERR;
806 /* database operations OK */
807 if (GNUNET_YES == update)
808 /* we updated an existing record */
809 crc->res = GNUNET_NO;
811 /* we created a new record */
812 crc->res = GNUNET_YES;
815 /* identical entry existed, so we did nothing */
816 crc->res = GNUNET_NO;
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name `%s' %u\n", crc->name, res);
826 static void handle_record_create (void *cls,
827 struct GNUNET_SERVER_Client * client,
828 const struct GNUNET_MessageHeader * message)
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
831 struct GNUNET_NAMESTORE_Client *nc;
832 struct GNUNET_NAMESTORE_CryptoContainer *cc;
833 struct CreateRecordContext crc;
834 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
835 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
836 struct RecordCreateResponseMessage rcr_msg;
837 GNUNET_HashCode pubkey_hash;
849 int res = GNUNET_SYSERR;
850 crc.res = GNUNET_SYSERR;
852 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
855 GNUNET_SERVER_receive_done (client, GNUNET_OK);
859 nc = client_lookup(client);
863 GNUNET_SERVER_receive_done (client, GNUNET_OK);
867 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
868 rid = ntohl (rp_msg->gns_header.r_id);
869 name_len = ntohs (rp_msg->name_len);
870 msg_size = ntohs (message->size);
871 rd_count = ntohs (rp_msg->rd_count);
872 rd_ser_len = ntohs (rp_msg->rd_len);
873 key_len = ntohs (rp_msg->pkey_len);
874 msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
876 if (msg_size != msg_size_exp)
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
880 GNUNET_SERVER_receive_done (client, GNUNET_OK);
884 if ((name_len == 0) || (name_len > 256))
887 GNUNET_SERVER_receive_done (client, GNUNET_OK);
891 pkey_tmp = (char *) &rp_msg[1];
892 name_tmp = &pkey_tmp[key_len];
893 rd_ser = &name_tmp[name_len];
895 if (name_tmp[name_len -1] != '\0')
898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
902 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
904 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
905 if ((res != GNUNET_OK) || (rd_count != 1))
911 /* Extracting and converting private key */
912 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
913 GNUNET_assert (pkey != NULL);
914 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
915 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
917 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(zonekeys, &pubkey_hash))
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received new private key for zone `%s'\n",GNUNET_h2s(&pubkey_hash));
921 cc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
922 cc->privkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
923 cc->pubkey = GNUNET_malloc(sizeof (pub));
924 memcpy (cc->pubkey, &pub, sizeof(pub));
925 cc->zone = pubkey_hash;
927 GNUNET_CONTAINER_multihashmap_put(zonekeys, &pubkey_hash, cc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
930 crc.expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
931 crc.res = GNUNET_SYSERR;
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
939 /* Get existing records for name */
940 res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
941 if (res != GNUNET_SYSERR)
943 GNUNET_CRYPTO_rsa_key_free(pkey);
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
949 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
950 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
951 rcr_msg.gns_header.r_id = htonl (rid);
952 if ((GNUNET_OK == res) && (crc.res == GNUNET_YES))
953 rcr_msg.op_result = htonl (GNUNET_YES);
954 else if ((GNUNET_OK == res) && (crc.res == GNUNET_NO))
955 rcr_msg.op_result = htonl (GNUNET_NO);
957 rcr_msg.op_result = htonl (GNUNET_SYSERR);
958 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
960 GNUNET_SERVER_receive_done (client, GNUNET_OK);
964 struct RemoveRecordContext
966 struct GNUNET_NAMESTORE_RecordData *rd;
967 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
972 handle_record_remove_it (void *cls,
973 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
974 struct GNUNET_TIME_Absolute expire,
976 unsigned int rd_count,
977 const struct GNUNET_NAMESTORE_RecordData *rd,
978 const struct GNUNET_CRYPTO_RsaSignature *signature)
980 struct RemoveRecordContext *rrc = cls;
984 unsigned int rd_count_new;
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
990 /* Could not find record to remove */
995 /* Find record to remove */
996 found = GNUNET_SYSERR;
997 for (c = 0; c < rd_count; c++)
999 if ((rd[c].expiration.abs_value == rrc->rd->expiration.abs_value) &&
1000 (rd[c].flags == rrc->rd->flags) &&
1001 (rd[c].record_type == rrc->rd->record_type) &&
1002 (rd[c].data_size == rrc->rd->data_size) &&
1003 (0 == memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size)))
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
1010 if (GNUNET_SYSERR == found)
1012 /* Could not find record to remove */
1017 rd_count_new = rd_count -1;
1018 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
1020 unsigned int c2 = 0;
1021 for (c = 0; c < rd_count; c++)
1025 GNUNET_assert (c2 < rd_count_new);
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
1033 /* Create dummy signature */
1034 struct GNUNET_CRYPTO_RsaSignature dummy_signature;
1035 memset (&dummy_signature, '\0', sizeof (dummy_signature));
1039 res = GSN_database->put_records(GSN_database->cls,
1043 rd_count_new, rd_new,
1045 if (GNUNET_OK != res)
1047 /* Could put records into database */
1055 static void handle_record_remove (void *cls,
1056 struct GNUNET_SERVER_Client * client,
1057 const struct GNUNET_MessageHeader * message)
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
1060 struct GNUNET_NAMESTORE_Client *nc;
1061 struct RecordRemoveResponseMessage rrr_msg;
1062 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
1063 struct GNUNET_NAMESTORE_CryptoContainer *cc = NULL;
1064 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1065 GNUNET_HashCode pubkey_hash;
1066 char * pkey_tmp = NULL;
1067 char * name_tmp = NULL;
1068 char * rd_ser = NULL;
1070 size_t name_len = 0;
1071 size_t rd_ser_len = 0;
1072 size_t msg_size = 0;
1073 size_t msg_size_exp = 0;
1077 int res = GNUNET_SYSERR;
1079 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
1081 GNUNET_break_op (0);
1082 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1086 nc = client_lookup(client);
1089 GNUNET_break_op (0);
1090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1094 struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
1095 rid = ntohl (rr_msg->gns_header.r_id);
1096 name_len = ntohs (rr_msg->name_len);
1097 rd_ser_len = ntohs (rr_msg->rd_len);
1098 rd_count = ntohs (rr_msg->rd_count);
1099 key_len = ntohs (rr_msg->pkey_len);
1100 msg_size = ntohs (message->size);
1102 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1104 GNUNET_break_op (0);
1105 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1109 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
1111 GNUNET_break_op (0);
1112 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1116 msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1117 if (msg_size != msg_size_exp)
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
1120 GNUNET_break_op (0);
1121 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1125 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
1127 GNUNET_break_op (0);
1128 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1132 pkey_tmp = (char *) &rr_msg[1];
1133 name_tmp = &pkey_tmp[key_len];
1134 rd_ser = &name_tmp[name_len];
1137 if ((name_len == 0) || (name_len > 256))
1139 GNUNET_break_op (0);
1140 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1144 if (name_tmp[name_len -1] != '\0')
1146 GNUNET_break_op (0);
1147 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1151 /* Extracting and converting private key */
1152 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
1153 GNUNET_assert (pkey != NULL);
1154 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
1155 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
1157 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(zonekeys, &pubkey_hash))
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received new private key for zone `%s'\n",GNUNET_h2s(&pubkey_hash));
1160 cc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
1161 cc->privkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
1162 cc->pubkey = GNUNET_malloc(sizeof (pub));
1163 memcpy (cc->pubkey, &pub, sizeof(pub));
1164 cc->zone = pubkey_hash;
1166 GNUNET_CONTAINER_multihashmap_put(zonekeys, &pubkey_hash, cc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1169 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
1170 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
1171 if ((res != GNUNET_OK) || (rd_count != 1))
1173 GNUNET_break_op (0);
1177 struct RemoveRecordContext rrc;
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
1183 /* Database operation */
1184 res = GSN_database->iterate_records (GSN_database->cls,
1188 handle_record_remove_it, &rrc);
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
1191 name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
1197 rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
1198 rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
1199 rrr_msg.gns_header.r_id = htonl (rid);
1200 rrr_msg.op_result = htonl (res);
1201 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
1203 GNUNET_CRYPTO_rsa_key_free (pkey);
1205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1209 struct ZoneToNameCtx
1211 struct GNUNET_NAMESTORE_Client *nc;
1216 handle_zone_to_name_it (void *cls,
1217 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1218 struct GNUNET_TIME_Absolute expire,
1220 unsigned int rd_count,
1221 const struct GNUNET_NAMESTORE_RecordData *rd,
1222 const struct GNUNET_CRYPTO_RsaSignature *signature)
1224 struct ZoneToNameCtx * ztn_ctx = cls;
1225 struct ZoneToNameResponseMessage *ztnr_msg;
1226 int16_t res = GNUNET_SYSERR;
1227 uint16_t name_len = 0;
1228 uint16_t rd_ser_len = 0 ;
1229 int32_t contains_sig = 0;
1230 size_t msg_size = 0;
1232 char *rd_ser = NULL;
1237 if ((zone_key != NULL) && (name != NULL))
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found results: name is `%s', has %u records\n", name, rd_count);
1242 name_len = strlen (name);
1246 /* no result found */
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found no results\n");
1254 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1255 rd_ser = GNUNET_malloc (rd_ser_len);
1256 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1261 if (signature != NULL)
1262 contains_sig = GNUNET_YES;
1264 contains_sig = GNUNET_NO;
1268 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len + contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
1269 ztnr_msg = GNUNET_malloc (msg_size);
1271 name_tmp = (char *) &ztnr_msg[1];
1272 rd_tmp = &name_tmp[name_len];
1273 sig_tmp = &rd_tmp[rd_ser_len];
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_TO_NAME_RESPONSE");
1276 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1277 ztnr_msg->gns_header.header.size = htons (msg_size);
1278 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1279 ztnr_msg->res = htons (res);
1280 ztnr_msg->rd_len = htons (rd_ser_len);
1281 ztnr_msg->rd_count = htons (rd_count);
1282 ztnr_msg->name_len = htons (name_len);
1283 ztnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
1284 if (zone_key != NULL)
1285 ztnr_msg->zone_key = *zone_key;
1287 memset (&ztnr_msg->zone_key, '\0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1289 if ((name_len > 0) && (name != NULL))
1290 memcpy (name_tmp, name, name_len);
1292 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);
1293 if ((rd_ser_len > 0) && (rd_ser != NULL))
1294 memcpy (rd_tmp, rd_ser, rd_ser_len);
1295 if ((GNUNET_YES == contains_sig) && (signature != NULL))
1296 memcpy (sig_tmp, signature, contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature));
1298 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client, (const struct GNUNET_MessageHeader *) ztnr_msg, GNUNET_NO);
1299 GNUNET_free (ztnr_msg);
1300 GNUNET_free_non_null (rd_ser);
1304 static void handle_zone_to_name (void *cls,
1305 struct GNUNET_SERVER_Client * client,
1306 const struct GNUNET_MessageHeader * message)
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_TO_NAME");
1309 struct GNUNET_NAMESTORE_Client *nc;
1310 struct ZoneToNameCtx ztn_ctx;
1311 size_t msg_size = 0;
1314 if (ntohs (message->size) != sizeof (struct ZoneToNameMessage))
1316 GNUNET_break_op (0);
1317 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1321 nc = client_lookup(client);
1324 GNUNET_break_op (0);
1325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1329 struct ZoneToNameMessage *ztn_msg = (struct ZoneToNameMessage *) message;
1331 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1333 GNUNET_break_op (0);
1334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1338 rid = ntohl (ztn_msg->gns_header.r_id);
1343 char * z_tmp = strdup (GNUNET_h2s (&ztn_msg->zone));
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up name for zone `%s' in zone `%s'\n",
1346 GNUNET_h2s (&ztn_msg->value_zone));
1347 GNUNET_free (z_tmp);
1349 GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
1351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1354 struct ZoneIterationProcResult
1357 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
1360 struct GNUNET_CRYPTO_RsaSignature signature;
1361 struct GNUNET_TIME_Absolute expire;
1371 void zone_iteration_proc (void *cls,
1372 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1373 struct GNUNET_TIME_Absolute expire,
1375 unsigned int rd_count,
1376 const struct GNUNET_NAMESTORE_RecordData *rd,
1377 const struct GNUNET_CRYPTO_RsaSignature *signature)
1379 struct GNUNET_NAMESTORE_ZoneIteration *zi = cls;
1380 struct GNUNET_NAMESTORE_Client *nc = zi->client;
1381 struct GNUNET_NAMESTORE_CryptoContainer * cc;
1382 struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
1383 struct GNUNET_TIME_Absolute e;
1384 GNUNET_HashCode zone_key_hash;
1385 int authoritative = GNUNET_NO;
1387 if ((zone_key == NULL) && (name == NULL))
1389 struct ZoneIterationResponseMessage zir_msg;
1390 if (zi->has_zone == GNUNET_YES)
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for zone `%s'\n", GNUNET_h2s(&zi->zone));
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for all zones\n");
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message\n", "ZONE_ITERATION_RESPONSE");
1396 zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1397 zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
1398 zir_msg.gns_header.r_id = htonl(zi->request_id);
1399 zir_msg.expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_zero());
1400 zir_msg.name_len = htons (0);
1401 zir_msg.reserved = htons (0);
1402 zir_msg.rd_count = htons (0);
1403 zir_msg.rd_len = htons (0);
1404 memset (&zir_msg.public_key, '\0', sizeof (zir_msg.public_key));
1405 memset (&zir_msg.signature, '\0', sizeof (zir_msg.signature));
1406 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing zone iterator\n");
1409 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1415 struct ZoneIterationResponseMessage *zir_msg;
1416 if (zi->has_zone == GNUNET_YES)
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over zone `%s'\n",
1418 name, GNUNET_h2s(&zi->zone));
1419 if (zi->has_zone == GNUNET_NO)
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over all zones\n",
1428 name_len = strlen (name) +1;
1430 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1431 char rd_ser[rd_ser_len];
1432 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1433 msg_size = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_ser_len;
1434 zir_msg = GNUNET_malloc(msg_size);
1436 name_tmp = (char *) &zir_msg[1];
1437 rd_tmp = &name_tmp[name_len];
1439 GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
1440 if (GNUNET_CONTAINER_multihashmap_contains(zonekeys, &zone_key_hash))
1442 cc = GNUNET_CONTAINER_multihashmap_get(zonekeys, &zone_key_hash);
1443 e = get_block_expiration_time(rd_count, rd);
1445 signature_new = GNUNET_NAMESTORE_create_signature(cc->privkey, e, name, rd, rd_count);
1446 GNUNET_assert (signature_new != NULL);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating signature for `%s' in zone `%s' with %u records and expiration %llu\n", name, GNUNET_h2s(&zone_key_hash), rd_count, e.abs_value);
1448 authoritative = GNUNET_YES;
1452 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1453 zir_msg->gns_header.header.size = htons (msg_size);
1454 zir_msg->gns_header.r_id = htonl(zi->request_id);
1455 zir_msg->expire = GNUNET_TIME_absolute_hton(expire);
1456 zir_msg->reserved = htons (0);
1457 zir_msg->name_len = htons (name_len);
1458 zir_msg->rd_count = htons (rd_count);
1459 zir_msg->rd_len = htons (rd_ser_len);
1460 if ((GNUNET_YES == authoritative) && (NULL != signature_new))
1462 zir_msg->signature = *signature_new;
1463 GNUNET_free (signature_new);
1466 zir_msg->signature = *signature;
1467 GNUNET_assert (NULL != zone_key);
1468 if (zone_key != NULL)
1469 zir_msg->public_key = *zone_key;
1470 memcpy (name_tmp, name, name_len);
1471 memcpy (rd_tmp, rd_ser, rd_ser_len);
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message with size %u\n", "ZONE_ITERATION_RESPONSE", msg_size);
1474 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) zir_msg, GNUNET_NO);
1475 GNUNET_free (zir_msg);
1479 static void handle_iteration_start (void *cls,
1480 struct GNUNET_SERVER_Client * client,
1481 const struct GNUNET_MessageHeader * message)
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1485 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
1486 struct GNUNET_NAMESTORE_Client *nc;
1487 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1490 nc = client_lookup(client);
1493 GNUNET_break_op (0);
1494 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1498 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
1499 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1502 zi->zone = zis_msg->zone;
1504 GNUNET_HashCode dummy;
1505 GNUNET_HashCode *zone_tmp;
1506 memset (&dummy, '\0', sizeof (dummy));
1507 if (0 == memcmp (&dummy, &zis_msg->zone, sizeof (dummy)))
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over all zones\n");
1510 zi->has_zone = GNUNET_NO;
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over zone `%s'\n", GNUNET_h2s (&zis_msg->zone));
1516 zi->has_zone = GNUNET_YES;
1517 zone_tmp = &zis_msg->zone;
1520 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1522 res = GSN_database->iterate_records (GSN_database->cls, zone_tmp , NULL, zi->offset , &zone_iteration_proc, zi);
1523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1526 static void handle_iteration_stop (void *cls,
1527 struct GNUNET_SERVER_Client * client,
1528 const struct GNUNET_MessageHeader * message)
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1532 struct GNUNET_NAMESTORE_Client *nc;
1533 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1534 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1537 nc = client_lookup(client);
1540 GNUNET_break_op (0);
1541 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1545 rid = ntohl (zis_msg->gns_header.r_id);
1546 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1548 if (zi->request_id == rid)
1553 GNUNET_break_op (0);
1554 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1558 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1559 if (GNUNET_YES == zi->has_zone)
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration all zones\n");
1565 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1568 static void handle_iteration_next (void *cls,
1569 struct GNUNET_SERVER_Client * client,
1570 const struct GNUNET_MessageHeader * message)
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1574 struct GNUNET_NAMESTORE_Client *nc;
1575 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1576 GNUNET_HashCode *zone_tmp;
1577 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1580 nc = client_lookup(client);
1583 GNUNET_break_op (0);
1584 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1588 rid = ntohl (zis_msg->gns_header.r_id);
1589 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1591 if (zi->request_id == rid)
1596 GNUNET_break_op (0);
1597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1601 if (GNUNET_YES == zi->has_zone)
1602 zone_tmp = &zi->zone;
1607 GSN_database->iterate_records (GSN_database->cls, zone_tmp, NULL, zi->offset , &zone_iteration_proc, zi);
1608 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1611 int zonekey_file_it (void *cls, const char *filename)
1614 if ((filename != NULL) && (NULL != strstr(filename, ".zone")))
1616 struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
1617 struct GNUNET_NAMESTORE_CryptoContainer *c;
1618 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(filename);
1619 if (privkey == NULL)
1622 c = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
1623 c->pubkey = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1624 c->privkey = privkey;
1625 GNUNET_CRYPTO_rsa_key_get_public(privkey, c->pubkey);
1626 GNUNET_CRYPTO_hash(c->pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &c->zone);
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found zonefile for zone `%s'\n", GNUNET_h2s (&c->zone));
1630 GNUNET_CONTAINER_multihashmap_put(zonekeys, &c->zone, c, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1638 * Process template requests.
1640 * @param cls closure
1641 * @param server the initialized server
1642 * @param cfg configuration to use
1645 run (void *cls, struct GNUNET_SERVER_Handle *server,
1646 const struct GNUNET_CONFIGURATION_Handle *cfg)
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1652 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1653 {&handle_start, NULL,
1654 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1655 {&handle_lookup_name, NULL,
1656 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1657 {&handle_record_put, NULL,
1658 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1659 {&handle_record_create, NULL,
1660 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1661 {&handle_record_remove, NULL,
1662 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1663 {&handle_zone_to_name, NULL,
1664 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, 0},
1665 {&handle_iteration_start, NULL,
1666 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1667 {&handle_iteration_next, NULL,
1668 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1669 {&handle_iteration_stop, NULL,
1670 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, 0},
1676 /* Load private keys from disk */
1678 GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore", "zonefile_directory",
1679 &zonefile_directory))
1681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No directory to load zonefiles specified in configuration\n"));
1682 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1686 if (GNUNET_NO == GNUNET_DISK_file_test (zonefile_directory))
1688 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (zonefile_directory))
1690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Creating directory `%s' for zone files failed!\n"), zonefile_directory);
1691 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created directory `%s' for zone files\n", zonefile_directory);
1697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scanning directory `%s' for zone files\n", zonefile_directory);
1698 zonekeys = GNUNET_CONTAINER_multihashmap_create (10);
1699 GNUNET_DISK_directory_scan (zonefile_directory, zonekey_file_it, &counter);
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u zone files\n", counter);
1702 /* Loading database plugin */
1704 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1708 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1709 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1710 GNUNET_free (database);
1711 if (GSN_database == NULL)
1713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1715 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1719 /* Configuring server handles */
1720 GNUNET_SERVER_add_handlers (server, handlers);
1721 snc = GNUNET_SERVER_notification_context_create (server, 16);
1722 GNUNET_SERVER_disconnect_notify (server,
1723 &client_disconnect_notification,
1726 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1733 * The main function for the template service.
1735 * @param argc number of arguments from the command line
1736 * @param argv command line arguments
1737 * @return 0 ok, 1 on error
1740 main (int argc, char *const *argv)
1742 return (GNUNET_OK ==
1743 GNUNET_SERVICE_run (argc, argv, "namestore",
1744 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1747 /* end of gnunet-service-namestore.c */