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;
224 /* Operation done, remove */
225 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
231 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
232 struct GNUNET_TIME_Absolute expire;
233 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key_tmp;
238 int contains_sig = GNUNET_NO;
241 rd_len = ntohs (msg->rd_len);
242 rd_count = ntohs (msg->rd_count);
243 msg_len = ntohs (msg->gns_header.header.size);
244 name_len = ntohs (msg->name_len);
245 contains_sig = ntohs (msg->contains_sig);
246 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
248 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
249 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
252 if (msg_len != exp_msg_len)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
255 msg_len, exp_msg_len);
260 name = (char *) &msg[1];
261 rd_tmp = &name[name_len];
263 /* deserialize records */
264 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
265 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd))
272 /* reset values if values not contained */
273 if (contains_sig == GNUNET_NO)
276 signature = &msg->signature;
281 public_key_tmp = &msg->public_key;
283 public_key_tmp = NULL;
285 if (qe->proc != NULL)
287 qe->proc (qe->proc_cls, public_key_tmp, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
294 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
295 struct RecordPutResponseMessage* msg,
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
299 "RECORD_PUT_RESPONSE");
301 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
302 /* Operation done, remove */
303 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
305 int res = ntohl (msg->op_result);
307 if (res == GNUNET_OK)
309 if (qe->cont != NULL)
311 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
315 else if (res == GNUNET_SYSERR)
317 if (qe->cont != NULL)
319 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
333 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
334 struct RecordCreateResponseMessage* msg,
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
338 "RECORD_CREATE_RESPONSE");
340 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
341 /* Operation done, remove */
342 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
344 int res = ntohl (msg->op_result);
345 if (res == GNUNET_YES)
347 if (qe->cont != NULL)
349 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
353 else if (res == GNUNET_NO)
355 if (qe->cont != NULL)
357 qe->cont (qe->cont_cls, res, _("Namestore record already existed"));
362 if (qe->cont != NULL)
364 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
373 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
374 struct RecordRemoveResponseMessage* msg,
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
378 "RECORD_REMOVE_RESPONSE");
380 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
381 /* Operation done, remove */
382 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
384 int res = ntohl (msg->op_result);
388 * 1 : No records for entry
389 * 2 : Could not find record to remove
390 * 3 : Failed to create new signature
391 * 4 : Failed to put new set of records in database
395 if (qe->cont != NULL)
397 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
402 if (qe->cont != NULL)
404 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
409 if (qe->cont != NULL)
411 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
416 if (qe->cont != NULL)
418 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
423 if (qe->cont != NULL)
425 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
437 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
438 struct ZoneToNameResponseMessage* msg,
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
442 "ZONE_TO_NAME_RESPONSE");
444 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
445 /* Operation done, remove */
446 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
448 int res = ntohs (msg->res);
450 struct GNUNET_TIME_Absolute expire;
453 unsigned int rd_count;
458 if (res == GNUNET_SYSERR)
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\n");
461 if (qe->proc != NULL)
462 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
464 else if (res == GNUNET_NO)
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
467 if (qe->proc != NULL)
468 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
470 else if (res == GNUNET_YES)
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
474 name_len = ntohs (msg->name_len);
475 rd_count = ntohs (msg->rd_count);
476 rd_ser_len = ntohs (msg->rd_len);
477 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
479 name_tmp = (char *) &msg[1];
480 rd_tmp = &name_tmp[name_len];
482 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
483 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
489 if (qe->proc != NULL)
490 qe->proc (qe->proc_cls, &msg->zone_key, expire, name_tmp, rd_count, rd, &msg->signature);
500 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
501 const struct GNUNET_MessageHeader *msg,
502 int type, size_t size)
505 /* handle different message type */
507 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
508 if (size < sizeof (struct LookupNameResponseMessage))
513 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
515 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
516 if (size != sizeof (struct RecordPutResponseMessage))
521 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
523 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
524 if (size != sizeof (struct RecordCreateResponseMessage))
529 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
531 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
532 if (size != sizeof (struct RecordRemoveResponseMessage))
537 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
539 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
540 if (size < sizeof (struct ZoneToNameResponseMessage))
545 handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
554 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
555 struct ZoneIterationResponseMessage *msg,
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
559 "ZONE_ITERATION_RESPONSE");
561 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubdummy;
563 size_t exp_msg_len = 0;
566 unsigned rd_count = 0;
570 struct GNUNET_TIME_Absolute expire;
572 msg_len = ntohs (msg->gns_header.header.size);
573 rd_len = ntohs (msg->rd_len);
574 rd_count = ntohs (msg->rd_count);
575 name_len = ntohs (msg->name_len);
576 expire = GNUNET_TIME_absolute_ntoh (msg->expire);
578 exp_msg_len = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_len;
579 if (msg_len != exp_msg_len)
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
582 msg_len, exp_msg_len);
586 if (0 != ntohs (msg->reserved))
592 memset (&pubdummy, '\0', sizeof (pubdummy));
593 if ((0 == name_len) && (0 == (memcmp (&msg->public_key, &pubdummy, sizeof (pubdummy)))))
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zone iteration is completed!\n");
597 GNUNET_CONTAINER_DLL_remove(ze->h->z_head, ze->h->z_tail, ze);
599 if (ze->proc != NULL)
600 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_zero (), NULL , 0, NULL, NULL);
606 name_tmp = (char *) &msg[1];
607 if ((name_tmp[name_len -1] != '\0') || (name_len > 256))
612 rd_ser_tmp = (char *) &name_tmp[name_len];
613 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
614 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
620 if (ze->proc != NULL)
621 ze->proc(ze->proc_cls, &msg->public_key, expire, name_tmp, rd_count, rd, &msg->signature);
626 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
627 const struct GNUNET_MessageHeader *msg,
628 int type, size_t size)
631 /* handle different message type */
633 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
634 if (size < sizeof (struct ZoneIterationResponseMessage))
639 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
648 * Type of a function to call when we receive a message
651 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
652 * @param msg message received, NULL on timeout or fatal error
655 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
657 struct GNUNET_NAMESTORE_Handle *h = cls;
658 struct GNUNET_NAMESTORE_Header * gm;
659 struct GNUNET_NAMESTORE_QueueEntry *qe;
660 struct GNUNET_NAMESTORE_ZoneIterator *ze;
663 uint32_t r_id = UINT32_MAX;
671 size = ntohs (msg->size);
672 type = ntohs (msg->type);
674 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
677 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
678 GNUNET_TIME_UNIT_FOREVER_REL);
682 gm = (struct GNUNET_NAMESTORE_Header *) msg;
683 r_id = ntohl (gm->r_id);
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
687 /* Find matching operation */
690 /* No matching pending operation found */
692 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
693 GNUNET_TIME_UNIT_FOREVER_REL);
697 /* Is it a record related operation ? */
698 for (qe = h->op_head; qe != NULL; qe = qe->next)
700 if (qe->op_id == r_id)
705 manage_record_operations (qe, msg, type, size);
708 /* Is it a zone iteration operation ? */
709 for (ze = h->z_head; ze != NULL; ze = ze->next)
711 if (ze->op_id == r_id)
716 manage_zone_operations (ze, msg, type, size);
719 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
720 GNUNET_TIME_UNIT_FOREVER_REL);
722 if (GNUNET_YES == h->reconnect)
729 * Transmit messages from the message queue to the service
730 * (if there are any, and if we are not already trying).
732 * @param h handle to use
735 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
739 * We can now transmit a message to NAMESTORE. Do it.
741 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
742 * @param size number of bytes we can transmit
743 * @param buf where to copy the messages
744 * @return number of bytes copied into buf
747 transmit_message_to_namestore (void *cls, size_t size, void *buf)
749 struct GNUNET_NAMESTORE_Handle *h = cls;
750 struct PendingMessage *p;
755 if ((size == 0) || (buf == NULL))
762 while ((NULL != (p = h->pending_head)) && (p->size <= size))
764 memcpy (&cbuf[ret], &p[1], p->size);
767 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
768 if (GNUNET_YES == p->is_init)
769 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
770 GNUNET_TIME_UNIT_FOREVER_REL);
779 * Transmit messages from the message queue to the service
780 * (if there are any, and if we are not already trying).
782 * @param h handle to use
785 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
787 struct PendingMessage *p;
791 if (NULL == (p = h->pending_head))
793 if (NULL == h->client)
794 return; /* currently reconnecting */
796 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
797 GNUNET_TIME_UNIT_FOREVER_REL,
798 GNUNET_NO, &transmit_message_to_namestore,
804 * Reconnect to namestore service.
806 * @param h the handle to the namestore service
809 reconnect (struct GNUNET_NAMESTORE_Handle *h)
811 struct PendingMessage *p;
812 struct StartMessage *init;
814 GNUNET_assert (NULL == h->client);
815 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
816 GNUNET_assert (NULL != h->client);
818 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
820 p = GNUNET_malloc (sizeof (struct PendingMessage) +
821 sizeof (struct StartMessage));
822 p->size = sizeof (struct StartMessage);
823 p->is_init = GNUNET_YES;
824 init = (struct StartMessage *) &p[1];
825 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
826 init->header.size = htons (sizeof (struct StartMessage));
827 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
833 * Re-establish the connection to the service.
835 * @param cls handle to use to re-connect.
836 * @param tc scheduler context
839 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
841 struct GNUNET_NAMESTORE_Handle *h = cls;
843 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
849 * Disconnect from service and then reconnect.
851 * @param h our handle
854 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
856 h->reconnect = GNUNET_NO;
857 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
859 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
865 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
867 uint32_t op_id = h->op_id;
873 * Initialize the connection with the NAMESTORE service.
875 * @param cfg configuration to use
876 * @return handle to the GNS service, or NULL on error
878 struct GNUNET_NAMESTORE_Handle *
879 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
881 struct GNUNET_NAMESTORE_Handle *h;
883 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
885 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
891 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
893 struct PendingMessage *p;
894 struct GNUNET_NAMESTORE_QueueEntry *q;
895 struct GNUNET_NAMESTORE_ZoneIterator *z;
896 struct GNUNET_NAMESTORE_Handle *h = cls;
897 GNUNET_assert (h != NULL);
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
899 while (NULL != (p = h->pending_head))
901 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
905 while (NULL != (q = h->op_head))
907 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
911 while (NULL != (z = h->z_head))
913 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
917 if (NULL != h->client)
919 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
922 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
924 GNUNET_SCHEDULER_cancel (h->reconnect_task);
925 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
932 * Disconnect from the namestore service (and free associated
935 * @param h handle to the namestore
936 * @param drop set to GNUNET_YES to delete all data in namestore (!)
939 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from namestore service\n");
942 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
947 * Store an item in the namestore. If the item is already present,
948 * the expiration time is updated to the max of the existing time and
949 * the new time. This API is used when we cache signatures from other
952 * @param h handle to the namestore
953 * @param zone_key public key of the zone
954 * @param name name that is being mapped (at most 255 characters long)
955 * @param expire when does the corresponding block in the DHT expire (until
956 * when should we never do a DHT lookup for the same name again)?
957 * @param rd_count number of entries in 'rd' array
958 * @param rd array of records with data to store
959 * @param signature signature for all the records in the zone under the given name
960 * @param cont continuation to call when done
961 * @param cont_cls closure for cont
962 * @return handle to abort the request
964 struct GNUNET_NAMESTORE_QueueEntry *
965 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
966 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
968 struct GNUNET_TIME_Absolute expire,
969 unsigned int rd_count,
970 const struct GNUNET_NAMESTORE_RecordData *rd,
971 const struct GNUNET_CRYPTO_RsaSignature *signature,
972 GNUNET_NAMESTORE_ContinuationWithStatus cont,
975 struct GNUNET_NAMESTORE_QueueEntry *qe;
976 struct PendingMessage *pe;
978 /* pointer to elements */
984 size_t rd_ser_len = 0;
987 GNUNET_assert (NULL != h);
988 GNUNET_assert (NULL != zone_key);
989 GNUNET_assert (NULL != name);
990 GNUNET_assert (NULL != rd);
991 GNUNET_assert (NULL != signature);
993 name_len = strlen(name) + 1;
1001 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1004 qe->cont_cls = cont_cls;
1006 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1009 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1010 char rd_ser[rd_ser_len];
1011 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1013 struct RecordPutMessage * msg;
1014 msg_size = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
1016 /* create msg here */
1017 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1018 pe->size = msg_size;
1019 pe->is_init = GNUNET_NO;
1020 msg = (struct RecordPutMessage *) &pe[1];
1021 name_tmp = (char *) &msg[1];
1022 rd_tmp = &name_tmp[name_len];
1024 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
1025 msg->gns_header.header.size = htons (msg_size);
1026 msg->gns_header.r_id = htonl (rid);
1027 msg->signature = *signature;
1028 msg->name_len = htons (name_len);
1029 msg->expire = GNUNET_TIME_absolute_hton (expire);
1030 msg->rd_len = htons (rd_ser_len);
1031 msg->rd_count = htons (rd_count);
1033 msg->public_key = *zone_key;
1034 memcpy (name_tmp, name, name_len);
1035 memcpy (rd_tmp, rd_ser, rd_ser_len);
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1039 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1047 * Check if a signature is valid. This API is used by the GNS Block
1048 * to validate signatures received from the network.
1050 * @param public_key public key of the zone
1051 * @param name name that is being mapped (at most 255 characters long)
1052 * @param rd_count number of entries in 'rd' array
1053 * @param rd array of records with data to store
1054 * @param signature signature for all the records in the zone under the given name
1055 * @return GNUNET_OK if the signature is valid
1058 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1060 unsigned int rd_count,
1061 const struct GNUNET_NAMESTORE_RecordData *rd,
1062 const struct GNUNET_CRYPTO_RsaSignature *signature)
1064 int res = GNUNET_SYSERR;
1065 size_t rd_ser_len = 0;
1066 size_t name_len = 0;
1069 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1071 GNUNET_assert (public_key != NULL);
1072 GNUNET_assert (name != NULL);
1073 GNUNET_assert (rd != NULL);
1074 GNUNET_assert (signature != NULL);
1077 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1078 char rd_ser[rd_ser_len];
1079 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1081 name_len = strlen (name) + 1;
1085 return GNUNET_SYSERR;
1088 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1089 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1090 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1091 name_tmp = (char *) &sig_purpose[1];
1092 rd_tmp = &name_tmp[name_len];
1093 memcpy (name_tmp, name, name_len);
1094 memcpy (rd_tmp, rd_ser, rd_ser_len);
1096 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1098 GNUNET_free (sig_purpose);
1104 * Store an item in the namestore. If the item is already present,
1105 * the expiration time is updated to the max of the existing time and
1106 * the new time. This API is used by the authority of a zone.
1108 * @param h handle to the namestore
1109 * @param pkey private key of the zone
1110 * @param expire block expiration time
1111 * @param name name that is being mapped (at most 255 characters long)
1112 * @param rd record data to store
1113 * @param cont continuation to call when done
1114 * @param cont_cls closure for cont
1115 * @return handle to abort the request
1117 struct GNUNET_NAMESTORE_QueueEntry *
1118 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1119 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1121 const struct GNUNET_NAMESTORE_RecordData *rd,
1122 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1125 struct GNUNET_NAMESTORE_QueueEntry *qe;
1126 struct PendingMessage *pe;
1130 size_t rd_ser_len = 0;
1131 size_t msg_size = 0;
1132 size_t name_len = 0;
1136 GNUNET_assert (NULL != h);
1137 GNUNET_assert (NULL != pkey);
1138 GNUNET_assert (NULL != name);
1139 GNUNET_assert (NULL != rd);
1141 name_len = strlen(name) + 1;
1149 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1152 qe->cont_cls = cont_cls;
1154 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1157 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1158 GNUNET_assert (pkey_enc != NULL);
1159 key_len = ntohs (pkey_enc->len);
1161 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1162 char rd_ser[rd_ser_len];
1163 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1165 struct RecordCreateMessage * msg;
1166 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1168 /* create msg here */
1169 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1170 pe->size = msg_size;
1171 pe->is_init = GNUNET_NO;
1172 msg = (struct RecordCreateMessage *) &pe[1];
1174 pkey_tmp = (char *) &msg[1];
1175 name_tmp = &pkey_tmp[key_len];
1176 rd_tmp = &name_tmp[name_len];
1178 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1179 msg->gns_header.header.size = htons (msg_size);
1180 msg->gns_header.r_id = htonl (rid);
1181 msg->name_len = htons (name_len);
1182 msg->rd_count = htons (1);
1183 msg->rd_len = htons (rd_ser_len);
1184 msg->pkey_len = htons (key_len);
1185 msg->expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever());
1186 memcpy (pkey_tmp, pkey_enc, key_len);
1187 memcpy (name_tmp, name, name_len);
1188 memcpy (rd_tmp, rd_ser, rd_ser_len);
1189 GNUNET_free (pkey_enc);
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1193 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1200 * Explicitly remove some content from the database. The
1201 * "cont"inuation will be called with status "GNUNET_OK" if content
1202 * was removed, "GNUNET_NO" if no matching entry was found and
1203 * "GNUNET_SYSERR" on all other types of errors.
1204 * This API is used by the authority of a zone.
1206 * @param h handle to the namestore
1207 * @param pkey private key of the zone
1208 * @param name name that is being mapped (at most 255 characters long)
1209 * @param rd record data
1210 * @param cont continuation to call when done
1211 * @param cont_cls closure for cont
1212 * @return handle to abort the request
1214 struct GNUNET_NAMESTORE_QueueEntry *
1215 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1216 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1218 const struct GNUNET_NAMESTORE_RecordData *rd,
1219 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1222 struct GNUNET_NAMESTORE_QueueEntry *qe;
1223 struct PendingMessage *pe;
1227 size_t rd_ser_len = 0;
1228 size_t msg_size = 0;
1229 size_t name_len = 0;
1233 GNUNET_assert (NULL != h);
1236 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1239 qe->cont_cls = cont_cls;
1241 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1244 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1245 GNUNET_assert (pkey_enc != NULL);
1246 key_len = ntohs (pkey_enc->len);
1248 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1249 char rd_ser[rd_ser_len];
1250 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1252 name_len = strlen (name) + 1;
1254 struct RecordRemoveMessage * msg;
1255 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1257 /* create msg here */
1258 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1259 pe->size = msg_size;
1260 pe->is_init = GNUNET_NO;
1261 msg = (struct RecordRemoveMessage *) &pe[1];
1263 pkey_tmp = (char *) &msg[1];
1264 name_tmp = &pkey_tmp[key_len];
1265 rd_tmp = &name_tmp[name_len];
1267 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1268 msg->gns_header.header.size = htons (msg_size);
1269 msg->gns_header.r_id = htonl (rid);
1270 msg->name_len = htons (name_len);
1271 msg->rd_len = htons (rd_ser_len);
1272 msg->rd_count = htons (1);
1273 msg->pkey_len = htons (key_len);
1274 memcpy (pkey_tmp, pkey_enc, key_len);
1275 memcpy (name_tmp, name, name_len);
1276 memcpy (rd_tmp, rd_ser, rd_ser_len);
1278 GNUNET_free (pkey_enc);
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1282 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1289 * Get a result for a particular key from the namestore. The processor
1290 * will only be called once.
1292 * @param h handle to the namestore
1293 * @param zone zone to look up a record from
1294 * @param name name to look up
1295 * @param record_type desired record type, 0 for all
1296 * @param proc function to call on the matching records, or with
1297 * NULL (rd_count == 0) if there are no matching records
1298 * @param proc_cls closure for proc
1299 * @return a handle that can be used to
1302 struct GNUNET_NAMESTORE_QueueEntry *
1303 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1304 const GNUNET_HashCode *zone,
1306 uint32_t record_type,
1307 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1309 struct GNUNET_NAMESTORE_QueueEntry *qe;
1310 struct PendingMessage *pe;
1311 size_t msg_size = 0;
1312 size_t name_len = 0;
1315 GNUNET_assert (NULL != h);
1316 GNUNET_assert (NULL != zone);
1317 GNUNET_assert (NULL != name);
1319 name_len = strlen (name) + 1;
1320 if ((name_len == 0) || (name_len > 256))
1327 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1330 qe->proc_cls = proc_cls;
1332 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1335 msg_size = sizeof (struct LookupNameMessage) + name_len;
1336 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1338 /* create msg here */
1339 struct LookupNameMessage * msg;
1340 pe->size = msg_size;
1341 pe->is_init = GNUNET_NO;
1342 msg = (struct LookupNameMessage *) &pe[1];
1343 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1344 msg->gns_header.header.size = htons (msg_size);
1345 msg->gns_header.r_id = htonl (rid);
1346 msg->record_type = htonl (record_type);
1347 msg->name_len = htonl (name_len);
1349 memcpy (&msg[1], name, name_len);
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1353 /* transmit message */
1354 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1362 * Look for an existing PKEY delegation record for a given public key.
1363 * Returns at most one result to the processor.
1365 * @param h handle to the namestore
1366 * @param zone hash of public key of the zone to look up in, never NULL
1367 * @param value_zone hash of the public key of the target zone (value), never NULL
1368 * @param proc function to call on the matching records, or with
1369 * NULL (rd_count == 0) if there are no matching records
1370 * @param proc_cls closure for proc
1371 * @return a handle that can be used to
1374 struct GNUNET_NAMESTORE_QueueEntry *
1375 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1376 const GNUNET_HashCode *zone,
1377 const GNUNET_HashCode *value_zone,
1378 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1380 struct GNUNET_NAMESTORE_QueueEntry *qe;
1381 struct PendingMessage *pe;
1382 size_t msg_size = 0;
1385 GNUNET_assert (NULL != h);
1386 GNUNET_assert (NULL != zone);
1387 GNUNET_assert (NULL != value_zone);
1390 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1393 qe->proc_cls = proc_cls;
1395 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1398 msg_size = sizeof (struct ZoneToNameMessage);
1399 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1401 /* create msg here */
1402 struct ZoneToNameMessage * msg;
1403 pe->size = msg_size;
1404 pe->is_init = GNUNET_NO;
1405 msg = (struct ZoneToNameMessage *) &pe[1];
1406 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1407 msg->gns_header.header.size = htons (msg_size);
1408 msg->gns_header.r_id = htonl (rid);
1410 msg->value_zone = *value_zone;
1412 char * z_tmp = strdup (GNUNET_h2s (zone));
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1414 "NAMESTORE_ZONE_TO_NAME",
1416 GNUNET_h2s (value_zone));
1417 GNUNET_free (z_tmp);
1419 /* transmit message */
1420 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1429 * Starts a new zone iteration (used to periodically PUT all of our
1430 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1431 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1432 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1433 * immediately, and then again after
1434 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1436 * @param h handle to the namestore
1437 * @param zone zone to access, NULL for all zones
1438 * @param must_have_flags flags that must be set for the record to be returned
1439 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1440 * @param proc function to call on each name from the zone; it
1441 * will be called repeatedly with a value (if available)
1442 * and always once at the end with a name of NULL.
1443 * @param proc_cls closure for proc
1444 * @return an iterator handle to use for iteration
1446 struct GNUNET_NAMESTORE_ZoneIterator *
1447 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1448 const GNUNET_HashCode *zone,
1449 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1450 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1451 GNUNET_NAMESTORE_RecordProcessor proc,
1454 struct GNUNET_NAMESTORE_ZoneIterator *it;
1455 struct PendingMessage *pe;
1456 size_t msg_size = 0;
1459 GNUNET_assert (NULL != h);
1463 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1466 it->proc_cls = proc;
1472 it->has_zone = GNUNET_YES;
1476 memset (&it->zone, '\0', sizeof (it->zone));
1477 it->has_zone = GNUNET_NO;
1479 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1482 msg_size = sizeof (struct ZoneIterationStartMessage);
1483 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1485 /* create msg here */
1486 struct ZoneIterationStartMessage * msg;
1487 pe->size = msg_size;
1488 pe->is_init = GNUNET_NO;
1489 msg = (struct ZoneIterationStartMessage *) &pe[1];
1490 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1491 msg->gns_header.header.size = htons (msg_size);
1492 msg->gns_header.r_id = htonl (rid);
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_START");
1501 memset (&msg->zone, '\0', sizeof (msg->zone));
1503 msg->must_have_flags = ntohs (must_have_flags);
1504 msg->must_not_have_flags = ntohs (must_not_have_flags);
1508 /* transmit message */
1509 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1517 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1518 * for the next record.
1520 * @param it the iterator
1523 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1525 struct GNUNET_NAMESTORE_Handle *h;
1526 struct PendingMessage *pe;
1527 size_t msg_size = 0;
1529 GNUNET_assert (NULL != it);
1533 msg_size = sizeof (struct ZoneIterationNextMessage);
1534 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1536 /* create msg here */
1537 struct ZoneIterationNextMessage * msg;
1538 pe->size = msg_size;
1539 pe->is_init = GNUNET_NO;
1540 msg = (struct ZoneIterationNextMessage *) &pe[1];
1541 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1542 msg->gns_header.header.size = htons (msg_size);
1543 msg->gns_header.r_id = htonl (it->op_id);
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_NEXT");
1547 /* transmit message */
1548 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1554 * Stops iteration and releases the namestore handle for further calls.
1556 * @param it the iterator
1559 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1561 GNUNET_assert (NULL != it);
1562 struct PendingMessage *pe;
1563 size_t msg_size = 0;
1564 struct GNUNET_NAMESTORE_Handle *h = it->h;
1567 msg_size = sizeof (struct ZoneIterationStopMessage);
1568 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1570 /* create msg here */
1571 struct ZoneIterationStopMessage * msg;
1572 pe->size = msg_size;
1573 pe->is_init = GNUNET_NO;
1574 msg = (struct ZoneIterationStopMessage *) &pe[1];
1575 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1576 msg->gns_header.header.size = htons (msg_size);
1577 msg->gns_header.r_id = htonl (it->op_id);
1579 if (GNUNET_YES == it->has_zone)
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_STOP");
1584 /* transmit message */
1585 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1591 * Cancel a namestore operation. The final callback from the
1592 * operation must not have been done yet.
1594 * @param qe operation to cancel
1597 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1599 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1601 GNUNET_assert (qe != NULL);
1603 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1608 /* end of namestore_api.c */