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_namestore_service.h"
37 #include "namestore.h"
40 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-api",__VA_ARGS__)
44 * An QueueEntry used to store information for a pending
45 * NAMESTORE record operation
47 struct GNUNET_NAMESTORE_QueueEntry
53 struct GNUNET_NAMESTORE_QueueEntry *next;
58 struct GNUNET_NAMESTORE_QueueEntry *prev;
61 * Main handle to access the namestore.
63 struct GNUNET_NAMESTORE_Handle *nsh;
66 * Continuation to call
68 GNUNET_NAMESTORE_ContinuationWithStatus cont;
76 * Function to call with the records we get back; or NULL.
78 GNUNET_NAMESTORE_RecordMonitor proc;
86 * Function to call with the blocks we get back; or NULL.
88 GNUNET_NAMESTORE_BlockProcessor block_proc;
91 * Closure for 'block_proc'.
96 * The operation id this zone iteration operation has
104 * Handle for a zone iterator operation
106 struct GNUNET_NAMESTORE_ZoneIterator
112 struct GNUNET_NAMESTORE_ZoneIterator *next;
117 struct GNUNET_NAMESTORE_ZoneIterator *prev;
120 * Main handle to access the namestore.
122 struct GNUNET_NAMESTORE_Handle *h;
125 * The continuation to call with the results
127 GNUNET_NAMESTORE_RecordMonitor proc;
130 * Closure for 'proc'.
135 * Private key of the zone.
137 struct GNUNET_CRYPTO_EccPrivateKey zone;
140 * The operation id this zone iteration operation has
148 * Message in linked list we should send to the service. The
149 * actual binary message follows this struct.
151 struct PendingMessage
157 struct PendingMessage *next;
162 struct PendingMessage *prev;
165 * Size of the message.
173 * Connection to the NAMESTORE service.
175 struct GNUNET_NAMESTORE_Handle
179 * Configuration to use.
181 const struct GNUNET_CONFIGURATION_Handle *cfg;
184 * Socket (if available).
186 struct GNUNET_CLIENT_Connection *client;
189 * Currently pending transmission request (or NULL).
191 struct GNUNET_CLIENT_TransmitHandle *th;
194 * Head of linked list of pending messages to send to the service
196 struct PendingMessage * pending_head;
199 * Tail of linked list of pending messages to send to the service
201 struct PendingMessage * pending_tail;
204 * Head of pending namestore queue entries
206 struct GNUNET_NAMESTORE_QueueEntry * op_head;
209 * Tail of pending namestore queue entries
211 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
214 * Head of pending namestore zone iterator entries
216 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
219 * Tail of pending namestore zone iterator entries
221 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
226 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
229 * Should we reconnect to service due to some serious error?
234 * Did we start to receive yet?
239 * The last operation id used for a NAMESTORE operation
241 uint32_t last_op_id_used;
247 * Disconnect from service and then reconnect.
249 * @param h our handle
252 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
256 * Handle an incoming message of type #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE.
258 * @param qe the respective entry in the message queue
259 * @param msg the message we received
260 * @param size the message size
261 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
264 handle_lookup_block_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
265 const struct LookupBlockResponseMessage *msg,
268 struct GNUNET_NAMESTORE_Block *block;
269 char buf[size + sizeof (struct GNUNET_NAMESTORE_Block) - sizeof (struct LookupBlockResponseMessage)];
271 LOG (GNUNET_ERROR_TYPE_DEBUG,
273 "LOOKUP_BLOCK_RESPONSE");
274 if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
277 if (NULL != qe->block_proc)
278 qe->block_proc (qe->block_proc_cls, NULL);
282 block = (struct GNUNET_NAMESTORE_Block *) buf;
283 block->signature = msg->signature;
284 block->derived_key = msg->derived_key;
285 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
286 block->purpose.size = htonl (size - sizeof (struct LookupBlockResponseMessage) +
287 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
288 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
289 block->expiration_time = msg->expire;
292 size - sizeof (struct LookupBlockResponseMessage));
294 GNUNET_NAMESTORE_block_verify (block))
297 return GNUNET_SYSERR;
299 if (NULL != qe->block_proc)
300 qe->block_proc (qe->block_proc_cls, block);
308 * Handle an incoming message of type 'GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE'
310 * @param qe the respective entry in the message queue
311 * @param msg the message we received
312 * @param size the message size
313 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
316 handle_block_cache_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
317 const struct BlockCacheResponseMessage *msg,
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s'\n",
323 "BLOCK_CACHE_RESPONSE");
324 res = ntohl (msg->op_result);
325 /* TODO: add actual error message from namestore to response... */
326 if (NULL != qe->cont)
327 qe->cont (qe->cont_cls, res, (GNUNET_OK == res) ? NULL : _("Namestore failed to cache block"));
333 * Handle an incoming message of type #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
335 * @param qe the respective entry in the message queue
336 * @param msg the message we received
337 * @param size the message size
338 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
341 handle_record_store_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
342 const struct RecordStoreResponseMessage* msg,
348 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s'\n",
349 "RECORD_STORE_RESPONSE");
350 /* TODO: add actual error message from namestore to response... */
351 res = ntohl (msg->op_result);
352 if (GNUNET_SYSERR == res)
353 emsg = _("Namestore failed to store record\n");
356 if (NULL != qe->cont)
357 qe->cont (qe->cont_cls, res, emsg);
363 * Handle an incoming message of type #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_NAMESTORE_RecordData rd[rd_count];
408 if (GNUNET_OK != GNUNET_NAMESTORE_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 #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
429 * @param qe the respective entry in the message queue
430 * @param msg the message we received
431 * @param size the message size
432 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
433 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
436 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
437 const struct ZoneToNameResponseMessage *msg,
443 unsigned int rd_count;
444 const char *name_tmp;
447 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 "ZONE_TO_NAME_RESPONSE");
450 res = ntohs (msg->res);
454 LOG (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\n");
457 LOG (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
461 name_len = ntohs (msg->name_len);
462 rd_count = ntohs (msg->rd_count);
463 rd_ser_len = ntohs (msg->rd_len);
464 name_tmp = (const char *) &msg[1];
465 if ( (name_len > 0) &&
466 ('\0' != name_tmp[name_len -1]) )
469 return GNUNET_SYSERR;
471 rd_tmp = &name_tmp[name_len];
473 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
474 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
477 return GNUNET_SYSERR;
479 /* normal end, call continuation with result */
480 if (NULL != qe->proc)
481 qe->proc (qe->proc_cls,
485 /* return is important here: break would call continuation with error! */
490 return GNUNET_SYSERR;
492 /* error case, call continuation with error */
493 if (NULL != qe->proc)
494 qe->proc (qe->proc_cls, NULL, NULL, 0, NULL);
500 * Handle incoming messages for record operations
502 * @param qe the respective zone iteration handle
503 * @param msg the message we received
504 * @param type the message type in host byte order
505 * @param size the message size
506 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
507 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
510 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
511 const struct GNUNET_MessageHeader *msg,
515 /* handle different message type */
518 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE:
519 if (size < sizeof (struct LookupBlockResponseMessage))
522 return GNUNET_SYSERR;
524 return handle_lookup_block_response (qe, (const struct LookupBlockResponseMessage *) msg, size);
525 case GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE:
526 if (size != sizeof (struct BlockCacheResponseMessage))
529 return GNUNET_SYSERR;
531 return handle_block_cache_response (qe, (const struct BlockCacheResponseMessage *) msg, size);
532 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE:
533 if (size != sizeof (struct RecordStoreResponseMessage))
536 return GNUNET_SYSERR;
538 return handle_record_store_response (qe, (const struct RecordStoreResponseMessage *) msg, size);
539 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
540 if (size < sizeof (struct ZoneToNameResponseMessage))
543 return GNUNET_SYSERR;
545 return handle_zone_to_name_response (qe, (const struct ZoneToNameResponseMessage *) msg, size);
546 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
547 if (size < sizeof (struct RecordResultMessage))
550 return GNUNET_SYSERR;
552 return handle_record_result (qe, (const struct RecordResultMessage *) msg, size);
555 return GNUNET_SYSERR;
561 * Handle a response from NAMESTORE service for a zone iteration request
563 * @param ze the respective iterator for this operation
564 * @param msg the message containing the respoonse
565 * @param size the message size
566 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
567 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
570 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
571 const struct RecordResultMessage *msg,
574 static struct GNUNET_CRYPTO_EccPrivateKey priv_dummy;
580 const char *name_tmp;
581 const char *rd_ser_tmp;
583 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 "ZONE_ITERATION_RESPONSE");
586 msg_len = ntohs (msg->gns_header.header.size);
587 rd_len = ntohs (msg->rd_len);
588 rd_count = ntohs (msg->rd_count);
589 name_len = ntohs (msg->name_len);
590 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
591 if (msg_len != exp_msg_len)
594 return GNUNET_SYSERR;
596 if ((0 == name_len) && (0 == (memcmp (&msg->private_key, &priv_dummy, sizeof (priv_dummy)))))
598 LOG (GNUNET_ERROR_TYPE_DEBUG,
599 "Zone iteration is completed!\n");
600 if (NULL != ze->proc)
601 ze->proc (ze->proc_cls, NULL, NULL, 0, NULL);
604 name_tmp = (const char *) &msg[1];
605 if ((name_tmp[name_len -1] != '\0') || (name_len > MAX_NAME_LEN))
608 return GNUNET_SYSERR;
610 rd_ser_tmp = (const char *) &name_tmp[name_len];
612 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
614 if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
617 return GNUNET_SYSERR;
619 if (NULL != ze->proc)
620 ze->proc (ze->proc_cls, &msg->private_key, name_tmp, rd_count, rd);
627 * Handle incoming messages for zone iterations
629 * @param ze the respective zone iteration handle
630 * @param msg the message we received
631 * @param type the message type in HBO
632 * @param size the message size
633 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
634 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
637 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
638 const struct GNUNET_MessageHeader *msg,
639 int type, size_t size)
641 /* handle different message type */
644 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
645 if (size < sizeof (struct RecordResultMessage))
648 return GNUNET_SYSERR;
650 return handle_zone_iteration_response (ze, (const struct RecordResultMessage *) msg, size);
653 return GNUNET_SYSERR;
659 * Type of a function to call when we receive a message
662 * @param cls the `struct GNUNET_NAMESTORE_SchedulingHandle`
663 * @param msg message received, NULL on timeout or fatal error
666 process_namestore_message (void *cls,
667 const struct GNUNET_MessageHeader *msg)
669 struct GNUNET_NAMESTORE_Handle *h = cls;
670 const struct GNUNET_NAMESTORE_Header *gm;
671 struct GNUNET_NAMESTORE_QueueEntry *qe;
672 struct GNUNET_NAMESTORE_ZoneIterator *ze;
683 size = ntohs (msg->size);
684 type = ntohs (msg->type);
685 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
688 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
689 GNUNET_TIME_UNIT_FOREVER_REL);
692 gm = (const struct GNUNET_NAMESTORE_Header *) msg;
693 r_id = ntohl (gm->r_id);
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "Received message type %u size %u op %u\n",
699 (unsigned int) r_id);
701 /* Is it a record related operation ? */
702 for (qe = h->op_head; qe != NULL; qe = qe->next)
703 if (qe->op_id == r_id)
707 ret = manage_record_operations (qe, msg, type, size);
708 if (GNUNET_OK != ret)
710 /* protocol error, need to reconnect */
711 h->reconnect = GNUNET_YES;
713 if (GNUNET_SYSERR != ret)
715 /* client was notified about success or failure, clean up 'qe' */
716 GNUNET_CONTAINER_DLL_remove (h->op_head,
722 /* Is it a zone iteration operation? */
723 for (ze = h->z_head; ze != NULL; ze = ze->next)
724 if (ze->op_id == r_id)
728 ret = manage_zone_operations (ze, msg, type, size);
729 if (GNUNET_NO == ret)
731 /* end of iteration, clean up 'ze' */
732 GNUNET_CONTAINER_DLL_remove (h->z_head,
737 if (GNUNET_SYSERR == ret)
739 /* protocol error, need to reconnect */
740 h->reconnect = GNUNET_YES;
743 if (GNUNET_YES == h->reconnect)
748 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
749 GNUNET_TIME_UNIT_FOREVER_REL);
754 * Transmit messages from the message queue to the service
755 * (if there are any, and if we are not already trying).
757 * @param h handle to use
760 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
764 * We can now transmit a message to NAMESTORE. Do it.
766 * @param cls the `struct GNUNET_NAMESTORE_Handle`
767 * @param size number of bytes we can transmit
768 * @param buf where to copy the messages
769 * @return number of bytes copied into buf
772 transmit_message_to_namestore (void *cls, size_t size, void *buf)
774 struct GNUNET_NAMESTORE_Handle *h = cls;
775 struct PendingMessage *p;
780 if ((0 == size) || (NULL == buf))
787 while ((NULL != (p = h->pending_head)) && (p->size <= size))
789 memcpy (&cbuf[ret], &p[1], p->size);
792 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
793 if (GNUNET_NO == h->is_receiving)
795 h->is_receiving = GNUNET_YES;
796 GNUNET_CLIENT_receive (h->client,
797 &process_namestore_message, h,
798 GNUNET_TIME_UNIT_FOREVER_REL);
808 * Transmit messages from the message queue to the service
809 * (if there are any, and if we are not already trying).
811 * @param h handle to use
814 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
816 struct PendingMessage *p;
819 return; /* transmission request already pending */
820 if (NULL == (p = h->pending_head))
821 return; /* transmission queue empty */
822 if (NULL == h->client)
823 return; /* currently reconnecting */
824 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
825 GNUNET_TIME_UNIT_FOREVER_REL,
826 GNUNET_NO, &transmit_message_to_namestore,
832 * Reconnect to namestore service.
834 * @param h the handle to the NAMESTORE service
837 reconnect (struct GNUNET_NAMESTORE_Handle *h)
839 GNUNET_assert (NULL == h->client);
840 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
841 GNUNET_assert (NULL != h->client);
847 * Re-establish the connection to the service.
849 * @param cls handle to use to re-connect.
850 * @param tc scheduler context
853 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 struct GNUNET_NAMESTORE_Handle *h = cls;
857 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
863 * Disconnect from service and then reconnect.
865 * @param h our handle
868 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
870 h->reconnect = GNUNET_NO;
871 GNUNET_CLIENT_disconnect (h->client);
872 h->is_receiving = GNUNET_NO;
874 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
881 * Get a fresh operation id to distinguish between namestore requests
883 * @param h the namestore handle
884 * @return next operation id to use
887 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
889 return h->last_op_id_used++;
894 * Initialize the connection with the NAMESTORE service.
896 * @param cfg configuration to use
897 * @return handle to the GNS service, or NULL on error
899 struct GNUNET_NAMESTORE_Handle *
900 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
902 struct GNUNET_NAMESTORE_Handle *h;
904 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
906 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
907 h->last_op_id_used = 0;
913 * Disconnect from the namestore service (and free associated
916 * @param h handle to the namestore
919 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
921 struct PendingMessage *p;
922 struct GNUNET_NAMESTORE_QueueEntry *q;
923 struct GNUNET_NAMESTORE_ZoneIterator *z;
925 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
926 GNUNET_assert (NULL != h);
927 while (NULL != (p = h->pending_head))
929 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
932 GNUNET_break (NULL == h->op_head);
933 while (NULL != (q = h->op_head))
935 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
938 GNUNET_break (NULL == h->z_head);
939 while (NULL != (z = h->z_head))
941 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
944 if (NULL != h->client)
946 GNUNET_CLIENT_disconnect (h->client);
949 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
951 GNUNET_SCHEDULER_cancel (h->reconnect_task);
952 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
959 * Store an item in the namestore. If the item is already present,
960 * it is replaced with the new record.
962 * @param h handle to the namestore
963 * @param block block to store
964 * @param cont continuation to call when done
965 * @param cont_cls closure for cont
966 * @return handle to abort the request
968 struct GNUNET_NAMESTORE_QueueEntry *
969 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
970 const struct GNUNET_NAMESTORE_Block *block,
971 GNUNET_NAMESTORE_ContinuationWithStatus cont,
974 struct GNUNET_NAMESTORE_QueueEntry *qe;
975 struct PendingMessage *pe;
976 struct BlockCacheMessage *msg;
981 GNUNET_assert (NULL != h);
982 blen = ntohl (block->purpose.size) - sizeof (struct GNUNET_TIME_AbsoluteNBO);
984 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
987 qe->cont_cls = cont_cls;
989 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
992 msg_size = sizeof (struct BlockCacheMessage) + blen;
993 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
995 msg = (struct BlockCacheMessage *) &pe[1];
996 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE);
997 msg->gns_header.header.size = htons (msg_size);
998 msg->gns_header.r_id = htonl (rid);
999 msg->expire = block->expiration_time;
1000 msg->signature = block->signature;
1001 msg->derived_key = block->derived_key;
1002 memcpy (&msg[1], &block[1], blen);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Sending `%s' message with size %u\n",
1005 "NAMESTORE_BLOCK_CACHE",
1006 (unsigned int) msg_size);
1007 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1014 * Store an item in the namestore. If the item is already present,
1015 * it is replaced with the new record. Use an empty array to
1016 * remove all records under the given name.
1018 * @param h handle to the namestore
1019 * @param pkey private key of the zone
1020 * @param label name that is being mapped (at most 255 characters long)
1021 * @param rd_count number of records in the 'rd' array
1022 * @param rd array of records with data to store
1023 * @param cont continuation to call when done
1024 * @param cont_cls closure for 'cont'
1025 * @return handle to abort the request
1027 struct GNUNET_NAMESTORE_QueueEntry *
1028 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1029 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
1031 unsigned int rd_count,
1032 const struct GNUNET_NAMESTORE_RecordData *rd,
1033 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1036 struct GNUNET_NAMESTORE_QueueEntry *qe;
1037 struct PendingMessage *pe;
1044 struct RecordStoreMessage *msg;
1046 GNUNET_assert (NULL != h);
1047 GNUNET_assert (NULL != pkey);
1048 GNUNET_assert (NULL != label);
1049 name_len = strlen (label) + 1;
1050 if (name_len > MAX_NAME_LEN)
1055 rid = get_op_id (h);
1056 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1059 qe->cont_cls = cont_cls;
1061 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1064 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1065 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1066 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1067 pe->size = msg_size;
1068 msg = (struct RecordStoreMessage *) &pe[1];
1069 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1070 msg->gns_header.header.size = htons (msg_size);
1071 msg->gns_header.r_id = htonl (rid);
1072 msg->name_len = htons (name_len);
1073 msg->rd_count = htons (rd_count);
1074 msg->rd_len = htons (rd_ser_len);
1075 msg->reserved = htons (0);
1076 msg->private_key = *pkey;
1078 name_tmp = (char *) &msg[1];
1079 memcpy (name_tmp, label, name_len);
1080 rd_ser = &name_tmp[name_len];
1081 GNUNET_break (rd_ser_len == GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser));
1083 LOG (GNUNET_ERROR_TYPE_DEBUG,
1084 "Sending `%s' message for name `%s' with size %u\n",
1085 "NAMESTORE_RECORD_STORE", label, msg_size);
1086 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1093 * Get a result for a particular key from the namestore. The processor
1094 * will only be called once.
1096 * @param h handle to the namestore
1097 * @param derived_hash hash of zone key combined with name to lookup
1098 * @param proc function to call on the matching block, or with
1099 * NULL if there is no matching block
1100 * @param proc_cls closure for proc
1101 * @return a handle that can be used to cancel
1103 struct GNUNET_NAMESTORE_QueueEntry *
1104 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h,
1105 const struct GNUNET_HashCode *derived_hash,
1106 GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls)
1108 struct GNUNET_NAMESTORE_QueueEntry *qe;
1109 struct PendingMessage *pe;
1110 struct LookupBlockMessage *msg;
1114 GNUNET_assert (NULL != h);
1115 GNUNET_assert (NULL != derived_hash);
1116 LOG (GNUNET_ERROR_TYPE_DEBUG,
1117 "Looking for block under %s\n",
1118 GNUNET_h2s (derived_hash));
1120 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1122 qe->block_proc = proc;
1123 qe->block_proc_cls = proc_cls;
1125 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1127 msg_size = sizeof (struct LookupBlockMessage);
1128 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1129 pe->size = msg_size;
1130 msg = (struct LookupBlockMessage *) &pe[1];
1131 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK);
1132 msg->gns_header.header.size = htons (msg_size);
1133 msg->gns_header.r_id = htonl (rid);
1134 msg->query = *derived_hash;
1135 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1142 * Look for an existing PKEY delegation record for a given public key.
1143 * Returns at most one result to the processor.
1145 * @param h handle to the namestore
1146 * @param zone public key of the zone to look up in, never NULL
1147 * @param value_zone public key of the target zone (value), never NULL
1148 * @param proc function to call on the matching records, or with
1149 * NULL (rd_count == 0) if there are no matching records
1150 * @param proc_cls closure for proc
1151 * @return a handle that can be used to
1154 struct GNUNET_NAMESTORE_QueueEntry *
1155 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1156 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1157 const struct GNUNET_CRYPTO_EccPublicSignKey *value_zone,
1158 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1160 struct GNUNET_NAMESTORE_QueueEntry *qe;
1161 struct PendingMessage *pe;
1162 struct ZoneToNameMessage * msg;
1166 GNUNET_assert (NULL != h);
1167 GNUNET_assert (NULL != zone);
1168 GNUNET_assert (NULL != value_zone);
1170 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1173 qe->proc_cls = proc_cls;
1175 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1177 msg_size = sizeof (struct ZoneToNameMessage);
1178 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1179 pe->size = msg_size;
1180 msg = (struct ZoneToNameMessage *) &pe[1];
1181 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1182 msg->gns_header.header.size = htons (msg_size);
1183 msg->gns_header.r_id = htonl (rid);
1185 msg->value_zone = *value_zone;
1187 /* transmit message */
1188 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1195 * Starts a new zone iteration (used to periodically PUT all of our
1196 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1197 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1198 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1199 * immediately, and then again after
1200 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1202 * @param h handle to the namestore
1203 * @param zone zone to access, NULL for all zones
1204 * @param proc function to call on each name from the zone; it
1205 * will be called repeatedly with a value (if available)
1206 * and always once at the end with a name of NULL.
1207 * @param proc_cls closure for @a proc
1208 * @return an iterator handle to use for iteration
1210 struct GNUNET_NAMESTORE_ZoneIterator *
1211 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1212 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1213 GNUNET_NAMESTORE_RecordMonitor proc,
1216 struct GNUNET_NAMESTORE_ZoneIterator *it;
1217 struct PendingMessage *pe;
1218 struct ZoneIterationStartMessage * msg;
1222 GNUNET_assert (NULL != h);
1224 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1227 it->proc_cls = proc_cls;
1231 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1233 msg_size = sizeof (struct ZoneIterationStartMessage);
1234 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1235 pe->size = msg_size;
1236 msg = (struct ZoneIterationStartMessage *) &pe[1];
1237 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1238 msg->gns_header.header.size = htons (msg_size);
1239 msg->gns_header.r_id = htonl (rid);
1242 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1249 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1250 * for the next record.
1252 * @param it the iterator
1255 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1257 struct GNUNET_NAMESTORE_Handle *h;
1258 struct ZoneIterationNextMessage * msg;
1259 struct PendingMessage *pe;
1262 GNUNET_assert (NULL != it);
1264 msg_size = sizeof (struct ZoneIterationNextMessage);
1265 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1266 pe->size = msg_size;
1267 msg = (struct ZoneIterationNextMessage *) &pe[1];
1268 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1269 msg->gns_header.header.size = htons (msg_size);
1270 msg->gns_header.r_id = htonl (it->op_id);
1271 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_NEXT");
1272 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1278 * Stops iteration and releases the namestore handle for further calls.
1280 * @param it the iterator
1283 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1285 struct GNUNET_NAMESTORE_Handle *h;
1286 struct PendingMessage *pe;
1288 struct ZoneIterationStopMessage * msg;
1290 GNUNET_assert (NULL != it);
1292 GNUNET_CONTAINER_DLL_remove (h->z_head,
1295 msg_size = sizeof (struct ZoneIterationStopMessage);
1296 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1297 pe->size = msg_size;
1298 msg = (struct ZoneIterationStopMessage *) &pe[1];
1299 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1300 msg->gns_header.header.size = htons (msg_size);
1301 msg->gns_header.r_id = htonl (it->op_id);
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Sending `%s' message\n",
1304 "ZONE_ITERATION_STOP");
1305 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1312 * Cancel a namestore operation. The final callback from the
1313 * operation must not have been done yet.
1315 * @param qe operation to cancel
1318 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1320 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1322 GNUNET_assert (NULL != qe);
1323 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1328 /* end of namestore_api.c */