2 This file is part of GNUnet.
3 (C) 2010-2013 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
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__)
45 * An QueueEntry used to store information for a pending
46 * NAMESTORE record operation
48 struct GNUNET_NAMESTORE_QueueEntry
54 struct GNUNET_NAMESTORE_QueueEntry *next;
59 struct GNUNET_NAMESTORE_QueueEntry *prev;
62 * Main handle to access the namestore.
64 struct GNUNET_NAMESTORE_Handle *nsh;
67 * Continuation to call
69 GNUNET_NAMESTORE_ContinuationWithStatus cont;
77 * Function to call with the records we get back; or NULL.
79 GNUNET_NAMESTORE_RecordMonitor proc;
82 * Closure for @e proc.
87 * The operation id this zone iteration operation has
95 * Handle for a zone iterator operation
97 struct GNUNET_NAMESTORE_ZoneIterator
103 struct GNUNET_NAMESTORE_ZoneIterator *next;
108 struct GNUNET_NAMESTORE_ZoneIterator *prev;
111 * Main handle to access the namestore.
113 struct GNUNET_NAMESTORE_Handle *h;
116 * The continuation to call with the results
118 GNUNET_NAMESTORE_RecordMonitor proc;
121 * Closure for @e proc.
126 * Private key of the zone.
128 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
131 * The operation id this zone iteration operation has
139 * Message in linked list we should send to the service. The
140 * actual binary message follows this struct.
142 struct PendingMessage
148 struct PendingMessage *next;
153 struct PendingMessage *prev;
156 * Size of the message.
164 * Connection to the NAMESTORE service.
166 struct GNUNET_NAMESTORE_Handle
170 * Configuration to use.
172 const struct GNUNET_CONFIGURATION_Handle *cfg;
175 * Socket (if available).
177 struct GNUNET_CLIENT_Connection *client;
180 * Currently pending transmission request (or NULL).
182 struct GNUNET_CLIENT_TransmitHandle *th;
185 * Head of linked list of pending messages to send to the service
187 struct PendingMessage *pending_head;
190 * Tail of linked list of pending messages to send to the service
192 struct PendingMessage *pending_tail;
195 * Head of pending namestore queue entries
197 struct GNUNET_NAMESTORE_QueueEntry *op_head;
200 * Tail of pending namestore queue entries
202 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
205 * Head of pending namestore zone iterator entries
207 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
210 * Tail of pending namestore zone iterator entries
212 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
217 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
220 * Delay introduced before we reconnect.
222 struct GNUNET_TIME_Relative reconnect_delay;
225 * Should we reconnect to service due to some serious error?
230 * Did we start to receive yet?
235 * The last operation id used for a NAMESTORE operation
237 uint32_t last_op_id_used;
243 * Disconnect from service and then reconnect.
245 * @param h our handle
248 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
252 * Handle an incoming message of type
253 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
255 * @param qe the respective entry in the message queue
256 * @param msg the message we received
257 * @param size the message size
258 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
261 handle_record_store_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
262 const struct RecordStoreResponseMessage* msg,
268 LOG (GNUNET_ERROR_TYPE_DEBUG,
270 "RECORD_STORE_RESPONSE");
271 /* TODO: add actual error message from namestore to response... */
272 res = ntohl (msg->op_result);
273 if (GNUNET_SYSERR == res)
274 emsg = _("Namestore failed to store record\n");
277 if (NULL != qe->cont)
278 qe->cont (qe->cont_cls, res, emsg);
284 * Handle an incoming message of type
285 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
287 * @param qe the respective entry in the message queue
288 * @param msg the message we received
289 * @param size the message size
290 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
293 handle_lookup_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
294 const struct LabelLookupResponseMessage *msg,
303 unsigned int rd_count;
306 LOG (GNUNET_ERROR_TYPE_DEBUG,
308 "RECORD_LOOKUP_RESULT");
310 rd_len = ntohs (msg->rd_len);
311 rd_count = ntohs (msg->rd_count);
312 msg_len = ntohs (msg->gns_header.header.size);
313 name_len = ntohs (msg->name_len);
314 found = ntohs (msg->found);
315 exp_msg_len = sizeof (struct LabelLookupResponseMessage) + name_len + rd_len;
316 if (msg_len != exp_msg_len)
319 return GNUNET_SYSERR;
321 name = (const char *) &msg[1];
322 if ( (name_len > 0) &&
323 ('\0' != name[name_len -1]) )
326 return GNUNET_SYSERR;
328 if (GNUNET_NO == found)
330 /* label was not in namestore */
331 if (NULL != qe->proc)
332 qe->proc (qe->proc_cls,
339 rd_tmp = &name[name_len];
341 struct GNUNET_GNSRECORD_Data rd[rd_count];
343 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_len, rd_tmp, rd_count, rd))
346 return GNUNET_SYSERR;
350 if (NULL != qe->proc)
351 qe->proc (qe->proc_cls,
355 (rd_count > 0) ? rd : NULL);
362 * Handle an incoming message of type
363 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
365 * @param qe the respective entry in the message queue
366 * @param msg the message we received
367 * @param size the message size
368 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
371 handle_record_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
372 const struct RecordResultMessage *msg,
381 unsigned int rd_count;
383 LOG (GNUNET_ERROR_TYPE_DEBUG,
386 rd_len = ntohs (msg->rd_len);
387 rd_count = ntohs (msg->rd_count);
388 msg_len = ntohs (msg->gns_header.header.size);
389 name_len = ntohs (msg->name_len);
390 GNUNET_break (0 == ntohs (msg->reserved));
391 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
392 if (msg_len != exp_msg_len)
395 return GNUNET_SYSERR;
397 name = (const char *) &msg[1];
398 if ( (name_len > 0) &&
399 ('\0' != name[name_len -1]) )
402 return GNUNET_SYSERR;
404 rd_tmp = &name[name_len];
406 struct GNUNET_GNSRECORD_Data rd[rd_count];
408 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_len, rd_tmp, rd_count, rd))
411 return GNUNET_SYSERR;
415 if (NULL != qe->proc)
416 qe->proc (qe->proc_cls,
420 (rd_count > 0) ? rd : NULL);
427 * Handle an incoming message of type
428 * #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
430 * @param qe the respective entry in the message queue
431 * @param msg the message we received
432 * @param size the message size
433 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
434 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
437 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
438 const struct ZoneToNameResponseMessage *msg,
444 unsigned int rd_count;
445 const char *name_tmp;
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "ZONE_TO_NAME_RESPONSE");
451 res = ntohs (msg->res);
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "An error occured during zone to name operation\n");
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Namestore has no result for zone to name mapping \n");
463 LOG (GNUNET_ERROR_TYPE_DEBUG,
464 "Namestore has result for zone to name mapping \n");
465 name_len = ntohs (msg->name_len);
466 rd_count = ntohs (msg->rd_count);
467 rd_ser_len = ntohs (msg->rd_len);
468 name_tmp = (const char *) &msg[1];
469 if ( (name_len > 0) &&
470 ('\0' != name_tmp[name_len -1]) )
473 return GNUNET_SYSERR;
475 rd_tmp = &name_tmp[name_len];
477 struct GNUNET_GNSRECORD_Data rd[rd_count];
479 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
482 return GNUNET_SYSERR;
484 /* normal end, call continuation with result */
485 if (NULL != qe->proc)
486 qe->proc (qe->proc_cls,
490 /* return is important here: break would call continuation with error! */
495 return GNUNET_SYSERR;
497 /* error case, call continuation with error */
498 if (NULL != qe->proc)
499 qe->proc (qe->proc_cls, NULL, NULL, 0, NULL);
505 * Handle incoming messages for record operations
507 * @param qe the respective zone iteration handle
508 * @param msg the message we received
509 * @param type the message type in host byte order
510 * @param size the message size
511 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
512 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
515 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
516 const struct GNUNET_MessageHeader *msg,
520 /* handle different message type */
523 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE:
524 if (size != sizeof (struct RecordStoreResponseMessage))
527 return GNUNET_SYSERR;
529 return handle_record_store_response (qe, (const struct RecordStoreResponseMessage *) msg, size);
530 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
531 if (size < sizeof (struct ZoneToNameResponseMessage))
534 return GNUNET_SYSERR;
536 return handle_zone_to_name_response (qe, (const struct ZoneToNameResponseMessage *) msg, size);
537 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
538 if (size < sizeof (struct RecordResultMessage))
541 return GNUNET_SYSERR;
543 return handle_record_result (qe, (const struct RecordResultMessage *) msg, size);
544 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE:
545 if (size < sizeof (struct LabelLookupResponseMessage))
548 return GNUNET_SYSERR;
550 return handle_lookup_result (qe, (const struct LabelLookupResponseMessage *) msg, size);
553 return GNUNET_SYSERR;
559 * Handle a response from NAMESTORE service for a zone iteration request
561 * @param ze the respective iterator for this operation
562 * @param msg the message containing the respoonse
563 * @param size the message size
564 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
565 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
568 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
569 const struct RecordResultMessage *msg,
572 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
578 const char *name_tmp;
579 const char *rd_ser_tmp;
581 LOG (GNUNET_ERROR_TYPE_DEBUG,
583 "ZONE_ITERATION_RESPONSE");
584 msg_len = ntohs (msg->gns_header.header.size);
585 rd_len = ntohs (msg->rd_len);
586 rd_count = ntohs (msg->rd_count);
587 name_len = ntohs (msg->name_len);
588 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
589 if (msg_len != exp_msg_len)
592 return GNUNET_SYSERR;
594 if ( (0 == name_len) &&
595 (0 == (memcmp (&msg->private_key,
597 sizeof (priv_dummy)))) )
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Zone iteration completed!\n");
601 if (NULL != ze->proc)
602 ze->proc (ze->proc_cls, NULL, NULL, 0, NULL);
605 name_tmp = (const char *) &msg[1];
606 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
609 return GNUNET_SYSERR;
611 rd_ser_tmp = (const char *) &name_tmp[name_len];
613 struct GNUNET_GNSRECORD_Data rd[rd_count];
615 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (rd_len,
621 return GNUNET_SYSERR;
623 if (NULL != ze->proc)
624 ze->proc (ze->proc_cls,
634 * Handle incoming messages for zone iterations
636 * @param ze the respective zone iteration handle
637 * @param msg the message we received
638 * @param type the message type in HBO
639 * @param size the message size
640 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
641 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
644 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
645 const struct GNUNET_MessageHeader *msg,
646 int type, size_t size)
648 /* handle different message type */
651 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
652 if (size < sizeof (struct RecordResultMessage))
655 return GNUNET_SYSERR;
657 return handle_zone_iteration_response (ze,
658 (const struct RecordResultMessage *) msg,
662 return GNUNET_SYSERR;
668 * Type of a function to call when we receive a message
671 * @param cls the `struct GNUNET_NAMESTORE_SchedulingHandle`
672 * @param msg message received, NULL on timeout or fatal error
675 process_namestore_message (void *cls,
676 const struct GNUNET_MessageHeader *msg)
678 struct GNUNET_NAMESTORE_Handle *h = cls;
679 const struct GNUNET_NAMESTORE_Header *gm;
680 struct GNUNET_NAMESTORE_QueueEntry *qe;
681 struct GNUNET_NAMESTORE_ZoneIterator *ze;
692 size = ntohs (msg->size);
693 type = ntohs (msg->type);
694 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
697 GNUNET_CLIENT_receive (h->client,
698 &process_namestore_message, h,
699 GNUNET_TIME_UNIT_FOREVER_REL);
702 gm = (const struct GNUNET_NAMESTORE_Header *) msg;
703 r_id = ntohl (gm->r_id);
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "Received message type %u size %u op %u\n",
709 (unsigned int) r_id);
711 /* Is it a record related operation ? */
712 for (qe = h->op_head; qe != NULL; qe = qe->next)
713 if (qe->op_id == r_id)
717 ret = manage_record_operations (qe, msg, type, size);
718 if (GNUNET_SYSERR == ret)
720 /* protocol error, need to reconnect */
721 h->reconnect = GNUNET_YES;
725 /* client was notified about success or failure, clean up 'qe' */
726 GNUNET_CONTAINER_DLL_remove (h->op_head,
732 /* Is it a zone iteration operation? */
733 for (ze = h->z_head; ze != NULL; ze = ze->next)
734 if (ze->op_id == r_id)
738 ret = manage_zone_operations (ze, msg, type, size);
739 if (GNUNET_NO == ret)
741 /* end of iteration, clean up 'ze' */
742 GNUNET_CONTAINER_DLL_remove (h->z_head,
747 if (GNUNET_SYSERR == ret)
749 /* protocol error, need to reconnect */
750 h->reconnect = GNUNET_YES;
753 if (GNUNET_YES == h->reconnect)
758 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
759 GNUNET_TIME_UNIT_FOREVER_REL);
764 * Transmit messages from the message queue to the service
765 * (if there are any, and if we are not already trying).
767 * @param h handle to use
770 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
774 * We can now transmit a message to NAMESTORE. Do it.
776 * @param cls the `struct GNUNET_NAMESTORE_Handle`
777 * @param size number of bytes we can transmit
778 * @param buf where to copy the messages
779 * @return number of bytes copied into @a buf
782 transmit_message_to_namestore (void *cls,
786 struct GNUNET_NAMESTORE_Handle *h = cls;
787 struct PendingMessage *p;
792 if ((0 == size) || (NULL == buf))
799 while ( (NULL != (p = h->pending_head)) &&
802 memcpy (&cbuf[ret], &p[1], p->size);
805 GNUNET_CONTAINER_DLL_remove (h->pending_head,
808 if (GNUNET_NO == h->is_receiving)
810 h->is_receiving = GNUNET_YES;
811 GNUNET_CLIENT_receive (h->client,
812 &process_namestore_message, h,
813 GNUNET_TIME_UNIT_FOREVER_REL);
823 * Transmit messages from the message queue to the service
824 * (if there are any, and if we are not already trying).
826 * @param h handle to use
829 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
831 struct PendingMessage *p;
834 return; /* transmission request already pending */
835 if (NULL == (p = h->pending_head))
836 return; /* transmission queue empty */
837 if (NULL == h->client)
838 return; /* currently reconnecting */
839 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
840 GNUNET_TIME_UNIT_FOREVER_REL,
841 GNUNET_NO, &transmit_message_to_namestore,
843 GNUNET_break (NULL != h->th);
848 * Reconnect to namestore service.
850 * @param h the handle to the NAMESTORE service
853 reconnect (struct GNUNET_NAMESTORE_Handle *h)
855 GNUNET_assert (NULL == h->client);
856 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
857 GNUNET_assert (NULL != h->client);
863 * Re-establish the connection to the service.
865 * @param cls handle to use to re-connect.
866 * @param tc scheduler context
869 reconnect_task (void *cls,
870 const struct GNUNET_SCHEDULER_TaskContext *tc)
872 struct GNUNET_NAMESTORE_Handle *h = cls;
874 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
880 * Disconnect from service and then reconnect.
882 * @param h our handle
885 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
889 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
892 h->reconnect = GNUNET_NO;
893 GNUNET_CLIENT_disconnect (h->client);
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Reconnecting to namestore\n");
896 h->is_receiving = GNUNET_NO;
898 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
899 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
906 * Get a fresh operation id to distinguish between namestore requests
908 * @param h the namestore handle
909 * @return next operation id to use
912 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
914 return h->last_op_id_used++;
919 * Initialize the connection with the NAMESTORE service.
921 * @param cfg configuration to use
922 * @return handle to the GNS service, or NULL on error
924 struct GNUNET_NAMESTORE_Handle *
925 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
927 struct GNUNET_NAMESTORE_Handle *h;
929 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
931 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
932 h->last_op_id_used = 0;
938 * Disconnect from the namestore service (and free associated
941 * @param h handle to the namestore
944 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
946 struct PendingMessage *p;
947 struct GNUNET_NAMESTORE_QueueEntry *q;
948 struct GNUNET_NAMESTORE_ZoneIterator *z;
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
951 GNUNET_assert (NULL != h);
954 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
957 while (NULL != (p = h->pending_head))
959 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
962 GNUNET_break (NULL == h->op_head);
963 while (NULL != (q = h->op_head))
965 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
968 GNUNET_break (NULL == h->z_head);
969 while (NULL != (z = h->z_head))
971 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
974 if (NULL != h->client)
976 GNUNET_CLIENT_disconnect (h->client);
979 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
981 GNUNET_SCHEDULER_cancel (h->reconnect_task);
982 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
989 * Store an item in the namestore. If the item is already present,
990 * it is replaced with the new record. Use an empty array to
991 * remove all records under the given name.
993 * @param h handle to the namestore
994 * @param pkey private key of the zone
995 * @param label name that is being mapped (at most 255 characters long)
996 * @param rd_count number of records in the 'rd' array
997 * @param rd array of records with data to store
998 * @param cont continuation to call when done
999 * @param cont_cls closure for 'cont'
1000 * @return handle to abort the request
1002 struct GNUNET_NAMESTORE_QueueEntry *
1003 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1004 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1006 unsigned int rd_count,
1007 const struct GNUNET_GNSRECORD_Data *rd,
1008 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1011 struct GNUNET_NAMESTORE_QueueEntry *qe;
1012 struct PendingMessage *pe;
1019 struct RecordStoreMessage *msg;
1021 GNUNET_assert (NULL != h);
1022 GNUNET_assert (NULL != pkey);
1023 GNUNET_assert (NULL != label);
1024 name_len = strlen (label) + 1;
1025 if (name_len > MAX_NAME_LEN)
1030 rid = get_op_id (h);
1031 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1034 qe->cont_cls = cont_cls;
1036 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1039 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1040 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1041 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1042 pe->size = msg_size;
1043 msg = (struct RecordStoreMessage *) &pe[1];
1044 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1045 msg->gns_header.header.size = htons (msg_size);
1046 msg->gns_header.r_id = htonl (rid);
1047 msg->name_len = htons (name_len);
1048 msg->rd_count = htons (rd_count);
1049 msg->rd_len = htons (rd_ser_len);
1050 msg->reserved = htons (0);
1051 msg->private_key = *pkey;
1053 name_tmp = (char *) &msg[1];
1054 memcpy (name_tmp, label, name_len);
1055 rd_ser = &name_tmp[name_len];
1056 GNUNET_break (rd_ser_len ==
1057 GNUNET_GNSRECORD_records_serialize (rd_count, rd,
1060 LOG (GNUNET_ERROR_TYPE_DEBUG,
1061 "Sending `%s' message for name `%s' with size %u and %u records\n",
1062 "NAMESTORE_RECORD_STORE", label, msg_size,
1064 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1070 * Set the desired nick name for a zone
1072 * @param h handle to the namestore
1073 * @param pkey private key of the zone
1074 * @param nick the nick name to set
1075 * @param cont continuation to call when done
1076 * @param cont_cls closure for 'cont'
1077 * @return handle to abort the request
1079 struct GNUNET_NAMESTORE_QueueEntry *
1080 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
1081 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1083 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1086 struct GNUNET_GNSRECORD_Data rd;
1088 memset (&rd, 0, sizeof (rd));
1090 rd.data_size = strlen (nick) +1;
1091 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
1092 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1093 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
1094 return GNUNET_NAMESTORE_records_store(h, pkey, GNUNET_GNS_MASTERZONE_STR, 1, &rd, cont, cont_cls);
1099 * Lookup an item in the namestore.
1101 * @param h handle to the namestore
1102 * @param pkey private key of the zone
1103 * @param label name that is being mapped (at most 255 characters long)
1104 * @param rm function to call with the result (with 0 records if we don't have that label)
1105 * @param rm_cls closure for @a rm
1106 * @return handle to abort the request
1108 struct GNUNET_NAMESTORE_QueueEntry *
1109 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1110 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1112 GNUNET_NAMESTORE_RecordMonitor rm,
1115 struct GNUNET_NAMESTORE_QueueEntry *qe;
1116 struct PendingMessage *pe;
1117 struct LabelLookupMessage * msg;
1121 GNUNET_assert (NULL != h);
1122 GNUNET_assert (NULL != pkey);
1123 GNUNET_assert (NULL != label);
1125 if (1 == (label_len = strlen (label) + 1))
1128 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1131 qe->proc_cls = rm_cls;
1132 qe->op_id = get_op_id(h);
1133 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1135 msg_size = sizeof (struct LabelLookupMessage) + label_len;
1136 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1137 pe->size = msg_size;
1138 msg = (struct LabelLookupMessage *) &pe[1];
1139 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1140 msg->gns_header.header.size = htons (msg_size);
1141 msg->gns_header.r_id = htonl (qe->op_id);
1143 msg->label_len = htonl(label_len);
1144 memcpy (&msg[1], label, label_len);
1146 /* transmit message */
1147 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1154 * Look for an existing PKEY delegation record for a given public key.
1155 * Returns at most one result to the processor.
1157 * @param h handle to the namestore
1158 * @param zone public key of the zone to look up in, never NULL
1159 * @param value_zone public key of the target zone (value), never NULL
1160 * @param proc function to call on the matching records, or with
1161 * NULL (rd_count == 0) if there are no matching records
1162 * @param proc_cls closure for @a proc
1163 * @return a handle that can be used to
1166 struct GNUNET_NAMESTORE_QueueEntry *
1167 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1168 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1169 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1170 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1172 struct GNUNET_NAMESTORE_QueueEntry *qe;
1173 struct PendingMessage *pe;
1174 struct ZoneToNameMessage * msg;
1178 GNUNET_assert (NULL != h);
1179 GNUNET_assert (NULL != zone);
1180 GNUNET_assert (NULL != value_zone);
1182 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1185 qe->proc_cls = proc_cls;
1187 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1189 msg_size = sizeof (struct ZoneToNameMessage);
1190 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1191 pe->size = msg_size;
1192 msg = (struct ZoneToNameMessage *) &pe[1];
1193 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1194 msg->gns_header.header.size = htons (msg_size);
1195 msg->gns_header.r_id = htonl (rid);
1197 msg->value_zone = *value_zone;
1199 /* transmit message */
1200 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1207 * Starts a new zone iteration (used to periodically PUT all of our
1208 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1209 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1210 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1211 * immediately, and then again after
1212 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1214 * @param h handle to the namestore
1215 * @param zone zone to access, NULL for all zones
1216 * @param proc function to call on each name from the zone; it
1217 * will be called repeatedly with a value (if available)
1218 * and always once at the end with a name of NULL.
1219 * @param proc_cls closure for @a proc
1220 * @return an iterator handle to use for iteration
1222 struct GNUNET_NAMESTORE_ZoneIterator *
1223 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1224 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1225 GNUNET_NAMESTORE_RecordMonitor proc,
1228 struct GNUNET_NAMESTORE_ZoneIterator *it;
1229 struct PendingMessage *pe;
1230 struct ZoneIterationStartMessage * msg;
1234 GNUNET_assert (NULL != h);
1236 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1239 it->proc_cls = proc_cls;
1243 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1245 msg_size = sizeof (struct ZoneIterationStartMessage);
1246 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1247 pe->size = msg_size;
1248 msg = (struct ZoneIterationStartMessage *) &pe[1];
1249 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1250 msg->gns_header.header.size = htons (msg_size);
1251 msg->gns_header.r_id = htonl (rid);
1254 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1261 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1262 * for the next record.
1264 * @param it the iterator
1267 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1269 struct GNUNET_NAMESTORE_Handle *h;
1270 struct ZoneIterationNextMessage * msg;
1271 struct PendingMessage *pe;
1274 GNUNET_assert (NULL != it);
1276 msg_size = sizeof (struct ZoneIterationNextMessage);
1277 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1278 pe->size = msg_size;
1279 msg = (struct ZoneIterationNextMessage *) &pe[1];
1280 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1281 msg->gns_header.header.size = htons (msg_size);
1282 msg->gns_header.r_id = htonl (it->op_id);
1283 LOG (GNUNET_ERROR_TYPE_DEBUG,
1284 "Sending `%s' message\n",
1285 "ZONE_ITERATION_NEXT");
1286 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1292 * Stops iteration and releases the namestore handle for further calls.
1294 * @param it the iterator
1297 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1299 struct GNUNET_NAMESTORE_Handle *h;
1300 struct PendingMessage *pe;
1302 struct ZoneIterationStopMessage * msg;
1304 GNUNET_assert (NULL != it);
1306 GNUNET_CONTAINER_DLL_remove (h->z_head,
1309 msg_size = sizeof (struct ZoneIterationStopMessage);
1310 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1311 pe->size = msg_size;
1312 msg = (struct ZoneIterationStopMessage *) &pe[1];
1313 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1314 msg->gns_header.header.size = htons (msg_size);
1315 msg->gns_header.r_id = htonl (it->op_id);
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "Sending `%s' message\n",
1318 "ZONE_ITERATION_STOP");
1319 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1326 * Cancel a namestore operation. The final callback from the
1327 * operation must not have been done yet.
1329 * @param qe operation to cancel
1332 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1334 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1336 GNUNET_assert (NULL != qe);
1337 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, qe);
1342 /* end of namestore_api.c */