GNUnet 0.21.1
gnunet-service-rps_sampler_elem.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C)
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 */
20
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
30
31#include <inttypes.h>
32
33#include "rps-test_util.h"
34
35#define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler_elem", __VA_ARGS__)
36
37
38/***********************************************************************
39* WARNING: This section needs to be reviewed regarding the use of
40* functions providing (pseudo)randomness!
41***********************************************************************/
42
43
49void
51{
52 sampler_elem->is_empty = EMPTY;
53
54 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
56 &(sampler_elem->auth_key.key),
58
60
61 sampler_elem->birth = GNUNET_TIME_absolute_get ();
62 sampler_elem->num_peers = 0;
63 sampler_elem->num_change = 0;
64}
65
66
75struct RPS_SamplerElement *
77{
78 struct RPS_SamplerElement *s;
79
80 s = GNUNET_new (struct RPS_SamplerElement);
81
83
84 return s;
85}
86
87
93void
95{
96 GNUNET_free (sampler_elem);
97}
98
99
106void
108 const struct GNUNET_PeerIdentity *new_ID)
109{
110 struct GNUNET_HashCode other_hash;
111
112 sampler_elem->num_peers++;
113
114 if (0 == GNUNET_memcmp (new_ID, &(sampler_elem->peer_id)))
115 {
116 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
117 GNUNET_i2s (&(sampler_elem->peer_id)));
118 }
119 else
120 {
121 GNUNET_CRYPTO_hmac (&sampler_elem->auth_key,
122 new_ID,
123 sizeof(struct GNUNET_PeerIdentity),
124 &other_hash);
125
126 if (EMPTY == sampler_elem->is_empty)
127 {
129 "Got PeerID %s; Simply accepting (was empty previously).\n",
130 GNUNET_i2s (new_ID));
131 sampler_elem->peer_id = *new_ID;
132 sampler_elem->peer_id_hash = other_hash;
133
134 sampler_elem->num_change++;
135 }
136 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash,
137 &sampler_elem->peer_id_hash))
138 {
139 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
140 GNUNET_i2s (&sampler_elem->peer_id));
141 sampler_elem->peer_id = *new_ID;
142 sampler_elem->peer_id_hash = other_hash;
143
144 sampler_elem->num_change++;
145 }
146 else
147 {
148 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
149 GNUNET_i2s (&sampler_elem->peer_id));
150 }
151 }
152 sampler_elem->is_empty = NOT_EMPTY;
153}
154
155
162void
164 struct GNUNET_CRYPTO_AuthKey auth_key)
165{
166 sampler_elem->auth_key = auth_key;
167}
168
169
170/* end of gnunet-service-rps.c */
void RPS_sampler_elem_set(struct RPS_SamplerElement *sampler_elem, struct GNUNET_CRYPTO_AuthKey auth_key)
Set the min-wise independent function of the given sampler element.
void RPS_sampler_elem_reinit(struct RPS_SamplerElement *sampler_elem)
Reinitialise a previously initialised sampler element.
void RPS_sampler_elem_destroy(struct RPS_SamplerElement *sampler_elem)
Destroy a sampler element.
struct RPS_SamplerElement * RPS_sampler_elem_create(void)
Create a sampler element and initialise it.
#define LOG(kind,...)
void RPS_sampler_elem_next(struct RPS_SamplerElement *sampler_elem, const struct GNUNET_PeerIdentity *new_ID)
Update a sampler element with a PeerID.
sampler element implementation
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
@ GNUNET_CRYPTO_QUALITY_STRONG
High-quality operations are desired.
void GNUNET_CRYPTO_hmac(const struct GNUNET_CRYPTO_AuthKey *key, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104)
Definition: crypto_hash.c:330
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:221
#define GNUNET_CRYPTO_HASH_LENGTH
Length of a hash value.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
Some utils facilitating the view into the internals for the sampler needed for evaluation.
type for (message) authentication keys
unsigned char key[(512/8)]
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
A sampler element sampling one PeerID at a time.
struct GNUNET_TIME_Absolute last_client_request
Time of last request.
struct GNUNET_TIME_Absolute birth
'Birth'
struct GNUNET_CRYPTO_AuthKey auth_key
Min-wise linear permutation used by this sampler.
uint32_t num_peers
How many times a PeerID was put in this sampler.
struct GNUNET_HashCode peer_id_hash
The according hash value of this PeerID.
uint32_t num_change
How many times this sampler changed the peer_id.
struct GNUNET_PeerIdentity peer_id
The PeerID this sampler currently samples.
enum RPS_SamplerEmpty is_empty
Flag that indicates that we are not holding a valid PeerID right now.