- doc
[oweals/gnunet.git] / src / peerstore / peerstore_common.c
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
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.
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       General Public License for more details.
14
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.
19  */
20 /**
21  * @file peerstore/peerstore_common.c
22  * @brief Helper peerstore functions
23  * @author Omar Tarabai
24  */
25
26 #include "peerstore_common.h"
27
28 /**
29  * Creates a hash of the given key combination
30  *
31  */
32 void
33 PEERSTORE_hash_key (const char *sub_system,
34                     const struct GNUNET_PeerIdentity *peer,
35                     const char *key,
36                     struct GNUNET_HashCode *ret)
37 {
38   size_t sssize;
39   size_t psize;
40   size_t ksize;
41   size_t totalsize;
42   void *block;
43   void *blockptr;
44
45   sssize = strlen (sub_system) + 1;
46   psize = sizeof (struct GNUNET_PeerIdentity);
47   ksize = strlen (key) + 1;
48   totalsize = sssize + psize + ksize;
49   block = GNUNET_malloc (totalsize);
50   blockptr = block;
51   GNUNET_memcpy (blockptr, sub_system, sssize);
52   blockptr += sssize;
53   GNUNET_memcpy (blockptr, peer, psize);
54   blockptr += psize;
55   GNUNET_memcpy (blockptr, key, ksize);
56   GNUNET_CRYPTO_hash (block, totalsize, ret);
57   GNUNET_free (block);
58 }
59
60
61 /**
62  * Creates a MQ envelope for a single record
63  *
64  * @param sub_system sub system string
65  * @param peer Peer identity (can be NULL)
66  * @param key record key string (can be NULL)
67  * @param value record value BLOB (can be NULL)
68  * @param value_size record value size in bytes (set to 0 if value is NULL)
69  * @param expiry time after which the record expires
70  * @param options options specific to the storage operation
71  * @param msg_type message type to be set in header
72  * @return pointer to record message struct
73  */
74 struct GNUNET_MQ_Envelope *
75 PEERSTORE_create_record_mq_envelope (const char *sub_system,
76                                      const struct GNUNET_PeerIdentity *peer,
77                                      const char *key,
78                                      const void *value,
79                                      size_t value_size,
80                                      struct GNUNET_TIME_Absolute *expiry,
81                                      enum GNUNET_PEERSTORE_StoreOption options,
82                                      uint16_t msg_type)
83 {
84   struct StoreRecordMessage *srm;
85   struct GNUNET_MQ_Envelope *ev;
86   size_t ss_size;
87   size_t key_size;
88   size_t msg_size;
89   void *dummy;
90
91   GNUNET_assert (NULL != sub_system);
92   ss_size = strlen (sub_system) + 1;
93   if (NULL == key)
94     key_size = 0;
95   else
96     key_size = strlen (key) + 1;
97   msg_size = ss_size + key_size + value_size;
98   ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type);
99   srm->key_size = htons (key_size);
100   if (NULL != expiry)
101     srm->expiry = *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
122 /**
123  * Parses a message carrying a record
124  *
125  * @param srm the actual message
126  * @return Pointer to record or NULL if error
127  */
128 struct GNUNET_PEERSTORE_Record *
129 PEERSTORE_parse_record_message (const struct StoreRecordMessage *srm)
130 {
131   struct GNUNET_PEERSTORE_Record *record;
132   uint16_t req_size;
133   uint16_t ss_size;
134   uint16_t key_size;
135   uint16_t value_size;
136   char *dummy;
137
138   req_size = ntohs (srm->header.size) - sizeof (*srm);
139   ss_size = ntohs (srm->sub_system_size);
140   key_size = ntohs (srm->key_size);
141   value_size = ntohs (srm->value_size);
142   if (ss_size + key_size + value_size != req_size)
143   {
144     GNUNET_break (0);
145     return NULL;
146   }
147   record = GNUNET_new (struct GNUNET_PEERSTORE_Record);
148   if (GNUNET_YES == ntohs (srm->peer_set))
149   {
150     record->peer = GNUNET_new (struct GNUNET_PeerIdentity);
151     *record->peer = srm->peer;
152   }
153   record->expiry = GNUNET_new (struct GNUNET_TIME_Absolute);
154
155   *(record->expiry) = srm->expiry;
156   dummy = (char *) &srm[1];
157   if (ss_size > 0)
158   {
159     record->sub_system = GNUNET_strdup (dummy);
160     dummy += ss_size;
161   }
162   if (key_size > 0)
163   {
164     record->key = GNUNET_strdup (dummy);
165     dummy += key_size;
166   }
167   if (value_size > 0)
168   {
169     record->value = GNUNET_malloc (value_size);
170     GNUNET_memcpy (record->value, dummy, value_size);
171   }
172   record->value_size = value_size;
173   return record;
174 }
175
176
177 /**
178  * Free any memory allocated for this record
179  *
180  * @param record
181  */
182 void
183 PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record)
184 {
185   if (NULL != record->sub_system)
186     GNUNET_free (record->sub_system);
187   if (NULL != record->peer)
188     GNUNET_free (record->peer);
189   if (NULL != record->key)
190     GNUNET_free (record->key);
191   if (NULL != record->value)
192   {
193     GNUNET_free (record->value);
194     record->value = 0;
195   }
196   if (NULL != record->expiry)
197     GNUNET_free (record->expiry);
198   GNUNET_free (record);
199 }