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;
328 if (ntohs (msg->op_result) == GNUNET_OK)
331 if (qe->cont != NULL)
333 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
337 else if (ntohs (msg->op_result) == GNUNET_NO)
340 if (qe->cont != NULL)
342 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
351 /* Operation done, remove */
352 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
359 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
360 struct RecordCreateResponseMessage* msg,
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
364 "RECORD_CREATE_RESPONSE");
366 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' %i\n",
369 "RECORD_CREATE_RESPONSE", ntohs (msg->op_result));
370 if (ntohs (msg->op_result) == GNUNET_YES)
372 if (qe->cont != NULL)
374 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore added record successfully"));
378 else if (ntohs (msg->op_result) == GNUNET_NO)
380 if (qe->cont != NULL)
382 qe->cont (qe->cont_cls, GNUNET_NO, _("Namestore record already existed"));
387 if (qe->cont != NULL)
389 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
393 /* Operation done, remove */
394 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
401 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
402 struct RecordRemoveResponseMessage* msg,
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
406 "RECORD_REMOVE_RESPONSE");
408 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
409 int res = ntohs (msg->op_result);
414 * 1 : No records for entry
415 * 2 : Could not find record to remove
416 * 3 : Failed to create new signature
417 * 4 : Failed to put new set of records in database
421 if (qe->cont != NULL)
423 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
428 if (qe->cont != NULL)
430 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
435 if (qe->cont != NULL)
437 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
442 if (qe->cont != NULL)
444 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
449 if (qe->cont != NULL)
451 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
459 /* Operation done, remove */
460 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
466 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
467 struct ZoneToNameResponseMessage* msg,
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
471 "ZONE_TO_NAME_RESPONSE");
473 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
474 int res = ntohs (msg->res);
476 struct GNUNET_TIME_Absolute expire;
479 unsigned int rd_count;
484 if (res == GNUNET_SYSERR)
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\n");
487 if (qe->proc != NULL)
488 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
490 else if (res == GNUNET_NO)
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
493 if (qe->proc != NULL)
494 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
496 else if (res == GNUNET_YES)
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
500 name_len = ntohs (msg->name_len);
501 rd_count = ntohs (msg->rd_count);
502 rd_ser_len = ntohs (msg->rd_len);
503 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
505 name_tmp = (char *) &msg[1];
506 rd_tmp = &name_tmp[name_len];
508 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
509 GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd);
511 if (qe->proc != NULL)
512 qe->proc (qe->proc_cls, &msg->zone_key, expire, name_tmp, rd_count, rd, &msg->signature);
517 /* Operation done, remove */
518 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
524 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
525 const struct GNUNET_MessageHeader *msg,
526 int type, size_t size)
529 /* handle different message type */
531 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
532 if (size < sizeof (struct LookupNameResponseMessage))
537 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
539 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
540 if (size != sizeof (struct RecordPutResponseMessage))
545 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
547 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
548 if (size != sizeof (struct RecordCreateResponseMessage))
553 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
555 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
556 if (size != sizeof (struct RecordRemoveResponseMessage))
561 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
563 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
564 if (size < sizeof (struct ZoneToNameResponseMessage))
569 handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
578 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
579 struct ZoneIterationResponseMessage *msg,
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
583 "ZONE_ITERATION_RESPONSE");
586 if (ze->proc != NULL)
589 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
595 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
596 const struct GNUNET_MessageHeader *msg,
597 int type, size_t size)
600 /* handle different message type */
602 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
603 if (size < sizeof (struct ZoneIterationResponseMessage))
608 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
617 * Type of a function to call when we receive a message
620 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
621 * @param msg message received, NULL on timeout or fatal error
624 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
626 struct GNUNET_NAMESTORE_Handle *h = cls;
627 struct GNUNET_NAMESTORE_Header * gm;
628 struct GNUNET_NAMESTORE_QueueEntry *qe;
629 struct GNUNET_NAMESTORE_ZoneIterator *ze;
632 uint32_t r_id = UINT32_MAX;
640 size = ntohs (msg->size);
641 type = ntohs (msg->type);
643 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
646 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
647 GNUNET_TIME_UNIT_FOREVER_REL);
651 gm = (struct GNUNET_NAMESTORE_Header *) msg;
652 r_id = ntohl (gm->r_id);
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
656 /* Find matching operation */
659 /* No matching pending operation found */
661 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
662 GNUNET_TIME_UNIT_FOREVER_REL);
666 /* Is it a record related operation ? */
667 for (qe = h->op_head; qe != NULL; qe = qe->next)
669 if (qe->op_id == r_id)
674 manage_record_operations (qe, msg, type, size);
677 /* Is it a zone iteration operation ? */
678 for (ze = h->z_head; ze != NULL; ze = ze->next)
680 if (ze->op_id == r_id)
685 manage_zone_operations (ze, msg, type, size);
688 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
689 GNUNET_TIME_UNIT_FOREVER_REL);
691 if (GNUNET_YES == h->reconnect)
698 * Transmit messages from the message queue to the service
699 * (if there are any, and if we are not already trying).
701 * @param h handle to use
704 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
708 * We can now transmit a message to NAMESTORE. Do it.
710 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
711 * @param size number of bytes we can transmit
712 * @param buf where to copy the messages
713 * @return number of bytes copied into buf
716 transmit_message_to_namestore (void *cls, size_t size, void *buf)
718 struct GNUNET_NAMESTORE_Handle *h = cls;
719 struct PendingMessage *p;
724 if ((size == 0) || (buf == NULL))
731 while ((NULL != (p = h->pending_head)) && (p->size <= size))
733 memcpy (&cbuf[ret], &p[1], p->size);
736 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
737 if (GNUNET_YES == p->is_init)
738 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
739 GNUNET_TIME_UNIT_FOREVER_REL);
748 * Transmit messages from the message queue to the service
749 * (if there are any, and if we are not already trying).
751 * @param h handle to use
754 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
756 struct PendingMessage *p;
760 if (NULL == (p = h->pending_head))
762 if (NULL == h->client)
763 return; /* currently reconnecting */
765 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
766 GNUNET_TIME_UNIT_FOREVER_REL,
767 GNUNET_NO, &transmit_message_to_namestore,
773 * Reconnect to namestore service.
775 * @param h the handle to the namestore service
778 reconnect (struct GNUNET_NAMESTORE_Handle *h)
780 struct PendingMessage *p;
781 struct StartMessage *init;
783 GNUNET_assert (NULL == h->client);
784 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
785 GNUNET_assert (NULL != h->client);
787 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
789 p = GNUNET_malloc (sizeof (struct PendingMessage) +
790 sizeof (struct StartMessage));
791 p->size = sizeof (struct StartMessage);
792 p->is_init = GNUNET_YES;
793 init = (struct StartMessage *) &p[1];
794 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
795 init->header.size = htons (sizeof (struct StartMessage));
796 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
802 * Re-establish the connection to the service.
804 * @param cls handle to use to re-connect.
805 * @param tc scheduler context
808 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
810 struct GNUNET_NAMESTORE_Handle *h = cls;
812 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
818 * Disconnect from service and then reconnect.
820 * @param h our handle
823 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
825 h->reconnect = GNUNET_NO;
826 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
828 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
834 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
836 uint32_t op_id = h->op_id;
842 * Initialize the connection with the NAMESTORE service.
844 * @param cfg configuration to use
845 * @return handle to the GNS service, or NULL on error
847 struct GNUNET_NAMESTORE_Handle *
848 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
850 struct GNUNET_NAMESTORE_Handle *h;
852 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
854 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
860 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
862 struct PendingMessage *p;
863 struct GNUNET_NAMESTORE_QueueEntry *q;
864 struct GNUNET_NAMESTORE_ZoneIterator *z;
865 struct GNUNET_NAMESTORE_Handle *h = cls;
866 GNUNET_assert (h != NULL);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
868 while (NULL != (p = h->pending_head))
870 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
874 while (NULL != (q = h->op_head))
877 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
881 while (NULL != (z = h->z_head))
883 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
887 if (NULL != h->client)
889 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
892 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
894 GNUNET_SCHEDULER_cancel (h->reconnect_task);
895 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
902 * Disconnect from the namestore service (and free associated
905 * @param h handle to the namestore
906 * @param drop set to GNUNET_YES to delete all data in namestore (!)
909 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
911 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Disconnecting from namestore service\n");
912 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
917 * Store an item in the namestore. If the item is already present,
918 * the expiration time is updated to the max of the existing time and
919 * the new time. This API is used when we cache signatures from other
922 * @param h handle to the namestore
923 * @param zone_key public key of the zone
924 * @param name name that is being mapped (at most 255 characters long)
925 * @param expire when does the corresponding block in the DHT expire (until
926 * when should we never do a DHT lookup for the same name again)?
927 * @param rd_count number of entries in 'rd' array
928 * @param rd array of records with data to store
929 * @param signature signature for all the records in the zone under the given name
930 * @param cont continuation to call when done
931 * @param cont_cls closure for cont
932 * @return handle to abort the request
934 struct GNUNET_NAMESTORE_QueueEntry *
935 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
936 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
938 struct GNUNET_TIME_Absolute expire,
939 unsigned int rd_count,
940 const struct GNUNET_NAMESTORE_RecordData *rd,
941 const struct GNUNET_CRYPTO_RsaSignature *signature,
942 GNUNET_NAMESTORE_ContinuationWithStatus cont,
945 struct GNUNET_NAMESTORE_QueueEntry *qe;
946 struct PendingMessage *pe;
948 /* pointer to elements */
954 size_t rd_ser_len = 0;
955 size_t pubkey_len = 0;
958 GNUNET_assert (NULL != h);
959 GNUNET_assert (NULL != zone_key);
960 GNUNET_assert (NULL != name);
961 GNUNET_assert (NULL != rd);
962 GNUNET_assert (NULL != signature);
964 name_len = strlen(name) + 1;
972 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
975 qe->cont_cls = cont_cls;
977 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
980 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
981 char rd_ser[rd_ser_len];
982 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
984 pubkey_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
985 struct RecordPutMessage * msg;
986 msg_size = sizeof (struct RecordPutMessage) + pubkey_len + name_len + rd_ser_len;
988 /* create msg here */
989 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
991 pe->is_init = GNUNET_NO;
992 msg = (struct RecordPutMessage *) &pe[1];
993 name_tmp = (char *) &msg[1];
994 rd_tmp = &name_tmp[name_len];
996 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
997 msg->gns_header.header.size = htons (msg_size);
998 msg->gns_header.r_id = htonl (rid);
999 msg->signature = *signature;
1000 msg->name_len = htons (name_len);
1001 msg->expire = GNUNET_TIME_absolute_hton (expire);
1002 msg->rd_len = htons (rd_ser_len);
1003 msg->rd_count = htons (rd_count);
1005 msg->public_key = *zone_key;
1006 memcpy (name_tmp, name, name_len);
1007 memcpy (rd_tmp, rd_ser, rd_ser_len);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1011 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1019 * Check if a signature is valid. This API is used by the GNS Block
1020 * to validate signatures received from the network.
1022 * @param public_key public key of the zone
1023 * @param name name that is being mapped (at most 255 characters long)
1024 * @param rd_count number of entries in 'rd' array
1025 * @param rd array of records with data to store
1026 * @param signature signature for all the records in the zone under the given name
1027 * @return GNUNET_OK if the signature is valid
1030 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1032 unsigned int rd_count,
1033 const struct GNUNET_NAMESTORE_RecordData *rd,
1034 const struct GNUNET_CRYPTO_RsaSignature *signature)
1036 int res = GNUNET_SYSERR;
1037 size_t rd_ser_len = 0;
1038 size_t name_len = 0;
1041 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1043 GNUNET_assert (public_key != NULL);
1044 GNUNET_assert (name != NULL);
1045 GNUNET_assert (signature != NULL);
1047 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1048 char rd_ser[rd_ser_len];
1049 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1051 name_len = strlen (name) + 1;
1053 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1054 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1055 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1056 name_tmp = (char *) &sig_purpose[1];
1057 rd_tmp = &name_tmp[name_len];
1058 memcpy (name_tmp, name, name_len);
1059 memcpy (rd_tmp, rd_ser, rd_ser_len);
1061 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1063 GNUNET_free (sig_purpose);
1069 * Store an item in the namestore. If the item is already present,
1070 * the expiration time is updated to the max of the existing time and
1071 * the new time. This API is used by the authority of a zone.
1073 * @param h handle to the namestore
1074 * @param pkey private key of the zone
1075 * @param name name that is being mapped (at most 255 characters long)
1076 * @param rd record data to store
1077 * @param cont continuation to call when done
1078 * @param cont_cls closure for cont
1079 * @return handle to abort the request
1081 struct GNUNET_NAMESTORE_QueueEntry *
1082 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1083 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1085 const struct GNUNET_NAMESTORE_RecordData *rd,
1086 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1089 struct GNUNET_NAMESTORE_QueueEntry *qe;
1090 struct PendingMessage *pe;
1094 size_t rd_ser_len = 0;
1095 size_t msg_size = 0;
1096 size_t name_len = 0;
1100 GNUNET_assert (NULL != h);
1101 GNUNET_assert (NULL != pkey);
1102 GNUNET_assert (NULL != name);
1103 GNUNET_assert (NULL != rd);
1105 name_len = strlen(name) + 1;
1113 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1116 qe->cont_cls = cont_cls;
1118 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1121 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1122 GNUNET_assert (pkey_enc != NULL);
1123 key_len = ntohs (pkey_enc->len);
1125 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1126 char rd_ser[rd_ser_len];
1127 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1129 struct RecordCreateMessage * msg;
1130 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1132 /* create msg here */
1133 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1134 pe->size = msg_size;
1135 pe->is_init = GNUNET_NO;
1136 msg = (struct RecordCreateMessage *) &pe[1];
1138 pkey_tmp = (char *) &msg[1];
1139 name_tmp = &pkey_tmp[key_len];
1140 rd_tmp = &name_tmp[name_len];
1142 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1143 msg->gns_header.header.size = htons (msg_size);
1144 msg->gns_header.r_id = htonl (rid);
1145 msg->name_len = htons (name_len);
1146 msg->rd_count = htons (1);
1147 msg->rd_len = htons (rd_ser_len);
1148 msg->pkey_len = htons (key_len);
1149 memcpy (pkey_tmp, pkey_enc, key_len);
1150 memcpy (name_tmp, name, name_len);
1151 memcpy (rd_tmp, rd_ser, rd_ser_len);
1152 GNUNET_free (pkey_enc);
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1156 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1163 * Explicitly remove some content from the database. The
1164 * "cont"inuation will be called with status "GNUNET_OK" if content
1165 * was removed, "GNUNET_NO" if no matching entry was found and
1166 * "GNUNET_SYSERR" on all other types of errors.
1167 * This API is used by the authority of a zone.
1169 * @param h handle to the namestore
1170 * @param pkey private key of the zone
1171 * @param name name that is being mapped (at most 255 characters long)
1172 * @param rd record data
1173 * @param cont continuation to call when done
1174 * @param cont_cls closure for cont
1175 * @return handle to abort the request
1177 struct GNUNET_NAMESTORE_QueueEntry *
1178 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1179 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1181 const struct GNUNET_NAMESTORE_RecordData *rd,
1182 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1185 struct GNUNET_NAMESTORE_QueueEntry *qe;
1186 struct PendingMessage *pe;
1190 size_t rd_ser_len = 0;
1191 size_t msg_size = 0;
1192 size_t name_len = 0;
1196 GNUNET_assert (NULL != h);
1199 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1202 qe->cont_cls = cont_cls;
1204 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1207 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1208 GNUNET_assert (pkey_enc != NULL);
1209 key_len = ntohs (pkey_enc->len);
1211 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1212 char rd_ser[rd_ser_len];
1213 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1215 name_len = strlen (name) + 1;
1217 struct RecordRemoveMessage * msg;
1218 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1220 /* create msg here */
1221 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1222 pe->size = msg_size;
1223 pe->is_init = GNUNET_NO;
1224 msg = (struct RecordRemoveMessage *) &pe[1];
1226 pkey_tmp = (char *) &msg[1];
1227 name_tmp = &pkey_tmp[key_len];
1228 rd_tmp = &name_tmp[name_len];
1230 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1231 msg->gns_header.header.size = htons (msg_size);
1232 msg->gns_header.r_id = htonl (rid);
1233 msg->name_len = htons (name_len);
1234 msg->rd_len = htons (rd_ser_len);
1235 msg->rd_count = htons (1);
1236 msg->pkey_len = htons (key_len);
1237 memcpy (pkey_tmp, pkey_enc, key_len);
1238 memcpy (name_tmp, name, name_len);
1239 memcpy (rd_tmp, rd_ser, rd_ser_len);
1241 GNUNET_free (pkey_enc);
1243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1245 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1252 * Get a result for a particular key from the namestore. The processor
1253 * will only be called once.
1255 * @param h handle to the namestore
1256 * @param zone zone to look up a record from
1257 * @param name name to look up
1258 * @param record_type desired record type, 0 for all
1259 * @param proc function to call on the matching records, or with
1260 * NULL (rd_count == 0) if there are no matching records
1261 * @param proc_cls closure for proc
1262 * @return a handle that can be used to
1265 struct GNUNET_NAMESTORE_QueueEntry *
1266 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1267 const GNUNET_HashCode *zone,
1269 uint32_t record_type,
1270 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1272 struct GNUNET_NAMESTORE_QueueEntry *qe;
1273 struct PendingMessage *pe;
1274 size_t msg_size = 0;
1275 size_t name_len = 0;
1278 GNUNET_assert (NULL != h);
1279 GNUNET_assert (NULL != zone);
1280 GNUNET_assert (NULL != name);
1282 name_len = strlen (name) + 1;
1283 if ((name_len == 0) || (name_len > 256))
1290 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1293 qe->proc_cls = proc_cls;
1295 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1298 msg_size = sizeof (struct LookupNameMessage) + name_len;
1299 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1301 /* create msg here */
1302 struct LookupNameMessage * msg;
1303 pe->size = msg_size;
1304 pe->is_init = GNUNET_NO;
1305 msg = (struct LookupNameMessage *) &pe[1];
1306 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1307 msg->gns_header.header.size = htons (msg_size);
1308 msg->gns_header.r_id = htonl (rid);
1309 msg->record_type = htonl (record_type);
1311 msg->name_len = htonl (name_len);
1312 memcpy (&msg[1], name, name_len);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1316 /* transmit message */
1317 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1325 * Look for an existing PKEY delegation record for a given public key.
1326 * Returns at most one result to the processor.
1328 * @param h handle to the namestore
1329 * @param zone hash of public key of the zone to look up in, never NULL
1330 * @param value_zone hash of the public key of the target zone (value), never NULL
1331 * @param proc function to call on the matching records, or with
1332 * NULL (rd_count == 0) if there are no matching records
1333 * @param proc_cls closure for proc
1334 * @return a handle that can be used to
1337 struct GNUNET_NAMESTORE_QueueEntry *
1338 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1339 const GNUNET_HashCode *zone,
1340 const GNUNET_HashCode *value_zone,
1341 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1343 struct GNUNET_NAMESTORE_QueueEntry *qe;
1344 struct PendingMessage *pe;
1345 size_t msg_size = 0;
1348 GNUNET_assert (NULL != h);
1349 GNUNET_assert (NULL != zone);
1350 GNUNET_assert (NULL != value_zone);
1353 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1356 qe->proc_cls = proc_cls;
1358 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1361 msg_size = sizeof (struct ZoneToNameMessage);
1362 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1364 /* create msg here */
1365 struct ZoneToNameMessage * msg;
1366 pe->size = msg_size;
1367 pe->is_init = GNUNET_NO;
1368 msg = (struct ZoneToNameMessage *) &pe[1];
1369 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1370 msg->gns_header.header.size = htons (msg_size);
1371 msg->gns_header.r_id = htonl (rid);
1373 msg->value_zone = *value_zone;
1375 char * z_tmp = strdup (GNUNET_h2s (zone));
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1377 "NAMESTORE_ZONE_TO_NAME",
1379 GNUNET_h2s (value_zone));
1380 GNUNET_free (z_tmp);
1382 /* transmit message */
1383 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1392 * Starts a new zone iteration (used to periodically PUT all of our
1393 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1394 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1395 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1396 * immediately, and then again after
1397 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1399 * @param h handle to the namestore
1400 * @param zone zone to access, NULL for all zones
1401 * @param must_have_flags flags that must be set for the record to be returned
1402 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1403 * @param proc function to call on each name from the zone; it
1404 * will be called repeatedly with a value (if available)
1405 * and always once at the end with a name of NULL.
1406 * @param proc_cls closure for proc
1407 * @return an iterator handle to use for iteration
1409 struct GNUNET_NAMESTORE_ZoneIterator *
1410 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1411 const GNUNET_HashCode *zone,
1412 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1413 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1414 GNUNET_NAMESTORE_RecordProcessor proc,
1417 struct GNUNET_NAMESTORE_ZoneIterator *it;
1418 struct PendingMessage *pe;
1419 size_t msg_size = 0;
1422 GNUNET_assert (NULL != h);
1423 GNUNET_assert (NULL != zone);
1426 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1429 it->proc_cls = proc;
1432 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1435 msg_size = sizeof (struct ZoneIterationStartMessage);
1436 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1438 /* create msg here */
1439 struct ZoneIterationStartMessage * msg;
1440 pe->size = msg_size;
1441 pe->is_init = GNUNET_NO;
1442 msg = (struct ZoneIterationStartMessage *) &pe[1];
1443 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1444 msg->gns_header.header.size = htons (msg_size);
1445 msg->gns_header.r_id = htonl (rid);
1447 msg->must_have_flags = ntohs (must_have_flags);
1448 msg->must_not_have_flags = ntohs (must_not_have_flags);
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1452 /* transmit message */
1453 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1461 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1462 * for the next record.
1464 * @param it the iterator
1467 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1469 struct GNUNET_NAMESTORE_Handle *h;
1470 struct PendingMessage *pe;
1471 size_t msg_size = 0;
1473 GNUNET_assert (NULL != it);
1477 msg_size = sizeof (struct ZoneIterationNextMessage);
1478 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1480 /* create msg here */
1481 struct ZoneIterationNextMessage * msg;
1482 pe->size = msg_size;
1483 pe->is_init = GNUNET_NO;
1484 msg = (struct ZoneIterationNextMessage *) &pe[1];
1485 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1486 msg->gns_header.header.size = htons (msg_size);
1487 msg->gns_header.r_id = htonl (it->op_id);
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1491 /* transmit message */
1492 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1498 * Stops iteration and releases the namestore handle for further calls.
1500 * @param it the iterator
1503 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1505 GNUNET_assert (NULL != it);
1506 struct PendingMessage *pe;
1507 size_t msg_size = 0;
1508 struct GNUNET_NAMESTORE_Handle *h = it->h;
1511 msg_size = sizeof (struct ZoneIterationStopMessage);
1512 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1514 /* create msg here */
1515 struct ZoneIterationStopMessage * msg;
1516 pe->size = msg_size;
1517 pe->is_init = GNUNET_NO;
1518 msg = (struct ZoneIterationStopMessage *) &pe[1];
1519 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1520 msg->gns_header.header.size = htons (msg_size);
1521 msg->gns_header.r_id = htonl (it->op_id);
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1525 /* transmit message */
1526 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1532 * Cancel a namestore operation. The final callback from the
1533 * operation must not have been done yet.
1535 * @param qe operation to cancel
1538 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1540 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1542 GNUNET_assert (qe != NULL);
1544 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1549 /* end of namestore_api.c */