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_signatures.h"
34 #include "gnunet_namestore_service.h"
35 #include "namestore.h"
37 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
39 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
44 struct GNUNET_NAMESTORE_QueueEntry
46 struct GNUNET_NAMESTORE_QueueEntry *next;
47 struct GNUNET_NAMESTORE_QueueEntry *prev;
49 struct GNUNET_NAMESTORE_Handle *nsh;
53 GNUNET_NAMESTORE_ContinuationWithStatus cont;
56 GNUNET_NAMESTORE_RecordProcessor proc;
59 char *data; /*stub data pointer*/
66 struct GNUNET_NAMESTORE_ZoneIterator
68 struct GNUNET_NAMESTORE_ZoneIterator *next;
69 struct GNUNET_NAMESTORE_ZoneIterator *prev;
73 struct GNUNET_NAMESTORE_Handle *h;
74 GNUNET_NAMESTORE_RecordProcessor proc;
83 * Message in linked list we should send to the service. The
84 * actual binary message follows this struct.
92 struct PendingMessage *next;
97 struct PendingMessage *prev;
100 * Size of the message.
105 * Is this the 'START' message?
112 * Connection to the NAMESTORE service.
114 struct GNUNET_NAMESTORE_Handle
118 * Configuration to use.
120 const struct GNUNET_CONFIGURATION_Handle *cfg;
123 * Socket (if available).
125 struct GNUNET_CLIENT_Connection *client;
128 * Currently pending transmission request (or NULL).
130 struct GNUNET_CLIENT_TransmitHandle *th;
135 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
138 * Pending messages to send to the service
141 struct PendingMessage * pending_head;
142 struct PendingMessage * pending_tail;
145 * Should we reconnect to service due to some serious error?
151 * Pending namestore queue entries
153 struct GNUNET_NAMESTORE_QueueEntry * op_head;
154 struct GNUNET_NAMESTORE_QueueEntry * op_tail;
159 * Pending namestore zone iterator entries
161 struct GNUNET_NAMESTORE_ZoneIterator * z_head;
162 struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
165 struct GNUNET_NAMESTORE_SimpleRecord
170 struct GNUNET_NAMESTORE_SimpleRecord *next;
175 struct GNUNET_NAMESTORE_SimpleRecord *prev;
178 const GNUNET_HashCode *zone;
179 uint32_t record_type;
180 struct GNUNET_TIME_Absolute expiration;
181 enum GNUNET_NAMESTORE_RecordFlags flags;
188 * Disconnect from service and then reconnect.
190 * @param h our handle
193 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
196 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
197 struct LookupNameResponseMessage * msg,
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
201 "LOOKUP_NAME_RESPONSE");
203 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
204 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
208 struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
209 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
210 struct GNUNET_TIME_Absolute expire;
215 int contains_sig = GNUNET_NO;
218 rd_len = ntohs (msg->rd_len);
219 rd_count = ntohs (msg->rd_count);
220 msg_len = ntohs (msg->gns_header.header.size);
221 name_len = ntohs (msg->name_len);
222 contains_sig = ntohs (msg->contains_sig);
223 expire = GNUNET_TIME_absolute_ntoh(msg->expire);
225 exp_msg_len = sizeof (struct LookupNameResponseMessage) +
226 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
229 contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
231 if (msg_len != exp_msg_len)
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
234 msg_len, exp_msg_len);
239 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
240 name = (char *) &zone_key[1];
241 rd_tmp = &name[name_len];
243 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
244 GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
246 /* reset values if values not contained */
247 if (contains_sig == GNUNET_NO)
250 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_len];
254 memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
255 if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
258 if (qe->proc != NULL)
260 qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
263 /* Operation done, remove */
264 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
270 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
271 struct RecordPutResponseMessage* msg,
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
275 "RECORD_PUT_RESPONSE");
277 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
280 if (ntohs (msg->op_result) == GNUNET_OK)
283 if (qe->cont != NULL)
285 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
289 else if (ntohs (msg->op_result) == GNUNET_NO)
292 if (qe->cont != NULL)
294 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
303 /* Operation done, remove */
304 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
311 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
312 struct RecordCreateResponseMessage* msg,
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
316 "RECORD_CREATE_RESPONSE");
318 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
321 if (ntohs (msg->op_result) == GNUNET_OK)
324 if (qe->cont != NULL)
326 qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
330 else if (ntohs (msg->op_result) == GNUNET_NO)
333 if (qe->cont != NULL)
335 qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
344 /* Operation done, remove */
345 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
352 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
353 struct RecordRemoveResponseMessage* msg,
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
357 "RECORD_REMOVE_RESPONSE");
359 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
360 int res = ntohs (msg->op_result);
365 * 1 : No records for entry
366 * 2 : Could not find record to remove
367 * 3 : Failed to create new signature
368 * 4 : Failed to put new set of records in database
372 if (qe->cont != NULL)
374 qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
379 if (qe->cont != NULL)
381 qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
386 if (qe->cont != NULL)
388 qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
393 if (qe->cont != NULL)
395 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
400 if (qe->cont != NULL)
402 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
410 /* Operation done, remove */
411 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
418 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
419 const struct GNUNET_MessageHeader *msg,
420 int type, size_t size)
423 /* handle different message type */
425 case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
426 if (size < sizeof (struct LookupNameResponseMessage))
431 handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
433 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
434 if (size != sizeof (struct RecordPutResponseMessage))
439 handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
441 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
442 if (size != sizeof (struct RecordCreateResponseMessage))
447 handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
449 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
450 if (size != sizeof (struct RecordRemoveResponseMessage))
455 handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
464 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
465 struct ZoneIterationResponseMessage *msg,
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
469 "ZONE_ITERATION_RESPONSE");
472 if (ze->proc != NULL)
475 ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
481 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
482 const struct GNUNET_MessageHeader *msg,
483 int type, size_t size)
486 /* handle different message type */
488 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
489 if (size < sizeof (struct ZoneIterationResponseMessage))
494 handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
503 * Type of a function to call when we receive a message
506 * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
507 * @param msg message received, NULL on timeout or fatal error
510 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
512 struct GNUNET_NAMESTORE_Handle *h = cls;
513 struct GNUNET_NAMESTORE_Header * gm;
514 struct GNUNET_NAMESTORE_QueueEntry *qe;
515 struct GNUNET_NAMESTORE_ZoneIterator *ze;
518 uint32_t r_id = UINT32_MAX;
526 size = ntohs (msg->size);
527 type = ntohs (msg->type);
529 if (size < sizeof (struct GNUNET_NAMESTORE_Header))
532 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
533 GNUNET_TIME_UNIT_FOREVER_REL);
537 gm = (struct GNUNET_NAMESTORE_Header *) msg;
538 r_id = ntohl (gm->r_id);
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
542 /* Find matching operation */
545 /* No matching pending operation found */
547 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
548 GNUNET_TIME_UNIT_FOREVER_REL);
552 /* Is it a record related operation ? */
553 for (qe = h->op_head; qe != NULL; qe = qe->next)
555 if (qe->op_id == r_id)
560 manage_record_operations (qe, msg, type, size);
563 /* Is it a zone iteration operation ? */
564 for (ze = h->z_head; ze != NULL; ze = ze->next)
566 if (ze->op_id == r_id)
571 manage_zone_operations (ze, msg, type, size);
574 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
575 GNUNET_TIME_UNIT_FOREVER_REL);
577 if (GNUNET_YES == h->reconnect)
584 * Transmit messages from the message queue to the service
585 * (if there are any, and if we are not already trying).
587 * @param h handle to use
590 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
594 * We can now transmit a message to NAMESTORE. Do it.
596 * @param cls the 'struct GNUNET_NAMESTORE_Handle'
597 * @param size number of bytes we can transmit
598 * @param buf where to copy the messages
599 * @return number of bytes copied into buf
602 transmit_message_to_namestore (void *cls, size_t size, void *buf)
604 struct GNUNET_NAMESTORE_Handle *h = cls;
605 struct PendingMessage *p;
610 if ((size == 0) || (buf == NULL))
617 while ((NULL != (p = h->pending_head)) && (p->size <= size))
619 memcpy (&cbuf[ret], &p[1], p->size);
622 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
623 if (GNUNET_YES == p->is_init)
624 GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
625 GNUNET_TIME_UNIT_FOREVER_REL);
634 * Transmit messages from the message queue to the service
635 * (if there are any, and if we are not already trying).
637 * @param h handle to use
640 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
642 struct PendingMessage *p;
646 if (NULL == (p = h->pending_head))
648 if (NULL == h->client)
649 return; /* currently reconnecting */
651 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
652 GNUNET_TIME_UNIT_FOREVER_REL,
653 GNUNET_NO, &transmit_message_to_namestore,
659 * Reconnect to namestore service.
661 * @param h the handle to the namestore service
664 reconnect (struct GNUNET_NAMESTORE_Handle *h)
666 struct PendingMessage *p;
667 struct StartMessage *init;
669 GNUNET_assert (NULL == h->client);
670 h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
671 GNUNET_assert (NULL != h->client);
673 if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
675 p = GNUNET_malloc (sizeof (struct PendingMessage) +
676 sizeof (struct StartMessage));
677 p->size = sizeof (struct StartMessage);
678 p->is_init = GNUNET_YES;
679 init = (struct StartMessage *) &p[1];
680 init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
681 init->header.size = htons (sizeof (struct StartMessage));
682 GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
688 * Re-establish the connection to the service.
690 * @param cls handle to use to re-connect.
691 * @param tc scheduler context
694 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
696 struct GNUNET_NAMESTORE_Handle *h = cls;
698 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
704 * Disconnect from service and then reconnect.
706 * @param h our handle
709 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
711 h->reconnect = GNUNET_NO;
712 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
714 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
720 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
722 uint32_t op_id = h->op_id;
728 * Initialize the connection with the NAMESTORE service.
730 * @param cfg configuration to use
731 * @return handle to the GNS service, or NULL on error
733 struct GNUNET_NAMESTORE_Handle *
734 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
736 struct GNUNET_NAMESTORE_Handle *h;
738 h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
740 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
747 * Disconnect from the namestore service (and free associated
750 * @param h handle to the namestore
751 * @param drop set to GNUNET_YES to delete all data in namestore (!)
754 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
756 struct PendingMessage *p;
757 struct GNUNET_NAMESTORE_QueueEntry *q;
758 struct GNUNET_NAMESTORE_ZoneIterator *z;
760 GNUNET_assert (h != NULL);
762 while (NULL != (p = h->pending_head))
764 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
768 while (NULL != (q = h->op_head))
770 GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
774 while (NULL != (z = h->z_head))
776 GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
780 if (NULL != h->client)
782 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
785 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
787 GNUNET_SCHEDULER_cancel (h->reconnect_task);
788 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
796 * Store an item in the namestore. If the item is already present,
797 * the expiration time is updated to the max of the existing time and
798 * the new time. This API is used when we cache signatures from other
801 * @param h handle to the namestore
802 * @param zone_key public key of the zone
803 * @param name name that is being mapped (at most 255 characters long)
804 * @param expire when does the corresponding block in the DHT expire (until
805 * when should we never do a DHT lookup for the same name again)?
806 * @param rd_count number of entries in 'rd' array
807 * @param rd array of records with data to store
808 * @param signature signature for all the records in the zone under the given name
809 * @param cont continuation to call when done
810 * @param cont_cls closure for cont
811 * @return handle to abort the request
813 struct GNUNET_NAMESTORE_QueueEntry *
814 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
815 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
817 struct GNUNET_TIME_Absolute expire,
818 unsigned int rd_count,
819 const struct GNUNET_NAMESTORE_RecordData *rd,
820 const struct GNUNET_CRYPTO_RsaSignature *signature,
821 GNUNET_NAMESTORE_ContinuationWithStatus cont,
824 struct GNUNET_NAMESTORE_QueueEntry *qe;
825 struct PendingMessage *pe;
827 /* pointer to elements */
834 size_t rd_ser_len = 0;
835 size_t pubkey_len = 0;
838 GNUNET_assert (NULL != h);
839 GNUNET_assert (NULL != zone_key);
840 GNUNET_assert (NULL != name);
841 GNUNET_assert (NULL != rd);
842 GNUNET_assert (NULL != signature);
844 name_len = strlen(name) + 1;
852 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
855 qe->cont_cls = cont_cls;
857 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
860 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
861 char rd_ser[rd_ser_len];
862 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
864 pubkey_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
865 struct RecordPutMessage * msg;
866 msg_size = sizeof (struct RecordPutMessage) + pubkey_len + name_len + rd_ser_len;
868 /* create msg here */
869 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
871 pe->is_init = GNUNET_NO;
872 msg = (struct RecordPutMessage *) &pe[1];
873 zone_key_tmp = (char *) &msg[1];
874 name_tmp = (char *) &zone_key_tmp[pubkey_len];
875 rd_tmp = &name_tmp[name_len];
877 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
878 msg->gns_header.header.size = htons (msg_size);
879 msg->gns_header.r_id = htonl (rid);
880 msg->key_len = htons (pubkey_len);
881 memcpy (zone_key_tmp, zone_key, pubkey_len);
882 msg->signature = *signature;
883 msg->name_len = htons (name_len);
884 memcpy (name_tmp, name, name_len);
885 msg->expire = GNUNET_TIME_absolute_hton (expire);
886 msg->rd_len = htons (rd_ser_len);
887 msg->rd_count = htons (rd_count);
889 memcpy (rd_tmp, rd_ser, rd_ser_len);
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
893 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
901 * Check if a signature is valid. This API is used by the GNS Block
902 * to validate signatures received from the network.
904 * @param public_key public key of the zone
905 * @param name name that is being mapped (at most 255 characters long)
906 * @param rd_count number of entries in 'rd' array
907 * @param rd array of records with data to store
908 * @param signature signature for all the records in the zone under the given name
909 * @return GNUNET_OK if the signature is valid
912 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
914 unsigned int rd_count,
915 const struct GNUNET_NAMESTORE_RecordData *rd,
916 const struct GNUNET_CRYPTO_RsaSignature *signature)
918 int res = GNUNET_SYSERR;
919 size_t rd_ser_len = 0;
923 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
925 GNUNET_assert (public_key != NULL);
926 GNUNET_assert (name != NULL);
927 GNUNET_assert (signature != NULL);
929 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
930 char rd_ser[rd_ser_len];
931 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
933 name_len = strlen (name) + 1;
935 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
936 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
937 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
938 name_tmp = (char *) &sig_purpose[1];
939 rd_tmp = &name_tmp[name_len];
940 memcpy (name_tmp, name, name_len);
941 memcpy (rd_tmp, rd_ser, rd_ser_len);
943 res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
945 GNUNET_free (sig_purpose);
951 * Store an item in the namestore. If the item is already present,
952 * the expiration time is updated to the max of the existing time and
953 * the new time. This API is used by the authority of a zone.
955 * @param h handle to the namestore
956 * @param pkey private key of the zone
957 * @param name name that is being mapped (at most 255 characters long)
958 * @param rd record data to store
959 * @param cont continuation to call when done
960 * @param cont_cls closure for cont
961 * @return handle to abort the request
963 struct GNUNET_NAMESTORE_QueueEntry *
964 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
965 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
967 const struct GNUNET_NAMESTORE_RecordData *rd,
968 GNUNET_NAMESTORE_ContinuationWithStatus cont,
971 struct GNUNET_NAMESTORE_QueueEntry *qe;
972 struct PendingMessage *pe;
976 size_t rd_ser_len = 0;
982 GNUNET_assert (NULL != h);
983 GNUNET_assert (NULL != pkey);
984 GNUNET_assert (NULL != name);
985 GNUNET_assert (NULL != rd);
987 name_len = strlen(name) + 1;
995 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
998 qe->cont_cls = cont_cls;
1000 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1003 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1004 GNUNET_assert (pkey_enc != NULL);
1005 key_len = ntohs (pkey_enc->len);
1007 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1008 char rd_ser[rd_ser_len];
1009 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1011 struct RecordCreateMessage * msg;
1012 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1014 /* create msg here */
1015 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1016 pe->size = msg_size;
1017 pe->is_init = GNUNET_NO;
1018 msg = (struct RecordCreateMessage *) &pe[1];
1020 pkey_tmp = (char *) &msg[1];
1021 name_tmp = &pkey_tmp[key_len];
1022 rd_tmp = &name_tmp[name_len];
1024 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1025 msg->gns_header.header.size = htons (msg_size);
1026 msg->gns_header.r_id = htonl (rid);
1027 msg->name_len = htons (name_len);
1028 msg->rd_count = htons (1);
1029 msg->rd_len = htons (rd_ser_len);
1030 msg->pkey_len = htons (key_len);
1031 memcpy (pkey_tmp, pkey_enc, key_len);
1032 memcpy (name_tmp, name, name_len);
1033 memcpy (rd_tmp, rd_ser, rd_ser_len);
1034 GNUNET_free (pkey_enc);
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1038 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1045 * Explicitly remove some content from the database. The
1046 * "cont"inuation will be called with status "GNUNET_OK" if content
1047 * was removed, "GNUNET_NO" if no matching entry was found and
1048 * "GNUNET_SYSERR" on all other types of errors.
1049 * This API is used by the authority of a zone.
1051 * @param h handle to the namestore
1052 * @param pkey private key of the zone
1053 * @param name name that is being mapped (at most 255 characters long)
1054 * @param rd record data
1055 * @param cont continuation to call when done
1056 * @param cont_cls closure for cont
1057 * @return handle to abort the request
1059 struct GNUNET_NAMESTORE_QueueEntry *
1060 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1061 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1063 const struct GNUNET_NAMESTORE_RecordData *rd,
1064 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1067 struct GNUNET_NAMESTORE_QueueEntry *qe;
1068 struct PendingMessage *pe;
1072 size_t rd_ser_len = 0;
1073 size_t msg_size = 0;
1074 size_t name_len = 0;
1078 GNUNET_assert (NULL != h);
1081 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1084 qe->cont_cls = cont_cls;
1086 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1089 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1090 GNUNET_assert (pkey_enc != NULL);
1091 key_len = ntohs (pkey_enc->len);
1093 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1094 char rd_ser[rd_ser_len];
1095 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1097 name_len = strlen (name) + 1;
1099 struct RecordRemoveMessage * msg;
1100 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1102 /* create msg here */
1103 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1104 pe->size = msg_size;
1105 pe->is_init = GNUNET_NO;
1106 msg = (struct RecordRemoveMessage *) &pe[1];
1108 pkey_tmp = (char *) &msg[1];
1109 name_tmp = &pkey_tmp[key_len];
1110 rd_tmp = &name_tmp[name_len];
1112 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1113 msg->gns_header.header.size = htons (msg_size);
1114 msg->gns_header.r_id = htonl (rid);
1115 msg->name_len = htons (name_len);
1116 msg->rd_len = htons (rd_ser_len);
1117 msg->rd_count = htons (1);
1118 msg->key_len = htons (key_len);
1119 memcpy (pkey_tmp, pkey_enc, key_len);
1120 memcpy (name_tmp, name, name_len);
1121 memcpy (rd_tmp, rd_ser, rd_ser_len);
1123 GNUNET_free (pkey_enc);
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1127 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1134 * Get a result for a particular key from the namestore. The processor
1135 * will only be called once.
1137 * @param h handle to the namestore
1138 * @param zone zone to look up a record from
1139 * @param name name to look up
1140 * @param record_type desired record type, 0 for all
1141 * @param proc function to call on the matching records, or with
1142 * NULL (rd_count == 0) if there are no matching records
1143 * @param proc_cls closure for proc
1144 * @return a handle that can be used to
1147 struct GNUNET_NAMESTORE_QueueEntry *
1148 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h,
1149 const GNUNET_HashCode *zone,
1151 uint32_t record_type,
1152 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1154 struct GNUNET_NAMESTORE_QueueEntry *qe;
1155 struct PendingMessage *pe;
1156 size_t msg_size = 0;
1157 size_t name_len = 0;
1160 GNUNET_assert (NULL != h);
1161 GNUNET_assert (NULL != zone);
1162 GNUNET_assert (NULL != name);
1164 name_len = strlen (name) + 1;
1165 if ((name_len == 0) || (name_len > 256))
1172 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1175 qe->proc_cls = proc_cls;
1177 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1180 msg_size = sizeof (struct LookupNameMessage) + name_len;
1181 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1183 /* create msg here */
1184 struct LookupNameMessage * msg;
1185 pe->size = msg_size;
1186 pe->is_init = GNUNET_NO;
1187 msg = (struct LookupNameMessage *) &pe[1];
1188 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1189 msg->gns_header.header.size = htons (msg_size);
1190 msg->gns_header.r_id = htonl (rid);
1191 msg->record_type = htonl (record_type);
1193 msg->name_len = htonl (name_len);
1194 memcpy (&msg[1], name, name_len);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1198 /* transmit message */
1199 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1208 * Starts a new zone iteration (used to periodically PUT all of our
1209 * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1210 * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1211 * GNUNET_NAMESTORE_zone_iteration_stop. "proc" will be called once
1212 * immediately, and then again after
1213 * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1215 * @param h handle to the namestore
1216 * @param zone zone to access, NULL for all zones
1217 * @param must_have_flags flags that must be set for the record to be returned
1218 * @param must_not_have_flags flags that must NOT be set for the record to be returned
1219 * @param proc function to call on each name from the zone; it
1220 * will be called repeatedly with a value (if available)
1221 * and always once at the end with a name of NULL.
1222 * @param proc_cls closure for proc
1223 * @return an iterator handle to use for iteration
1225 struct GNUNET_NAMESTORE_ZoneIterator *
1226 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1227 const GNUNET_HashCode *zone,
1228 enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1229 enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1230 GNUNET_NAMESTORE_RecordProcessor proc,
1233 struct GNUNET_NAMESTORE_ZoneIterator *it;
1234 struct PendingMessage *pe;
1235 size_t msg_size = 0;
1238 GNUNET_assert (NULL != h);
1239 GNUNET_assert (NULL != zone);
1242 it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1245 it->proc_cls = proc;
1248 GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1251 msg_size = sizeof (struct ZoneIterationStartMessage);
1252 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1254 /* create msg here */
1255 struct ZoneIterationStartMessage * msg;
1256 pe->size = msg_size;
1257 pe->is_init = GNUNET_NO;
1258 msg = (struct ZoneIterationStartMessage *) &pe[1];
1259 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1260 msg->gns_header.header.size = htons (msg_size);
1261 msg->gns_header.r_id = htonl (rid);
1263 msg->must_have_flags = ntohs (must_have_flags);
1264 msg->must_not_have_flags = ntohs (must_not_have_flags);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1268 /* transmit message */
1269 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1277 * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1278 * for the next record.
1280 * @param it the iterator
1283 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1285 struct GNUNET_NAMESTORE_Handle *h;
1286 struct PendingMessage *pe;
1287 size_t msg_size = 0;
1289 GNUNET_assert (NULL != it);
1293 msg_size = sizeof (struct ZoneIterationNextMessage);
1294 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1296 /* create msg here */
1297 struct ZoneIterationNextMessage * msg;
1298 pe->size = msg_size;
1299 pe->is_init = GNUNET_NO;
1300 msg = (struct ZoneIterationNextMessage *) &pe[1];
1301 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1302 msg->gns_header.header.size = htons (msg_size);
1303 msg->gns_header.r_id = htonl (it->op_id);
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1307 /* transmit message */
1308 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1314 * Stops iteration and releases the namestore handle for further calls.
1316 * @param it the iterator
1319 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1321 GNUNET_assert (NULL != it);
1322 struct PendingMessage *pe;
1323 size_t msg_size = 0;
1324 struct GNUNET_NAMESTORE_Handle *h = it->h;
1327 msg_size = sizeof (struct ZoneIterationStopMessage);
1328 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1330 /* create msg here */
1331 struct ZoneIterationStopMessage * msg;
1332 pe->size = msg_size;
1333 pe->is_init = GNUNET_NO;
1334 msg = (struct ZoneIterationStopMessage *) &pe[1];
1335 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1336 msg->gns_header.header.size = htons (msg_size);
1337 msg->gns_header.r_id = htonl (it->op_id);
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1341 /* transmit message */
1342 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1348 * Cancel a namestore operation. The final callback from the
1349 * operation must not have been done yet.
1351 * @param qe operation to cancel
1354 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1356 struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1358 GNUNET_assert (qe != NULL);
1360 GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1365 /* end of namestore_api.c */