7d6c7f8452a04bc284915acc44b4c18c6dd6ccf9
[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 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
16 /**
17  * @file rps/gnunet-service-rps_sampler.c
18  * @brief sampler implementation
19  * @author Julius Bünger
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23
24 #include "gnunet-service-rps_sampler_elem.h"
25
26 #include <inttypes.h>
27
28 #include "rps-test_util.h"
29
30 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler_elem",__VA_ARGS__)
31
32
33 // TODO check for overflows
34
35 /***********************************************************************
36  * WARNING: This section needs to be reviewed regarding the use of
37  * functions providing (pseudo)randomness!
38 ***********************************************************************/
39
40 // TODO care about invalid input of the caller (size 0 or less...)
41
42
43 /**
44  * Reinitialise a previously initialised sampler element.
45  *
46  * @param sampler pointer to the memory that keeps the value.
47  */
48 void
49 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
50 {
51   sampler_el->is_empty = EMPTY;
52
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);
57
58   #ifdef TO_FILE
59   /* Create a file(-name) to store internals to */
60   char *name_buf;
61   name_buf = auth_key_to_string (sampler_el->auth_key);
62
63   sampler_el->file_name = create_file (name_buf);
64   GNUNET_free (name_buf);
65   #endif /* TO_FILE */
66
67   sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
68
69   sampler_el->birth = GNUNET_TIME_absolute_get ();
70   sampler_el->num_peers = 0;
71   sampler_el->num_change = 0;
72 }
73
74
75 /**
76  * Create a sampler element and initialise it.
77  *
78  * In this implementation this means choosing an auth_key for later use in
79  * a hmac at random.
80  *
81  * @return a newly created RPS_SamplerElement which currently holds no id.
82  */
83 struct RPS_SamplerElement *
84 RPS_sampler_elem_create (void)
85 {
86   struct RPS_SamplerElement *s;
87
88   s = GNUNET_new (struct RPS_SamplerElement);
89
90   RPS_sampler_elem_reinit (s);
91
92   return s;
93 }
94
95
96 /**
97  * Destroy a sampler element.
98  *
99  * @param sampler_elem the element to destroy
100  */
101 void
102 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
103 {
104   #ifdef TO_FILE
105   if (NULL != sampler_elem->file_name)
106   {
107     GNUNET_free (sampler_elem->file_name);
108   }
109   #endif /* TO_FILE */
110   GNUNET_free (sampler_elem);
111 }
112
113
114 /**
115  * Input an PeerID into the given sampler element.
116  *
117  * @param sampler the sampler the @a s_elem belongs to.
118  *                Needed to know the
119  */
120 void
121 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
122                        const struct GNUNET_PeerIdentity *other)
123 {
124   struct GNUNET_HashCode other_hash;
125
126   s_elem->num_peers++;
127
128   #ifdef TO_FILE
129   to_file (s_elem->file_name,
130            "Got id %s",
131            GNUNET_i2s_full (other));
132   #endif /* TO_FILE */
133
134   if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
135   {
136     LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
137         GNUNET_i2s (&(s_elem->peer_id)));
138   }
139   else
140   {
141     GNUNET_CRYPTO_hmac(&s_elem->auth_key,
142         other,
143         sizeof(struct GNUNET_PeerIdentity),
144         &other_hash);
145
146     if (EMPTY == s_elem->is_empty)
147     {
148       LOG (GNUNET_ERROR_TYPE_DEBUG,
149            "Got PeerID %s; Simply accepting (was empty previously).\n",
150            GNUNET_i2s(other));
151       s_elem->peer_id = *other;
152       s_elem->peer_id_hash = other_hash;
153
154       s_elem->num_change++;
155     }
156     else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
157     {
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;
162
163       s_elem->num_change++;
164     }
165     else
166     {
167       LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
168           GNUNET_i2s (&s_elem->peer_id));
169     }
170   }
171   s_elem->is_empty = NOT_EMPTY;
172
173   #ifdef TO_FILE
174   to_file (s_elem->file_name,
175            "Now holding %s",
176            GNUNET_i2s_full (&s_elem->peer_id));
177   #endif /* TO_FILE */
178 }
179
180 /**
181  * Initialise the min-wise independent function of the given sampler element.
182  *
183  * @param s_elem the sampler element
184  * @param auth_key the key to use
185  */
186 void
187 RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
188                       struct GNUNET_CRYPTO_AuthKey auth_key)
189 {
190   s_elem->auth_key = auth_key;
191
192   #ifdef TO_FILE
193   /* Create a file(-name) to store internals to */
194   char *name_buf;
195   name_buf = auth_key_to_string (s_elem->auth_key);
196
197   s_elem->file_name = create_file (name_buf);
198   GNUNET_free (name_buf);
199   #endif /* TO_FILE */
200 }
201
202 /* end of gnunet-service-rps.c */