GNUnet  0.10.x
Functions
peerstore_common.c File Reference

Helper peerstore functions. More...

#include "peerstore_common.h"
Include dependency graph for peerstore_common.c:

Go to the source code of this file.

Functions

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. More...
 
struct GNUNET_MQ_EnvelopePEERSTORE_create_record_mq_envelope (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. More...
 
struct GNUNET_PEERSTORE_RecordPEERSTORE_parse_record_message (const struct StoreRecordMessage *srm)
 Parses a message carrying a record. More...
 
void PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record)
 Free any memory allocated for this record. More...
 

Detailed Description

Helper peerstore functions.

Author
Omar Tarabai

Definition in file peerstore_common.c.

Function Documentation

◆ PEERSTORE_hash_key()

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.

Definition at line 33 of file peerstore_common.c.

References GNUNET_CRYPTO_hash(), GNUNET_free, GNUNET_malloc, and GNUNET_memcpy.

Referenced by GNUNET_PEERSTORE_watch(), handle_watch_record(), and watch_notifier().

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 }
#define GNUNET_memcpy(dst, src, n)
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
struct GNUNET_HashCode key
The key used in the DHT.
The identity of the host (wraps the signing key of the peer).
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PEERSTORE_create_record_mq_envelope()

struct GNUNET_MQ_Envelope* PEERSTORE_create_record_mq_envelope ( 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.

Parameters
sub_systemsub system string
peerPeer identity (can be NULL)
keyrecord key string (can be NULL)
valuerecord value BLOB (can be NULL)
value_sizerecord value size in bytes (set to 0 if value is NULL)
expirytime after which the record expires
optionsoptions specific to the storage operation
msg_typemessage type to be set in header
Returns
pointer to record message struct

Definition at line 75 of file peerstore_common.c.

References dummy, StoreRecordMessage::expiry, GNUNET_assert, GNUNET_memcpy, GNUNET_MQ_msg_extra, GNUNET_NO, GNUNET_TIME_absolute_hton(), GNUNET_YES, StoreRecordMessage::key_size, StoreRecordMessage::options, StoreRecordMessage::peer, peer, StoreRecordMessage::peer_set, StoreRecordMessage::sub_system_size, and StoreRecordMessage::value_size.

Referenced by GNUNET_PEERSTORE_iterate(), GNUNET_PEERSTORE_store(), reconnect(), record_iterator(), and watch_notifier_it().

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  srm->expiry = GNUNET_TIME_absolute_hton (expiry);
101  if (NULL == peer)
102  srm->peer_set = htons (GNUNET_NO);
103  else
104  {
105  srm->peer_set = htons (GNUNET_YES);
106  srm->peer = *peer;
107  }
108  srm->sub_system_size = htons (ss_size);
109  srm->value_size = htons (value_size);
110  srm->options = htonl (options);
111  dummy = &srm[1];
112  GNUNET_memcpy (dummy, sub_system, ss_size);
113  dummy += ss_size;
114  GNUNET_memcpy (dummy, key, key_size);
115  dummy += key_size;
116  GNUNET_memcpy (dummy, value, value_size);
117  return ev;
118 }
uint16_t sub_system_size
Size of the sub_system string Allocated at position 0 after this struct.
Definition: peerstore.h:53
static struct in_addr dummy
Target "dummy" address.
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
struct GNUNET_PeerIdentity peer
Peer Identity.
Definition: peerstore.h:58
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
uint16_t value_size
Size of value blob Allocated at position 2 after this struct.
Definition: peerstore.h:75
#define GNUNET_NO
Definition: gnunet_common.h:81
#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:52
#define GNUNET_memcpy(dst, src, n)
static char * value
Value of the record to add/remove.
uint16_t peer_set
GNUNET_YES if peer id value set, GNUNET_NO otherwise
Definition: peerstore.h:47
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:63
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t options
Options, needed only in case of a store operation.
Definition: peerstore.h:81
Message carrying a PEERSTORE record message.
Definition: peerstore.h:36
#define GNUNET_YES
Definition: gnunet_common.h:80
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:654
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PEERSTORE_parse_record_message()

struct GNUNET_PEERSTORE_Record* PEERSTORE_parse_record_message ( const struct StoreRecordMessage srm)

Parses a message carrying a record.

Parameters
srmthe actual message
Returns
Pointer to record or NULL if error

Definition at line 128 of file peerstore_common.c.

