GNUnet 0.22.2
peerstore_common.c
Go to the documentation of this file.
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 */
25#include "platform.h"
26#include "peerstore_common.h"
27
32void
33PEERSTORE_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
61struct GNUNET_MQ_Envelope *
63 const char *sub_system,
64 const struct GNUNET_PeerIdentity *peer,
65 const char *key,
66 const void *value,
67 size_t value_size,
68 struct GNUNET_TIME_Absolute expiry,
70 uint16_t msg_type)
71{
72 struct PeerstoreRecordMessage *srm;
73 struct GNUNET_MQ_Envelope *ev;
74 size_t ss_size;
75 size_t key_size;
76 size_t msg_size;
77 void *dummy;
78
79 GNUNET_assert (NULL != sub_system);
80 ss_size = strlen (sub_system) + 1;
81 if (NULL == key)
82 key_size = 0;
83 else
84 key_size = strlen (key) + 1;
85 msg_size = ss_size + key_size + value_size;
86 ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type);
87 srm->key_size = htons (key_size);
88 srm->expiry = GNUNET_TIME_absolute_hton (expiry);
89 GNUNET_assert (NULL != peer);
90 srm->peer = *peer;
91 srm->rid = htons (rid);
92 srm->sub_system_size = htons (ss_size);
93 srm->value_size = htons (value_size);
94 srm->options = htonl (options);
95 dummy = &srm[1];
96 GNUNET_memcpy (dummy, sub_system, ss_size);
97 dummy += ss_size;
98 GNUNET_memcpy (dummy, key, key_size);
99 dummy += key_size;
100 GNUNET_memcpy (dummy, value, value_size);
101 return ev;
102}
103
104
107{
109 uint16_t req_size;
110 uint16_t ss_size;
111 uint16_t key_size;
112 uint16_t value_size;
113 char *dummy;
114
115 req_size = ntohs (srm->header.size) - sizeof(*srm);
116 ss_size = ntohs (srm->sub_system_size);
117 key_size = ntohs (srm->key_size);
118 value_size = ntohs (srm->value_size);
119 if (ss_size + key_size + value_size != req_size)
120 {
121 GNUNET_break (0);
122 return NULL;
123 }
125 record->peer = srm->peer;
126 record->expiry = GNUNET_TIME_absolute_ntoh (srm->expiry);
127 dummy = (char *) &srm[1];
128 if (ss_size > 0)
129 {
130 record->sub_system = GNUNET_strdup (dummy);
131 dummy += ss_size;
132 }
133 if (key_size > 0)
134 {
135 record->key = GNUNET_strdup (dummy);
136 dummy += key_size;
137 }
138 if (value_size > 0)
139 {
141 GNUNET_memcpy (record->value,
142 dummy,
143 value_size);
144 }
145 record->value_size = value_size;
146 return record;
147}
148
149
155void
157{
158 if (NULL != record->sub_system)
159 GNUNET_free (record->sub_system);
160 if (NULL != record->key)
161 GNUNET_free (record->key);
162 if (NULL != record->value)
163 {
164 GNUNET_free (record->value);
165 record->value = 0;
166 }
168}
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static int ret
Final status code.
Definition: gnunet-arm.c:93
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
struct GNUNET_HashCode key
The key used in the DHT.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static char * value
Value of the record to add/remove.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:61
GNUNET_PEERSTORE_StoreOption
Options for storing values in PEERSTORE.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:741
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:640
void PEERSTORE_hash_key(const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, struct GNUNET_HashCode *ret)
Creates a hash of the given key combination.
struct GNUNET_PEERSTORE_Record * PEERSTORE_parse_record_message(const struct PeerstoreRecordMessage *srm)
Parses a message carrying a record.
struct GNUNET_MQ_Envelope * PEERSTORE_create_record_mq_envelope(uint32_t rid, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t value_size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, uint16_t msg_type)
Creates a MQ envelope for a single record.
void PEERSTORE_destroy_record(struct GNUNET_PEERSTORE_Record *record)
Free any memory allocated for this record.
Helper peerstore functions.
A 512-bit hashcode.
Single PEERSTORE record.
size_t value_size
Size of value BLOB.
The identity of the host (wraps the signing key of the peer).
Time for absolute times used by GNUnet, in microseconds.
Message carrying a PEERSTORE record message.
Definition: peerstore.h:38
uint16_t value_size
Size of value blob Allocated at position 2 after this struct.
Definition: peerstore.h:75
uint16_t key_size
Size of the key string Allocated at position 1 after this struct.
Definition: peerstore.h:69
struct GNUNET_TIME_AbsoluteNBO expiry
Expiry time of entry.
Definition: peerstore.h:52
uint32_t rid
Request id.
Definition: peerstore.h:57
uint32_t options
Options, needed only in case of a store operation.
Definition: peerstore.h:63
uint16_t sub_system_size
Size of the sub_system string Allocated at position 0 after this struct.
Definition: peerstore.h:82
struct GNUNET_PeerIdentity peer
Peer Identity.
Definition: peerstore.h:47
struct GNUNET_MessageHeader header
GNUnet message header.
Definition: peerstore.h:42