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;
48 struct GNUNET_CRYPTO_ShortHashCode zone;
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
76 struct GNUNET_CRYPTO_ShortHashCode zone;
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 filename where to write
114 * @param c the crypto container
116 * @return GNUNET_OK on success, GNUNET_SYSERR on fail
120 write_key_to_file (const char *filename, struct GNUNET_NAMESTORE_CryptoContainer *c)
122 struct GNUNET_CRYPTO_RsaPrivateKey *ret = c->privkey;
123 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *enc;
124 struct GNUNET_DISK_FileHandle *fd;
126 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
128 struct GNUNET_CRYPTO_ShortHashCode zone;
129 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
130 struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
132 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(filename);
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136 _("File zone `%s' but corrupt content already exists, failed to write! \n"), GNUNET_short_h2s (&zone));
137 return GNUNET_SYSERR;
140 GNUNET_CRYPTO_rsa_key_get_public (privkey, &pubkey);
141 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone);
142 GNUNET_CRYPTO_rsa_key_free (privkey);
144 if (0 == memcmp (&zone, &c->zone, sizeof(zone)))
146 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
147 _("File zone `%s' containing this key already exists\n"), GNUNET_short_h2s (&zone));
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 _("File zone `%s' but different zone key already exists, failed to write! \n"), GNUNET_short_h2s (&zone));
157 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);
162 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
164 /* must exist but not be accessible, fail for good! */
165 if (0 != ACCESS (filename, R_OK))
166 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
168 GNUNET_break (0); /* what is going on!? */
169 return GNUNET_SYSERR;
172 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
173 return GNUNET_SYSERR;
176 if (GNUNET_YES != GNUNET_DISK_file_lock (fd, 0, sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded), GNUNET_YES))
177 return GNUNET_SYSERR;
178 enc = GNUNET_CRYPTO_rsa_encode_key (ret);
179 GNUNET_assert (enc != NULL);
180 GNUNET_assert (ntohs (enc->len) == GNUNET_DISK_file_write (fd, enc, ntohs (enc->len)));
182 GNUNET_DISK_file_sync (fd);
183 if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded)))
184 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
185 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 _("Stored zonekey for zone `%s' in file `%s'\n"), GNUNET_short_h2s(&c->zone), c->filename);
192 int zone_to_disk_it (void *cls,
193 const GNUNET_HashCode *key,
196 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.zkey", zonefile_directory, GNUNET_short_h2s (&c->zone));
202 write_key_to_file(c->filename, c);
206 GNUNET_CONTAINER_multihashmap_remove (zonekeys, key, value);
207 GNUNET_CRYPTO_rsa_key_free (c->privkey);
208 GNUNET_free (c->pubkey);
209 GNUNET_free (c->filename);
216 struct GNUNET_TIME_Absolute
217 get_block_expiration_time (unsigned int rd_count, const struct GNUNET_NAMESTORE_RecordData *rd)
220 struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get_forever();
222 return GNUNET_TIME_absolute_get_zero();
223 for (c = 0; c < rd_count; c++)
225 if (rd[c].expiration.abs_value < expire.abs_value)
226 expire = rd[c].expiration;
232 * Task run during shutdown.
238 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
241 struct GNUNET_NAMESTORE_ZoneIteration * no;
242 struct GNUNET_NAMESTORE_ZoneIteration * tmp;
243 struct GNUNET_NAMESTORE_Client * nc;
244 struct GNUNET_NAMESTORE_Client * next;
246 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))
310 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
315 GNUNET_SERVER_client_drop(nc->client);
316 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
323 static void handle_start (void *cls,
324 struct GNUNET_SERVER_Client * client,
325 const struct GNUNET_MessageHeader * message)
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
329 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
331 GNUNET_SERVER_notification_context_add (snc, client);
332 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
333 GNUNET_SERVER_client_keep (client);
334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
337 struct LookupNameContext
339 struct GNUNET_NAMESTORE_Client *nc;
341 uint32_t record_type;
342 struct GNUNET_CRYPTO_ShortHashCode *zone;
346 void drop_iterator (void *cls,
347 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
348 struct GNUNET_TIME_Absolute expire,
351 const struct GNUNET_NAMESTORE_RecordData *rd,
352 const struct GNUNET_CRYPTO_RsaSignature *signature)
354 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
356 if (NULL != zone_key)
358 GNUNET_CRYPTO_short_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_hash);
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting zone `%s'\n", GNUNET_short_h2s (&zone_hash));
360 GSN_database->delete_zone (GSN_database->cls, &zone_hash);
364 (*stop) = GNUNET_YES;
370 handle_lookup_name_it (void *cls,
371 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
372 struct GNUNET_TIME_Absolute expire,
374 unsigned int rd_count,
375 const struct GNUNET_NAMESTORE_RecordData *rd,
376 const struct GNUNET_CRYPTO_RsaSignature *signature)
379 struct LookupNameContext *lnc = cls;
380 struct LookupNameResponseMessage *lnr_msg;
381 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
382 struct GNUNET_NAMESTORE_CryptoContainer *cc;
383 struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
384 struct GNUNET_TIME_Absolute e;
385 struct GNUNET_CRYPTO_ShortHashCode zone_key_hash;
386 GNUNET_HashCode long_hash;
393 int copied_elements = 0;
394 int contains_signature = GNUNET_NO;
395 int authoritative = GNUNET_NO;
399 name_len = strlen(name) + 1;
401 /* count records to copy */
404 if (lnc->record_type != 0)
406 /* special record type needed */
407 for (c = 0; c < rd_count; c ++)
408 if (rd[c].record_type == lnc->record_type)
409 copied_elements++; /* found matching record */
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records with type %u for name `%s' in zone `%s'\n",
411 copied_elements, lnc->record_type, lnc->name, GNUNET_short_h2s(lnc->zone));
412 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
414 for (c = 0; c < rd_count; c ++)
416 if (rd[c].record_type == lnc->record_type)
418 /* found matching record */
419 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
426 copied_elements = rd_count;
427 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
435 expire = GNUNET_TIME_UNIT_ZERO_ABS;
438 rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
439 char rd_ser[rd_ser_len];
440 GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records for name `%s' in zone `%s'\n",
443 copied_elements, lnc->name, GNUNET_short_h2s(lnc->zone));
445 if ((copied_elements == rd_count) && (NULL != signature))
446 contains_signature = GNUNET_YES; /* returning all records, so include signature */
448 contains_signature = GNUNET_NO; /* returning not all records, so do not include signature */
451 if ((NULL != zone_key) && (copied_elements == rd_count))
453 GNUNET_CRYPTO_short_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
454 GNUNET_CRYPTO_short_hash_double (&zone_key_hash, &long_hash);
455 if (GNUNET_CONTAINER_multihashmap_contains(zonekeys, &long_hash))
457 cc = GNUNET_CONTAINER_multihashmap_get(zonekeys, &long_hash);
458 e = get_block_expiration_time(rd_count, rd);
459 signature_new = GNUNET_NAMESTORE_create_signature(cc->privkey, e, name, rd, rd_count);
460 GNUNET_assert (signature_new != NULL);
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating signature for name `%s' with %u records in zone `%s'\n",name, copied_elements, GNUNET_short_h2s(&zone_key_hash));
462 authoritative = GNUNET_YES;
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am not authoritative for name `%s' in zone `%s'\n",name, GNUNET_short_h2s(&zone_key_hash));
468 r_size = sizeof (struct LookupNameResponseMessage) +
469 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
474 lnr_msg = GNUNET_malloc (r_size);
475 lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
476 lnr_msg->gns_header.header.size = ntohs (r_size);
477 lnr_msg->gns_header.r_id = htonl (lnc->request_id);
478 lnr_msg->rd_count = htons (copied_elements);
479 lnr_msg->rd_len = htons (rd_ser_len);
480 lnr_msg->name_len = htons (name_len);
481 lnr_msg->expire = GNUNET_TIME_absolute_hton(get_block_expiration_time(copied_elements, rd_selected));
483 if (rd_selected != rd)
484 GNUNET_free (rd_selected);
486 if (zone_key != NULL)
487 lnr_msg->public_key = (*zone_key);
489 memset(&lnr_msg->public_key, '\0', sizeof (lnr_msg->public_key));
491 if (GNUNET_YES == authoritative)
492 { /* use new created signature */
493 lnr_msg->contains_sig = htons (GNUNET_YES);
494 GNUNET_assert (signature_new != NULL);
495 lnr_msg->signature = *signature_new;
496 GNUNET_free (signature_new);
498 else if (GNUNET_YES == contains_signature)
500 /* use existing signature */
501 lnr_msg->contains_sig = htons (GNUNET_YES);
502 GNUNET_assert (signature != NULL);
503 lnr_msg->signature = *signature;
507 /* use no signature */
508 memset (&lnr_msg->signature, '\0', sizeof (lnr_msg->signature));
511 name_tmp = (char *) &lnr_msg[1];
512 rd_tmp = &name_tmp[name_len];
514 memcpy (name_tmp, name, name_len);
515 memcpy (rd_tmp, rd_ser, rd_ser_len);
517 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
518 GNUNET_free (lnr_msg);
521 static void handle_lookup_name (void *cls,
522 struct GNUNET_SERVER_Client * client,
523 const struct GNUNET_MessageHeader * message)
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
526 struct LookupNameContext lnc;
527 struct GNUNET_NAMESTORE_Client *nc;
533 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
536 GNUNET_SERVER_receive_done (client, GNUNET_OK);
540 nc = client_lookup(client);
544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
548 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
549 rid = ntohl (ln_msg->gns_header.r_id);
550 name_len = ntohl (ln_msg->name_len);
551 type = ntohl (ln_msg->record_type);
553 if ((name_len == 0) || (name_len > 256))
556 GNUNET_SERVER_receive_done (client, GNUNET_OK);
560 name = (char *) &ln_msg[1];
561 if (name[name_len -1] != '\0')
564 GNUNET_SERVER_receive_done (client, GNUNET_OK);
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up all records for name `%s' in zone `%s'\n", name, GNUNET_short_h2s(&ln_msg->zone));
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up records with type %u for name `%s' in zone `%s'\n", type, name, GNUNET_short_h2s(&ln_msg->zone));
573 /* do the actual lookup */
574 lnc.request_id = rid;
576 lnc.record_type = type;
578 lnc.zone = &ln_msg->zone;
579 GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
581 GNUNET_SERVER_receive_done (client, GNUNET_OK);
584 static void handle_record_put (void *cls,
585 struct GNUNET_SERVER_Client * client,
586 const struct GNUNET_MessageHeader * message)
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
589 struct GNUNET_NAMESTORE_Client *nc;
590 struct GNUNET_TIME_Absolute expire;
591 struct GNUNET_CRYPTO_RsaSignature *signature;
592 struct RecordPutResponseMessage rpr_msg;
601 int res = GNUNET_SYSERR;
603 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
606 GNUNET_SERVER_receive_done (client, GNUNET_OK);
610 nc = client_lookup (client);
614 GNUNET_SERVER_receive_done (client, GNUNET_OK);
618 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
620 rid = ntohl (rp_msg->gns_header.r_id);
621 msg_size = ntohs (rp_msg->gns_header.header.size);
622 name_len = ntohs (rp_msg->name_len);
623 rd_count = ntohs (rp_msg->rd_count);
624 rd_ser_len = ntohs(rp_msg->rd_len);
626 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
629 GNUNET_SERVER_receive_done (client, GNUNET_OK);
633 if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
636 GNUNET_SERVER_receive_done (client, GNUNET_OK);
640 msg_size_exp = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
641 if (msg_size != msg_size_exp)
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
645 GNUNET_SERVER_receive_done (client, GNUNET_OK);
648 if ((name_len == 0) || (name_len > 256))
651 GNUNET_SERVER_receive_done (client, GNUNET_OK);
655 name = (char *) &rp_msg[1];
657 if (name[name_len -1] != '\0')
660 GNUNET_SERVER_receive_done (client, GNUNET_OK);
664 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
665 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
667 rd_ser = &name[name_len];
668 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
669 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
670 if (res != GNUNET_OK)
676 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
677 GNUNET_CRYPTO_short_hash (&rp_msg->public_key, sizeof (rp_msg->public_key), &zone_hash);
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting %u record for name `%s' in zone `%s'\n", rd_count, name, GNUNET_short_h2s(&zone_hash));
681 /* Database operation */
682 res = GSN_database->put_records(GSN_database->cls,
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
690 name, (res == GNUNET_OK) ? "OK" : "FAIL");
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
695 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
696 rpr_msg.gns_header.header.size = htons (sizeof (struct RecordPutResponseMessage));
697 rpr_msg.gns_header.r_id = htonl (rid);
698 rpr_msg.op_result = htonl (res);
699 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
701 GNUNET_SERVER_receive_done (client, GNUNET_OK);
704 struct CreateRecordContext
706 struct GNUNET_NAMESTORE_RecordData *rd;
707 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
708 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
709 struct GNUNET_TIME_Absolute expire;
716 handle_create_record_it (void *cls,
717 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey,
718 struct GNUNET_TIME_Absolute expire,
720 unsigned int rd_count,
721 const struct GNUNET_NAMESTORE_RecordData *rd,
722 const struct GNUNET_CRYPTO_RsaSignature *signature)
724 struct CreateRecordContext * crc = cls;
725 struct GNUNET_NAMESTORE_RecordData *rd_new = NULL;
726 struct GNUNET_CRYPTO_RsaSignature dummy_signature;
727 struct GNUNET_TIME_Absolute block_expiration;
729 int exist = GNUNET_SYSERR;
730 int update = GNUNET_NO;
732 int rd_count_new = 0;
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, crc->name);
735 for (c = 0; c < rd_count; c++)
737 if ((crc->rd->record_type == GNUNET_NAMESTORE_TYPE_PKEY) && (rd[c].record_type == GNUNET_NAMESTORE_TYPE_PKEY))
739 /* Update unique PKEY */
744 else if ((crc->rd->record_type == GNUNET_NAMESTORE_TYPE_PSEU) && (rd[c].record_type == GNUNET_NAMESTORE_TYPE_PSEU))
746 /* Update unique PSEU */
751 else if ((crc->rd->record_type == rd[c].record_type) &&
752 (crc->rd->data_size == rd[c].data_size) &&
753 (0 == memcmp (crc->rd->data, rd[c].data, rd[c].data_size)))
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing records for `%s' to update expiration date!\n", crc->name);
757 if (crc->rd->expiration.abs_value != rd[c].expiration.abs_value)
763 if (exist == GNUNET_SYSERR)
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New record does not exist for name `%s'!\n", crc->name);
766 if (exist == GNUNET_SYSERR)
768 rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
769 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
770 rd_count_new = rd_count + 1;
771 rd_new[rd_count] = *(crc->rd);
773 else if (update == GNUNET_NO)
775 /* Exact same record already exists */
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No update for %s' record required!\n", crc->name);
780 else if (update == GNUNET_YES)
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing records for `%s'!\n", crc->name);
784 rd_new = GNUNET_malloc ((rd_count) * sizeof (struct GNUNET_NAMESTORE_RecordData));
785 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
786 rd_count_new = rd_count;
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration from %llu to %llu!\n", rd_new[exist].expiration.abs_value, crc->rd->expiration.abs_value);
788 rd_new[exist] = *(crc->rd);
791 block_expiration = GNUNET_TIME_absolute_max(crc->expire, expire);
792 if (block_expiration.abs_value != expire.abs_value)
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updated block expiration time\n");
795 memset (&dummy_signature, '\0', sizeof (dummy_signature));
797 /* Database operation */
798 GNUNET_assert ((rd_new != NULL) && (rd_count_new > 0));
799 res = GSN_database->put_records(GSN_database->cls,
800 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
803 rd_count_new, rd_new,
805 GNUNET_break (GNUNET_OK == res);
806 if (res == GNUNET_OK)
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully put record for `%s' in database \n", crc->name);
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to put record for `%s' in database \n", crc->name);
813 GNUNET_free_non_null (rd_new);
817 /* failed to create the record */
818 crc->res = GNUNET_SYSERR;
821 /* database operations OK */
822 if (GNUNET_YES == update)
824 /* we updated an existing record */
825 crc->res = GNUNET_NO;
829 /* we created a new record */
830 crc->res = GNUNET_YES;
834 /* identical entry existed, so we did nothing */
836 crc->res = GNUNET_NO;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name `%s' %u\n", crc->name, res);
846 static void handle_record_create (void *cls,
847 struct GNUNET_SERVER_Client * client,
848 const struct GNUNET_MessageHeader * message)
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
851 struct GNUNET_NAMESTORE_Client *nc;
852 struct GNUNET_NAMESTORE_CryptoContainer *cc;
853 struct CreateRecordContext crc;
854 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
855 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
856 struct RecordCreateResponseMessage rcr_msg;
857 struct GNUNET_CRYPTO_ShortHashCode pubkey_hash;
858 GNUNET_HashCode long_hash;
870 int res = GNUNET_SYSERR;
871 crc.res = GNUNET_SYSERR;
873 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
876 GNUNET_SERVER_receive_done (client, GNUNET_OK);
880 nc = client_lookup(client);
884 GNUNET_SERVER_receive_done (client, GNUNET_OK);
888 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
889 rid = ntohl (rp_msg->gns_header.r_id);
890 name_len = ntohs (rp_msg->name_len);
891 msg_size = ntohs (message->size);
892 rd_count = ntohs (rp_msg->rd_count);
893 rd_ser_len = ntohs (rp_msg->rd_len);
894 key_len = ntohs (rp_msg->pkey_len);
895 msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
897 if (msg_size != msg_size_exp)
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
901 GNUNET_SERVER_receive_done (client, GNUNET_OK);
905 if ((name_len == 0) || (name_len > 256))
908 GNUNET_SERVER_receive_done (client, GNUNET_OK);
912 pkey_tmp = (char *) &rp_msg[1];
913 name_tmp = &pkey_tmp[key_len];
914 rd_ser = &name_tmp[name_len];
916 if (name_tmp[name_len -1] != '\0')
919 GNUNET_SERVER_receive_done (client, GNUNET_OK);
923 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
925 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
926 if ((res != GNUNET_OK) || (rd_count != 1))
931 /* Extracting and converting private key */
932 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
933 GNUNET_assert (pkey != NULL);
934 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
935 GNUNET_CRYPTO_short_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
936 GNUNET_CRYPTO_short_hash_double (&pubkey_hash, &long_hash);
938 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(zonekeys, &long_hash))
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received new private key for zone `%s'\n",GNUNET_short_h2s(&pubkey_hash));
942 cc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
943 cc->privkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
944 cc->pubkey = GNUNET_malloc(sizeof (pub));
945 memcpy (cc->pubkey, &pub, sizeof(pub));
946 cc->zone = pubkey_hash;
947 GNUNET_CONTAINER_multihashmap_put(zonekeys, &long_hash, cc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
950 crc.expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
951 crc.res = GNUNET_SYSERR;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_short_h2s(&pubkey_hash));
959 /* Get existing records for name */
960 res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
961 if (res != GNUNET_SYSERR)
963 GNUNET_CRYPTO_rsa_key_free(pkey);
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
969 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
970 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
971 rcr_msg.gns_header.r_id = htonl (rid);
972 if ((GNUNET_OK == res) && (crc.res == GNUNET_YES))
973 rcr_msg.op_result = htonl (GNUNET_YES);
974 else if ((GNUNET_OK == res) && (crc.res == GNUNET_NO))
975 rcr_msg.op_result = htonl (GNUNET_NO);
977 rcr_msg.op_result = htonl (GNUNET_SYSERR);
978 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
980 GNUNET_SERVER_receive_done (client, GNUNET_OK);
984 struct RemoveRecordContext
986 struct GNUNET_NAMESTORE_RecordData *rd;
987 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
992 handle_record_remove_it (void *cls,
993 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
994 struct GNUNET_TIME_Absolute expire,
996 unsigned int rd_count,
997 const struct GNUNET_NAMESTORE_RecordData *rd,
998 const struct GNUNET_CRYPTO_RsaSignature *signature)
1000 struct RemoveRecordContext *rrc = cls;
1004 unsigned int rd_count_new;
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
1010 /* Could not find record to remove */
1015 /* Find record to remove */
1016 found = GNUNET_SYSERR;
1017 for (c = 0; c < rd_count; c++)
1019 if (rd[c].expiration.abs_value != rrc->rd->expiration.abs_value)
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SENT FLAGES: %u \n",rd[c].flags);
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STORED FLAGES: %u \n",rrc->rd->flags);
1025 if (rd[c].flags != rrc->rd->flags)
1028 if (rd[c].record_type != rrc->rd->record_type)
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SENT FLAGES: %u \n",rd[c].data_size);
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STORED FLAGES: %u \n",rrc->rd->data_size);
1034 if (rd[c].data_size != rrc->rd->data_size)
1037 if (0 != memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size))
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
1044 if (GNUNET_SYSERR == found)
1046 /* Could not find record to remove */
1051 rd_count_new = rd_count -1;
1052 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
1054 unsigned int c2 = 0;
1055 for (c = 0; c < rd_count; c++)
1059 GNUNET_assert (c2 < rd_count_new);
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
1067 /* Create dummy signature */
1068 struct GNUNET_CRYPTO_RsaSignature dummy_signature;
1069 memset (&dummy_signature, '\0', sizeof (dummy_signature));
1073 res = GSN_database->put_records(GSN_database->cls,
1077 rd_count_new, rd_new,
1079 if (GNUNET_OK != res)
1081 /* Could put records into database */
1089 static void handle_record_remove (void *cls,
1090 struct GNUNET_SERVER_Client * client,
1091 const struct GNUNET_MessageHeader * message)
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
1094 struct GNUNET_NAMESTORE_Client *nc;
1095 struct RecordRemoveResponseMessage rrr_msg;
1096 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
1097 struct GNUNET_NAMESTORE_CryptoContainer *cc = NULL;
1098 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1099 struct GNUNET_CRYPTO_ShortHashCode pubkey_hash;
1100 GNUNET_HashCode long_hash;
1101 char * pkey_tmp = NULL;
1102 char * name_tmp = NULL;
1103 char * rd_ser = NULL;
1105 size_t name_len = 0;
1106 size_t rd_ser_len = 0;
1107 size_t msg_size = 0;
1108 size_t msg_size_exp = 0;
1112 int res = GNUNET_SYSERR;
1114 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
1116 GNUNET_break_op (0);
1117 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1121 nc = client_lookup(client);
1124 GNUNET_break_op (0);
1125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1129 struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
1130 rid = ntohl (rr_msg->gns_header.r_id);
1131 name_len = ntohs (rr_msg->name_len);
1132 rd_ser_len = ntohs (rr_msg->rd_len);
1133 rd_count = ntohs (rr_msg->rd_count);
1134 key_len = ntohs (rr_msg->pkey_len);
1135 msg_size = ntohs (message->size);
1137 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1139 GNUNET_break_op (0);
1140 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1144 if ((name_len >=256) || (name_len == 0))
1146 GNUNET_break_op (0);
1147 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1151 msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1152 if (msg_size != msg_size_exp)
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
1155 GNUNET_break_op (0);
1156 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1160 pkey_tmp = (char *) &rr_msg[1];
1161 name_tmp = &pkey_tmp[key_len];
1162 rd_ser = &name_tmp[name_len];
1165 if ((name_len == 0) || (name_len > 256))
1167 GNUNET_break_op (0);
1168 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1172 if (name_tmp[name_len -1] != '\0')
1174 GNUNET_break_op (0);
1175 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1179 /* Extracting and converting private key */
1180 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
1181 GNUNET_assert (pkey != NULL);
1182 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
1183 GNUNET_CRYPTO_short_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
1184 GNUNET_CRYPTO_short_hash_double (&pubkey_hash, &long_hash);
1186 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(zonekeys, &long_hash))
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received new private key for zone `%s'\n",GNUNET_short_h2s(&pubkey_hash));
1189 cc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
1190 cc->privkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
1191 cc->pubkey = GNUNET_malloc(sizeof (pub));
1192 memcpy (cc->pubkey, &pub, sizeof(pub));
1193 cc->zone = pubkey_hash;
1195 GNUNET_CONTAINER_multihashmap_put(zonekeys, &long_hash, cc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1199 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
1200 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
1201 if ((res != GNUNET_OK) || (rd_count > 1))
1203 GNUNET_break_op (0);
1209 /* remove the whole name and all records */
1210 /* Database operation */
1211 res = GSN_database->remove_records (GSN_database->cls,
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing name `%s': %s\n",
1215 name_tmp, (GNUNET_OK == res) ? "OK" : "FAIL");
1217 if (GNUNET_OK != res)
1218 /* Could not remove entry from database */
1225 /* remove a single record */
1226 struct RemoveRecordContext rrc;
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_short_h2s(&pubkey_hash));
1232 /* Database operation */
1233 res = GSN_database->iterate_records (GSN_database->cls,
1237 handle_record_remove_it, &rrc);
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
1240 name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
1246 rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
1247 rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
1248 rrr_msg.gns_header.r_id = htonl (rid);
1249 rrr_msg.op_result = htonl (res);
1250 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
1252 GNUNET_CRYPTO_rsa_key_free (pkey);
1254 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1258 struct ZoneToNameCtx
1260 struct GNUNET_NAMESTORE_Client *nc;
1265 handle_zone_to_name_it (void *cls,
1266 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1267 struct GNUNET_TIME_Absolute expire,
1269 unsigned int rd_count,
1270 const struct GNUNET_NAMESTORE_RecordData *rd,
1271 const struct GNUNET_CRYPTO_RsaSignature *signature)
1273 struct ZoneToNameCtx * ztn_ctx = cls;
1274 struct ZoneToNameResponseMessage *ztnr_msg;
1275 int16_t res = GNUNET_SYSERR;
1276 uint16_t name_len = 0;
1277 uint16_t rd_ser_len = 0 ;
1278 int32_t contains_sig = 0;
1279 size_t msg_size = 0;
1281 char *rd_ser = NULL;
1286 if ((zone_key != NULL) && (name != NULL))
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found results: name is `%s', has %u records\n", name, rd_count);
1291 name_len = strlen (name) +1;
1295 /* no result found */
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found no results\n");
1303 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1304 rd_ser = GNUNET_malloc (rd_ser_len);
1305 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1310 if (signature != NULL)
1311 contains_sig = GNUNET_YES;
1313 contains_sig = GNUNET_NO;
1317 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len + contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
1318 ztnr_msg = GNUNET_malloc (msg_size);
1320 name_tmp = (char *) &ztnr_msg[1];
1321 rd_tmp = &name_tmp[name_len];
1322 sig_tmp = &rd_tmp[rd_ser_len];
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_TO_NAME_RESPONSE");
1325 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1326 ztnr_msg->gns_header.header.size = htons (msg_size);
1327 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1328 ztnr_msg->res = htons (res);
1329 ztnr_msg->rd_len = htons (rd_ser_len);
1330 ztnr_msg->rd_count = htons (rd_count);
1331 ztnr_msg->name_len = htons (name_len);
1332 ztnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
1333 if (zone_key != NULL)
1334 ztnr_msg->zone_key = *zone_key;
1336 memset (&ztnr_msg->zone_key, '\0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1338 if ((name_len > 0) && (name != NULL))
1339 memcpy (name_tmp, name, name_len);
1341 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);
1342 if ((rd_ser_len > 0) && (rd_ser != NULL))
1343 memcpy (rd_tmp, rd_ser, rd_ser_len);
1344 if ((GNUNET_YES == contains_sig) && (signature != NULL))
1345 memcpy (sig_tmp, signature, contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature));
1347 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client, (const struct GNUNET_MessageHeader *) ztnr_msg, GNUNET_NO);
1348 GNUNET_free (ztnr_msg);
1349 GNUNET_free_non_null (rd_ser);
1353 static void handle_zone_to_name (void *cls,
1354 struct GNUNET_SERVER_Client * client,
1355 const struct GNUNET_MessageHeader * message)
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_TO_NAME");
1358 struct GNUNET_NAMESTORE_Client *nc;
1359 struct ZoneToNameCtx ztn_ctx;
1360 size_t msg_size = 0;
1363 if (ntohs (message->size) != sizeof (struct ZoneToNameMessage))
1365 GNUNET_break_op (0);
1366 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1370 nc = client_lookup(client);
1373 GNUNET_break_op (0);
1374 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1378 struct ZoneToNameMessage *ztn_msg = (struct ZoneToNameMessage *) message;
1380 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1382 GNUNET_break_op (0);
1383 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1387 rid = ntohl (ztn_msg->gns_header.r_id);
1392 struct GNUNET_CRYPTO_ShortHashAsciiEncoded z_tmp;
1393 GNUNET_CRYPTO_short_hash_to_enc(&ztn_msg->zone, &z_tmp);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up name for zone `%s' in zone `%s'\n",
1396 GNUNET_short_h2s (&ztn_msg->value_zone));
1398 GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
1400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1403 struct ZoneIterationProcResult
1406 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
1409 struct GNUNET_CRYPTO_RsaSignature signature;
1410 struct GNUNET_TIME_Absolute expire;
1420 void zone_iteration_proc (void *cls,
1421 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1422 struct GNUNET_TIME_Absolute expire,
1424 unsigned int rd_count,
1425 const struct GNUNET_NAMESTORE_RecordData *rd,
1426 const struct GNUNET_CRYPTO_RsaSignature *signature)
1428 struct GNUNET_NAMESTORE_ZoneIteration *zi = cls;
1429 struct GNUNET_NAMESTORE_Client *nc = zi->client;
1430 struct GNUNET_NAMESTORE_CryptoContainer * cc;
1431 struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
1432 struct GNUNET_TIME_Absolute e;
1433 struct GNUNET_CRYPTO_ShortHashCode zone_key_hash;
1434 GNUNET_HashCode long_hash;
1435 int authoritative = GNUNET_NO;
1437 if ((zone_key == NULL) && (name == NULL))
1439 struct ZoneIterationResponseMessage zir_msg;
1440 if (zi->has_zone == GNUNET_YES)
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for zone `%s'\n", GNUNET_short_h2s(&zi->zone));
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for all zones\n");
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message\n", "ZONE_ITERATION_RESPONSE");
1446 zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1447 zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
1448 zir_msg.gns_header.r_id = htonl(zi->request_id);
1449 zir_msg.expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_zero());
1450 zir_msg.name_len = htons (0);
1451 zir_msg.reserved = htons (0);
1452 zir_msg.rd_count = htons (0);
1453 zir_msg.rd_len = htons (0);
1454 memset (&zir_msg.public_key, '\0', sizeof (zir_msg.public_key));
1455 memset (&zir_msg.signature, '\0', sizeof (zir_msg.signature));
1456 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing zone iterator\n");
1459 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1465 struct ZoneIterationResponseMessage *zir_msg;
1466 if (zi->has_zone == GNUNET_YES)
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over zone `%s'\n",
1468 name, GNUNET_short_h2s(&zi->zone));
1469 if (zi->has_zone == GNUNET_NO)
1470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over all zones\n",
1478 name_len = strlen (name) +1;
1480 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1481 char rd_ser[rd_ser_len];
1482 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1483 msg_size = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_ser_len;
1484 zir_msg = GNUNET_malloc(msg_size);
1486 name_tmp = (char *) &zir_msg[1];
1487 rd_tmp = &name_tmp[name_len];
1489 GNUNET_CRYPTO_short_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
1490 GNUNET_CRYPTO_short_hash_double(&zone_key_hash, &long_hash);
1491 if (GNUNET_CONTAINER_multihashmap_contains(zonekeys, &long_hash))
1493 cc = GNUNET_CONTAINER_multihashmap_get(zonekeys, &long_hash);
1494 e = get_block_expiration_time(rd_count, rd);
1496 signature_new = GNUNET_NAMESTORE_create_signature(cc->privkey, e, name, rd, rd_count);
1497 GNUNET_assert (signature_new != NULL);
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating signature for `%s' in zone `%s' with %u records and expiration %llu\n", name, GNUNET_short_h2s(&zone_key_hash), rd_count, e.abs_value);
1499 authoritative = GNUNET_YES;
1503 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1504 zir_msg->gns_header.header.size = htons (msg_size);
1505 zir_msg->gns_header.r_id = htonl(zi->request_id);
1506 zir_msg->expire = GNUNET_TIME_absolute_hton(expire);
1507 zir_msg->reserved = htons (0);
1508 zir_msg->name_len = htons (name_len);
1509 zir_msg->rd_count = htons (rd_count);
1510 zir_msg->rd_len = htons (rd_ser_len);
1511 if ((GNUNET_YES == authoritative) && (NULL != signature_new))
1513 zir_msg->signature = *signature_new;
1514 GNUNET_free (signature_new);
1517 zir_msg->signature = *signature;
1518 GNUNET_assert (NULL != zone_key);
1519 if (zone_key != NULL)
1520 zir_msg->public_key = *zone_key;
1521 memcpy (name_tmp, name, name_len);
1522 memcpy (rd_tmp, rd_ser, rd_ser_len);
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with size %u\n", "ZONE_ITERATION_RESPONSE", msg_size);
1525 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) zir_msg, GNUNET_NO);
1526 GNUNET_free (zir_msg);
1530 static void handle_iteration_start (void *cls,
1531 struct GNUNET_SERVER_Client * client,
1532 const struct GNUNET_MessageHeader * message)
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1536 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
1537 struct GNUNET_NAMESTORE_Client *nc;
1538 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1540 nc = client_lookup(client);
1543 GNUNET_break_op (0);
1544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1548 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
1549 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1552 zi->zone = zis_msg->zone;
1554 struct GNUNET_CRYPTO_ShortHashCode dummy;
1555 struct GNUNET_CRYPTO_ShortHashCode *zone_tmp;
1556 memset (&dummy, '\0', sizeof (dummy));
1557 if (0 == memcmp (&dummy, &zis_msg->zone, sizeof (dummy)))
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over all zones\n");
1560 zi->has_zone = GNUNET_NO;
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over zone `%s'\n", GNUNET_short_h2s (&zis_msg->zone));
1566 zi->has_zone = GNUNET_YES;
1567 zone_tmp = &zis_msg->zone;
1570 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1572 GSN_database->iterate_records (GSN_database->cls, zone_tmp , NULL, zi->offset , &zone_iteration_proc, zi);
1573 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1576 static void handle_iteration_stop (void *cls,
1577 struct GNUNET_SERVER_Client * client,
1578 const struct GNUNET_MessageHeader * message)
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1582 struct GNUNET_NAMESTORE_Client *nc;
1583 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1584 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1587 nc = client_lookup(client);
1590 GNUNET_break_op (0);
1591 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1595 rid = ntohl (zis_msg->gns_header.r_id);
1596 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1598 if (zi->request_id == rid)
1603 GNUNET_break_op (0);
1604 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1608 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1609 if (GNUNET_YES == zi->has_zone)
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_short_h2s (&zi->zone));
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration all zones\n");
1615 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1618 static void handle_iteration_next (void *cls,
1619 struct GNUNET_SERVER_Client * client,
1620 const struct GNUNET_MessageHeader * message)
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1624 struct GNUNET_NAMESTORE_Client *nc;
1625 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1626 struct GNUNET_CRYPTO_ShortHashCode *zone_tmp;
1627 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1630 nc = client_lookup(client);
1633 GNUNET_break_op (0);
1634 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1638 rid = ntohl (zis_msg->gns_header.r_id);
1639 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1641 if (zi->request_id == rid)
1646 GNUNET_break_op (0);
1647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1651 if (GNUNET_YES == zi->has_zone)
1652 zone_tmp = &zi->zone;
1657 GSN_database->iterate_records (GSN_database->cls, zone_tmp, NULL, zi->offset , &zone_iteration_proc, zi);
1658 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1661 int zonekey_file_it (void *cls, const char *filename)
1663 GNUNET_HashCode long_hash;
1665 if ((filename != NULL) && (NULL != strstr(filename, ".zkey")))
1667 struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
1668 struct GNUNET_NAMESTORE_CryptoContainer *c;
1669 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(filename);
1670 if (privkey == NULL)
1673 c = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
1674 c->pubkey = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1675 c->privkey = privkey;
1676 GNUNET_CRYPTO_rsa_key_get_public(privkey, c->pubkey);
1677 GNUNET_CRYPTO_short_hash(c->pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &c->zone);
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found zonefile for zone `%s'\n", GNUNET_short_h2s (&c->zone));
1680 GNUNET_CRYPTO_short_hash_double (&c->zone, &long_hash);
1681 GNUNET_CONTAINER_multihashmap_put(zonekeys, &long_hash, c, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1689 * Process template requests.
1691 * @param cls closure
1692 * @param server the initialized server
1693 * @param cfg configuration to use
1696 run (void *cls, struct GNUNET_SERVER_Handle *server,
1697 const struct GNUNET_CONFIGURATION_Handle *cfg)
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1703 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1704 {&handle_start, NULL,
1705 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1706 {&handle_lookup_name, NULL,
1707 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1708 {&handle_record_put, NULL,
1709 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1710 {&handle_record_create, NULL,
1711 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1712 {&handle_record_remove, NULL,
1713 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1714 {&handle_zone_to_name, NULL,
1715 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, 0},
1716 {&handle_iteration_start, NULL,
1717 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1718 {&handle_iteration_next, NULL,
1719 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1720 {&handle_iteration_stop, NULL,
1721 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, 0},
1727 /* Load private keys from disk */
1729 GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore", "zonefile_directory",
1730 &zonefile_directory))
1732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No directory to load zonefiles specified in configuration\n"));
1733 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1737 if (GNUNET_NO == GNUNET_DISK_file_test (zonefile_directory))
1739 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (zonefile_directory))
1741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Creating directory `%s' for zone files failed!\n"), zonefile_directory);
1742 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created directory `%s' for zone files\n", zonefile_directory);
1748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scanning directory `%s' for zone files\n", zonefile_directory);
1749 zonekeys = GNUNET_CONTAINER_multihashmap_create (10);
1750 GNUNET_DISK_directory_scan (zonefile_directory, zonekey_file_it, &counter);
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u zone files\n", counter);
1753 /* Loading database plugin */
1755 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1759 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1760 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1761 GNUNET_free (database);
1762 if (GSN_database == NULL)
1764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1766 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1770 /* Configuring server handles */
1771 GNUNET_SERVER_add_handlers (server, handlers);
1772 snc = GNUNET_SERVER_notification_context_create (server, 16);
1773 GNUNET_SERVER_disconnect_notify (server,
1774 &client_disconnect_notification,
1777 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1784 * The main function for the template service.
1786 * @param argc number of arguments from the command line
1787 * @param argv command line arguments
1788 * @return 0 ok, 1 on error
1791 main (int argc, char *const *argv)
1793 return (GNUNET_OK ==
1794 GNUNET_SERVICE_run (argc, argv, "namestore",
1795 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1798 /* end of gnunet-service-namestore.c */