rps sampler_elem test: add to Makefile
[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  * Destroy a sampler element.
103  *
104  * @param sampler_elem the element to destroy
105  */
106 void
107 RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
108 {
109   #ifdef TO_FILE
110   if (NULL != sampler_elem->file_name)
111   {
112     GNUNET_free (sampler_elem->file_name);
113   }
114   #endif /* TO_FILE */
115   GNUNET_free (sampler_elem);
116 }
117
118
119 /**
120  * Input an PeerID into the given sampler element.
121  *
122  * @param sampler the sampler the @a s_elem belongs to.
123  *                Needed to know the
124  */
125 void
126 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
127                        const struct GNUNET_PeerIdentity *other)
128 {
129   struct GNUNET_HashCode other_hash;
130
131   s_elem->num_peers++;
132
133   #ifdef TO_FILE
134   to_file (s_elem->file_name,
135            "Got id %s",
136            GNUNET_i2s_full (other));
137   #endif /* TO_FILE */
138
139   if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
140   {
141     LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
142         GNUNET_i2s (&(s_elem->peer_id)));
143   }
144   else
145   {
146     GNUNET_CRYPTO_hmac(&s_elem->auth_key,
147         other,
148         sizeof(struct GNUNET_PeerIdentity),
149         &other_hash);
150
151     if (EMPTY == s_elem->is_empty)
152     {
153       LOG (GNUNET_ERROR_TYPE_DEBUG,
154            "Got PeerID %s; Simply accepting (was empty previously).\n",
155            GNUNET_i2s(other));
156       s_elem->peer_id = *other;
157       s_elem->peer_id_hash = other_hash;
158
159       s_elem->num_change++;
160     }
161     else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
162     {
163       LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
164           GNUNET_i2s (&s_elem->peer_id));
165       s_elem->peer_id = *other;
166       s_elem->peer_id_hash = other_hash;
167
168       s_elem->num_change++;
169     }
170     else
171     {
172       LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
173           GNUNET_i2s (&s_elem->peer_id));
174     }
175   }
176   s_elem->is_empty = NOT_EMPTY;
177
178   #ifdef TO_FILE
179   to_file (s_elem->file_name,
180            "Now holding %s",
181            GNUNET_i2s_full (&s_elem->peer_id));
182   #endif /* TO_FILE */
183 }
184
185 /**
186  * Initialise the min-wise independent function of the given sampler element.
187  *
188  * @param s_elem the sampler element
189  * @param auth_key the key to use
190  */
191 void
192 RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
193                       struct GNUNET_CRYPTO_AuthKey auth_key)
194 {
195   s_elem->auth_key = auth_key;
196
197   #ifdef TO_FILE
198   /* Create a file(-name) to store internals to */
199   char *name_buf;
200   name_buf = auth_key_to_string (s_elem->auth_key);
201
202   s_elem->file_name = create_file (name_buf);
203   GNUNET_free (name_buf);
204   #endif /* TO_FILE */
205 }
206
207 /* end of gnunet-service-rps.c */