GNUnet 0.22.2
bg_bf.c File Reference

implementation of a block group using a Bloom filter to drop duplicate blocks More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_block_group_lib.h"
#include "gnunet_block_plugin.h"
Include dependency graph for bg_bf.c:

Go to the source code of this file.

Data Structures

struct  BfGroupInternals
 Internal data structure for a block group. More...
 

Functions

static enum GNUNET_GenericReturnValue bf_group_serialize_cb (struct GNUNET_BLOCK_Group *bg, void **raw_data, size_t *raw_data_size)
 Serialize state of a block group. More...
 
static void bf_group_mark_seen_cb (struct GNUNET_BLOCK_Group *bg, const struct GNUNET_HashCode *seen_results, unsigned int seen_results_count)
 Mark elements as "seen" using a hash of the element. More...
 
static enum GNUNET_GenericReturnValue bf_group_merge_cb (struct GNUNET_BLOCK_Group *bg1, const struct GNUNET_BLOCK_Group *bg2)
 Merge two groups, if possible. More...
 
static void bf_group_destroy_cb (struct GNUNET_BLOCK_Group *bg)
 Destroy resources used by a block group. More...
 
struct GNUNET_BLOCK_GroupGNUNET_BLOCK_GROUP_bf_create (void *cls, size_t bf_size, unsigned int bf_k, enum GNUNET_BLOCK_Type type, const void *raw_data, size_t raw_data_size)
 Create a new block group that filters duplicates using a Bloom filter. More...
 
enum GNUNET_GenericReturnValue GNUNET_BLOCK_GROUP_bf_test_and_set (struct GNUNET_BLOCK_Group *bg, const struct GNUNET_HashCode *hc)
 Test if hc is contained in the Bloom filter of bg. More...
 
size_t GNUNET_BLOCK_GROUP_compute_bloomfilter_size (unsigned int entry_count, unsigned int k)
 How many bytes should a bloomfilter be if we have already seen entry_count responses? Sized so that do not have to re-size the filter too often (to keep it cheap). More...
 

Detailed Description

implementation of a block group using a Bloom filter to drop duplicate blocks

Author
Christian Grothoff

Definition in file bg_bf.c.

Function Documentation

◆ bf_group_serialize_cb()

static enum GNUNET_GenericReturnValue bf_group_serialize_cb ( struct GNUNET_BLOCK_Group bg,
void **  raw_data,
size_t *  raw_data_size 
)
static

Serialize state of a block group.

Parameters
bggroup to serialize
[out]raw_dataset to the serialized state
[out]raw_data_sizeset to the number of bytes in raw_data
Returns
GNUNET_OK on success, GNUNET_NO if serialization is not supported, GNUNET_SYSERR on error

Definition at line 64 of file bg_bf.c.

67{
68 struct BfGroupInternals *gi = bg->internal_cls;
69 void *raw;
70
71 raw = GNUNET_malloc (gi->bf_size + sizeof (uint32_t));
72 if (GNUNET_OK !=
74 raw + sizeof (uint32_t),
75 gi->bf_size))
76 {
78 GNUNET_break (0);
79 return GNUNET_SYSERR;
80 }
81 memcpy (raw,
82 &gi->bf_mutator,
83 sizeof (uint32_t));
84 *raw_data = raw;
85 *raw_data_size = gi->bf_size + sizeof (uint32_t);
86 return GNUNET_OK;
87}
static int raw
raw output
Definition: gnunet-gns.c:78
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_bloomfilter_get_raw_data(const struct GNUNET_CONTAINER_BloomFilter *bf, char *data, size_t size)
Copy the raw data of this Bloom filter into the given data array.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Internal data structure for a block group.
Definition: bg_bf.c:36
uint32_t bf_size
Size of bf.
Definition: bg_bf.c:50
uint32_t bf_mutator
Set from the nonce to mingle the hashes before going into the bf.
Definition: bg_bf.c:45
struct GNUNET_CONTAINER_BloomFilter * bf
A Bloom filter to weed out duplicate replies probabilistically.
Definition: bg_bf.c:40
void * internal_cls
Internal data structure of the plugin.

