2 This file is part of GNUnet.
3 (C) 2009, 2010 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/namestore_api.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
29 #include "gnunet_util_lib.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_arm_service.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_namestore_service.h"
36 #include "namestore.h"
38 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
40 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
45 struct GNUNET_NAMESTORE_QueueEntry
51 struct GNUNET_NAMESTORE_QueueEntry *next;
56 struct GNUNET_NAMESTORE_QueueEntry *prev;
58 struct GNUNET_NAMESTORE_Handle *nsh;
62 GNUNET_NAMESTORE_ContinuationWithStatus cont;
65 GNUNET_NAMESTORE_RecordProcessor proc;
68 char *data; /*stub data pointer*/
75 struct GNUNET_NAMESTORE_ZoneIterator
81 struct GNUNET_NAMESTORE_ZoneIterator *next;
86 struct GNUNET_NAMESTORE_ZoneIterator *prev;
90 struct GNUNET_NAMESTORE_Handle *h;
91 GNUNET_NAMESTORE_RecordProcessor proc;
101 * Message in linked list we should send to the service. The
102 * actual binary message follows this struct.
104 struct PendingMessage
110 struct PendingMessage *next;
115 struct PendingMessage *prev;
118 * Size of the message.
123 * Is this the 'START' message?
130 * Connection to the NAMESTORE service.
132 struct GNUNET_NAMESTORE_Handle
136 * Configuration to use.
138 const struct GNUNET_CONFIGURATION_Handle *cfg;
141 * Socket (if available).
143 struct GNUNET_CLIENT_Connection *client;
146 * Currently pending transmission request (or NULL).
148 struct GNUNET_CLIENT_TransmitHandle *th;
153 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
156 * Pending messages to send to the service
159 struct PendingMessage * pending_head;
160 struct PendingMessage * pending_tail;
163 * Should we reconnect to service due to some serious error?
169 * Pending namestore queue entries
171 struct GNUNET_NAMESTORE_QueueEntry * op_head;
172 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
177 * Pending namestore zone iterator entries
179 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
180 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
183 struct GNUNET_NAMESTORE_SimpleRecord
188 struct GNUNET_NAMESTORE_SimpleRecord *next;
193 struct GNUNET_NAMESTORE_SimpleRecord *prev;
196 const GNUNET_HashCode *zone;
197 uint32_t record_type;
198 struct GNUNET_TIME_Absolute expiration;
199 enum GNUNET_NAMESTORE_RecordFlags flags;
207 * Disconnect from service and then reconnect.
209 * @param h our handle
212 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
215 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
216 struct LookupNameResponseMessage * msg,
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
220 "LOOKUP_NAME_RESPONSE");
222 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
226 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
227 struct GNUNET_TIME_Absolute expire;
228 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key_tmp;
233 int contains_sig = GNUNET_NO;
236 rd_len = ntohs (msg->rd_len);
237 rd_count = ntohs (msg->rd_count);
238 msg_len = ntohs (msg->gns_header.header.size);
239 name_len = ntohs (msg->name_len);
240 contains_sig = ntohs (msg->contains_sig);
241 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
243 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
244 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
247 if (msg_len != exp_msg_len)
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
250 msg_len, exp_msg_len);
255 name = (char *) &msg[1];
256 rd_tmp = &name[name_len];
258 /* deserialize records */
259 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
260 GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
262 /* reset values if values not contained */
263 if (contains_sig == GNUNET_NO)
266 signature = &msg->signature;
271 public_key_tmp = &msg->public_key;
273 public_key_tmp = NULL;
275 if (qe->proc != NULL)
277 qe->proc (qe->proc_cls, public_key_tmp, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
280 /* Operation done, remove */
281 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
287 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
288 struct RecordPutResponseMessage* msg,
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
292 "RECORD_PUT_RESPONSE");
293 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
294 int res = ntohl (msg->op_result);
296 if (res == GNUNET_OK)
298 if (qe->cont != NULL)
300 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
304 else if (res == GNUNET_SYSERR)
306 if (qe->cont != NULL)
308 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
317 /* Operation done, remove */
318 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
325 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
326 struct RecordCreateResponseMessage* msg,
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
330 "RECORD_CREATE_RESPONSE");
331 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
332 int res = ntohl (msg->op_result);
334 if (res == GNUNET_YES)
336 if (qe->cont != NULL)
338 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
342 else if (res == GNUNET_NO)
344 if (qe->cont != NULL)
346 qe->cont (qe->cont_cls, res, _("Namestore record already existed"));
351 if (qe->cont != NULL)
353 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
357 /* Operation done, remove */
358 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
365 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
366 struct RecordRemoveResponseMessage* msg,
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
370 "RECORD_REMOVE_RESPONSE");
372 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
373 int res = ntohl (msg->op_result);
378 * 1 : No records for entry
379 * 2 : Could not find record to remove
380 * 3 : Failed to create new signature
381 * 4 : Failed to put new set of records in database
385 if (qe->cont != NULL)
387 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
392 if (qe->cont != NULL)
394 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
399 if (qe->cont != NULL)
401 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
406 if (qe->cont != NULL)
408 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
413 if (qe->cont != NULL)
415 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
423 /* Operation done, remove */
424 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
430 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
431 struct ZoneToNameResponseMessage* msg,
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
435 "ZONE_TO_NAME_RESPONSE");
437 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
438 int res = ntohs (msg->res);
440 struct GNUNET_TIME_Absolute expire;
443 unsigned int rd_count;
448 if (res == GNUNET_SYSERR)
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\n");
451 if (qe->proc != NULL)
452 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
454 else if (res == GNUNET_NO)
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
457 if (qe->proc != NULL)
458 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
460 else if (res == GNUNET_YES)
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
464 name_len = ntohs (msg->name_len);
465 rd_count = ntohs (msg->rd_count);
466 rd_ser_len = ntohs (msg->rd_len);
467 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
469 name_tmp = (char *) &msg[1];
470 rd_tmp = &name_tmp[name_len];
472 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
473 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd);
475 if (qe->proc != NULL)
476 qe->proc (qe->proc_cls, &msg->zone_key, expire, name_tmp, rd_count, rd, &msg->signature);
481 /* Operation done, remove */
482 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
488 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
489 const struct GNUNET_MessageHeader *msg,
490 int type, size_t size)
493 /* handle different message type */
495 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
496 if (size < sizeof (struct LookupNameResponseMessage))
501 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
503 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
504 if (size != sizeof (struct RecordPutResponseMessage))
509 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
511 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
512 if (size != sizeof (struct RecordCreateResponseMessage))
517 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
519 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
520 if (size != sizeof (struct RecordRemoveResponseMessage))
525 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
527 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
528 if (size < sizeof (struct ZoneToNameResponseMessage))
533 handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
542 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
543 struct ZoneIterationResponseMessage *msg,
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
547 "ZONE_ITERATION_RESPONSE");
549 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubdummy;
551 size_t exp_msg_len = 0;
554 unsigned rd_count = 0;
558 struct GNUNET_TIME_Absolute expire;
560 msg_len = ntohs (msg->gns_header.header.size);
561 rd_len = ntohs (msg->rd_len);
562 rd_count = ntohs (msg->rd_count);
563 name_len = ntohs (msg->name_len);
564 expire = GNUNET_TIME_absolute_ntoh (msg->expire);
566 exp_msg_len = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_len;
567 if (msg_len != exp_msg_len)
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
570 msg_len, exp_msg_len);
574 if (0 != ntohs (msg->reserved))
580 memset (&pubdummy, '\0', sizeof (pubdummy));
581 if ((0 == name_len) && (0 == (memcmp (&msg->public_key, &pubdummy, sizeof (pubdummy)))))
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zone iteration is completed!\n");
585 if (ze->proc != NULL)
586 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_zero (), NULL , 0, NULL, NULL);
588 GNUNET_CONTAINER_DLL_remove(ze->h->z_head, ze->h->z_tail, ze);
593 name_tmp = (char *) &msg[1];
594 if ((name_tmp[name_len -1] != '\0') || (name_len > 256))
599 rd_ser_tmp = (char *) &name_tmp[name_len];
600 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
601 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
607 if (ze->proc != NULL)
608 ze->proc(ze->proc_cls, &msg->public_key, expire, name_tmp, rd_count, rd, &msg->signature);
613 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
614 const struct GNUNET_MessageHeader *msg,
615 int type, size_t size)
618 /* handle different message type */
620 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
621 if (size < sizeof (struct ZoneIterationResponseMessage))
626 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
635 * Type of a function to call when we receive a message
638 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
639 * @param msg message received, NULL on timeout or fatal error
642 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
644 struct GNUNET_NAMESTORE_Handle *h = cls;
645 struct GNUNET_NAMESTORE_Header * gm;
646 struct GNUNET_NAMESTORE_QueueEntry *qe;
647 struct GNUNET_NAMESTORE_ZoneIterator *ze;
650 uint32_t r_id = UINT32_MAX;
658 size = ntohs (msg->size);
659 type = ntohs (msg->type);
661 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
664 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
665 GNUNET_TIME_UNIT_FOREVER_REL);
669 gm = (struct GNUNET_NAMESTORE_Header *) msg;
670 r_id = ntohl (gm->r_id);
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
674 /* Find matching operation */
677 /* No matching pending operation found */
679 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
680 GNUNET_TIME_UNIT_FOREVER_REL);
684 /* Is it a record related operation ? */
685 for (qe = h->op_head; qe != NULL; qe = qe->next)
687 if (qe->op_id == r_id)
692 manage_record_operations (qe, msg, type, size);
695 /* Is it a zone iteration operation ? */
696 for (ze = h->z_head; ze != NULL; ze = ze->next)
698 if (ze->op_id == r_id)
703 manage_zone_operations (ze, msg, type, size);
706 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
707 GNUNET_TIME_UNIT_FOREVER_REL);
709 if (GNUNET_YES == h->reconnect)
716 * Transmit messages from the message queue to the service
717 * (if there are any, and if we are not already trying).
719 * @param h handle to use
722 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
726 * We can now transmit a message to NAMESTORE. Do it.
728 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
729 * @param size number of bytes we can transmit
730 * @param buf where to copy the messages
731 * @return number of bytes copied into buf
734 transmit_message_to_namestore (void *cls, size_t size, void *buf)
736 struct GNUNET_NAMESTORE_Handle *h = cls;
737 struct PendingMessage *p;
742 if ((size == 0) || (buf == NULL))
749 while ((NULL != (p = h->pending_head)) && (p->size <= size))
751 memcpy (&cbuf[ret], &p[1], p->size);
754 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
755 if (GNUNET_YES == p->is_init)
756 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
757 GNUNET_TIME_UNIT_FOREVER_REL);
766 * Transmit messages from the message queue to the service
767 * (if there are any, and if we are not already trying).
769 * @param h handle to use
772 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
774 struct PendingMessage *p;
778 if (NULL == (p = h->pending_head))
780 if (NULL == h->client)
781 return; /* currently reconnecting */
783 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
784 GNUNET_TIME_UNIT_FOREVER_REL,
785 GNUNET_NO, &transmit_message_to_namestore,
791 * Reconnect to namestore service.
793 * @param h the handle to the namestore service
796 reconnect (struct GNUNET_NAMESTORE_Handle *h)
798 struct PendingMessage *p;
799 struct StartMessage *init;
801 GNUNET_assert (NULL == h->client);
802 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
803 GNUNET_assert (NULL != h->client);
805 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
807 p = GNUNET_malloc (sizeof (struct PendingMessage) +
808 sizeof (struct StartMessage));
809 p->size = sizeof (struct StartMessage);
810 p->is_init = GNUNET_YES;
811 init = (struct StartMessage *) &p[1];
812 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
813 init->header.size = htons (sizeof (struct StartMessage));
814 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
820 * Re-establish the connection to the service.
822 * @param cls handle to use to re-connect.
823 * @param tc scheduler context
826 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
828 struct GNUNET_NAMESTORE_Handle *h = cls;
830 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
836 * Disconnect from service and then reconnect.
838 * @param h our handle
841 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
843 h->reconnect = GNUNET_NO;
844 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
846 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
852 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
854 uint32_t op_id = h->op_id;
860 * Initialize the connection with the NAMESTORE service.
862 * @param cfg configuration to use
863 * @return handle to the GNS service, or NULL on error
865 struct GNUNET_NAMESTORE_Handle *
866 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
868 struct GNUNET_NAMESTORE_Handle *h;
870 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
872 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
878 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
880 struct PendingMessage *p;
881 struct GNUNET_NAMESTORE_QueueEntry *q;
882 struct GNUNET_NAMESTORE_ZoneIterator *z;
883 struct GNUNET_NAMESTORE_Handle *h = cls;
884 GNUNET_assert (h != NULL);
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
886 while (NULL != (p = h->pending_head))
888 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
892 while (NULL != (q = h->op_head))
895 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
899 while (NULL != (z = h->z_head))
901 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
905 if (NULL != h->client)
907 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
910 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
912 GNUNET_SCHEDULER_cancel (h->reconnect_task);
913 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
920 * Disconnect from the namestore service (and free associated
923 * @param h handle to the namestore
924 * @param drop set to GNUNET_YES to delete all data in namestore (!)
927 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from namestore service\n");
930 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
935 * Store an item in the namestore. If the item is already present,
936 * the expiration time is updated to the max of the existing time and
937 * the new time. This API is used when we cache signatures from other
940 * @param h handle to the namestore
941 * @param zone_key public key of the zone
942 * @param name name that is being mapped (at most 255 characters long)
943 * @param expire when does the corresponding block in the DHT expire (until
944 * when should we never do a DHT lookup for the same name again)?
945 * @param rd_count number of entries in 'rd' array
946 * @param rd array of records with data to store
947 * @param signature signature for all the records in the zone under the given name
948 * @param cont continuation to call when done
949 * @param cont_cls closure for cont
950 * @return handle to abort the request
952 struct GNUNET_NAMESTORE_QueueEntry *
953 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
954 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
956 struct GNUNET_TIME_Absolute expire,
957 unsigned int rd_count,
958 const struct GNUNET_NAMESTORE_RecordData *rd,
959 const struct GNUNET_CRYPTO_RsaSignature *signature,
960 GNUNET_NAMESTORE_ContinuationWithStatus cont,
963 struct GNUNET_NAMESTORE_QueueEntry *qe;
964 struct PendingMessage *pe;
966 /* pointer to elements */
972 size_t rd_ser_len = 0;
975 GNUNET_assert (NULL != h);
976 GNUNET_assert (NULL != zone_key);
977 GNUNET_assert (NULL != name);
978 GNUNET_assert (NULL != rd);
979 GNUNET_assert (NULL != signature);
981 name_len = strlen(name) + 1;
989 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
992 qe->cont_cls = cont_cls;
994 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
997 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
998 char rd_ser[rd_ser_len];
999 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1001 struct RecordPutMessage * msg;
1002 msg_size = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
1004 /* create msg here */
1005 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1006 pe->size = msg_size;
1007 pe->is_init = GNUNET_NO;
1008 msg = (struct RecordPutMessage *) &pe[1];
1009 name_tmp = (char *) &msg[1];
1010 rd_tmp = &name_tmp[name_len];
1012 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
1013 msg->gns_header.header.size = htons (msg_size);
1014 msg->gns_header.r_id = htonl (rid);
1015 msg->signature = *signature;
1016 msg->name_len = htons (name_len);
1017 msg->expire = GNUNET_TIME_absolute_hton (expire);
1018 msg->rd_len = htons (rd_ser_len);
1019 msg->rd_count = htons (rd_count);
1021 msg->public_key = *zone_key;
1022 memcpy (name_tmp, name, name_len);
1023 memcpy (rd_tmp, rd_ser, rd_ser_len);
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1027 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1035 * Check if a signature is valid. This API is used by the GNS Block
1036 * to validate signatures received from the network.
1038 * @param public_key public key of the zone
1039 * @param name name that is being mapped (at most 255 characters long)
1040 * @param rd_count number of entries in 'rd' array
1041 * @param rd array of records with data to store
1042 * @param signature signature for all the records in the zone under the given name
1043 * @return GNUNET_OK if the signature is valid
1046 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1048 unsigned int rd_count,
1049 const struct GNUNET_NAMESTORE_RecordData *rd,
1050 const struct GNUNET_CRYPTO_RsaSignature *signature)
1052 int res = GNUNET_SYSERR;
1053 size_t rd_ser_len = 0;
1054 size_t name_len = 0;
1057 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1059 GNUNET_assert (public_key != NULL);
1060 GNUNET_assert (name != NULL);
1061 GNUNET_assert (rd != NULL);
1062 GNUNET_assert (signature != NULL);
1065 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1066 char rd_ser[rd_ser_len];
1067 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1069 name_len = strlen (name) + 1;
1073 return GNUNET_SYSERR;
1076 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1077 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1078 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1079 name_tmp = (char *) &sig_purpose[1];
1080 rd_tmp = &name_tmp[name_len];
1081 memcpy (name_tmp, name, name_len);
1082 memcpy (rd_tmp, rd_ser, rd_ser_len);
1084 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1086 GNUNET_free (sig_purpose);
1092 * Store an item in the namestore. If the item is already present,
1093 * the expiration time is updated to the max of the existing time and
1094 * the new time. This API is used by the authority of a zone.
1096 * @param h handle to the namestore
1097 * @param pkey private key of the zone
1098 * @param expire block expiration time
1099 * @param name name that is being mapped (at most 255 characters long)
1100 * @param rd record data to store
1101 * @param cont continuation to call when done
1102 * @param cont_cls closure for cont
1103 * @return handle to abort the request
1105 struct GNUNET_NAMESTORE_QueueEntry *
1106 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1107 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1108 const struct GNUNET_TIME_Absolute expire,
1110 const struct GNUNET_NAMESTORE_RecordData *rd,
1111 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1114 struct GNUNET_NAMESTORE_QueueEntry *qe;
1115 struct PendingMessage *pe;
1119 size_t rd_ser_len = 0;
1120 size_t msg_size = 0;
1121 size_t name_len = 0;
1125 GNUNET_assert (NULL != h);
1126 GNUNET_assert (NULL != pkey);
1127 GNUNET_assert (NULL != name);
1128 GNUNET_assert (NULL != rd);
1130 name_len = strlen(name) + 1;
1138 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1141 qe->cont_cls = cont_cls;
1143 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1146 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1147 GNUNET_assert (pkey_enc != NULL);
1148 key_len = ntohs (pkey_enc->len);
1150 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1151 char rd_ser[rd_ser_len];
1152 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1154 struct RecordCreateMessage * msg;
1155 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1157 /* create msg here */
1158 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1159 pe->size = msg_size;
1160 pe->is_init = GNUNET_NO;
1161 msg = (struct RecordCreateMessage *) &pe[1];
1163 pkey_tmp = (char *) &msg[1];
1164 name_tmp = &pkey_tmp[key_len];
1165 rd_tmp = &name_tmp[name_len];
1167 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1168 msg->gns_header.header.size = htons (msg_size);
1169 msg->gns_header.r_id = htonl (rid);
1170 msg->name_len = htons (name_len);
1171 msg->rd_count = htons (1);
1172 msg->rd_len = htons (rd_ser_len);
1173 msg->pkey_len = htons (key_len);
1174 msg->expire = GNUNET_TIME_absolute_hton(expire);
1175 memcpy (pkey_tmp, pkey_enc, key_len);
1176 memcpy (name_tmp, name, name_len);
1177 memcpy (rd_tmp, rd_ser, rd_ser_len);
1178 GNUNET_free (pkey_enc);
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1182 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1189 * Explicitly remove some content from the database. The
1190 * "cont"inuation will be called with status "GNUNET_OK" if content
1191 * was removed, "GNUNET_NO" if no matching entry was found and
1192 * "GNUNET_SYSERR" on all other types of errors.
1193 * This API is used by the authority of a zone.
1195 * @param h handle to the namestore
1196 * @param pkey private key of the zone
1197 * @param name name that is being mapped (at most 255 characters long)
1198 * @param rd record data
1199 * @param cont continuation to call when done
1200 * @param cont_cls closure for cont
1201 * @return handle to abort the request
1203 struct GNUNET_NAMESTORE_QueueEntry *
1204 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1205 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1207 const struct GNUNET_NAMESTORE_RecordData *rd,
1208 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1211 struct GNUNET_NAMESTORE_QueueEntry *qe;
1212 struct PendingMessage *pe;
1216 size_t rd_ser_len = 0;
1217 size_t msg_size = 0;
1218 size_t name_len = 0;
1222 GNUNET_assert (NULL != h);
1225 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1228 qe->cont_cls = cont_cls;
1230 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1233 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1234 GNUNET_assert (pkey_enc != NULL);
1235 key_len = ntohs (pkey_enc->len);
1237 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1238 char rd_ser[rd_ser_len];
1239 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1241 name_len = strlen (name) + 1;
1243 struct RecordRemoveMessage * msg;
1244 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1246 /* create msg here */
1247 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1248 pe->size = msg_size;
1249 pe->is_init = GNUNET_NO;
1250 msg = (struct RecordRemoveMessage *) &pe[1];
1252 pkey_tmp = (char *) &msg[1];
1253 name_tmp = &pkey_tmp[key_len];
1254 rd_tmp = &name_tmp[name_len];
1256 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1257 msg->gns_header.header.size = htons (msg_size);
1258 msg->gns_header.r_id = htonl (rid);
1259 msg->name_len = htons (name_len);
1260 msg->rd_len = htons (rd_ser_len);
1261 msg->rd_count = htons (1);
1262 msg->pkey_len = htons (key_len);
1263 memcpy (pkey_tmp, pkey_enc, key_len);
1264 memcpy (name_tmp, name, name_len);
1265 memcpy (rd_tmp, rd_ser, rd_ser_len);
1267 GNUNET_free (pkey_enc);
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1271 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1278 * Get a result for a particular key from the namestore. The processor
1279 * will only be called once.
1281 * @param h handle to the namestore
1282 * @param zone zone to look up a record from
1283 * @param name name to look up
1284 * @param record_type desired record type, 0 for all
1285 * @param proc function to call on the matching records, or with
1286 * NULL (rd_count == 0) if there are no matching records
1287 * @param proc_cls closure for proc
1288 * @return a handle that can be used to
1291 struct GNUNET_NAMESTORE_QueueEntry *
1292 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1293 const GNUNET_HashCode *zone,
1295 uint32_t record_type,
1296 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1298 struct GNUNET_NAMESTORE_QueueEntry *qe;
1299 struct PendingMessage *pe;
1300 size_t msg_size = 0;
1301 size_t name_len = 0;
1304 GNUNET_assert (NULL != h);
1305 GNUNET_assert (NULL != zone);
1306 GNUNET_assert (NULL != name);
1308 name_len = strlen (name) + 1;
1309 if ((name_len == 0) || (name_len > 256))
1316 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1319 qe->proc_cls = proc_cls;
1321 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1324 msg_size = sizeof (struct LookupNameMessage) + name_len;
1325 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1327 /* create msg here */
1328 struct LookupNameMessage * msg;
1329 pe->size = msg_size;
1330 pe->is_init = GNUNET_NO;
1331 msg = (struct LookupNameMessage *) &pe[1];
1332 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1333 msg->gns_header.header.size = htons (msg_size);
1334 msg->gns_header.r_id = htonl (rid);
1335 msg->record_type = htonl (record_type);
1336 msg->name_len = htonl (name_len);
1338 memcpy (&msg[1], name, name_len);
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1342 /* transmit message */
1343 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1351 * Look for an existing PKEY delegation record for a given public key.
1352 * Returns at most one result to the processor.
1354 * @param h handle to the namestore
1355 * @param zone hash of public key of the zone to look up in, never NULL
1356 * @param value_zone hash of the public key of the target zone (value), never NULL
1357 * @param proc function to call on the matching records, or with
1358 * NULL (rd_count == 0) if there are no matching records
1359 * @param proc_cls closure for proc
1360 * @return a handle that can be used to
1363 struct GNUNET_NAMESTORE_QueueEntry *
1364 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1365 const GNUNET_HashCode *zone,
1366 const GNUNET_HashCode *value_zone,
1367 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1369 struct GNUNET_NAMESTORE_QueueEntry *qe;
1370 struct PendingMessage *pe;
1371 size_t msg_size = 0;
1374 GNUNET_assert (NULL != h);
1375 GNUNET_assert (NULL != zone);
1376 GNUNET_assert (NULL != value_zone);
1379 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1382 qe->proc_cls = proc_cls;
1384 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1387 msg_size = sizeof (struct ZoneToNameMessage);
1388 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1390 /* create msg here */
1391 struct ZoneToNameMessage * msg;
1392 pe->size = msg_size;
1393 pe->is_init = GNUNET_NO;
1394 msg = (struct ZoneToNameMessage *) &pe[1];
1395 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1396 msg->gns_header.header.size = htons (msg_size);
1397 msg->gns_header.r_id = htonl (rid);
1399 msg->value_zone = *value_zone;
1401 char * z_tmp = strdup (GNUNET_h2s (zone));
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1403 "NAMESTORE_ZONE_TO_NAME",
1405 GNUNET_h2s (value_zone));
1406 GNUNET_free (z_tmp);
1408 /* transmit message */
1409 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1418 * Starts a new zone iteration (used to periodically PUT all of our
1419 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1420 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1421 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1422 * immediately, and then again after
1423 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1425 * @param h handle to the namestore
1426 * @param zone zone to access, NULL for all zones
1427 * @param must_have_flags flags that must be set for the record to be returned
1428 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1429 * @param proc function to call on each name from the zone; it
1430 * will be called repeatedly with a value (if available)
1431 * and always once at the end with a name of NULL.
1432 * @param proc_cls closure for proc
1433 * @return an iterator handle to use for iteration
1435 struct GNUNET_NAMESTORE_ZoneIterator *
1436 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1437 const GNUNET_HashCode *zone,
1438 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1439 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1440 GNUNET_NAMESTORE_RecordProcessor proc,
1443 struct GNUNET_NAMESTORE_ZoneIterator *it;
1444 struct PendingMessage *pe;
1445 size_t msg_size = 0;
1448 GNUNET_assert (NULL != h);
1452 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1455 it->proc_cls = proc;
1461 it->has_zone = GNUNET_YES;
1465 memset (&it->zone, '\0', sizeof (it->zone));
1466 it->has_zone = GNUNET_NO;
1468 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1471 msg_size = sizeof (struct ZoneIterationStartMessage);
1472 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1474 /* create msg here */
1475 struct ZoneIterationStartMessage * msg;
1476 pe->size = msg_size;
1477 pe->is_init = GNUNET_NO;
1478 msg = (struct ZoneIterationStartMessage *) &pe[1];
1479 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1480 msg->gns_header.header.size = htons (msg_size);
1481 msg->gns_header.r_id = htonl (rid);
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_START");
1490 memset (&msg->zone, '\0', sizeof (msg->zone));
1492 msg->must_have_flags = ntohs (must_have_flags);
1493 msg->must_not_have_flags = ntohs (must_not_have_flags);
1497 /* transmit message */
1498 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1506 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1507 * for the next record.
1509 * @param it the iterator
1512 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1514 struct GNUNET_NAMESTORE_Handle *h;
1515 struct PendingMessage *pe;
1516 size_t msg_size = 0;
1518 GNUNET_assert (NULL != it);
1522 msg_size = sizeof (struct ZoneIterationNextMessage);
1523 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1525 /* create msg here */
1526 struct ZoneIterationNextMessage * msg;
1527 pe->size = msg_size;
1528 pe->is_init = GNUNET_NO;
1529 msg = (struct ZoneIterationNextMessage *) &pe[1];
1530 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1531 msg->gns_header.header.size = htons (msg_size);
1532 msg->gns_header.r_id = htonl (it->op_id);
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_NEXT");
1536 /* transmit message */
1537 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1543 * Stops iteration and releases the namestore handle for further calls.
1545 * @param it the iterator
1548 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1550 GNUNET_assert (NULL != it);
1551 struct PendingMessage *pe;
1552 size_t msg_size = 0;
1553 struct GNUNET_NAMESTORE_Handle *h = it->h;
1556 msg_size = sizeof (struct ZoneIterationStopMessage);
1557 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1559 /* create msg here */
1560 struct ZoneIterationStopMessage * msg;
1561 pe->size = msg_size;
1562 pe->is_init = GNUNET_NO;
1563 msg = (struct ZoneIterationStopMessage *) &pe[1];
1564 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1565 msg->gns_header.header.size = htons (msg_size);
1566 msg->gns_header.r_id = htonl (it->op_id);
1568 if (GNUNET_YES == it->has_zone)
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_STOP");
1573 /* transmit message */
1574 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1580 * Cancel a namestore operation. The final callback from the
1581 * operation must not have been done yet.
1583 * @param qe operation to cancel
1586 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1588 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1590 GNUNET_assert (qe != NULL);
1592 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1597 /* end of namestore_api.c */