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,
1109 const struct GNUNET_NAMESTORE_RecordData *rd,
1110 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1113 struct GNUNET_NAMESTORE_QueueEntry *qe;
1114 struct PendingMessage *pe;
1118 size_t rd_ser_len = 0;
1119 size_t msg_size = 0;
1120 size_t name_len = 0;
1124 GNUNET_assert (NULL != h);
1125 GNUNET_assert (NULL != pkey);
1126 GNUNET_assert (NULL != name);
1127 GNUNET_assert (NULL != rd);
1129 name_len = strlen(name) + 1;
1137 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1140 qe->cont_cls = cont_cls;
1142 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1145 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1146 GNUNET_assert (pkey_enc != NULL);
1147 key_len = ntohs (pkey_enc->len);
1149 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1150 char rd_ser[rd_ser_len];
1151 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1153 struct RecordCreateMessage * msg;
1154 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1156 /* create msg here */
1157 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1158 pe->size = msg_size;
1159 pe->is_init = GNUNET_NO;
1160 msg = (struct RecordCreateMessage *) &pe[1];
1162 pkey_tmp = (char *) &msg[1];
1163 name_tmp = &pkey_tmp[key_len];
1164 rd_tmp = &name_tmp[name_len];
1166 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1167 msg->gns_header.header.size = htons (msg_size);
1168 msg->gns_header.r_id = htonl (rid);
1169 msg->name_len = htons (name_len);
1170 msg->rd_count = htons (1);
1171 msg->rd_len = htons (rd_ser_len);
1172 msg->pkey_len = htons (key_len);
1173 msg->expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever());
1174 memcpy (pkey_tmp, pkey_enc, key_len);
1175 memcpy (name_tmp, name, name_len);
1176 memcpy (rd_tmp, rd_ser, rd_ser_len);
1177 GNUNET_free (pkey_enc);
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1181 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1188 * Explicitly remove some content from the database. The
1189 * "cont"inuation will be called with status "GNUNET_OK" if content
1190 * was removed, "GNUNET_NO" if no matching entry was found and
1191 * "GNUNET_SYSERR" on all other types of errors.
1192 * This API is used by the authority of a zone.
1194 * @param h handle to the namestore
1195 * @param pkey private key of the zone
1196 * @param name name that is being mapped (at most 255 characters long)
1197 * @param rd record data
1198 * @param cont continuation to call when done
1199 * @param cont_cls closure for cont
1200 * @return handle to abort the request
1202 struct GNUNET_NAMESTORE_QueueEntry *
1203 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1204 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1206 const struct GNUNET_NAMESTORE_RecordData *rd,
1207 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1210 struct GNUNET_NAMESTORE_QueueEntry *qe;
1211 struct PendingMessage *pe;
1215 size_t rd_ser_len = 0;
1216 size_t msg_size = 0;
1217 size_t name_len = 0;
1221 GNUNET_assert (NULL != h);
1224 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1227 qe->cont_cls = cont_cls;
1229 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1232 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1233 GNUNET_assert (pkey_enc != NULL);
1234 key_len = ntohs (pkey_enc->len);
1236 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1237 char rd_ser[rd_ser_len];
1238 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1240 name_len = strlen (name) + 1;
1242 struct RecordRemoveMessage * msg;
1243 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1245 /* create msg here */
1246 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1247 pe->size = msg_size;
1248 pe->is_init = GNUNET_NO;
1249 msg = (struct RecordRemoveMessage *) &pe[1];
1251 pkey_tmp = (char *) &msg[1];
1252 name_tmp = &pkey_tmp[key_len];
1253 rd_tmp = &name_tmp[name_len];
1255 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1256 msg->gns_header.header.size = htons (msg_size);
1257 msg->gns_header.r_id = htonl (rid);
1258 msg->name_len = htons (name_len);
1259 msg->rd_len = htons (rd_ser_len);
1260 msg->rd_count = htons (1);
1261 msg->pkey_len = htons (key_len);
1262 memcpy (pkey_tmp, pkey_enc, key_len);
1263 memcpy (name_tmp, name, name_len);
1264 memcpy (rd_tmp, rd_ser, rd_ser_len);
1266 GNUNET_free (pkey_enc);
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1270 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1277 * Get a result for a particular key from the namestore. The processor
1278 * will only be called once.
1280 * @param h handle to the namestore
1281 * @param zone zone to look up a record from
1282 * @param name name to look up
1283 * @param record_type desired record type, 0 for all
1284 * @param proc function to call on the matching records, or with
1285 * NULL (rd_count == 0) if there are no matching records
1286 * @param proc_cls closure for proc
1287 * @return a handle that can be used to
1290 struct GNUNET_NAMESTORE_QueueEntry *
1291 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1292 const GNUNET_HashCode *zone,
1294 uint32_t record_type,
1295 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1297 struct GNUNET_NAMESTORE_QueueEntry *qe;
1298 struct PendingMessage *pe;
1299 size_t msg_size = 0;
1300 size_t name_len = 0;
1303 GNUNET_assert (NULL != h);
1304 GNUNET_assert (NULL != zone);
1305 GNUNET_assert (NULL != name);
1307 name_len = strlen (name) + 1;
1308 if ((name_len == 0) || (name_len > 256))
1315 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1318 qe->proc_cls = proc_cls;
1320 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1323 msg_size = sizeof (struct LookupNameMessage) + name_len;
1324 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1326 /* create msg here */
1327 struct LookupNameMessage * msg;
1328 pe->size = msg_size;
1329 pe->is_init = GNUNET_NO;
1330 msg = (struct LookupNameMessage *) &pe[1];
1331 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1332 msg->gns_header.header.size = htons (msg_size);
1333 msg->gns_header.r_id = htonl (rid);
1334 msg->record_type = htonl (record_type);
1335 msg->name_len = htonl (name_len);
1337 memcpy (&msg[1], name, name_len);
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1341 /* transmit message */
1342 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1350 * Look for an existing PKEY delegation record for a given public key.
1351 * Returns at most one result to the processor.
1353 * @param h handle to the namestore
1354 * @param zone hash of public key of the zone to look up in, never NULL
1355 * @param value_zone hash of the public key of the target zone (value), never NULL
1356 * @param proc function to call on the matching records, or with
1357 * NULL (rd_count == 0) if there are no matching records
1358 * @param proc_cls closure for proc
1359 * @return a handle that can be used to
1362 struct GNUNET_NAMESTORE_QueueEntry *
1363 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1364 const GNUNET_HashCode *zone,
1365 const GNUNET_HashCode *value_zone,
1366 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1368 struct GNUNET_NAMESTORE_QueueEntry *qe;
1369 struct PendingMessage *pe;
1370 size_t msg_size = 0;
1373 GNUNET_assert (NULL != h);
1374 GNUNET_assert (NULL != zone);
1375 GNUNET_assert (NULL != value_zone);
1378 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1381 qe->proc_cls = proc_cls;
1383 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1386 msg_size = sizeof (struct ZoneToNameMessage);
1387 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1389 /* create msg here */
1390 struct ZoneToNameMessage * msg;
1391 pe->size = msg_size;
1392 pe->is_init = GNUNET_NO;
1393 msg = (struct ZoneToNameMessage *) &pe[1];
1394 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1395 msg->gns_header.header.size = htons (msg_size);
1396 msg->gns_header.r_id = htonl (rid);
1398 msg->value_zone = *value_zone;
1400 char * z_tmp = strdup (GNUNET_h2s (zone));
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1402 "NAMESTORE_ZONE_TO_NAME",
1404 GNUNET_h2s (value_zone));
1405 GNUNET_free (z_tmp);
1407 /* transmit message */
1408 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1417 * Starts a new zone iteration (used to periodically PUT all of our
1418 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1419 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1420 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1421 * immediately, and then again after
1422 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1424 * @param h handle to the namestore
1425 * @param zone zone to access, NULL for all zones
1426 * @param must_have_flags flags that must be set for the record to be returned
1427 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1428 * @param proc function to call on each name from the zone; it
1429 * will be called repeatedly with a value (if available)
1430 * and always once at the end with a name of NULL.
1431 * @param proc_cls closure for proc
1432 * @return an iterator handle to use for iteration
1434 struct GNUNET_NAMESTORE_ZoneIterator *
1435 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1436 const GNUNET_HashCode *zone,
1437 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1438 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1439 GNUNET_NAMESTORE_RecordProcessor proc,
1442 struct GNUNET_NAMESTORE_ZoneIterator *it;
1443 struct PendingMessage *pe;
1444 size_t msg_size = 0;
1447 GNUNET_assert (NULL != h);
1451 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1454 it->proc_cls = proc;
1460 it->has_zone = GNUNET_YES;
1464 memset (&it->zone, '\0', sizeof (it->zone));
1465 it->has_zone = GNUNET_NO;
1467 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1470 msg_size = sizeof (struct ZoneIterationStartMessage);
1471 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1473 /* create msg here */
1474 struct ZoneIterationStartMessage * msg;
1475 pe->size = msg_size;
1476 pe->is_init = GNUNET_NO;
1477 msg = (struct ZoneIterationStartMessage *) &pe[1];
1478 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1479 msg->gns_header.header.size = htons (msg_size);
1480 msg->gns_header.r_id = htonl (rid);
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_START");
1489 memset (&msg->zone, '\0', sizeof (msg->zone));
1491 msg->must_have_flags = ntohs (must_have_flags);
1492 msg->must_not_have_flags = ntohs (must_not_have_flags);
1496 /* transmit message */
1497 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1505 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1506 * for the next record.
1508 * @param it the iterator
1511 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1513 struct GNUNET_NAMESTORE_Handle *h;
1514 struct PendingMessage *pe;
1515 size_t msg_size = 0;
1517 GNUNET_assert (NULL != it);
1521 msg_size = sizeof (struct ZoneIterationNextMessage);
1522 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1524 /* create msg here */
1525 struct ZoneIterationNextMessage * msg;
1526 pe->size = msg_size;
1527 pe->is_init = GNUNET_NO;
1528 msg = (struct ZoneIterationNextMessage *) &pe[1];
1529 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1530 msg->gns_header.header.size = htons (msg_size);
1531 msg->gns_header.r_id = htonl (it->op_id);
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_NEXT");
1535 /* transmit message */
1536 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1542 * Stops iteration and releases the namestore handle for further calls.
1544 * @param it the iterator
1547 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1549 GNUNET_assert (NULL != it);
1550 struct PendingMessage *pe;
1551 size_t msg_size = 0;
1552 struct GNUNET_NAMESTORE_Handle *h = it->h;
1555 msg_size = sizeof (struct ZoneIterationStopMessage);
1556 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1558 /* create msg here */
1559 struct ZoneIterationStopMessage * msg;
1560 pe->size = msg_size;
1561 pe->is_init = GNUNET_NO;
1562 msg = (struct ZoneIterationStopMessage *) &pe[1];
1563 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1564 msg->gns_header.header.size = htons (msg_size);
1565 msg->gns_header.r_id = htonl (it->op_id);
1567 if (GNUNET_YES == it->has_zone)
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_STOP");
1572 /* transmit message */
1573 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1579 * Cancel a namestore operation. The final callback from the
1580 * operation must not have been done yet.
1582 * @param qe operation to cancel
1585 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1587 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1589 GNUNET_assert (qe != NULL);
1591 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1596 /* end of namestore_api.c */