-rps doxygen
[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
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.
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      General Public License for more details.
14
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.
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 // TODO check for overflows
39
40 /***********************************************************************
41  * WARNING: This section needs to be reviewed regarding the use of
42  * functions providing (pseudo)randomness!
43 ***********************************************************************/
44
45 // TODO care about invalid input of the caller (size 0 or less...)
46
47
48 /**
49  * Reinitialise a previously initialised sampler element.
50  *
51  * @param sampler pointer to the memory that keeps the value.
52  */
53 void
54 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
55 {
56   sampler_el->is_empty = EMPTY;
57
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);
62
63   #ifdef TO_FILE
64   /* Create a file(-name) to store internals to */
65   char *name_buf;
66   name_buf = auth_key_to_string (sampler_el->auth_key);
67
68   sampler_el->file_name = create_file (name_buf);
69   GNUNET_free (name_buf);
70   #endif /* TO_FILE */
71
72   sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
73
74   sampler_el->birth = GNUNET_TIME_absolute_get ();
75   sampler_el->num_peers = 0;
76   sampler_el->num_change = 0;
77 }
78
79
80 /**
81  * Create a sampler element and initialise it.
82  *
83  * In this implementation this means choosing an auth_key for later use in
84  * a hmac at random.
85  *
86  * @return a newly created RPS_SamplerElement which currently holds no id.
87  */
88 struct RPS_SamplerElement *
89 RPS_sampler_elem_create (void)
90 {
91   struct RPS_SamplerElement *s;
92
93   s = GNUNET_new (struct RPS_SamplerElement);
94
95   RPS_sampler_elem_reinit (s);
96
97   return s;
98 }
99
100
101 /**
102  * Input an PeerID into the given sampler element.
103  *
104  * @param sampler the sampler the @a s_elem belongs to.
105  *                Needed to know the
106  */
107 void
108 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
109                        const struct GNUNET_PeerIdentity *other)
110 {
111   struct GNUNET_HashCode other_hash;
112
113   s_elem->num_peers++;
114
115   to_file (s_elem->file_name,
116            "Got id %s",
117            GNUNET_i2s_full (other));
118
119   if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
120   {
121     LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
122         GNUNET_i2s (&(s_elem->peer_id)));
123   }
124   else
125   {
126     GNUNET_CRYPTO_hmac(&s_elem->auth_key,
127         other,
128         sizeof(struct GNUNET_PeerIdentity),
129         &other_hash);
130
131     if (EMPTY == s_elem->is_empty)
132     {
133       LOG (GNUNET_ERROR_TYPE_DEBUG,
134            "Got PeerID %s; Simply accepting (was empty previously).\n",
135            GNUNET_i2s(other));
136       s_elem->peer_id = *other;
137       s_elem->peer_id_hash = other_hash;
138
139       s_elem->num_change++;
140     }
141     else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
142     {
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;
147
148       s_elem->num_change++;
149     }
150     else
151     {
152       LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
153           GNUNET_i2s (&s_elem->peer_id));
154     }
155   }
156   s_elem->is_empty = NOT_EMPTY;
157
158   to_file (s_elem->file_name,
159            "Now holding %s",
160            GNUNET_i2s_full (&s_elem->peer_id));
161 }
162
163 /**
164  * Initialise the min-wise independent function of the given sampler element.
165  *
166  * @param s_elem the sampler element
167  * @param auth_key the key to use
168  */
169 void
170 RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
171                       struct GNUNET_CRYPTO_AuthKey auth_key)
172 {
173   s_elem->auth_key = auth_key;
174
175   #ifdef TO_FILE
176   /* Create a file(-name) to store internals to */
177   char *name_buf;
178   name_buf = auth_key_to_string (s_elem->auth_key);
179
180   s_elem->file_name = create_file (name_buf);
181   GNUNET_free (name_buf);
182   #endif /* TO_FILE */
183 }
184
185 /* end of gnunet-service-rps.c */