GNUnet  0.10.x
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 
49 void
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 
75 struct RPS_SamplerElement *
77 {
78  struct RPS_SamplerElement *s;
79 
80  s = GNUNET_new(struct RPS_SamplerElement);
81 
83 
84  return s;
85 }
86 
87 
93 void
95 {
96  GNUNET_free(sampler_elem);
97 }
98 
99 
106 void
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, &sampler_elem->peer_id_hash))
137  {
138  LOG(GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
139  GNUNET_i2s(&sampler_elem->peer_id));
140  sampler_elem->peer_id = *new_ID;
141  sampler_elem->peer_id_hash = other_hash;
142 
143  sampler_elem->num_change++;
144  }
145  else
146  {
147  LOG(GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
148  GNUNET_i2s(&sampler_elem->peer_id));
149  }
150  }
151  sampler_elem->is_empty = NOT_EMPTY;
152 }
153 
160 void
162  struct GNUNET_CRYPTO_AuthKey auth_key)
163 {
164  sampler_elem->auth_key = auth_key;
165 }
166 
167 /* end of gnunet-service-rps.c */
uint32_t num_peers
How many times a PeerID was put in this sampler.
struct GNUNET_TIME_Absolute birth
&#39;Birth&#39;
#define GNUNET_CRYPTO_HASH_LENGTH
Length of a hash value.
A sampler element sampling one PeerID at a time.
void RPS_sampler_elem_destroy(struct RPS_SamplerElement *sampler_elem)
Destroy a sampler element.
Some utils faciliating the view into the internals for the sampler needed for evaluation.
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.
sampler element implementation
struct RPS_SamplerElement * RPS_sampler_elem_create(void)
Create a sampler element and initialise it.
struct GNUNET_TIME_Absolute last_client_request
Time of last request.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void RPS_sampler_elem_reinit(struct RPS_SamplerElement *sampler_elem)
Reinitialise a previously initialised sampler element.
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:418
#define LOG(kind,...)
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
enum RPS_SamplerEmpty is_empty
Flag that indicates that we are not holding a valid PeerID right now.
struct GNUNET_PeerIdentity peer_id
The PeerID this sampler currently samples.
unsigned char key[(512/8)]
A 512-bit hashcode.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
struct GNUNET_HashCode peer_id_hash
The according hash value of this PeerID.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
The identity of the host (wraps the signing key of the peer).
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:278
struct GNUNET_CRYPTO_AuthKey auth_key
Min-wise linear permutation used by this sampler.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
void RPS_sampler_elem_next(struct RPS_SamplerElement *sampler_elem, const struct GNUNET_PeerIdentity *new_ID)
Update a sampler element with a PeerID.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
uint32_t num_change
How many times this sampler changed the peer_id.
type for (message) authentication keys
#define GNUNET_free(ptr)
Wrapper around free.
High-quality operations are desired.