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 GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
267 /* reset values if values not contained */
268 if (contains_sig == GNUNET_NO)
271 signature = &msg->signature;
276 public_key_tmp = &msg->public_key;
278 public_key_tmp = NULL;
280 if (qe->proc != NULL)
282 qe->proc (qe->proc_cls, public_key_tmp, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
289 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
290 struct RecordPutResponseMessage* msg,
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
294 "RECORD_PUT_RESPONSE");
296 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
297 /* Operation done, remove */
298 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
300 int res = ntohl (msg->op_result);
302 if (res == GNUNET_OK)
304 if (qe->cont != NULL)
306 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
310 else if (res == GNUNET_SYSERR)
312 if (qe->cont != NULL)
314 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
328 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
329 struct RecordCreateResponseMessage* msg,
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
333 "RECORD_CREATE_RESPONSE");
335 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
336 /* Operation done, remove */
337 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
339 int res = ntohl (msg->op_result);
340 if (res == GNUNET_YES)
342 if (qe->cont != NULL)
344 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
348 else if (res == GNUNET_NO)
350 if (qe->cont != NULL)
352 qe->cont (qe->cont_cls, res, _("Namestore record already existed"));
357 if (qe->cont != NULL)
359 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
368 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
369 struct RecordRemoveResponseMessage* msg,
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
373 "RECORD_REMOVE_RESPONSE");
375 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
376 /* Operation done, remove */
377 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
380 int res = ntohl (msg->op_result);
384 * 1 : No records for entry
385 * 2 : Could not find record to remove
386 * 3 : Failed to create new signature
387 * 4 : Failed to put new set of records in database
391 if (qe->cont != NULL)
393 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
398 if (qe->cont != NULL)
400 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
405 if (qe->cont != NULL)
407 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
412 if (qe->cont != NULL)
414 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
419 if (qe->cont != NULL)
421 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
433 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
434 struct ZoneToNameResponseMessage* msg,
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
438 "ZONE_TO_NAME_RESPONSE");
440 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
441 /* Operation done, remove */
442 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
444 int res = ntohs (msg->res);
446 struct GNUNET_TIME_Absolute expire;
449 unsigned int rd_count;
454 if (res == GNUNET_SYSERR)
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\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_NO)
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
463 if (qe->proc != NULL)
464 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
466 else if (res == GNUNET_YES)
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
470 name_len = ntohs (msg->name_len);
471 rd_count = ntohs (msg->rd_count);
472 rd_ser_len = ntohs (msg->rd_len);
473 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
475 name_tmp = (char *) &msg[1];
476 rd_tmp = &name_tmp[name_len];
478 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
479 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd);
481 if (qe->proc != NULL)
482 qe->proc (qe->proc_cls, &msg->zone_key, expire, name_tmp, rd_count, rd, &msg->signature);
492 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
493 const struct GNUNET_MessageHeader *msg,
494 int type, size_t size)
497 /* handle different message type */
499 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
500 if (size < sizeof (struct LookupNameResponseMessage))
505 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
507 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
508 if (size != sizeof (struct RecordPutResponseMessage))
513 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
515 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
516 if (size != sizeof (struct RecordCreateResponseMessage))
521 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
523 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
524 if (size != sizeof (struct RecordRemoveResponseMessage))
529 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
531 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
532 if (size < sizeof (struct ZoneToNameResponseMessage))
537 handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
546 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
547 struct ZoneIterationResponseMessage *msg,
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
551 "ZONE_ITERATION_RESPONSE");
553 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubdummy;
555 size_t exp_msg_len = 0;
558 unsigned rd_count = 0;
562 struct GNUNET_TIME_Absolute expire;
564 msg_len = ntohs (msg->gns_header.header.size);
565 rd_len = ntohs (msg->rd_len);
566 rd_count = ntohs (msg->rd_count);
567 name_len = ntohs (msg->name_len);
568 expire = GNUNET_TIME_absolute_ntoh (msg->expire);
570 exp_msg_len = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_len;
571 if (msg_len != exp_msg_len)
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
574 msg_len, exp_msg_len);
578 if (0 != ntohs (msg->reserved))
584 memset (&pubdummy, '\0', sizeof (pubdummy));
585 if ((0 == name_len) && (0 == (memcmp (&msg->public_key, &pubdummy, sizeof (pubdummy)))))
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zone iteration is completed!\n");
589 GNUNET_CONTAINER_DLL_remove(ze->h->z_head, ze->h->z_tail, ze);
591 if (ze->proc != NULL)
592 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_zero (), NULL , 0, NULL, NULL);
598 name_tmp = (char *) &msg[1];
599 if ((name_tmp[name_len -1] != '\0') || (name_len > 256))
604 rd_ser_tmp = (char *) &name_tmp[name_len];
605 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
606 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
612 if (ze->proc != NULL)
613 ze->proc(ze->proc_cls, &msg->public_key, expire, name_tmp, rd_count, rd, &msg->signature);
618 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
619 const struct GNUNET_MessageHeader *msg,
620 int type, size_t size)
623 /* handle different message type */
625 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
626 if (size < sizeof (struct ZoneIterationResponseMessage))
631 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
640 * Type of a function to call when we receive a message
643 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
644 * @param msg message received, NULL on timeout or fatal error
647 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
649 struct GNUNET_NAMESTORE_Handle *h = cls;
650 struct GNUNET_NAMESTORE_Header * gm;
651 struct GNUNET_NAMESTORE_QueueEntry *qe;
652 struct GNUNET_NAMESTORE_ZoneIterator *ze;
655 uint32_t r_id = UINT32_MAX;
663 size = ntohs (msg->size);
664 type = ntohs (msg->type);
666 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
669 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
670 GNUNET_TIME_UNIT_FOREVER_REL);
674 gm = (struct GNUNET_NAMESTORE_Header *) msg;
675 r_id = ntohl (gm->r_id);
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
679 /* Find matching operation */
682 /* No matching pending operation found */
684 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
685 GNUNET_TIME_UNIT_FOREVER_REL);
689 /* Is it a record related operation ? */
690 for (qe = h->op_head; qe != NULL; qe = qe->next)
692 if (qe->op_id == r_id)
697 manage_record_operations (qe, msg, type, size);
700 /* Is it a zone iteration operation ? */
701 for (ze = h->z_head; ze != NULL; ze = ze->next)
703 if (ze->op_id == r_id)
708 manage_zone_operations (ze, msg, type, size);
711 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
712 GNUNET_TIME_UNIT_FOREVER_REL);
714 if (GNUNET_YES == h->reconnect)
721 * Transmit messages from the message queue to the service
722 * (if there are any, and if we are not already trying).
724 * @param h handle to use
727 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
731 * We can now transmit a message to NAMESTORE. Do it.
733 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
734 * @param size number of bytes we can transmit
735 * @param buf where to copy the messages
736 * @return number of bytes copied into buf
739 transmit_message_to_namestore (void *cls, size_t size, void *buf)
741 struct GNUNET_NAMESTORE_Handle *h = cls;
742 struct PendingMessage *p;
747 if ((size == 0) || (buf == NULL))
754 while ((NULL != (p = h->pending_head)) && (p->size <= size))
756 memcpy (&cbuf[ret], &p[1], p->size);
759 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
760 if (GNUNET_YES == p->is_init)
761 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
762 GNUNET_TIME_UNIT_FOREVER_REL);
771 * Transmit messages from the message queue to the service
772 * (if there are any, and if we are not already trying).
774 * @param h handle to use
777 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
779 struct PendingMessage *p;
783 if (NULL == (p = h->pending_head))
785 if (NULL == h->client)
786 return; /* currently reconnecting */
788 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
789 GNUNET_TIME_UNIT_FOREVER_REL,
790 GNUNET_NO, &transmit_message_to_namestore,
796 * Reconnect to namestore service.
798 * @param h the handle to the namestore service
801 reconnect (struct GNUNET_NAMESTORE_Handle *h)
803 struct PendingMessage *p;
804 struct StartMessage *init;
806 GNUNET_assert (NULL == h->client);
807 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
808 GNUNET_assert (NULL != h->client);
810 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
812 p = GNUNET_malloc (sizeof (struct PendingMessage) +
813 sizeof (struct StartMessage));
814 p->size = sizeof (struct StartMessage);
815 p->is_init = GNUNET_YES;
816 init = (struct StartMessage *) &p[1];
817 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
818 init->header.size = htons (sizeof (struct StartMessage));
819 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
825 * Re-establish the connection to the service.
827 * @param cls handle to use to re-connect.
828 * @param tc scheduler context
831 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 struct GNUNET_NAMESTORE_Handle *h = cls;
835 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
841 * Disconnect from service and then reconnect.
843 * @param h our handle
846 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
848 h->reconnect = GNUNET_NO;
849 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
851 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
857 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
859 uint32_t op_id = h->op_id;
865 * Initialize the connection with the NAMESTORE service.
867 * @param cfg configuration to use
868 * @return handle to the GNS service, or NULL on error
870 struct GNUNET_NAMESTORE_Handle *
871 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
873 struct GNUNET_NAMESTORE_Handle *h;
875 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
877 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
883 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
885 struct PendingMessage *p;
886 struct GNUNET_NAMESTORE_QueueEntry *q;
887 struct GNUNET_NAMESTORE_ZoneIterator *z;
888 struct GNUNET_NAMESTORE_Handle *h = cls;
889 GNUNET_assert (h != NULL);
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
891 while (NULL != (p = h->pending_head))
893 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
897 while (NULL != (q = h->op_head))
900 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
904 while (NULL != (z = h->z_head))
906 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
910 if (NULL != h->client)
912 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
915 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
917 GNUNET_SCHEDULER_cancel (h->reconnect_task);
918 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
925 * Disconnect from the namestore service (and free associated
928 * @param h handle to the namestore
929 * @param drop set to GNUNET_YES to delete all data in namestore (!)
932 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from namestore service\n");
935 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
940 * Store an item in the namestore. If the item is already present,
941 * the expiration time is updated to the max of the existing time and
942 * the new time. This API is used when we cache signatures from other
945 * @param h handle to the namestore
946 * @param zone_key public key of the zone
947 * @param name name that is being mapped (at most 255 characters long)
948 * @param expire when does the corresponding block in the DHT expire (until
949 * when should we never do a DHT lookup for the same name again)?
950 * @param rd_count number of entries in 'rd' array
951 * @param rd array of records with data to store
952 * @param signature signature for all the records in the zone under the given name
953 * @param cont continuation to call when done
954 * @param cont_cls closure for cont
955 * @return handle to abort the request
957 struct GNUNET_NAMESTORE_QueueEntry *
958 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
959 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
961 struct GNUNET_TIME_Absolute expire,
962 unsigned int rd_count,
963 const struct GNUNET_NAMESTORE_RecordData *rd,
964 const struct GNUNET_CRYPTO_RsaSignature *signature,
965 GNUNET_NAMESTORE_ContinuationWithStatus cont,
968 struct GNUNET_NAMESTORE_QueueEntry *qe;
969 struct PendingMessage *pe;
971 /* pointer to elements */
977 size_t rd_ser_len = 0;
980 GNUNET_assert (NULL != h);
981 GNUNET_assert (NULL != zone_key);
982 GNUNET_assert (NULL != name);
983 GNUNET_assert (NULL != rd);
984 GNUNET_assert (NULL != signature);
986 name_len = strlen(name) + 1;
994 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
997 qe->cont_cls = cont_cls;
999 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1002 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1003 char rd_ser[rd_ser_len];
1004 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1006 struct RecordPutMessage * msg;
1007 msg_size = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
1009 /* create msg here */
1010 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1011 pe->size = msg_size;
1012 pe->is_init = GNUNET_NO;
1013 msg = (struct RecordPutMessage *) &pe[1];
1014 name_tmp = (char *) &msg[1];
1015 rd_tmp = &name_tmp[name_len];
1017 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
1018 msg->gns_header.header.size = htons (msg_size);
1019 msg->gns_header.r_id = htonl (rid);
1020 msg->signature = *signature;
1021 msg->name_len = htons (name_len);
1022 msg->expire = GNUNET_TIME_absolute_hton (expire);
1023 msg->rd_len = htons (rd_ser_len);
1024 msg->rd_count = htons (rd_count);
1026 msg->public_key = *zone_key;
1027 memcpy (name_tmp, name, name_len);
1028 memcpy (rd_tmp, rd_ser, rd_ser_len);
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1032 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1040 * Check if a signature is valid. This API is used by the GNS Block
1041 * to validate signatures received from the network.
1043 * @param public_key public key of the zone
1044 * @param name name that is being mapped (at most 255 characters long)
1045 * @param rd_count number of entries in 'rd' array
1046 * @param rd array of records with data to store
1047 * @param signature signature for all the records in the zone under the given name
1048 * @return GNUNET_OK if the signature is valid
1051 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1053 unsigned int rd_count,
1054 const struct GNUNET_NAMESTORE_RecordData *rd,
1055 const struct GNUNET_CRYPTO_RsaSignature *signature)
1057 int res = GNUNET_SYSERR;
1058 size_t rd_ser_len = 0;
1059 size_t name_len = 0;
1062 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1064 GNUNET_assert (public_key != NULL);
1065 GNUNET_assert (name != NULL);
1066 GNUNET_assert (rd != NULL);
1067 GNUNET_assert (signature != NULL);
1070 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1071 char rd_ser[rd_ser_len];
1072 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1074 name_len = strlen (name) + 1;
1078 return GNUNET_SYSERR;
1081 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1082 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1083 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1084 name_tmp = (char *) &sig_purpose[1];
1085 rd_tmp = &name_tmp[name_len];
1086 memcpy (name_tmp, name, name_len);
1087 memcpy (rd_tmp, rd_ser, rd_ser_len);
1089 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1091 GNUNET_free (sig_purpose);
1097 * Store an item in the namestore. If the item is already present,
1098 * the expiration time is updated to the max of the existing time and
1099 * the new time. This API is used by the authority of a zone.
1101 * @param h handle to the namestore
1102 * @param pkey private key of the zone
1103 * @param expire block expiration time
1104 * @param name name that is being mapped (at most 255 characters long)
1105 * @param rd record data to store
1106 * @param cont continuation to call when done
1107 * @param cont_cls closure for cont
1108 * @return handle to abort the request
1110 struct GNUNET_NAMESTORE_QueueEntry *
1111 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1112 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1114 const struct GNUNET_NAMESTORE_RecordData *rd,
1115 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1118 struct GNUNET_NAMESTORE_QueueEntry *qe;
1119 struct PendingMessage *pe;
1123 size_t rd_ser_len = 0;
1124 size_t msg_size = 0;
1125 size_t name_len = 0;
1129 GNUNET_assert (NULL != h);
1130 GNUNET_assert (NULL != pkey);
1131 GNUNET_assert (NULL != name);
1132 GNUNET_assert (NULL != rd);
1134 name_len = strlen(name) + 1;
1142 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1145 qe->cont_cls = cont_cls;
1147 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1150 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1151 GNUNET_assert (pkey_enc != NULL);
1152 key_len = ntohs (pkey_enc->len);
1154 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1155 char rd_ser[rd_ser_len];
1156 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1158 struct RecordCreateMessage * msg;
1159 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1161 /* create msg here */
1162 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1163 pe->size = msg_size;
1164 pe->is_init = GNUNET_NO;
1165 msg = (struct RecordCreateMessage *) &pe[1];
1167 pkey_tmp = (char *) &msg[1];
1168 name_tmp = &pkey_tmp[key_len];
1169 rd_tmp = &name_tmp[name_len];
1171 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1172 msg->gns_header.header.size = htons (msg_size);
1173 msg->gns_header.r_id = htonl (rid);
1174 msg->name_len = htons (name_len);
1175 msg->rd_count = htons (1);
1176 msg->rd_len = htons (rd_ser_len);
1177 msg->pkey_len = htons (key_len);
1178 msg->expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever());
1179 memcpy (pkey_tmp, pkey_enc, key_len);
1180 memcpy (name_tmp, name, name_len);
1181 memcpy (rd_tmp, rd_ser, rd_ser_len);
1182 GNUNET_free (pkey_enc);
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1186 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1193 * Explicitly remove some content from the database. The
1194 * "cont"inuation will be called with status "GNUNET_OK" if content
1195 * was removed, "GNUNET_NO" if no matching entry was found and
1196 * "GNUNET_SYSERR" on all other types of errors.
1197 * This API is used by the authority of a zone.
1199 * @param h handle to the namestore
1200 * @param pkey private key of the zone
1201 * @param name name that is being mapped (at most 255 characters long)
1202 * @param rd record data
1203 * @param cont continuation to call when done
1204 * @param cont_cls closure for cont
1205 * @return handle to abort the request
1207 struct GNUNET_NAMESTORE_QueueEntry *
1208 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1209 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1211 const struct GNUNET_NAMESTORE_RecordData *rd,
1212 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1215 struct GNUNET_NAMESTORE_QueueEntry *qe;
1216 struct PendingMessage *pe;
1220 size_t rd_ser_len = 0;
1221 size_t msg_size = 0;
1222 size_t name_len = 0;
1226 GNUNET_assert (NULL != h);
1229 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1232 qe->cont_cls = cont_cls;
1234 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1237 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1238 GNUNET_assert (pkey_enc != NULL);
1239 key_len = ntohs (pkey_enc->len);
1241 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1242 char rd_ser[rd_ser_len];
1243 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1245 name_len = strlen (name) + 1;
1247 struct RecordRemoveMessage * msg;
1248 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1250 /* create msg here */
1251 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1252 pe->size = msg_size;
1253 pe->is_init = GNUNET_NO;
1254 msg = (struct RecordRemoveMessage *) &pe[1];
1256 pkey_tmp = (char *) &msg[1];
1257 name_tmp = &pkey_tmp[key_len];
1258 rd_tmp = &name_tmp[name_len];
1260 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1261 msg->gns_header.header.size = htons (msg_size);
1262 msg->gns_header.r_id = htonl (rid);
1263 msg->name_len = htons (name_len);
1264 msg->rd_len = htons (rd_ser_len);
1265 msg->rd_count = htons (1);
1266 msg->pkey_len = htons (key_len);
1267 memcpy (pkey_tmp, pkey_enc, key_len);
1268 memcpy (name_tmp, name, name_len);
1269 memcpy (rd_tmp, rd_ser, rd_ser_len);
1271 GNUNET_free (pkey_enc);
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1275 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1282 * Get a result for a particular key from the namestore. The processor
1283 * will only be called once.
1285 * @param h handle to the namestore
1286 * @param zone zone to look up a record from
1287 * @param name name to look up
1288 * @param record_type desired record type, 0 for all
1289 * @param proc function to call on the matching records, or with
1290 * NULL (rd_count == 0) if there are no matching records
1291 * @param proc_cls closure for proc
1292 * @return a handle that can be used to
1295 struct GNUNET_NAMESTORE_QueueEntry *
1296 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1297 const GNUNET_HashCode *zone,
1299 uint32_t record_type,
1300 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1302 struct GNUNET_NAMESTORE_QueueEntry *qe;
1303 struct PendingMessage *pe;
1304 size_t msg_size = 0;
1305 size_t name_len = 0;
1308 GNUNET_assert (NULL != h);
1309 GNUNET_assert (NULL != zone);
1310 GNUNET_assert (NULL != name);
1312 name_len = strlen (name) + 1;
1313 if ((name_len == 0) || (name_len > 256))
1320 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1323 qe->proc_cls = proc_cls;
1325 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1328 msg_size = sizeof (struct LookupNameMessage) + name_len;
1329 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1331 /* create msg here */
1332 struct LookupNameMessage * msg;
1333 pe->size = msg_size;
1334 pe->is_init = GNUNET_NO;
1335 msg = (struct LookupNameMessage *) &pe[1];
1336 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1337 msg->gns_header.header.size = htons (msg_size);
1338 msg->gns_header.r_id = htonl (rid);
1339 msg->record_type = htonl (record_type);
1340 msg->name_len = htonl (name_len);
1342 memcpy (&msg[1], name, name_len);
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1346 /* transmit message */
1347 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1355 * Look for an existing PKEY delegation record for a given public key.
1356 * Returns at most one result to the processor.
1358 * @param h handle to the namestore
1359 * @param zone hash of public key of the zone to look up in, never NULL
1360 * @param value_zone hash of the public key of the target zone (value), never NULL
1361 * @param proc function to call on the matching records, or with
1362 * NULL (rd_count == 0) if there are no matching records
1363 * @param proc_cls closure for proc
1364 * @return a handle that can be used to
1367 struct GNUNET_NAMESTORE_QueueEntry *
1368 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1369 const GNUNET_HashCode *zone,
1370 const GNUNET_HashCode *value_zone,
1371 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1373 struct GNUNET_NAMESTORE_QueueEntry *qe;
1374 struct PendingMessage *pe;
1375 size_t msg_size = 0;
1378 GNUNET_assert (NULL != h);
1379 GNUNET_assert (NULL != zone);
1380 GNUNET_assert (NULL != value_zone);
1383 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1386 qe->proc_cls = proc_cls;
1388 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1391 msg_size = sizeof (struct ZoneToNameMessage);
1392 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1394 /* create msg here */
1395 struct ZoneToNameMessage * msg;
1396 pe->size = msg_size;
1397 pe->is_init = GNUNET_NO;
1398 msg = (struct ZoneToNameMessage *) &pe[1];
1399 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1400 msg->gns_header.header.size = htons (msg_size);
1401 msg->gns_header.r_id = htonl (rid);
1403 msg->value_zone = *value_zone;
1405 char * z_tmp = strdup (GNUNET_h2s (zone));
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1407 "NAMESTORE_ZONE_TO_NAME",
1409 GNUNET_h2s (value_zone));
1410 GNUNET_free (z_tmp);
1412 /* transmit message */
1413 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1422 * Starts a new zone iteration (used to periodically PUT all of our
1423 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1424 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1425 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1426 * immediately, and then again after
1427 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1429 * @param h handle to the namestore
1430 * @param zone zone to access, NULL for all zones
1431 * @param must_have_flags flags that must be set for the record to be returned
1432 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1433 * @param proc function to call on each name from the zone; it
1434 * will be called repeatedly with a value (if available)
1435 * and always once at the end with a name of NULL.
1436 * @param proc_cls closure for proc
1437 * @return an iterator handle to use for iteration
1439 struct GNUNET_NAMESTORE_ZoneIterator *
1440 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1441 const GNUNET_HashCode *zone,
1442 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1443 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1444 GNUNET_NAMESTORE_RecordProcessor proc,
1447 struct GNUNET_NAMESTORE_ZoneIterator *it;
1448 struct PendingMessage *pe;
1449 size_t msg_size = 0;
1452 GNUNET_assert (NULL != h);
1456 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1459 it->proc_cls = proc;
1465 it->has_zone = GNUNET_YES;
1469 memset (&it->zone, '\0', sizeof (it->zone));
1470 it->has_zone = GNUNET_NO;
1472 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1475 msg_size = sizeof (struct ZoneIterationStartMessage);
1476 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1478 /* create msg here */
1479 struct ZoneIterationStartMessage * msg;
1480 pe->size = msg_size;
1481 pe->is_init = GNUNET_NO;
1482 msg = (struct ZoneIterationStartMessage *) &pe[1];
1483 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1484 msg->gns_header.header.size = htons (msg_size);
1485 msg->gns_header.r_id = htonl (rid);
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_START");
1494 memset (&msg->zone, '\0', sizeof (msg->zone));
1496 msg->must_have_flags = ntohs (must_have_flags);
1497 msg->must_not_have_flags = ntohs (must_not_have_flags);
1501 /* transmit message */
1502 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1510 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1511 * for the next record.
1513 * @param it the iterator
1516 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1518 struct GNUNET_NAMESTORE_Handle *h;
1519 struct PendingMessage *pe;
1520 size_t msg_size = 0;
1522 GNUNET_assert (NULL != it);
1526 msg_size = sizeof (struct ZoneIterationNextMessage);
1527 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1529 /* create msg here */
1530 struct ZoneIterationNextMessage * msg;
1531 pe->size = msg_size;
1532 pe->is_init = GNUNET_NO;
1533 msg = (struct ZoneIterationNextMessage *) &pe[1];
1534 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1535 msg->gns_header.header.size = htons (msg_size);
1536 msg->gns_header.r_id = htonl (it->op_id);
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_NEXT");
1540 /* transmit message */
1541 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1547 * Stops iteration and releases the namestore handle for further calls.
1549 * @param it the iterator
1552 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1554 GNUNET_assert (NULL != it);
1555 struct PendingMessage *pe;
1556 size_t msg_size = 0;
1557 struct GNUNET_NAMESTORE_Handle *h = it->h;
1560 msg_size = sizeof (struct ZoneIterationStopMessage);
1561 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1563 /* create msg here */
1564 struct ZoneIterationStopMessage * msg;
1565 pe->size = msg_size;
1566 pe->is_init = GNUNET_NO;
1567 msg = (struct ZoneIterationStopMessage *) &pe[1];
1568 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1569 msg->gns_header.header.size = htons (msg_size);
1570 msg->gns_header.r_id = htonl (it->op_id);
1572 if (GNUNET_YES == it->has_zone)
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_STOP");
1577 /* transmit message */
1578 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1584 * Cancel a namestore operation. The final callback from the
1585 * operation must not have been done yet.
1587 * @param qe operation to cancel
1590 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1592 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1594 GNUNET_assert (qe != NULL);
1596 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1601 /* end of namestore_api.c */