aboutsummaryrefslogtreecommitdiff
path: root/src/service/peerstore/peerstore_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/peerstore/peerstore_common.c')
-rw-r--r--src/service/peerstore/peerstore_common.c188
1 files changed, 188 insertions, 0 deletions
diff --git a/src/service/peerstore/peerstore_common.c b/src/service/peerstore/peerstore_common.c
new file mode 100644
index 000000000..e3bb77d86
--- /dev/null
+++ b/src/service/peerstore/peerstore_common.c
@@ -0,0 +1,188 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2012-2013 GNUnet e.V.
4
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.
9
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.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file peerstore/peerstore_common.c
22 * @brief Helper peerstore functions
23 * @author Omar Tarabai
24 */
25
26#include "platform.h"
27#include "peerstore_common.h"
28
29/**
30 * Creates a hash of the given key combination
31 *
32 */
33void
34PEERSTORE_hash_key (const char *sub_system,
35 const struct GNUNET_PeerIdentity *peer,
36 const char *key,
37 struct GNUNET_HashCode *ret)
38{
39 size_t sssize;
40 size_t psize;
41 size_t ksize;
42 size_t totalsize;
43 void *block;
44 void *blockptr;
45
46 sssize = strlen (sub_system) + 1;
47 psize = sizeof(struct GNUNET_PeerIdentity);
48 ksize = strlen (key) + 1;
49 totalsize = sssize + psize + ksize;
50 block = GNUNET_malloc (totalsize);
51 blockptr = block;
52 GNUNET_memcpy (blockptr, sub_system, sssize);
53 blockptr += sssize;
54 GNUNET_memcpy (blockptr, peer, psize);
55 blockptr += psize;
56 GNUNET_memcpy (blockptr, key, ksize);
57 GNUNET_CRYPTO_hash (block, totalsize, ret);
58 GNUNET_free (block);
59}
60
61
62/**
63 * Creates a MQ envelope for a single record
64 *
65 * @param sub_system sub system string
66 * @param peer Peer identity (can be NULL)
67 * @param key record key string (can be NULL)
68 * @param value record value BLOB (can be NULL)
69 * @param value_size record value size in bytes (set to 0 if value is NULL)
70 * @param expiry time after which the record expires
71 * @param options options specific to the storage operation
72 * @param msg_type message type to be set in header
73 * @return pointer to record message struct
74 */
75struct GNUNET_MQ_Envelope *
76PEERSTORE_create_record_mq_envelope (const char *sub_system,
77 const struct GNUNET_PeerIdentity *peer,
78 const char *key,
79 const void *value,
80 size_t value_size,
81 struct GNUNET_TIME_Absolute expiry,
82 enum GNUNET_PEERSTORE_StoreOption options,
83 uint16_t msg_type)
84{
85 struct StoreRecordMessage *srm;
86 struct GNUNET_MQ_Envelope *ev;
87 size_t ss_size;
88 size_t key_size;
89 size_t msg_size;
90 void *dummy;
91
92 GNUNET_assert (NULL != sub_system);
93 ss_size = strlen (sub_system) + 1;
94 if (NULL == key)
95 key_size = 0;
96 else
97 key_size = strlen (key) + 1;
98 msg_size = ss_size + key_size + value_size;
99 ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type);
100 srm->key_size = htons (key_size);
101 srm->expiry = GNUNET_TIME_absolute_hton (expiry);
102 if (NULL == peer)
103 srm->peer_set = htons (GNUNET_NO);
104 else
105 {
106 srm->peer_set = htons (GNUNET_YES);
107 srm->peer = *peer;
108 }
109 srm->sub_system_size = htons (ss_size);
110 srm->value_size = htons (value_size);
111 srm->options = htonl (options);
112 dummy = &srm[1];
113 GNUNET_memcpy (dummy, sub_system, ss_size);
114 dummy += ss_size;
115 GNUNET_memcpy (dummy, key, key_size);
116 dummy += key_size;
117 GNUNET_memcpy (dummy, value, value_size);
118 return ev;
119}
120
121
122struct GNUNET_PEERSTORE_Record *
123PEERSTORE_parse_record_message (const struct StoreRecordMessage *srm)
124{
125 struct GNUNET_PEERSTORE_Record *record;
126 uint16_t req_size;
127 uint16_t ss_size;
128 uint16_t key_size;
129 uint16_t value_size;
130 char *dummy;
131
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)
137 {
138 GNUNET_break (0);
139 return NULL;
140 }
141 record = GNUNET_new (struct GNUNET_PEERSTORE_Record);
142 if (GNUNET_YES == ntohs (srm->peer_set))
143 {
144 record->peer = srm->peer;
145 }
146 record->expiry = GNUNET_TIME_absolute_ntoh (srm->expiry);
147 dummy = (char *) &srm[1];
148 if (ss_size > 0)
149 {
150 record->sub_system = GNUNET_strdup (dummy);
151 dummy += ss_size;
152 }
153 if (key_size > 0)
154 {
155 record->key = GNUNET_strdup (dummy);
156 dummy += key_size;
157 }
158 if (value_size > 0)
159 {
160 record->value = GNUNET_malloc (value_size);
161 GNUNET_memcpy (record->value,
162 dummy,
163 value_size);
164 }
165 record->value_size = value_size;
166 return record;
167}
168
169
170/**
171 * Free any memory allocated for this record
172 *
173 * @param record
174 */
175void
176PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record)
177{
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)
183 {
184 GNUNET_free (record->value);
185 record->value = 0;
186 }
187 GNUNET_free (record);
188}