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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file peerstore/peerstore_common.c
17 * @brief Helper peerstore functions
18 * @author Omar Tarabai
21 #include "peerstore_common.h"
24 * Creates a hash of the given key combination
28 PEERSTORE_hash_key (const char *sub_system,
29 const struct GNUNET_PeerIdentity *peer,
31 struct GNUNET_HashCode *ret)
40 sssize = strlen (sub_system) + 1;
41 psize = sizeof (struct GNUNET_PeerIdentity);
42 ksize = strlen (key) + 1;
43 totalsize = sssize + psize + ksize;
44 block = GNUNET_malloc (totalsize);
46 GNUNET_memcpy (blockptr, sub_system, sssize);
48 GNUNET_memcpy (blockptr, peer, psize);
50 GNUNET_memcpy (blockptr, key, ksize);
51 GNUNET_CRYPTO_hash (block, totalsize, ret);
57 * Creates a MQ envelope for a single record
59 * @param sub_system sub system string
60 * @param peer Peer identity (can be NULL)
61 * @param key record key string (can be NULL)
62 * @param value record value BLOB (can be NULL)
63 * @param value_size record value size in bytes (set to 0 if value is NULL)
64 * @param expiry time after which the record expires
65 * @param options options specific to the storage operation
66 * @param msg_type message type to be set in header
67 * @return pointer to record message struct
69 struct GNUNET_MQ_Envelope *
70 PEERSTORE_create_record_mq_envelope (const char *sub_system,
71 const struct GNUNET_PeerIdentity *peer,
75 struct GNUNET_TIME_Absolute expiry,
76 enum GNUNET_PEERSTORE_StoreOption options,
79 struct StoreRecordMessage *srm;
80 struct GNUNET_MQ_Envelope *ev;
86 GNUNET_assert (NULL != sub_system);
87 ss_size = strlen (sub_system) + 1;
91 key_size = strlen (key) + 1;
92 msg_size = ss_size + key_size + value_size;
93 ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type);
94 srm->key_size = htons (key_size);
95 srm->expiry = GNUNET_TIME_absolute_hton (expiry);
97 srm->peer_set = htons (GNUNET_NO);
100 srm->peer_set = htons (GNUNET_YES);
103 srm->sub_system_size = htons (ss_size);
104 srm->value_size = htons (value_size);
105 srm->options = htonl (options);
107 GNUNET_memcpy (dummy, sub_system, ss_size);
109 GNUNET_memcpy (dummy, key, key_size);
111 GNUNET_memcpy (dummy, value, value_size);
117 * Parses a message carrying a record
119 * @param srm the actual message
120 * @return Pointer to record or NULL if error
122 struct GNUNET_PEERSTORE_Record *
123 PEERSTORE_parse_record_message (const struct StoreRecordMessage *srm)
125 struct GNUNET_PEERSTORE_Record *record;
132 req_size = ntohs (srm->header.size) - sizeof (*srm);
133 ss_size = ntohs (srm->sub_system_size);
134 key_size = ntohs (srm->key_size);
135 value_size = ntohs (srm->value_size);
136 if (ss_size + key_size + value_size != req_size)
141 record = GNUNET_new (struct GNUNET_PEERSTORE_Record);
142 if (GNUNET_YES == ntohs (srm->peer_set))
144 record->peer = srm->peer;
146 record->expiry = GNUNET_TIME_absolute_ntoh (srm->expiry);
147 dummy = (char *) &srm[1];
150 record->sub_system = GNUNET_strdup (dummy);
155 record->key = GNUNET_strdup (dummy);
160 record->value = GNUNET_malloc (value_size);
161 GNUNET_memcpy (record->value,
165 record->value_size = value_size;
171 * Free any memory allocated for this record
176 PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record)
178 if (NULL != record->sub_system)
179 GNUNET_free (record->sub_system);
180 if (NULL != record->key)
181 GNUNET_free (record->key);
182 if (NULL != record->value)
184 GNUNET_free (record->value);
187 GNUNET_free (record);