2 This file is part of GNUnet.
3 (C) 2009, 2010 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
29 #include "gnunet_util_lib.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "namestore.h"
38 #include "gnunet_common.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_disk_lib.h"
42 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
44 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
49 struct GNUNET_NAMESTORE_QueueEntry
51 struct GNUNET_NAMESTORE_QueueEntry *next;
52 struct GNUNET_NAMESTORE_QueueEntry *prev;
54 struct GNUNET_NAMESTORE_Handle *nsh;
58 GNUNET_NAMESTORE_ContinuationWithStatus cont;
61 GNUNET_NAMESTORE_RecordProcessor proc;
64 char *data; /*stub data pointer*/
71 struct GNUNET_NAMESTORE_ZoneIterator
73 struct GNUNET_NAMESTORE_ZoneIterator *next;
74 struct GNUNET_NAMESTORE_ZoneIterator *prev;
78 struct GNUNET_NAMESTORE_Handle *h;
79 GNUNET_NAMESTORE_RecordProcessor proc;
88 * Message in linked list we should send to the service. The
89 * actual binary message follows this struct.
97 struct PendingMessage *next;
102 struct PendingMessage *prev;
105 * Size of the message.
110 * Is this the 'START' message?
117 * Connection to the NAMESTORE service.
119 struct GNUNET_NAMESTORE_Handle
123 * Configuration to use.
125 const struct GNUNET_CONFIGURATION_Handle *cfg;
128 * Socket (if available).
130 struct GNUNET_CLIENT_Connection *client;
133 * Currently pending transmission request (or NULL).
135 struct GNUNET_CLIENT_TransmitHandle *th;
140 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
143 * Pending messages to send to the service
146 struct PendingMessage * pending_head;
147 struct PendingMessage * pending_tail;
150 * Should we reconnect to service due to some serious error?
156 * Pending namestore queue entries
158 struct GNUNET_NAMESTORE_QueueEntry * op_head;
159 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
164 * Pending namestore zone iterator entries
166 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
167 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
170 struct GNUNET_NAMESTORE_SimpleRecord
175 struct GNUNET_NAMESTORE_SimpleRecord *next;
180 struct GNUNET_NAMESTORE_SimpleRecord *prev;
183 const GNUNET_HashCode *zone;
184 uint32_t record_type;
185 struct GNUNET_TIME_Absolute expiration;
186 enum GNUNET_NAMESTORE_RecordFlags flags;
193 * Disconnect from service and then reconnect.
195 * @param h our handle
198 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
201 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
202 struct LookupNameResponseMessage * msg,
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
206 "LOOKUP_NAME_RESPONSE");
208 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
209 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
213 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
214 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
215 struct GNUNET_TIME_Absolute expire;
220 int contains_sig = GNUNET_NO;
223 rd_len = ntohs (msg->rd_len);
224 rd_count = ntohs (msg->rd_count);
225 msg_len = ntohs (msg->gns_header.header.size);
226 name_len = ntohs (msg->name_len);
227 contains_sig = ntohs (msg->contains_sig);
228 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
230 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
231 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
234 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
236 if (msg_len != exp_msg_len)
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
239 msg_len, exp_msg_len);
244 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
245 name = (char *) &zone_key[1];
246 rd_tmp = &name[name_len];
248 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
249 GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
251 /* reset values if values not contained */
252 if (contains_sig == GNUNET_NO)
255 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_len];
259 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
260 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
263 if (qe->proc != NULL)
265 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
268 /* Operation done, remove */
269 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
275 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
276 struct RecordPutResponseMessage* msg,
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
280 "RECORD_PUT_RESPONSE");
282 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
285 if (ntohs (msg->op_result) == GNUNET_OK)
288 if (qe->cont != NULL)
290 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
294 else if (ntohs (msg->op_result) == GNUNET_NO)
297 if (qe->cont != NULL)
299 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
308 /* Operation done, remove */
309 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
316 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
317 struct RecordCreateResponseMessage* msg,
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
321 "RECORD_CREATE_RESPONSE");
323 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
326 if (ntohs (msg->op_result) == GNUNET_OK)
329 if (qe->cont != NULL)
331 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
335 else if (ntohs (msg->op_result) == GNUNET_NO)
338 if (qe->cont != NULL)
340 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
349 /* Operation done, remove */
350 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
357 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
358 const struct GNUNET_MessageHeader *msg,
359 int type, size_t size)
362 /* handle different message type */
364 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
365 if (size < sizeof (struct LookupNameResponseMessage))
370 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
372 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
373 if (size != sizeof (struct RecordPutResponseMessage))
378 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
380 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
381 if (size != sizeof (struct RecordCreateResponseMessage))
386 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
395 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
396 struct ZoneIterationResponseMessage *msg,
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
400 "ZONE_ITERATION_RESPONSE");
403 if (ze->proc != NULL)
406 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
412 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
413 const struct GNUNET_MessageHeader *msg,
414 int type, size_t size)
417 /* handle different message type */
419 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
420 if (size < sizeof (struct ZoneIterationResponseMessage))
425 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
434 * Type of a function to call when we receive a message
437 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
438 * @param msg message received, NULL on timeout or fatal error
441 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
443 struct GNUNET_NAMESTORE_Handle *h = cls;
444 struct GNUNET_NAMESTORE_Header * gm;
445 struct GNUNET_NAMESTORE_QueueEntry *qe;
446 struct GNUNET_NAMESTORE_ZoneIterator *ze;
449 uint32_t r_id = UINT32_MAX;
457 size = ntohs (msg->size);
458 type = ntohs (msg->type);
460 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
463 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
464 GNUNET_TIME_UNIT_FOREVER_REL);
468 gm = (struct GNUNET_NAMESTORE_Header *) msg;
469 r_id = ntohl (gm->r_id);
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
473 /* Find matching operation */
476 /* No matching pending operation found */
478 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
479 GNUNET_TIME_UNIT_FOREVER_REL);
483 /* Is it a record related operation ? */
484 for (qe = h->op_head; qe != NULL; qe = qe->next)
486 if (qe->op_id == r_id)
491 manage_record_operations (qe, msg, type, size);
494 /* Is it a zone iteration operation ? */
495 for (ze = h->z_head; ze != NULL; ze = ze->next)
497 if (ze->op_id == r_id)
502 manage_zone_operations (ze, msg, type, size);
505 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
506 GNUNET_TIME_UNIT_FOREVER_REL);
508 if (GNUNET_YES == h->reconnect)
515 * Transmit messages from the message queue to the service
516 * (if there are any, and if we are not already trying).
518 * @param h handle to use
521 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
525 * We can now transmit a message to NAMESTORE. Do it.
527 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
528 * @param size number of bytes we can transmit
529 * @param buf where to copy the messages
530 * @return number of bytes copied into buf
533 transmit_message_to_namestore (void *cls, size_t size, void *buf)
535 struct GNUNET_NAMESTORE_Handle *h = cls;
536 struct PendingMessage *p;
541 if ((size == 0) || (buf == NULL))
548 while ((NULL != (p = h->pending_head)) && (p->size <= size))
550 memcpy (&cbuf[ret], &p[1], p->size);
553 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
554 if (GNUNET_YES == p->is_init)
555 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
556 GNUNET_TIME_UNIT_FOREVER_REL);
565 * Transmit messages from the message queue to the service
566 * (if there are any, and if we are not already trying).
568 * @param h handle to use
571 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
573 struct PendingMessage *p;
577 if (NULL == (p = h->pending_head))
579 if (NULL == h->client)
580 return; /* currently reconnecting */
582 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
583 GNUNET_TIME_UNIT_FOREVER_REL,
584 GNUNET_NO, &transmit_message_to_namestore,
590 * Reconnect to namestore service.
592 * @param h the handle to the namestore service
595 reconnect (struct GNUNET_NAMESTORE_Handle *h)
597 struct PendingMessage *p;
598 struct StartMessage *init;
600 GNUNET_assert (NULL == h->client);
601 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
602 GNUNET_assert (NULL != h->client);
604 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
606 p = GNUNET_malloc (sizeof (struct PendingMessage) +
607 sizeof (struct StartMessage));
608 p->size = sizeof (struct StartMessage);
609 p->is_init = GNUNET_YES;
610 init = (struct StartMessage *) &p[1];
611 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
612 init->header.size = htons (sizeof (struct StartMessage));
613 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
619 * Re-establish the connection to the service.
621 * @param cls handle to use to re-connect.
622 * @param tc scheduler context
625 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
627 struct GNUNET_NAMESTORE_Handle *h = cls;
629 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
635 * Disconnect from service and then reconnect.
637 * @param h our handle
640 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
642 h->reconnect = GNUNET_NO;
643 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
645 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
651 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
653 uint32_t op_id = h->op_id;
659 * Initialize the connection with the NAMESTORE service.
661 * @param cfg configuration to use
662 * @return handle to the GNS service, or NULL on error
664 struct GNUNET_NAMESTORE_Handle *
665 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
667 struct GNUNET_NAMESTORE_Handle *h;
669 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
671 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
678 * Disconnect from the namestore service (and free associated
681 * @param h handle to the namestore
682 * @param drop set to GNUNET_YES to delete all data in namestore (!)
685 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
687 struct PendingMessage *p;
688 struct GNUNET_NAMESTORE_QueueEntry *q;
689 struct GNUNET_NAMESTORE_ZoneIterator *z;
691 GNUNET_assert (h != NULL);
693 while (NULL != (p = h->pending_head))
695 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
699 while (NULL != (q = h->op_head))
701 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
705 while (NULL != (z = h->z_head))
707 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
711 if (NULL != h->client)
713 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
716 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
718 GNUNET_SCHEDULER_cancel (h->reconnect_task);
719 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
727 * Store an item in the namestore. If the item is already present,
728 * the expiration time is updated to the max of the existing time and
729 * the new time. This API is used when we cache signatures from other
732 * @param h handle to the namestore
733 * @param zone_key public key of the zone
734 * @param name name that is being mapped (at most 255 characters long)
735 * @param expire when does the corresponding block in the DHT expire (until
736 * when should we never do a DHT lookup for the same name again)?
737 * @param rd_count number of entries in 'rd' array
738 * @param rd array of records with data to store
739 * @param signature signature for all the records in the zone under the given name
740 * @param cont continuation to call when done
741 * @param cont_cls closure for cont
742 * @return handle to abort the request
744 struct GNUNET_NAMESTORE_QueueEntry *
745 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
746 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
748 struct GNUNET_TIME_Absolute expire,
749 unsigned int rd_count,
750 const struct GNUNET_NAMESTORE_RecordData *rd,
751 const struct GNUNET_CRYPTO_RsaSignature *signature,
752 GNUNET_NAMESTORE_ContinuationWithStatus cont,
755 struct GNUNET_NAMESTORE_QueueEntry *qe;
756 struct PendingMessage *pe;
758 /* pointer to elements */
765 size_t rd_ser_len = 0;
766 size_t pubkey_len = 0;
769 GNUNET_assert (NULL != h);
770 GNUNET_assert (NULL != zone_key);
771 GNUNET_assert (NULL != name);
772 GNUNET_assert (NULL != rd);
773 GNUNET_assert (NULL != signature);
775 name_len = strlen(name) + 1;
783 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
786 qe->cont_cls = cont_cls;
788 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
791 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
792 char rd_ser[rd_ser_len];
793 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
795 pubkey_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
796 struct RecordPutMessage * msg;
797 msg_size = sizeof (struct RecordPutMessage) + pubkey_len + name_len + rd_ser_len;
799 /* create msg here */
800 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
802 pe->is_init = GNUNET_NO;
803 msg = (struct RecordPutMessage *) &pe[1];
804 zone_key_tmp = (char *) &msg[1];
805 name_tmp = (char *) &zone_key_tmp[pubkey_len];
806 rd_tmp = &name_tmp[name_len];
808 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
809 msg->gns_header.header.size = htons (msg_size);
810 msg->gns_header.r_id = htonl (rid);
811 msg->key_len = htons (pubkey_len);
812 memcpy (zone_key_tmp, zone_key, pubkey_len);
813 msg->signature = *signature;
814 msg->name_len = htons (name_len);
815 memcpy (name_tmp, name, name_len);
816 msg->expire = GNUNET_TIME_absolute_hton (expire);
817 msg->rd_len = htons (rd_ser_len);
818 msg->rd_count = htons (rd_count);
820 memcpy (rd_tmp, rd_ser, rd_ser_len);
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
824 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
832 * Check if a signature is valid. This API is used by the GNS Block
833 * to validate signatures received from the network.
835 * @param public_key public key of the zone
836 * @param name name that is being mapped (at most 255 characters long)
837 * @param rd_count number of entries in 'rd' array
838 * @param rd array of records with data to store
839 * @param signature signature for all the records in the zone under the given name
840 * @return GNUNET_OK if the signature is valid
843 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
845 unsigned int rd_count,
846 const struct GNUNET_NAMESTORE_RecordData *rd,
847 const struct GNUNET_CRYPTO_RsaSignature *signature)
849 return GNUNET_SYSERR;
853 * Store an item in the namestore. If the item is already present,
854 * the expiration time is updated to the max of the existing time and
855 * the new time. This API is used by the authority of a zone.
857 * @param h handle to the namestore
858 * @param pkey private key of the zone
859 * @param name name that is being mapped (at most 255 characters long)
860 * @param rd record data to store
861 * @param cont continuation to call when done
862 * @param cont_cls closure for cont
863 * @return handle to abort the request
865 struct GNUNET_NAMESTORE_QueueEntry *
866 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
867 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
869 const struct GNUNET_NAMESTORE_RecordData *rd,
870 GNUNET_NAMESTORE_ContinuationWithStatus cont,
873 struct GNUNET_NAMESTORE_QueueEntry *qe;
874 struct PendingMessage *pe;
878 size_t rd_ser_len = 0;
884 GNUNET_assert (NULL != h);
885 GNUNET_assert (NULL != pkey);
886 GNUNET_assert (NULL != name);
887 GNUNET_assert (NULL != rd);
889 name_len = strlen(name) + 1;
897 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
900 qe->cont_cls = cont_cls;
902 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
905 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
906 GNUNET_assert (pkey_enc != NULL);
907 key_len = ntohs (pkey_enc->len);
909 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
910 char rd_ser[rd_ser_len];
911 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
913 struct RecordCreateMessage * msg;
914 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
916 /* create msg here */
917 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
919 pe->is_init = GNUNET_NO;
920 msg = (struct RecordCreateMessage *) &pe[1];
922 pkey_tmp = (char *) &msg[1];
923 name_tmp = &pkey_tmp[key_len];
924 rd_tmp = &name_tmp[name_len];
926 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
927 msg->gns_header.header.size = htons (msg_size);
928 msg->gns_header.r_id = htonl (rid);
929 msg->name_len = htons (name_len);
930 msg->rd_count = htons (1);
931 msg->rd_len = htons (rd_ser_len);
932 msg->pkey_len = htons (key_len);
933 memcpy (pkey_tmp, pkey_enc, key_len);
934 memcpy (name_tmp, name, name_len);
935 memcpy (rd_tmp, rd_ser, rd_ser_len);
936 GNUNET_free (pkey_enc);
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
940 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
947 * Explicitly remove some content from the database. The
948 * "cont"inuation will be called with status "GNUNET_OK" if content
949 * was removed, "GNUNET_NO" if no matching entry was found and
950 * "GNUNET_SYSERR" on all other types of errors.
951 * This API is used by the authority of a zone.
953 * @param h handle to the namestore
954 * @param pkey private key of the zone
955 * @param name name that is being mapped (at most 255 characters long)
956 * @param rd record data
957 * @param cont continuation to call when done
958 * @param cont_cls closure for cont
959 * @return handle to abort the request
961 struct GNUNET_NAMESTORE_QueueEntry *
962 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
963 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
965 const struct GNUNET_NAMESTORE_RecordData *rd,
966 GNUNET_NAMESTORE_ContinuationWithStatus cont,
969 struct GNUNET_NAMESTORE_QueueEntry *qe;
970 struct PendingMessage *pe;
973 size_t rd_ser_len = 0;
978 GNUNET_assert (NULL != h);
981 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
984 qe->cont_cls = cont_cls;
988 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
989 char rd_ser[rd_ser_len];
990 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
992 struct RecordRemoveMessage * msg;
993 msg_size = sizeof (struct RecordRemoveMessage) + name_len + rd_ser_len;
995 /* create msg here */
996 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
998 pe->is_init = GNUNET_NO;
999 msg = (struct RecordRemoveMessage *) &pe[1];
1001 name_tmp = (char *) &msg[1];
1002 rd_tmp = &name_tmp[name_len];
1004 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1005 msg->gns_header.header.size = htons (msg_size);
1006 msg->gns_header.r_id = htonl (rid);
1007 //msg->signature = *signature;
1008 msg->name_len = htons (name_len);
1009 memcpy (name_tmp, name, name_len);
1010 memcpy (rd_tmp, rd_ser, rd_ser_len);
1011 GNUNET_free (rd_ser);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1015 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1022 * Get a result for a particular key from the namestore. The processor
1023 * will only be called once.
1025 * @param h handle to the namestore
1026 * @param zone zone to look up a record from
1027 * @param name name to look up
1028 * @param record_type desired record type, 0 for all
1029 * @param proc function to call on the matching records, or with
1030 * NULL (rd_count == 0) if there are no matching records
1031 * @param proc_cls closure for proc
1032 * @return a handle that can be used to
1035 struct GNUNET_NAMESTORE_QueueEntry *
1036 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1037 const GNUNET_HashCode *zone,
1039 uint32_t record_type,
1040 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1042 struct GNUNET_NAMESTORE_QueueEntry *qe;
1043 struct PendingMessage *pe;
1044 size_t msg_size = 0;
1045 size_t name_len = 0;
1048 GNUNET_assert (NULL != h);
1049 GNUNET_assert (NULL != zone);
1050 GNUNET_assert (NULL != name);
1052 name_len = strlen (name) + 1;
1053 if ((name_len == 0) || (name_len > 256))
1060 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1063 qe->proc_cls = proc_cls;
1065 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1068 msg_size = sizeof (struct LookupNameMessage) + name_len;
1069 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1071 /* create msg here */
1072 struct LookupNameMessage * msg;
1073 pe->size = msg_size;
1074 pe->is_init = GNUNET_NO;
1075 msg = (struct LookupNameMessage *) &pe[1];
1076 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1077 msg->gns_header.header.size = htons (msg_size);
1078 msg->gns_header.r_id = htonl (rid);
1079 msg->record_type = htonl (record_type);
1081 msg->name_len = htonl (name_len);
1082 memcpy (&msg[1], name, name_len);
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1086 /* transmit message */
1087 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1096 * Starts a new zone iteration (used to periodically PUT all of our
1097 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1098 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1099 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1100 * immediately, and then again after
1101 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1103 * @param h handle to the namestore
1104 * @param zone zone to access, NULL for all zones
1105 * @param must_have_flags flags that must be set for the record to be returned
1106 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1107 * @param proc function to call on each name from the zone; it
1108 * will be called repeatedly with a value (if available)
1109 * and always once at the end with a name of NULL.
1110 * @param proc_cls closure for proc
1111 * @return an iterator handle to use for iteration
1113 struct GNUNET_NAMESTORE_ZoneIterator *
1114 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1115 const GNUNET_HashCode *zone,
1116 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1117 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1118 GNUNET_NAMESTORE_RecordProcessor proc,
1121 struct GNUNET_NAMESTORE_ZoneIterator *it;
1122 struct PendingMessage *pe;
1123 size_t msg_size = 0;
1126 GNUNET_assert (NULL != h);
1127 GNUNET_assert (NULL != zone);
1130 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1133 it->proc_cls = proc;
1136 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1139 msg_size = sizeof (struct ZoneIterationStartMessage);
1140 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1142 /* create msg here */
1143 struct ZoneIterationStartMessage * msg;
1144 pe->size = msg_size;
1145 pe->is_init = GNUNET_NO;
1146 msg = (struct ZoneIterationStartMessage *) &pe[1];
1147 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1148 msg->gns_header.header.size = htons (msg_size);
1149 msg->gns_header.r_id = htonl (rid);
1151 msg->must_have_flags = ntohs (must_have_flags);
1152 msg->must_not_have_flags = ntohs (must_not_have_flags);
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1156 /* transmit message */
1157 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1165 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1166 * for the next record.
1168 * @param it the iterator
1171 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1173 struct GNUNET_NAMESTORE_Handle *h;
1174 struct PendingMessage *pe;
1175 size_t msg_size = 0;
1177 GNUNET_assert (NULL != it);
1181 msg_size = sizeof (struct ZoneIterationNextMessage);
1182 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1184 /* create msg here */
1185 struct ZoneIterationNextMessage * msg;
1186 pe->size = msg_size;
1187 pe->is_init = GNUNET_NO;
1188 msg = (struct ZoneIterationNextMessage *) &pe[1];
1189 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1190 msg->gns_header.header.size = htons (msg_size);
1191 msg->gns_header.r_id = htonl (it->op_id);
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1195 /* transmit message */
1196 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1202 * Stops iteration and releases the namestore handle for further calls.
1204 * @param it the iterator
1207 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1209 GNUNET_assert (NULL != it);
1210 struct PendingMessage *pe;
1211 size_t msg_size = 0;
1212 struct GNUNET_NAMESTORE_Handle *h = it->h;
1215 msg_size = sizeof (struct ZoneIterationStopMessage);
1216 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1218 /* create msg here */
1219 struct ZoneIterationStopMessage * msg;
1220 pe->size = msg_size;
1221 pe->is_init = GNUNET_NO;
1222 msg = (struct ZoneIterationStopMessage *) &pe[1];
1223 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1224 msg->gns_header.header.size = htons (msg_size);
1225 msg->gns_header.r_id = htonl (it->op_id);
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1229 /* transmit message */
1230 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1236 * Cancel a namestore operation. The final callback from the
1237 * operation must not have been done yet.
1239 * @param qe operation to cancel
1242 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1244 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1246 GNUNET_assert (qe != NULL);
1248 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1253 /* end of namestore_api.c */