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_cound 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;
136 * Deserialize the given records to the given destination.
138 * @param len size of the serialized record data
139 * @param src the serialized record data
140 * @param rd_cound number of records in the rd array
141 * @param dest where to put the data
143 * @return GNUNET_OK on success, GNUNET_SYSERR on error
146 GNUNET_NAMESTORE_records_deserialize (size_t len,
148 unsigned int rd_count,
149 struct GNUNET_NAMESTORE_RecordData *dest)
151 struct NetworkRecord rec;
156 for (i=0;i<rd_count;i++)
158 if (off + sizeof (rec) > len)
159 return GNUNET_SYSERR;
160 memcpy (&rec, &src[off], sizeof (rec));
161 dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
162 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
163 dest[i].record_type = ntohl (rec.record_type);
164 dest[i].flags = ntohl (rec.flags);
167 if (off + sizeof (dest[i].data_size) > len)
168 return GNUNET_SYSERR;
169 dest[i].data = &src[off];
170 off += dest[i].data_size;
175 struct GNUNET_CRYPTO_RsaSignature *
176 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key, const char *name, struct GNUNET_NAMESTORE_RecordData *rd, unsigned int rd_count)
178 struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
179 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
192 name_len = strlen (name) + 1;
194 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
195 char rd_ser[rd_ser_len];
196 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
198 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
200 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
201 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
202 name_tmp = (char *) &sig_purpose[1];
203 rd_tmp = &name_tmp[name_len];
204 memcpy (name_tmp, name, name_len);
205 memcpy (rd_tmp, rd_ser, rd_ser_len);
207 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
209 GNUNET_free (sig_purpose);
211 if (GNUNET_OK != res)
220 /* end of namestore_api.c */