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 name name that is being mapped (at most 255 characters long)
1099 * @param rd record data to store
1100 * @param cont continuation to call when done
1101 * @param cont_cls closure for cont
1102 * @return handle to abort the request
1104 struct GNUNET_NAMESTORE_QueueEntry *
1105 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1106 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1108 const struct GNUNET_NAMESTORE_RecordData *rd,
1109 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1112 struct GNUNET_NAMESTORE_QueueEntry *qe;
1113 struct PendingMessage *pe;
1117 size_t rd_ser_len = 0;
1118 size_t msg_size = 0;
1119 size_t name_len = 0;
1123 GNUNET_assert (NULL != h);
1124 GNUNET_assert (NULL != pkey);
1125 GNUNET_assert (NULL != name);
1126 GNUNET_assert (NULL != rd);
1128 name_len = strlen(name) + 1;
1136 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1139 qe->cont_cls = cont_cls;
1141 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1144 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1145 GNUNET_assert (pkey_enc != NULL);
1146 key_len = ntohs (pkey_enc->len);
1148 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1149 char rd_ser[rd_ser_len];
1150 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1152 struct RecordCreateMessage * msg;
1153 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1155 /* create msg here */
1156 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1157 pe->size = msg_size;
1158 pe->is_init = GNUNET_NO;
1159 msg = (struct RecordCreateMessage *) &pe[1];
1161 pkey_tmp = (char *) &msg[1];
1162 name_tmp = &pkey_tmp[key_len];
1163 rd_tmp = &name_tmp[name_len];
1165 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1166 msg->gns_header.header.size = htons (msg_size);
1167 msg->gns_header.r_id = htonl (rid);
1168 msg->name_len = htons (name_len);
1169 msg->rd_count = htons (1);
1170 msg->rd_len = htons (rd_ser_len);
1171 msg->pkey_len = htons (key_len);
1172 memcpy (pkey_tmp, pkey_enc, key_len);
1173 memcpy (name_tmp, name, name_len);
1174 memcpy (rd_tmp, rd_ser, rd_ser_len);
1175 GNUNET_free (pkey_enc);
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1179 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1186 * Explicitly remove some content from the database. The
1187 * "cont"inuation will be called with status "GNUNET_OK" if content
1188 * was removed, "GNUNET_NO" if no matching entry was found and
1189 * "GNUNET_SYSERR" on all other types of errors.
1190 * This API is used by the authority of a zone.
1192 * @param h handle to the namestore
1193 * @param pkey private key of the zone
1194 * @param name name that is being mapped (at most 255 characters long)
1195 * @param rd record data
1196 * @param cont continuation to call when done
1197 * @param cont_cls closure for cont
1198 * @return handle to abort the request
1200 struct GNUNET_NAMESTORE_QueueEntry *
1201 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1202 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1204 const struct GNUNET_NAMESTORE_RecordData *rd,
1205 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1208 struct GNUNET_NAMESTORE_QueueEntry *qe;
1209 struct PendingMessage *pe;
1213 size_t rd_ser_len = 0;
1214 size_t msg_size = 0;
1215 size_t name_len = 0;
1219 GNUNET_assert (NULL != h);
1222 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1225 qe->cont_cls = cont_cls;
1227 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1230 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1231 GNUNET_assert (pkey_enc != NULL);
1232 key_len = ntohs (pkey_enc->len);
1234 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1235 char rd_ser[rd_ser_len];
1236 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1238 name_len = strlen (name) + 1;
1240 struct RecordRemoveMessage * msg;
1241 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1243 /* create msg here */
1244 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1245 pe->size = msg_size;
1246 pe->is_init = GNUNET_NO;
1247 msg = (struct RecordRemoveMessage *) &pe[1];
1249 pkey_tmp = (char *) &msg[1];
1250 name_tmp = &pkey_tmp[key_len];
1251 rd_tmp = &name_tmp[name_len];
1253 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1254 msg->gns_header.header.size = htons (msg_size);
1255 msg->gns_header.r_id = htonl (rid);
1256 msg->name_len = htons (name_len);
1257 msg->rd_len = htons (rd_ser_len);
1258 msg->rd_count = htons (1);
1259 msg->pkey_len = htons (key_len);
1260 memcpy (pkey_tmp, pkey_enc, key_len);
1261 memcpy (name_tmp, name, name_len);
1262 memcpy (rd_tmp, rd_ser, rd_ser_len);
1264 GNUNET_free (pkey_enc);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1268 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1275 * Get a result for a particular key from the namestore. The processor
1276 * will only be called once.
1278 * @param h handle to the namestore
1279 * @param zone zone to look up a record from
1280 * @param name name to look up
1281 * @param record_type desired record type, 0 for all
1282 * @param proc function to call on the matching records, or with
1283 * NULL (rd_count == 0) if there are no matching records
1284 * @param proc_cls closure for proc
1285 * @return a handle that can be used to
1288 struct GNUNET_NAMESTORE_QueueEntry *
1289 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1290 const GNUNET_HashCode *zone,
1292 uint32_t record_type,
1293 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1295 struct GNUNET_NAMESTORE_QueueEntry *qe;
1296 struct PendingMessage *pe;
1297 size_t msg_size = 0;
1298 size_t name_len = 0;
1301 GNUNET_assert (NULL != h);
1302 GNUNET_assert (NULL != zone);
1303 GNUNET_assert (NULL != name);
1305 name_len = strlen (name) + 1;
1306 if ((name_len == 0) || (name_len > 256))
1313 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1316 qe->proc_cls = proc_cls;
1318 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1321 msg_size = sizeof (struct LookupNameMessage) + name_len;
1322 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1324 /* create msg here */
1325 struct LookupNameMessage * msg;
1326 pe->size = msg_size;
1327 pe->is_init = GNUNET_NO;
1328 msg = (struct LookupNameMessage *) &pe[1];
1329 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1330 msg->gns_header.header.size = htons (msg_size);
1331 msg->gns_header.r_id = htonl (rid);
1332 msg->record_type = htonl (record_type);
1333 msg->name_len = htonl (name_len);
1335 memcpy (&msg[1], name, name_len);
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1339 /* transmit message */
1340 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1348 * Look for an existing PKEY delegation record for a given public key.
1349 * Returns at most one result to the processor.
1351 * @param h handle to the namestore
1352 * @param zone hash of public key of the zone to look up in, never NULL
1353 * @param value_zone hash of the public key of the target zone (value), never NULL
1354 * @param proc function to call on the matching records, or with
1355 * NULL (rd_count == 0) if there are no matching records
1356 * @param proc_cls closure for proc
1357 * @return a handle that can be used to
1360 struct GNUNET_NAMESTORE_QueueEntry *
1361 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1362 const GNUNET_HashCode *zone,
1363 const GNUNET_HashCode *value_zone,
1364 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1366 struct GNUNET_NAMESTORE_QueueEntry *qe;
1367 struct PendingMessage *pe;
1368 size_t msg_size = 0;
1371 GNUNET_assert (NULL != h);
1372 GNUNET_assert (NULL != zone);
1373 GNUNET_assert (NULL != value_zone);
1376 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1379 qe->proc_cls = proc_cls;
1381 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1384 msg_size = sizeof (struct ZoneToNameMessage);
1385 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1387 /* create msg here */
1388 struct ZoneToNameMessage * msg;
1389 pe->size = msg_size;
1390 pe->is_init = GNUNET_NO;
1391 msg = (struct ZoneToNameMessage *) &pe[1];
1392 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1393 msg->gns_header.header.size = htons (msg_size);
1394 msg->gns_header.r_id = htonl (rid);
1396 msg->value_zone = *value_zone;
1398 char * z_tmp = strdup (GNUNET_h2s (zone));
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1400 "NAMESTORE_ZONE_TO_NAME",
1402 GNUNET_h2s (value_zone));
1403 GNUNET_free (z_tmp);
1405 /* transmit message */
1406 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1415 * Starts a new zone iteration (used to periodically PUT all of our
1416 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1417 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1418 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1419 * immediately, and then again after
1420 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1422 * @param h handle to the namestore
1423 * @param zone zone to access, NULL for all zones
1424 * @param must_have_flags flags that must be set for the record to be returned
1425 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1426 * @param proc function to call on each name from the zone; it
1427 * will be called repeatedly with a value (if available)
1428 * and always once at the end with a name of NULL.
1429 * @param proc_cls closure for proc
1430 * @return an iterator handle to use for iteration
1432 struct GNUNET_NAMESTORE_ZoneIterator *
1433 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1434 const GNUNET_HashCode *zone,
1435 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1436 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1437 GNUNET_NAMESTORE_RecordProcessor proc,
1440 struct GNUNET_NAMESTORE_ZoneIterator *it;
1441 struct PendingMessage *pe;
1442 size_t msg_size = 0;
1445 GNUNET_assert (NULL != h);
1449 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1452 it->proc_cls = proc;
1458 it->has_zone = GNUNET_YES;
1462 memset (&it->zone, '\0', sizeof (it->zone));
1463 it->has_zone = GNUNET_NO;
1465 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1468 msg_size = sizeof (struct ZoneIterationStartMessage);
1469 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1471 /* create msg here */
1472 struct ZoneIterationStartMessage * msg;
1473 pe->size = msg_size;
1474 pe->is_init = GNUNET_NO;
1475 msg = (struct ZoneIterationStartMessage *) &pe[1];
1476 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1477 msg->gns_header.header.size = htons (msg_size);
1478 msg->gns_header.r_id = htonl (rid);
1482 memset (&msg->zone, '\0', sizeof (msg->zone));
1483 msg->must_have_flags = ntohs (must_have_flags);
1484 msg->must_not_have_flags = ntohs (must_not_have_flags);
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1488 /* transmit message */
1489 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1497 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1498 * for the next record.
1500 * @param it the iterator
1503 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1505 struct GNUNET_NAMESTORE_Handle *h;
1506 struct PendingMessage *pe;
1507 size_t msg_size = 0;
1509 GNUNET_assert (NULL != it);
1513 msg_size = sizeof (struct ZoneIterationNextMessage);
1514 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1516 /* create msg here */
1517 struct ZoneIterationNextMessage * msg;
1518 pe->size = msg_size;
1519 pe->is_init = GNUNET_NO;
1520 msg = (struct ZoneIterationNextMessage *) &pe[1];
1521 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1522 msg->gns_header.header.size = htons (msg_size);
1523 msg->gns_header.r_id = htonl (it->op_id);
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1527 /* transmit message */
1528 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1534 * Stops iteration and releases the namestore handle for further calls.
1536 * @param it the iterator
1539 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1541 GNUNET_assert (NULL != it);
1542 struct PendingMessage *pe;
1543 size_t msg_size = 0;
1544 struct GNUNET_NAMESTORE_Handle *h = it->h;
1547 msg_size = sizeof (struct ZoneIterationStopMessage);
1548 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1550 /* create msg here */
1551 struct ZoneIterationStopMessage * msg;
1552 pe->size = msg_size;
1553 pe->is_init = GNUNET_NO;
1554 msg = (struct ZoneIterationStopMessage *) &pe[1];
1555 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1556 msg->gns_header.header.size = htons (msg_size);
1557 msg->gns_header.r_id = htonl (it->op_id);
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1561 /* transmit message */
1562 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1568 * Cancel a namestore operation. The final callback from the
1569 * operation must not have been done yet.
1571 * @param qe operation to cancel
1574 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1576 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1578 GNUNET_assert (qe != NULL);
1580 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1585 /* end of namestore_api.c */