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 * Input an PeerID into the given sampler element.
104 * @param sampler the sampler the @a s_elem belongs to.
108 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
109 const struct GNUNET_PeerIdentity *other)
111 struct GNUNET_HashCode other_hash;
115 to_file (s_elem->file_name,
117 GNUNET_i2s_full (other));
119 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
121 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
122 GNUNET_i2s (&(s_elem->peer_id)));
126 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
128 sizeof(struct GNUNET_PeerIdentity),
131 if (EMPTY == s_elem->is_empty)
133 LOG (GNUNET_ERROR_TYPE_DEBUG,
134 "Got PeerID %s; Simply accepting (was empty previously).\n",
136 s_elem->peer_id = *other;
137 s_elem->peer_id_hash = other_hash;
139 s_elem->num_change++;
141 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
143 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
144 GNUNET_i2s (&s_elem->peer_id));
145 s_elem->peer_id = *other;
146 s_elem->peer_id_hash = other_hash;
148 s_elem->num_change++;
152 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
153 GNUNET_i2s (&s_elem->peer_id));
156 s_elem->is_empty = NOT_EMPTY;
158 to_file (s_elem->file_name,
160 GNUNET_i2s_full (&s_elem->peer_id));
164 * Initialise the min-wise independent function of the given sampler element.
166 * @param s_elem the sampler element
167 * @param auth_key the key to use
170 RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
171 struct GNUNET_CRYPTO_AuthKey auth_key)
173 s_elem->auth_key = auth_key;
176 /* Create a file(-name) to store internals to */
178 name_buf = auth_key_to_string (s_elem->auth_key);
180 s_elem->file_name = create_file (name_buf);
181 GNUNET_free (name_buf);
185 /* end of gnunet-service-rps.c */