References BfGroupInternals::bf, BfGroupInternals::bf_mutator, BfGroupInternals::bf_size, GNUNET_break, GNUNET_CONTAINER_bloomfilter_get_raw_data(), GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_SYSERR, GNUNET_BLOCK_Group::internal_cls, and raw.

Referenced by GNUNET_BLOCK_GROUP_bf_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bf_group_mark_seen_cb()

static void bf_group_mark_seen_cb ( struct GNUNET_BLOCK_Group bg,
const struct GNUNET_HashCode seen_results,
unsigned int  seen_results_count 
)
static

Mark elements as "seen" using a hash of the element.

Not supported by all block plugins.

Parameters
bggroup to update
seen_resultsresults already seen
seen_results_countnumber of entries in seen_results

Definition at line 99 of file bg_bf.c.

102{
103 struct BfGroupInternals *gi = bg->internal_cls;
104
105 for (unsigned int i = 0; i < seen_results_count; i++)
106 {
107 struct GNUNET_HashCode mhash;
108
109 GNUNET_BLOCK_mingle_hash (&seen_results[i],
110 gi->bf_mutator,
111 &mhash);
113 &mhash);
114 }
115}
void GNUNET_BLOCK_mingle_hash(const struct GNUNET_HashCode *in, uint32_t mingle_number, struct GNUNET_HashCode *hc)
Mingle hash with the mingle_number to produce different bits.
Definition: block.c:96
void GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
A 512-bit hashcode.

References BfGroupInternals::bf, BfGroupInternals::bf_mutator, GNUNET_BLOCK_mingle_hash(), GNUNET_CONTAINER_bloomfilter_add(), and GNUNET_BLOCK_Group::internal_cls.

Referenced by GNUNET_BLOCK_GROUP_bf_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bf_group_merge_cb()

static enum GNUNET_GenericReturnValue bf_group_merge_cb ( struct GNUNET_BLOCK_Group bg1,
const struct GNUNET_BLOCK_Group bg2 
)
static

Merge two groups, if possible.

Not supported by all block plugins, can also fail if the nonces were different.

Parameters
bg1group to update
bg2group to merge into bg1
Returns
GNUNET_OK on success, GNUNET_NO if the nonces were different and thus we failed.

Definition at line 128 of file bg_bf.c.

130{
131 struct BfGroupInternals *gi1 = bg1->internal_cls;
132 struct BfGroupInternals *gi2 = bg2->internal_cls;
133
134 if (gi1->bf_mutator != gi2->bf_mutator)
135 return GNUNET_NO;
136 if (gi1->bf_size != gi2->bf_size)
137 return GNUNET_NO;
139 gi2->bf);
140 return GNUNET_OK;
141}
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_bloomfilter_or2(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_CONTAINER_BloomFilter *to_or)
"or" the entries of the given raw data array with the data of the given Bloom filter.
@ GNUNET_NO

References BfGroupInternals::bf, BfGroupInternals::bf_mutator, BfGroupInternals::bf_size, GNUNET_CONTAINER_bloomfilter_or2(), GNUNET_NO, GNUNET_OK, and GNUNET_BLOCK_Group::internal_cls.

Referenced by GNUNET_BLOCK_GROUP_bf_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bf_group_destroy_cb()

static void bf_group_destroy_cb ( struct GNUNET_BLOCK_Group bg)
static

Destroy resources used by a block group.

Parameters
bggroup to destroy, NULL is allowed

Definition at line 150 of file bg_bf.c.

151{
152 struct BfGroupInternals *gi = bg->internal_cls;
153
155 GNUNET_free (gi);
156 GNUNET_free (bg);
157}
void GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
Free the space associated with a filter in memory, flush to drive if needed (do not free the space on...

References BfGroupInternals::bf, GNUNET_CONTAINER_bloomfilter_free(), GNUNET_free, and GNUNET_BLOCK_Group::internal_cls.

Referenced by GNUNET_BLOCK_GROUP_bf_create().

Here is the call graph for this function:
Here is the caller graph for this function: