2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file namestore/gnunet-service-namestore.c
23 * @brief namestore for the GNUnet naming system
24 * @author Matthias Wachs
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_namestore_plugin.h"
31 #include "gnunet_signatures.h"
32 #include "namestore.h"
37 * A namestore operation.
39 struct GNUNET_NAMESTORE_ZoneIteration
41 struct GNUNET_NAMESTORE_ZoneIteration *next;
42 struct GNUNET_NAMESTORE_ZoneIteration *prev;
44 struct GNUNET_NAMESTORE_Client * client;
57 struct GNUNET_NAMESTORE_Client
59 struct GNUNET_NAMESTORE_Client *next;
60 struct GNUNET_NAMESTORE_Client *prev;
62 struct GNUNET_SERVER_Client * client;
64 struct GNUNET_NAMESTORE_ZoneIteration *op_head;
65 struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
71 * Configuration handle.
73 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
75 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
78 * Our notification context.
80 static struct GNUNET_SERVER_NotificationContext *snc;
82 static char *db_lib_name;
84 static struct GNUNET_NAMESTORE_Client *client_head;
85 static struct GNUNET_NAMESTORE_Client *client_tail;
89 * Task run during shutdown.
95 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
99 struct GNUNET_NAMESTORE_ZoneIteration * no;
100 struct GNUNET_NAMESTORE_ZoneIteration * tmp;
101 struct GNUNET_NAMESTORE_Client * nc;
102 struct GNUNET_NAMESTORE_Client * next;
104 GNUNET_SERVER_notification_context_destroy (snc);
107 for (nc = client_head; nc != NULL; nc = next)
110 for (no = nc->op_head; no != NULL; no = tmp)
113 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
117 GNUNET_SERVER_client_drop(nc->client);
118 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
122 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
123 GNUNET_free (db_lib_name);
126 static struct GNUNET_NAMESTORE_Client *
127 client_lookup (struct GNUNET_SERVER_Client *client)
129 struct GNUNET_NAMESTORE_Client * nc;
131 GNUNET_assert (NULL != client);
133 for (nc = client_head; nc != NULL; nc = nc->next)
135 if (client == nc->client)
143 * Called whenever a client is disconnected. Frees our
144 * resources associated with that client.
147 * @param client identification of the client
150 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
152 struct GNUNET_NAMESTORE_ZoneIteration * no;
153 struct GNUNET_NAMESTORE_Client * nc;
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
159 nc = client_lookup (client);
161 if ((NULL == client) || (NULL == nc))
164 for (no = nc->op_head; no != NULL; no = no->next)
166 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
170 GNUNET_SERVER_client_drop(nc->client);
171 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
175 static void handle_start (void *cls,
176 struct GNUNET_SERVER_Client * client,
177 const struct GNUNET_MessageHeader * message)
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
181 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
183 GNUNET_SERVER_notification_context_add (snc, client);
184 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
185 GNUNET_SERVER_client_keep (client);
186 GNUNET_SERVER_receive_done (client, GNUNET_OK);
189 struct LookupNameContext
191 struct GNUNET_NAMESTORE_Client *nc;
193 uint32_t record_type;
200 handle_lookup_name_it (void *cls,
201 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
202 struct GNUNET_TIME_Absolute expire,
204 unsigned int rd_count,
205 const struct GNUNET_NAMESTORE_RecordData *rd,
206 const struct GNUNET_CRYPTO_RsaSignature *signature)
209 struct LookupNameContext *lnc = cls;
210 struct LookupNameResponseMessage *lnr_msg;
212 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
213 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
217 struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
225 name_len = strlen(name) + 1;
227 int copied_elements = 0;
228 int contains_signature = 0;
231 /* count records to copy */
234 if (lnc->record_type != 0)
236 /* special record type needed */
237 for (c = 0; c < rd_count; c ++)
238 if (rd[c].record_type == lnc->record_type)
239 copied_elements++; /* found matching record */
240 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
242 for (c = 0; c < rd_count; c ++)
244 if (rd[c].record_type == lnc->record_type)
246 /* found matching record */
247 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
254 copied_elements = rd_count;
255 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
263 expire = GNUNET_TIME_UNIT_ZERO_ABS;
268 rd_ser_len = GNUNET_NAMESTORE_records_get_size(copied_elements, rd_selected);
269 char rd_ser[rd_ser_len];
270 GNUNET_NAMESTORE_records_serialize(copied_elements, rd_selected, rd_ser_len, rd_ser);
273 if ((copied_elements == rd_count) && (signature != NULL))
274 contains_signature = GNUNET_YES;
276 if (rd_selected != rd)
277 GNUNET_free (rd_selected);
279 r_size = sizeof (struct LookupNameResponseMessage) +
280 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
283 contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
285 lnr_msg = GNUNET_malloc (r_size);
287 lnr_msg->gns_header.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
288 lnr_msg->gns_header.header.size = ntohs (r_size);
289 lnr_msg->gns_header.r_id = htonl (lnc->request_id);
290 lnr_msg->rd_count = htons (rd_count);
291 lnr_msg->rd_len = htons (rd_ser_len);
292 lnr_msg->name_len = htons (name_len);
293 lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
294 lnr_msg->contains_sig = htons (contains_signature);
296 zone_key_tmp = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
297 name_tmp = (char *) &zone_key_tmp[1];
298 rd_tmp = &name_tmp[name_len];
299 signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_ser_len];
301 if (zone_key != NULL)
302 memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
305 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
306 memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
307 memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
309 memcpy (name_tmp, name, name_len);
310 memcpy (rd_tmp, rd_ser, rd_ser_len);
312 if (GNUNET_YES == contains_signature)
313 memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
314 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
316 GNUNET_free (lnr_msg);
319 static void handle_lookup_name (void *cls,
320 struct GNUNET_SERVER_Client * client,
321 const struct GNUNET_MessageHeader * message)
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
324 struct LookupNameContext lnc;
325 struct GNUNET_NAMESTORE_Client *nc;
332 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
335 GNUNET_SERVER_receive_done (client, GNUNET_OK);
339 nc = client_lookup(client);
343 GNUNET_SERVER_receive_done (client, GNUNET_OK);
347 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
348 rid = ntohl (ln_msg->gns_header.r_id);
349 name_len = ntohl (ln_msg->name_len);
350 type = ntohl (ln_msg->record_type);
352 if ((name_len == 0) || (name_len > 256))
355 GNUNET_SERVER_receive_done (client, GNUNET_OK);
359 name = (char *) &ln_msg[1];
360 if (name[name_len -1] != '\0')
363 GNUNET_SERVER_receive_done (client, GNUNET_OK);
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s' in zone `%s'\n", name, GNUNET_h2s(&ln_msg->zone));
369 /* do the actual lookup */
370 lnc.request_id = rid;
372 lnc.record_type = type;
373 res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
375 GNUNET_SERVER_receive_done (client, GNUNET_OK);
378 static void handle_record_put (void *cls,
379 struct GNUNET_SERVER_Client * client,
380 const struct GNUNET_MessageHeader * message)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
383 struct GNUNET_NAMESTORE_Client *nc;
384 struct GNUNET_TIME_Absolute expire;
385 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
386 struct GNUNET_CRYPTO_RsaSignature *signature;
387 struct RecordPutResponseMessage rpr_msg;
397 int res = GNUNET_SYSERR;
399 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
402 GNUNET_SERVER_receive_done (client, GNUNET_OK);
406 nc = client_lookup (client);
410 GNUNET_SERVER_receive_done (client, GNUNET_OK);
414 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
416 rid = ntohl (rp_msg->gns_header.r_id);
417 msg_size = ntohs (rp_msg->gns_header.header.size);
418 key_len = ntohs (rp_msg->key_len);
419 name_len = ntohs (rp_msg->name_len);
420 rd_count = ntohs (rp_msg->rd_count);
421 rd_ser_len = ntohs(rp_msg->rd_len);
423 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
426 GNUNET_SERVER_receive_done (client, GNUNET_OK);
430 if ((rd_count < 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
437 msg_size_exp = sizeof (struct RecordPutMessage) + key_len + name_len + rd_ser_len;
438 if (msg_size != msg_size_exp)
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
442 GNUNET_SERVER_receive_done (client, GNUNET_OK);
445 if ((name_len == 0) || (name_len > 256))
448 GNUNET_SERVER_receive_done (client, GNUNET_OK);
452 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
453 name = &((char *)zone_key)[key_len];
455 if (name[name_len -1] != '\0')
458 GNUNET_SERVER_receive_done (client, GNUNET_OK);
462 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
463 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
465 rd_ser = &name[name_len];
466 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
467 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
469 GNUNET_HashCode zone_hash;
470 GNUNET_CRYPTO_hash (zone_key, key_len, &zone_hash);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting %u record for name `%s' in zone `%s'\n", rd_count, name, GNUNET_h2s(&zone_hash));
474 /* Database operation */
475 res = GSN_database->put_records(GSN_database->cls,
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
483 name, (res == GNUNET_OK) ? "OK" : "FAIL");
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
488 rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
489 rpr_msg.op_id = rp_msg->gns_header.r_id;
490 rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
491 if (GNUNET_OK == res)
492 rpr_msg.op_result = htons (GNUNET_OK);
494 rpr_msg.op_result = htons (GNUNET_NO);
495 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
497 GNUNET_SERVER_receive_done (client, GNUNET_OK);
500 struct CreateRecordContext
502 struct GNUNET_NAMESTORE_RecordData *rd;
503 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
504 struct GNUNET_TIME_Absolute expire;
506 struct GNUNET_NAMESTORE_Client *nc;
511 handle_create_record_it (void *cls,
512 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
513 struct GNUNET_TIME_Absolute expire,
515 unsigned int rd_count,
516 const struct GNUNET_NAMESTORE_RecordData *rd,
517 const struct GNUNET_CRYPTO_RsaSignature *signature)
519 struct CreateRecordContext * crc = cls;
520 struct GNUNET_CRYPTO_RsaSignature *signature_new;
521 struct RecordCreateResponseMessage rcr_msg;
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u existing records for `%s'\n", rd_count, name);
525 struct GNUNET_NAMESTORE_RecordData *rd_new = GNUNET_malloc ((rd_count+1) * sizeof (struct GNUNET_NAMESTORE_RecordData));
526 memcpy (rd_new, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
528 rd_new[rd_count] = *(crc->rd);
530 signature_new = GNUNET_NAMESTORE_create_signature (crc->pkey, name, rd_new, rd_count+1);
532 /* Database operation */
533 res = GSN_database->put_records(GSN_database->cls,
540 GNUNET_free (rd_new);
541 GNUNET_free (signature_new);
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update result for name %u\n", res);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
547 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
548 rcr_msg.gns_header.r_id = htonl (crc->op_id);
549 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordCreateResponseMessage));
550 if (GNUNET_OK == res)
551 rcr_msg.op_result = htons (GNUNET_OK);
553 rcr_msg.op_result = htons (GNUNET_NO);
554 GNUNET_SERVER_notification_context_unicast (snc, crc->nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
558 static void handle_record_create (void *cls,
559 struct GNUNET_SERVER_Client * client,
560 const struct GNUNET_MessageHeader * message)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
563 struct GNUNET_NAMESTORE_Client *nc;
564 struct CreateRecordContext crc;
565 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
566 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
567 GNUNET_HashCode pubkey_hash;
580 int res = GNUNET_SYSERR;
582 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
589 nc = client_lookup(client);
593 GNUNET_SERVER_receive_done (client, GNUNET_OK);
597 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
598 rid = ntohl (rp_msg->gns_header.r_id);
599 name_len = ntohs (rp_msg->name_len);
600 msg_size = ntohs (message->size);
601 rd_count = ntohs (rp_msg->rd_count);
602 rd_ser_len = ntohs (rp_msg->rd_len);
603 key_len = ntohs (rp_msg->pkey_len);
604 msg_size_exp = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
606 if (msg_size != msg_size_exp)
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
610 GNUNET_SERVER_receive_done (client, GNUNET_OK);
614 if ((name_len == 0) || (name_len > 256))
617 GNUNET_SERVER_receive_done (client, GNUNET_OK);
621 pkey_tmp = (char *) &rp_msg[1];
622 name_tmp = &pkey_tmp[key_len];
623 rd_ser = &name_tmp[name_len];
625 if (name_tmp[name_len -1] != '\0')
628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
632 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
633 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
634 GNUNET_assert (rd_count == 1);
636 /* Extracting and converting private key */
637 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
638 GNUNET_assert (pkey != NULL);
639 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
640 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
649 /* Get existing records for name */
650 res = GSN_database->iterate_records(GSN_database->cls, &pubkey_hash, name_tmp, 0, &handle_create_record_it, &crc);
652 GNUNET_CRYPTO_rsa_key_free(pkey);
654 GNUNET_SERVER_receive_done (client, GNUNET_OK);
658 struct RemoveRecordContext
660 struct GNUNET_NAMESTORE_RecordData *rd;
661 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
666 handle_record_remove_it (void *cls,
667 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
668 struct GNUNET_TIME_Absolute expire,
670 unsigned int rd_count,
671 const struct GNUNET_NAMESTORE_RecordData *rd,
672 const struct GNUNET_CRYPTO_RsaSignature *signature)
674 struct RemoveRecordContext *rrc = cls;
675 unsigned int rd_count_new = rd_count -1;
676 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count_new];
679 int found = GNUNET_NO;
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s 'currently has %u records\n", name, rd_count);
685 /* Could not find record to remove */
690 /* Find record to remove */
692 for (c = 0; c < rd_count; c++)
694 if ((rd[c].expiration.abs_value == rrc->rd->expiration.abs_value) &&
695 (rd[c].flags == rrc->rd->flags) &&
696 (rd[c].record_type == rrc->rd->record_type) &&
697 (rd[c].data_size == rrc->rd->data_size) &&
698 (0 == memcmp (rd[c].data, rrc->rd->data, rrc->rd->data_size)))
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found record to remove!\n", rd_count);
710 if ((c2 != rd_count_new) || (found == GNUNET_NO))
712 /* Could not find record to remove */
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name `%s' now has %u records\n", name, rd_count_new);
718 /* Create new signature */
719 struct GNUNET_CRYPTO_RsaSignature * new_signature;
720 new_signature = GNUNET_NAMESTORE_create_signature (rrc->pkey, name, rd_new, rd_count_new);
722 if (new_signature == NULL)
724 /* Signature failed */
730 res = GSN_database->put_records(GSN_database->cls,
734 rd_count_new, rd_new,
736 GNUNET_free (new_signature);
738 if (GNUNET_OK != res)
740 /* Could put records into database */
748 static void handle_record_remove (void *cls,
749 struct GNUNET_SERVER_Client * client,
750 const struct GNUNET_MessageHeader * message)
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
753 struct GNUNET_NAMESTORE_Client *nc;
754 struct RecordRemoveResponseMessage rrr_msg;
755 struct GNUNET_CRYPTO_RsaPrivateKey *pkey;
756 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
757 GNUNET_HashCode pubkey_hash;
758 char * pkey_tmp = NULL;
759 char * name_tmp = NULL;
760 char * rd_ser = NULL;
763 size_t rd_ser_len = 0;
765 size_t msg_size_exp = 0;
769 int res = GNUNET_SYSERR;
771 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
778 nc = client_lookup(client);
782 GNUNET_SERVER_receive_done (client, GNUNET_OK);
786 struct RecordRemoveMessage * rr_msg = (struct RecordRemoveMessage *) message;
787 rid = ntohl (rr_msg->gns_header.r_id);
788 name_len = ntohs (rr_msg->name_len);
789 rd_ser_len = ntohs (rr_msg->rd_len);
790 rd_count = ntohs (rr_msg->rd_count);
791 key_len = ntohs (rr_msg->key_len);
792 msg_size = ntohs (message->size);
794 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
797 GNUNET_SERVER_receive_done (client, GNUNET_OK);
801 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
804 GNUNET_SERVER_receive_done (client, GNUNET_OK);
808 msg_size_exp = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
809 if (msg_size != msg_size_exp)
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
813 GNUNET_SERVER_receive_done (client, GNUNET_OK);
817 if ((rd_count != 1) || (rd_ser_len < 1) || (name_len >=256) || (name_len == 0))
820 GNUNET_SERVER_receive_done (client, GNUNET_OK);
824 pkey_tmp = (char *) &rr_msg[1];
825 name_tmp = &pkey_tmp[key_len];
826 rd_ser = &name_tmp[name_len];
829 if ((name_len == 0) || (name_len > 256))
832 GNUNET_SERVER_receive_done (client, GNUNET_OK);
836 if (name_tmp[name_len -1] != '\0')
839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
843 /* Extracting and converting private key */
844 pkey = GNUNET_CRYPTO_rsa_decode_key((char *) pkey_tmp, key_len);
845 GNUNET_assert (pkey != NULL);
846 GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
847 GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
849 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
850 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
851 GNUNET_assert (rd_count == 1);
853 struct RemoveRecordContext rrc;
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s' in zone `%s'\n", name_tmp, GNUNET_h2s(&pubkey_hash));
859 /* Database operation */
860 res = GSN_database->iterate_records (GSN_database->cls,
864 handle_record_remove_it, &rrc);
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing record for name `%s': %s\n",
867 name_tmp, (rrc.op_res == 0) ? "OK" : "FAIL");
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
871 rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
872 rrr_msg.gns_header.r_id = rr_msg->gns_header.r_id;
873 rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
874 rrr_msg.op_result = htons (rrc.op_res);
875 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
877 GNUNET_CRYPTO_rsa_key_free (pkey);
879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
882 struct ZoneIterationProcResult
885 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
888 struct GNUNET_CRYPTO_RsaSignature signature;
889 struct GNUNET_TIME_Absolute expire;
899 void zone_iteration_proc (void *cls,
900 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
901 struct GNUNET_TIME_Absolute expire,
903 unsigned int rd_count,
904 const struct GNUNET_NAMESTORE_RecordData *rd,
905 const struct GNUNET_CRYPTO_RsaSignature *signature)
907 struct ZoneIterationProcResult *zipr = cls;
909 if (zone_key != NULL)
911 zipr->zone_key = *zone_key;
912 zipr->have_zone_key = GNUNET_YES;
915 zipr->have_zone_key = GNUNET_NO;
917 zipr->expire = expire;
921 memcpy (zipr->name, name, strlen(name) + 1);
922 zipr->have_name = GNUNET_YES;
925 zipr->have_name = GNUNET_NO;
927 if (signature != NULL)
929 zipr->signature = *signature;
930 zipr->have_signature = GNUNET_YES;
933 zipr->have_signature = GNUNET_NO;
935 if ((rd_count > 0) && (rd != NULL))
937 len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
938 zipr->rd_ser = GNUNET_malloc (len);
939 GNUNET_NAMESTORE_records_serialize(rd_count, rd, len, zipr->rd_ser);
940 zipr->rd_ser_len = len;
944 static void handle_iteration_start (void *cls,
945 struct GNUNET_SERVER_Client * client,
946 const struct GNUNET_MessageHeader * message)
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
950 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
951 struct GNUNET_NAMESTORE_Client *nc;
952 struct GNUNET_NAMESTORE_ZoneIteration *zi;
953 struct ZoneIterationResponseMessage zir_msg;
954 struct ZoneIterationProcResult zipr;
957 nc = client_lookup(client);
961 GNUNET_SERVER_receive_done (client, GNUNET_OK);
965 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
966 zi->request_id = ntohl (zis_msg->gns_header.r_id);
969 zi->zone = zis_msg->zone;
971 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
973 res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
976 /* GNUNET_OK on success */
980 /* GNUNET_SYSERR on error */
983 /* GNUNET_NO if there were no results, */
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
992 zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
993 zir_msg.gns_header.r_id = htonl(zi->request_id);
994 zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
997 GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
1000 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1003 static void handle_iteration_stop (void *cls,
1004 struct GNUNET_SERVER_Client * client,
1005 const struct GNUNET_MessageHeader * message)
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
1009 struct GNUNET_NAMESTORE_Client *nc;
1010 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1011 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1014 nc = client_lookup(client);
1017 GNUNET_break_op (0);
1018 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1022 rid = ntohl (zis_msg->gns_header.r_id);
1023 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1025 if (zi->request_id == rid)
1030 GNUNET_break_op (0);
1031 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1035 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
1039 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1042 static void handle_iteration_next (void *cls,
1043 struct GNUNET_SERVER_Client * client,
1044 const struct GNUNET_MessageHeader * message)
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1048 struct GNUNET_NAMESTORE_Client *nc;
1049 struct GNUNET_NAMESTORE_ZoneIteration *zi;
1050 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
1054 nc = client_lookup(client);
1057 GNUNET_break_op (0);
1058 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1062 rid = ntohl (zis_msg->gns_header.r_id);
1063 for (zi = nc->op_head; zi != NULL; zi = zi->next)
1065 if (zi->request_id == rid)
1070 GNUNET_break_op (0);
1071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1076 res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
1082 * Process template requests.
1084 * @param cls closure
1085 * @param server the initialized server
1086 * @param cfg configuration to use
1089 run (void *cls, struct GNUNET_SERVER_Handle *server,
1090 const struct GNUNET_CONFIGURATION_Handle *cfg)
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1096 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1097 {&handle_start, NULL,
1098 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
1099 {&handle_lookup_name, NULL,
1100 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
1101 {&handle_record_put, NULL,
1102 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
1103 {&handle_record_create, NULL,
1104 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
1105 {&handle_record_remove, NULL,
1106 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
1107 {&handle_iteration_start, NULL,
1108 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
1109 {&handle_iteration_stop, NULL,
1110 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage)},
1111 {&handle_iteration_next, NULL,
1112 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
1118 /* Loading database plugin */
1120 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1124 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1125 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1126 if (GSN_database == NULL)
1127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
1129 GNUNET_free (database);
1131 /* Configuring server handles */
1132 GNUNET_SERVER_add_handlers (server, handlers);
1133 snc = GNUNET_SERVER_notification_context_create (server, 16);
1134 GNUNET_SERVER_disconnect_notify (server,
1135 &client_disconnect_notification,
1138 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1145 * The main function for the template service.
1147 * @param argc number of arguments from the command line
1148 * @param argv command line arguments
1149 * @return 0 ok, 1 on error
1152 main (int argc, char *const *argv)
1154 return (GNUNET_OK ==
1155 GNUNET_SERVICE_run (argc, argv, "namestore",
1156 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1159 /* end of gnunet-service-namestore.c */