References dummy, StoreRecordMessage::expiry, GNUNET_PEERSTORE_Record::expiry, GNUNET_break, GNUNET_malloc, GNUNET_memcpy, GNUNET_new, GNUNET_strdup, GNUNET_TIME_absolute_ntoh(), GNUNET_YES, StoreRecordMessage::header, GNUNET_PEERSTORE_Record::key, StoreRecordMessage::key_size, StoreRecordMessage::peer, GNUNET_PEERSTORE_Record::peer, StoreRecordMessage::peer_set, record(), GNUNET_MessageHeader::size, GNUNET_PEERSTORE_Record::sub_system, StoreRecordMessage::sub_system_size, GNUNET_PEERSTORE_Record::value, StoreRecordMessage::value_size, and GNUNET_PEERSTORE_Record::value_size.

Referenced by check_iterate(), check_store(), handle_iterate(), handle_iterate_result(), handle_store(), and handle_watch_record().

129 {
131  uint16_t req_size;
132  uint16_t ss_size;
133  uint16_t key_size;
134  uint16_t value_size;
135  char *dummy;
136 
137  req_size = ntohs (srm->header.size) - sizeof (*srm);
138  ss_size = ntohs (srm->sub_system_size);
139  key_size = ntohs (srm->key_size);
140  value_size = ntohs (srm->value_size);
141  if (ss_size + key_size + value_size != req_size)
142  {
143  GNUNET_break (0);
144  return NULL;
145  }
146  record = GNUNET_new (struct GNUNET_PEERSTORE_Record);
147  if (GNUNET_YES == ntohs (srm->peer_set))
148  {
149  record->peer = srm->peer;
150  }
151  record->expiry = GNUNET_TIME_absolute_ntoh (srm->expiry);
152  dummy = (char *) &srm[1];
153  if (ss_size > 0)
154  {
155  record->sub_system = GNUNET_strdup (dummy);
156  dummy += ss_size;
157  }
158  if (key_size > 0)
159  {
160  record->key = GNUNET_strdup (dummy);
161  dummy += key_size;
162  }
163  if (value_size > 0)
164  {
165  record->value = GNUNET_malloc (value_size);
166  GNUNET_memcpy (record->value,
167  dummy,
168  value_size);
169  }
170  record->value_size = value_size;
171  return record;
172 }
struct GNUNET_MessageHeader header
GNUnet message header.
Definition: peerstore.h:42
uint16_t sub_system_size
Size of the sub_system string Allocated at position 0 after this struct.
Definition: peerstore.h:53
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:670
static struct in_addr dummy
Target "dummy" address.
struct GNUNET_PeerIdentity peer
Peer Identity.
size_t value_size
Size of value BLOB.
struct GNUNET_PeerIdentity peer
Peer Identity.
Definition: peerstore.h:58
uint16_t value_size
Size of value blob Allocated at position 2 after this struct.
Definition: peerstore.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
char * key
Record key string.
#define GNUNET_memcpy(dst, src, n)
void * value
Record value BLOB.
uint16_t peer_set
GNUNET_YES if peer id value set, GNUNET_NO otherwise
Definition: peerstore.h:47
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:63
struct GNUNET_TIME_Absolute expiry
Expiry time of entry.
char * sub_system
Responsible sub system string.
Single PEERSTORE record.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
#define GNUNET_YES
Definition: gnunet_common.h:80
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PEERSTORE_destroy_record()

void PEERSTORE_destroy_record ( struct GNUNET_PEERSTORE_Record record)

Free any memory allocated for this record.

Parameters
record

Definition at line 181 of file peerstore_common.c.

References GNUNET_free, GNUNET_PEERSTORE_Record::key, GNUNET_PEERSTORE_Record::sub_system, and GNUNET_PEERSTORE_Record::value.

Referenced by check_iterate(), check_store(), handle_iterate(), handle_iterate_result(), handle_store(), handle_watch_record(), record_iterator(), and store_record_continuation().

182 {
183  if (NULL != record->sub_system)
184  GNUNET_free (record->sub_system);
185  if (NULL != record->key)
186  GNUNET_free (record->key);
187  if (NULL != record->value)
188  {
189  GNUNET_free (record->value);
190  record->value = 0;
191  }
192  GNUNET_free (record);
193 }
char * key
Record key string.
void * value
Record value BLOB.
char * sub_system
Responsible sub system string.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function: