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);
379 int res = ntohl (msg->op_result);
383 * 1 : No records for entry
384 * 2 : Could not find record to remove
385 * 3 : Failed to create new signature
386 * 4 : Failed to put new set of records in database
390 if (qe->cont != NULL)
392 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
397 if (qe->cont != NULL)
399 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
404 if (qe->cont != NULL)
406 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
411 if (qe->cont != NULL)
413 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
418 if (qe->cont != NULL)
420 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
432 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
433 struct ZoneToNameResponseMessage* msg,
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
437 "ZONE_TO_NAME_RESPONSE");
439 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
440 /* Operation done, remove */
441 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
443 int res = ntohs (msg->res);
445 struct GNUNET_TIME_Absolute expire;
448 unsigned int rd_count;
453 if (res == GNUNET_SYSERR)
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\n");
456 if (qe->proc != NULL)
457 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
459 else if (res == GNUNET_NO)
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
462 if (qe->proc != NULL)
463 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
465 else if (res == GNUNET_YES)
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
469 name_len = ntohs (msg->name_len);
470 rd_count = ntohs (msg->rd_count);
471 rd_ser_len = ntohs (msg->rd_len);
472 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
474 name_tmp = (char *) &msg[1];
475 rd_tmp = &name_tmp[name_len];
477 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
478 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd);
480 if (qe->proc != NULL)
481 qe->proc (qe->proc_cls, &msg->zone_key, expire, name_tmp, rd_count, rd, &msg->signature);
491 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
492 const struct GNUNET_MessageHeader *msg,
493 int type, size_t size)
496 /* handle different message type */
498 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
499 if (size < sizeof (struct LookupNameResponseMessage))
504 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
506 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
507 if (size != sizeof (struct RecordPutResponseMessage))
512 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
514 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
515 if (size != sizeof (struct RecordCreateResponseMessage))
520 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
522 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
523 if (size != sizeof (struct RecordRemoveResponseMessage))
528 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
530 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
531 if (size < sizeof (struct ZoneToNameResponseMessage))
536 handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
545 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
546 struct ZoneIterationResponseMessage *msg,
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
550 "ZONE_ITERATION_RESPONSE");
552 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubdummy;
554 size_t exp_msg_len = 0;
557 unsigned rd_count = 0;
561 struct GNUNET_TIME_Absolute expire;
563 msg_len = ntohs (msg->gns_header.header.size);
564 rd_len = ntohs (msg->rd_len);
565 rd_count = ntohs (msg->rd_count);
566 name_len = ntohs (msg->name_len);
567 expire = GNUNET_TIME_absolute_ntoh (msg->expire);
569 exp_msg_len = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_len;
570 if (msg_len != exp_msg_len)
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
573 msg_len, exp_msg_len);
577 if (0 != ntohs (msg->reserved))
583 memset (&pubdummy, '\0', sizeof (pubdummy));
584 if ((0 == name_len) && (0 == (memcmp (&msg->public_key, &pubdummy, sizeof (pubdummy)))))
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zone iteration is completed!\n");
588 GNUNET_CONTAINER_DLL_remove(ze->h->z_head, ze->h->z_tail, ze);
590 if (ze->proc != NULL)
591 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_zero (), NULL , 0, NULL, NULL);
597 name_tmp = (char *) &msg[1];
598 if ((name_tmp[name_len -1] != '\0') || (name_len > 256))
603 rd_ser_tmp = (char *) &name_tmp[name_len];
604 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
605 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
611 if (ze->proc != NULL)
612 ze->proc(ze->proc_cls, &msg->public_key, expire, name_tmp, rd_count, rd, &msg->signature);
617 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
618 const struct GNUNET_MessageHeader *msg,
619 int type, size_t size)
622 /* handle different message type */
624 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
625 if (size < sizeof (struct ZoneIterationResponseMessage))
630 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
639 * Type of a function to call when we receive a message
642 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
643 * @param msg message received, NULL on timeout or fatal error
646 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
648 struct GNUNET_NAMESTORE_Handle *h = cls;
649 struct GNUNET_NAMESTORE_Header * gm;
650 struct GNUNET_NAMESTORE_QueueEntry *qe;
651 struct GNUNET_NAMESTORE_ZoneIterator *ze;
654 uint32_t r_id = UINT32_MAX;
662 size = ntohs (msg->size);
663 type = ntohs (msg->type);
665 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
668 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
669 GNUNET_TIME_UNIT_FOREVER_REL);
673 gm = (struct GNUNET_NAMESTORE_Header *) msg;
674 r_id = ntohl (gm->r_id);
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
678 /* Find matching operation */
681 /* No matching pending operation found */
683 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
684 GNUNET_TIME_UNIT_FOREVER_REL);
688 /* Is it a record related operation ? */
689 for (qe = h->op_head; qe != NULL; qe = qe->next)
691 if (qe->op_id == r_id)
696 manage_record_operations (qe, msg, type, size);
699 /* Is it a zone iteration operation ? */
700 for (ze = h->z_head; ze != NULL; ze = ze->next)
702 if (ze->op_id == r_id)
707 manage_zone_operations (ze, msg, type, size);
710 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
711 GNUNET_TIME_UNIT_FOREVER_REL);
713 if (GNUNET_YES == h->reconnect)
720 * Transmit messages from the message queue to the service
721 * (if there are any, and if we are not already trying).
723 * @param h handle to use
726 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
730 * We can now transmit a message to NAMESTORE. Do it.
732 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
733 * @param size number of bytes we can transmit
734 * @param buf where to copy the messages
735 * @return number of bytes copied into buf
738 transmit_message_to_namestore (void *cls, size_t size, void *buf)
740 struct GNUNET_NAMESTORE_Handle *h = cls;
741 struct PendingMessage *p;
746 if ((size == 0) || (buf == NULL))
753 while ((NULL != (p = h->pending_head)) && (p->size <= size))
755 memcpy (&cbuf[ret], &p[1], p->size);
758 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
759 if (GNUNET_YES == p->is_init)
760 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
761 GNUNET_TIME_UNIT_FOREVER_REL);
770 * Transmit messages from the message queue to the service
771 * (if there are any, and if we are not already trying).
773 * @param h handle to use
776 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
778 struct PendingMessage *p;
782 if (NULL == (p = h->pending_head))
784 if (NULL == h->client)
785 return; /* currently reconnecting */
787 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
788 GNUNET_TIME_UNIT_FOREVER_REL,
789 GNUNET_NO, &transmit_message_to_namestore,
795 * Reconnect to namestore service.
797 * @param h the handle to the namestore service
800 reconnect (struct GNUNET_NAMESTORE_Handle *h)
802 struct PendingMessage *p;
803 struct StartMessage *init;
805 GNUNET_assert (NULL == h->client);
806 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
807 GNUNET_assert (NULL != h->client);
809 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
811 p = GNUNET_malloc (sizeof (struct PendingMessage) +
812 sizeof (struct StartMessage));
813 p->size = sizeof (struct StartMessage);
814 p->is_init = GNUNET_YES;
815 init = (struct StartMessage *) &p[1];
816 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
817 init->header.size = htons (sizeof (struct StartMessage));
818 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
824 * Re-establish the connection to the service.
826 * @param cls handle to use to re-connect.
827 * @param tc scheduler context
830 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
832 struct GNUNET_NAMESTORE_Handle *h = cls;
834 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
840 * Disconnect from service and then reconnect.
842 * @param h our handle
845 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
847 h->reconnect = GNUNET_NO;
848 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
850 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
856 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
858 uint32_t op_id = h->op_id;
864 * Initialize the connection with the NAMESTORE service.
866 * @param cfg configuration to use
867 * @return handle to the GNS service, or NULL on error
869 struct GNUNET_NAMESTORE_Handle *
870 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
872 struct GNUNET_NAMESTORE_Handle *h;
874 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
876 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
882 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
884 struct PendingMessage *p;
885 struct GNUNET_NAMESTORE_QueueEntry *q;
886 struct GNUNET_NAMESTORE_ZoneIterator *z;
887 struct GNUNET_NAMESTORE_Handle *h = cls;
888 GNUNET_assert (h != NULL);
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
890 while (NULL != (p = h->pending_head))
892 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
896 while (NULL != (q = h->op_head))
898 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
902 while (NULL != (z = h->z_head))
904 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
908 if (NULL != h->client)
910 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
913 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
915 GNUNET_SCHEDULER_cancel (h->reconnect_task);
916 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
923 * Disconnect from the namestore service (and free associated
926 * @param h handle to the namestore
927 * @param drop set to GNUNET_YES to delete all data in namestore (!)
930 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from namestore service\n");
933 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
938 * Store an item in the namestore. If the item is already present,
939 * the expiration time is updated to the max of the existing time and
940 * the new time. This API is used when we cache signatures from other
943 * @param h handle to the namestore
944 * @param zone_key public key of the zone
945 * @param name name that is being mapped (at most 255 characters long)
946 * @param expire when does the corresponding block in the DHT expire (until
947 * when should we never do a DHT lookup for the same name again)?
948 * @param rd_count number of entries in 'rd' array
949 * @param rd array of records with data to store
950 * @param signature signature for all the records in the zone under the given name
951 * @param cont continuation to call when done
952 * @param cont_cls closure for cont
953 * @return handle to abort the request
955 struct GNUNET_NAMESTORE_QueueEntry *
956 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
957 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
959 struct GNUNET_TIME_Absolute expire,
960 unsigned int rd_count,
961 const struct GNUNET_NAMESTORE_RecordData *rd,
962 const struct GNUNET_CRYPTO_RsaSignature *signature,
963 GNUNET_NAMESTORE_ContinuationWithStatus cont,
966 struct GNUNET_NAMESTORE_QueueEntry *qe;
967 struct PendingMessage *pe;
969 /* pointer to elements */
975 size_t rd_ser_len = 0;
978 GNUNET_assert (NULL != h);
979 GNUNET_assert (NULL != zone_key);
980 GNUNET_assert (NULL != name);
981 GNUNET_assert (NULL != rd);
982 GNUNET_assert (NULL != signature);
984 name_len = strlen(name) + 1;
992 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
995 qe->cont_cls = cont_cls;
997 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1000 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1001 char rd_ser[rd_ser_len];
1002 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1004 struct RecordPutMessage * msg;
1005 msg_size = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
1007 /* create msg here */
1008 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1009 pe->size = msg_size;
1010 pe->is_init = GNUNET_NO;
1011 msg = (struct RecordPutMessage *) &pe[1];
1012 name_tmp = (char *) &msg[1];
1013 rd_tmp = &name_tmp[name_len];
1015 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
1016 msg->gns_header.header.size = htons (msg_size);
1017 msg->gns_header.r_id = htonl (rid);
1018 msg->signature = *signature;
1019 msg->name_len = htons (name_len);
1020 msg->expire = GNUNET_TIME_absolute_hton (expire);
1021 msg->rd_len = htons (rd_ser_len);
1022 msg->rd_count = htons (rd_count);
1024 msg->public_key = *zone_key;
1025 memcpy (name_tmp, name, name_len);
1026 memcpy (rd_tmp, rd_ser, rd_ser_len);
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1030 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1038 * Check if a signature is valid. This API is used by the GNS Block
1039 * to validate signatures received from the network.
1041 * @param public_key public key of the zone
1042 * @param name name that is being mapped (at most 255 characters long)
1043 * @param rd_count number of entries in 'rd' array
1044 * @param rd array of records with data to store
1045 * @param signature signature for all the records in the zone under the given name
1046 * @return GNUNET_OK if the signature is valid
1049 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1051 unsigned int rd_count,
1052 const struct GNUNET_NAMESTORE_RecordData *rd,
1053 const struct GNUNET_CRYPTO_RsaSignature *signature)
1055 int res = GNUNET_SYSERR;
1056 size_t rd_ser_len = 0;
1057 size_t name_len = 0;
1060 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1062 GNUNET_assert (public_key != NULL);
1063 GNUNET_assert (name != NULL);
1064 GNUNET_assert (rd != NULL);
1065 GNUNET_assert (signature != NULL);
1068 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1069 char rd_ser[rd_ser_len];
1070 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1072 name_len = strlen (name) + 1;
1076 return GNUNET_SYSERR;
1079 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1080 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1081 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1082 name_tmp = (char *) &sig_purpose[1];
1083 rd_tmp = &name_tmp[name_len];
1084 memcpy (name_tmp, name, name_len);
1085 memcpy (rd_tmp, rd_ser, rd_ser_len);
1087 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1089 GNUNET_free (sig_purpose);
1095 * Store an item in the namestore. If the item is already present,
1096 * the expiration time is updated to the max of the existing time and
1097 * the new time. This API is used by the authority of a zone.
1099 * @param h handle to the namestore
1100 * @param pkey private key of the zone
1101 * @param expire block expiration time
1102 * @param name name that is being mapped (at most 255 characters long)
1103 * @param rd record data to store
1104 * @param cont continuation to call when done
1105 * @param cont_cls closure for cont
1106 * @return handle to abort the request
1108 struct GNUNET_NAMESTORE_QueueEntry *
1109 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1110 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1112 const struct GNUNET_NAMESTORE_RecordData *rd,
1113 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1116 struct GNUNET_NAMESTORE_QueueEntry *qe;
1117 struct PendingMessage *pe;
1121 size_t rd_ser_len = 0;
1122 size_t msg_size = 0;
1123 size_t name_len = 0;
1127 GNUNET_assert (NULL != h);
1128 GNUNET_assert (NULL != pkey);
1129 GNUNET_assert (NULL != name);
1130 GNUNET_assert (NULL != rd);
1132 name_len = strlen(name) + 1;
1140 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1143 qe->cont_cls = cont_cls;
1145 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1148 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1149 GNUNET_assert (pkey_enc != NULL);
1150 key_len = ntohs (pkey_enc->len);
1152 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1153 char rd_ser[rd_ser_len];
1154 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1156 struct RecordCreateMessage * msg;
1157 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1159 /* create msg here */
1160 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1161 pe->size = msg_size;
1162 pe->is_init = GNUNET_NO;
1163 msg = (struct RecordCreateMessage *) &pe[1];
1165 pkey_tmp = (char *) &msg[1];
1166 name_tmp = &pkey_tmp[key_len];
1167 rd_tmp = &name_tmp[name_len];
1169 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1170 msg->gns_header.header.size = htons (msg_size);
1171 msg->gns_header.r_id = htonl (rid);
1172 msg->name_len = htons (name_len);
1173 msg->rd_count = htons (1);
1174 msg->rd_len = htons (rd_ser_len);
1175 msg->pkey_len = htons (key_len);
1176 msg->expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever());
1177 memcpy (pkey_tmp, pkey_enc, key_len);
1178 memcpy (name_tmp, name, name_len);
1179 memcpy (rd_tmp, rd_ser, rd_ser_len);
1180 GNUNET_free (pkey_enc);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1184 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1191 * Explicitly remove some content from the database. The
1192 * "cont"inuation will be called with status "GNUNET_OK" if content
1193 * was removed, "GNUNET_NO" if no matching entry was found and
1194 * "GNUNET_SYSERR" on all other types of errors.
1195 * This API is used by the authority of a zone.
1197 * @param h handle to the namestore
1198 * @param pkey private key of the zone
1199 * @param name name that is being mapped (at most 255 characters long)
1200 * @param rd record data
1201 * @param cont continuation to call when done
1202 * @param cont_cls closure for cont
1203 * @return handle to abort the request
1205 struct GNUNET_NAMESTORE_QueueEntry *
1206 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1207 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1209 const struct GNUNET_NAMESTORE_RecordData *rd,
1210 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1213 struct GNUNET_NAMESTORE_QueueEntry *qe;
1214 struct PendingMessage *pe;
1218 size_t rd_ser_len = 0;
1219 size_t msg_size = 0;
1220 size_t name_len = 0;
1224 GNUNET_assert (NULL != h);
1227 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1230 qe->cont_cls = cont_cls;
1232 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1235 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1236 GNUNET_assert (pkey_enc != NULL);
1237 key_len = ntohs (pkey_enc->len);
1239 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1240 char rd_ser[rd_ser_len];
1241 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1243 name_len = strlen (name) + 1;
1245 struct RecordRemoveMessage * msg;
1246 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1248 /* create msg here */
1249 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1250 pe->size = msg_size;
1251 pe->is_init = GNUNET_NO;
1252 msg = (struct RecordRemoveMessage *) &pe[1];
1254 pkey_tmp = (char *) &msg[1];
1255 name_tmp = &pkey_tmp[key_len];
1256 rd_tmp = &name_tmp[name_len];
1258 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1259 msg->gns_header.header.size = htons (msg_size);
1260 msg->gns_header.r_id = htonl (rid);
1261 msg->name_len = htons (name_len);
1262 msg->rd_len = htons (rd_ser_len);
1263 msg->rd_count = htons (1);
1264 msg->pkey_len = htons (key_len);
1265 memcpy (pkey_tmp, pkey_enc, key_len);
1266 memcpy (name_tmp, name, name_len);
1267 memcpy (rd_tmp, rd_ser, rd_ser_len);
1269 GNUNET_free (pkey_enc);
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1273 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1280 * Get a result for a particular key from the namestore. The processor
1281 * will only be called once.
1283 * @param h handle to the namestore
1284 * @param zone zone to look up a record from
1285 * @param name name to look up
1286 * @param record_type desired record type, 0 for all
1287 * @param proc function to call on the matching records, or with
1288 * NULL (rd_count == 0) if there are no matching records
1289 * @param proc_cls closure for proc
1290 * @return a handle that can be used to
1293 struct GNUNET_NAMESTORE_QueueEntry *
1294 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1295 const GNUNET_HashCode *zone,
1297 uint32_t record_type,
1298 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1300 struct GNUNET_NAMESTORE_QueueEntry *qe;
1301 struct PendingMessage *pe;
1302 size_t msg_size = 0;
1303 size_t name_len = 0;
1306 GNUNET_assert (NULL != h);
1307 GNUNET_assert (NULL != zone);
1308 GNUNET_assert (NULL != name);
1310 name_len = strlen (name) + 1;
1311 if ((name_len == 0) || (name_len > 256))
1318 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1321 qe->proc_cls = proc_cls;
1323 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1326 msg_size = sizeof (struct LookupNameMessage) + name_len;
1327 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1329 /* create msg here */
1330 struct LookupNameMessage * msg;
1331 pe->size = msg_size;
1332 pe->is_init = GNUNET_NO;
1333 msg = (struct LookupNameMessage *) &pe[1];
1334 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1335 msg->gns_header.header.size = htons (msg_size);
1336 msg->gns_header.r_id = htonl (rid);
1337 msg->record_type = htonl (record_type);
1338 msg->name_len = htonl (name_len);
1340 memcpy (&msg[1], name, name_len);
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1344 /* transmit message */
1345 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1353 * Look for an existing PKEY delegation record for a given public key.
1354 * Returns at most one result to the processor.
1356 * @param h handle to the namestore
1357 * @param zone hash of public key of the zone to look up in, never NULL
1358 * @param value_zone hash of the public key of the target zone (value), never NULL
1359 * @param proc function to call on the matching records, or with
1360 * NULL (rd_count == 0) if there are no matching records
1361 * @param proc_cls closure for proc
1362 * @return a handle that can be used to
1365 struct GNUNET_NAMESTORE_QueueEntry *
1366 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1367 const GNUNET_HashCode *zone,
1368 const GNUNET_HashCode *value_zone,
1369 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1371 struct GNUNET_NAMESTORE_QueueEntry *qe;
1372 struct PendingMessage *pe;
1373 size_t msg_size = 0;
1376 GNUNET_assert (NULL != h);
1377 GNUNET_assert (NULL != zone);
1378 GNUNET_assert (NULL != value_zone);
1381 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1384 qe->proc_cls = proc_cls;
1386 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1389 msg_size = sizeof (struct ZoneToNameMessage);
1390 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1392 /* create msg here */
1393 struct ZoneToNameMessage * msg;
1394 pe->size = msg_size;
1395 pe->is_init = GNUNET_NO;
1396 msg = (struct ZoneToNameMessage *) &pe[1];
1397 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1398 msg->gns_header.header.size = htons (msg_size);
1399 msg->gns_header.r_id = htonl (rid);
1401 msg->value_zone = *value_zone;
1403 char * z_tmp = strdup (GNUNET_h2s (zone));
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1405 "NAMESTORE_ZONE_TO_NAME",
1407 GNUNET_h2s (value_zone));
1408 GNUNET_free (z_tmp);
1410 /* transmit message */
1411 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1420 * Starts a new zone iteration (used to periodically PUT all of our
1421 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1422 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1423 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1424 * immediately, and then again after
1425 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1427 * @param h handle to the namestore
1428 * @param zone zone to access, NULL for all zones
1429 * @param must_have_flags flags that must be set for the record to be returned
1430 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1431 * @param proc function to call on each name from the zone; it
1432 * will be called repeatedly with a value (if available)
1433 * and always once at the end with a name of NULL.
1434 * @param proc_cls closure for proc
1435 * @return an iterator handle to use for iteration
1437 struct GNUNET_NAMESTORE_ZoneIterator *
1438 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1439 const GNUNET_HashCode *zone,
1440 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1441 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1442 GNUNET_NAMESTORE_RecordProcessor proc,
1445 struct GNUNET_NAMESTORE_ZoneIterator *it;
1446 struct PendingMessage *pe;
1447 size_t msg_size = 0;
1450 GNUNET_assert (NULL != h);
1454 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1457 it->proc_cls = proc;
1463 it->has_zone = GNUNET_YES;
1467 memset (&it->zone, '\0', sizeof (it->zone));
1468 it->has_zone = GNUNET_NO;
1470 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1473 msg_size = sizeof (struct ZoneIterationStartMessage);
1474 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1476 /* create msg here */
1477 struct ZoneIterationStartMessage * msg;
1478 pe->size = msg_size;
1479 pe->is_init = GNUNET_NO;
1480 msg = (struct ZoneIterationStartMessage *) &pe[1];
1481 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1482 msg->gns_header.header.size = htons (msg_size);
1483 msg->gns_header.r_id = htonl (rid);
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_START");
1492 memset (&msg->zone, '\0', sizeof (msg->zone));
1494 msg->must_have_flags = ntohs (must_have_flags);
1495 msg->must_not_have_flags = ntohs (must_not_have_flags);
1499 /* transmit message */
1500 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1508 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1509 * for the next record.
1511 * @param it the iterator
1514 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1516 struct GNUNET_NAMESTORE_Handle *h;
1517 struct PendingMessage *pe;
1518 size_t msg_size = 0;
1520 GNUNET_assert (NULL != it);
1524 msg_size = sizeof (struct ZoneIterationNextMessage);
1525 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1527 /* create msg here */
1528 struct ZoneIterationNextMessage * msg;
1529 pe->size = msg_size;
1530 pe->is_init = GNUNET_NO;
1531 msg = (struct ZoneIterationNextMessage *) &pe[1];
1532 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1533 msg->gns_header.header.size = htons (msg_size);
1534 msg->gns_header.r_id = htonl (it->op_id);
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_NEXT");
1538 /* transmit message */
1539 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1545 * Stops iteration and releases the namestore handle for further calls.
1547 * @param it the iterator
1550 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1552 GNUNET_assert (NULL != it);
1553 struct PendingMessage *pe;
1554 size_t msg_size = 0;
1555 struct GNUNET_NAMESTORE_Handle *h = it->h;
1558 msg_size = sizeof (struct ZoneIterationStopMessage);
1559 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1561 /* create msg here */
1562 struct ZoneIterationStopMessage * msg;
1563 pe->size = msg_size;
1564 pe->is_init = GNUNET_NO;
1565 msg = (struct ZoneIterationStopMessage *) &pe[1];
1566 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1567 msg->gns_header.header.size = htons (msg_size);
1568 msg->gns_header.r_id = htonl (it->op_id);
1570 if (GNUNET_YES == it->has_zone)
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for all zones\n", "ZONE_ITERATION_STOP");
1575 /* transmit message */
1576 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1582 * Cancel a namestore operation. The final callback from the
1583 * operation must not have been done yet.
1585 * @param qe operation to cancel
1588 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1590 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1592 GNUNET_assert (qe != NULL);
1594 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1599 /* end of namestore_api.c */