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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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)
534 rd_len = ntohs (msg->rd_len);
535 msg_len = ntohs (msg->gns_header.header.size);
536 name_len = ntohs (msg->name_len);
537 if (0 != ntohs (msg->reserved))
540 return GNUNET_SYSERR;
542 if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
545 return GNUNET_SYSERR;
547 name = (const char *) &msg[1];
548 if ( (name_len > 0) &&
549 ('\0' != name[name_len -1]) )
552 return GNUNET_SYSERR;
554 return check_rd (rd_len,
556 ntohs (msg->rd_count));
561 * Handle an incoming message of type
562 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
565 * @param msg the message we received
568 handle_record_result (void *cls,
569 const struct RecordResultMessage *msg)
571 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
572 struct GNUNET_NAMESTORE_Handle *h = cls;
573 struct GNUNET_NAMESTORE_QueueEntry *qe;
574 struct GNUNET_NAMESTORE_ZoneIterator *ze;
579 unsigned int rd_count;
581 LOG (GNUNET_ERROR_TYPE_DEBUG,
582 "Received RECORD_RESULT\n");
583 rd_len = ntohs (msg->rd_len);
584 rd_count = ntohs (msg->rd_count);
585 name_len = ntohs (msg->name_len);
587 ntohl (msg->gns_header.r_id));
589 ntohl (msg->gns_header.r_id));
592 return; /* rid not found */
596 GNUNET_break (0); /* rid ambigous */
600 if ( (0 == name_len) &&
601 (0 == (memcmp (&msg->private_key,
603 sizeof (priv_dummy)))) )
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "Zone iteration completed!\n");
613 if (NULL != ze->finish_cb)
614 ze->finish_cb (ze->finish_cb_cls);
619 name = (const char *) &msg[1];
620 rd_tmp = &name[name_len];
622 struct GNUNET_GNSRECORD_Data rd[rd_count];
624 GNUNET_assert (GNUNET_OK ==
625 GNUNET_GNSRECORD_records_deserialize(rd_len,
633 if (NULL != qe->proc)
634 qe->proc (qe->proc_cls,
638 (rd_count > 0) ? rd : NULL);
644 if (NULL != ze->proc)
645 ze->proc (ze->proc_cls,
658 * Handle an incoming message of type
659 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
661 * @param qe the respective entry in the message queue
662 * @param msg the message we received
663 * @return #GNUNET_OK on success, #GNUNET_SYSERR if message malformed
666 check_zone_to_name_response (void *cls,
667 const struct ZoneToNameResponseMessage *msg)
671 const char *name_tmp;
674 if (GNUNET_OK != ntohs (msg->res))
676 name_len = ntohs (msg->name_len);
677 rd_ser_len = ntohs (msg->rd_len);
678 if (ntohs (msg->gns_header.header.size) !=
679 sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
682 return GNUNET_SYSERR;
684 name_tmp = (const char *) &msg[1];
685 if ( (name_len > 0) &&
686 ('\0' != name_tmp[name_len -1]) )
689 return GNUNET_SYSERR;
691 return check_rd (rd_ser_len,
693 ntohs (msg->rd_count));
698 * Handle an incoming message of type
699 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
702 * @param msg the message we received
705 handle_zone_to_name_response (void *cls,
706 const struct ZoneToNameResponseMessage *msg)
708 struct GNUNET_NAMESTORE_Handle *h = cls;
709 struct GNUNET_NAMESTORE_QueueEntry *qe;
713 unsigned int rd_count;
714 const char *name_tmp;
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Received ZONE_TO_NAME_RESPONSE\n");
720 ntohl (msg->gns_header.r_id));
721 res = ntohs (msg->res);
725 LOG (GNUNET_ERROR_TYPE_DEBUG,
726 "An error occured during zone to name operation\n");
729 LOG (GNUNET_ERROR_TYPE_DEBUG,
730 "Namestore has no result for zone to name mapping \n");
731 if (NULL != qe->proc)
732 qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
736 LOG (GNUNET_ERROR_TYPE_DEBUG,
737 "Namestore has result for zone to name mapping \n");
738 name_len = ntohs (msg->name_len);
739 rd_count = ntohs (msg->rd_count);
740 rd_ser_len = ntohs (msg->rd_len);
741 name_tmp = (const char *) &msg[1];
742 rd_tmp = &name_tmp[name_len];
744 struct GNUNET_GNSRECORD_Data rd[rd_count];
746 GNUNET_assert (GNUNET_OK ==
747 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
751 /* normal end, call continuation with result */
752 if (NULL != qe->proc)
753 qe->proc (qe->proc_cls,
758 /* return is important here: break would call continuation with error! */
767 /* error case, call continuation with error */
768 if (NULL != qe->error_cb)
769 qe->error_cb (qe->error_cb_cls);
776 * Generic error handler, called with the appropriate error code and
777 * the same closure specified at the creation of the message queue.
778 * Not every message queue implementation supports an error handler.
780 * @param cls closure with the `struct GNUNET_NAMESTORE_Handle *`
781 * @param error error code
784 mq_error_handler (void *cls,
785 enum GNUNET_MQ_Error error)
787 struct GNUNET_NAMESTORE_Handle *h = cls;
795 * Reconnect to namestore service.
797 * @param h the handle to the NAMESTORE service
800 reconnect (struct GNUNET_NAMESTORE_Handle *h)
802 struct GNUNET_MQ_MessageHandler handlers[] = {
803 GNUNET_MQ_hd_fixed_size (record_store_response,
804 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE,
805 struct RecordStoreResponseMessage,
807 GNUNET_MQ_hd_var_size (zone_to_name_response,
808 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE,
809 struct ZoneToNameResponseMessage,
811 GNUNET_MQ_hd_var_size (record_result,
812 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT,
813 struct RecordResultMessage,
815 GNUNET_MQ_hd_var_size (lookup_result,
816 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE,
817 struct LabelLookupResponseMessage,
819 GNUNET_MQ_handler_end ()
821 struct GNUNET_NAMESTORE_ZoneIterator *it;
822 struct GNUNET_NAMESTORE_QueueEntry *qe;
824 GNUNET_assert (NULL == h->mq);
825 h->mq = GNUNET_CLIENT_connect (h->cfg,
832 /* re-transmit pending requests that waited for a reconnect... */
833 for (it = h->z_head; NULL != it; it = it->next)
835 GNUNET_MQ_send (h->mq,
839 for (qe = h->op_head; NULL != qe; qe = qe->next)
841 GNUNET_MQ_send (h->mq,
849 * Re-establish the connection to the service.
851 * @param cls handle to use to re-connect.
854 reconnect_task (void *cls)
856 struct GNUNET_NAMESTORE_Handle *h = cls;
858 h->reconnect_task = NULL;
864 * Disconnect from service and then reconnect.
866 * @param h our handle
869 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
871 struct GNUNET_NAMESTORE_ZoneIterator *ze;
872 struct GNUNET_NAMESTORE_QueueEntry *qe;
874 GNUNET_MQ_destroy (h->mq);
876 while (NULL != (ze = h->z_head))
878 if (NULL != ze->error_cb)
879 ze->error_cb (ze->error_cb_cls);
882 while (NULL != (qe = h->op_head))
884 if (NULL != qe->error_cb)
885 qe->error_cb (qe->error_cb_cls);
886 if (NULL != qe->cont)
887 qe->cont (qe->cont_cls,
889 "failure in communication with namestore service");
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Reconnecting to namestore\n");
895 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
896 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
903 * Get a fresh operation id to distinguish between namestore requests
905 * @param h the namestore handle
906 * @return next operation id to use
909 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
911 return h->last_op_id_used++;
916 * Initialize the connection with the NAMESTORE service.
918 * @param cfg configuration to use
919 * @return handle to the GNS service, or NULL on error
921 struct GNUNET_NAMESTORE_Handle *
922 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
924 struct GNUNET_NAMESTORE_Handle *h;
926 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
939 * Disconnect from the namestore service (and free associated
942 * @param h handle to the namestore
945 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
947 struct GNUNET_NAMESTORE_QueueEntry *q;
948 struct GNUNET_NAMESTORE_ZoneIterator *z;
950 LOG (GNUNET_ERROR_TYPE_DEBUG,
952 GNUNET_break (NULL == h->op_head);
953 while (NULL != (q = h->op_head))
955 GNUNET_CONTAINER_DLL_remove (h->op_head,
960 GNUNET_break (NULL == h->z_head);
961 while (NULL != (z = h->z_head))
963 GNUNET_CONTAINER_DLL_remove (h->z_head,
970 GNUNET_MQ_destroy (h->mq);
973 if (NULL != h->reconnect_task)
975 GNUNET_SCHEDULER_cancel (h->reconnect_task);
976 h->reconnect_task = NULL;
983 * Task launched to warn the user that the namestore is
984 * excessively slow and that a query was thus dropped.
986 * @param cls a `struct GNUNET_NAMESTORE_QueueEntry *`
989 warn_delay (void *cls)
991 struct GNUNET_NAMESTORE_QueueEntry *qe = cls;
993 qe->timeout_task = NULL;
994 LOG (GNUNET_ERROR_TYPE_WARNING,
995 "Did not receive response from namestore after %s!\n",
996 GNUNET_STRINGS_relative_time_to_string (NAMESTORE_DELAY_TOLERANCE,
998 if (NULL != qe->cont)
1000 qe->cont (qe->cont_cls,
1005 GNUNET_NAMESTORE_cancel (qe);
1010 * Store an item in the namestore. If the item is already present,
1011 * it is replaced with the new record. Use an empty array to
1012 * remove all records under the given name.
1014 * @param h handle to the namestore
1015 * @param pkey private key of the zone
1016 * @param label name that is being mapped (at most 255 characters long)
1017 * @param rd_count number of records in the @a rd array
1018 * @param rd array of records with data to store
1019 * @param cont continuation to call when done
1020 * @param cont_cls closure for @a cont
1021 * @return handle to abort the request
1023 struct GNUNET_NAMESTORE_QueueEntry *
1024 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1025 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1027 unsigned int rd_count,
1028 const struct GNUNET_GNSRECORD_Data *rd,
1029 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1032 struct GNUNET_NAMESTORE_QueueEntry *qe;
1033 struct GNUNET_MQ_Envelope *env;
1039 struct RecordStoreMessage *msg;
1042 name_len = strlen (label) + 1;
1043 if (name_len > MAX_NAME_LEN)
1048 rid = get_op_id (h);
1049 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1052 qe->cont_cls = cont_cls;
1054 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1059 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1061 env = GNUNET_MQ_msg_extra (msg,
1062 name_len + rd_ser_len,
1063 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1064 msg->gns_header.r_id = htonl (rid);
1065 msg->name_len = htons (name_len);
1066 msg->rd_count = htons (rd_count);
1067 msg->rd_len = htons (rd_ser_len);
1068 msg->reserved = htons (0);
1069 msg->private_key = *pkey;
1071 name_tmp = (char *) &msg[1];
1072 GNUNET_memcpy (name_tmp,
1075 rd_ser = &name_tmp[name_len];
1076 sret = GNUNET_GNSRECORD_records_serialize (rd_count,
1085 GNUNET_assert (rd_ser_len == (size_t) sret);
1086 LOG (GNUNET_ERROR_TYPE_DEBUG,
1087 "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
1090 qe->timeout_task = GNUNET_SCHEDULER_add_delayed (NAMESTORE_DELAY_TOLERANCE,
1096 LOG (GNUNET_ERROR_TYPE_WARNING,
1097 "Delaying NAMESTORE_RECORD_STORE message as namestore is not ready!\n");
1101 GNUNET_MQ_send (h->mq,
1109 * Set the desired nick name for a zone
1111 * @param h handle to the namestore
1112 * @param pkey private key of the zone
1113 * @param nick the nick name to set
1114 * @param cont continuation to call when done
1115 * @param cont_cls closure for @a cont
1116 * @return handle to abort the request
1118 struct GNUNET_NAMESTORE_QueueEntry *
1119 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
1120 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1122 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1125 struct GNUNET_GNSRECORD_Data rd;
1129 memset (&rd, 0, sizeof (rd));
1131 rd.data_size = strlen (nick) +1;
1132 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
1133 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1134 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1135 return GNUNET_NAMESTORE_records_store (h,
1137 GNUNET_GNS_EMPTY_LABEL_AT,
1146 * Lookup an item in the namestore.
1148 * @param h handle to the namestore
1149 * @param pkey private key of the zone
1150 * @param label name that is being mapped (at most 255 characters long)
1151 * @param error_cb function to call on error (i.e. disconnect)
1152 * @param error_cb_cls closure for @a error_cb
1153 * @param rm function to call with the result (with 0 records if we don't have that label)
1154 * @param rm_cls closure for @a rm
1155 * @return handle to abort the request
1157 struct GNUNET_NAMESTORE_QueueEntry *
1158 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1159 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1161 GNUNET_SCHEDULER_TaskCallback error_cb,
1163 GNUNET_NAMESTORE_RecordMonitor rm,
1166 struct GNUNET_NAMESTORE_QueueEntry *qe;
1167 struct GNUNET_MQ_Envelope *env;
1168 struct LabelLookupMessage *msg;
1171 if (1 == (label_len = strlen (label) + 1))
1177 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1179 qe->error_cb = error_cb;
1180 qe->error_cb_cls = error_cb_cls;
1182 qe->proc_cls = rm_cls;
1183 qe->op_id = get_op_id(h);
1184 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1188 env = GNUNET_MQ_msg_extra (msg,
1190 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1191 msg->gns_header.r_id = htonl (qe->op_id);
1193 msg->label_len = htonl (label_len);
1194 GNUNET_memcpy (&msg[1],
1200 GNUNET_MQ_send (h->mq,
1207 * Look for an existing PKEY delegation record for a given public key.
1208 * Returns at most one result to the processor.
1210 * @param h handle to the namestore
1211 * @param zone public key of the zone to look up in, never NULL
1212 * @param value_zone public key of the target zone (value), never NULL
1213 * @param error_cb function to call on error (i.e. disconnect)
1214 * @param error_cb_cls closure for @a error_cb
1215 * @param proc function to call on the matching records, or with
1216 * NULL (rd_count == 0) if there are no matching records
1217 * @param proc_cls closure for @a proc
1218 * @return a handle that can be used to
1221 struct GNUNET_NAMESTORE_QueueEntry *
1222 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1223 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1224 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1225 GNUNET_SCHEDULER_TaskCallback error_cb,
1227 GNUNET_NAMESTORE_RecordMonitor proc,
1230 struct GNUNET_NAMESTORE_QueueEntry *qe;
1231 struct GNUNET_MQ_Envelope *env;
1232 struct ZoneToNameMessage *msg;
1236 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1238 qe->error_cb = error_cb;
1239 qe->error_cb_cls = error_cb_cls;
1241 qe->proc_cls = proc_cls;
1243 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
1247 env = GNUNET_MQ_msg (msg,
1248 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1249 msg->gns_header.r_id = htonl (rid);
1251 msg->value_zone = *value_zone;
1255 GNUNET_MQ_send (h->mq,
1262 * Starts a new zone iteration (used to periodically PUT all of our
1263 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1264 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1265 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1266 * immediately, and then again after
1267 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1269 * @param h handle to the namestore
1270 * @param zone zone to access, NULL for all zones
1271 * @param error_cb function to call on error (i.e. disconnect)
1272 * @param error_cb_cls closure for @a error_cb
1273 * @param proc function to call on each name from the zone; it
1274 * will be called repeatedly with a value (if available)
1275 * @param proc_cls closure for @a proc
1276 * @param finish_cb function to call on completion
1277 * @param finish_cb_cls closure for @a finish_cb
1278 * @return an iterator handle to use for iteration
1280 struct GNUNET_NAMESTORE_ZoneIterator *
1281 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1282 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1283 GNUNET_SCHEDULER_TaskCallback error_cb,
1285 GNUNET_NAMESTORE_RecordMonitor proc,
1287 GNUNET_SCHEDULER_TaskCallback finish_cb,
1288 void *finish_cb_cls)
1290 struct GNUNET_NAMESTORE_ZoneIterator *it;
1291 struct GNUNET_MQ_Envelope *env;
1292 struct ZoneIterationStartMessage *msg;
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Sending ZONE_ITERATION_START message\n");
1297 rid = get_op_id (h);
1298 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1300 it->error_cb = error_cb;
1301 it->error_cb_cls = error_cb_cls;
1302 it->finish_cb = finish_cb;
1303 it->finish_cb_cls = finish_cb_cls;
1305 it->proc_cls = proc_cls;
1309 GNUNET_CONTAINER_DLL_insert_tail (h->z_head,
1312 env = GNUNET_MQ_msg (msg,
1313 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1314 msg->gns_header.r_id = htonl (rid);
1320 GNUNET_MQ_send (h->mq,
1327 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1328 * for the next record.
1330 * @param it the iterator
1331 * @param limit number of records to return to the iterator in one shot
1332 * (before #GNUNET_NAMESTORE_zone_iterator_next is to be called again)
1335 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it,
1338 struct GNUNET_NAMESTORE_Handle *h = it->h;
1339 struct ZoneIterationNextMessage *msg;
1340 struct GNUNET_MQ_Envelope *env;
1342 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 "Sending ZONE_ITERATION_NEXT message with limit %llu\n",
1344 (unsigned long long) limit);
1345 env = GNUNET_MQ_msg (msg,
1346 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1347 msg->gns_header.r_id = htonl (it->op_id);
1348 msg->limit = GNUNET_htonll (limit);
1349 GNUNET_MQ_send (h->mq,
1355 * Stops iteration and releases the namestore handle for further calls.
1357 * @param it the iterator
1360 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1362 struct GNUNET_NAMESTORE_Handle *h = it->h;
1363 struct GNUNET_MQ_Envelope *env;
1364 struct ZoneIterationStopMessage *msg;
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "Sending ZONE_ITERATION_STOP message\n");
1370 env = GNUNET_MQ_msg (msg,
1371 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1372 msg->gns_header.r_id = htonl (it->op_id);
1373 GNUNET_MQ_send (h->mq,
1381 * Cancel a namestore operation. The final callback from the
1382 * operation must not have been done yet.
1384 * @param qe operation to cancel
1387 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1393 /* end of namestore_api.c */