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 // TODO check for overflows
38 /***********************************************************************
39 * WARNING: This section needs to be reviewed regarding the use of
40 * functions providing (pseudo)randomness!
41 ***********************************************************************/
43 // TODO care about invalid input of the caller (size 0 or less...)
47 * Reinitialise a previously initialised sampler element.
49 * @param sampler pointer to the memory that keeps the value.
52 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
54 sampler_el->is_empty = EMPTY;
56 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
57 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
58 &(sampler_el->auth_key.key),
59 GNUNET_CRYPTO_HASH_LENGTH);
62 /* Create a file(-name) to store internals to */
64 name_buf = auth_key_to_string (sampler_el->auth_key);
66 sampler_el->file_name = create_file (name_buf);
67 GNUNET_free (name_buf);
70 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
72 sampler_el->birth = GNUNET_TIME_absolute_get ();
73 sampler_el->num_peers = 0;
74 sampler_el->num_change = 0;
79 * Create a sampler element and initialise it.
81 * In this implementation this means choosing an auth_key for later use in
84 * @return a newly created RPS_SamplerElement which currently holds no id.
86 struct RPS_SamplerElement *
87 RPS_sampler_elem_create (void)
89 struct RPS_SamplerElement *s;
91 s = GNUNET_new (struct RPS_SamplerElement);
93 RPS_sampler_elem_reinit (s);
100 * Destroy a sampler element.
102 * @param sampler_elem the element to destroy
105 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
108 if (NULL != sampler_elem->file_name)
110 GNUNET_free (sampler_elem->file_name);
113 GNUNET_free (sampler_elem);
118 * Input an PeerID into the given sampler element.
120 * @param sampler the sampler the @a s_elem belongs to.
124 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
125 const struct GNUNET_PeerIdentity *other)
127 struct GNUNET_HashCode other_hash;
132 to_file (s_elem->file_name,
134 GNUNET_i2s_full (other));
137 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
139 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
140 GNUNET_i2s (&(s_elem->peer_id)));
144 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
146 sizeof(struct GNUNET_PeerIdentity),
149 if (EMPTY == s_elem->is_empty)
151 LOG (GNUNET_ERROR_TYPE_DEBUG,
152 "Got PeerID %s; Simply accepting (was empty previously).\n",
154 s_elem->peer_id = *other;
155 s_elem->peer_id_hash = other_hash;
157 s_elem->num_change++;
159 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
161 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
162 GNUNET_i2s (&s_elem->peer_id));
163 s_elem->peer_id = *other;
164 s_elem->peer_id_hash = other_hash;
166 s_elem->num_change++;
170 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
171 GNUNET_i2s (&s_elem->peer_id));
174 s_elem->is_empty = NOT_EMPTY;
177 to_file (s_elem->file_name,
179 GNUNET_i2s_full (&s_elem->peer_id));
184 * Initialise the min-wise independent function of the given sampler element.
186 * @param s_elem the sampler element
187 * @param auth_key the key to use
190 RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
191 struct GNUNET_CRYPTO_AuthKey auth_key)
193 s_elem->auth_key = auth_key;
196 /* Create a file(-name) to store internals to */
198 name_buf = auth_key_to_string (s_elem->auth_key);
200 s_elem->file_name = create_file (name_buf);
201 GNUNET_free (name_buf);
205 /* end of gnunet-service-rps.c */