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