2 This file is part of GNUnet
3 Copyright (C) 2012-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file peerstore/peerstore_common.c
22 * @brief Helper peerstore functions
23 * @author Omar Tarabai
26 #include "peerstore_common.h"
29 * Creates a hash of the given key combination
33 PEERSTORE_hash_key (const char *sub_system,
34 const struct GNUNET_PeerIdentity *peer, const char *key,
35 struct GNUNET_HashCode *ret)
44 sssize = strlen (sub_system) + 1;
45 psize = sizeof (struct GNUNET_PeerIdentity);
46 ksize = strlen (key) + 1;
47 totalsize = sssize + psize + ksize;
48 block = GNUNET_malloc (totalsize);
50 memcpy (blockptr, sub_system, sssize);
52 memcpy (blockptr, peer, psize);
54 memcpy (blockptr, key, ksize);
55 GNUNET_CRYPTO_hash (block, totalsize, ret);
61 * Creates a record message ready to be sent
63 * @param sub_system sub system string
64 * @param peer Peer identity (can be NULL)
65 * @param key record key string (can be NULL)
66 * @param value record value BLOB (can be NULL)
67 * @param value_size record value size in bytes (set to 0 if value is NULL)
68 * @param expiry absolute time after which the record expires
69 * @param msg_type message type to be set in header
70 * @return pointer to record message struct
72 struct StoreRecordMessage *
73 PEERSTORE_create_record_message (const char *sub_system,
74 const struct GNUNET_PeerIdentity *peer,
75 const char *key, const void *value,
77 struct GNUNET_TIME_Absolute *expiry,
80 struct StoreRecordMessage *srm;
86 ss_size = strlen (sub_system) + 1;
90 key_size = strlen (key) + 1;
92 sizeof (struct StoreRecordMessage) + ss_size + key_size + value_size;
93 srm = GNUNET_malloc (request_size);
94 srm->header.size = htons (request_size);
95 srm->header.type = htons (msg_type);
96 srm->key_size = htons (key_size);
98 srm->expiry = *expiry;
100 srm->peer_set = htons (GNUNET_NO);
103 srm->peer_set = htons (GNUNET_YES);
106 srm->sub_system_size = htons (ss_size);
107 srm->value_size = htons (value_size);
109 memcpy (dummy, sub_system, ss_size);
111 memcpy (dummy, key, key_size);
113 memcpy (dummy, value, value_size);
119 * Creates a MQ envelope for a single record
121 * @param sub_system sub system string
122 * @param peer Peer identity (can be NULL)
123 * @param key record key string (can be NULL)
124 * @param value record value BLOB (can be NULL)
125 * @param value_size record value size in bytes (set to 0 if value is NULL)
126 * @param expiry time after which the record expires
127 * @param options options specific to the storage operation
128 * @param msg_type message type to be set in header
129 * @return pointer to record message struct
131 struct GNUNET_MQ_Envelope *
132 PEERSTORE_create_record_mq_envelope (const char *sub_system,
133 const struct GNUNET_PeerIdentity *peer,
134 const char *key, const void *value,
136 struct GNUNET_TIME_Absolute *expiry,
137 enum GNUNET_PEERSTORE_StoreOption options,
140 struct StoreRecordMessage *srm;
141 struct GNUNET_MQ_Envelope *ev;
147 GNUNET_assert (NULL != sub_system);
148 ss_size = strlen (sub_system) + 1;
152 key_size = strlen (key) + 1;
153 msg_size = ss_size + key_size + value_size;
154 ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type);
155 srm->key_size = htons (key_size);
157 srm->expiry = *expiry;
159 srm->peer_set = htons (GNUNET_NO);
162 srm->peer_set = htons (GNUNET_YES);
165 srm->sub_system_size = htons (ss_size);
166 srm->value_size = htons (value_size);
167 srm->options = htonl (options);
169 memcpy (dummy, sub_system, ss_size);
171 memcpy (dummy, key, key_size);
173 memcpy (dummy, value, value_size);
179 * Parses a message carrying a record
181 * @param message the actual message
182 * @return Pointer to record or NULL if error
184 struct GNUNET_PEERSTORE_Record *
185 PEERSTORE_parse_record_message (const struct GNUNET_MessageHeader *message)
187 struct StoreRecordMessage *srm;
188 struct GNUNET_PEERSTORE_Record *record;
195 req_size = ntohs (message->size);
196 if (req_size < sizeof (struct StoreRecordMessage))
198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
199 "Received message with invalid size: (%d < %d).\n", req_size,
200 sizeof (struct StoreRecordMessage));
203 srm = (struct StoreRecordMessage *) message;
204 ss_size = ntohs (srm->sub_system_size);
205 key_size = ntohs (srm->key_size);
206 value_size = ntohs (srm->value_size);
207 if (ss_size + key_size + value_size + sizeof (struct StoreRecordMessage) !=
210 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
211 "Received message with invalid sizes: (%d + %d + %d + %d != %d).\n",
212 ss_size, key_size, value_size,
213 sizeof (struct StoreRecordMessage), req_size);
216 record = GNUNET_new (struct GNUNET_PEERSTORE_Record);
217 if (GNUNET_YES == ntohs (srm->peer_set))
219 record->peer = GNUNET_new (struct GNUNET_PeerIdentity);
221 memcpy (record->peer, &srm->peer, sizeof (struct GNUNET_PeerIdentity));
223 record->expiry = GNUNET_new (struct GNUNET_TIME_Absolute);
225 *(record->expiry) = srm->expiry;
226 dummy = (char *) &srm[1];
229 record->sub_system = GNUNET_strdup (dummy);
234 record->key = GNUNET_strdup (dummy);
239 record->value = GNUNET_malloc (value_size);
240 memcpy (record->value, dummy, value_size);
242 record->value_size = value_size;
248 * Free any memory allocated for this record
253 PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record)
255 if (NULL != record->sub_system)
256 GNUNET_free (record->sub_system);
257 if (NULL != record->peer)
258 GNUNET_free (record->peer);
259 if (NULL != record->key)
260 GNUNET_free (record->key);
261 if (NULL != record->value)
263 GNUNET_free (record->value);
266 if (NULL != record->expiry)
267 GNUNET_free (record->expiry);
268 GNUNET_free (record);