error handling
[oweals/gnunet.git] / src / rps / gnunet-service-rps_sampler_elem.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file rps/gnunet-service-rps_sampler.c
23  * @brief sampler implementation
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29 #include "gnunet-service-rps_sampler_elem.h"
30
31 #include <inttypes.h>
32
33 #include "rps-test_util.h"
34
35 #define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler_elem", __VA_ARGS__)
36
37
38 /***********************************************************************
39 * WARNING: This section needs to be reviewed regarding the use of
40 * functions providing (pseudo)randomness!
41 ***********************************************************************/
42
43
44 /**
45  * Reinitialise a previously initialised sampler element.
46  *
47  * @param sampler_el The sampler element to (re-) initialise
48  */
49 void
50 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_elem)
51 {
52   sampler_elem->is_empty = EMPTY;
53
54   // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
55   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
56                               &(sampler_elem->auth_key.key),
57                               GNUNET_CRYPTO_HASH_LENGTH);
58
59   sampler_elem->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
60
61   sampler_elem->birth = GNUNET_TIME_absolute_get ();
62   sampler_elem->num_peers = 0;
63   sampler_elem->num_change = 0;
64 }
65
66
67 /**
68  * Create a sampler element and initialise it.
69  *
70  * In this implementation this means choosing an auth_key for later use in
71  * a hmac at random.
72  *
73  * @return a newly created RPS_SamplerElement which currently holds no id.
74  */
75 struct RPS_SamplerElement *
76 RPS_sampler_elem_create (void)
77 {
78   struct RPS_SamplerElement *s;
79
80   s = GNUNET_new (struct RPS_SamplerElement);
81
82   RPS_sampler_elem_reinit (s);
83
84   return s;
85 }
86
87
88 /**
89  * Destroy a sampler element.
90  *
91  * @param sampler_elem the element to destroy
92  */
93 void
94 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
95 {
96   GNUNET_free (sampler_elem);
97 }
98
99
100 /**
101  * Update a sampler element with a PeerID
102  *
103  * @param sampler_elem The sampler element to update
104  * @param new_ID The PeerID to update with
105  */
106 void
107 RPS_sampler_elem_next (struct RPS_SamplerElement *sampler_elem,
108                        const struct GNUNET_PeerIdentity *new_ID)
109 {
110   struct GNUNET_HashCode other_hash;
111
112   sampler_elem->num_peers++;
113
114   if (0 == GNUNET_memcmp (new_ID, &(sampler_elem->peer_id)))
115   {
116     LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
117          GNUNET_i2s (&(sampler_elem->peer_id)));
118   }
119   else
120   {
121     GNUNET_CRYPTO_hmac (&sampler_elem->auth_key,
122                         new_ID,
123                         sizeof(struct GNUNET_PeerIdentity),
124                         &other_hash);
125
126     if (EMPTY == sampler_elem->is_empty)
127     {
128       LOG (GNUNET_ERROR_TYPE_DEBUG,
129            "Got PeerID %s; Simply accepting (was empty previously).\n",
130            GNUNET_i2s (new_ID));
131       sampler_elem->peer_id = *new_ID;
132       sampler_elem->peer_id_hash = other_hash;
133
134       sampler_elem->num_change++;
135     }
136     else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash,
137                                          &sampler_elem->peer_id_hash))
138     {
139       LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
140            GNUNET_i2s (&sampler_elem->peer_id));
141       sampler_elem->peer_id = *new_ID;
142       sampler_elem->peer_id_hash = other_hash;
143
144       sampler_elem->num_change++;
145     }
146     else
147     {
148       LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
149            GNUNET_i2s (&sampler_elem->peer_id));
150     }
151   }
152   sampler_elem->is_empty = NOT_EMPTY;
153 }
154
155
156 /**
157  * Set the min-wise independent function of the given sampler element.
158  *
159  * @param sampler_elem the sampler element
160  * @param auth_key the key to use
161  */
162 void
163 RPS_sampler_elem_set (struct RPS_SamplerElement *sampler_elem,
164                       struct GNUNET_CRYPTO_AuthKey auth_key)
165 {
166   sampler_elem->auth_key = auth_key;
167 }
168
169
170 /* end of gnunet-service-rps.c */