2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 // TODO check for overflows
40 /***********************************************************************
41 * WARNING: This section needs to be reviewed regarding the use of
42 * functions providing (pseudo)randomness!
43 ***********************************************************************/
45 // TODO care about invalid input of the caller (size 0 or less...)
49 * Reinitialise a previously initialised sampler element.
51 * @param sampler pointer to the memory that keeps the value.
54 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
56 sampler_el->is_empty = EMPTY;
58 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
59 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
60 &(sampler_el->auth_key.key),
61 GNUNET_CRYPTO_HASH_LENGTH);
64 /* Create a file(-name) to store internals to */
66 name_buf = auth_key_to_string (sampler_el->auth_key);
68 sampler_el->file_name = create_file (name_buf);
69 GNUNET_free (name_buf);
72 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
74 sampler_el->birth = GNUNET_TIME_absolute_get ();
75 sampler_el->num_peers = 0;
76 sampler_el->num_change = 0;
81 * Create a sampler element and initialise it.
83 * In this implementation this means choosing an auth_key for later use in
86 * @return a newly created RPS_SamplerElement which currently holds no id.
88 struct RPS_SamplerElement *
89 RPS_sampler_elem_create (void)
91 struct RPS_SamplerElement *s;
93 s = GNUNET_new (struct RPS_SamplerElement);
95 RPS_sampler_elem_reinit (s);
102 * Destroy a sampler element.
104 * @param sampler_elem the element to destroy
107 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
110 if (NULL != sampler_elem->file_name)
112 GNUNET_free (sampler_elem->file_name);
115 GNUNET_free (sampler_elem);
120 * Input an PeerID into the given sampler element.
122 * @param sampler the sampler the @a s_elem belongs to.
126 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
127 const struct GNUNET_PeerIdentity *other)
129 struct GNUNET_HashCode other_hash;
134 to_file (s_elem->file_name,
136 GNUNET_i2s_full (other));
139 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
141 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
142 GNUNET_i2s (&(s_elem->peer_id)));
146 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
148 sizeof(struct GNUNET_PeerIdentity),
151 if (EMPTY == s_elem->is_empty)
153 LOG (GNUNET_ERROR_TYPE_DEBUG,
154 "Got PeerID %s; Simply accepting (was empty previously).\n",
156 s_elem->peer_id = *other;
157 s_elem->peer_id_hash = other_hash;
159 s_elem->num_change++;
161 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
163 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
164 GNUNET_i2s (&s_elem->peer_id));
165 s_elem->peer_id = *other;
166 s_elem->peer_id_hash = other_hash;
168 s_elem->num_change++;
172 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
173 GNUNET_i2s (&s_elem->peer_id));
176 s_elem->is_empty = NOT_EMPTY;
179 to_file (s_elem->file_name,
181 GNUNET_i2s_full (&s_elem->peer_id));
186 * Initialise the min-wise independent function of the given sampler element.
188 * @param s_elem the sampler element
189 * @param auth_key the key to use
192 RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
193 struct GNUNET_CRYPTO_AuthKey auth_key)
195 s_elem->auth_key = auth_key;
198 /* Create a file(-name) to store internals to */
200 name_buf = auth_key_to_string (s_elem->auth_key);
202 s_elem->file_name = create_file (name_buf);
203 GNUNET_free (name_buf);
207 /* end of gnunet-service-rps.c */