2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file namestore/namestore_api.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_gns_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "namestore.h"
41 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-api",__VA_ARGS__)
44 * We grant the namestore up to 1 minute of latency, if it is slower than
45 * that, store queries will fail.
47 #define NAMESTORE_DELAY_TOLERANCE GNUNET_TIME_UNIT_MINUTES
50 * An QueueEntry used to store information for a pending
51 * NAMESTORE record operation
53 struct GNUNET_NAMESTORE_QueueEntry
59 struct GNUNET_NAMESTORE_QueueEntry *next;
64 struct GNUNET_NAMESTORE_QueueEntry *prev;
67 * Main handle to access the namestore.
69 struct GNUNET_NAMESTORE_Handle *h;
72 * Continuation to call
74 GNUNET_NAMESTORE_ContinuationWithStatus cont;
77 * Closure for @e cont.
82 * Function to call with the records we get back; or NULL.
84 GNUNET_NAMESTORE_RecordMonitor proc;
87 * Closure for @e proc.
92 * Function to call on errors.
94 GNUNET_SCHEDULER_TaskCallback error_cb;
97 * Closure for @e error_cb.
102 * Envelope of the message to send to the service, if not yet
105 struct GNUNET_MQ_Envelope *env;
108 * Task scheduled to warn us if the namestore is way too slow.
110 struct GNUNET_SCHEDULER_Task *timeout_task;
113 * The operation id this zone iteration operation has
121 * Handle for a zone iterator operation
123 struct GNUNET_NAMESTORE_ZoneIterator
129 struct GNUNET_NAMESTORE_ZoneIterator *next;
134 struct GNUNET_NAMESTORE_ZoneIterator *prev;
137 * Main handle to access the namestore.
139 struct GNUNET_NAMESTORE_Handle *h;
142 * Function to call on completion.
144 GNUNET_SCHEDULER_TaskCallback finish_cb;
147 * Closure for @e error_cb.
152 * The continuation to call with the results
154 GNUNET_NAMESTORE_RecordMonitor proc;
157 * Closure for @e proc.
162 * Function to call on errors.
164 GNUNET_SCHEDULER_TaskCallback error_cb;
167 * Closure for @e error_cb.
172 * Envelope of the message to send to the service, if not yet
175 struct GNUNET_MQ_Envelope *env;
178 * Private key of the zone.
180 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
183 * The operation id this zone iteration operation has
191 * Connection to the NAMESTORE service.
193 struct GNUNET_NAMESTORE_Handle
197 * Configuration to use.
199 const struct GNUNET_CONFIGURATION_Handle *cfg;
202 * Connection to the service (if available).
204 struct GNUNET_MQ_Handle *mq;
207 * Head of pending namestore queue entries
209 struct GNUNET_NAMESTORE_QueueEntry *op_head;
212 * Tail of pending namestore queue entries
214 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
217 * Head of pending namestore zone iterator entries
219 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
222 * Tail of pending namestore zone iterator entries
224 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
229 struct GNUNET_SCHEDULER_Task *reconnect_task;
232 * Delay introduced before we reconnect.
234 struct GNUNET_TIME_Relative reconnect_delay;
237 * Should we reconnect to service due to some serious error?
242 * The last operation id used for a NAMESTORE operation
244 uint32_t last_op_id_used;
250 * Disconnect from service and then reconnect.
252 * @param h our handle
255 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
259 * Find the queue entry that matches the @a rid
261 * @param h namestore handle
262 * @param rid id to look up
263 * @return NULL if @a rid was not found
265 static struct GNUNET_NAMESTORE_QueueEntry *
266 find_qe (struct GNUNET_NAMESTORE_Handle *h,
269 struct GNUNET_NAMESTORE_QueueEntry *qe;
271 for (qe = h->op_head; qe != NULL; qe = qe->next)
272 if (qe->op_id == rid)
279 * Find the zone iteration entry that matches the @a rid
281 * @param h namestore handle
282 * @param rid id to look up
283 * @return NULL if @a rid was not found
285 static struct GNUNET_NAMESTORE_ZoneIterator *
286 find_zi (struct GNUNET_NAMESTORE_Handle *h,
289 struct GNUNET_NAMESTORE_ZoneIterator *ze;
291 for (ze = h->z_head; ze != NULL; ze = ze->next)
292 if (ze->op_id == rid)
301 * @param qe entry to free
304 free_qe (struct GNUNET_NAMESTORE_QueueEntry *qe)
306 struct GNUNET_NAMESTORE_Handle *h = qe->h;
308 GNUNET_CONTAINER_DLL_remove (h->op_head,
312 GNUNET_MQ_discard (qe->env);
313 if (NULL != qe->timeout_task)
314 GNUNET_SCHEDULER_cancel (qe->timeout_task);
322 * @param ze entry to free
325 free_ze (struct GNUNET_NAMESTORE_ZoneIterator *ze)
327 struct GNUNET_NAMESTORE_Handle *h = ze->h;
329 GNUNET_CONTAINER_DLL_remove (h->z_head,
333 GNUNET_MQ_discard (ze->env);
339 * Check that @a rd_buf of lenght @a rd_len contains
340 * @a rd_count records.
342 * @param rd_len length of @a rd_buf
343 * @param rd_buf buffer with serialized records
344 * @param rd_count number of records expected
345 * @return #GNUNET_OK if @a rd_buf is well-formed
348 check_rd (size_t rd_len,
350 unsigned int rd_count)
352 struct GNUNET_GNSRECORD_Data rd[rd_count];
355 GNUNET_GNSRECORD_records_deserialize (rd_len,
361 return GNUNET_SYSERR;
368 * Handle an incoming message of type
369 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
372 * @param msg the message we received
375 handle_record_store_response (void *cls,
376 const struct RecordStoreResponseMessage *msg)
378 struct GNUNET_NAMESTORE_Handle *h = cls;
379 struct GNUNET_NAMESTORE_QueueEntry *qe;
384 ntohl (msg->gns_header.r_id));
385 res = ntohl (msg->op_result);
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "Received RECORD_STORE_RESPONSE with result %d\n",
389 /* TODO: add actual error message from namestore to response... */
390 if (GNUNET_SYSERR == res)
391 emsg = _("Namestore failed to store record\n");
394 if (NULL != qe->cont)
395 qe->cont (qe->cont_cls,
403 * Check validity of an incoming message of type
404 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
407 * @param msg the message we received
408 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
411 check_lookup_result (void *cls,
412 const struct LabelLookupResponseMessage *msg)
421 rd_len = ntohs (msg->rd_len);
422 msg_len = ntohs (msg->gns_header.header.size);
423 name_len = ntohs (msg->name_len);
424 exp_msg_len = sizeof (*msg) + name_len + rd_len;
425 if (msg_len != exp_msg_len)
428 return GNUNET_SYSERR;
430 name = (const char *) &msg[1];
431 if ( (name_len > 0) &&
432 ('\0' != name[name_len -1]) )
435 return GNUNET_SYSERR;
437 if (GNUNET_NO == ntohs (msg->found))
439 if (0 != ntohs (msg->rd_count))
442 return GNUNET_SYSERR;
446 return check_rd (rd_len,
448 ntohs (msg->rd_count));
453 * Handle an incoming message of type
454 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
457 * @param msg the message we received
460 handle_lookup_result (void *cls,
461 const struct LabelLookupResponseMessage *msg)
463 struct GNUNET_NAMESTORE_Handle *h = cls;
464 struct GNUNET_NAMESTORE_QueueEntry *qe;
469 unsigned int rd_count;
471 LOG (GNUNET_ERROR_TYPE_DEBUG,
472 "Received RECORD_LOOKUP_RESULT\n");
474 ntohl (msg->gns_header.r_id));
477 rd_len = ntohs (msg->rd_len);
478 rd_count = ntohs (msg->rd_count);
479 name_len = ntohs (msg->name_len);
480 name = (const char *) &msg[1];
481 if (GNUNET_NO == ntohs (msg->found))
483 /* label was not in namestore */
484 if (NULL != qe->proc)
485 qe->proc (qe->proc_cls,
494 rd_tmp = &name[name_len];
496 struct GNUNET_GNSRECORD_Data rd[rd_count];
498 GNUNET_assert (GNUNET_OK ==
499 GNUNET_GNSRECORD_records_deserialize (rd_len,
505 if (NULL != qe->proc)
506 qe->proc (qe->proc_cls,
510 (rd_count > 0) ? rd : NULL);
517 * Handle an incoming message of type
518 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
521 * @param msg the message we received
522 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
525 check_record_result (void *cls,
526 const struct RecordResultMessage *msg)
528 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
535 rd_len = ntohs (msg->rd_len);
536 msg_len = ntohs (msg->gns_header.header.size);
537 name_len = ntohs (msg->name_len);
538 if (0 != ntohs (msg->reserved))
541 return GNUNET_SYSERR;
543 if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
546 return GNUNET_SYSERR;
548 name = (const char *) &msg[1];
549 if ( (0 == name_len) ||
550 ('\0' != name[name_len -1]) )
553 return GNUNET_SYSERR;
555 if (0 == memcmp (&msg->private_key,
557 sizeof (priv_dummy)) )
560 return GNUNET_SYSERR;
562 return check_rd (rd_len,
564 ntohs (msg->rd_count));
569 * Handle an incoming message of type
570 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
573 * @param msg the message we received
576 handle_record_result (void *cls,
577 const struct RecordResultMessage *msg)
579 struct GNUNET_NAMESTORE_Handle *h = cls;
580 struct GNUNET_NAMESTORE_QueueEntry *qe;
581 struct GNUNET_NAMESTORE_ZoneIterator *ze;
586 unsigned int rd_count;
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
589 "Received RECORD_RESULT\n");
590 rd_len = ntohs (msg->rd_len);
591 rd_count = ntohs (msg->rd_count);
592 name_len = ntohs (msg->name_len);
594 ntohl (msg->gns_header.r_id));
596 ntohl (msg->gns_header.r_id));
599 return; /* rid not found */
603 GNUNET_break (0); /* rid ambigous */
607 name = (const char *) &msg[1];
608 rd_tmp = &name[name_len];
610 struct GNUNET_GNSRECORD_Data rd[rd_count];
612 GNUNET_assert (GNUNET_OK ==
613 GNUNET_GNSRECORD_records_deserialize(rd_len,
621 if (NULL != qe->proc)
622 qe->proc (qe->proc_cls,
626 (rd_count > 0) ? rd : NULL);
632 if (NULL != ze->proc)
633 ze->proc (ze->proc_cls,
646 * Handle an incoming message of type
647 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END
650 * @param msg the message we received
653 handle_record_result_end (void *cls,
654 const struct GNUNET_NAMESTORE_Header *msg)
656 struct GNUNET_NAMESTORE_Handle *h = cls;
657 struct GNUNET_NAMESTORE_QueueEntry *qe;
658 struct GNUNET_NAMESTORE_ZoneIterator *ze;
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "Received RECORD_RESULT_END\n");
668 return; /* rid not found */
672 GNUNET_break (0); /* rid ambigous */
676 LOG (GNUNET_ERROR_TYPE_DEBUG,
677 "Zone iteration completed!\n");
684 if (NULL != ze->finish_cb)
685 ze->finish_cb (ze->finish_cb_cls);
691 * Handle an incoming message of type
692 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
694 * @param qe the respective entry in the message queue
695 * @param msg the message we received
696 * @return #GNUNET_OK on success, #GNUNET_SYSERR if message malformed
699 check_zone_to_name_response (void *cls,
700 const struct ZoneToNameResponseMessage *msg)
704 const char *name_tmp;
707 if (GNUNET_OK != ntohs (msg->res))
709 name_len = ntohs (msg->name_len);
710 rd_ser_len = ntohs (msg->rd_len);
711 if (ntohs (msg->gns_header.header.size) !=
712 sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
715 return GNUNET_SYSERR;
717 name_tmp = (const char *) &msg[1];
718 if ( (name_len > 0) &&
719 ('\0' != name_tmp[name_len -1]) )
722 return GNUNET_SYSERR;
724 return check_rd (rd_ser_len,
726 ntohs (msg->rd_count));
731 * Handle an incoming message of type
732 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
735 * @param msg the message we received
738 handle_zone_to_name_response (void *cls,
739 const struct ZoneToNameResponseMessage *msg)
741 struct GNUNET_NAMESTORE_Handle *h = cls;
742 struct GNUNET_NAMESTORE_QueueEntry *qe;
746 unsigned int rd_count;
747 const char *name_tmp;
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 "Received ZONE_TO_NAME_RESPONSE\n");
753 ntohl (msg->gns_header.r_id));
754 res = ntohs (msg->res);
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "An error occurred during zone to name operation\n");
762 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 "Namestore has no result for zone to name mapping \n");
764 if (NULL != qe->proc)
765 qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
769 LOG (GNUNET_ERROR_TYPE_DEBUG,
770 "Namestore has result for zone to name mapping \n");
771 name_len = ntohs (msg->name_len);
772 rd_count = ntohs (msg->rd_count);
773 rd_ser_len = ntohs (msg->rd_len);
774 name_tmp = (const char *) &msg[1];
775 rd_tmp = &name_tmp[name_len];
777 struct GNUNET_GNSRECORD_Data rd[rd_count];
779 GNUNET_assert (GNUNET_OK ==
780 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
784 /* normal end, call continuation with result */
785 if (NULL != qe->proc)
786 qe->proc (qe->proc_cls,
791 /* return is important here: break would call continuation with error! */
800 /* error case, call continuation with error */
801 if (NULL != qe->error_cb)
802 qe->error_cb (qe->error_cb_cls);
809 * Generic error handler, called with the appropriate error code and
810 * the same closure specified at the creation of the message queue.
811 * Not every message queue implementation supports an error handler.
813 * @param cls closure with the `struct GNUNET_NAMESTORE_Handle *`
814 * @param error error code
817 mq_error_handler (void *cls,
818 enum GNUNET_MQ_Error error)
820 struct GNUNET_NAMESTORE_Handle *h = cls;
828 * Reconnect to namestore service.
830 * @param h the handle to the NAMESTORE service
833 reconnect (struct GNUNET_NAMESTORE_Handle *h)
835 struct GNUNET_MQ_MessageHandler handlers[] = {
836 GNUNET_MQ_hd_fixed_size (record_store_response,
837 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE,
838 struct RecordStoreResponseMessage,
840 GNUNET_MQ_hd_var_size (zone_to_name_response,
841 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE,
842 struct ZoneToNameResponseMessage,
844 GNUNET_MQ_hd_var_size (record_result,
845 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
846 struct RecordResultMessage,
848 GNUNET_MQ_hd_fixed_size (record_result_end,
849 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END,
850 struct GNUNET_NAMESTORE_Header,
852 GNUNET_MQ_hd_var_size (lookup_result,
853 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE,
854 struct LabelLookupResponseMessage,
856 GNUNET_MQ_handler_end ()
858 struct GNUNET_NAMESTORE_ZoneIterator *it;
859 struct GNUNET_NAMESTORE_QueueEntry *qe;
861 GNUNET_assert (NULL == h->mq);
862 h->mq = GNUNET_CLIENT_connect (h->cfg,
869 /* re-transmit pending requests that waited for a reconnect... */
870 for (it = h->z_head; NULL != it; it = it->next)
872 GNUNET_MQ_send (h->mq,
876 for (qe = h->op_head; NULL != qe; qe = qe->next)
878 GNUNET_MQ_send (h->mq,
886 * Re-establish the connection to the service.
888 * @param cls handle to use to re-connect.
891 reconnect_task (void *cls)
893 struct GNUNET_NAMESTORE_Handle *h = cls;
895 h->reconnect_task = NULL;
901 * Disconnect from service and then reconnect.
903 * @param h our handle
906 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
908 struct GNUNET_NAMESTORE_ZoneIterator *ze;
909 struct GNUNET_NAMESTORE_QueueEntry *qe;
911 GNUNET_MQ_destroy (h->mq);
913 while (NULL != (ze = h->z_head))
915 if (NULL != ze->error_cb)
916 ze->error_cb (ze->error_cb_cls);
919 while (NULL != (qe = h->op_head))
921 if (NULL != qe->error_cb)
922 qe->error_cb (qe->error_cb_cls);
923 if (NULL != qe->cont)
924 qe->cont (qe->cont_cls,
926 "failure in communication with namestore service");
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 "Reconnecting to namestore\n");
932 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
933 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
940 * Get a fresh operation id to distinguish between namestore requests
942 * @param h the namestore handle
943 * @return next operation id to use
946 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
948 return h->last_op_id_used++;
953 * Initialize the connection with the NAMESTORE service.
955 * @param cfg configuration to use
956 * @return handle to the GNS service, or NULL on error
958 struct GNUNET_NAMESTORE_Handle *
959 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
961 struct GNUNET_NAMESTORE_Handle *h;
963 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
976 * Disconnect from the namestore service (and free associated
979 * @param h handle to the namestore
982 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
984 struct GNUNET_NAMESTORE_QueueEntry *q;
985 struct GNUNET_NAMESTORE_ZoneIterator *z;
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
989 GNUNET_break (NULL == h->op_head);
990 while (NULL != (q = h->op_head))
992 GNUNET_CONTAINER_DLL_remove (h->op_head,
997 GNUNET_break (NULL == h->z_head);
998 while (NULL != (z = h->z_head))
1000 GNUNET_CONTAINER_DLL_remove (h->z_head,
1007 GNUNET_MQ_destroy (h->mq);
1010 if (NULL != h->reconnect_task)
1012 GNUNET_SCHEDULER_cancel (h->reconnect_task);
1013 h->reconnect_task = NULL;
1020 * Task launched to warn the user that the namestore is
1021 * excessively slow and that a query was thus dropped.
1023 * @param cls a `struct GNUNET_NAMESTORE_QueueEntry *`
1026 warn_delay (void *cls)
1028 struct GNUNET_NAMESTORE_QueueEntry *qe = cls;
1030 qe->timeout_task = NULL;
1031 LOG (GNUNET_ERROR_TYPE_WARNING,
1032 "Did not receive response from namestore after %s!\n",
1033 GNUNET_STRINGS_relative_time_to_string (NAMESTORE_DELAY_TOLERANCE,
1035 if (NULL != qe->cont)
1037 qe->cont (qe->cont_cls,
1042 GNUNET_NAMESTORE_cancel (qe);
1047 * Store an item in the namestore. If the item is already present,
1048 * it is replaced with the new record. Use an empty array to
1049 * remove all records under the given name.
1051 * @param h handle to the namestore
1052 * @param pkey private key of the zone
1053 * @param label name that is being mapped (at most 255 characters long)
1054 * @param rd_count number of records in the @a rd array
1055 * @param rd array of records with data to store
1056 * @param cont continuation to call when done
1057 * @param cont_cls closure for @a cont
1058 * @return handle to abort the request
1060 struct GNUNET_NAMESTORE_QueueEntry *
1061 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1062 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1064 unsigned int rd_count,
1065 const struct GNUNET_GNSRECORD_Data *rd,
1066 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1069 struct GNUNET_NAMESTORE_QueueEntry *qe;
1070 struct GNUNET_MQ_Envelope *env;
1076 struct RecordStoreMessage *msg;
1079 name_len = strlen (label) + 1;
1080 if (name_len > MAX_NAME_LEN)
1085 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1092 if (rd_ser_len > UINT16_MAX)
1097 rid = get_op_id (h);
1098 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1101 qe->cont_cls = cont_cls;
1103 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1108 env = GNUNET_MQ_msg_extra (msg,
1109 name_len + rd_ser_len,
1110 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1111 msg->gns_header.r_id = htonl (rid);
1112 msg->name_len = htons (name_len);
1113 msg->rd_count = htons (rd_count);
1114 msg->rd_len = htons (rd_ser_len);
1115 msg->reserved = htons (0);
1116 msg->private_key = *pkey;
1118 name_tmp = (char *) &msg[1];
1119 GNUNET_memcpy (name_tmp,
1122 rd_ser = &name_tmp[name_len];
1123 sret = GNUNET_GNSRECORD_records_serialize (rd_count,
1128 (sret != rd_ser_len) )
1134 GNUNET_assert (rd_ser_len == (size_t) sret);
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
1139 qe->timeout_task = GNUNET_SCHEDULER_add_delayed (NAMESTORE_DELAY_TOLERANCE,
1145 LOG (GNUNET_ERROR_TYPE_WARNING,
1146 "Delaying NAMESTORE_RECORD_STORE message as namestore is not ready!\n");
1150 GNUNET_MQ_send (h->mq,
1158 * Set the desired nick name for a zone
1160 * @param h handle to the namestore
1161 * @param pkey private key of the zone
1162 * @param nick the nick name to set
1163 * @param cont continuation to call when done
1164 * @param cont_cls closure for @a cont
1165 * @return handle to abort the request
1167 struct GNUNET_NAMESTORE_QueueEntry *
1168 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
1169 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1171 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1174 struct GNUNET_GNSRECORD_Data rd;
1178 memset (&rd, 0, sizeof (rd));
1180 rd.data_size = strlen (nick) +1;
1181 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
1182 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1183 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1184 return GNUNET_NAMESTORE_records_store (h,
1186 GNUNET_GNS_EMPTY_LABEL_AT,
1195 * Lookup an item in the namestore.
1197 * @param h handle to the namestore
1198 * @param pkey private key of the zone
1199 * @param label name that is being mapped (at most 255 characters long)
1200 * @param error_cb function to call on error (i.e. disconnect)
1201 * @param error_cb_cls closure for @a error_cb
1202 * @param rm function to call with the result (with 0 records if we don't have that label)
1203 * @param rm_cls closure for @a rm
1204 * @return handle to abort the request
1206 struct GNUNET_NAMESTORE_QueueEntry *
1207 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1208 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1210 GNUNET_SCHEDULER_TaskCallback error_cb,
1212 GNUNET_NAMESTORE_RecordMonitor rm,
1215 struct GNUNET_NAMESTORE_QueueEntry *qe;
1216 struct GNUNET_MQ_Envelope *env;
1217 struct LabelLookupMessage *msg;
1220 if (1 == (label_len = strlen (label) + 1))
1226 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1228 qe->error_cb = error_cb;
1229 qe->error_cb_cls = error_cb_cls;
1231 qe->proc_cls = rm_cls;
1232 qe->op_id = get_op_id(h);
1233 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1237 env = GNUNET_MQ_msg_extra (msg,
1239 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1240 msg->gns_header.r_id = htonl (qe->op_id);
1242 msg->label_len = htonl (label_len);
1243 GNUNET_memcpy (&msg[1],
1249 GNUNET_MQ_send (h->mq,
1256 * Look for an existing PKEY delegation record for a given public key.
1257 * Returns at most one result to the processor.
1259 * @param h handle to the namestore
1260 * @param zone public key of the zone to look up in, never NULL
1261 * @param value_zone public key of the target zone (value), never NULL
1262 * @param error_cb function to call on error (i.e. disconnect)
1263 * @param error_cb_cls closure for @a error_cb
1264 * @param proc function to call on the matching records, or with
1265 * NULL (rd_count == 0) if there are no matching records
1266 * @param proc_cls closure for @a proc
1267 * @return a handle that can be used to
1270 struct GNUNET_NAMESTORE_QueueEntry *
1271 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1272 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1273 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1274 GNUNET_SCHEDULER_TaskCallback error_cb,
1276 GNUNET_NAMESTORE_RecordMonitor proc,
1279 struct GNUNET_NAMESTORE_QueueEntry *qe;
1280 struct GNUNET_MQ_Envelope *env;
1281 struct ZoneToNameMessage *msg;
1285 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1287 qe->error_cb = error_cb;
1288 qe->error_cb_cls = error_cb_cls;
1290 qe->proc_cls = proc_cls;
1292 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1296 env = GNUNET_MQ_msg (msg,
1297 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1298 msg->gns_header.r_id = htonl (rid);
1300 msg->value_zone = *value_zone;
1304 GNUNET_MQ_send (h->mq,
1311 * Starts a new zone iteration (used to periodically PUT all of our
1312 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1313 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1314 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1315 * immediately, and then again after
1316 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1318 * @param h handle to the namestore
1319 * @param zone zone to access, NULL for all zones
1320 * @param error_cb function to call on error (i.e. disconnect)
1321 * @param error_cb_cls closure for @a error_cb
1322 * @param proc function to call on each name from the zone; it
1323 * will be called repeatedly with a value (if available)
1324 * @param proc_cls closure for @a proc
1325 * @param finish_cb function to call on completion
1326 * @param finish_cb_cls closure for @a finish_cb
1327 * @return an iterator handle to use for iteration
1329 struct GNUNET_NAMESTORE_ZoneIterator *
1330 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1331 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1332 GNUNET_SCHEDULER_TaskCallback error_cb,
1334 GNUNET_NAMESTORE_RecordMonitor proc,
1336 GNUNET_SCHEDULER_TaskCallback finish_cb,
1337 void *finish_cb_cls)
1339 struct GNUNET_NAMESTORE_ZoneIterator *it;
1340 struct GNUNET_MQ_Envelope *env;
1341 struct ZoneIterationStartMessage *msg;
1344 LOG (GNUNET_ERROR_TYPE_DEBUG,
1345 "Sending ZONE_ITERATION_START message\n");
1346 rid = get_op_id (h);
1347 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1349 it->error_cb = error_cb;
1350 it->error_cb_cls = error_cb_cls;
1351 it->finish_cb = finish_cb;
1352 it->finish_cb_cls = finish_cb_cls;
1354 it->proc_cls = proc_cls;
1358 GNUNET_CONTAINER_DLL_insert_tail (h->z_head,
1361 env = GNUNET_MQ_msg (msg,
1362 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1363 msg->gns_header.r_id = htonl (rid);
1369 GNUNET_MQ_send (h->mq,
1376 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1377 * for the next record.
1379 * @param it the iterator
1380 * @param limit number of records to return to the iterator in one shot
1381 * (before #GNUNET_NAMESTORE_zone_iterator_next is to be called again)
1384 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it,
1387 struct GNUNET_NAMESTORE_Handle *h = it->h;
1388 struct ZoneIterationNextMessage *msg;
1389 struct GNUNET_MQ_Envelope *env;
1391 LOG (GNUNET_ERROR_TYPE_DEBUG,
1392 "Sending ZONE_ITERATION_NEXT message with limit %llu\n",
1393 (unsigned long long) limit);
1394 env = GNUNET_MQ_msg (msg,
1395 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1396 msg->gns_header.r_id = htonl (it->op_id);
1397 msg->limit = GNUNET_htonll (limit);
1398 GNUNET_MQ_send (h->mq,
1404 * Stops iteration and releases the namestore handle for further calls.
1406 * @param it the iterator
1409 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1411 struct GNUNET_NAMESTORE_Handle *h = it->h;
1412 struct GNUNET_MQ_Envelope *env;
1413 struct ZoneIterationStopMessage *msg;
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Sending ZONE_ITERATION_STOP message\n");
1419 env = GNUNET_MQ_msg (msg,
1420 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1421 msg->gns_header.r_id = htonl (it->op_id);
1422 GNUNET_MQ_send (h->mq,
1430 * Cancel a namestore operation. The final callback from the
1431 * operation must not have been done yet.
1433 * @param qe operation to cancel
1436 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1442 /* end of namestore_api.c */