2 This file is part of GNUnet.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file rps/gnunet-service-rps_sampler.c
23 * @brief sampler implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
29 #include "gnunet-service-rps_sampler_elem.h"
33 #include "rps-test_util.h"
35 #define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler_elem", __VA_ARGS__)
38 /***********************************************************************
39 * WARNING: This section needs to be reviewed regarding the use of
40 * functions providing (pseudo)randomness!
41 ***********************************************************************/
45 * Reinitialise a previously initialised sampler element.
47 * @param sampler_el The sampler element to (re-) initialise
50 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_elem)
52 sampler_elem->is_empty = EMPTY;
54 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
55 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
56 &(sampler_elem->auth_key.key),
57 GNUNET_CRYPTO_HASH_LENGTH);
59 sampler_elem->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
61 sampler_elem->birth = GNUNET_TIME_absolute_get ();
62 sampler_elem->num_peers = 0;
63 sampler_elem->num_change = 0;
68 * Create a sampler element and initialise it.
70 * In this implementation this means choosing an auth_key for later use in
73 * @return a newly created RPS_SamplerElement which currently holds no id.
75 struct RPS_SamplerElement *
76 RPS_sampler_elem_create (void)
78 struct RPS_SamplerElement *s;
80 s = GNUNET_new (struct RPS_SamplerElement);
82 RPS_sampler_elem_reinit (s);
89 * Destroy a sampler element.
91 * @param sampler_elem the element to destroy
94 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
96 GNUNET_free (sampler_elem);
101 * Update a sampler element with a PeerID
103 * @param sampler_elem The sampler element to update
104 * @param new_ID The PeerID to update with
107 RPS_sampler_elem_next (struct RPS_SamplerElement *sampler_elem,
108 const struct GNUNET_PeerIdentity *new_ID)
110 struct GNUNET_HashCode other_hash;
112 sampler_elem->num_peers++;
114 if (0 == GNUNET_memcmp (new_ID, &(sampler_elem->peer_id)))
116 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
117 GNUNET_i2s (&(sampler_elem->peer_id)));
121 GNUNET_CRYPTO_hmac (&sampler_elem->auth_key,
123 sizeof(struct GNUNET_PeerIdentity),
126 if (EMPTY == sampler_elem->is_empty)
128 LOG (GNUNET_ERROR_TYPE_DEBUG,
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;
134 sampler_elem->num_change++;
136 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash,
137 &sampler_elem->peer_id_hash))
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;
144 sampler_elem->num_change++;
148 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
149 GNUNET_i2s (&sampler_elem->peer_id));
152 sampler_elem->is_empty = NOT_EMPTY;
157 * Set the min-wise independent function of the given sampler element.
159 * @param sampler_elem the sampler element
160 * @param auth_key the key to use
163 RPS_sampler_elem_set (struct RPS_SamplerElement *sampler_elem,
164 struct GNUNET_CRYPTO_AuthKey auth_key)
166 sampler_elem->auth_key = auth_key;
170 /* end of gnunet-service-rps.c */