diff options
author | Julius Bünger <buenger@mytum.de> | 2015-07-23 18:21:45 +0000 |
---|---|---|
committer | Julius Bünger <buenger@mytum.de> | 2015-07-23 18:21:45 +0000 |
commit | 66fa52570d0d9f29d8b6bf8bd4667c18931b3806 (patch) | |
tree | 90134869b5cd862a14f9858a7c9d757e7d13a951 /src/rps/gnunet-service-rps_sampler_elem.c | |
parent | 5103dcbd8d3b1974aab07f57d919ef7dc66322fd (diff) | |
download | gnunet-66fa52570d0d9f29d8b6bf8bd4667c18931b3806.tar.gz gnunet-66fa52570d0d9f29d8b6bf8bd4667c18931b3806.zip |
-split up sampler and sampler element
Diffstat (limited to 'src/rps/gnunet-service-rps_sampler_elem.c')
-rw-r--r-- | src/rps/gnunet-service-rps_sampler_elem.c | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/rps/gnunet-service-rps_sampler_elem.c b/src/rps/gnunet-service-rps_sampler_elem.c new file mode 100644 index 000000000..16b9cb39e --- /dev/null +++ b/src/rps/gnunet-service-rps_sampler_elem.c | |||
@@ -0,0 +1,169 @@ | |||
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 | * (Re)Initialise given Sampler with random min-wise independent function. | ||
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, " Got PeerID %s\n", | ||
122 | GNUNET_i2s (other)); | ||
123 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n", | ||
124 | GNUNET_i2s (&(s_elem->peer_id))); | ||
125 | } | ||
126 | else | ||
127 | { | ||
128 | GNUNET_CRYPTO_hmac(&s_elem->auth_key, | ||
129 | other, | ||
130 | sizeof(struct GNUNET_PeerIdentity), | ||
131 | &other_hash); | ||
132 | |||
133 | if (EMPTY == s_elem->is_empty) | ||
134 | { | ||
135 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
136 | "Got PeerID %s; Simply accepting (was empty previously).\n", | ||
137 | GNUNET_i2s(other)); | ||
138 | s_elem->peer_id = *other; | ||
139 | s_elem->peer_id_hash = other_hash; | ||
140 | |||
141 | s_elem->num_change++; | ||
142 | } | ||
143 | else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash)) | ||
144 | { | ||
145 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", | ||
146 | GNUNET_i2s (other)); | ||
147 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n", | ||
148 | GNUNET_i2s (&s_elem->peer_id)); | ||
149 | s_elem->peer_id = *other; | ||
150 | s_elem->peer_id_hash = other_hash; | ||
151 | |||
152 | s_elem->num_change++; | ||
153 | } | ||
154 | else | ||
155 | { | ||
156 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", | ||
157 | GNUNET_i2s (other)); | ||
158 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n", | ||
159 | GNUNET_i2s (&s_elem->peer_id)); | ||
160 | } | ||
161 | } | ||
162 | s_elem->is_empty = NOT_EMPTY; | ||
163 | |||
164 | to_file (s_elem->file_name, | ||
165 | "Now holding %s", | ||
166 | GNUNET_i2s_full (&s_elem->peer_id)); | ||
167 | } | ||
168 | |||
169 | /* end of gnunet-service-rps.c */ | ||