diff options
Diffstat (limited to 'src/peerstore/peerstore_common.c')
-rw-r--r-- | src/peerstore/peerstore_common.c | 203 |
1 files changed, 100 insertions, 103 deletions
diff --git a/src/peerstore/peerstore_common.c b/src/peerstore/peerstore_common.c index 0cb801cc4..1f86fe442 100644 --- a/src/peerstore/peerstore_common.c +++ b/src/peerstore/peerstore_common.c | |||
@@ -30,10 +30,9 @@ | |||
30 | * | 30 | * |
31 | */ | 31 | */ |
32 | void | 32 | void |
33 | PEERSTORE_hash_key(const char *sub_system, | 33 | PEERSTORE_hash_key (const char *sub_system, |
34 | const struct GNUNET_PeerIdentity *peer, | 34 | const struct GNUNET_PeerIdentity *peer, const char *key, |
35 | const char *key, | 35 | struct GNUNET_HashCode *ret) |
36 | struct GNUNET_HashCode *ret) | ||
37 | { | 36 | { |
38 | size_t sssize; | 37 | size_t sssize; |
39 | size_t psize; | 38 | size_t psize; |
@@ -42,21 +41,22 @@ PEERSTORE_hash_key(const char *sub_system, | |||
42 | void *block; | 41 | void *block; |
43 | void *blockptr; | 42 | void *blockptr; |
44 | 43 | ||
45 | sssize = strlen(sub_system) + 1; | 44 | sssize = strlen (sub_system) + 1; |
46 | psize = sizeof(struct GNUNET_PeerIdentity); | 45 | psize = sizeof (struct GNUNET_PeerIdentity); |
47 | ksize = strlen(key) + 1; | 46 | ksize = strlen (key) + 1; |
48 | totalsize = sssize + psize + ksize; | 47 | totalsize = sssize + psize + ksize; |
49 | block = GNUNET_malloc(totalsize); | 48 | block = GNUNET_malloc (totalsize); |
50 | blockptr = block; | 49 | blockptr = block; |
51 | memcpy(blockptr, sub_system, sssize); | 50 | memcpy (blockptr, sub_system, sssize); |
52 | blockptr += sssize; | 51 | blockptr += sssize; |
53 | memcpy(blockptr, peer, psize); | 52 | memcpy (blockptr, peer, psize); |
54 | blockptr += psize; | 53 | blockptr += psize; |
55 | memcpy(blockptr, key, ksize); | 54 | memcpy (blockptr, key, ksize); |
56 | GNUNET_CRYPTO_hash(block, totalsize, ret); | 55 | GNUNET_CRYPTO_hash (block, totalsize, ret); |
57 | GNUNET_free(block); | 56 | GNUNET_free (block); |
58 | } | 57 | } |
59 | 58 | ||
59 | |||
60 | /** | 60 | /** |
61 | * Creates a record message ready to be sent | 61 | * Creates a record message ready to be sent |
62 | * | 62 | * |
@@ -70,13 +70,12 @@ PEERSTORE_hash_key(const char *sub_system, | |||
70 | * @return pointer to record message struct | 70 | * @return pointer to record message struct |
71 | */ | 71 | */ |
72 | struct StoreRecordMessage * | 72 | struct StoreRecordMessage * |
73 | PEERSTORE_create_record_message(const char *sub_system, | 73 | PEERSTORE_create_record_message (const char *sub_system, |
74 | const struct GNUNET_PeerIdentity *peer, | 74 | const struct GNUNET_PeerIdentity *peer, |
75 | const char *key, | 75 | const char *key, const void *value, |
76 | const void *value, | 76 | size_t value_size, |
77 | size_t value_size, | 77 | struct GNUNET_TIME_Absolute *expiry, |
78 | struct GNUNET_TIME_Absolute *expiry, | 78 | uint16_t msg_type) |
79 | uint16_t msg_type) | ||
80 | { | 79 | { |
81 | struct StoreRecordMessage *srm; | 80 | struct StoreRecordMessage *srm; |
82 | size_t ss_size; | 81 | size_t ss_size; |
@@ -84,40 +83,38 @@ PEERSTORE_create_record_message(const char *sub_system, | |||
84 | size_t request_size; | 83 | size_t request_size; |
85 | void *dummy; | 84 | void *dummy; |
86 | 85 | ||
87 | ss_size = strlen(sub_system) + 1; | 86 | ss_size = strlen (sub_system) + 1; |
88 | if(NULL == key) | 87 | if (NULL == key) |
89 | key_size = 0; | 88 | key_size = 0; |
90 | else | 89 | else |
91 | key_size = strlen(key) + 1; | 90 | key_size = strlen (key) + 1; |
92 | request_size = sizeof(struct StoreRecordMessage) + | 91 | request_size = |
93 | ss_size + | 92 | sizeof (struct StoreRecordMessage) + ss_size + key_size + value_size; |
94 | key_size + | 93 | srm = GNUNET_malloc (request_size); |
95 | value_size; | 94 | srm->header.size = htons (request_size); |
96 | srm = GNUNET_malloc(request_size); | 95 | srm->header.type = htons (msg_type); |
97 | srm->header.size = htons(request_size); | 96 | srm->key_size = htons (key_size); |
98 | srm->header.type = htons(msg_type); | 97 | if (NULL != expiry) |
99 | srm->key_size = htons(key_size); | ||
100 | if(NULL != expiry) | ||
101 | srm->expiry = *expiry; | 98 | srm->expiry = *expiry; |
102 | if(NULL == peer) | 99 | if (NULL == peer) |
103 | srm->peer_set = htons(GNUNET_NO); | 100 | srm->peer_set = htons (GNUNET_NO); |
104 | else | 101 | else |
105 | { | 102 | { |
106 | srm->peer_set = htons(GNUNET_YES); | 103 | srm->peer_set = htons (GNUNET_YES); |
107 | srm->peer = *peer; | 104 | srm->peer = *peer; |
108 | } | 105 | } |
109 | srm->sub_system_size = htons(ss_size); | 106 | srm->sub_system_size = htons (ss_size); |
110 | srm->value_size = htons(value_size); | 107 | srm->value_size = htons (value_size); |
111 | dummy = &srm[1]; | 108 | dummy = &srm[1]; |
112 | memcpy(dummy, sub_system, ss_size); | 109 | memcpy (dummy, sub_system, ss_size); |
113 | dummy += ss_size; | 110 | dummy += ss_size; |
114 | memcpy(dummy, key, key_size); | 111 | memcpy (dummy, key, key_size); |
115 | dummy += key_size; | 112 | dummy += key_size; |
116 | memcpy(dummy, value, value_size); | 113 | memcpy (dummy, value, value_size); |
117 | return srm; | 114 | return srm; |
118 | |||
119 | } | 115 | } |
120 | 116 | ||
117 | |||
121 | /** | 118 | /** |
122 | * Creates a MQ envelope for a single record | 119 | * Creates a MQ envelope for a single record |
123 | * | 120 | * |
@@ -132,14 +129,13 @@ PEERSTORE_create_record_message(const char *sub_system, | |||
132 | * @return pointer to record message struct | 129 | * @return pointer to record message struct |
133 | */ | 130 | */ |
134 | struct GNUNET_MQ_Envelope * | 131 | struct GNUNET_MQ_Envelope * |
135 | PEERSTORE_create_record_mq_envelope(const char *sub_system, | 132 | PEERSTORE_create_record_mq_envelope (const char *sub_system, |
136 | const struct GNUNET_PeerIdentity *peer, | 133 | const struct GNUNET_PeerIdentity *peer, |
137 | const char *key, | 134 | const char *key, const void *value, |
138 | const void *value, | 135 | size_t value_size, |
139 | size_t value_size, | 136 | struct GNUNET_TIME_Absolute *expiry, |
140 | struct GNUNET_TIME_Absolute *expiry, | 137 | enum GNUNET_PEERSTORE_StoreOption options, |
141 | enum GNUNET_PEERSTORE_StoreOption options, | 138 | uint16_t msg_type) |
142 | uint16_t msg_type) | ||
143 | { | 139 | { |
144 | struct StoreRecordMessage *srm; | 140 | struct StoreRecordMessage *srm; |
145 | struct GNUNET_MQ_Envelope *ev; | 141 | struct GNUNET_MQ_Envelope *ev; |
@@ -148,39 +144,37 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, | |||
148 | size_t msg_size; | 144 | size_t msg_size; |
149 | void *dummy; | 145 | void *dummy; |
150 | 146 | ||
151 | GNUNET_assert(NULL != sub_system); | 147 | GNUNET_assert (NULL != sub_system); |
152 | ss_size = strlen(sub_system) + 1; | 148 | ss_size = strlen (sub_system) + 1; |
153 | if(NULL == key) | 149 | if (NULL == key) |
154 | key_size = 0; | 150 | key_size = 0; |
155 | else | 151 | else |
156 | key_size = strlen(key) + 1; | 152 | key_size = strlen (key) + 1; |
157 | msg_size = ss_size + | 153 | msg_size = ss_size + key_size + value_size; |
158 | key_size + | 154 | ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type); |
159 | value_size; | 155 | srm->key_size = htons (key_size); |
160 | ev = GNUNET_MQ_msg_extra(srm, msg_size, msg_type); | 156 | if (NULL != expiry) |
161 | srm->key_size = htons(key_size); | ||
162 | if(NULL != expiry) | ||
163 | srm->expiry = *expiry; | 157 | srm->expiry = *expiry; |
164 | if(NULL == peer) | 158 | if (NULL == peer) |
165 | srm->peer_set = htons(GNUNET_NO); | 159 | srm->peer_set = htons (GNUNET_NO); |
166 | else | 160 | else |
167 | { | 161 | { |
168 | srm->peer_set = htons(GNUNET_YES); | 162 | srm->peer_set = htons (GNUNET_YES); |
169 | srm->peer = *peer; | 163 | srm->peer = *peer; |
170 | } | 164 | } |
171 | srm->sub_system_size = htons(ss_size); | 165 | srm->sub_system_size = htons (ss_size); |
172 | srm->value_size = htons(value_size); | 166 | srm->value_size = htons (value_size); |
173 | srm->options = options; | 167 | srm->options = options; |
174 | dummy = &srm[1]; | 168 | dummy = &srm[1]; |
175 | memcpy(dummy, sub_system, ss_size); | 169 | memcpy (dummy, sub_system, ss_size); |
176 | dummy += ss_size; | 170 | dummy += ss_size; |
177 | memcpy(dummy, key, key_size); | 171 | memcpy (dummy, key, key_size); |
178 | dummy += key_size; | 172 | dummy += key_size; |
179 | memcpy(dummy, value, value_size); | 173 | memcpy (dummy, value, value_size); |
180 | |||
181 | return ev; | 174 | return ev; |
182 | } | 175 | } |
183 | 176 | ||
177 | |||
184 | /** | 178 | /** |
185 | * Parses a message carrying a record | 179 | * Parses a message carrying a record |
186 | * | 180 | * |
@@ -188,7 +182,7 @@ PEERSTORE_create_record_mq_envelope(const char *sub_system, | |||
188 | * @return Pointer to record or NULL if error | 182 | * @return Pointer to record or NULL if error |
189 | */ | 183 | */ |
190 | struct GNUNET_PEERSTORE_Record * | 184 | struct GNUNET_PEERSTORE_Record * |
191 | PEERSTORE_parse_record_message(const struct GNUNET_MessageHeader *message) | 185 | PEERSTORE_parse_record_message (const struct GNUNET_MessageHeader *message) |
192 | { | 186 | { |
193 | struct StoreRecordMessage *srm; | 187 | struct StoreRecordMessage *srm; |
194 | struct GNUNET_PEERSTORE_Record *record; | 188 | struct GNUNET_PEERSTORE_Record *record; |
@@ -198,64 +192,67 @@ PEERSTORE_parse_record_message(const struct GNUNET_MessageHeader *message) | |||
198 | uint16_t value_size; | 192 | uint16_t value_size; |
199 | char *dummy; | 193 | char *dummy; |
200 | 194 | ||
201 | req_size = ntohs(message->size); | 195 | req_size = ntohs (message->size); |
202 | if(req_size < sizeof(struct StoreRecordMessage)) | 196 | if (req_size < sizeof (struct StoreRecordMessage)) |
203 | return NULL; | 197 | return NULL; |
204 | srm = (struct StoreRecordMessage *)message; | 198 | srm = (struct StoreRecordMessage *) message; |
205 | ss_size = ntohs(srm->sub_system_size); | 199 | ss_size = ntohs (srm->sub_system_size); |
206 | key_size = ntohs(srm->key_size); | 200 | key_size = ntohs (srm->key_size); |
207 | value_size = ntohs(srm->value_size); | 201 | value_size = ntohs (srm->value_size); |
208 | if(ss_size + key_size + value_size + sizeof(struct StoreRecordMessage) | 202 | if (ss_size + key_size + value_size + sizeof (struct StoreRecordMessage) != |
209 | != req_size) | 203 | req_size) |
210 | return NULL; | 204 | return NULL; |
211 | record = GNUNET_new(struct GNUNET_PEERSTORE_Record); | 205 | record = GNUNET_new (struct GNUNET_PEERSTORE_Record); |
212 | if(GNUNET_YES == ntohs(srm->peer_set)) | 206 | if (GNUNET_YES == ntohs (srm->peer_set)) |
213 | { | 207 | { |
214 | record->peer = GNUNET_new(struct GNUNET_PeerIdentity); | 208 | record->peer = GNUNET_new (struct GNUNET_PeerIdentity); |
215 | memcpy(record->peer, &srm->peer, sizeof(struct GNUNET_PeerIdentity)); | 209 | |
210 | memcpy (record->peer, &srm->peer, sizeof (struct GNUNET_PeerIdentity)); | ||
216 | } | 211 | } |
217 | record->expiry = GNUNET_new(struct GNUNET_TIME_Absolute); | 212 | record->expiry = GNUNET_new (struct GNUNET_TIME_Absolute); |
213 | |||
218 | *(record->expiry) = srm->expiry; | 214 | *(record->expiry) = srm->expiry; |
219 | dummy = (char *)&srm[1]; | 215 | dummy = (char *) &srm[1]; |
220 | if(ss_size > 0) | 216 | if (ss_size > 0) |
221 | { | 217 | { |
222 | record->sub_system = GNUNET_strdup(dummy); | 218 | record->sub_system = GNUNET_strdup (dummy); |
223 | dummy += ss_size; | 219 | dummy += ss_size; |
224 | } | 220 | } |
225 | if(key_size > 0) | 221 | if (key_size > 0) |
226 | { | 222 | { |
227 | record->key = GNUNET_strdup(dummy); | 223 | record->key = GNUNET_strdup (dummy); |
228 | dummy += key_size; | 224 | dummy += key_size; |
229 | } | 225 | } |
230 | if(value_size > 0) | 226 | if (value_size > 0) |
231 | { | 227 | { |
232 | record->value = GNUNET_malloc(value_size); | 228 | record->value = GNUNET_malloc (value_size); |
233 | memcpy(record->value, dummy, value_size); | 229 | memcpy (record->value, dummy, value_size); |
234 | } | 230 | } |
235 | record->value_size = value_size; | 231 | record->value_size = value_size; |
236 | |||
237 | return record; | 232 | return record; |
238 | } | 233 | } |
239 | 234 | ||
235 | |||
240 | /** | 236 | /** |
241 | * Free any memory allocated for this record | 237 | * Free any memory allocated for this record |
242 | * | 238 | * |
243 | * @param record | 239 | * @param record |
244 | */ | 240 | */ |
245 | void PEERSTORE_destroy_record(struct GNUNET_PEERSTORE_Record *record) | 241 | void |
242 | PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record) | ||
246 | { | 243 | { |
247 | if(NULL != record->sub_system) | 244 | if (NULL != record->sub_system) |
248 | GNUNET_free(record->sub_system); | 245 | GNUNET_free (record->sub_system); |
249 | if(NULL != record->peer) | 246 | if (NULL != record->peer) |
250 | GNUNET_free(record->peer); | 247 | GNUNET_free (record->peer); |
251 | if(NULL != record->key) | 248 | if (NULL != record->key) |
252 | GNUNET_free(record->key); | 249 | GNUNET_free (record->key); |
253 | if(NULL != record->value) | 250 | if (NULL != record->value) |
254 | { | 251 | { |
255 | GNUNET_free(record->value); | 252 | GNUNET_free (record->value); |
256 | record->value = 0; | 253 | record->value = 0; |
257 | } | 254 | } |
258 | if(NULL != record->expiry) | 255 | if (NULL != record->expiry) |
259 | GNUNET_free(record->expiry); | 256 | GNUNET_free (record->expiry); |
260 | GNUNET_free(record); | 257 | GNUNET_free (record); |
261 | } | 258 | } |