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;
81 * Closure for @e proc.
86 * The operation id this zone iteration operation has
94 * Handle for a zone iterator operation
96 struct GNUNET_NAMESTORE_ZoneIterator
102 struct GNUNET_NAMESTORE_ZoneIterator *next;
107 struct GNUNET_NAMESTORE_ZoneIterator *prev;
110 * Main handle to access the namestore.
112 struct GNUNET_NAMESTORE_Handle *h;
115 * The continuation to call with the results
117 GNUNET_NAMESTORE_RecordMonitor proc;
120 * Closure for @e proc.
125 * Private key of the zone.
127 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
130 * The operation id this zone iteration operation has
138 * Message in linked list we should send to the service. The
139 * actual binary message follows this struct.
141 struct PendingMessage
147 struct PendingMessage *next;
152 struct PendingMessage *prev;
155 * Size of the message.
163 * Connection to the NAMESTORE service.
165 struct GNUNET_NAMESTORE_Handle
169 * Configuration to use.
171 const struct GNUNET_CONFIGURATION_Handle *cfg;
174 * Socket (if available).
176 struct GNUNET_CLIENT_Connection *client;
179 * Currently pending transmission request (or NULL).
181 struct GNUNET_CLIENT_TransmitHandle *th;
184 * Head of linked list of pending messages to send to the service
186 struct PendingMessage *pending_head;
189 * Tail of linked list of pending messages to send to the service
191 struct PendingMessage *pending_tail;
194 * Head of pending namestore queue entries
196 struct GNUNET_NAMESTORE_QueueEntry *op_head;
199 * Tail of pending namestore queue entries
201 struct GNUNET_NAMESTORE_QueueEntry *op_tail;
204 * Head of pending namestore zone iterator entries
206 struct GNUNET_NAMESTORE_ZoneIterator *z_head;
209 * Tail of pending namestore zone iterator entries
211 struct GNUNET_NAMESTORE_ZoneIterator *z_tail;
216 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
219 * Delay introduced before we reconnect.
221 struct GNUNET_TIME_Relative reconnect_delay;
224 * Should we reconnect to service due to some serious error?
229 * Did we start to receive yet?
234 * The last operation id used for a NAMESTORE operation
236 uint32_t last_op_id_used;
242 * Disconnect from service and then reconnect.
244 * @param h our handle
247 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
251 * Handle an incoming message of type
252 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
254 * @param qe the respective entry in the message queue
255 * @param msg the message we received
256 * @param size the message size
257 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
260 handle_record_store_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
261 const struct RecordStoreResponseMessage* msg,
267 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "RECORD_STORE_RESPONSE");
270 /* TODO: add actual error message from namestore to response... */
271 res = ntohl (msg->op_result);
272 if (GNUNET_SYSERR == res)
273 emsg = _("Namestore failed to store record\n");
276 if (NULL != qe->cont)
277 qe->cont (qe->cont_cls, res, emsg);
283 * Handle an incoming message of type
284 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
286 * @param qe the respective entry in the message queue
287 * @param msg the message we received
288 * @param size the message size
289 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
292 handle_lookup_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
293 const struct LabelLookupResponseMessage *msg,
302 unsigned int rd_count;
305 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 "RECORD_LOOKUP_RESULT");
309 rd_len = ntohs (msg->rd_len);
310 rd_count = ntohs (msg->rd_count);
311 msg_len = ntohs (msg->gns_header.header.size);
312 name_len = ntohs (msg->name_len);
313 found = ntohs (msg->found);
314 exp_msg_len = sizeof (struct LabelLookupResponseMessage) + name_len + rd_len;
315 if (msg_len != exp_msg_len)
318 return GNUNET_SYSERR;
320 name = (const char *) &msg[1];
321 if ( (name_len > 0) &&
322 ('\0' != name[name_len -1]) )
325 return GNUNET_SYSERR;
327 if (GNUNET_NO == found)
329 /* label was not in namestore */
330 if (NULL != qe->proc)
331 qe->proc (qe->proc_cls,
338 rd_tmp = &name[name_len];
340 struct GNUNET_GNSRECORD_Data rd[rd_count];
342 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_len, rd_tmp, rd_count, rd))
345 return GNUNET_SYSERR;
349 if (NULL != qe->proc)
350 qe->proc (qe->proc_cls,
354 (rd_count > 0) ? rd : NULL);
361 * Handle an incoming message of type
362 * #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
364 * @param qe the respective entry in the message queue
365 * @param msg the message we received
366 * @param size the message size
367 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
370 handle_record_result (struct GNUNET_NAMESTORE_QueueEntry *qe,
371 const struct RecordResultMessage *msg,
380 unsigned int rd_count;
382 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 rd_len = ntohs (msg->rd_len);
386 rd_count = ntohs (msg->rd_count);
387 msg_len = ntohs (msg->gns_header.header.size);
388 name_len = ntohs (msg->name_len);
389 GNUNET_break (0 == ntohs (msg->reserved));
390 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
391 if (msg_len != exp_msg_len)
394 return GNUNET_SYSERR;
396 name = (const char *) &msg[1];
397 if ( (name_len > 0) &&
398 ('\0' != name[name_len -1]) )
401 return GNUNET_SYSERR;
403 rd_tmp = &name[name_len];
405 struct GNUNET_GNSRECORD_Data rd[rd_count];
407 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_len, rd_tmp, rd_count, rd))
410 return GNUNET_SYSERR;
414 if (NULL != qe->proc)
415 qe->proc (qe->proc_cls,
419 (rd_count > 0) ? rd : NULL);
426 * Handle an incoming message of type
427 * #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,
455 "An error occured during zone to name operation\n");
458 LOG (GNUNET_ERROR_TYPE_DEBUG,
459 "Namestore has no result for zone to name mapping \n");
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 "Namestore has result for zone to name mapping \n");
464 name_len = ntohs (msg->name_len);
465 rd_count = ntohs (msg->rd_count);
466 rd_ser_len = ntohs (msg->rd_len);
467 name_tmp = (const char *) &msg[1];
468 if ( (name_len > 0) &&
469 ('\0' != name_tmp[name_len -1]) )
472 return GNUNET_SYSERR;
474 rd_tmp = &name_tmp[name_len];
476 struct GNUNET_GNSRECORD_Data rd[rd_count];
478 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd))
481 return GNUNET_SYSERR;
483 /* normal end, call continuation with result */
484 if (NULL != qe->proc)
485 qe->proc (qe->proc_cls,
489 /* return is important here: break would call continuation with error! */
494 return GNUNET_SYSERR;
496 /* error case, call continuation with error */
497 if (NULL != qe->proc)
498 qe->proc (qe->proc_cls, NULL, NULL, 0, NULL);
504 * Handle incoming messages for record operations
506 * @param qe the respective zone iteration handle
507 * @param msg the message we received
508 * @param type the message type in host byte order
509 * @param size the message size
510 * @return #GNUNET_OK on success, #GNUNET_NO if we notified the client about
511 * the error, #GNUNET_SYSERR on error and we did NOT notify the client
514 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
515 const struct GNUNET_MessageHeader *msg,
519 /* handle different message type */
522 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE:
523 if (size != sizeof (struct RecordStoreResponseMessage))
526 return GNUNET_SYSERR;
528 return handle_record_store_response (qe, (const struct RecordStoreResponseMessage *) msg, size);
529 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
530 if (size < sizeof (struct ZoneToNameResponseMessage))
533 return GNUNET_SYSERR;
535 return handle_zone_to_name_response (qe, (const struct ZoneToNameResponseMessage *) msg, size);
536 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
537 if (size < sizeof (struct RecordResultMessage))
540 return GNUNET_SYSERR;
542 return handle_record_result (qe, (const struct RecordResultMessage *) msg, size);
543 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE:
544 if (size < sizeof (struct LabelLookupResponseMessage))
547 return GNUNET_SYSERR;
549 return handle_lookup_result (qe, (const struct LabelLookupResponseMessage *) msg, size);
552 return GNUNET_SYSERR;
558 * Handle a response from NAMESTORE service for a zone iteration request
560 * @param ze the respective iterator for this operation
561 * @param msg the message containing the respoonse
562 * @param size the message size
563 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
564 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
567 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
568 const struct RecordResultMessage *msg,
571 static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
577 const char *name_tmp;
578 const char *rd_ser_tmp;
580 LOG (GNUNET_ERROR_TYPE_DEBUG,
582 "ZONE_ITERATION_RESPONSE");
583 msg_len = ntohs (msg->gns_header.header.size);
584 rd_len = ntohs (msg->rd_len);
585 rd_count = ntohs (msg->rd_count);
586 name_len = ntohs (msg->name_len);
587 exp_msg_len = sizeof (struct RecordResultMessage) + name_len + rd_len;
588 if (msg_len != exp_msg_len)
591 return GNUNET_SYSERR;
593 if ( (0 == name_len) &&
594 (0 == (memcmp (&msg->private_key,
596 sizeof (priv_dummy)))) )
598 LOG (GNUNET_ERROR_TYPE_DEBUG,
599 "Zone iteration 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_GNSRECORD_Data rd[rd_count];
614 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (rd_len,
620 return GNUNET_SYSERR;
622 if (NULL != ze->proc)
623 ze->proc (ze->proc_cls,
633 * Handle incoming messages for zone iterations
635 * @param ze the respective zone iteration handle
636 * @param msg the message we received
637 * @param type the message type in HBO
638 * @param size the message size
639 * @return #GNUNET_YES on success, @a ze should be kept, #GNUNET_NO on success if @a ze should
640 * not be kept any longer, #GNUNET_SYSERR on error (disconnect) and @a ze should be kept
643 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
644 const struct GNUNET_MessageHeader *msg,
645 int type, size_t size)
647 /* handle different message type */
650 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT:
651 if (size < sizeof (struct RecordResultMessage))
654 return GNUNET_SYSERR;
656 return handle_zone_iteration_response (ze,
657 (const struct RecordResultMessage *) msg,
661 return GNUNET_SYSERR;
667 * Type of a function to call when we receive a message
670 * @param cls the `struct GNUNET_NAMESTORE_SchedulingHandle`
671 * @param msg message received, NULL on timeout or fatal error
674 process_namestore_message (void *cls,
675 const struct GNUNET_MessageHeader *msg)
677 struct GNUNET_NAMESTORE_Handle *h = cls;
678 const struct GNUNET_NAMESTORE_Header *gm;
679 struct GNUNET_NAMESTORE_QueueEntry *qe;
680 struct GNUNET_NAMESTORE_ZoneIterator *ze;
691 size = ntohs (msg->size);
692 type = ntohs (msg->type);
693 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
696 GNUNET_CLIENT_receive (h->client,
697 &process_namestore_message, h,
698 GNUNET_TIME_UNIT_FOREVER_REL);
701 gm = (const struct GNUNET_NAMESTORE_Header *) msg;
702 r_id = ntohl (gm->r_id);
704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "Received message type %u size %u op %u\n",
708 (unsigned int) r_id);
710 /* Is it a record related operation ? */
711 for (qe = h->op_head; qe != NULL; qe = qe->next)
712 if (qe->op_id == r_id)
716 ret = manage_record_operations (qe, msg, type, size);
717 if (GNUNET_SYSERR == ret)
719 /* protocol error, need to reconnect */
720 h->reconnect = GNUNET_YES;
724 /* client was notified about success or failure, clean up 'qe' */
725 GNUNET_CONTAINER_DLL_remove (h->op_head,
731 /* Is it a zone iteration operation? */
732 for (ze = h->z_head; ze != NULL; ze = ze->next)
733 if (ze->op_id == r_id)
737 ret = manage_zone_operations (ze, msg, type, size);
738 if (GNUNET_NO == ret)
740 /* end of iteration, clean up 'ze' */
741 GNUNET_CONTAINER_DLL_remove (h->z_head,
746 if (GNUNET_SYSERR == ret)
748 /* protocol error, need to reconnect */
749 h->reconnect = GNUNET_YES;
752 if (GNUNET_YES == h->reconnect)
757 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
758 GNUNET_TIME_UNIT_FOREVER_REL);
763 * Transmit messages from the message queue to the service
764 * (if there are any, and if we are not already trying).
766 * @param h handle to use
769 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
773 * We can now transmit a message to NAMESTORE. Do it.
775 * @param cls the `struct GNUNET_NAMESTORE_Handle`
776 * @param size number of bytes we can transmit
777 * @param buf where to copy the messages
778 * @return number of bytes copied into @a buf
781 transmit_message_to_namestore (void *cls,
785 struct GNUNET_NAMESTORE_Handle *h = cls;
786 struct PendingMessage *p;
791 if ((0 == size) || (NULL == buf))
798 while ( (NULL != (p = h->pending_head)) &&
801 memcpy (&cbuf[ret], &p[1], p->size);
804 GNUNET_CONTAINER_DLL_remove (h->pending_head,
807 if (GNUNET_NO == h->is_receiving)
809 h->is_receiving = GNUNET_YES;
810 GNUNET_CLIENT_receive (h->client,
811 &process_namestore_message, h,
812 GNUNET_TIME_UNIT_FOREVER_REL);
822 * Transmit messages from the message queue to the service
823 * (if there are any, and if we are not already trying).
825 * @param h handle to use
828 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
830 struct PendingMessage *p;
833 return; /* transmission request already pending */
834 if (NULL == (p = h->pending_head))
835 return; /* transmission queue empty */
836 if (NULL == h->client)
837 return; /* currently reconnecting */
838 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
839 GNUNET_TIME_UNIT_FOREVER_REL,
840 GNUNET_NO, &transmit_message_to_namestore,
842 GNUNET_break (NULL != h->th);
847 * Reconnect to namestore service.
849 * @param h the handle to the NAMESTORE service
852 reconnect (struct GNUNET_NAMESTORE_Handle *h)
854 GNUNET_assert (NULL == h->client);
855 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
856 GNUNET_assert (NULL != h->client);
862 * Re-establish the connection to the service.
864 * @param cls handle to use to re-connect.
865 * @param tc scheduler context
868 reconnect_task (void *cls,
869 const struct GNUNET_SCHEDULER_TaskContext *tc)
871 struct GNUNET_NAMESTORE_Handle *h = cls;
873 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
879 * Disconnect from service and then reconnect.
881 * @param h our handle
884 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
888 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
891 h->reconnect = GNUNET_NO;
892 GNUNET_CLIENT_disconnect (h->client);
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Reconnecting to namestore\n");
895 h->is_receiving = GNUNET_NO;
897 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
898 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
905 * Get a fresh operation id to distinguish between namestore requests
907 * @param h the namestore handle
908 * @return next operation id to use
911 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
913 return h->last_op_id_used++;
918 * Initialize the connection with the NAMESTORE service.
920 * @param cfg configuration to use
921 * @return handle to the GNS service, or NULL on error
923 struct GNUNET_NAMESTORE_Handle *
924 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
926 struct GNUNET_NAMESTORE_Handle *h;
928 h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
930 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
931 h->last_op_id_used = 0;
937 * Disconnect from the namestore service (and free associated
940 * @param h handle to the namestore
943 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h)
945 struct PendingMessage *p;
946 struct GNUNET_NAMESTORE_QueueEntry *q;
947 struct GNUNET_NAMESTORE_ZoneIterator *z;
949 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
950 GNUNET_assert (NULL != h);
953 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
956 while (NULL != (p = h->pending_head))
958 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
961 GNUNET_break (NULL == h->op_head);
962 while (NULL != (q = h->op_head))
964 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
967 GNUNET_break (NULL == h->z_head);
968 while (NULL != (z = h->z_head))
970 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
973 if (NULL != h->client)
975 GNUNET_CLIENT_disconnect (h->client);
978 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
980 GNUNET_SCHEDULER_cancel (h->reconnect_task);
981 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
988 * Store an item in the namestore. If the item is already present,
989 * it is replaced with the new record. Use an empty array to
990 * remove all records under the given name.
992 * @param h handle to the namestore
993 * @param pkey private key of the zone
994 * @param label name that is being mapped (at most 255 characters long)
995 * @param rd_count number of records in the 'rd' array
996 * @param rd array of records with data to store
997 * @param cont continuation to call when done
998 * @param cont_cls closure for 'cont'
999 * @return handle to abort the request
1001 struct GNUNET_NAMESTORE_QueueEntry *
1002 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
1003 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1005 unsigned int rd_count,
1006 const struct GNUNET_GNSRECORD_Data *rd,
1007 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1010 struct GNUNET_NAMESTORE_QueueEntry *qe;
1011 struct PendingMessage *pe;
1018 struct RecordStoreMessage *msg;
1020 GNUNET_assert (NULL != h);
1021 GNUNET_assert (NULL != pkey);
1022 GNUNET_assert (NULL != label);
1023 name_len = strlen (label) + 1;
1024 if (name_len > MAX_NAME_LEN)
1029 rid = get_op_id (h);
1030 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1033 qe->cont_cls = cont_cls;
1035 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1038 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1039 msg_size = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1040 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1041 pe->size = msg_size;
1042 msg = (struct RecordStoreMessage *) &pe[1];
1043 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE);
1044 msg->gns_header.header.size = htons (msg_size);
1045 msg->gns_header.r_id = htonl (rid);
1046 msg->name_len = htons (name_len);
1047 msg->rd_count = htons (rd_count);
1048 msg->rd_len = htons (rd_ser_len);
1049 msg->reserved = htons (0);
1050 msg->private_key = *pkey;
1052 name_tmp = (char *) &msg[1];
1053 memcpy (name_tmp, label, name_len);
1054 rd_ser = &name_tmp[name_len];
1055 GNUNET_break (rd_ser_len ==
1056 GNUNET_GNSRECORD_records_serialize (rd_count, rd,
1059 LOG (GNUNET_ERROR_TYPE_DEBUG,
1060 "Sending `%s' message for name `%s' with size %u and %u records\n",
1061 "NAMESTORE_RECORD_STORE", label, msg_size,
1063 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1070 * Lookup an item in the namestore.
1072 * @param h handle to the namestore
1073 * @param pkey private key of the zone
1074 * @param label name that is being mapped (at most 255 characters long)
1075 * @param rm function to call with the result (with 0 records if we don't have that label)
1076 * @param rm_cls closure for @a rm
1077 * @return handle to abort the request
1079 struct GNUNET_NAMESTORE_QueueEntry *
1080 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
1081 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1083 GNUNET_NAMESTORE_RecordMonitor rm,
1086 struct GNUNET_NAMESTORE_QueueEntry *qe;
1087 struct PendingMessage *pe;
1088 struct LabelLookupMessage * msg;
1092 GNUNET_assert (NULL != h);
1093 GNUNET_assert (NULL != pkey);
1094 GNUNET_assert (NULL != label);
1096 if (1 == (label_len = strlen (label) + 1))
1099 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1102 qe->proc_cls = rm_cls;
1103 qe->op_id = get_op_id(h);
1104 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1106 msg_size = sizeof (struct LabelLookupMessage) + label_len;
1107 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1108 pe->size = msg_size;
1109 msg = (struct LabelLookupMessage *) &pe[1];
1110 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP);
1111 msg->gns_header.header.size = htons (msg_size);
1112 msg->gns_header.r_id = htonl (qe->op_id);
1114 msg->label_len = htonl(label_len);
1115 memcpy (&msg[1], label, label_len);
1117 /* transmit message */
1118 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1125 * Look for an existing PKEY delegation record for a given public key.
1126 * Returns at most one result to the processor.
1128 * @param h handle to the namestore
1129 * @param zone public key of the zone to look up in, never NULL
1130 * @param value_zone public key of the target zone (value), never NULL
1131 * @param proc function to call on the matching records, or with
1132 * NULL (rd_count == 0) if there are no matching records
1133 * @param proc_cls closure for @a proc
1134 * @return a handle that can be used to
1137 struct GNUNET_NAMESTORE_QueueEntry *
1138 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1139 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1140 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1141 GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
1143 struct GNUNET_NAMESTORE_QueueEntry *qe;
1144 struct PendingMessage *pe;
1145 struct ZoneToNameMessage * msg;
1149 GNUNET_assert (NULL != h);
1150 GNUNET_assert (NULL != zone);
1151 GNUNET_assert (NULL != value_zone);
1153 qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry);
1156 qe->proc_cls = proc_cls;
1158 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1160 msg_size = sizeof (struct ZoneToNameMessage);
1161 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1162 pe->size = msg_size;
1163 msg = (struct ZoneToNameMessage *) &pe[1];
1164 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1165 msg->gns_header.header.size = htons (msg_size);
1166 msg->gns_header.r_id = htonl (rid);
1168 msg->value_zone = *value_zone;
1170 /* transmit message */
1171 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1178 * Starts a new zone iteration (used to periodically PUT all of our
1179 * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
1180 * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
1181 * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
1182 * immediately, and then again after
1183 * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
1185 * @param h handle to the namestore
1186 * @param zone zone to access, NULL for all zones
1187 * @param proc function to call on each name from the zone; it
1188 * will be called repeatedly with a value (if available)
1189 * and always once at the end with a name of NULL.
1190 * @param proc_cls closure for @a proc
1191 * @return an iterator handle to use for iteration
1193 struct GNUNET_NAMESTORE_ZoneIterator *
1194 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1195 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1196 GNUNET_NAMESTORE_RecordMonitor proc,
1199 struct GNUNET_NAMESTORE_ZoneIterator *it;
1200 struct PendingMessage *pe;
1201 struct ZoneIterationStartMessage * msg;
1205 GNUNET_assert (NULL != h);
1207 it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator);
1210 it->proc_cls = proc_cls;
1214 GNUNET_CONTAINER_DLL_insert_tail (h->z_head, h->z_tail, it);
1216 msg_size = sizeof (struct ZoneIterationStartMessage);
1217 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1218 pe->size = msg_size;
1219 msg = (struct ZoneIterationStartMessage *) &pe[1];
1220 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1221 msg->gns_header.header.size = htons (msg_size);
1222 msg->gns_header.r_id = htonl (rid);
1225 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1232 * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
1233 * for the next record.
1235 * @param it the iterator
1238 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1240 struct GNUNET_NAMESTORE_Handle *h;
1241 struct ZoneIterationNextMessage * msg;
1242 struct PendingMessage *pe;
1245 GNUNET_assert (NULL != it);
1247 msg_size = sizeof (struct ZoneIterationNextMessage);
1248 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1249 pe->size = msg_size;
1250 msg = (struct ZoneIterationNextMessage *) &pe[1];
1251 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1252 msg->gns_header.header.size = htons (msg_size);
1253 msg->gns_header.r_id = htonl (it->op_id);
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Sending `%s' message\n",
1256 "ZONE_ITERATION_NEXT");
1257 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1263 * Stops iteration and releases the namestore handle for further calls.
1265 * @param it the iterator
1268 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1270 struct GNUNET_NAMESTORE_Handle *h;
1271 struct PendingMessage *pe;
1273 struct ZoneIterationStopMessage * msg;
1275 GNUNET_assert (NULL != it);
1277 GNUNET_CONTAINER_DLL_remove (h->z_head,
1280 msg_size = sizeof (struct ZoneIterationStopMessage);
1281 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1282 pe->size = msg_size;
1283 msg = (struct ZoneIterationStopMessage *) &pe[1];
1284 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1285 msg->gns_header.header.size = htons (msg_size);
1286 msg->gns_header.r_id = htonl (it->op_id);
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Sending `%s' message\n",
1289 "ZONE_ITERATION_STOP");
1290 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1297 * Cancel a namestore operation. The final callback from the
1298 * operation must not have been done yet.
1300 * @param qe operation to cancel
1303 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1305 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1307 GNUNET_assert (NULL != qe);
1308 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, qe);
1313 /* end of namestore_api.c */