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;
100 * Message in linked list we should send to the service. The
101 * actual binary message follows this struct.
103 struct PendingMessage
109 struct PendingMessage *next;
114 struct PendingMessage *prev;
117 * Size of the message.
122 * Is this the 'START' message?
129 * Connection to the NAMESTORE service.
131 struct GNUNET_NAMESTORE_Handle
135 * Configuration to use.
137 const struct GNUNET_CONFIGURATION_Handle *cfg;
140 * Socket (if available).
142 struct GNUNET_CLIENT_Connection *client;
145 * Currently pending transmission request (or NULL).
147 struct GNUNET_CLIENT_TransmitHandle *th;
152 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
155 * Pending messages to send to the service
158 struct PendingMessage * pending_head;
159 struct PendingMessage * pending_tail;
162 * Should we reconnect to service due to some serious error?
168 * Pending namestore queue entries
170 struct GNUNET_NAMESTORE_QueueEntry * op_head;
171 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
176 * Pending namestore zone iterator entries
178 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
179 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
182 struct GNUNET_NAMESTORE_SimpleRecord
187 struct GNUNET_NAMESTORE_SimpleRecord *next;
192 struct GNUNET_NAMESTORE_SimpleRecord *prev;
195 const GNUNET_HashCode *zone;
196 uint32_t record_type;
197 struct GNUNET_TIME_Absolute expiration;
198 enum GNUNET_NAMESTORE_RecordFlags flags;
206 * Convert a type name (i.e. "AAAA") to the corresponding number.
208 * @param typename name to convert
209 * @return corresponding number, UINT32_MAX on error
212 GNUNET_NAMESTORE_typename_to_number (const char *typename)
218 { "A", GNUNET_DNSPARSER_TYPE_A },
219 { "NS", GNUNET_DNSPARSER_TYPE_NS },
220 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
221 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
222 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
223 { "MX", GNUNET_DNSPARSER_TYPE_MX },
224 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
225 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
226 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
227 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
233 while ( (map[i].name != NULL) &&
234 (0 != strcasecmp (typename, map[i].name)) )
236 return map[i].number;
241 * Disconnect from service and then reconnect.
243 * @param h our handle
246 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
249 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
250 struct LookupNameResponseMessage * msg,
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
254 "LOOKUP_NAME_RESPONSE");
256 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
260 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
261 struct GNUNET_TIME_Absolute expire;
266 int contains_sig = GNUNET_NO;
269 rd_len = ntohs (msg->rd_len);
270 rd_count = ntohs (msg->rd_count);
271 msg_len = ntohs (msg->gns_header.header.size);
272 name_len = ntohs (msg->name_len);
273 contains_sig = ntohs (msg->contains_sig);
274 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
276 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
277 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
280 if (msg_len != exp_msg_len)
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
283 msg_len, exp_msg_len);
293 name = (char *) &msg[1];
294 rd_tmp = &name[name_len];
296 /* deserialize records */
297 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
298 GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
300 /* reset values if values not contained */
301 if (contains_sig == GNUNET_NO)
304 signature = &msg->signature;
306 if (qe->proc != NULL)
308 qe->proc (qe->proc_cls, &msg->public_key, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
311 /* Operation done, remove */
312 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
318 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
319 struct RecordPutResponseMessage* msg,
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
323 "RECORD_PUT_RESPONSE");
325 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
327 if (ntohs (msg->op_result) == GNUNET_OK)
329 if (qe->cont != NULL)
331 qe->cont (qe->cont_cls, GNUNET_OK, _("Namestore added record successfully"));
335 else if (ntohs (msg->op_result) == GNUNET_SYSERR)
337 if (qe->cont != NULL)
339 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record"));
348 /* Operation done, remove */
349 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
356 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
357 struct RecordCreateResponseMessage* msg,
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
361 "RECORD_CREATE_RESPONSE");
363 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' %i\n",
366 "RECORD_CREATE_RESPONSE", ntohs (msg->op_result));
367 if (ntohs (msg->op_result) == GNUNET_YES)
369 if (qe->cont != NULL)
371 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore added record successfully"));
375 else if (ntohs (msg->op_result) == GNUNET_NO)
377 if (qe->cont != NULL)
379 qe->cont (qe->cont_cls, GNUNET_NO, _("Namestore record already existed"));
384 if (qe->cont != NULL)
386 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
390 /* Operation done, remove */
391 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
398 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
399 struct RecordRemoveResponseMessage* msg,
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
403 "RECORD_REMOVE_RESPONSE");
405 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
406 int res = ntohs (msg->op_result);
411 * 1 : No records for entry
412 * 2 : Could not find record to remove
413 * 3 : Failed to create new signature
414 * 4 : Failed to put new set of records in database
418 if (qe->cont != NULL)
420 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
425 if (qe->cont != NULL)
427 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
432 if (qe->cont != NULL)
434 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
439 if (qe->cont != NULL)
441 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
446 if (qe->cont != NULL)
448 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
456 /* Operation done, remove */
457 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
463 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
464 struct ZoneToNameResponseMessage* msg,
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
468 "ZONE_TO_NAME_RESPONSE");
470 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
471 int res = ntohs (msg->res);
473 struct GNUNET_TIME_Absolute expire;
476 unsigned int rd_count;
481 if (res == GNUNET_SYSERR)
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\n");
484 if (qe->proc != NULL)
485 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
487 else if (res == GNUNET_NO)
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
490 if (qe->proc != NULL)
491 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
493 else if (res == GNUNET_YES)
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
497 name_len = ntohs (msg->name_len);
498 rd_count = ntohs (msg->rd_count);
499 rd_ser_len = ntohs (msg->rd_len);
500 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
502 name_tmp = (char *) &msg[1];
503 rd_tmp = &name_tmp[name_len];
505 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
506 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd);
508 if (qe->proc != NULL)
509 qe->proc (qe->proc_cls, &msg->zone_key, expire, name_tmp, rd_count, rd, &msg->signature);
514 /* Operation done, remove */
515 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
521 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
522 const struct GNUNET_MessageHeader *msg,
523 int type, size_t size)
526 /* handle different message type */
528 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
529 if (size < sizeof (struct LookupNameResponseMessage))
534 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
536 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
537 if (size != sizeof (struct RecordPutResponseMessage))
542 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
544 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
545 if (size != sizeof (struct RecordCreateResponseMessage))
550 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
552 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
553 if (size != sizeof (struct RecordRemoveResponseMessage))
558 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
560 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
561 if (size < sizeof (struct ZoneToNameResponseMessage))
566 handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
575 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
576 struct ZoneIterationResponseMessage *msg,
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
580 "ZONE_ITERATION_RESPONSE");
583 if (ze->proc != NULL)
586 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
592 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
593 const struct GNUNET_MessageHeader *msg,
594 int type, size_t size)
597 /* handle different message type */
599 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
600 if (size < sizeof (struct ZoneIterationResponseMessage))
605 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
614 * Type of a function to call when we receive a message
617 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
618 * @param msg message received, NULL on timeout or fatal error
621 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
623 struct GNUNET_NAMESTORE_Handle *h = cls;
624 struct GNUNET_NAMESTORE_Header * gm;
625 struct GNUNET_NAMESTORE_QueueEntry *qe;
626 struct GNUNET_NAMESTORE_ZoneIterator *ze;
629 uint32_t r_id = UINT32_MAX;
637 size = ntohs (msg->size);
638 type = ntohs (msg->type);
640 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
643 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
644 GNUNET_TIME_UNIT_FOREVER_REL);
648 gm = (struct GNUNET_NAMESTORE_Header *) msg;
649 r_id = ntohl (gm->r_id);
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
653 /* Find matching operation */
656 /* No matching pending operation found */
658 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
659 GNUNET_TIME_UNIT_FOREVER_REL);
663 /* Is it a record related operation ? */
664 for (qe = h->op_head; qe != NULL; qe = qe->next)
666 if (qe->op_id == r_id)
671 manage_record_operations (qe, msg, type, size);
674 /* Is it a zone iteration operation ? */
675 for (ze = h->z_head; ze != NULL; ze = ze->next)
677 if (ze->op_id == r_id)
682 manage_zone_operations (ze, msg, type, size);
685 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
686 GNUNET_TIME_UNIT_FOREVER_REL);
688 if (GNUNET_YES == h->reconnect)
695 * Transmit messages from the message queue to the service
696 * (if there are any, and if we are not already trying).
698 * @param h handle to use
701 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
705 * We can now transmit a message to NAMESTORE. Do it.
707 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
708 * @param size number of bytes we can transmit
709 * @param buf where to copy the messages
710 * @return number of bytes copied into buf
713 transmit_message_to_namestore (void *cls, size_t size, void *buf)
715 struct GNUNET_NAMESTORE_Handle *h = cls;
716 struct PendingMessage *p;
721 if ((size == 0) || (buf == NULL))
728 while ((NULL != (p = h->pending_head)) && (p->size <= size))
730 memcpy (&cbuf[ret], &p[1], p->size);
733 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
734 if (GNUNET_YES == p->is_init)
735 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
736 GNUNET_TIME_UNIT_FOREVER_REL);
745 * Transmit messages from the message queue to the service
746 * (if there are any, and if we are not already trying).
748 * @param h handle to use
751 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
753 struct PendingMessage *p;
757 if (NULL == (p = h->pending_head))
759 if (NULL == h->client)
760 return; /* currently reconnecting */
762 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
763 GNUNET_TIME_UNIT_FOREVER_REL,
764 GNUNET_NO, &transmit_message_to_namestore,
770 * Reconnect to namestore service.
772 * @param h the handle to the namestore service
775 reconnect (struct GNUNET_NAMESTORE_Handle *h)
777 struct PendingMessage *p;
778 struct StartMessage *init;
780 GNUNET_assert (NULL == h->client);
781 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
782 GNUNET_assert (NULL != h->client);
784 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
786 p = GNUNET_malloc (sizeof (struct PendingMessage) +
787 sizeof (struct StartMessage));
788 p->size = sizeof (struct StartMessage);
789 p->is_init = GNUNET_YES;
790 init = (struct StartMessage *) &p[1];
791 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
792 init->header.size = htons (sizeof (struct StartMessage));
793 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
799 * Re-establish the connection to the service.
801 * @param cls handle to use to re-connect.
802 * @param tc scheduler context
805 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
807 struct GNUNET_NAMESTORE_Handle *h = cls;
809 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
815 * Disconnect from service and then reconnect.
817 * @param h our handle
820 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
822 h->reconnect = GNUNET_NO;
823 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
825 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
831 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
833 uint32_t op_id = h->op_id;
839 * Initialize the connection with the NAMESTORE service.
841 * @param cfg configuration to use
842 * @return handle to the GNS service, or NULL on error
844 struct GNUNET_NAMESTORE_Handle *
845 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
847 struct GNUNET_NAMESTORE_Handle *h;
849 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
851 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
857 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
859 struct PendingMessage *p;
860 struct GNUNET_NAMESTORE_QueueEntry *q;
861 struct GNUNET_NAMESTORE_ZoneIterator *z;
862 struct GNUNET_NAMESTORE_Handle *h = cls;
863 GNUNET_assert (h != NULL);
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
865 while (NULL != (p = h->pending_head))
867 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
871 while (NULL != (q = h->op_head))
874 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
878 while (NULL != (z = h->z_head))
880 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
884 if (NULL != h->client)
886 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
889 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
891 GNUNET_SCHEDULER_cancel (h->reconnect_task);
892 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
899 * Disconnect from the namestore service (and free associated
902 * @param h handle to the namestore
903 * @param drop set to GNUNET_YES to delete all data in namestore (!)
906 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
908 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Disconnecting from namestore service\n");
909 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
914 * Store an item in the namestore. If the item is already present,
915 * the expiration time is updated to the max of the existing time and
916 * the new time. This API is used when we cache signatures from other
919 * @param h handle to the namestore
920 * @param zone_key public key of the zone
921 * @param name name that is being mapped (at most 255 characters long)
922 * @param expire when does the corresponding block in the DHT expire (until
923 * when should we never do a DHT lookup for the same name again)?
924 * @param rd_count number of entries in 'rd' array
925 * @param rd array of records with data to store
926 * @param signature signature for all the records in the zone under the given name
927 * @param cont continuation to call when done
928 * @param cont_cls closure for cont
929 * @return handle to abort the request
931 struct GNUNET_NAMESTORE_QueueEntry *
932 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
933 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
935 struct GNUNET_TIME_Absolute expire,
936 unsigned int rd_count,
937 const struct GNUNET_NAMESTORE_RecordData *rd,
938 const struct GNUNET_CRYPTO_RsaSignature *signature,
939 GNUNET_NAMESTORE_ContinuationWithStatus cont,
942 struct GNUNET_NAMESTORE_QueueEntry *qe;
943 struct PendingMessage *pe;
945 /* pointer to elements */
951 size_t rd_ser_len = 0;
954 GNUNET_assert (NULL != h);
955 GNUNET_assert (NULL != zone_key);
956 GNUNET_assert (NULL != name);
957 GNUNET_assert (NULL != rd);
958 GNUNET_assert (NULL != signature);
960 name_len = strlen(name) + 1;
968 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
971 qe->cont_cls = cont_cls;
973 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
976 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
977 char rd_ser[rd_ser_len];
978 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
980 struct RecordPutMessage * msg;
981 msg_size = sizeof (struct RecordPutMessage) + name_len + rd_ser_len;
983 /* create msg here */
984 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
986 pe->is_init = GNUNET_NO;
987 msg = (struct RecordPutMessage *) &pe[1];
988 name_tmp = (char *) &msg[1];
989 rd_tmp = &name_tmp[name_len];
991 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
992 msg->gns_header.header.size = htons (msg_size);
993 msg->gns_header.r_id = htonl (rid);
994 msg->signature = *signature;
995 msg->name_len = htons (name_len);
996 msg->expire = GNUNET_TIME_absolute_hton (expire);
997 msg->rd_len = htons (rd_ser_len);
998 msg->rd_count = htons (rd_count);
1000 msg->public_key = *zone_key;
1001 memcpy (name_tmp, name, name_len);
1002 memcpy (rd_tmp, rd_ser, rd_ser_len);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1006 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1014 * Check if a signature is valid. This API is used by the GNS Block
1015 * to validate signatures received from the network.
1017 * @param public_key public key of the zone
1018 * @param name name that is being mapped (at most 255 characters long)
1019 * @param rd_count number of entries in 'rd' array
1020 * @param rd array of records with data to store
1021 * @param signature signature for all the records in the zone under the given name
1022 * @return GNUNET_OK if the signature is valid
1025 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1027 unsigned int rd_count,
1028 const struct GNUNET_NAMESTORE_RecordData *rd,
1029 const struct GNUNET_CRYPTO_RsaSignature *signature)
1031 int res = GNUNET_SYSERR;
1032 size_t rd_ser_len = 0;
1033 size_t name_len = 0;
1036 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1038 GNUNET_assert (public_key != NULL);
1039 GNUNET_assert (name != NULL);
1040 GNUNET_assert (rd != NULL);
1041 GNUNET_assert (signature != NULL);
1044 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1045 char rd_ser[rd_ser_len];
1046 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1048 name_len = strlen (name) + 1;
1052 return GNUNET_SYSERR;
1055 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1056 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1057 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1058 name_tmp = (char *) &sig_purpose[1];
1059 rd_tmp = &name_tmp[name_len];
1060 memcpy (name_tmp, name, name_len);
1061 memcpy (rd_tmp, rd_ser, rd_ser_len);
1063 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1065 GNUNET_free (sig_purpose);
1071 * Store an item in the namestore. If the item is already present,
1072 * the expiration time is updated to the max of the existing time and
1073 * the new time. This API is used by the authority of a zone.
1075 * @param h handle to the namestore
1076 * @param pkey private key of the zone
1077 * @param name name that is being mapped (at most 255 characters long)
1078 * @param rd record data to store
1079 * @param cont continuation to call when done
1080 * @param cont_cls closure for cont
1081 * @return handle to abort the request
1083 struct GNUNET_NAMESTORE_QueueEntry *
1084 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1085 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1087 const struct GNUNET_NAMESTORE_RecordData *rd,
1088 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1091 struct GNUNET_NAMESTORE_QueueEntry *qe;
1092 struct PendingMessage *pe;
1096 size_t rd_ser_len = 0;
1097 size_t msg_size = 0;
1098 size_t name_len = 0;
1102 GNUNET_assert (NULL != h);
1103 GNUNET_assert (NULL != pkey);
1104 GNUNET_assert (NULL != name);
1105 GNUNET_assert (NULL != rd);
1107 name_len = strlen(name) + 1;
1115 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1118 qe->cont_cls = cont_cls;
1120 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1123 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1124 GNUNET_assert (pkey_enc != NULL);
1125 key_len = ntohs (pkey_enc->len);
1127 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1128 char rd_ser[rd_ser_len];
1129 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1131 struct RecordCreateMessage * msg;
1132 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1134 /* create msg here */
1135 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1136 pe->size = msg_size;
1137 pe->is_init = GNUNET_NO;
1138 msg = (struct RecordCreateMessage *) &pe[1];
1140 pkey_tmp = (char *) &msg[1];
1141 name_tmp = &pkey_tmp[key_len];
1142 rd_tmp = &name_tmp[name_len];
1144 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1145 msg->gns_header.header.size = htons (msg_size);
1146 msg->gns_header.r_id = htonl (rid);
1147 msg->name_len = htons (name_len);
1148 msg->rd_count = htons (1);
1149 msg->rd_len = htons (rd_ser_len);
1150 msg->pkey_len = htons (key_len);
1151 memcpy (pkey_tmp, pkey_enc, key_len);
1152 memcpy (name_tmp, name, name_len);
1153 memcpy (rd_tmp, rd_ser, rd_ser_len);
1154 GNUNET_free (pkey_enc);
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1158 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1165 * Explicitly remove some content from the database. The
1166 * "cont"inuation will be called with status "GNUNET_OK" if content
1167 * was removed, "GNUNET_NO" if no matching entry was found and
1168 * "GNUNET_SYSERR" on all other types of errors.
1169 * This API is used by the authority of a zone.
1171 * @param h handle to the namestore
1172 * @param pkey private key of the zone
1173 * @param name name that is being mapped (at most 255 characters long)
1174 * @param rd record data
1175 * @param cont continuation to call when done
1176 * @param cont_cls closure for cont
1177 * @return handle to abort the request
1179 struct GNUNET_NAMESTORE_QueueEntry *
1180 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1181 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1183 const struct GNUNET_NAMESTORE_RecordData *rd,
1184 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1187 struct GNUNET_NAMESTORE_QueueEntry *qe;
1188 struct PendingMessage *pe;
1192 size_t rd_ser_len = 0;
1193 size_t msg_size = 0;
1194 size_t name_len = 0;
1198 GNUNET_assert (NULL != h);
1201 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1204 qe->cont_cls = cont_cls;
1206 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1209 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1210 GNUNET_assert (pkey_enc != NULL);
1211 key_len = ntohs (pkey_enc->len);
1213 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1214 char rd_ser[rd_ser_len];
1215 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1217 name_len = strlen (name) + 1;
1219 struct RecordRemoveMessage * msg;
1220 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1222 /* create msg here */
1223 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1224 pe->size = msg_size;
1225 pe->is_init = GNUNET_NO;
1226 msg = (struct RecordRemoveMessage *) &pe[1];
1228 pkey_tmp = (char *) &msg[1];
1229 name_tmp = &pkey_tmp[key_len];
1230 rd_tmp = &name_tmp[name_len];
1232 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1233 msg->gns_header.header.size = htons (msg_size);
1234 msg->gns_header.r_id = htonl (rid);
1235 msg->name_len = htons (name_len);
1236 msg->rd_len = htons (rd_ser_len);
1237 msg->rd_count = htons (1);
1238 msg->pkey_len = htons (key_len);
1239 memcpy (pkey_tmp, pkey_enc, key_len);
1240 memcpy (name_tmp, name, name_len);
1241 memcpy (rd_tmp, rd_ser, rd_ser_len);
1243 GNUNET_free (pkey_enc);
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1247 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1254 * Get a result for a particular key from the namestore. The processor
1255 * will only be called once.
1257 * @param h handle to the namestore
1258 * @param zone zone to look up a record from
1259 * @param name name to look up
1260 * @param record_type desired record type, 0 for all
1261 * @param proc function to call on the matching records, or with
1262 * NULL (rd_count == 0) if there are no matching records
1263 * @param proc_cls closure for proc
1264 * @return a handle that can be used to
1267 struct GNUNET_NAMESTORE_QueueEntry *
1268 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1269 const GNUNET_HashCode *zone,
1271 uint32_t record_type,
1272 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1274 struct GNUNET_NAMESTORE_QueueEntry *qe;
1275 struct PendingMessage *pe;
1276 size_t msg_size = 0;
1277 size_t name_len = 0;
1280 GNUNET_assert (NULL != h);
1281 GNUNET_assert (NULL != zone);
1282 GNUNET_assert (NULL != name);
1284 name_len = strlen (name) + 1;
1285 if ((name_len == 0) || (name_len > 256))
1292 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1295 qe->proc_cls = proc_cls;
1297 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1300 msg_size = sizeof (struct LookupNameMessage) + name_len;
1301 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1303 /* create msg here */
1304 struct LookupNameMessage * msg;
1305 pe->size = msg_size;
1306 pe->is_init = GNUNET_NO;
1307 msg = (struct LookupNameMessage *) &pe[1];
1308 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1309 msg->gns_header.header.size = htons (msg_size);
1310 msg->gns_header.r_id = htonl (rid);
1311 msg->record_type = htonl (record_type);
1313 msg->name_len = htonl (name_len);
1314 memcpy (&msg[1], name, name_len);
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1318 /* transmit message */
1319 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1327 * Look for an existing PKEY delegation record for a given public key.
1328 * Returns at most one result to the processor.
1330 * @param h handle to the namestore
1331 * @param zone hash of public key of the zone to look up in, never NULL
1332 * @param value_zone hash of the public key of the target zone (value), never NULL
1333 * @param proc function to call on the matching records, or with
1334 * NULL (rd_count == 0) if there are no matching records
1335 * @param proc_cls closure for proc
1336 * @return a handle that can be used to
1339 struct GNUNET_NAMESTORE_QueueEntry *
1340 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1341 const GNUNET_HashCode *zone,
1342 const GNUNET_HashCode *value_zone,
1343 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1345 struct GNUNET_NAMESTORE_QueueEntry *qe;
1346 struct PendingMessage *pe;
1347 size_t msg_size = 0;
1350 GNUNET_assert (NULL != h);
1351 GNUNET_assert (NULL != zone);
1352 GNUNET_assert (NULL != value_zone);
1355 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1358 qe->proc_cls = proc_cls;
1360 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1363 msg_size = sizeof (struct ZoneToNameMessage);
1364 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1366 /* create msg here */
1367 struct ZoneToNameMessage * msg;
1368 pe->size = msg_size;
1369 pe->is_init = GNUNET_NO;
1370 msg = (struct ZoneToNameMessage *) &pe[1];
1371 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1372 msg->gns_header.header.size = htons (msg_size);
1373 msg->gns_header.r_id = htonl (rid);
1375 msg->value_zone = *value_zone;
1377 char * z_tmp = strdup (GNUNET_h2s (zone));
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1379 "NAMESTORE_ZONE_TO_NAME",
1381 GNUNET_h2s (value_zone));
1382 GNUNET_free (z_tmp);
1384 /* transmit message */
1385 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1394 * Starts a new zone iteration (used to periodically PUT all of our
1395 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1396 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1397 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1398 * immediately, and then again after
1399 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1401 * @param h handle to the namestore
1402 * @param zone zone to access, NULL for all zones
1403 * @param must_have_flags flags that must be set for the record to be returned
1404 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1405 * @param proc function to call on each name from the zone; it
1406 * will be called repeatedly with a value (if available)
1407 * and always once at the end with a name of NULL.
1408 * @param proc_cls closure for proc
1409 * @return an iterator handle to use for iteration
1411 struct GNUNET_NAMESTORE_ZoneIterator *
1412 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1413 const GNUNET_HashCode *zone,
1414 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1415 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1416 GNUNET_NAMESTORE_RecordProcessor proc,
1419 struct GNUNET_NAMESTORE_ZoneIterator *it;
1420 struct PendingMessage *pe;
1421 size_t msg_size = 0;
1424 GNUNET_assert (NULL != h);
1425 GNUNET_assert (NULL != zone);
1428 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1431 it->proc_cls = proc;
1434 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1437 msg_size = sizeof (struct ZoneIterationStartMessage);
1438 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1440 /* create msg here */
1441 struct ZoneIterationStartMessage * msg;
1442 pe->size = msg_size;
1443 pe->is_init = GNUNET_NO;
1444 msg = (struct ZoneIterationStartMessage *) &pe[1];
1445 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1446 msg->gns_header.header.size = htons (msg_size);
1447 msg->gns_header.r_id = htonl (rid);
1449 msg->must_have_flags = ntohs (must_have_flags);
1450 msg->must_not_have_flags = ntohs (must_not_have_flags);
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1454 /* transmit message */
1455 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1463 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1464 * for the next record.
1466 * @param it the iterator
1469 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1471 struct GNUNET_NAMESTORE_Handle *h;
1472 struct PendingMessage *pe;
1473 size_t msg_size = 0;
1475 GNUNET_assert (NULL != it);
1479 msg_size = sizeof (struct ZoneIterationNextMessage);
1480 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1482 /* create msg here */
1483 struct ZoneIterationNextMessage * msg;
1484 pe->size = msg_size;
1485 pe->is_init = GNUNET_NO;
1486 msg = (struct ZoneIterationNextMessage *) &pe[1];
1487 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1488 msg->gns_header.header.size = htons (msg_size);
1489 msg->gns_header.r_id = htonl (it->op_id);
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1493 /* transmit message */
1494 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1500 * Stops iteration and releases the namestore handle for further calls.
1502 * @param it the iterator
1505 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1507 GNUNET_assert (NULL != it);
1508 struct PendingMessage *pe;
1509 size_t msg_size = 0;
1510 struct GNUNET_NAMESTORE_Handle *h = it->h;
1513 msg_size = sizeof (struct ZoneIterationStopMessage);
1514 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1516 /* create msg here */
1517 struct ZoneIterationStopMessage * msg;
1518 pe->size = msg_size;
1519 pe->is_init = GNUNET_NO;
1520 msg = (struct ZoneIterationStopMessage *) &pe[1];
1521 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
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_STOP", GNUNET_h2s(&it->zone));
1527 /* transmit message */
1528 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1534 * Cancel a namestore operation. The final callback from the
1535 * operation must not have been done yet.
1537 * @param qe operation to cancel
1540 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1542 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1544 GNUNET_assert (qe != NULL);
1546 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1551 /* end of namestore_api.c */