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_common.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_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "namestore.h"
35 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
37 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41 * Internal format of a record in the serialized form.
47 * Expiration time for the DNS record.
49 struct GNUNET_TIME_AbsoluteNBO expiration;
52 * Number of bytes in 'data', network byte order.
57 * Type of the GNS/DNS record, network byte order.
62 * Flags for the record, network byte order.
69 * Calculate how many bytes we will need to serialize the given
72 * @param rd_count number of records in the rd array
73 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
75 * @return the required size to serialize
79 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
80 const struct GNUNET_NAMESTORE_RecordData *rd)
85 ret = sizeof (struct NetworkRecord) * rd_count;
86 for (i=0;i<rd_count;i++)
88 GNUNET_assert (ret + rd[i].data_size >= ret);
89 ret += rd[i].data_size;
96 * Serialize the given records to the given destination buffer.
98 * @param rd_count number of records in the rd array
99 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
100 * @param dest_size size of the destination array
101 * @param dest where to write the result
103 * @return the size of serialized records
106 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
107 const struct GNUNET_NAMESTORE_RecordData *rd,
111 struct NetworkRecord rec;
116 for (i=0;i<rd_count;i++)
118 rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
119 rec.data_size = htonl ((uint32_t) rd[i].data_size);
120 rec.record_type = htonl (rd[i].record_type);
121 rec.flags = htonl (rd[i].flags);
122 if (off + sizeof (rec) > dest_size)
124 memcpy (&dest[off], &rec, sizeof (rec));
126 if (off + rd[i].data_size > dest_size)
128 memcpy (&dest[off], rd[i].data, rd[i].data_size);
129 off += rd[i].data_size;
135 * Compares if two records are equal
140 * @return GNUNET_YES or GNUNET_NO
143 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
144 const struct GNUNET_NAMESTORE_RecordData *b)
146 if ((a->record_type == b->record_type) &&
147 (a->expiration.abs_value == b->expiration.abs_value) &&
148 (a->data_size == b->data_size) &&
149 (0 == memcmp (a->data, b->data, a->data_size)))
157 * Deserialize the given records to the given destination.
159 * @param len size of the serialized record data
160 * @param src the serialized record data
161 * @param rd_count number of records in the rd array
162 * @param dest where to put the data
164 * @return GNUNET_OK on success, GNUNET_SYSERR on error
167 GNUNET_NAMESTORE_records_deserialize (size_t len,
169 unsigned int rd_count,
170 struct GNUNET_NAMESTORE_RecordData *dest)
172 struct NetworkRecord rec;
177 for (i=0;i<rd_count;i++)
179 if (off + sizeof (rec) > len)
180 return GNUNET_SYSERR;
181 memcpy (&rec, &src[off], sizeof (rec));
182 dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
183 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
184 dest[i].record_type = ntohl (rec.record_type);
185 dest[i].flags = ntohl (rec.flags);
188 if (off + dest[i].data_size > len)
189 return GNUNET_SYSERR;
190 dest[i].data = &src[off];
191 off += dest[i].data_size;
197 * Sign name and records
199 * @param key the private key
200 * @param name the name
201 * @param rd record data
202 * @param rd_count number of records
204 * @return the signature
206 struct GNUNET_CRYPTO_RsaSignature *
207 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
209 struct GNUNET_NAMESTORE_RecordData *rd,
210 unsigned int rd_count)
212 struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
213 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
226 name_len = strlen (name) + 1;
228 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
229 char rd_ser[rd_ser_len];
230 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
232 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
234 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
235 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
236 name_tmp = (char *) &sig_purpose[1];
237 rd_tmp = &name_tmp[name_len];
238 memcpy (name_tmp, name, name_len);
239 memcpy (rd_tmp, rd_ser, rd_ser_len);
241 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
243 GNUNET_free (sig_purpose);
245 if (GNUNET_OK != res)
254 /* end of namestore_api.c */