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);
216 * Task run during shutdown.
222 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
225 struct GNUNET_NAMESTORE_ZoneIteration * no;
226 struct GNUNET_NAMESTORE_ZoneIteration * tmp;
227 struct GNUNET_NAMESTORE_Client * nc;
228 struct GNUNET_NAMESTORE_Client * next;
230 GNUNET_SERVER_notification_context_destroy (snc);
233 GNUNET_CONTAINER_multihashmap_iterate(zonekeys, &zone_to_disk_it, NULL);
235 for (nc = client_head; nc != NULL; nc = next)
238 for (no = nc->op_head; no != NULL; no = tmp)
240 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
244 GNUNET_SERVER_client_drop(nc->client);
245 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
249 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
250 GNUNET_free (db_lib_name);
251 GNUNET_free_non_null(zonefile_directory);
254 static struct GNUNET_NAMESTORE_Client *
255 client_lookup (struct GNUNET_SERVER_Client *client)
257 struct GNUNET_NAMESTORE_Client * nc;
259 GNUNET_assert (NULL != client);
261 for (nc = client_head; nc != NULL; nc = nc->next)
263 if (client == nc->client)
270 * Called whenever a client is disconnected. Frees our
271 * resources associated with that client.
274 * @param client identification of the client
277 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
279 struct GNUNET_NAMESTORE_ZoneIteration * no;
280 struct GNUNET_NAMESTORE_Client * nc;
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
286 nc = client_lookup (client);
288 if ((NULL == client) || (NULL == nc))
291 for (no = nc->op_head; no != NULL; no = no->next)
293 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
298 GNUNET_SERVER_client_drop(nc->client);
299 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
306 static void handle_start (void *cls,
307 struct GNUNET_SERVER_Client * client,
308 const struct GNUNET_MessageHeader * message)
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
312 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
314 GNUNET_SERVER_notification_context_add (snc, client);
315 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
316 GNUNET_SERVER_client_keep (client);
317 GNUNET_SERVER_receive_done (client, GNUNET_OK);
320 struct LookupNameContext
322 struct GNUNET_NAMESTORE_Client *nc;
324 uint32_t record_type;
325 GNUNET_HashCode *zone;
329 void drop_iterator (void *cls,
330 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
331 struct GNUNET_TIME_Absolute expire,
334 const struct GNUNET_NAMESTORE_RecordData *rd,
335 const struct GNUNET_CRYPTO_RsaSignature *signature)
337 GNUNET_HashCode zone_hash;
339 if (NULL != zone_key)
341 GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_hash);
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting zone `%s'\n", GNUNET_h2s (&zone_hash));
343 GSN_database->delete_zone (GSN_database->cls, &zone_hash);
347 (*stop) = GNUNET_YES;
353 handle_lookup_name_it (void *cls,
354 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
355 struct GNUNET_TIME_Absolute expire,
357 unsigned int rd_count,
358 const struct GNUNET_NAMESTORE_RecordData *rd,
359 const struct GNUNET_CRYPTO_RsaSignature *signature)
362 struct LookupNameContext *lnc = cls;
363 struct LookupNameResponseMessage *lnr_msg;
364 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
365 struct GNUNET_NAMESTORE_CryptoContainer *cc;
366 struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
367 GNUNET_HashCode zone_key_hash;
374 int copied_elements = 0;
375 int contains_signature = GNUNET_NO;
376 int authoritative = GNUNET_NO;
380 name_len = strlen(name) + 1;
382 /* count records to copy */
385 if (lnc->record_type != 0)
387 /* special record type needed */
388 for (c = 0; c < rd_count; c ++)
389 if (rd[c].record_type == lnc->record_type)
390 copied_elements++; /* found matching record */
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records with type %u for name `%s' in zone `%s'\n",
392 copied_elements, lnc->record_type, lnc->name, GNUNET_h2s(lnc->zone));
393 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
395 for (c = 0; c < rd_count; c ++)
397 if (rd[c].record_type == lnc->record_type)
399 /* found matching record */
400 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
407 copied_elements = rd_count;
408 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
416 expire = GNUNET_TIME_UNIT_ZERO_ABS;
419 rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
420 char rd_ser[rd_ser_len];
421 GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u records for name `%s' in zone `%s'\n",
424 copied_elements, lnc->name, GNUNET_h2s(lnc->zone));
426 if ((copied_elements == rd_count) && (NULL != signature))
427 contains_signature = GNUNET_YES;
429 contains_signature = GNUNET_NO;
432 if ((NULL != zone_key) && (copied_elements == rd_count))
434 GNUNET_CRYPTO_hash(zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
435 if (GNUNET_CONTAINER_multihashmap_contains(zonekeys, &zone_key_hash))
437 cc = GNUNET_CONTAINER_multihashmap_get(zonekeys, &zone_key_hash);
438 signature_new = GNUNET_NAMESTORE_create_signature(cc->privkey, name, rd, rd_count);
439 GNUNET_assert (signature_new != NULL);
440 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));
441 authoritative = GNUNET_YES;
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am not authoritative for name `%s' in zone `%s'\n",name, GNUNET_h2s(&zone_key_hash));
447 if (rd_selected != rd)
448 GNUNET_free (rd_selected);
450 r_size = sizeof (struct LookupNameResponseMessage) +
451 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
456 lnr_msg = GNUNET_malloc (r_size);
457 lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
458 lnr_msg->gns_header.header.size = ntohs (r_size);
459 lnr_msg->gns_header.r_id = htonl (lnc->request_id);
460 lnr_msg->rd_count = htons (copied_elements);
461 lnr_msg->rd_len = htons (rd_ser_len);
462 lnr_msg->name_len = htons (name_len);
463 lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
465 if (zone_key != NULL)
466 lnr_msg->public_key = (*zone_key);
468 memset(&lnr_msg->public_key, '\0', sizeof (lnr_msg->public_key));
470 if (GNUNET_YES == authoritative)
471 { /* use new created signature */
472 lnr_msg->contains_sig = htons (GNUNET_YES);
473 GNUNET_assert (signature_new != NULL);
474 lnr_msg->signature = *signature_new;
475 GNUNET_free (signature_new);
477 else if (GNUNET_YES == contains_signature)
479 /* use existing signature */
480 lnr_msg->contains_sig = htons (GNUNET_YES);
481 GNUNET_assert (signature != NULL);
482 lnr_msg->signature = *signature;
486 /* use no signature */
487 memset (&lnr_msg->signature, '\0', sizeof (lnr_msg->signature));
490 name_tmp = (char *) &lnr_msg[1];
491 rd_tmp = &name_tmp[name_len];
493 memcpy (name_tmp, name, name_len);
494 memcpy (rd_tmp, rd_ser, rd_ser_len);
496 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
497 GNUNET_free (lnr_msg);
500 static void handle_lookup_name (void *cls,
501 struct GNUNET_SERVER_Client * client,
502 const struct GNUNET_MessageHeader * message)
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
505 struct LookupNameContext lnc;
506 struct GNUNET_NAMESTORE_Client *nc;
512 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
515 GNUNET_SERVER_receive_done (client, GNUNET_OK);
519 nc = client_lookup(client);
523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
527 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
528 rid = ntohl (ln_msg->gns_header.r_id);
529 name_len = ntohl (ln_msg->name_len);
530 type = ntohl (ln_msg->record_type);
532 if ((name_len == 0) || (name_len > 256))
535 GNUNET_SERVER_receive_done (client, GNUNET_OK);
539 name = (char *) &ln_msg[1];
540 if (name[name_len -1] != '\0')
543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up all records for name `%s' in zone `%s'\n", name, GNUNET_h2s(&ln_msg->zone));
550 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));
552 /* do the actual lookup */
553 lnc.request_id = rid;
555 lnc.record_type = type;
557 lnc.zone = &ln_msg->zone;
558 GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
560 GNUNET_SERVER_receive_done (client, GNUNET_OK);
563 static void handle_record_put (void *cls,
564 struct GNUNET_SERVER_Client * client,
565 const struct GNUNET_MessageHeader * message)
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
568 struct GNUNET_NAMESTORE_Client *nc;
569 struct GNUNET_TIME_Absolute expire;
570 struct GNUNET_CRYPTO_RsaSignature *signature;
571 struct RecordPutResponseMessage rpr_msg;
580 int res = GNUNET_SYSERR;
582 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
589 nc = client_lookup (client);
593 GNUNET_SERVER_receive_done (client, GNUNET_OK);
597 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
599 rid = ntohl (rp_msg->gns_header.r_id);
600 msg_size = ntohs (rp_msg->gns_header.header.size);
601 name_len = ntohs (rp_msg->name_len);
602 rd_count = ntohs (rp_msg->rd_count);
603 rd_ser_len = ntohs(rp_msg->rd_len);
605 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
608 GNUNET_SERVER_receive_done (client, GNUNET_OK);
612 if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
615 GNUNET_SERVER_receive_done (client, GNUNET_OK);
619 msg_size_exp = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
620 if (msg_size != msg_size_exp)
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
624 GNUNET_SERVER_receive_done (client, GNUNET_OK);
627 if ((name_len == 0) || (name_len > 256))
630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
634 name = (char *) &rp_msg[1];
636 if (name[name_len -1] != '\0')
639 GNUNET_SERVER_receive_done (client, GNUNET_OK);
643 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
644 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
646 rd_ser = &name[name_len];
647 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
648 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
649 if (res != GNUNET_OK)
655 GNUNET_HashCode zone_hash;
656 GNUNET_CRYPTO_hash (&rp_msg->public_key, sizeof (rp_msg->public_key), &zone_hash);
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting %u record for name `%s' in zone `%s'\n", rd_count, name, GNUNET_h2s(&zone_hash));
660 /* Database operation */
661 res = GSN_database->put_records(GSN_database->cls,
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
669 name, (res == GNUNET_OK) ? "OK" : "FAIL");
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
674 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
675 rpr_msg.gns_header.header.size = htons (sizeof (struct RecordPutResponseMessage));
676 rpr_msg.gns_header.r_id = htonl (rid);
677 rpr_msg.op_result = htonl (res);
678 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
680 GNUNET_SERVER_receive_done (client, GNUNET_OK);
683 struct CreateRecordContext
685 struct GNUNET_NAMESTORE_RecordData *rd;
686 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
687 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey;
688 struct GNUNET_TIME_Absolute expire;
695 handle_create_record_it (void *cls,
696 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pubkey,
697 struct GNUNET_TIME_Absolute expire,
699 unsigned int rd_count,
700 const struct GNUNET_NAMESTORE_RecordData *rd,
701 const struct GNUNET_CRYPTO_RsaSignature *signature)
703 struct CreateRecordContext * crc = cls;
704 struct GNUNET_NAMESTORE_RecordData *rd_new = NULL;
705 struct GNUNET_CRYPTO_RsaSignature dummy_signature;
706 struct GNUNET_TIME_Absolute block_expiration;
708 int exist = GNUNET_SYSERR;
709 int update = GNUNET_NO;
711 int rd_count_new = 0;
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, crc->name);
715 for (c = 0; c < rd_count; c++)
718 if ((crc->rd->record_type == rd[c].record_type) &&
719 (crc->rd->data_size == rd[c].data_size) &&
720 (0 == memcmp (crc->rd->data, rd[c].data, rd[c].data_size)))
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing records for `%s' to update expiration date!\n", crc->name);
724 if (crc->rd->expiration.abs_value != rd[c].expiration.abs_value)
730 if (exist == GNUNET_SYSERR)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New record does not exist for name `%s'!\n", crc->name);
733 if (exist == GNUNET_SYSERR)
735 rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
736 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
737 rd_count_new = rd_count + 1;
738 rd_new[rd_count] = *(crc->rd);
740 else if (update == GNUNET_NO)
742 /* Exact same record already exists */
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No update for %s' record required!\n", crc->name);
747 else if (update == GNUNET_YES)
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating existing records for `%s'!\n", crc->name);
751 rd_new = GNUNET_malloc ((rd_count) * sizeof (struct GNUNET_NAMESTORE_RecordData));
752 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
753 rd_count_new = rd_count;
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating expiration from %llu to %llu!\n", rd_new[exist].expiration.abs_value, crc->rd->expiration.abs_value);
755 rd_new[exist].expiration = crc->rd->expiration;
758 block_expiration = GNUNET_TIME_absolute_max(crc->expire, expire);
759 if (block_expiration.abs_value != expire.abs_value)
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updated block expiration time\n");
762 memset (&dummy_signature, '\0', sizeof (dummy_signature));
764 /* Database operation */
765 GNUNET_assert ((rd_new != NULL) && (rd_count_new > 0));
766 res = GSN_database->put_records(GSN_database->cls,
767 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
770 rd_count_new, rd_new,
772 GNUNET_break (GNUNET_OK == res);
773 if (res == GNUNET_OK)
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully put record for `%s' in database \n", crc->name);
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to put record for `%s' in database \n", crc->name);
780 GNUNET_free_non_null (rd_new);
784 /* failed to create the record */
785 crc->res = GNUNET_SYSERR;
788 /* database operations OK */
789 if (GNUNET_YES == update)
790 /* we updated an existing record */
791 crc->res = GNUNET_NO;
793 /* we created a new record */
794 crc->res = GNUNET_YES;
797 /* identical entry existed, so we did nothing */
798 crc->res = GNUNET_NO;
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name `%s' %u\n", crc->name, res);
808 static void handle_record_create (void *cls,
809 struct GNUNET_SERVER_Client * client,
810 const struct GNUNET_MessageHeader * message)
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
813 struct GNUNET_NAMESTORE_Client *nc;
814 struct CreateRecordContext crc;
815 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
816 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
817 struct RecordCreateResponseMessage rcr_msg;
818 GNUNET_HashCode pubkey_hash;
830 int res = GNUNET_SYSERR;
831 crc.res = GNUNET_SYSERR;
833 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
836 GNUNET_SERVER_receive_done (client, GNUNET_OK);
840 nc = client_lookup(client);
844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
848 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
849 rid = ntohl (rp_msg->gns_header.r_id);
850 name_len = ntohs (rp_msg->name_len);
851 msg_size = ntohs (message->size);
852 rd_count = ntohs (rp_msg->rd_count);
853 rd_ser_len = ntohs (rp_msg->rd_len);
854 key_len = ntohs (rp_msg->pkey_len);
855 msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
857 if (msg_size != msg_size_exp)
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
861 GNUNET_SERVER_receive_done (client, GNUNET_OK);
865 if ((name_len == 0) || (name_len > 256))
868 GNUNET_SERVER_receive_done (client, GNUNET_OK);
872 pkey_tmp = (char *) &rp_msg[1];
873 name_tmp = &pkey_tmp[key_len];
874 rd_ser = &name_tmp[name_len];
876 if (name_tmp[name_len -1] != '\0')
879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
883 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
885 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
886 if ((res != GNUNET_OK) || (rd_count != 1))
892 /* Extracting and converting private key */
893 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
894 GNUNET_assert (pkey != NULL);
895 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
896 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
898 crc.expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
899 crc.res = GNUNET_SYSERR;
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
907 /* Get existing records for name */
908 res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
909 if (res != GNUNET_SYSERR)
911 GNUNET_CRYPTO_rsa_key_free(pkey);
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
916 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
917 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
918 rcr_msg.gns_header.r_id = htonl (rid);
919 if ((GNUNET_OK == res) && (crc.res == GNUNET_YES))
920 rcr_msg.op_result = htonl (GNUNET_YES);
921 else if ((GNUNET_OK == res) && (crc.res == GNUNET_NO))
922 rcr_msg.op_result = htonl (GNUNET_NO);
924 rcr_msg.op_result = htonl (GNUNET_SYSERR);
925 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
927 GNUNET_SERVER_receive_done (client, GNUNET_OK);
931 struct RemoveRecordContext
933 struct GNUNET_NAMESTORE_RecordData *rd;
934 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
939 handle_record_remove_it (void *cls,
940 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
941 struct GNUNET_TIME_Absolute expire,
943 unsigned int rd_count,
944 const struct GNUNET_NAMESTORE_RecordData *rd,
945 const struct GNUNET_CRYPTO_RsaSignature *signature)
947 struct RemoveRecordContext *rrc = cls;
951 unsigned int rd_count_new;
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
957 /* Could not find record to remove */
962 /* Find record to remove */
963 found = GNUNET_SYSERR;
964 for (c = 0; c < rd_count; c++)
966 if ((rd[c].expiration.abs_value == rrc->rd->expiration.abs_value) &&
967 (rd[c].flags == rrc->rd->flags) &&
968 (rd[c].record_type == rrc->rd->record_type) &&
969 (rd[c].data_size == rrc->rd->data_size) &&
970 (0 == memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size)))
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
977 if (GNUNET_SYSERR == found)
979 /* Could not find record to remove */
984 rd_count_new = rd_count -1;
985 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
988 for (c = 0; c < rd_count; c++)
992 GNUNET_assert (c2 < rd_count_new);
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
1000 /* Create dummy signature */
1001 struct GNUNET_CRYPTO_RsaSignature dummy_signature;
1002 memset (&dummy_signature, '\0', sizeof (dummy_signature));
1006 res = GSN_database->put_records(GSN_database->cls,
1010 rd_count_new, rd_new,
1012 if (GNUNET_OK != res)
1014 /* Could put records into database */
1022 static void handle_record_remove (void *cls,
1023 struct GNUNET_SERVER_Client * client,
1024 const struct GNUNET_MessageHeader * message)
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
1027 struct GNUNET_NAMESTORE_Client *nc;
1028 struct RecordRemoveResponseMessage rrr_msg;
1029 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
1030 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1031 GNUNET_HashCode pubkey_hash;
1032 char * pkey_tmp = NULL;
1033 char * name_tmp = NULL;
1034 char * rd_ser = NULL;
1036 size_t name_len = 0;
1037 size_t rd_ser_len = 0;
1038 size_t msg_size = 0;
1039 size_t msg_size_exp = 0;
1043 int res = GNUNET_SYSERR;
1045 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
1047 GNUNET_break_op (0);
1048 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1052 nc = client_lookup(client);
1055 GNUNET_break_op (0);
1056 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1060 struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
1061 rid = ntohl (rr_msg->gns_header.r_id);
1062 name_len = ntohs (rr_msg->name_len);
1063 rd_ser_len = ntohs (rr_msg->rd_len);
1064 rd_count = ntohs (rr_msg->rd_count);
1065 key_len = ntohs (rr_msg->pkey_len);
1066 msg_size = ntohs (message->size);
1068 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1070 GNUNET_break_op (0);
1071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1075 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
1077 GNUNET_break_op (0);
1078 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1082 msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1083 if (msg_size != msg_size_exp)
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
1086 GNUNET_break_op (0);
1087 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1091 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
1093 GNUNET_break_op (0);
1094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1098 pkey_tmp = (char *) &rr_msg[1];
1099 name_tmp = &pkey_tmp[key_len];
1100 rd_ser = &name_tmp[name_len];
1103 if ((name_len == 0) || (name_len > 256))
1105 GNUNET_break_op (0);
1106 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1110 if (name_tmp[name_len -1] != '\0')
1112 GNUNET_break_op (0);
1113 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1117 /* Extracting and converting private key */
1118 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
1119 GNUNET_assert (pkey != NULL);
1120 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
1121 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
1123 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
1124 res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
1125 if ((res != GNUNET_OK) || (rd_count != 1))
1127 GNUNET_break_op (0);
1131 struct RemoveRecordContext rrc;
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
1137 /* Database operation */
1138 res = GSN_database->iterate_records (GSN_database->cls,
1142 handle_record_remove_it, &rrc);
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
1145 name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
1151 rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
1152 rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
1153 rrr_msg.gns_header.r_id = htonl (rid);
1154 rrr_msg.op_result = htonl (res);
1155 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
1157 GNUNET_CRYPTO_rsa_key_free (pkey);
1159 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1163 struct ZoneToNameCtx
1165 struct GNUNET_NAMESTORE_Client *nc;
1170 handle_zone_to_name_it (void *cls,
1171 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1172 struct GNUNET_TIME_Absolute expire,
1174 unsigned int rd_count,
1175 const struct GNUNET_NAMESTORE_RecordData *rd,
1176 const struct GNUNET_CRYPTO_RsaSignature *signature)
1178 struct ZoneToNameCtx * ztn_ctx = cls;
1179 struct ZoneToNameResponseMessage *ztnr_msg;
1180 int16_t res = GNUNET_SYSERR;
1181 uint16_t name_len = 0;
1182 uint16_t rd_ser_len = 0 ;
1183 int32_t contains_sig = 0;
1184 size_t msg_size = 0;
1186 char *rd_ser = NULL;
1191 if ((zone_key != NULL) && (name != NULL))
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found results: name is `%s', has %u records\n", name, rd_count);
1196 name_len = strlen (name);
1200 /* no result found */
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found no results\n");
1208 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1209 rd_ser = GNUNET_malloc (rd_ser_len);
1210 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1215 if (signature != NULL)
1216 contains_sig = GNUNET_YES;
1218 contains_sig = GNUNET_NO;
1222 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len + contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
1223 ztnr_msg = GNUNET_malloc (msg_size);
1225 name_tmp = (char *) &ztnr_msg[1];
1226 rd_tmp = &name_tmp[name_len];
1227 sig_tmp = &rd_tmp[rd_ser_len];
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_TO_NAME_RESPONSE");
1230 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1231 ztnr_msg->gns_header.header.size = htons (msg_size);
1232 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1233 ztnr_msg->res = htons (res);
1234 ztnr_msg->rd_len = htons (rd_ser_len);
1235 ztnr_msg->rd_count = htons (rd_count);
1236 ztnr_msg->name_len = htons (name_len);
1237 ztnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
1238 if (zone_key != NULL)
1239 ztnr_msg->zone_key = *zone_key;
1241 memset (&ztnr_msg->zone_key, '\0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1243 if ((name_len > 0) && (name != NULL))
1244 memcpy (name_tmp, name, name_len);
1246 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);
1247 if ((rd_ser_len > 0) && (rd_ser != NULL))
1248 memcpy (rd_tmp, rd_ser, rd_ser_len);
1249 if ((GNUNET_YES == contains_sig) && (signature != NULL))
1250 memcpy (sig_tmp, signature, contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature));
1252 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client, (const struct GNUNET_MessageHeader *) ztnr_msg, GNUNET_NO);
1253 GNUNET_free (ztnr_msg);
1254 GNUNET_free_non_null (rd_ser);
1258 static void handle_zone_to_name (void *cls,
1259 struct GNUNET_SERVER_Client * client,
1260 const struct GNUNET_MessageHeader * message)
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_TO_NAME");
1263 struct GNUNET_NAMESTORE_Client *nc;
1264 struct ZoneToNameCtx ztn_ctx;
1265 size_t msg_size = 0;
1268 if (ntohs (message->size) != sizeof (struct ZoneToNameMessage))
1270 GNUNET_break_op (0);
1271 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1275 nc = client_lookup(client);
1278 GNUNET_break_op (0);
1279 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1283 struct ZoneToNameMessage *ztn_msg = (struct ZoneToNameMessage *) message;
1285 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1287 GNUNET_break_op (0);
1288 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1292 rid = ntohl (ztn_msg->gns_header.r_id);
1297 char * z_tmp = strdup (GNUNET_h2s (&ztn_msg->zone));
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up name for zone `%s' in zone `%s'\n",
1300 GNUNET_h2s (&ztn_msg->value_zone));
1301 GNUNET_free (z_tmp);
1303 GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
1305 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1308 struct ZoneIterationProcResult
1311 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
1314 struct GNUNET_CRYPTO_RsaSignature signature;
1315 struct GNUNET_TIME_Absolute expire;
1325 void zone_iteration_proc (void *cls,
1326 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1327 struct GNUNET_TIME_Absolute expire,
1329 unsigned int rd_count,
1330 const struct GNUNET_NAMESTORE_RecordData *rd,
1331 const struct GNUNET_CRYPTO_RsaSignature *signature)
1333 struct GNUNET_NAMESTORE_ZoneIteration *zi = cls;
1334 struct GNUNET_NAMESTORE_Client *nc = zi->client;
1337 if ((zone_key == NULL) && (name == NULL))
1339 struct ZoneIterationResponseMessage zir_msg;
1340 if (zi->has_zone == GNUNET_YES)
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for zone `%s'\n", GNUNET_h2s(&zi->zone));
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for all zones\n");
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message\n", "ZONE_ITERATION_RESPONSE");
1346 zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1347 zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
1348 zir_msg.gns_header.r_id = htonl(zi->request_id);
1349 zir_msg.expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_zero());
1350 zir_msg.name_len = htons (0);
1351 zir_msg.reserved = htons (0);
1352 zir_msg.rd_count = htons (0);
1353 zir_msg.rd_len = htons (0);
1354 memset (&zir_msg.public_key, '\0', sizeof (zir_msg.public_key));
1355 memset (&zir_msg.signature, '\0', sizeof (zir_msg.signature));
1356 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing zone iterator\n");
1359 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1365 struct ZoneIterationResponseMessage *zir_msg;
1366 if (zi->has_zone == GNUNET_YES)
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over zone `%s'\n",
1368 name, GNUNET_h2s(&zi->zone));
1369 if (zi->has_zone == GNUNET_NO)
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over all zones\n",
1378 name_len = strlen (name) +1;
1380 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1381 char rd_ser[rd_ser_len];
1382 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1383 msg_size = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_ser_len;
1384 zir_msg = GNUNET_malloc(msg_size);
1386 name_tmp = (char *) &zir_msg[1];
1387 rd_tmp = &name_tmp[name_len];
1389 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
1390 zir_msg->gns_header.header.size = htons (msg_size);
1391 zir_msg->gns_header.r_id = htonl(zi->request_id);
1392 zir_msg->expire = GNUNET_TIME_absolute_hton(expire);
1393 zir_msg->reserved = htons (0);
1394 zir_msg->name_len = htons (name_len);
1395 zir_msg->rd_count = htons (rd_count);
1396 zir_msg->rd_len = htons (rd_ser_len);
1397 zir_msg->signature = *signature;
1398 GNUNET_assert (NULL != zone_key);
1399 if (zone_key != NULL)
1400 zir_msg->public_key = *zone_key;
1401 memcpy (name_tmp, name, name_len);
1402 memcpy (rd_tmp, rd_ser, rd_ser_len);
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message with size %u\n", "ZONE_ITERATION_RESPONSE", msg_size);
1405 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) zir_msg, GNUNET_NO);
1406 GNUNET_free (zir_msg);
1410 static void handle_iteration_start (void *cls,
1411 struct GNUNET_SERVER_Client * client,
1412 const struct GNUNET_MessageHeader * message)
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1416 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
1417 struct GNUNET_NAMESTORE_Client *nc;
1418 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1421 nc = client_lookup(client);
1424 GNUNET_break_op (0);
1425 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1429 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
1430 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1433 zi->zone = zis_msg->zone;
1435 GNUNET_HashCode dummy;
1436 GNUNET_HashCode *zone_tmp;
1437 memset (&dummy, '\0', sizeof (dummy));
1438 if (0 == memcmp (&dummy, &zis_msg->zone, sizeof (dummy)))
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over all zones\n");
1441 zi->has_zone = GNUNET_NO;
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over zone `%s'\n", GNUNET_h2s (&zis_msg->zone));
1447 zi->has_zone = GNUNET_YES;
1448 zone_tmp = &zis_msg->zone;
1451 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1453 res = GSN_database->iterate_records (GSN_database->cls, zone_tmp , NULL, zi->offset , &zone_iteration_proc, zi);
1454 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1457 static void handle_iteration_stop (void *cls,
1458 struct GNUNET_SERVER_Client * client,
1459 const struct GNUNET_MessageHeader * message)
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1463 struct GNUNET_NAMESTORE_Client *nc;
1464 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1465 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1468 nc = client_lookup(client);
1471 GNUNET_break_op (0);
1472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1476 rid = ntohl (zis_msg->gns_header.r_id);
1477 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1479 if (zi->request_id == rid)
1484 GNUNET_break_op (0);
1485 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1489 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1490 if (GNUNET_YES == zi->has_zone)
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration all zones\n");
1496 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1499 static void handle_iteration_next (void *cls,
1500 struct GNUNET_SERVER_Client * client,
1501 const struct GNUNET_MessageHeader * message)
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1505 struct GNUNET_NAMESTORE_Client *nc;
1506 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1507 GNUNET_HashCode *zone_tmp;
1508 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1511 nc = client_lookup(client);
1514 GNUNET_break_op (0);
1515 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1519 rid = ntohl (zis_msg->gns_header.r_id);
1520 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1522 if (zi->request_id == rid)
1527 GNUNET_break_op (0);
1528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1532 if (GNUNET_YES == zi->has_zone)
1533 zone_tmp = &zi->zone;
1538 GSN_database->iterate_records (GSN_database->cls, zone_tmp, NULL, zi->offset , &zone_iteration_proc, zi);
1539 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1542 int zonekey_file_it (void *cls, const char *filename)
1545 if ((filename != NULL) && (NULL != strstr(filename, ".zone")))
1547 struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
1548 struct GNUNET_NAMESTORE_CryptoContainer *c;
1549 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(filename);
1550 if (privkey == NULL)
1553 c = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_CryptoContainer));
1554 c->pubkey = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1555 c->privkey = privkey;
1556 GNUNET_CRYPTO_rsa_key_get_public(privkey, c->pubkey);
1557 GNUNET_CRYPTO_hash(c->pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &c->zone);
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found zonefile for zone `%s'\n", GNUNET_h2s (&c->zone));
1561 GNUNET_CONTAINER_multihashmap_put(zonekeys, &c->zone, c, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1569 * Process template requests.
1571 * @param cls closure
1572 * @param server the initialized server
1573 * @param cfg configuration to use
1576 run (void *cls, struct GNUNET_SERVER_Handle *server,
1577 const struct GNUNET_CONFIGURATION_Handle *cfg)
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1583 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1584 {&handle_start, NULL,
1585 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1586 {&handle_lookup_name, NULL,
1587 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1588 {&handle_record_put, NULL,
1589 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1590 {&handle_record_create, NULL,
1591 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1592 {&handle_record_remove, NULL,
1593 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1594 {&handle_zone_to_name, NULL,
1595 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, 0},
1596 {&handle_iteration_start, NULL,
1597 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1598 {&handle_iteration_next, NULL,
1599 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1600 {&handle_iteration_stop, NULL,
1601 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, 0},
1607 /* Load private keys from disk */
1609 GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore", "zonefile_directory",
1610 &zonefile_directory))
1612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No directory to load zonefiles specified in configuration\n"));
1613 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1617 if (GNUNET_NO == GNUNET_DISK_file_test (zonefile_directory))
1619 if (GNUNET_SYSERR == GNUNET_DISK_directory_create (zonefile_directory))
1621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Creating directory `%s' for zone files failed!\n"), zonefile_directory);
1622 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created directory `%s' for zone files\n", zonefile_directory);
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scanning directory `%s' for zone files\n", zonefile_directory);
1629 zonekeys = GNUNET_CONTAINER_multihashmap_create (10);
1630 GNUNET_DISK_directory_scan (zonefile_directory, zonekey_file_it, &counter);
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u zone files\n", counter);
1633 /* Loading database plugin */
1635 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1637 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1639 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1640 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1641 GNUNET_free (database);
1642 if (GSN_database == NULL)
1644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1646 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1650 /* Configuring server handles */
1651 GNUNET_SERVER_add_handlers (server, handlers);
1652 snc = GNUNET_SERVER_notification_context_create (server, 16);
1653 GNUNET_SERVER_disconnect_notify (server,
1654 &client_disconnect_notification,
1657 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1664 * The main function for the template service.
1666 * @param argc number of arguments from the command line
1667 * @param argv command line arguments
1668 * @return 0 ok, 1 on error
1671 main (int argc, char *const *argv)
1673 return (GNUNET_OK ==
1674 GNUNET_SERVICE_run (argc, argv, "namestore",
1675 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1678 /* end of gnunet-service-namestore.c */