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 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.
17 * @file rps/gnunet-service-rps_sampler.c
18 * @brief sampler implementation
19 * @author Julius Bünger
22 #include "gnunet_util_lib.h"
24 #include "gnunet-service-rps_sampler_elem.h"
28 #include "rps-test_util.h"
30 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler_elem",__VA_ARGS__)
33 // TODO check for overflows
35 /***********************************************************************
36 * WARNING: This section needs to be reviewed regarding the use of
37 * functions providing (pseudo)randomness!
38 ***********************************************************************/
40 // TODO care about invalid input of the caller (size 0 or less...)
44 * Reinitialise a previously initialised sampler element.
46 * @param sampler pointer to the memory that keeps the value.
49 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
51 sampler_el->is_empty = EMPTY;
53 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
54 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
55 &(sampler_el->auth_key.key),
56 GNUNET_CRYPTO_HASH_LENGTH);
59 /* Create a file(-name) to store internals to */
61 name_buf = auth_key_to_string (sampler_el->auth_key);
63 sampler_el->file_name = create_file (name_buf);
64 GNUNET_free (name_buf);
67 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
69 sampler_el->birth = GNUNET_TIME_absolute_get ();
70 sampler_el->num_peers = 0;
71 sampler_el->num_change = 0;
76 * Create a sampler element and initialise it.
78 * In this implementation this means choosing an auth_key for later use in
81 * @return a newly created RPS_SamplerElement which currently holds no id.
83 struct RPS_SamplerElement *
84 RPS_sampler_elem_create (void)
86 struct RPS_SamplerElement *s;
88 s = GNUNET_new (struct RPS_SamplerElement);
90 RPS_sampler_elem_reinit (s);
97 * Destroy a sampler element.
99 * @param sampler_elem the element to destroy
102 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
105 if (NULL != sampler_elem->file_name)
107 GNUNET_free (sampler_elem->file_name);
110 GNUNET_free (sampler_elem);
115 * Input an PeerID into the given sampler element.
117 * @param sampler the sampler the @a s_elem belongs to.
121 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
122 const struct GNUNET_PeerIdentity *other)
124 struct GNUNET_HashCode other_hash;
129 to_file (s_elem->file_name,
131 GNUNET_i2s_full (other));
134 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
137 GNUNET_i2s (&(s_elem->peer_id)));
141 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
143 sizeof(struct GNUNET_PeerIdentity),
146 if (EMPTY == s_elem->is_empty)
148 LOG (GNUNET_ERROR_TYPE_DEBUG,
149 "Got PeerID %s; Simply accepting (was empty previously).\n",
151 s_elem->peer_id = *other;
152 s_elem->peer_id_hash = other_hash;
154 s_elem->num_change++;
156 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
158 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
159 GNUNET_i2s (&s_elem->peer_id));
160 s_elem->peer_id = *other;
161 s_elem->peer_id_hash = other_hash;
163 s_elem->num_change++;
167 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
168 GNUNET_i2s (&s_elem->peer_id));
171 s_elem->is_empty = NOT_EMPTY;
174 to_file (s_elem->file_name,
176 GNUNET_i2s_full (&s_elem->peer_id));
181 * Initialise the min-wise independent function of the given sampler element.
183 * @param s_elem the sampler element
184 * @param auth_key the key to use
187 RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
188 struct GNUNET_CRYPTO_AuthKey auth_key)
190 s_elem->auth_key = auth_key;
193 /* Create a file(-name) to store internals to */
195 name_buf = auth_key_to_string (s_elem->auth_key);
197 s_elem->file_name = create_file (name_buf);
198 GNUNET_free (name_buf);
202 /* end of gnunet-service-rps.c */