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;
257 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
261 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
262 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
263 struct GNUNET_TIME_Absolute expire;
268 int contains_sig = GNUNET_NO;
271 rd_len = ntohs (msg->rd_len);
272 rd_count = ntohs (msg->rd_count);
273 msg_len = ntohs (msg->gns_header.header.size);
274 name_len = ntohs (msg->name_len);
275 contains_sig = ntohs (msg->contains_sig);
276 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
278 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
279 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
282 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
284 if (msg_len != exp_msg_len)
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
287 msg_len, exp_msg_len);
292 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
293 name = (char *) &zone_key[1];
294 rd_tmp = &name[name_len];
296 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
297 GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
299 /* reset values if values not contained */
300 if (contains_sig == GNUNET_NO)
303 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_len];
307 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
308 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
311 if (qe->proc != NULL)
313 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
316 /* Operation done, remove */
317 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
323 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
324 struct RecordPutResponseMessage* msg,
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
328 "RECORD_PUT_RESPONSE");
330 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
333 if (ntohs (msg->op_result) == GNUNET_OK)
336 if (qe->cont != NULL)
338 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
342 else if (ntohs (msg->op_result) == GNUNET_NO)
345 if (qe->cont != NULL)
347 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
356 /* Operation done, remove */
357 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
364 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
365 struct RecordCreateResponseMessage* msg,
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
369 "RECORD_CREATE_RESPONSE");
371 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' %i\n",
374 "RECORD_CREATE_RESPONSE", ntohs (msg->op_result));
375 if (ntohs (msg->op_result) == GNUNET_YES)
377 if (qe->cont != NULL)
379 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore added record successfully"));
383 else if (ntohs (msg->op_result) == GNUNET_NO)
385 if (qe->cont != NULL)
387 qe->cont (qe->cont_cls, GNUNET_NO, _("Namestore record already existed"));
392 if (qe->cont != NULL)
394 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
398 /* Operation done, remove */
399 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
406 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
407 struct RecordRemoveResponseMessage* msg,
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
411 "RECORD_REMOVE_RESPONSE");
413 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
414 int res = ntohs (msg->op_result);
419 * 1 : No records for entry
420 * 2 : Could not find record to remove
421 * 3 : Failed to create new signature
422 * 4 : Failed to put new set of records in database
426 if (qe->cont != NULL)
428 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
433 if (qe->cont != NULL)
435 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
440 if (qe->cont != NULL)
442 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
447 if (qe->cont != NULL)
449 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
454 if (qe->cont != NULL)
456 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
464 /* Operation done, remove */
465 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
471 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
472 struct ZoneToNameResponseMessage* msg,
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
476 "ZONE_TO_NAME_RESPONSE");
478 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
479 int res = ntohs (msg->res);
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);
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
489 if (qe->proc != NULL)
490 qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
496 /* Operation done, remove */
497 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
503 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
504 const struct GNUNET_MessageHeader *msg,
505 int type, size_t size)
508 /* handle different message type */
510 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
511 if (size < sizeof (struct LookupNameResponseMessage))
516 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
518 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
519 if (size != sizeof (struct RecordPutResponseMessage))
524 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
526 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
527 if (size != sizeof (struct RecordCreateResponseMessage))
532 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
534 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
535 if (size != sizeof (struct RecordRemoveResponseMessage))
540 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
542 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
543 if (size < sizeof (struct ZoneToNameResponseMessage))
548 handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
557 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
558 struct ZoneIterationResponseMessage *msg,
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
562 "ZONE_ITERATION_RESPONSE");
565 if (ze->proc != NULL)
568 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
574 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
575 const struct GNUNET_MessageHeader *msg,
576 int type, size_t size)
579 /* handle different message type */
581 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
582 if (size < sizeof (struct ZoneIterationResponseMessage))
587 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
596 * Type of a function to call when we receive a message
599 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
600 * @param msg message received, NULL on timeout or fatal error
603 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
605 struct GNUNET_NAMESTORE_Handle *h = cls;
606 struct GNUNET_NAMESTORE_Header * gm;
607 struct GNUNET_NAMESTORE_QueueEntry *qe;
608 struct GNUNET_NAMESTORE_ZoneIterator *ze;
611 uint32_t r_id = UINT32_MAX;
619 size = ntohs (msg->size);
620 type = ntohs (msg->type);
622 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
625 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
626 GNUNET_TIME_UNIT_FOREVER_REL);
630 gm = (struct GNUNET_NAMESTORE_Header *) msg;
631 r_id = ntohl (gm->r_id);
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
635 /* Find matching operation */
638 /* No matching pending operation found */
640 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
641 GNUNET_TIME_UNIT_FOREVER_REL);
645 /* Is it a record related operation ? */
646 for (qe = h->op_head; qe != NULL; qe = qe->next)
648 if (qe->op_id == r_id)
653 manage_record_operations (qe, msg, type, size);
656 /* Is it a zone iteration operation ? */
657 for (ze = h->z_head; ze != NULL; ze = ze->next)
659 if (ze->op_id == r_id)
664 manage_zone_operations (ze, msg, type, size);
667 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
668 GNUNET_TIME_UNIT_FOREVER_REL);
670 if (GNUNET_YES == h->reconnect)
677 * Transmit messages from the message queue to the service
678 * (if there are any, and if we are not already trying).
680 * @param h handle to use
683 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
687 * We can now transmit a message to NAMESTORE. Do it.
689 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
690 * @param size number of bytes we can transmit
691 * @param buf where to copy the messages
692 * @return number of bytes copied into buf
695 transmit_message_to_namestore (void *cls, size_t size, void *buf)
697 struct GNUNET_NAMESTORE_Handle *h = cls;
698 struct PendingMessage *p;
703 if ((size == 0) || (buf == NULL))
710 while ((NULL != (p = h->pending_head)) && (p->size <= size))
712 memcpy (&cbuf[ret], &p[1], p->size);
715 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
716 if (GNUNET_YES == p->is_init)
717 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
718 GNUNET_TIME_UNIT_FOREVER_REL);
727 * Transmit messages from the message queue to the service
728 * (if there are any, and if we are not already trying).
730 * @param h handle to use
733 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
735 struct PendingMessage *p;
739 if (NULL == (p = h->pending_head))
741 if (NULL == h->client)
742 return; /* currently reconnecting */
744 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
745 GNUNET_TIME_UNIT_FOREVER_REL,
746 GNUNET_NO, &transmit_message_to_namestore,
752 * Reconnect to namestore service.
754 * @param h the handle to the namestore service
757 reconnect (struct GNUNET_NAMESTORE_Handle *h)
759 struct PendingMessage *p;
760 struct StartMessage *init;
762 GNUNET_assert (NULL == h->client);
763 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
764 GNUNET_assert (NULL != h->client);
766 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
768 p = GNUNET_malloc (sizeof (struct PendingMessage) +
769 sizeof (struct StartMessage));
770 p->size = sizeof (struct StartMessage);
771 p->is_init = GNUNET_YES;
772 init = (struct StartMessage *) &p[1];
773 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
774 init->header.size = htons (sizeof (struct StartMessage));
775 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
781 * Re-establish the connection to the service.
783 * @param cls handle to use to re-connect.
784 * @param tc scheduler context
787 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
789 struct GNUNET_NAMESTORE_Handle *h = cls;
791 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
797 * Disconnect from service and then reconnect.
799 * @param h our handle
802 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
804 h->reconnect = GNUNET_NO;
805 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
807 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
813 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
815 uint32_t op_id = h->op_id;
821 * Initialize the connection with the NAMESTORE service.
823 * @param cfg configuration to use
824 * @return handle to the GNS service, or NULL on error
826 struct GNUNET_NAMESTORE_Handle *
827 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
829 struct GNUNET_NAMESTORE_Handle *h;
831 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
833 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
838 struct DisconnectContext
840 struct GNUNET_NAMESTORE_Handle *h;
845 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
847 struct PendingMessage *p;
848 struct GNUNET_NAMESTORE_QueueEntry *q;
849 struct GNUNET_NAMESTORE_ZoneIterator *z;
850 struct GNUNET_NAMESTORE_Handle *h = cls;
851 GNUNET_assert (h != NULL);
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
853 while (NULL != (p = h->pending_head))
855 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
859 while (NULL != (q = h->op_head))
862 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
866 while (NULL != (z = h->z_head))
868 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
872 if (NULL != h->client)
874 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
877 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
879 GNUNET_SCHEDULER_cancel (h->reconnect_task);
880 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
887 transmit_disconnect_to_namestore (void *cls, size_t size, void *buf)
889 struct DisconnectContext * d_ctx = cls;
890 struct DisconnectMessage d_msg;
891 struct GNUNET_NAMESTORE_Handle *h = d_ctx->h;
894 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_DISCONNECT);
895 d_msg.header.size = htons (sizeof (struct DisconnectMessage));
896 d_msg.drop = htonl (d_ctx->drop);
899 if ((size == 0) || (buf == NULL) || (size < sizeof (struct DisconnectMessage)))
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to service \n", "NAMESTORE_DISCONNECT");
907 memcpy (buf, &d_msg, sizeof (struct DisconnectMessage));
908 res = sizeof (struct DisconnectMessage);
911 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
917 * Disconnect from the namestore service (and free associated
920 * @param h handle to the namestore
921 * @param drop set to GNUNET_YES to delete all data in namestore (!)
924 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
928 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
931 if (h->client != NULL)
933 struct DisconnectContext *d_ctx = GNUNET_malloc (sizeof (struct DisconnectContext));
937 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, sizeof (struct DisconnectMessage),
938 GNUNET_TIME_UNIT_FOREVER_REL,
939 GNUNET_NO, &transmit_disconnect_to_namestore,
944 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not send disconnect notification to namestore service, we are not connected!\n");
945 if (GNUNET_YES == drop)
946 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NAMESTORE will not drop content\n");
947 GNUNET_SCHEDULER_add_now (&clean_up_task, h);
953 * Store an item in the namestore. If the item is already present,
954 * the expiration time is updated to the max of the existing time and
955 * the new time. This API is used when we cache signatures from other
958 * @param h handle to the namestore
959 * @param zone_key public key of the zone
960 * @param name name that is being mapped (at most 255 characters long)
961 * @param expire when does the corresponding block in the DHT expire (until
962 * when should we never do a DHT lookup for the same name again)?
963 * @param rd_count number of entries in 'rd' array
964 * @param rd array of records with data to store
965 * @param signature signature for all the records in the zone under the given name
966 * @param cont continuation to call when done
967 * @param cont_cls closure for cont
968 * @return handle to abort the request
970 struct GNUNET_NAMESTORE_QueueEntry *
971 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
972 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
974 struct GNUNET_TIME_Absolute expire,
975 unsigned int rd_count,
976 const struct GNUNET_NAMESTORE_RecordData *rd,
977 const struct GNUNET_CRYPTO_RsaSignature *signature,
978 GNUNET_NAMESTORE_ContinuationWithStatus cont,
981 struct GNUNET_NAMESTORE_QueueEntry *qe;
982 struct PendingMessage *pe;
984 /* pointer to elements */
991 size_t rd_ser_len = 0;
992 size_t pubkey_len = 0;
995 GNUNET_assert (NULL != h);
996 GNUNET_assert (NULL != zone_key);
997 GNUNET_assert (NULL != name);
998 GNUNET_assert (NULL != rd);
999 GNUNET_assert (NULL != signature);
1001 name_len = strlen(name) + 1;
1009 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1012 qe->cont_cls = cont_cls;
1014 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1017 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1018 char rd_ser[rd_ser_len];
1019 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1021 pubkey_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1022 struct RecordPutMessage * msg;
1023 msg_size = sizeof (struct RecordPutMessage) + pubkey_len + name_len + rd_ser_len;
1025 /* create msg here */
1026 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1027 pe->size = msg_size;
1028 pe->is_init = GNUNET_NO;
1029 msg = (struct RecordPutMessage *) &pe[1];
1030 zone_key_tmp = (char *) &msg[1];
1031 name_tmp = (char *) &zone_key_tmp[pubkey_len];
1032 rd_tmp = &name_tmp[name_len];
1034 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
1035 msg->gns_header.header.size = htons (msg_size);
1036 msg->gns_header.r_id = htonl (rid);
1037 msg->key_len = htons (pubkey_len);
1038 memcpy (zone_key_tmp, zone_key, pubkey_len);
1039 msg->signature = *signature;
1040 msg->name_len = htons (name_len);
1041 memcpy (name_tmp, name, name_len);
1042 msg->expire = GNUNET_TIME_absolute_hton (expire);
1043 msg->rd_len = htons (rd_ser_len);
1044 msg->rd_count = htons (rd_count);
1046 memcpy (rd_tmp, rd_ser, rd_ser_len);
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1050 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1058 * Check if a signature is valid. This API is used by the GNS Block
1059 * to validate signatures received from the network.
1061 * @param public_key public key of the zone
1062 * @param name name that is being mapped (at most 255 characters long)
1063 * @param rd_count number of entries in 'rd' array
1064 * @param rd array of records with data to store
1065 * @param signature signature for all the records in the zone under the given name
1066 * @return GNUNET_OK if the signature is valid
1069 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1071 unsigned int rd_count,
1072 const struct GNUNET_NAMESTORE_RecordData *rd,
1073 const struct GNUNET_CRYPTO_RsaSignature *signature)
1075 int res = GNUNET_SYSERR;
1076 size_t rd_ser_len = 0;
1077 size_t name_len = 0;
1080 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1082 GNUNET_assert (public_key != NULL);
1083 GNUNET_assert (name != NULL);
1084 GNUNET_assert (signature != NULL);
1086 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1087 char rd_ser[rd_ser_len];
1088 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1090 name_len = strlen (name) + 1;
1092 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1093 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1094 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1095 name_tmp = (char *) &sig_purpose[1];
1096 rd_tmp = &name_tmp[name_len];
1097 memcpy (name_tmp, name, name_len);
1098 memcpy (rd_tmp, rd_ser, rd_ser_len);
1100 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1102 GNUNET_free (sig_purpose);
1108 * Store an item in the namestore. If the item is already present,
1109 * the expiration time is updated to the max of the existing time and
1110 * the new time. This API is used by the authority of a zone.
1112 * @param h handle to the namestore
1113 * @param pkey private key of the zone
1114 * @param name name that is being mapped (at most 255 characters long)
1115 * @param rd record data to store
1116 * @param cont continuation to call when done
1117 * @param cont_cls closure for cont
1118 * @return handle to abort the request
1120 struct GNUNET_NAMESTORE_QueueEntry *
1121 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1122 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1124 const struct GNUNET_NAMESTORE_RecordData *rd,
1125 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1128 struct GNUNET_NAMESTORE_QueueEntry *qe;
1129 struct PendingMessage *pe;
1133 size_t rd_ser_len = 0;
1134 size_t msg_size = 0;
1135 size_t name_len = 0;
1139 GNUNET_assert (NULL != h);
1140 GNUNET_assert (NULL != pkey);
1141 GNUNET_assert (NULL != name);
1142 GNUNET_assert (NULL != rd);
1144 name_len = strlen(name) + 1;
1152 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1155 qe->cont_cls = cont_cls;
1157 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1160 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1161 GNUNET_assert (pkey_enc != NULL);
1162 key_len = ntohs (pkey_enc->len);
1164 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1165 char rd_ser[rd_ser_len];
1166 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1168 struct RecordCreateMessage * msg;
1169 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1171 /* create msg here */
1172 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1173 pe->size = msg_size;
1174 pe->is_init = GNUNET_NO;
1175 msg = (struct RecordCreateMessage *) &pe[1];
1177 pkey_tmp = (char *) &msg[1];
1178 name_tmp = &pkey_tmp[key_len];
1179 rd_tmp = &name_tmp[name_len];
1181 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1182 msg->gns_header.header.size = htons (msg_size);
1183 msg->gns_header.r_id = htonl (rid);
1184 msg->name_len = htons (name_len);
1185 msg->rd_count = htons (1);
1186 msg->rd_len = htons (rd_ser_len);
1187 msg->pkey_len = htons (key_len);
1188 memcpy (pkey_tmp, pkey_enc, key_len);
1189 memcpy (name_tmp, name, name_len);
1190 memcpy (rd_tmp, rd_ser, rd_ser_len);
1191 GNUNET_free (pkey_enc);
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1195 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1202 * Explicitly remove some content from the database. The
1203 * "cont"inuation will be called with status "GNUNET_OK" if content
1204 * was removed, "GNUNET_NO" if no matching entry was found and
1205 * "GNUNET_SYSERR" on all other types of errors.
1206 * This API is used by the authority of a zone.
1208 * @param h handle to the namestore
1209 * @param pkey private key of the zone
1210 * @param name name that is being mapped (at most 255 characters long)
1211 * @param rd record data
1212 * @param cont continuation to call when done
1213 * @param cont_cls closure for cont
1214 * @return handle to abort the request
1216 struct GNUNET_NAMESTORE_QueueEntry *
1217 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1218 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1220 const struct GNUNET_NAMESTORE_RecordData *rd,
1221 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1224 struct GNUNET_NAMESTORE_QueueEntry *qe;
1225 struct PendingMessage *pe;
1229 size_t rd_ser_len = 0;
1230 size_t msg_size = 0;
1231 size_t name_len = 0;
1235 GNUNET_assert (NULL != h);
1238 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1241 qe->cont_cls = cont_cls;
1243 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1246 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1247 GNUNET_assert (pkey_enc != NULL);
1248 key_len = ntohs (pkey_enc->len);
1250 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1251 char rd_ser[rd_ser_len];
1252 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1254 name_len = strlen (name) + 1;
1256 struct RecordRemoveMessage * msg;
1257 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1259 /* create msg here */
1260 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1261 pe->size = msg_size;
1262 pe->is_init = GNUNET_NO;
1263 msg = (struct RecordRemoveMessage *) &pe[1];
1265 pkey_tmp = (char *) &msg[1];
1266 name_tmp = &pkey_tmp[key_len];
1267 rd_tmp = &name_tmp[name_len];
1269 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1270 msg->gns_header.header.size = htons (msg_size);
1271 msg->gns_header.r_id = htonl (rid);
1272 msg->name_len = htons (name_len);
1273 msg->rd_len = htons (rd_ser_len);
1274 msg->rd_count = htons (1);
1275 msg->key_len = htons (key_len);
1276 memcpy (pkey_tmp, pkey_enc, key_len);
1277 memcpy (name_tmp, name, name_len);
1278 memcpy (rd_tmp, rd_ser, rd_ser_len);
1280 GNUNET_free (pkey_enc);
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1284 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1291 * Get a result for a particular key from the namestore. The processor
1292 * will only be called once.
1294 * @param h handle to the namestore
1295 * @param zone zone to look up a record from
1296 * @param name name to look up
1297 * @param record_type desired record type, 0 for all
1298 * @param proc function to call on the matching records, or with
1299 * NULL (rd_count == 0) if there are no matching records
1300 * @param proc_cls closure for proc
1301 * @return a handle that can be used to
1304 struct GNUNET_NAMESTORE_QueueEntry *
1305 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1306 const GNUNET_HashCode *zone,
1308 uint32_t record_type,
1309 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1311 struct GNUNET_NAMESTORE_QueueEntry *qe;
1312 struct PendingMessage *pe;
1313 size_t msg_size = 0;
1314 size_t name_len = 0;
1317 GNUNET_assert (NULL != h);
1318 GNUNET_assert (NULL != zone);
1319 GNUNET_assert (NULL != name);
1321 name_len = strlen (name) + 1;
1322 if ((name_len == 0) || (name_len > 256))
1329 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1332 qe->proc_cls = proc_cls;
1334 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1337 msg_size = sizeof (struct LookupNameMessage) + name_len;
1338 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1340 /* create msg here */
1341 struct LookupNameMessage * msg;
1342 pe->size = msg_size;
1343 pe->is_init = GNUNET_NO;
1344 msg = (struct LookupNameMessage *) &pe[1];
1345 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1346 msg->gns_header.header.size = htons (msg_size);
1347 msg->gns_header.r_id = htonl (rid);
1348 msg->record_type = htonl (record_type);
1350 msg->name_len = htonl (name_len);
1351 memcpy (&msg[1], name, name_len);
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1355 /* transmit message */
1356 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1364 * Look for an existing PKEY delegation record for a given public key.
1365 * Returns at most one result to the processor.
1367 * @param h handle to the namestore
1368 * @param zone hash of public key of the zone to look up in, never NULL
1369 * @param value_zone hash of the public key of the target zone (value), never NULL
1370 * @param proc function to call on the matching records, or with
1371 * NULL (rd_count == 0) if there are no matching records
1372 * @param proc_cls closure for proc
1373 * @return a handle that can be used to
1376 struct GNUNET_NAMESTORE_QueueEntry *
1377 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1378 const GNUNET_HashCode *zone,
1379 const GNUNET_HashCode *value_zone,
1380 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1382 struct GNUNET_NAMESTORE_QueueEntry *qe;
1383 struct PendingMessage *pe;
1384 size_t msg_size = 0;
1387 GNUNET_assert (NULL != h);
1388 GNUNET_assert (NULL != zone);
1389 GNUNET_assert (NULL != value_zone);
1392 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1395 qe->proc_cls = proc_cls;
1397 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1400 msg_size = sizeof (struct ZoneToNameMessage);
1401 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1403 /* create msg here */
1404 struct ZoneToNameMessage * msg;
1405 pe->size = msg_size;
1406 pe->is_init = GNUNET_NO;
1407 msg = (struct ZoneToNameMessage *) &pe[1];
1408 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1409 msg->gns_header.header.size = htons (msg_size);
1410 msg->gns_header.r_id = htonl (rid);
1412 msg->value_zone = *value_zone;
1414 char * z_tmp = strdup (GNUNET_h2s (zone));
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1416 "NAMESTORE_ZONE_TO_NAME",
1418 GNUNET_h2s (value_zone));
1419 GNUNET_free (z_tmp);
1421 /* transmit message */
1422 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1431 * Starts a new zone iteration (used to periodically PUT all of our
1432 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1433 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1434 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1435 * immediately, and then again after
1436 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1438 * @param h handle to the namestore
1439 * @param zone zone to access, NULL for all zones
1440 * @param must_have_flags flags that must be set for the record to be returned
1441 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1442 * @param proc function to call on each name from the zone; it
1443 * will be called repeatedly with a value (if available)
1444 * and always once at the end with a name of NULL.
1445 * @param proc_cls closure for proc
1446 * @return an iterator handle to use for iteration
1448 struct GNUNET_NAMESTORE_ZoneIterator *
1449 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1450 const GNUNET_HashCode *zone,
1451 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1452 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1453 GNUNET_NAMESTORE_RecordProcessor proc,
1456 struct GNUNET_NAMESTORE_ZoneIterator *it;
1457 struct PendingMessage *pe;
1458 size_t msg_size = 0;
1461 GNUNET_assert (NULL != h);
1462 GNUNET_assert (NULL != zone);
1465 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1468 it->proc_cls = proc;
1471 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1474 msg_size = sizeof (struct ZoneIterationStartMessage);
1475 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1477 /* create msg here */
1478 struct ZoneIterationStartMessage * msg;
1479 pe->size = msg_size;
1480 pe->is_init = GNUNET_NO;
1481 msg = (struct ZoneIterationStartMessage *) &pe[1];
1482 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1483 msg->gns_header.header.size = htons (msg_size);
1484 msg->gns_header.r_id = htonl (rid);
1486 msg->must_have_flags = ntohs (must_have_flags);
1487 msg->must_not_have_flags = ntohs (must_not_have_flags);
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1491 /* transmit message */
1492 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1500 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1501 * for the next record.
1503 * @param it the iterator
1506 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1508 struct GNUNET_NAMESTORE_Handle *h;
1509 struct PendingMessage *pe;
1510 size_t msg_size = 0;
1512 GNUNET_assert (NULL != it);
1516 msg_size = sizeof (struct ZoneIterationNextMessage);
1517 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1519 /* create msg here */
1520 struct ZoneIterationNextMessage * msg;
1521 pe->size = msg_size;
1522 pe->is_init = GNUNET_NO;
1523 msg = (struct ZoneIterationNextMessage *) &pe[1];
1524 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1525 msg->gns_header.header.size = htons (msg_size);
1526 msg->gns_header.r_id = htonl (it->op_id);
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1530 /* transmit message */
1531 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1537 * Stops iteration and releases the namestore handle for further calls.
1539 * @param it the iterator
1542 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1544 GNUNET_assert (NULL != it);
1545 struct PendingMessage *pe;
1546 size_t msg_size = 0;
1547 struct GNUNET_NAMESTORE_Handle *h = it->h;
1550 msg_size = sizeof (struct ZoneIterationStopMessage);
1551 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1553 /* create msg here */
1554 struct ZoneIterationStopMessage * msg;
1555 pe->size = msg_size;
1556 pe->is_init = GNUNET_NO;
1557 msg = (struct ZoneIterationStopMessage *) &pe[1];
1558 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1559 msg->gns_header.header.size = htons (msg_size);
1560 msg->gns_header.r_id = htonl (it->op_id);
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1564 /* transmit message */
1565 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1571 * Cancel a namestore operation. The final callback from the
1572 * operation must not have been done yet.
1574 * @param qe operation to cancel
1577 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1579 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1581 GNUNET_assert (qe != NULL);
1583 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1588 /* end of namestore_api.c */