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/>.
20 * @file rps/gnunet-service-rps_sampler.c
21 * @brief sampler implementation
22 * @author Julius Bünger
25 #include "gnunet_util_lib.h"
27 #include "gnunet-service-rps_sampler_elem.h"
31 #include "rps-test_util.h"
33 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler_elem",__VA_ARGS__)
36 /***********************************************************************
37 * WARNING: This section needs to be reviewed regarding the use of
38 * functions providing (pseudo)randomness!
39 ***********************************************************************/
43 * Reinitialise a previously initialised sampler element.
45 * @param sampler_el The sampler element to (re-) initialise
48 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_elem)
50 sampler_elem->is_empty = EMPTY;
52 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
53 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
54 &(sampler_elem->auth_key.key),
55 GNUNET_CRYPTO_HASH_LENGTH);
57 sampler_elem->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
59 sampler_elem->birth = GNUNET_TIME_absolute_get ();
60 sampler_elem->num_peers = 0;
61 sampler_elem->num_change = 0;
66 * Create a sampler element and initialise it.
68 * In this implementation this means choosing an auth_key for later use in
71 * @return a newly created RPS_SamplerElement which currently holds no id.
73 struct RPS_SamplerElement *
74 RPS_sampler_elem_create (void)
76 struct RPS_SamplerElement *s;
78 s = GNUNET_new (struct RPS_SamplerElement);
80 RPS_sampler_elem_reinit (s);
87 * Destroy a sampler element.
89 * @param sampler_elem the element to destroy
92 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
94 GNUNET_free (sampler_elem);
99 * Update a sampler element with a PeerID
101 * @param sampler_elem The sampler element to update
102 * @param new_ID The PeerID to update with
105 RPS_sampler_elem_next (struct RPS_SamplerElement *sampler_elem,
106 const struct GNUNET_PeerIdentity *new_ID)
108 struct GNUNET_HashCode other_hash;
110 sampler_elem->num_peers++;
112 if (0 == GNUNET_CRYPTO_cmp_peer_identity (new_ID, &(sampler_elem->peer_id)))
114 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
115 GNUNET_i2s (&(sampler_elem->peer_id)));
119 GNUNET_CRYPTO_hmac(&sampler_elem->auth_key,
121 sizeof(struct GNUNET_PeerIdentity),
124 if (EMPTY == sampler_elem->is_empty)
126 LOG (GNUNET_ERROR_TYPE_DEBUG,
127 "Got PeerID %s; Simply accepting (was empty previously).\n",
129 sampler_elem->peer_id = *new_ID;
130 sampler_elem->peer_id_hash = other_hash;
132 sampler_elem->num_change++;
134 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &sampler_elem->peer_id_hash))
136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
137 GNUNET_i2s (&sampler_elem->peer_id));
138 sampler_elem->peer_id = *new_ID;
139 sampler_elem->peer_id_hash = other_hash;
141 sampler_elem->num_change++;
145 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
146 GNUNET_i2s (&sampler_elem->peer_id));
149 sampler_elem->is_empty = NOT_EMPTY;
153 * Set the min-wise independent function of the given sampler element.
155 * @param sampler_elem the sampler element
156 * @param auth_key the key to use
159 RPS_sampler_elem_set (struct RPS_SamplerElement *sampler_elem,
160 struct GNUNET_CRYPTO_AuthKey auth_key)
162 sampler_elem->auth_key = auth_key;
165 /* end of gnunet-service-rps.c */