2 This file is part of GNUnet.
3 (C) 2009-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 gnsrecord/gnsrecord_serialization.c
23 * @brief API to serialize and deserialize GNS records
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
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_gnsrecord_lib.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_tun_lib.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__)
40 GNUNET_NETWORK_STRUCT_BEGIN
44 * Internal format of a record in the serialized form.
50 * Expiration time for the DNS record; relative or absolute depends
51 * on 'flags', network byte order.
53 uint64_t expiration_time GNUNET_PACKED;
56 * Number of bytes in 'data', network byte order.
58 uint32_t data_size GNUNET_PACKED;
61 * Type of the GNS/DNS record, network byte order.
63 uint32_t record_type GNUNET_PACKED;
66 * Flags for the record, network byte order.
68 uint32_t flags GNUNET_PACKED;
72 GNUNET_NETWORK_STRUCT_END
76 * Calculate how many bytes we will need to serialize the given
79 * @param rd_count number of records in the rd array
80 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
81 * @return the required size to serialize
84 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
85 const struct GNUNET_GNSRECORD_Data *rd)
90 ret = sizeof (struct NetworkRecord) * rd_count;
91 for (i=0;i<rd_count;i++)
93 GNUNET_assert ((ret + rd[i].data_size) >= ret);
94 ret += rd[i].data_size;
101 * Serialize the given records to the given destination buffer.
103 * @param rd_count number of records in the rd array
104 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
105 * @param dest_size size of the destination array
106 * @param dest where to write the result
107 * @return the size of serialized records, -1 if records do not fit
110 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
111 const struct GNUNET_GNSRECORD_Data *rd,
115 struct NetworkRecord rec;
120 for (i=0;i<rd_count;i++)
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
124 "Serializing record %u with flags %d and expiration time %llu\n",
127 (unsigned long long) rd[i].expiration_time);
129 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
130 rec.data_size = htonl ((uint32_t) rd[i].data_size);
131 rec.record_type = htonl (rd[i].record_type);
132 rec.flags = htonl (rd[i].flags);
133 if (off + sizeof (rec) > dest_size)
135 memcpy (&dest[off], &rec, sizeof (rec));
137 if (off + rd[i].data_size > dest_size)
139 memcpy (&dest[off], rd[i].data, rd[i].data_size);
140 off += rd[i].data_size;
147 * Deserialize the given records to the given destination.
149 * @param len size of the serialized record data
150 * @param src the serialized record data
151 * @param rd_count number of records in the rd array
152 * @param dest where to put the data
153 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
156 GNUNET_GNSRECORD_records_deserialize (size_t len,
158 unsigned int rd_count,
159 struct GNUNET_GNSRECORD_Data *dest)
161 struct NetworkRecord rec;
166 for (i=0;i<rd_count;i++)
168 if (off + sizeof (rec) > len)
169 return GNUNET_SYSERR;
170 memcpy (&rec, &src[off], sizeof (rec));
171 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
172 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
173 dest[i].record_type = ntohl (rec.record_type);
174 dest[i].flags = ntohl (rec.flags);
176 if (off + dest[i].data_size > len)
177 return GNUNET_SYSERR;
178 dest[i].data = &src[off];
179 off += dest[i].data_size;
181 LOG (GNUNET_ERROR_TYPE_DEBUG,
182 "Deserialized record %u with flags %d and expiration time %llu\n",
185 (unsigned long long) dest[i].expiration_time);
192 /* end of gnsrecord_serialization.c */