2 This file is part of GNUnet
3 (C) 2012-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.
21 * @file peerstore/peerstore_common.c
22 * @brief Helper peerstore functions
23 * @author Omar Tarabai
26 #include "peerstore_common.h"
29 * Creates a record message ready to be sent
31 * @param sub_system sub system string
32 * @param peer Peer identity (can be NULL)
33 * @param key record key string (can be NULL)
34 * @param value record value BLOB (can be NULL)
35 * @param value_size record value size in bytes (set to 0 if value is NULL)
36 * @param expiry absolute time after which the record expires
37 * @param msg_type message type to be set in header
38 * @return pointer to record message struct
40 struct StoreRecordMessage *
41 PEERSTORE_create_record_message(const char *sub_system,
42 const struct GNUNET_PeerIdentity *peer,
46 struct GNUNET_TIME_Absolute *expiry,
49 struct StoreRecordMessage *srm;
55 ss_size = strlen(sub_system) + 1;
59 key_size = strlen(key) + 1;
60 request_size = sizeof(struct StoreRecordMessage) +
64 srm = GNUNET_malloc(request_size);
65 srm->header.size = htons(request_size);
66 srm->header.type = htons(msg_type);
67 srm->key_size = htons(key_size);
69 srm->expiry = *expiry;
71 srm->peer_set = htons(GNUNET_NO);
74 srm->peer_set = htons(GNUNET_YES);
77 srm->sub_system_size = htons(ss_size);
78 srm->value_size = htons(value_size);
80 memcpy(dummy, sub_system, ss_size);
82 memcpy(dummy, key, key_size);
84 memcpy(dummy, value, value_size);
90 * Creates a MQ envelope for a single record
92 * @param sub_system sub system string
93 * @param peer Peer identity (can be NULL)
94 * @param key record key string (can be NULL)
95 * @param value record value BLOB (can be NULL)
96 * @param value_size record value size in bytes (set to 0 if value is NULL)
97 * @param expiry time after which the record expires
98 * @param msg_type message type to be set in header
99 * @return pointer to record message struct
101 struct GNUNET_MQ_Envelope *
102 PEERSTORE_create_record_mq_envelope(const char *sub_system,
103 const struct GNUNET_PeerIdentity *peer,
107 struct GNUNET_TIME_Absolute *expiry,
110 struct StoreRecordMessage *srm;
111 struct GNUNET_MQ_Envelope *ev;
117 GNUNET_assert(NULL != sub_system);
118 ss_size = strlen(sub_system) + 1;
122 key_size = strlen(key) + 1;
126 ev = GNUNET_MQ_msg_extra(srm, msg_size, msg_type);
127 srm->key_size = htons(key_size);
129 srm->expiry = *expiry;
131 srm->peer_set = htons(GNUNET_NO);
134 srm->peer_set = htons(GNUNET_YES);
137 srm->sub_system_size = htons(ss_size);
138 srm->value_size = htons(value_size);
140 memcpy(dummy, sub_system, ss_size);
142 memcpy(dummy, key, key_size);
144 memcpy(dummy, value, value_size);
150 * Parses a message carrying a record
152 * @param message the actual message
153 * @return Pointer to record or NULL if error
155 struct GNUNET_PEERSTORE_Record *
156 PEERSTORE_parse_record_message(const struct GNUNET_MessageHeader *message)
158 struct StoreRecordMessage *srm;
159 struct GNUNET_PEERSTORE_Record *record;
166 req_size = ntohs(message->size);
167 if(req_size < sizeof(struct StoreRecordMessage))
169 srm = (struct StoreRecordMessage *)message;
170 ss_size = ntohs(srm->sub_system_size);
171 key_size = ntohs(srm->key_size);
172 value_size = ntohs(srm->value_size);
173 if(ss_size + key_size + value_size + sizeof(struct StoreRecordMessage)
176 record = GNUNET_new(struct GNUNET_PEERSTORE_Record);
177 if(GNUNET_YES == ntohs(srm->peer_set))
179 record->peer = GNUNET_new(struct GNUNET_PeerIdentity);
180 memcpy(record->peer, &srm->peer, sizeof(struct GNUNET_PeerIdentity));
182 record->expiry = GNUNET_new(struct GNUNET_TIME_Absolute);
183 *(record->expiry) = srm->expiry;
184 dummy = (char *)&srm[1];
187 record->sub_system = GNUNET_strdup(dummy);
192 record->key = GNUNET_strdup(dummy);
197 record->value = GNUNET_malloc(value_size);
198 memcpy(record->value, dummy, value_size);
200 record->value_size = value_size;