aboutsummaryrefslogtreecommitdiff
path: root/src/rps
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps')
-rw-r--r--src/rps/.gitignore16
-rw-r--r--src/rps/Makefile.am172
-rw-r--r--src/rps/gnunet-rps-profiler.c3199
-rw-r--r--src/rps/gnunet-rps.c294
-rw-r--r--src/rps/gnunet-service-rps.c5007
-rw-r--r--src/rps/gnunet-service-rps_custommap.c356
-rw-r--r--src/rps/gnunet-service-rps_custommap.h159
-rw-r--r--src/rps/gnunet-service-rps_sampler.c268
-rw-r--r--src/rps/gnunet-service-rps_sampler.h157
-rw-r--r--src/rps/gnunet-service-rps_sampler_elem.c170
-rw-r--r--src/rps/gnunet-service-rps_sampler_elem.h153
-rw-r--r--src/rps/gnunet-service-rps_view.c294
-rw-r--r--src/rps/gnunet-service-rps_view.h145
-rw-r--r--src/rps/profiler_rps.conf139
-rw-r--r--src/rps/rps-sampler_client.c439
-rw-r--r--src/rps/rps-sampler_client.h156
-rw-r--r--src/rps/rps-sampler_common.c764
-rw-r--r--src/rps/rps-sampler_common.h391
-rw-r--r--src/rps/rps-test_util.c550
-rw-r--r--src/rps/rps-test_util.h144
-rw-r--r--src/rps/rps.conf.in38
-rw-r--r--src/rps/rps.h336
-rw-r--r--src/rps/rps_api.c1320
-rw-r--r--src/rps/test_rps.c3183
-rw-r--r--src/rps/test_rps.conf135
-rw-r--r--src/rps/test_rps_api.c87
-rw-r--r--src/rps/test_service_rps_custommap.c127
-rw-r--r--src/rps/test_service_rps_sampler_elem.c213
-rw-r--r--src/rps/test_service_rps_view.c145
29 files changed, 0 insertions, 18557 deletions
diff --git a/src/rps/.gitignore b/src/rps/.gitignore
deleted file mode 100644
index 9e78e2ca0..000000000
--- a/src/rps/.gitignore
+++ /dev/null
@@ -1,16 +0,0 @@
1gnunet-service-rps
2gnunet-rps
3gnunet-rps-profiler
4test_rps_single_req
5test_rps_req_cancel
6test_rps_sub
7test_rps_seed_big
8test_rps_seed_request
9test_service_rps_custommap
10test_service_rps_sampler_elem
11test_service_rps_view
12test_rps_churn
13test_service_rps_peers
14test_rps_malicious_1
15test_rps_malicious_2
16test_rps_malicious_3
diff --git a/src/rps/Makefile.am b/src/rps/Makefile.am
deleted file mode 100644
index b5e8c0617..000000000
--- a/src/rps/Makefile.am
+++ /dev/null
@@ -1,172 +0,0 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4if USE_COVERAGE
5 AM_CFLAGS = --coverage -O0
6 XLIB = -lgcov
7endif
8
9pkgcfgdir= $(pkgdatadir)/config.d/
10
11libexecdir= $(pkglibdir)/libexec/
12
13pkgcfg_DATA = \
14 rps.conf
15
16bin_PROGRAMS = gnunet-rps
17
18gnunet_rps_SOURCES = \
19 gnunet-rps.c
20
21gnunet_rps_LDADD = \
22 libgnunetrps.la \
23 $(top_builddir)/src/util/libgnunetutil.la \
24 $(XLIB) $(GN_LIBINTL)
25
26lib_LTLIBRARIES = libgnunetrps.la
27
28libgnunetrps_la_SOURCES = \
29 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
30 rps-test_util.h rps-test_util.c \
31 rps-sampler_common.h rps-sampler_common.c \
32 rps-sampler_client.h rps-sampler_client.c \
33 rps_api.c rps.h
34libgnunetrps_la_LIBADD = \
35 $(top_builddir)/src/nse/libgnunetnse.la \
36 $(top_builddir)/src/util/libgnunetutil.la \
37 $(GN_LIBINTL) $(XLIB)
38libgnunetrps_la_LDFLAGS = \
39 $(GN_LIB_LDFLAGS) \
40 -version-info 0:0:0
41# Fix 'created both with libtool and without' error:
42libgnunetrps_la_CFLAGS = $(AM_CFLAGS)
43
44
45libexec_PROGRAMS = \
46 gnunet-service-rps
47
48noinst_PROGRAMS = \
49 gnunet-rps-profiler
50
51
52gnunet_service_rps_SOURCES = \
53 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
54 rps-sampler_common.h rps-sampler_common.c \
55 gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c \
56 gnunet-service-rps_custommap.h gnunet-service-rps_custommap.c \
57 gnunet-service-rps_view.h gnunet-service-rps_view.c \
58 gnunet-service-rps.c
59
60
61gnunet_service_rps_LDADD = \
62 libgnunetrps.la \
63 $(top_builddir)/src/util/libgnunetutil.la \
64 $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \
65 $(top_builddir)/src/cadet/libgnunetcadet.la \
66 $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \
67 $(top_builddir)/src/nse/libgnunetnse.la \
68 $(top_builddir)/src/statistics/libgnunetstatistics.la \
69 $(top_builddir)/src/core/libgnunetcore.la \
70 $(LIBGCRYPT_LIBS) \
71 -lm -lgcrypt \
72 $(GN_LIBINTL)
73
74
75check_PROGRAMS = \
76 test_service_rps_view \
77 test_service_rps_custommap \
78 test_service_rps_sampler_elem \
79 test_rps_single_req \
80 test_rps_req_cancel \
81 test_rps_sub \
82 test_rps_seed_request \
83 test_rps_seed_big \
84 test_rps_churn
85if ENABLE_MALICIOUS
86check_PROGRAMS += \
87 test_rps_malicious_1 \
88 test_rps_malicious_2 \
89 test_rps_malicious_3
90endif
91
92rps_test_src = \
93 test_rps.c \
94 rps-test_util.h rps-test_util.c \
95 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
96 rps-sampler_common.h rps-sampler_common.c \
97 gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c
98
99ld_rps_test_lib = \
100 libgnunetrps.la \
101 $(top_builddir)/src/util/libgnunetutil.la \
102 $(top_builddir)/src/statistics/libgnunetstatistics.la \
103 $(top_builddir)/src/testbed/libgnunettestbed.la \
104 -lm
105
106if ENABLE_TEST_RUN
107AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME;
108TESTS = $(check_PROGRAMS)
109endif
110
111test_service_rps_view_SOURCES = \
112 gnunet-service-rps_view.h gnunet-service-rps_view.c \
113 test_service_rps_view.c
114test_service_rps_view_LDADD = $(top_builddir)/src/util/libgnunetutil.la
115
116test_service_rps_custommap_SOURCES = \
117 gnunet-service-rps_custommap.h gnunet-service-rps_custommap.c \
118 test_service_rps_custommap.c
119test_service_rps_custommap_LDADD = \
120 $(top_builddir)/src/util/libgnunetutil.la
121
122test_service_rps_sampler_elem_SOURCES = \
123 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
124 rps-test_util.h rps-test_util.c \
125 test_service_rps_sampler_elem.c
126test_service_rps_sampler_elem_LDADD = $(top_builddir)/src/util/libgnunetutil.la
127
128test_rps_single_req_SOURCES = $(rps_test_src)
129test_rps_single_req_LDADD = $(ld_rps_test_lib)
130
131test_rps_seed_request_SOURCES = $(rps_test_src)
132test_rps_seed_request_LDADD = $(ld_rps_test_lib)
133
134test_rps_req_cancel_SOURCES = $(rps_test_src)
135test_rps_req_cancel_LDADD = $(ld_rps_test_lib)
136
137test_rps_sub_SOURCES = $(rps_test_src)
138test_rps_sub_LDADD = $(ld_rps_test_lib)
139
140test_rps_seed_big_SOURCES = $(rps_test_src)
141test_rps_seed_big_LDADD = $(ld_rps_test_lib)
142
143test_rps_churn_SOURCES = $(rps_test_src)
144test_rps_churn_LDADD = $(ld_rps_test_lib)
145
146test_rps_malicious_1_SOURCES = $(rps_test_src)
147test_rps_malicious_1_LDADD = $(ld_rps_test_lib)
148
149test_rps_malicious_2_SOURCES = $(rps_test_src)
150test_rps_malicious_2_LDADD = $(ld_rps_test_lib)
151
152test_rps_malicious_3_SOURCES = $(rps_test_src)
153test_rps_malicious_3_LDADD = $(ld_rps_test_lib)
154
155gnunet_rps_profiler_SOURCES = \
156 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
157 rps-sampler_common.h rps-sampler_common.c \
158 gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c \
159 rps-test_util.h rps-test_util.c \
160 gnunet-rps-profiler.c
161
162
163gnunet_rps_profiler_LDADD = \
164 $(top_builddir)/src/statistics/libgnunetstatistics.la \
165 libgnunetrps.la \
166 $(top_builddir)/src/util/libgnunetutil.la \
167 $(top_builddir)/src/testbed/libgnunettestbed.la \
168 -lm
169
170
171EXTRA_DIST = \
172 test_rps.conf
diff --git a/src/rps/gnunet-rps-profiler.c b/src/rps/gnunet-rps-profiler.c
deleted file mode 100644
index 6d1236f08..000000000
--- a/src/rps/gnunet-rps-profiler.c
+++ /dev/null
@@ -1,3199 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file rps/test_rps.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
26 */
27#include "platform.h"
28// #include "rps_test_lib.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_testbed_service.h"
31
32#include "gnunet_rps_service.h"
33#include "rps-test_util.h"
34#include "gnunet-service-rps_sampler_elem.h"
35
36#include <inttypes.h>
37
38
39#define BIT(n) (1 << (n))
40
41/**
42 * How many peers do we start?
43 */
44static uint32_t num_peers;
45
46/**
47 * @brief number of bits required to represent the largest peer id
48 */
49static unsigned bits_needed;
50
51/**
52 * How long do we run the test?
53 */
54static struct GNUNET_TIME_Relative duration;
55
56/**
57 * When do we do a hard shutdown?
58 */
59static struct GNUNET_TIME_Relative timeout;
60
61
62// /**
63// * Portion of malicious peers
64// */
65// static double portion = .1;
66
67/**
68 * Type of malicious peer to test
69 */
70static unsigned int mal_type = 0;
71
72/**
73 * Handles to all of the running peers
74 */
75static struct GNUNET_TESTBED_Peer **testbed_peers;
76
77enum STAT_TYPE
78{
79 STAT_TYPE_ROUNDS, /* 0 */
80 STAT_TYPE_BLOCKS, /* 1 */
81 STAT_TYPE_BLOCKS_MANY_PUSH, /* 2 */
82 STAT_TYPE_BLOCKS_NO_PUSH, /* 3 */
83 STAT_TYPE_BLOCKS_NO_PULL, /* 4 */
84 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL, /* 5 */
85 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL, /* 6 */
86 STAT_TYPE_ISSUED_PUSH_SEND, /* 7 */
87 STAT_TYPE_ISSUED_PUSH_SEND_MH, /* 8 */
88 STAT_TYPE_ISSUED_PULL_REQ, /* 9 */
89 STAT_TYPE_ISSUED_PULL_REQ_MH, /* 10 */
90 STAT_TYPE_ISSUED_PULL_REP, /* 11 */
91 STAT_TYPE_SENT_PUSH_SEND, /* 12 */
92 STAT_TYPE_SENT_PULL_REQ, /* 13 */
93 STAT_TYPE_SENT_PULL_REQ_MH, /* 14 */
94 STAT_TYPE_SENT_PULL_REP, /* 15 */
95 STAT_TYPE_RECV_PUSH_SEND, /* 16 */
96 STAT_TYPE_RECV_PUSH_SEND_MH, /* 17 */
97 STAT_TYPE_RECV_PULL_REQ, /* 18 */
98 STAT_TYPE_RECV_PULL_REQ_MH, /* 19 */
99 STAT_TYPE_RECV_PULL_REP, /* 20 */
100 STAT_TYPE_RECV_PULL_REP_MH, /* 21 */
101 STAT_TYPE_VIEW_SIZE, /* 22 */
102 STAT_TYPE_KNOWN_PEERS, /* 23 */
103 STAT_TYPE_VALID_PEERS, /* 24 */
104 STAT_TYPE_LEARND_PEERS, /* 25 */
105 STAT_TYPE_PENDING_ONLINE_CHECKS, /* 26 */
106 STAT_TYPE_UNREQUESTED_PULL_REPLIES, /* 27 */
107 STAT_TYPE_PEERS_IN_PUSH_MAP, /* 28 */
108 STAT_TYPE_PEERS_IN_PULL_MAP, /* 29 */
109 STAT_TYPE_PEERS_IN_VIEW, /* 30 */
110 STAT_TYPE_VIEW_SIZE_AIM, /* 31 */
111 STAT_TYPE_MAX, /* 32 */
112};
113
114static char*stat_type_strings[] = {
115 "# rounds",
116 "# rounds blocked",
117 "# rounds blocked - too many pushes",
118 "# rounds blocked - no pushes",
119 "# rounds blocked - no pull replies",
120 "# rounds blocked - too many pushes, no pull replies",
121 "# rounds blocked - no pushes, no pull replies",
122 "# push send issued",
123 "# push send issued (multi-hop peer)",
124 "# pull request send issued",
125 "# pull request send issued (multi-hop peer)",
126 "# pull reply send issued",
127 "# pushes sent",
128 "# pull requests sent",
129 "# pull requests sent (multi-hop peer)",
130 "# pull replies sent",
131 "# push message received",
132 "# push message received (multi-hop peer)",
133 "# pull request message received",
134 "# pull request message received (multi-hop peer)",
135 "# pull reply messages received",
136 "# pull reply messages received (multi-hop peer)",
137 "view size",
138 "# known peers",
139 "# valid peers",
140 "# learnd peers",
141 "# pending online checks",
142 "# unrequested pull replies",
143 "# peers in push map at end of round",
144 "# peers in pull map at end of round",
145 "# peers in view at end of round",
146 "view size aim",
147};
148
149struct STATcls
150{
151 struct RPSPeer *rps_peer;
152 enum STAT_TYPE stat_type;
153};
154
155
156/**
157 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
158 *
159 * @param stat_str string representation of statistics specifier
160 *
161 * @return corresponding enum
162 */
163enum STAT_TYPE
164stat_str_2_type (const char *stat_str)
165{
166 if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL],
167 stat_str,
168 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PULL])))
169 {
170 return STAT_TYPE_BLOCKS_NO_PULL;
171 }
172 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
173 stat_str,
174 strlen (
175 stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL])))
176 {
177 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
178 }
179 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH],
180 stat_str,
181 strlen (
182 stat_type_strings[STAT_TYPE_BLOCKS_MANY_PUSH])))
183 {
184 return STAT_TYPE_BLOCKS_MANY_PUSH;
185 }
186 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
187 stat_str,
188 strlen (
189 stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL])))
190 {
191 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
192 }
193 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH],
194 stat_str,
195 strlen (stat_type_strings[STAT_TYPE_BLOCKS_NO_PUSH])))
196 {
197 return STAT_TYPE_BLOCKS_NO_PUSH;
198 }
199 else if (0 == strncmp (stat_type_strings[STAT_TYPE_BLOCKS],
200 stat_str,
201 strlen (stat_type_strings[STAT_TYPE_BLOCKS])))
202 {
203 return STAT_TYPE_BLOCKS;
204 }
205 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ROUNDS],
206 stat_str,
207 strlen (stat_type_strings[STAT_TYPE_ROUNDS])))
208 {
209 return STAT_TYPE_ROUNDS;
210 }
211 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND],
212 stat_str,
213 strlen (
214 stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND])))
215 {
216 return STAT_TYPE_ISSUED_PUSH_SEND;
217 }
218 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND_MH],
219 stat_str,
220 strlen (
221 stat_type_strings[STAT_TYPE_ISSUED_PUSH_SEND_MH])))
222 {
223 return STAT_TYPE_ISSUED_PUSH_SEND_MH;
224 }
225 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ],
226 stat_str,
227 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ])))
228 {
229 return STAT_TYPE_ISSUED_PULL_REQ;
230 }
231 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH],
232 stat_str,
233 strlen (
234 stat_type_strings[STAT_TYPE_ISSUED_PULL_REQ_MH])))
235 {
236 return STAT_TYPE_ISSUED_PULL_REQ_MH;
237 }
238 else if (0 == strncmp (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP],
239 stat_str,
240 strlen (stat_type_strings[STAT_TYPE_ISSUED_PULL_REP])))
241 {
242 return STAT_TYPE_ISSUED_PULL_REP;
243 }
244 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND],
245 stat_str,
246 strlen (stat_type_strings[STAT_TYPE_SENT_PUSH_SEND])))
247 {
248 return STAT_TYPE_SENT_PUSH_SEND;
249 }
250 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ],
251 stat_str,
252 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REQ])))
253 {
254 return STAT_TYPE_SENT_PULL_REQ;
255 }
256 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH],
257 stat_str,
258 strlen (
259 stat_type_strings[STAT_TYPE_SENT_PULL_REQ_MH])))
260 {
261 return STAT_TYPE_SENT_PULL_REQ_MH;
262 }
263 else if (0 == strncmp (stat_type_strings[STAT_TYPE_SENT_PULL_REP],
264 stat_str,
265 strlen (stat_type_strings[STAT_TYPE_SENT_PULL_REP])))
266 {
267 return STAT_TYPE_SENT_PULL_REP;
268 }
269 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND],
270 stat_str,
271 strlen (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND])))
272 {
273 return STAT_TYPE_RECV_PUSH_SEND;
274 }
275 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PUSH_SEND_MH],
276 stat_str,
277 strlen (
278 stat_type_strings[STAT_TYPE_RECV_PUSH_SEND_MH])))
279 {
280 return STAT_TYPE_RECV_PUSH_SEND_MH;
281 }
282 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ],
283 stat_str,
284 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REQ])))
285 {
286 return STAT_TYPE_RECV_PULL_REQ;
287 }
288 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH],
289 stat_str,
290 strlen (
291 stat_type_strings[STAT_TYPE_RECV_PULL_REQ_MH])))
292 {
293 return STAT_TYPE_RECV_PULL_REQ_MH;
294 }
295 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP],
296 stat_str,
297 strlen (stat_type_strings[STAT_TYPE_RECV_PULL_REP])))
298 {
299 return STAT_TYPE_RECV_PULL_REP;
300 }
301 else if (0 == strncmp (stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH],
302 stat_str,
303 strlen (
304 stat_type_strings[STAT_TYPE_RECV_PULL_REP_MH])))
305 {
306 return STAT_TYPE_RECV_PULL_REP_MH;
307 }
308 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE],
309 stat_str,
310 strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE])))
311 {
312 return STAT_TYPE_VIEW_SIZE;
313 }
314 else if (0 == strncmp (stat_type_strings[STAT_TYPE_KNOWN_PEERS],
315 stat_str,
316 strlen (stat_type_strings[STAT_TYPE_KNOWN_PEERS])))
317 {
318 return STAT_TYPE_KNOWN_PEERS;
319 }
320 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VALID_PEERS],
321 stat_str,
322 strlen (stat_type_strings[STAT_TYPE_VALID_PEERS])))
323 {
324 return STAT_TYPE_VALID_PEERS;
325 }
326 else if (0 == strncmp (stat_type_strings[STAT_TYPE_LEARND_PEERS],
327 stat_str,
328 strlen (stat_type_strings[STAT_TYPE_LEARND_PEERS])))
329 {
330 return STAT_TYPE_LEARND_PEERS;
331 }
332 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS],
333 stat_str,
334 strlen (
335 stat_type_strings[STAT_TYPE_PENDING_ONLINE_CHECKS])))
336 {
337 return STAT_TYPE_PENDING_ONLINE_CHECKS;
338 }
339 else if (0 == strncmp (stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
340 stat_str,
341 strlen (
342 stat_type_strings[STAT_TYPE_UNREQUESTED_PULL_REPLIES])))
343 {
344 return STAT_TYPE_UNREQUESTED_PULL_REPLIES;
345 }
346 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP],
347 stat_str,
348 strlen (
349 stat_type_strings[STAT_TYPE_PEERS_IN_PUSH_MAP])))
350 {
351 return STAT_TYPE_PEERS_IN_PUSH_MAP;
352 }
353 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP],
354 stat_str,
355 strlen (
356 stat_type_strings[STAT_TYPE_PEERS_IN_PULL_MAP])))
357 {
358 return STAT_TYPE_PEERS_IN_PULL_MAP;
359 }
360 else if (0 == strncmp (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW],
361 stat_str,
362 strlen (stat_type_strings[STAT_TYPE_PEERS_IN_VIEW])))
363 {
364 return STAT_TYPE_PEERS_IN_VIEW;
365 }
366 else if (0 == strncmp (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM],
367 stat_str,
368 strlen (stat_type_strings[STAT_TYPE_VIEW_SIZE_AIM])))
369 {
370 return STAT_TYPE_VIEW_SIZE_AIM;
371 }
372 return STAT_TYPE_MAX;
373}
374
375
376/**
377 * @brief Indicates whether peer should go off- or online
378 */
379enum PEER_ONLINE_DELTA
380{
381 /**
382 * @brief Indicates peer going online
383 */
384 PEER_GO_ONLINE = 1,
385 /**
386 * @brief Indicates peer going offline
387 */
388 PEER_GO_OFFLINE = -1,
389};
390
391/**
392 * Operation map entry
393 */
394struct OpListEntry
395{
396 /**
397 * DLL next ptr
398 */
399 struct OpListEntry *next;
400
401 /**
402 * DLL prev ptr
403 */
404 struct OpListEntry *prev;
405
406 /**
407 * The testbed operation
408 */
409 struct GNUNET_TESTBED_Operation *op;
410
411 /**
412 * Depending on whether we start or stop RPS service at the peer, set this to
413 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
414 */
415 enum PEER_ONLINE_DELTA delta;
416
417 /**
418 * Index of the regarding peer
419 */
420 unsigned int index;
421};
422
423/**
424 * OpList DLL head
425 */
426static struct OpListEntry *oplist_head;
427
428/**
429 * OpList DLL tail
430 */
431static struct OpListEntry *oplist_tail;
432
433
434/**
435 * A pending reply: A request was sent and the reply is pending.
436 */
437struct PendingReply
438{
439 /**
440 * DLL next,prev ptr
441 */
442 struct PendingReply *next;
443 struct PendingReply *prev;
444
445 /**
446 * Handle to the request we are waiting for
447 */
448 struct GNUNET_RPS_Request_Handle_Single_Info *req_handle;
449
450 /**
451 * The peer that requested
452 */
453 struct RPSPeer *rps_peer;
454};
455
456
457/**
458 * A pending request: A request was not made yet but is scheduled for later.
459 */
460struct PendingRequest
461{
462 /**
463 * DLL next,prev ptr
464 */
465 struct PendingRequest *next;
466 struct PendingRequest *prev;
467
468 /**
469 * Handle to the request we are waiting for
470 */
471 struct GNUNET_SCHEDULER_Task *request_task;
472
473 /**
474 * The peer that requested
475 */
476 struct RPSPeer *rps_peer;
477};
478
479
480/**
481 * Information we track for each peer.
482 */
483struct RPSPeer
484{
485 /**
486 * Index of the peer.
487 */
488 uint32_t index;
489
490 /**
491 * Handle for RPS connect operation.
492 */
493 struct GNUNET_TESTBED_Operation *op;
494
495 /**
496 * Handle to RPS service.
497 */
498 struct GNUNET_RPS_Handle *rps_handle;
499
500 /**
501 * ID of the peer.
502 */
503 struct GNUNET_PeerIdentity *peer_id;
504
505 /**
506 * A request handle to check for an request
507 */
508 // struct GNUNET_RPS_Request_Handle *req_handle;
509
510 /**
511 * Peer on- or offline?
512 */
513 int online;
514
515 /**
516 * Number of Peer IDs to request during the whole test
517 */
518 unsigned int num_ids_to_request;
519
520 /**
521 * Pending requests DLL
522 */
523 struct PendingRequest *pending_req_head;
524 struct PendingRequest *pending_req_tail;
525
526 /**
527 * Number of pending requests
528 */
529 unsigned int num_pending_reqs;
530
531 /**
532 * Pending replies DLL
533 */
534 struct PendingReply *pending_rep_head;
535 struct PendingReply *pending_rep_tail;
536
537 /**
538 * Number of pending replies
539 */
540 unsigned int num_pending_reps;
541
542 /**
543 * Number of received PeerIDs
544 */
545 unsigned int num_recv_ids;
546
547 /**
548 * Pending operation on that peer
549 */
550 const struct OpListEntry *entry_op_manage;
551
552 /**
553 * Testbed operation to connect to statistics service
554 */
555 struct GNUNET_TESTBED_Operation *stat_op;
556
557 /**
558 * Handle to the statistics service
559 */
560 struct GNUNET_STATISTICS_Handle *stats_h;
561
562 /**
563 * @brief flags to indicate which statistics values have been already
564 * collected from the statistics service.
565 * Used to check whether we are able to shutdown.
566 */
567 uint32_t stat_collected_flags;
568
569 /**
570 * @brief File name of the file the stats are finally written to
571 */
572 const char *file_name_stats;
573
574 /**
575 * @brief File name of the file the stats are finally written to
576 */
577 const char *file_name_probs;
578
579 /**
580 * @brief File name of the file the stats are finally written to
581 */
582 const char *file_name_probs_hist;
583
584 /**
585 * @brief The current view
586 */
587 struct GNUNET_PeerIdentity *cur_view;
588
589 /**
590 * @brief Number of peers in the #cur_view.
591 */
592 uint32_t cur_view_count;
593
594 /**
595 * @brief Number of occurrences in other peer's view
596 */
597 uint32_t count_in_views;
598
599 /**
600 * @brief statistics values
601 */
602 uint64_t stats[STAT_TYPE_MAX];
603 /**
604 * @brief Handle for the statistics get request
605 */
606 struct GNUNET_STATISTICS_GetHandle *h_stat_get[STAT_TYPE_MAX];
607
608 /**
609 * @brief Keep the probabilities in cache for computing the probabilities
610 * with respect to history.
611 */
612 double *eval_probs_cache;
613};
614
615/**
616 * Information for all the peers.
617 */
618static struct RPSPeer *rps_peers;
619
620/**
621 * Peermap to get the index of a given peer ID quick.
622 */
623static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
624
625/**
626 * IDs of the peers.
627 */
628static struct GNUNET_PeerIdentity *rps_peer_ids;
629
630/**
631 * ID of the targeted peer.
632 */
633static struct GNUNET_PeerIdentity *target_peer;
634
635/**
636 * Number of online peers.
637 */
638static unsigned int num_peers_online;
639
640/**
641 * @brief The added sizes of the peer's views
642 */
643static unsigned int view_sizes;
644
645/**
646 * Return value from 'main'.
647 */
648static int ok;
649
650/**
651 * Identifier for the task that runs after the test to collect results
652 */
653static struct GNUNET_SCHEDULER_Task *post_test_task;
654
655/**
656 * Identifier for the shutdown task
657 */
658static struct GNUNET_SCHEDULER_Task *shutdown_task;
659
660
661/**
662 * Identifier for the churn task that runs periodically
663 */
664static struct GNUNET_SCHEDULER_Task *churn_task;
665
666/**
667 * Called to initialise the given RPSPeer
668 */
669typedef void (*InitPeer) (struct RPSPeer *rps_peer);
670
671/**
672 * @brief Called directly after connecting to the service
673 *
674 * @param rps_peer Specific peer the function is called on
675 * @param h the handle to the rps service
676 */
677typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
678
679/**
680 * @brief Executes functions to test the api/service for a given peer
681 *
682 * Called from within #rps_connect_complete_cb ()
683 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
684 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
685 *
686 * @param rps_peer the peer the task runs on
687 */
688typedef void (*MainTest) (struct RPSPeer *rps_peer);
689
690/**
691 * Callback called once the requested random peers are available
692 */
693typedef void (*ReplyHandle) (void *cls,
694 uint64_t n,
695 const struct GNUNET_PeerIdentity *recv_peers);
696
697/**
698 * Called directly before disconnecting from the service
699 */
700typedef void (*PostTest) (struct RPSPeer *peer);
701
702/**
703 * Function called after disconnect to evaluate test success
704 */
705typedef int (*EvaluationCallback) (void);
706
707/**
708 * @brief Do we have Churn?
709 */
710enum OPTION_CHURN
711{
712 /**
713 * @brief If we have churn this is set
714 */
715 HAVE_CHURN,
716 /**
717 * @brief If we have no churn this is set
718 */
719 HAVE_NO_CHURN,
720};
721
722/**
723 * @brief Is it ok to quit the test before the timeout?
724 */
725enum OPTION_QUICK_QUIT
726{
727 /**
728 * @brief It is ok for the test to quit before the timeout triggers
729 */
730 HAVE_QUICK_QUIT,
731
732 /**
733 * @brief It is NOT ok for the test to quit before the timeout triggers
734 */
735 HAVE_NO_QUICK_QUIT,
736};
737
738/**
739 * @brief Do we collect statistics at the end?
740 */
741enum OPTION_COLLECT_STATISTICS
742{
743 /**
744 * @brief We collect statistics at the end
745 */
746 COLLECT_STATISTICS,
747
748 /**
749 * @brief We do not collect statistics at the end
750 */
751 NO_COLLECT_STATISTICS,
752};
753
754/**
755 * @brief Do we collect views during run?
756 */
757enum OPTION_COLLECT_VIEW
758{
759 /**
760 * @brief We collect view during run
761 */
762 COLLECT_VIEW,
763
764 /**
765 * @brief We do not collect the view during run
766 */
767 NO_COLLECT_VIEW,
768};
769
770/**
771 * Structure to define a single test
772 */
773struct SingleTestRun
774{
775 /**
776 * Name of the test
777 */
778 char *name;
779
780 /**
781 * Called with a single peer in order to initialise that peer
782 */
783 InitPeer init_peer;
784
785 /**
786 * Called directly after connecting to the service
787 */
788 PreTest pre_test;
789
790 /**
791 * Main function for each peer
792 */
793 MainTest main_test;
794
795 /**
796 * Callback called once the requested peers are available
797 */
798 ReplyHandle reply_handle;
799
800 /**
801 * Called directly before disconnecting from the service
802 */
803 PostTest post_test;
804
805 /**
806 * Function to evaluate the test results
807 */
808 EvaluationCallback eval_cb;
809
810 /**
811 * Request interval
812 */
813 uint32_t request_interval;
814
815 /**
816 * Number of Requests to make.
817 */
818 uint32_t num_requests;
819
820 /**
821 * Run with (-out) churn
822 */
823 enum OPTION_CHURN have_churn;
824
825 /**
826 * Quit test before timeout?
827 */
828 enum OPTION_QUICK_QUIT have_quick_quit;
829
830 /**
831 * Collect statistics at the end?
832 */
833 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
834
835 /**
836 * Collect view during run?
837 */
838 enum OPTION_COLLECT_VIEW have_collect_view;
839
840 /**
841 * @brief Mark which values from the statistics service to collect at the end
842 * of the run
843 */
844 uint32_t stat_collect_flags;
845} cur_test_run;
846
847/**
848 * Did we finish the test?
849 */
850static int post_test;
851
852/**
853 * Are we shutting down?
854 */
855static int in_shutdown;
856
857/**
858 * Append arguments to file
859 */
860static void
861tofile_ (const char *file_name, const char *line)
862{
863 struct GNUNET_DISK_FileHandle *f;
864 /* char output_buffer[512]; */
865 size_t size;
866 /* int size; */
867 size_t size2;
868
869 if (NULL == (f = GNUNET_DISK_file_open (file_name,
870 GNUNET_DISK_OPEN_APPEND
871 | GNUNET_DISK_OPEN_WRITE
872 | GNUNET_DISK_OPEN_CREATE,
873 GNUNET_DISK_PERM_USER_READ
874 | GNUNET_DISK_PERM_USER_WRITE
875 | GNUNET_DISK_PERM_GROUP_READ
876 | GNUNET_DISK_PERM_OTHER_READ)))
877 {
878 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
879 "Not able to open file %s\n",
880 file_name);
881 return;
882 }
883 /* size = GNUNET_snprintf (output_buffer,
884 sizeof (output_buffer),
885 "%llu %s\n",
886 GNUNET_TIME_absolute_get ().abs_value_us,
887 line);
888 if (0 > size)
889 {
890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
891 "Failed to write string to buffer (size: %i)\n",
892 size);
893 return;
894 } */size = strlen (line) * sizeof(char);
895
896 size2 = GNUNET_DISK_file_write (f, line, size);
897 if (size != size2)
898 {
899 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
900 "Unable to write to file! (Size: %lu, size2: %lu)\n",
901 size,
902 size2);
903 if (GNUNET_YES != GNUNET_DISK_file_close (f))
904 {
905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
906 "Unable to close file\n");
907 }
908 return;
909 }
910
911 if (GNUNET_YES != GNUNET_DISK_file_close (f))
912 {
913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
914 "Unable to close file\n");
915 }
916}
917
918
919/**
920 * This function is used to facilitate writing important information to disk
921 */
922#define tofile(file_name, ...) do { \
923 char tmp_buf[512]; \
924 int size; \
925 size = GNUNET_snprintf (tmp_buf, sizeof(tmp_buf), __VA_ARGS__); \
926 if (0 > size) \
927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \
928 "Failed to create tmp_buf\n"); \
929 else \
930 tofile_ (file_name, tmp_buf); \
931} while (0);
932
933
934/**
935 * Write the ids and their according index in the given array to a file
936 * Unused
937 */
938/* static void
939 ids_to_file (char *file_name,
940 struct GNUNET_PeerIdentity *peer_ids,
941 unsigned int num_peer_ids)
942 {
943 unsigned int i;
944
945 for (i=0 ; i < num_peer_ids ; i++)
946 {
947 to_file (file_name,
948 "%u\t%s",
949 i,
950 GNUNET_i2s_full (&peer_ids[i]));
951 }
952 } */
953
954/**
955 * Test the success of a single test
956 */
957static int
958evaluate (void)
959{
960 unsigned int i;
961 int tmp_ok;
962
963 tmp_ok = 1;
964
965 for (i = 0; i < num_peers; i++)
966 {
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
969 i,
970 GNUNET_i2s (rps_peers[i].peer_id),
971 rps_peers[i].num_recv_ids,
972 rps_peers[i].num_ids_to_request,
973 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
974 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
975 }
976 return tmp_ok ? 0 : 1;
977}
978
979
980/**
981 * Creates an oplist entry and adds it to the oplist DLL
982 */
983static struct OpListEntry *
984make_oplist_entry ()
985{
986 struct OpListEntry *entry;
987
988 entry = GNUNET_new (struct OpListEntry);
989 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
990 return entry;
991}
992
993
994/**
995 * @brief Checks if given peer already received its statistics value from the
996 * statistics service.
997 *
998 * @param rps_peer the peer to check for
999 *
1000 * @return #GNUNET_YES if so
1001 * #GNUNET_NO otherwise
1002 */
1003static int
1004check_statistics_collect_completed_single_peer (
1005 const struct RPSPeer *rps_peer)
1006{
1007 if (cur_test_run.stat_collect_flags !=
1008 (cur_test_run.stat_collect_flags
1009 & rps_peer->stat_collected_flags))
1010 {
1011 return GNUNET_NO;
1012 }
1013 return GNUNET_YES;
1014}
1015
1016
1017/**
1018 * @brief Checks if all peers already received their statistics value from the
1019 * statistics service.
1020 *
1021 * @return #GNUNET_YES if so
1022 * #GNUNET_NO otherwise
1023 */
1024static int
1025check_statistics_collect_completed ()
1026{
1027 uint32_t i;
1028
1029 for (i = 0; i < num_peers; i++)
1030 {
1031 if (GNUNET_NO == check_statistics_collect_completed_single_peer (
1032 &rps_peers[i]))
1033 {
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "At least Peer %" PRIu32
1036 " did not yet receive all statistics values\n",
1037 i);
1038 return GNUNET_NO;
1039 }
1040 }
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "All peers received their statistics values\n");
1043 return GNUNET_YES;
1044}
1045
1046
1047static void
1048rps_disconnect_adapter (void *cls,
1049 void *op_result);
1050
1051static void
1052cancel_pending_req (struct PendingRequest *pending_req)
1053{
1054 struct RPSPeer *rps_peer;
1055
1056 rps_peer = pending_req->rps_peer;
1057 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1058 rps_peer->pending_req_tail,
1059 pending_req);
1060 rps_peer->num_pending_reqs--;
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "Cancelling pending rps get request\n");
1063 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1064 GNUNET_free (pending_req);
1065}
1066
1067
1068static void
1069cancel_request (struct PendingReply *pending_rep)
1070{
1071 struct RPSPeer *rps_peer;
1072
1073 rps_peer = pending_rep->rps_peer;
1074 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1075 rps_peer->pending_rep_tail,
1076 pending_rep);
1077 rps_peer->num_pending_reps--;
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Cancelling rps get reply\n");
1080 GNUNET_assert (NULL != pending_rep->req_handle);
1081 GNUNET_RPS_request_single_info_cancel (pending_rep->req_handle);
1082 pending_rep->req_handle = NULL;
1083 GNUNET_free (pending_rep);
1084 pending_rep = NULL;
1085}
1086
1087
1088void
1089clean_peer (unsigned peer_index)
1090{
1091 struct PendingRequest *pending_req;
1092
1093 while (NULL != (pending_req = rps_peers[peer_index].pending_req_head))
1094 {
1095 cancel_pending_req (pending_req);
1096 }
1097 pending_req = rps_peers[peer_index].pending_req_head;
1098 rps_disconnect_adapter (&rps_peers[peer_index],
1099 &rps_peers[peer_index].rps_handle);
1100 for (unsigned stat_type = STAT_TYPE_ROUNDS;
1101 stat_type < STAT_TYPE_MAX;
1102 stat_type++)
1103 {
1104 if (NULL != rps_peers[peer_index].h_stat_get[stat_type])
1105 {
1106 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1107 "(%u) did not yet receive stat value for `%s'\n",
1108 rps_peers[peer_index].index,
1109 stat_type_strings[stat_type]);
1110 GNUNET_STATISTICS_get_cancel (
1111 rps_peers[peer_index].h_stat_get[stat_type]);
1112 }
1113 }
1114 if (NULL != rps_peers[peer_index].op)
1115 {
1116 GNUNET_TESTBED_operation_done (rps_peers[peer_index].op);
1117 rps_peers[peer_index].op = NULL;
1118 }
1119}
1120
1121
1122/**
1123 * Task run on timeout to shut everything down.
1124 */
1125static void
1126shutdown_op (void *cls)
1127{
1128 unsigned int i;
1129 struct OpListEntry *entry;
1130
1131 (void) cls;
1132
1133 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1134 "Shutdown task scheduled, going down.\n");
1135 in_shutdown = GNUNET_YES;
1136
1137 if (NULL != shutdown_task)
1138 {
1139 GNUNET_SCHEDULER_cancel (shutdown_task);
1140 shutdown_task = NULL;
1141 }
1142 if (NULL != post_test_task)
1143 {
1144 GNUNET_SCHEDULER_cancel (post_test_task);
1145 post_test_task = NULL;
1146 }
1147 if (NULL != churn_task)
1148 {
1149 GNUNET_SCHEDULER_cancel (churn_task);
1150 churn_task = NULL;
1151 }
1152 entry = oplist_head;
1153 while (NULL != (entry = oplist_head))
1154 {
1155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1156 "Operation still pending on shutdown (%u)\n",
1157 entry->index);
1158 GNUNET_TESTBED_operation_done (entry->op);
1159 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1160 GNUNET_free (entry);
1161 }
1162 for (i = 0; i < num_peers; i++)
1163 {
1164 clean_peer (i);
1165 }
1166 close_all_files ();
1167}
1168
1169
1170static void
1171trigger_shutdown (void *cls)
1172{
1173 (void) cls;
1174
1175 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1176 "Shutdown was triggered by timeout, going down.\n");
1177 shutdown_task = NULL;
1178 GNUNET_SCHEDULER_shutdown ();
1179}
1180
1181
1182/**
1183 * Task run after #duration to collect statistics and potentially shut down.
1184 */
1185static void
1186post_test_op (void *cls)
1187{
1188 unsigned int i;
1189
1190 (void) cls;
1191
1192 post_test_task = NULL;
1193 post_test = GNUNET_YES;
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1195 "Executing post test op.\n");
1196 if (NULL != churn_task)
1197 {
1198 GNUNET_SCHEDULER_cancel (churn_task);
1199 churn_task = NULL;
1200 }
1201 for (i = 0; i < num_peers; i++)
1202 {
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Executing post test op. (peer %" PRIu32 ")\n",
1205 rps_peers[i].index);
1206 if (NULL != rps_peers[i].op)
1207 {
1208 GNUNET_TESTBED_operation_done (rps_peers[i].op);
1209 rps_peers[i].op = NULL;
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "Cancelled testbed operation\n");
1212 }
1213 if (NULL != cur_test_run.post_test)
1214 {
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n",
1216 i);
1217 cur_test_run.post_test (&rps_peers[i]);
1218 }
1219 }
1220 /* If we do not collect statistics, shut down directly */
1221 if ((NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics) ||
1222 (GNUNET_YES == check_statistics_collect_completed ()) )
1223 {
1224 GNUNET_SCHEDULER_cancel (shutdown_task);
1225 shutdown_task = NULL;
1226 GNUNET_SCHEDULER_shutdown ();
1227 }
1228}
1229
1230
1231/**
1232 * Get the id of peer i.
1233 */
1234void
1235info_cb (void *cb_cls,
1236 struct GNUNET_TESTBED_Operation *op,
1237 const struct GNUNET_TESTBED_PeerInformation *pinfo,
1238 const char *emsg)
1239{
1240 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
1241
1242 (void) op;
1243
1244 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1245 {
1246 return;
1247 }
1248
1249 if ((NULL == pinfo) || (NULL != emsg))
1250 {
1251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
1252 GNUNET_TESTBED_operation_done (entry->op);
1253 return;
1254 }
1255
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Peer %u is %s\n",
1258 entry->index,
1259 GNUNET_i2s (pinfo->result.id));
1260
1261 rps_peer_ids[entry->index] = *(pinfo->result.id);
1262 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
1263
1264 GNUNET_assert (GNUNET_OK ==
1265 GNUNET_CONTAINER_multipeermap_put (peer_map,
1266 &rps_peer_ids[entry->index],
1267 &rps_peers[entry->index],
1268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1269 tofile ("/tmp/rps/peer_ids",
1270 "%u\t%s\n",
1271 entry->index,
1272 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
1273
1274 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1275 GNUNET_TESTBED_operation_done (entry->op);
1276 GNUNET_free (entry);
1277}
1278
1279
1280/**
1281 * Callback to be called when RPS service connect operation is completed
1282 *
1283 * @param cls the callback closure from functions generating an operation
1284 * @param op the operation that has been finished
1285 * @param ca_result the RPS service handle returned from rps_connect_adapter
1286 * @param emsg error message in case the operation has failed; will be NULL if
1287 * operation has executed successfully.
1288 */
1289static void
1290rps_connect_complete_cb (void *cls,
1291 struct GNUNET_TESTBED_Operation *op,
1292 void *ca_result,
1293 const char *emsg)
1294{
1295 struct RPSPeer *rps_peer = cls;
1296 struct GNUNET_RPS_Handle *rps = ca_result;
1297
1298 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1299 {
1300 return;
1301 }
1302
1303 rps_peer->rps_handle = rps;
1304 rps_peer->online = GNUNET_YES;
1305 num_peers_online++;
1306
1307 GNUNET_assert (op == rps_peer->op);
1308 if (NULL != emsg)
1309 {
1310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1311 "Failed to connect to RPS service: %s\n",
1312 emsg);
1313 ok = 1;
1314 GNUNET_SCHEDULER_shutdown ();
1315 return;
1316 }
1317
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Started client successfully (%u)\n",
1320 rps_peer->index);
1321
1322 cur_test_run.main_test (rps_peer);
1323}
1324
1325
1326/**
1327 * Adapter function called to establish a connection to
1328 * the RPS service.
1329 *
1330 * @param cls closure
1331 * @param cfg configuration of the peer to connect to; will be available until
1332 * GNUNET_TESTBED_operation_done() is called on the operation returned
1333 * from GNUNET_TESTBED_service_connect()
1334 * @return service handle to return in 'op_result', NULL on error
1335 */
1336static void *
1337rps_connect_adapter (void *cls,
1338 const struct GNUNET_CONFIGURATION_Handle *cfg)
1339{
1340 struct GNUNET_RPS_Handle *h;
1341
1342 h = GNUNET_RPS_connect (cfg);
1343
1344 if (NULL != cur_test_run.pre_test)
1345 cur_test_run.pre_test (cls, h);
1346
1347 return h;
1348}
1349
1350
1351/**
1352 * Called to open a connection to the peer's statistics
1353 *
1354 * @param cls peer context
1355 * @param cfg configuration of the peer to connect to; will be available until
1356 * GNUNET_TESTBED_operation_done() is called on the operation returned
1357 * from GNUNET_TESTBED_service_connect()
1358 * @return service handle to return in 'op_result', NULL on error
1359 */
1360static void *
1361stat_connect_adapter (void *cls,
1362 const struct GNUNET_CONFIGURATION_Handle *cfg)
1363{
1364 struct RPSPeer *peer = cls;
1365
1366 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1367 return peer->stats_h;
1368}
1369
1370
1371/**
1372 * Called to disconnect from peer's statistics service
1373 *
1374 * @param cls peer context
1375 * @param op_result service handle returned from the connect adapter
1376 */
1377static void
1378stat_disconnect_adapter (void *cls, void *op_result)
1379{
1380 struct RPSPeer *peer = cls;
1381
1382 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1383 // (peer->stats_h, "core", "# peers connected",
1384 // stat_iterator, peer));
1385 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1386 // (peer->stats_h, "nse", "# peers connected",
1387 // stat_iterator, peer));
1388 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1389 peer->stats_h = NULL;
1390}
1391
1392
1393/**
1394 * Called after successfully opening a connection to a peer's statistics
1395 * service; we register statistics monitoring for CORE and NSE here.
1396 *
1397 * @param cls the callback closure from functions generating an operation
1398 * @param op the operation that has been finished
1399 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1400 * @param emsg error message in case the operation has failed; will be NULL if
1401 * operation has executed successfully.
1402 */
1403static void
1404stat_complete_cb (void *cls,
1405 struct GNUNET_TESTBED_Operation *op,
1406 void *ca_result,
1407 const char *emsg)
1408{
1409 // struct GNUNET_STATISTICS_Handle *sh = ca_result;
1410 // struct RPSPeer *peer = (struct RPSPeer *) cls;
1411 (void) cls;
1412 (void) op;
1413 (void) ca_result;
1414
1415 if (NULL != emsg)
1416 {
1417 GNUNET_break (0);
1418 return;
1419 }
1420 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1421 // (sh, "core", "# peers connected",
1422 // stat_iterator, peer));
1423 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1424 // (sh, "nse", "# peers connected",
1425 // stat_iterator, peer));
1426}
1427
1428
1429/**
1430 * Adapter function called to destroy connection to
1431 * RPS service.
1432 *
1433 * @param cls closure
1434 * @param op_result service handle returned from the connect adapter
1435 */
1436static void
1437rps_disconnect_adapter (void *cls,
1438 void *op_result)
1439{
1440 struct RPSPeer *peer = cls;
1441 struct GNUNET_RPS_Handle *h = op_result;
1442 struct PendingReply *pending_rep;
1443
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445 "disconnect_adapter (%u)\n",
1446 peer->index);
1447 GNUNET_assert (NULL != peer);
1448 if (NULL != peer->rps_handle)
1449 {
1450 while (NULL != (pending_rep = peer->pending_rep_head))
1451 {
1452 cancel_request (pending_rep);
1453 }
1454 GNUNET_assert (h == peer->rps_handle);
1455 if (NULL != h)
1456 {
1457 GNUNET_RPS_disconnect (h);
1458 h = NULL;
1459 }
1460 peer->rps_handle = NULL;
1461 }
1462}
1463
1464
1465/***********************************************************************
1466* Definition of tests
1467***********************************************************************/
1468
1469/**
1470 * Callback to call on receipt of a reply
1471 *
1472 * @param cls closure
1473 * @param n number of peers
1474 * @param recv_peers the received peers
1475 */
1476static void
1477default_reply_handle (void *cls,
1478 uint64_t n,
1479 const struct GNUNET_PeerIdentity *recv_peers)
1480{
1481 struct RPSPeer *rps_peer;
1482 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1483 unsigned int i;
1484
1485 rps_peer = pending_rep->rps_peer;
1486 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1487 rps_peer->pending_rep_tail,
1488 pending_rep);
1489 rps_peer->num_pending_reps--;
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "[%s] got %" PRIu64 " peers:\n",
1492 GNUNET_i2s (rps_peer->peer_id),
1493 n);
1494
1495 for (i = 0; i < n; i++)
1496 {
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "%u: %s\n",
1499 i,
1500 GNUNET_i2s (&recv_peers[i]));
1501
1502 rps_peer->num_recv_ids++;
1503 }
1504
1505 if (GNUNET_YES != post_test)
1506 return;
1507 if (HAVE_QUICK_QUIT != cur_test_run.have_quick_quit)
1508 return;
1509 if (0 == evaluate ())
1510 {
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Test succeeded before end of duration\n");
1513 if (NULL != post_test_task)
1514 GNUNET_SCHEDULER_cancel (post_test_task);
1515 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1516 GNUNET_assert (NULL != post_test_task);
1517 }
1518}
1519
1520
1521static void
1522profiler_reply_handle_info (void *cls,
1523 const struct GNUNET_PeerIdentity *recv_peer,
1524 double probability,
1525 uint32_t num_observed);
1526
1527/**
1528 * Request random peers.
1529 */
1530static void
1531request_peers (void *cls)
1532{
1533 struct PendingRequest *pending_req = cls;
1534 struct RPSPeer *rps_peer;
1535 struct PendingReply *pending_rep;
1536
1537 rps_peer = pending_req->rps_peer;
1538 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1539 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1540 rps_peer->pending_req_tail,
1541 pending_req);
1542 rps_peer->num_pending_reqs--;
1543 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1544 return;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "Requesting one peer\n");
1547 pending_rep = GNUNET_new (struct PendingReply);
1548 pending_rep->rps_peer = rps_peer;
1549 // pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1550 // 1,
1551 // cur_test_run.reply_handle,
1552 // pending_rep);
1553 pending_rep->req_handle = GNUNET_RPS_request_peer_info (rps_peer->rps_handle,
1554 profiler_reply_handle_info,
1555 pending_rep);
1556 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1557 rps_peer->pending_rep_tail,
1558 pending_rep);
1559 rps_peer->num_pending_reps++;
1560}
1561
1562
1563/**
1564 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1565 * issued, nor replied
1566 */
1567void
1568schedule_missing_requests (struct RPSPeer *rps_peer)
1569{
1570 unsigned int i;
1571 struct PendingRequest *pending_req;
1572
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Scheduling %u - %u missing requests\n",
1575 rps_peer->num_ids_to_request,
1576 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1577 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1578 rps_peer->num_ids_to_request);
1579 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1580 i < rps_peer->num_ids_to_request; i++)
1581 {
1582 pending_req = GNUNET_new (struct PendingRequest);
1583 pending_req->rps_peer = rps_peer;
1584 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1585 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1586 cur_test_run.request_interval * i),
1587 request_peers,
1588 pending_req);
1589 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1590 rps_peer->pending_req_tail,
1591 pending_req);
1592 rps_peer->num_pending_reqs++;
1593 }
1594}
1595
1596
1597void
1598cancel_pending_req_rep (struct RPSPeer *rps_peer)
1599{
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "Cancelling all (pending) requests.\n");
1602 while (NULL != rps_peer->pending_req_head)
1603 cancel_pending_req (rps_peer->pending_req_head);
1604 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1605 while (NULL != rps_peer->pending_rep_head)
1606 cancel_request (rps_peer->pending_rep_head);
1607 GNUNET_assert (0 == rps_peer->num_pending_reps);
1608}
1609
1610
1611/***********************************
1612* MALICIOUS
1613***********************************/
1614
1615///**
1616// * Initialise only non-mal RPSPeers
1617// */
1618//static void
1619//mal_init_peer (struct RPSPeer *rps_peer)
1620//{
1621// if (rps_peer->index >= round (portion * num_peers))
1622// rps_peer->num_ids_to_request = 1;
1623//}
1624
1625
1626///**
1627// * @brief Set peers to (non-)malicious before execution
1628// *
1629// * Of signature #PreTest
1630// *
1631// * @param rps_peer the peer to set (non-) malicious
1632// * @param h the handle to the service
1633// */
1634//static void
1635//mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1636//{
1637// #if ENABLE_MALICIOUS
1638// uint32_t num_mal_peers;
1639//
1640// GNUNET_assert ((1 >= portion) &&
1641// (0 < portion));
1642// num_mal_peers = round (portion * num_peers);
1643//
1644// if (rps_peer->index < num_mal_peers)
1645// {
1646// GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1647// "%u. peer [%s] of %" PRIu32
1648// " malicious peers turning malicious\n",
1649// rps_peer->index,
1650// GNUNET_i2s (rps_peer->peer_id),
1651// num_mal_peers);
1652//
1653// GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1654// rps_peer_ids, target_peer);
1655// }
1656// #endif /* ENABLE_MALICIOUS */
1657//}
1658
1659
1660// static void
1661// mal_cb (struct RPSPeer *rps_peer)
1662// {
1663// if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1664// {
1665// return;
1666// }
1667//
1668// #if ENABLE_MALICIOUS
1669// uint32_t num_mal_peers;
1670//
1671// GNUNET_assert ((1 >= portion) &&
1672// (0 < portion));
1673// num_mal_peers = round (portion * num_peers);
1674//
1675// if (rps_peer->index >= num_mal_peers)
1676// { /* It's useless to ask a malicious peer about a random sample -
1677// it's not sampling */
1678// GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
1679// GNUNET_TIME_UNIT_SECONDS, 2),
1680// seed_peers, rps_peer);
1681// schedule_missing_requests (rps_peer);
1682// }
1683// #endif /* ENABLE_MALICIOUS */
1684// }
1685
1686
1687/***********************************
1688* CHURN
1689***********************************/
1690
1691static void
1692churn (void *cls);
1693
1694
1695/***********************************
1696* PROFILER
1697***********************************/
1698
1699/**
1700 * Callback to be called when RPS service is started or stopped at peers
1701 *
1702 * @param cls NULL
1703 * @param op the operation handle
1704 * @param emsg NULL on success; otherwise an error description
1705 */
1706static void
1707churn_cb (void *cls,
1708 struct GNUNET_TESTBED_Operation *op,
1709 const char *emsg)
1710{
1711 // FIXME
1712 struct OpListEntry *entry = cls;
1713
1714 (void) op;
1715
1716 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1717 {
1718 return;
1719 }
1720
1721 GNUNET_TESTBED_operation_done (entry->op);
1722 if (NULL != emsg)
1723 {
1724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1725 "Failed to start/stop RPS at a peer\n");
1726 GNUNET_SCHEDULER_shutdown ();
1727 return;
1728 }
1729 GNUNET_assert (0 != entry->delta);
1730
1731 num_peers_online += entry->delta;
1732
1733 if (PEER_GO_OFFLINE == entry->delta)
1734 { /* Peer hopefully just went offline */
1735 if (GNUNET_YES != rps_peers[entry->index].online)
1736 {
1737 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1738 "peer %s was expected to go offline but is still marked as online\n",
1739 GNUNET_i2s (rps_peers[entry->index].peer_id));
1740 GNUNET_break (0);
1741 }
1742 else
1743 {
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "peer %s probably went offline as expected\n",
1746 GNUNET_i2s (rps_peers[entry->index].peer_id));
1747 }
1748 rps_peers[entry->index].online = GNUNET_NO;
1749 }
1750
1751 else if (PEER_GO_ONLINE < entry->delta)
1752 { /* Peer hopefully just went online */
1753 if (GNUNET_NO != rps_peers[entry->index].online)
1754 {
1755 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1756 "peer %s was expected to go online but is still marked as offline\n",
1757 GNUNET_i2s (rps_peers[entry->index].peer_id));
1758 GNUNET_break (0);
1759 }
1760 else
1761 {
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "peer %s probably went online as expected\n",
1764 GNUNET_i2s (rps_peers[entry->index].peer_id));
1765 if (NULL != cur_test_run.pre_test)
1766 {
1767 cur_test_run.pre_test (&rps_peers[entry->index],
1768 rps_peers[entry->index].rps_handle);
1769 schedule_missing_requests (&rps_peers[entry->index]);
1770 }
1771 }
1772 rps_peers[entry->index].online = GNUNET_YES;
1773 }
1774 else
1775 {
1776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1777 "Invalid value for delta: %i\n", entry->delta);
1778 GNUNET_break (0);
1779 }
1780
1781 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1782 rps_peers[entry->index].entry_op_manage = NULL;
1783 GNUNET_free (entry);
1784 // if (num_peers_in_round[current_round] == peers_running)
1785 // run_round ();
1786}
1787
1788
1789/**
1790 * @brief Set the rps-service up or down for a specific peer
1791 *
1792 * @param i index of action
1793 * @param j index of peer
1794 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1795 * @param prob_go_on_off the probability of the action
1796 */
1797static void
1798manage_service_wrapper (unsigned int i, unsigned int j,
1799 enum PEER_ONLINE_DELTA delta,
1800 double prob_go_on_off)
1801{
1802 struct OpListEntry *entry = NULL;
1803 uint32_t prob;
1804
1805 /* make sure that management operation is not already scheduled */
1806 if (NULL != rps_peers[j].entry_op_manage)
1807 {
1808 return;
1809 }
1810
1811 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1812 UINT32_MAX);
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1814 "%u. selected peer (%u: %s) is %s.\n",
1815 i,
1816 j,
1817 GNUNET_i2s (rps_peers[j].peer_id),
1818 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1819 if (prob < prob_go_on_off * UINT32_MAX)
1820 {
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "%s goes %s\n",
1823 GNUNET_i2s (rps_peers[j].peer_id),
1824 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1825
1826 if (PEER_GO_OFFLINE == delta)
1827 cancel_pending_req_rep (&rps_peers[j]);
1828 entry = make_oplist_entry ();
1829 entry->delta = delta;
1830 entry->index = j;
1831 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1832 testbed_peers[j],
1833 "rps",
1834 &churn_cb,
1835 entry,
1836 (PEER_GO_OFFLINE == delta) ?
1837 0 : 1);
1838 rps_peers[j].entry_op_manage = entry;
1839 }
1840}
1841
1842
1843static void
1844churn (void *cls)
1845{
1846 unsigned int i;
1847 unsigned int j;
1848 double portion_online;
1849 unsigned int *permut;
1850 double prob_go_offline;
1851 double portion_go_online;
1852 double portion_go_offline;
1853
1854 (void) cls;
1855
1856 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1857 {
1858 return;
1859 }
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Churn function executing\n");
1862
1863 churn_task = NULL; /* Should be invalid by now */
1864
1865 /* Compute the probability for an online peer to go offline
1866 * this round */
1867 portion_online = num_peers_online * 1.0 / num_peers;
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Portion online: %f\n",
1870 portion_online);
1871 portion_go_online = ((1 - portion_online) * .5 * .66);
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1873 "Portion that should go online: %f\n",
1874 portion_go_online);
1875 portion_go_offline = (portion_online + portion_go_online) - .75;
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 "Portion that probably goes offline: %f\n",
1878 portion_go_offline);
1879 prob_go_offline = portion_go_offline / (portion_online * .5);
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "Probability of a selected online peer to go offline: %f\n",
1882 prob_go_offline);
1883
1884 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1885 (unsigned int) num_peers);
1886
1887 /* Go over 50% randomly chosen peers */
1888 for (i = 0; i < .5 * num_peers; i++)
1889 {
1890 j = permut[i];
1891
1892 /* If online, shut down with certain probability */
1893 if (GNUNET_YES == rps_peers[j].online)
1894 {
1895 manage_service_wrapper (i, j, -1, prob_go_offline);
1896 }
1897
1898 /* If offline, restart with certain probability */
1899 else if (GNUNET_NO == rps_peers[j].online)
1900 {
1901 manage_service_wrapper (i, j, 1, 0.66);
1902 }
1903 }
1904
1905 GNUNET_free (permut);
1906
1907 churn_task = GNUNET_SCHEDULER_add_delayed (
1908 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1909 churn,
1910 NULL);
1911}
1912
1913
1914/**
1915 * Initialise given RPSPeer
1916 */
1917static void
1918profiler_init_peer (struct RPSPeer *rps_peer)
1919{
1920 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer shall request %i peers\n",
1922 rps_peer->num_ids_to_request);
1923}
1924
1925
1926/**
1927 * Callback to call on receipt of a reply
1928 *
1929 * @param cls closure
1930 * @param n number of peers
1931 * @param recv_peers the received peers
1932 */
1933static void
1934profiler_reply_handle (void *cls,
1935 uint64_t n,
1936 const struct GNUNET_PeerIdentity *recv_peers)
1937{
1938 struct RPSPeer *rps_peer;
1939 struct RPSPeer *rcv_rps_peer;
1940 char file_name_buf[128];
1941 char file_name_dh_buf[128];
1942 char file_name_dhr_buf[128];
1943 char file_name_dhru_buf[128];
1944 char *file_name = file_name_buf;
1945 char *file_name_dh = file_name_dh_buf;
1946 char *file_name_dhr = file_name_dhr_buf;
1947 char *file_name_dhru = file_name_dhru_buf;
1948 unsigned int i;
1949 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1950
1951 pending_rep->req_handle = NULL;
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "profiler_reply_handle()\n");
1953 rps_peer = pending_rep->rps_peer;
1954 (void) GNUNET_asprintf (&file_name,
1955 "/tmp/rps/received_ids-%u",
1956 rps_peer->index);
1957
1958 (void) GNUNET_asprintf (&file_name_dh,
1959 "/tmp/rps/diehard_input-%u",
1960 rps_peer->index);
1961 (void) GNUNET_asprintf (&file_name_dhr,
1962 "/tmp/rps/diehard_input_raw-%u",
1963 rps_peer->index);
1964 (void) GNUNET_asprintf (&file_name_dhru,
1965 "/tmp/rps/diehard_input_raw_aligned-%u",
1966 rps_peer->index);
1967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1968 "[%s] got %" PRIu64 " peers:\n",
1969 GNUNET_i2s (rps_peer->peer_id),
1970 n);
1971 for (i = 0; i < n; i++)
1972 {
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1974 "%u: %s\n",
1975 i,
1976 GNUNET_i2s (&recv_peers[i]));
1977 tofile (file_name,
1978 "%s\n",
1979 GNUNET_i2s_full (&recv_peers[i]));
1980 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1981 GNUNET_assert (NULL != rcv_rps_peer);
1982 tofile (file_name_dh,
1983 "%" PRIu32 "\n",
1984 (uint32_t) rcv_rps_peer->index);
1985#ifdef TO_FILE
1986 to_file_raw (file_name_dhr,
1987 (char *) &rcv_rps_peer->index,
1988 sizeof(uint32_t));
1989 to_file_raw_unaligned (file_name_dhru,
1990 (char *) &rcv_rps_peer->index,
1991 sizeof(uint32_t),
1992 bits_needed);
1993#endif /* TO_FILE */
1994 }
1995 default_reply_handle (cls, n, recv_peers);
1996}
1997
1998
1999/**
2000 * Callback to call on receipt of a reply
2001 *
2002 * @param cls closure
2003 * @param n number of peers
2004 * @param recv_peers the received peers
2005 */
2006static void
2007profiler_reply_handle_info (void *cls,
2008 const struct GNUNET_PeerIdentity *recv_peer,
2009 double probability,
2010 uint32_t num_observed)
2011{
2012 struct RPSPeer *rps_peer;
2013 struct RPSPeer *rcv_rps_peer;
2014 char file_name_buf[128];
2015 char file_name_dh_buf[128];
2016 char file_name_dhr_buf[128];
2017 char file_name_dhru_buf[128];
2018 char *file_name = file_name_buf;
2019 char *file_name_dh = file_name_dh_buf;
2020 char *file_name_dhr = file_name_dhr_buf;
2021 char *file_name_dhru = file_name_dhru_buf;
2022 struct PendingReply *pending_rep = (struct PendingReply *) cls;
2023
2024 pending_rep->req_handle = NULL;
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "profiler_reply_handle()\n");
2026 rps_peer = pending_rep->rps_peer;
2027 (void) GNUNET_asprintf (&file_name,
2028 "/tmp/rps/received_ids-%u",
2029 rps_peer->index);
2030
2031 (void) GNUNET_asprintf (&file_name_dh,
2032 "/tmp/rps/diehard_input-%u",
2033 rps_peer->index);
2034 (void) GNUNET_asprintf (&file_name_dhr,
2035 "/tmp/rps/diehard_input_raw-%u",
2036 rps_peer->index);
2037 (void) GNUNET_asprintf (&file_name_dhru,
2038 "/tmp/rps/diehard_input_raw_aligned-%u",
2039 rps_peer->index);
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 "[%s] got peer with info:\n",
2042 GNUNET_i2s (rps_peer->peer_id));
2043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2044 " %s\n",
2045 GNUNET_i2s (recv_peer));
2046 tofile (file_name,
2047 "%s %f %" PRIu32 " \n",
2048 GNUNET_i2s_full (recv_peer),
2049 probability,
2050 num_observed);
2051 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, recv_peer);
2052 GNUNET_assert (NULL != rcv_rps_peer);
2053 tofile (file_name_dh,
2054 "%" PRIu32 "\n",
2055 (uint32_t) rcv_rps_peer->index);
2056#ifdef TO_FILE
2057 to_file_raw (file_name_dhr,
2058 (char *) &rcv_rps_peer->index,
2059 sizeof(uint32_t));
2060 to_file_raw_unaligned (file_name_dhru,
2061 (char *) &rcv_rps_peer->index,
2062 sizeof(uint32_t),
2063 bits_needed);
2064#endif /* TO_FILE */
2065 default_reply_handle (cls, 1, recv_peer);
2066}
2067
2068
2069static void
2070profiler_cb (struct RPSPeer *rps_peer)
2071{
2072 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
2073 {
2074 return;
2075 }
2076
2077 /* Start churn */
2078 if ((HAVE_CHURN == cur_test_run.have_churn) && (NULL == churn_task))
2079 {
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2081 "Starting churn task\n");
2082 churn_task = GNUNET_SCHEDULER_add_delayed (
2083 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
2084 churn,
2085 NULL);
2086 }
2087 else
2088 {
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090 "Not starting churn task\n");
2091 }
2092
2093 /* Only request peer ids at one peer.
2094 * (It's the before-last because last one is target of the focused attack.)
2095 */
2096 if (0 < rps_peer->num_ids_to_request)
2097 schedule_missing_requests (rps_peer);
2098}
2099
2100
2101/**
2102 * Function called from #profiler_eval with a filename.
2103 *
2104 * @param cls closure
2105 * @param filename complete filename (absolute path)
2106 * @return #GNUNET_OK to continue to iterate,
2107 * #GNUNET_NO to stop iteration with no error,
2108 * #GNUNET_SYSERR to abort iteration with error!
2109 */
2110static int
2111file_name_cb (void *cls, const char *filename)
2112{
2113 if (NULL != strstr (filename, "sampler_el"))
2114 {
2115 struct RPS_SamplerElement *s_elem;
2116 struct GNUNET_CRYPTO_AuthKey auth_key;
2117 const char *key_char;
2118 uint32_t i;
2119 (void) cls;
2120
2121 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
2122 tofile (filename, "--------------------------\n");
2123
2124 auth_key = string_to_auth_key (key_char);
2125 s_elem = RPS_sampler_elem_create ();
2126 RPS_sampler_elem_set (s_elem, auth_key);
2127
2128 for (i = 0; i < num_peers; i++)
2129 {
2130 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
2131 }
2132 RPS_sampler_elem_destroy (s_elem);
2133 }
2134 return GNUNET_OK;
2135}
2136
2137
2138/**
2139 * This is run after the test finished.
2140 *
2141 * Compute all perfect samples.
2142 */
2143static int
2144profiler_eval (void)
2145{
2146#ifdef TO_FILE
2147 /* Compute perfect sample for each sampler element */
2148 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
2149 {
2150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
2151 }
2152#endif /* TO_FILE */
2153
2154 return evaluate ();
2155}
2156
2157
2158/** @brief is b in view of a?
2159 *
2160 * @param a
2161 * @param b
2162 *
2163 * @return
2164 */
2165static int
2166is_in_view (uint32_t a, uint32_t b)
2167{
2168 uint32_t i;
2169
2170 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2171 {
2172 if (0 == memcmp (rps_peers[b].peer_id,
2173 &rps_peers[a].cur_view[i],
2174 sizeof(struct GNUNET_PeerIdentity)))
2175 {
2176 return GNUNET_YES;
2177 }
2178 }
2179 return GNUNET_NO;
2180}
2181
2182
2183static uint32_t
2184get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
2185{
2186 uint32_t i;
2187
2188 for (i = 0; i < num_peers; i++)
2189 {
2190 if (0 == memcmp (pid,
2191 rps_peers[i].peer_id,
2192 sizeof(struct GNUNET_PeerIdentity)))
2193 {
2194 return i;
2195 }
2196 }
2197 // return 0; /* Should not happen - make compiler happy */
2198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2199 "No known _PeerIdentity %s!\n",
2200 GNUNET_i2s_full (pid));
2201 GNUNET_assert (0);
2202}
2203
2204
2205/**
2206 * @brief Counts number of peers in view of a that have b in their view
2207 *
2208 * @param a
2209 * @param uint32_tb
2210 *
2211 * @return
2212 */
2213static uint32_t
2214count_containing_views (uint32_t a, uint32_t b)
2215{
2216 uint32_t i;
2217 uint32_t peer_idx;
2218 uint32_t count = 0;
2219
2220 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2221 {
2222 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
2223 if (GNUNET_YES == is_in_view (peer_idx, b))
2224 {
2225 count++;
2226 }
2227 }
2228 return count;
2229}
2230
2231
2232/**
2233 * @brief Computes the probability for each other peer to be selected by the
2234 * sampling process based on the views of all peers
2235 *
2236 * @param peer_idx index of the peer that is about to sample
2237 */
2238static void
2239compute_probabilities (uint32_t peer_idx)
2240{
2241 // double probs[num_peers] = { 0 };
2242 double probs[num_peers];
2243 double probs_hist[num_peers]; /* Probability respecting the history */
2244 size_t probs_as_str_size = (num_peers * 10 + 2) * sizeof(char);
2245 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2246 char *probs_as_str_cpy;
2247 uint32_t i;
2248 double prob_push;
2249 double prob_pull;
2250 uint32_t view_size;
2251 uint32_t cont_views;
2252 uint32_t number_of_being_in_pull_events;
2253 int tmp;
2254 double sum_non_zero_prob = 0;
2255 double sum_non_zero_prob_hist = 0;
2256
2257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2258 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2259 /* Firstly without knowledge of old views */
2260 for (i = 0; i < num_peers; i++)
2261 {
2262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2263 "\tfor peer %" PRIu32 ":\n", i);
2264 view_size = rps_peers[i].cur_view_count;
2265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2266 "\t\tview_size: %" PRIu32 "\n", view_size);
2267 /* For peer i the probability of being sampled is
2268 * evenly distributed among all possibly observed peers. */
2269 /* We could have observed a peer in three cases:
2270 * 1. peer sent a push
2271 * 2. peer was contained in a pull reply
2272 * 3. peer was in history (sampler) - ignored for now */
2273 /* 1. Probability of having received a push from peer i */
2274 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2275 (1 <= (0.45 * view_size)))
2276 {
2277 if (0 == binom (view_size, 0.45 * view_size))
2278 prob_push = 0;
2279 else
2280 {
2281 prob_push = 1.0 * binom (0.45 * view_size, 1)
2282 /
2283 binom (view_size, 0.45 * view_size);
2284 }
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2286 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2287 peer_idx,
2288 i,
2289 prob_push);
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2291 "\t\tposs choices from view: %" PRIu32 ", containing i: %"
2292 PRIu32 "\n",
2293 binom (view_size, 0.45 * view_size),
2294 binom (0.45 * view_size, 1));
2295 }
2296 else
2297 {
2298 prob_push = 0;
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2300 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2301 peer_idx,
2302 i);
2303 }
2304 /* 2. Probability of peer i being contained in pulls */
2305 view_size = rps_peers[peer_idx].cur_view_count;
2306 cont_views = count_containing_views (peer_idx, i);
2307 number_of_being_in_pull_events =
2308 (binom (view_size, 0.45 * view_size)
2309 - binom (view_size - cont_views, 0.45 * view_size));
2310 if (0 != number_of_being_in_pull_events)
2311 {
2312 prob_pull = number_of_being_in_pull_events
2313 /
2314 (1.0 * binom (view_size, 0.45 * view_size));
2315 }
2316 else
2317 {
2318 prob_pull = 0;
2319 }
2320 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2323 " peers in its view who know %" PRIu32 " prob: %f\n",
2324 peer_idx,
2325 cont_views,
2326 view_size,
2327 i,
2328 prob_pull);
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2330 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2331 binom (view_size, 0.45 * view_size));
2332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333 "\t\tnumber of possible pull combinations without %" PRIu32
2334 ": %" PRIu32 "\n",
2335 i,
2336 binom (view_size - cont_views, 0.45 * view_size));
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2338 "\t\tnumber of possible pull combinations with %" PRIu32
2339 ": %" PRIu32 "\n",
2340 i,
2341 number_of_being_in_pull_events);
2342
2343 probs_hist[i] = 0.9 * rps_peers[peer_idx].eval_probs_cache[i] + probs[i];
2344 rps_peers[peer_idx].eval_probs_cache[i] = probs_hist[i];
2345
2346 sum_non_zero_prob += probs[i];
2347 sum_non_zero_prob_hist += probs_hist[i];
2348 }
2349 /* normalize */
2350 if (0 != sum_non_zero_prob)
2351 {
2352 for (i = 0; i < num_peers; i++)
2353 {
2354 probs[i] = probs[i] * (1.0 / sum_non_zero_prob);
2355 }
2356 }
2357 if (0 != sum_non_zero_prob_hist)
2358 {
2359 for (i = 0; i < num_peers; i++)
2360 {
2361 probs_hist[i] = probs_hist[i] * (1.0 / sum_non_zero_prob_hist);
2362 }
2363 }
2364
2365 /* str repr */
2366 for (i = 0; i < num_peers; i++)
2367 {
2368 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2369 tmp = GNUNET_snprintf (probs_as_str,
2370 probs_as_str_size,
2371 "%s %7.6f", probs_as_str_cpy, probs[i]);
2372 GNUNET_free (probs_as_str_cpy);
2373 GNUNET_assert (0 <= tmp);
2374 }
2375
2376 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2377 probs_as_str_size,
2378 "%s",
2379 probs_as_str);
2380
2381 probs_as_str[0] = '\0';
2382 for (i = 0; i < num_peers; i++)
2383 {
2384 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2385 tmp = GNUNET_snprintf (probs_as_str,
2386 probs_as_str_size,
2387 "%s %7.6f", probs_as_str_cpy, probs_hist[i]);
2388 GNUNET_free (probs_as_str_cpy);
2389 GNUNET_assert (0 <= tmp);
2390 }
2391
2392 to_file_w_len (rps_peers[peer_idx].file_name_probs_hist,
2393 probs_as_str_size,
2394 "%s",
2395 probs_as_str);
2396 GNUNET_free (probs_as_str);
2397}
2398
2399
2400/**
2401 * @brief This counts the number of peers in which views a given peer occurs.
2402 *
2403 * It also stores this value in the rps peer.
2404 *
2405 * @param peer_idx the index of the peer to count the representation
2406 *
2407 * @return the number of occurrences
2408 */
2409static uint32_t
2410count_peer_in_views_2 (uint32_t peer_idx)
2411{
2412 uint32_t i, j;
2413 uint32_t count = 0;
2414
2415 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2416 {
2417 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2418 {
2419 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2420 &rps_peers[i].cur_view[j],
2421 sizeof(struct GNUNET_PeerIdentity)))
2422 {
2423 count++;
2424 break;
2425 }
2426 }
2427 }
2428 rps_peers[peer_idx].count_in_views = count;
2429 return count;
2430}
2431
2432
2433static uint32_t
2434cumulated_view_sizes ()
2435{
2436 uint32_t i;
2437
2438 view_sizes = 0;
2439 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2440 {
2441 view_sizes += rps_peers[i].cur_view_count;
2442 }
2443 return view_sizes;
2444}
2445
2446
2447static void
2448count_peer_in_views (uint32_t *count_peers)
2449{
2450 uint32_t i, j;
2451
2452 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2453 {
2454 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2455 {
2456 if (0 == memcmp (rps_peers[i].peer_id,
2457 &rps_peers[i].cur_view[j],
2458 sizeof(struct GNUNET_PeerIdentity)))
2459 {
2460 count_peers[i]++;
2461 }
2462 }
2463 }
2464}
2465
2466
2467void
2468compute_diversity ()
2469{
2470 uint32_t i;
2471 /* ith entry represents the number of occurrences in other peer's views */
2472 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2473 uint32_t views_total_size;
2474 double expected;
2475 /* deviation from expected number of peers */
2476 double *deviation = GNUNET_new_array (num_peers, double);
2477
2478 views_total_size = 0;
2479 expected = 0;
2480
2481 /* For each peer count its representation in other peer's views*/
2482 for (i = 0; i < num_peers; i++) /* Peer to count */
2483 {
2484 views_total_size += rps_peers[i].cur_view_count;
2485 count_peer_in_views (count_peers);
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32
2488 "\n",
2489 i,
2490 GNUNET_i2s (rps_peers[i].peer_id),
2491 count_peers[i]);
2492 }
2493
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "size of all views combined: %" PRIu32 "\n",
2496 views_total_size);
2497 expected = ((double) 1 / num_peers) * views_total_size;
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "Expected number of occurrences of each peer in all views: %f\n",
2500 expected);
2501 for (i = 0; i < num_peers; i++) /* Peer to count */
2502 {
2503 deviation[i] = expected - count_peers[i];
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "Deviation from expectation: %f\n", deviation[i]);
2506 }
2507 GNUNET_free (count_peers);
2508 GNUNET_free (deviation);
2509}
2510
2511
2512void
2513print_view_sizes ()
2514{
2515 uint32_t i;
2516
2517 for (i = 0; i < num_peers; i++) /* Peer to count */
2518 {
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2521 i,
2522 GNUNET_i2s (rps_peers[i].peer_id),
2523 rps_peers[i].cur_view_count);
2524 }
2525}
2526
2527
2528void
2529all_views_updated_cb ()
2530{
2531 compute_diversity ();
2532 print_view_sizes ();
2533}
2534
2535
2536void
2537view_update_cb (void *cls,
2538 uint64_t view_size,
2539 const struct GNUNET_PeerIdentity *peers)
2540{
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "View was updated (%" PRIu64 ")\n", view_size);
2543 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2544 to_file ("/tmp/rps/view_sizes.txt",
2545 "%" PRIu32 " %" PRIu64 "",
2546 rps_peer->index,
2547 view_size);
2548 for (uint64_t i = 0; i < view_size; i++)
2549 {
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2551 "\t%s\n", GNUNET_i2s (&peers[i]));
2552 }
2553 GNUNET_array_grow (rps_peer->cur_view,
2554 rps_peer->cur_view_count,
2555 view_size);
2556 // *rps_peer->cur_view = *peers;
2557 GNUNET_memcpy (rps_peer->cur_view,
2558 peers,
2559 view_size * sizeof(struct GNUNET_PeerIdentity));
2560 to_file ("/tmp/rps/count_in_views.txt",
2561 "%" PRIu32 " %" PRIu32 "",
2562 rps_peer->index,
2563 count_peer_in_views_2 (rps_peer->index));
2564 cumulated_view_sizes ();
2565 if (0 != view_size)
2566 {
2567 to_file ("/tmp/rps/repr.txt",
2568 "%" PRIu32 /* index */
2569 " %" PRIu32 /* occurrence in views */
2570 " %" PRIu32 /* view sizes */
2571 " %f" /* fraction of repr in views */
2572 " %f" /* average view size */
2573 " %f" /* prob of occurrence in view slot */
2574 " %f" "", /* exp frac of repr in views */
2575 rps_peer->index,
2576 count_peer_in_views_2 (rps_peer->index),
2577 view_sizes,
2578 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2579 view_sizes / (view_size * 1.0), /* average view size */
2580 1.0 / view_size, /* prob of occurrence in view slot */
2581 (1.0 / view_size) * (view_sizes / view_size) /* expected fraction of repr in views */
2582 );
2583 }
2584 compute_probabilities (rps_peer->index);
2585 all_views_updated_cb ();
2586}
2587
2588
2589static void
2590pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2591{
2592 rps_peer->file_name_probs =
2593 store_prefix_file_name (rps_peer->index, "probs");
2594 rps_peer->file_name_probs_hist =
2595 store_prefix_file_name (rps_peer->index, "probs_hist");
2596 rps_peer->eval_probs_cache = GNUNET_new_array (num_peers, double);
2597 memset (rps_peer->eval_probs_cache, 0, num_peers * sizeof (double));
2598 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2599}
2600
2601void
2602write_final_stats (void)
2603{
2604 uint64_t sums[STAT_TYPE_MAX] = { 0 };
2605
2606 for (uint32_t i = 0; i < num_peers; i++)
2607 {
2608 to_file ("/tmp/rps/final_stats.csv",
2609 "%" PRIu32 ", " /* index */
2610 "%s, %" /* id */
2611 PRIu64 ", %" /* rounds */
2612 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %"
2613 PRIu64 ", %" /* blocking */
2614 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
2615 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
2616 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %"
2617 PRIu64 ", %" /* recv */
2618 PRIu64 ", %" /* view size */
2619 PRIu64 ", %" /* known peers */
2620 PRIu64 ", %" /* valid peers */
2621 PRIu64 ", %" /* learned peers */
2622 PRIu64 ", %" /* pending online checks */
2623 PRIu64 ", %" /* unrequested pull replies */
2624 PRIu64 ", %" /* peers in push map */
2625 PRIu64 ", %" /* peers in pull map */
2626 PRIu64 ", %" /* peers in view */
2627 PRIu64 "\n" /* view size aim */,
2628 i,
2629 GNUNET_i2s (rps_peers[i].peer_id),
2630 rps_peers[i].stats[STAT_TYPE_ROUNDS],
2631 rps_peers[i].stats[STAT_TYPE_BLOCKS],
2632 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH],
2633 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH],
2634 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PULL],
2635 rps_peers[i].stats[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2636 rps_peers[i].stats[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2637 rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND],
2638 rps_peers[i].stats[STAT_TYPE_ISSUED_PUSH_SEND_MH],
2639 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ],
2640 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REQ_MH],
2641 rps_peers[i].stats[STAT_TYPE_ISSUED_PULL_REP],
2642 rps_peers[i].stats[STAT_TYPE_SENT_PUSH_SEND],
2643 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ],
2644 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REQ_MH],
2645 rps_peers[i].stats[STAT_TYPE_SENT_PULL_REP],
2646 rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND],
2647 rps_peers[i].stats[STAT_TYPE_RECV_PUSH_SEND_MH],
2648 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ],
2649 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REQ_MH],
2650 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP_MH],
2651 rps_peers[i].stats[STAT_TYPE_RECV_PULL_REP],
2652 rps_peers[i].stats[STAT_TYPE_VIEW_SIZE],
2653 rps_peers[i].stats[STAT_TYPE_KNOWN_PEERS],
2654 rps_peers[i].stats[STAT_TYPE_VALID_PEERS],
2655 rps_peers[i].stats[STAT_TYPE_LEARND_PEERS],
2656 rps_peers[i].stats[STAT_TYPE_PENDING_ONLINE_CHECKS],
2657 rps_peers[i].stats[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
2658 rps_peers[i].stats[STAT_TYPE_PEERS_IN_PUSH_MAP],
2659 rps_peers[i].stats[STAT_TYPE_PEERS_IN_PULL_MAP],
2660 rps_peers[i].stats[STAT_TYPE_PEERS_IN_VIEW],
2661 rps_peers[i].stats[STAT_TYPE_VIEW_SIZE_AIM]);
2662 for (enum STAT_TYPE stat_type = STAT_TYPE_ROUNDS;
2663 stat_type < STAT_TYPE_MAX;
2664 stat_type++)
2665 {
2666 sums[stat_type] += rps_peers[i].stats[stat_type];
2667 }
2668 }
2669 to_file ("/tmp/rps/final_stats.dat",
2670 "SUM %"
2671 PRIu64 " %" /* rounds */
2672 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
2673 " %" /* blocking */
2674 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* issued */
2675 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" /* sent */
2676 PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %"
2677 PRIu64 ", %" /* recv */
2678 PRIu64 ", %" /* view size */
2679 PRIu64 ", %" /* known peers */
2680 PRIu64 ", %" /* valid peers */
2681 PRIu64 ", %" /* learned peers */
2682 PRIu64 ", %" /* pending online checks */
2683 PRIu64 ", %" /* unrequested pull replies */
2684 PRIu64 ", %" /* peers in push map */
2685 PRIu64 ", %" /* peers in pull map */
2686 PRIu64 ", %" /* peers in view */
2687 PRIu64 "\n" /* view size aim */,
2688 sums[STAT_TYPE_ROUNDS],
2689 sums[STAT_TYPE_BLOCKS],
2690 sums[STAT_TYPE_BLOCKS_MANY_PUSH],
2691 sums[STAT_TYPE_BLOCKS_NO_PUSH],
2692 sums[STAT_TYPE_BLOCKS_NO_PULL],
2693 sums[STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL],
2694 sums[STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL],
2695 sums[STAT_TYPE_ISSUED_PUSH_SEND],
2696 sums[STAT_TYPE_ISSUED_PUSH_SEND_MH],
2697 sums[STAT_TYPE_ISSUED_PULL_REQ],
2698 sums[STAT_TYPE_ISSUED_PULL_REQ_MH],
2699 sums[STAT_TYPE_ISSUED_PULL_REP],
2700 sums[STAT_TYPE_SENT_PUSH_SEND],
2701 sums[STAT_TYPE_SENT_PULL_REQ],
2702 sums[STAT_TYPE_SENT_PULL_REQ_MH],
2703 sums[STAT_TYPE_SENT_PULL_REP],
2704 sums[STAT_TYPE_RECV_PUSH_SEND],
2705 sums[STAT_TYPE_RECV_PUSH_SEND_MH],
2706 sums[STAT_TYPE_RECV_PULL_REQ],
2707 sums[STAT_TYPE_RECV_PULL_REQ_MH],
2708 sums[STAT_TYPE_RECV_PULL_REP],
2709 sums[STAT_TYPE_RECV_PULL_REP_MH],
2710 sums[STAT_TYPE_VIEW_SIZE],
2711 sums[STAT_TYPE_KNOWN_PEERS],
2712 sums[STAT_TYPE_VALID_PEERS],
2713 sums[STAT_TYPE_LEARND_PEERS],
2714 sums[STAT_TYPE_PENDING_ONLINE_CHECKS],
2715 sums[STAT_TYPE_UNREQUESTED_PULL_REPLIES],
2716 sums[STAT_TYPE_PEERS_IN_PUSH_MAP],
2717 sums[STAT_TYPE_PEERS_IN_PULL_MAP],
2718 sums[STAT_TYPE_PEERS_IN_VIEW],
2719 sums[STAT_TYPE_VIEW_SIZE_AIM]);
2720}
2721
2722
2723/**
2724 * Continuation called by #GNUNET_STATISTICS_get() functions.
2725 *
2726 * Remembers that this specific statistics value was received for this peer.
2727 * Checks whether all peers received their statistics yet.
2728 * Issues the shutdown.
2729 *
2730 * @param cls closure
2731 * @param success #GNUNET_OK if statistics were
2732 * successfully obtained, #GNUNET_SYSERR if not.
2733 */
2734void
2735post_test_shutdown_ready_cb (void *cls,
2736 int success)
2737{
2738 struct STATcls *stat_cls = (struct STATcls *) cls;
2739 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2740
2741 rps_peer->h_stat_get[stat_cls->stat_type] = NULL;
2742 if (GNUNET_OK == success)
2743 {
2744 /* set flag that we we got the value */
2745 rps_peer->stat_collected_flags |= BIT (stat_cls->stat_type);
2746 }
2747 else
2748 {
2749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2750 "Peer %u did not receive statistics value\n",
2751 rps_peer->index);
2752 GNUNET_free (stat_cls);
2753 GNUNET_break (0);
2754 return;
2755 }
2756
2757 if ((NULL != rps_peer->stat_op) &&
2758 (GNUNET_YES == check_statistics_collect_completed_single_peer (
2759 rps_peer)) )
2760 {
2761 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2762 }
2763
2764 write_final_stats ();
2765 if (GNUNET_YES == check_statistics_collect_completed ())
2766 {
2767 // write_final_stats ();
2768 GNUNET_free (stat_cls);
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2770 "Shutting down\n");
2771 GNUNET_SCHEDULER_shutdown ();
2772 }
2773 else
2774 {
2775 GNUNET_free (stat_cls);
2776 }
2777}
2778
2779
2780/**
2781 * Callback function to process statistic values.
2782 *
2783 * @param cls closure
2784 * @param subsystem name of subsystem that created the statistic
2785 * @param name the name of the datum
2786 * @param value the current value
2787 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2788 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2789 */
2790int
2791stat_iterator (void *cls,
2792 const char *subsystem,
2793 const char *name,
2794 uint64_t value,
2795 int is_persistent)
2796{
2797 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2798 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2799 enum STAT_TYPE stat_type;
2800
2801 (void) subsystem;
2802 (void) is_persistent;
2803
2804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2805 "Got stat value: %s - %" PRIu64 " (%u)\n",
2806 name,
2807 value,
2808 rps_peer->index);
2809 to_file (rps_peer->file_name_stats,
2810 "%s: %" PRIu64 "\n",
2811 name,
2812 value);
2813 stat_type = stat_str_2_type (name);
2814 GNUNET_assert (STAT_TYPE_ROUNDS <= stat_type &&
2815 STAT_TYPE_MAX > stat_type);
2816 rps_peer->stats[stat_type] = value;
2817 return GNUNET_OK;
2818}
2819
2820
2821void
2822post_profiler (struct RPSPeer *rps_peer)
2823{
2824 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2825 {
2826 return;
2827 }
2828
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2830 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2831 cur_test_run.stat_collect_flags);
2832
2833 struct STATcls *stat_cls;
2834 uint32_t stat_type;
2835 for (stat_type = STAT_TYPE_ROUNDS;
2836 stat_type < STAT_TYPE_MAX;
2837 stat_type++)
2838 {
2839 if (BIT (stat_type) & cur_test_run.stat_collect_flags)
2840 {
2841 stat_cls = GNUNET_malloc (sizeof(struct STATcls));
2842 stat_cls->rps_peer = rps_peer;
2843 stat_cls->stat_type = stat_type;
2844 rps_peer->file_name_stats =
2845 store_prefix_file_name (rps_peer->index, "stats");
2846 rps_peer->h_stat_get[stat_type] =
2847 GNUNET_STATISTICS_get (rps_peer->stats_h,
2848 "rps",
2849 stat_type_strings [stat_type],
2850 post_test_shutdown_ready_cb,
2851 stat_iterator,
2852 (struct STATcls *) stat_cls);
2853 GNUNET_assert (NULL != rps_peer->h_stat_get);
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2855 "Requested statistics for %s (peer %" PRIu32 ")\n",
2856 stat_type_strings [stat_type],
2857 rps_peer->index);
2858 }
2859 }
2860 GNUNET_free (rps_peer->eval_probs_cache);
2861}
2862
2863
2864/***********************************************************************
2865* /Definition of tests
2866***********************************************************************/
2867
2868
2869/**
2870 * Actual "main" function for the testcase.
2871 *
2872 * @param cls closure
2873 * @param h the run handle
2874 * @param n_peers number of peers in 'peers'
2875 * @param peers handle to peers run in the testbed
2876 * @param links_succeeded the number of overlay link connection attempts that
2877 * succeeded
2878 * @param links_failed the number of overlay link connection attempts that
2879 * failed
2880 */
2881static void
2882test_run (void *cls,
2883 struct GNUNET_TESTBED_RunHandle *h,
2884 unsigned int n_peers,
2885 struct GNUNET_TESTBED_Peer **peers,
2886 unsigned int links_succeeded,
2887 unsigned int links_failed)
2888{
2889 unsigned int i;
2890 struct OpListEntry *entry;
2891
2892 (void) cls;
2893 (void) h;
2894 (void) links_failed;
2895
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2897
2898 /* Check whether we timed out */
2899 if ((n_peers != num_peers) ||
2900 (NULL == peers) ||
2901 (0 == links_succeeded) )
2902 {
2903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2904 "Going down due to args (eg. timeout)\n");
2905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n",
2907 num_peers);
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n",
2910 links_succeeded);
2911 ok = 1;
2912 GNUNET_SCHEDULER_shutdown ();
2913 return;
2914 }
2915
2916
2917 /* Initialize peers */
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "going to initialise peers\n");
2919 testbed_peers = peers;
2920 num_peers_online = 0;
2921 for (i = 0; i < num_peers; i++)
2922 {
2923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "initialising %u\n", i);
2924 entry = make_oplist_entry ();
2925 entry->index = i;
2926 rps_peers[i].index = i;
2927 if (NULL != cur_test_run.init_peer)
2928 cur_test_run.init_peer (&rps_peers[i]);
2929 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2930 {
2931 rps_peers->cur_view_count = 0;
2932 rps_peers->cur_view = NULL;
2933 }
2934 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2935 GNUNET_TESTBED_PIT_IDENTITY,
2936 &info_cb,
2937 entry);
2938 }
2939
2940 /* Bring peers up */
2941 GNUNET_assert (num_peers == n_peers);
2942 for (i = 0; i < n_peers; i++)
2943 {
2944 rps_peers[i].index = i;
2945 rps_peers[i].op =
2946 GNUNET_TESTBED_service_connect (&rps_peers[i],
2947 peers[i],
2948 "rps",
2949 &rps_connect_complete_cb,
2950 &rps_peers[i],
2951 &rps_connect_adapter,
2952 &rps_disconnect_adapter,
2953 &rps_peers[i]);
2954 /* Connect all peers to statistics service */
2955 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2956 {
2957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2958 "Connecting to statistics service\n");
2959 rps_peers[i].stat_op =
2960 GNUNET_TESTBED_service_connect (NULL,
2961 peers[i],
2962 "statistics",
2963 stat_complete_cb,
2964 &rps_peers[i],
2965 &stat_connect_adapter,
2966 &stat_disconnect_adapter,
2967 &rps_peers[i]);
2968 }
2969 }
2970
2971 if (NULL != churn_task)
2972 GNUNET_SCHEDULER_cancel (churn_task);
2973 post_test_task = GNUNET_SCHEDULER_add_delayed (duration, &post_test_op, NULL);
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout for shutdown is %lu\n",
2975 timeout.rel_value_us / 1000000);
2976 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout,
2977 &trigger_shutdown,
2978 NULL);
2979 GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
2980}
2981
2982
2983/**
2984 * Entry point for the testcase, sets up the testbed.
2985 *
2986 * @param argc unused
2987 * @param argv unused
2988 */
2989static void
2990run (void *cls,
2991 char *const *args,
2992 const char *cfgfile,
2993 const struct GNUNET_CONFIGURATION_Handle *cfg)
2994{
2995 // int ret_value;
2996 (void) cls;
2997 (void) args;
2998 (void) cfgfile;
2999
3000 /* Defaults for tests */
3001 churn_task = NULL;
3002
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
3004 cur_test_run.name = "test-rps-profiler";
3005 if (0 == num_peers)
3006 num_peers = 10;
3007 mal_type = 3;
3008 cur_test_run.init_peer = profiler_init_peer;
3009 // cur_test_run.pre_test = mal_pre;
3010 cur_test_run.pre_test = pre_profiler;
3011 cur_test_run.main_test = profiler_cb;
3012 cur_test_run.reply_handle = profiler_reply_handle;
3013 cur_test_run.eval_cb = profiler_eval;
3014 cur_test_run.post_test = post_profiler;
3015 cur_test_run.request_interval = 2;
3016 if (0 == cur_test_run.num_requests)
3017 cur_test_run.num_requests = 5;
3018 // cur_test_run.have_churn = HAVE_CHURN;
3019 cur_test_run.have_churn = HAVE_NO_CHURN;
3020 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
3021 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
3022 cur_test_run.stat_collect_flags = BIT (STAT_TYPE_ROUNDS)
3023 | BIT (STAT_TYPE_BLOCKS)
3024 | BIT (STAT_TYPE_BLOCKS_MANY_PUSH)
3025 | BIT (STAT_TYPE_BLOCKS_NO_PUSH)
3026 | BIT (STAT_TYPE_BLOCKS_NO_PULL)
3027 | BIT (STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL)
3028 | BIT (STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL)
3029 | BIT (STAT_TYPE_ISSUED_PUSH_SEND)
3030 | BIT (STAT_TYPE_ISSUED_PUSH_SEND_MH)
3031 | BIT (STAT_TYPE_ISSUED_PULL_REQ)
3032 | BIT (STAT_TYPE_ISSUED_PULL_REQ_MH)
3033 | BIT (STAT_TYPE_ISSUED_PULL_REP)
3034 | BIT (STAT_TYPE_SENT_PUSH_SEND)
3035 | BIT (STAT_TYPE_SENT_PULL_REQ)
3036 | BIT (STAT_TYPE_SENT_PULL_REQ_MH)
3037 | BIT (STAT_TYPE_SENT_PULL_REP)
3038 | BIT (STAT_TYPE_RECV_PUSH_SEND)
3039 | BIT (STAT_TYPE_RECV_PUSH_SEND_MH)
3040 | BIT (STAT_TYPE_RECV_PULL_REQ)
3041 | BIT (STAT_TYPE_RECV_PULL_REQ_MH)
3042 | BIT (STAT_TYPE_RECV_PULL_REP)
3043 | BIT (STAT_TYPE_RECV_PULL_REP_MH)
3044 | BIT (STAT_TYPE_VIEW_SIZE)
3045 | BIT (STAT_TYPE_KNOWN_PEERS)
3046 | BIT (STAT_TYPE_VALID_PEERS)
3047 | BIT (STAT_TYPE_LEARND_PEERS)
3048 | BIT (STAT_TYPE_PENDING_ONLINE_CHECKS)
3049 | BIT (STAT_TYPE_UNREQUESTED_PULL_REPLIES)
3050 | BIT (STAT_TYPE_PEERS_IN_PUSH_MAP)
3051 | BIT (STAT_TYPE_PEERS_IN_PULL_MAP)
3052 | BIT (STAT_TYPE_PEERS_IN_VIEW)
3053 | BIT (STAT_TYPE_VIEW_SIZE_AIM);
3054 cur_test_run.have_collect_view = COLLECT_VIEW;
3055
3056 /* 'Clean' directory */
3057 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
3058 GNUNET_DISK_directory_create ("/tmp/rps/");
3059 if (0 == duration.rel_value_us)
3060 {
3061 if (0 == timeout.rel_value_us)
3062 {
3063 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
3064 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3065 (90 * 1.2)
3066 + (0.01 * num_peers));
3067 }
3068 else
3069 {
3070 duration = GNUNET_TIME_relative_multiply (
3071 GNUNET_TIME_UNIT_SECONDS,
3072 ( (double) timeout.rel_value_us / (double) 1000000)
3073 * 0.75);
3074 }
3075 }
3076 else
3077 {
3078 if (0 == timeout.rel_value_us)
3079 {
3080 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3081 ((duration.rel_value_us
3082 / 1000000)
3083 * 1.2) + (0.01 * num_peers));
3084 }
3085 }
3086 GNUNET_assert (duration.rel_value_us < timeout.rel_value_us);
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3088 "duration is %lus\n",
3089 duration.rel_value_us / 1000000);
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 "timeout is %lus\n",
3092 timeout.rel_value_us / 1000000);
3093
3094 /* Compute number of bits for representing largest peer id */
3095 for (bits_needed = 1; (uint32_t) (1 << bits_needed) < num_peers; bits_needed++)
3096 ;
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 "Need %u bits to represent %" PRIu32 " peers\n",
3099 bits_needed,
3100 num_peers);
3101
3102 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
3103 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
3104 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
3105 if ((2 == mal_type) ||
3106 (3 == mal_type))
3107 target_peer = &rps_peer_ids[num_peers - 2];
3108
3109 ok = 1;
3110 GNUNET_TESTBED_run (NULL,
3111 cfg,
3112 num_peers,
3113 0, /* event mask */
3114 NULL,
3115 NULL,
3116 &test_run,
3117 NULL);
3118}
3119
3120
3121/**
3122 * Entry point for the testcase, sets up the testbed.
3123 *
3124 * @param argc unused
3125 * @param argv unused
3126 * @return 0 on success
3127 */
3128int
3129main (int argc, char *argv[])
3130{
3131 int ret_value;
3132 struct GNUNET_GETOPT_CommandLineOption options[] = {
3133 GNUNET_GETOPT_option_uint ('n',
3134 "num-peers",
3135 "COUNT",
3136 gettext_noop ("number of peers to start"),
3137 &num_peers),
3138 GNUNET_GETOPT_option_relative_time ('d',
3139 "duration",
3140 "DURATION",
3141 gettext_noop (
3142 "duration of the profiling"),
3143 &duration),
3144 GNUNET_GETOPT_option_relative_time ('t',
3145 "timeout",
3146 "TIMEOUT",
3147 gettext_noop (
3148 "timeout for the profiling"),
3149 &timeout),
3150 GNUNET_GETOPT_option_uint ('r',
3151 "num-requests",
3152 "COUNT",
3153 gettext_noop ("number of PeerIDs to request"),
3154 &cur_test_run.num_requests),
3155 GNUNET_GETOPT_OPTION_END
3156 };
3157
3158 unsetenv ("XDG_DATA_HOME");
3159 unsetenv ("XDG_CONFIG_HOME");
3160 // if (GNUNET_OK !=
3161 // GNUNET_STRINGS_get_utf8_args (argc, argv,
3162 // &argc, &argv))
3163 // return 2;
3164 ret_value = 0;
3165 if (GNUNET_OK !=
3166 GNUNET_PROGRAM_run (argc,
3167 argv,
3168 "gnunet-rps-profiler",
3169 gettext_noop (
3170 "Measure quality and performance of the RPS service."),
3171 options,
3172 &run,
3173 NULL))
3174 {
3175 ret_value = 1;
3176 }
3177 if (0 != ret_value)
3178 {
3179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3180 "Test did not run successfully!\n");
3181 }
3182 else
3183 {
3184 ret_value = cur_test_run.eval_cb ();
3185 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
3186 {
3187 GNUNET_array_grow (rps_peers->cur_view,
3188 rps_peers->cur_view_count,
3189 0);
3190 }
3191 GNUNET_free (rps_peers);
3192 GNUNET_free (rps_peer_ids);
3193 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
3194 }
3195 return ret_value;
3196}
3197
3198
3199/* end of test_rps.c */
diff --git a/src/rps/gnunet-rps.c b/src/rps/gnunet-rps.c
deleted file mode 100644
index 33c03f006..000000000
--- a/src/rps/gnunet-rps.c
+++ /dev/null
@@ -1,294 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-rps.c
23 * @brief random peer sampling
24 * @author Julius Bünger
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_rps_service.h"
29#include <inttypes.h>
30
31static int ret;
32
33/**
34 * RPS handle
35 */
36static struct GNUNET_RPS_Handle *rps_handle;
37
38/**
39 * Request handle
40 */
41static struct GNUNET_RPS_Request_Handle *req_handle;
42
43/**
44 * PeerID (Option --seed)
45 */
46static struct GNUNET_PeerIdentity peer_id;
47
48/**
49 * @brief Do we want to receive updates of the view? (Option --view)
50 */
51static int view_update;
52
53/**
54 * @brief Do we want to receive updates of the view? (Option --view)
55 */
56static int stream_input;
57
58/**
59 * @brief Number of updates we want to receive
60 */
61static uint64_t num_view_updates;
62
63
64/**
65 * Task run when user presses CTRL-C to abort.
66 * Cancels pending request and disconnects.
67 *
68 * @param cls NULL
69 */
70static void
71do_shutdown (void *cls)
72{
73 (void) cls;
74
75 if (NULL != req_handle)
76 GNUNET_RPS_request_cancel (req_handle);
77 GNUNET_RPS_disconnect (rps_handle);
78}
79
80
81/**
82 * Callback called on receipt of reply.
83 * Prints replied PeerIDs.
84 *
85 * @param cls closure
86 * @param n number of peers
87 * @param recv_peers the received peers
88 */
89static void
90reply_handle (void *cls,
91 uint64_t n,
92 const struct GNUNET_PeerIdentity *recv_peers)
93{
94 uint64_t i;
95
96 (void) cls;
97
98 req_handle = NULL;
99 for (i = 0; i < n; i++)
100 {
101 fprintf (stdout, "%s\n",
102 GNUNET_i2s_full (&recv_peers[i]));
103 }
104 ret = 0;
105
106 GNUNET_SCHEDULER_shutdown ();
107}
108
109
110/**
111 * Callback called on receipt view update.
112 * Prints view.
113 *
114 * @param n number of peers
115 * @param recv_peers the received peers
116 */
117static void
118view_update_handle (void *cls,
119 uint64_t n,
120 const struct GNUNET_PeerIdentity *recv_peers)
121{
122 uint64_t i;
123
124 (void) cls;
125
126 if (0 == n)
127 {
128 fprintf (stdout, "Empty view\n");
129 }
130 req_handle = NULL;
131 for (i = 0; i < n; i++)
132 {
133 fprintf (stdout, "%s\n",
134 GNUNET_i2s_full (&recv_peers[i]));
135 }
136
137 if (1 == num_view_updates)
138 {
139 ret = 0;
140 GNUNET_SCHEDULER_shutdown ();
141 }
142 else if (1 < num_view_updates)
143 {
144 num_view_updates--;
145 }
146}
147
148
149/**
150 * Callback called on receipt of peer from biased stream
151 *
152 * @param n number of peers
153 * @param recv_peers the received peers
154 */
155static void
156stream_input_handle (void *cls,
157 uint64_t num_peers,
158 const struct GNUNET_PeerIdentity *recv_peers)
159{
160 uint64_t i;
161
162 (void) cls;
163
164 if (0 == num_peers)
165 {
166 fprintf (stdout, "No peer was returned\n");
167 }
168 req_handle = NULL;
169 for (i = 0; i < num_peers; i++)
170 {
171 fprintf (stdout, "%s\n",
172 GNUNET_i2s_full (&recv_peers[i]));
173 }
174}
175
176
177/**
178 * Main function that will be run by the scheduler.
179 *
180 * @param cls closure
181 * @param args remaining command-line arguments
182 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
183 * @param cfg configuration
184 */
185static void
186run (void *cls,
187 char *const *args,
188 const char *cfgfile,
189 const struct GNUNET_CONFIGURATION_Handle *cfg)
190{
191 static uint64_t num_peers;
192 static struct GNUNET_PeerIdentity zero_pid;
193
194 (void) cls;
195 (void) cfgfile;
196
197 rps_handle = GNUNET_RPS_connect (cfg);
198 if (NULL == rps_handle)
199 {
200 fprintf (stderr, "Failed to connect to the rps service\n");
201 return;
202 }
203
204 if ((0 == memcmp (&zero_pid, &peer_id, sizeof(peer_id))) &&
205 (! view_update) &&
206 (! stream_input))
207 { /* Request n PeerIDs */
208 /* If number was specified use it, else request single peer. */
209 if ((NULL == args[0]) ||
210 (0 == sscanf (args[0], "%lu", &num_peers)) )
211 {
212 num_peers = 1;
213 }
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
216 req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle,
217 NULL);
218 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
219 }
220 else if (view_update)
221 {
222 /* Get updates of view */
223 if ((NULL == args[0]) ||
224 (0 == sscanf (args[0], "%lu", &num_view_updates)) )
225 {
226 num_view_updates = 0;
227 }
228 GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle,
229 NULL);
230 if (0 != num_view_updates)
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Requesting %" PRIu64 " view updates\n", num_view_updates);
233 else
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Requesting continuous view updates\n");
236 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
237 }
238 else if (stream_input)
239 {
240 /* Get updates of view */
241 GNUNET_RPS_stream_request (rps_handle, stream_input_handle, NULL);
242 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
243 }
244 else
245 { /* Seed PeerID */
246 GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
247 fprintf (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
248 ret = 0;
249 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
250 }
251}
252
253
254/**
255 * The main function to rps.
256 *
257 * @param argc number of arguments from the command line
258 * @param argv command line arguments
259 * @return 0 ok, 1 on error
260 */
261int
262main (int argc, char *const *argv)
263{
264 const char helpstr[] =
265 "Get random GNUnet peers. If none is specified a single is requested.";
266 struct GNUNET_GETOPT_CommandLineOption options[] = {
267 GNUNET_GETOPT_option_base32_auto ('s',
268 "seed",
269 "PEER_ID",
270 gettext_noop ("Seed a PeerID"),
271 &peer_id),
272 GNUNET_GETOPT_option_flag ('V',
273 "view",
274 gettext_noop (
275 "Get updates of view (0 for infinite updates)"),
276 &view_update),
277 GNUNET_GETOPT_option_flag ('S',
278 "stream",
279 gettext_noop ("Get peers from biased stream"),
280 &stream_input),
281 GNUNET_GETOPT_OPTION_END
282 };
283
284 return (GNUNET_OK ==
285 GNUNET_PROGRAM_run (argc,
286 argv,
287 "gnunet-rps [NUMBER_OF_PEERS]",
288 gettext_noop
289 (helpstr),
290 options, &run, NULL)) ? ret : 1;
291}
292
293
294/* end of gnunet-rps.c */
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
deleted file mode 100644
index f6fcf3ae7..000000000
--- a/src/rps/gnunet-service-rps.c
+++ /dev/null
@@ -1,5007 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013-2015 GNUnet e.V.
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
25 */
26#include "platform.h"
27#include "gnunet_applications.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_cadet_service.h"
30#include "gnunet_core_service.h"
31#include "gnunet_peerinfo_service.h"
32#include "gnunet_nse_service.h"
33#include "gnunet_statistics_service.h"
34#include "rps.h"
35#include "rps-test_util.h"
36#include "gnunet-service-rps_sampler.h"
37#include "gnunet-service-rps_custommap.h"
38#include "gnunet-service-rps_view.h"
39
40#include <math.h>
41#include <inttypes.h>
42#include <string.h>
43
44#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
45
46// TODO check for overflows
47
48// TODO align message structs
49
50// TODO connect to friends
51
52// TODO blacklist? (-> mal peer detection on top of brahms)
53
54// hist_size_init, hist_size_max
55
56/***********************************************************************
57* Old gnunet-service-rps_peers.c
58***********************************************************************/
59
60/**
61 * Set a peer flag of given peer context.
62 */
63#define SET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask))
64
65/**
66 * Get peer flag of given peer context.
67 */
68#define check_peer_flag_set(peer_ctx, mask) \
69 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
70
71/**
72 * Unset flag of given peer context.
73 */
74#define UNSET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask))
75
76/**
77 * Get channel flag of given channel context.
78 */
79#define check_channel_flag_set(channel_flags, mask) \
80 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
81
82/**
83 * Unset flag of given channel context.
84 */
85#define unset_channel_flag(channel_flags, mask) ((*channel_flags) &= ~(mask))
86
87
88/**
89 * Pending operation on peer consisting of callback and closure
90 *
91 * When an operation cannot be executed right now this struct is used to store
92 * the callback and closure for later execution.
93 */
94struct PeerPendingOp
95{
96 /**
97 * Callback
98 */
99 PeerOp op;
100
101 /**
102 * Closure
103 */
104 void *op_cls;
105};
106
107/**
108 * List containing all messages that are yet to be send
109 *
110 * This is used to keep track of all messages that have not been sent yet. When
111 * a peer is to be removed the pending messages can be removed properly.
112 */
113struct PendingMessage
114{
115 /**
116 * DLL next, prev
117 */
118 struct PendingMessage *next;
119 struct PendingMessage *prev;
120
121 /**
122 * The envelope to the corresponding message
123 */
124 struct GNUNET_MQ_Envelope *ev;
125
126 /**
127 * The corresponding context
128 */
129 struct PeerContext *peer_ctx;
130
131 /**
132 * The message type
133 */
134 const char *type;
135};
136
137/**
138 * @brief Context for a channel
139 */
140struct ChannelCtx;
141
142/**
143 * Struct used to keep track of other peer's status
144 *
145 * This is stored in a multipeermap.
146 * It contains information such as cadet channels, a message queue for sending,
147 * status about the channels, the pending operations on this peer and some flags
148 * about the status of the peer itself. (online, valid, ...)
149 */
150struct PeerContext
151{
152 /**
153 * The Sub this context belongs to.
154 */
155 struct Sub *sub;
156
157 /**
158 * Message queue open to client
159 */
160 struct GNUNET_MQ_Handle *mq;
161
162 /**
163 * Channel open to client.
164 */
165 struct ChannelCtx *send_channel_ctx;
166
167 /**
168 * Channel open from client.
169 */
170 struct ChannelCtx *recv_channel_ctx;
171
172 /**
173 * Array of pending operations on this peer.
174 */
175 struct PeerPendingOp *pending_ops;
176
177 /**
178 * Handle to the callback given to cadet_ntfy_tmt_rdy()
179 *
180 * To be canceled on shutdown.
181 */
182 struct PendingMessage *online_check_pending;
183
184 /**
185 * Number of pending operations.
186 */
187 unsigned int num_pending_ops;
188
189 /**
190 * Identity of the peer
191 */
192 struct GNUNET_PeerIdentity peer_id;
193
194 /**
195 * Flags indicating status of peer
196 */
197 uint32_t peer_flags;
198
199 /**
200 * Last time we received something from that peer.
201 */
202 struct GNUNET_TIME_Absolute last_message_recv;
203
204 /**
205 * Last time we received a keepalive message.
206 */
207 struct GNUNET_TIME_Absolute last_keepalive;
208
209 /**
210 * DLL with all messages that are yet to be sent
211 */
212 struct PendingMessage *pending_messages_head;
213 struct PendingMessage *pending_messages_tail;
214
215 /**
216 * This is pobably followed by 'statistical' data (when we first saw
217 * it, how did we get its ID, how many pushes (in a timeinterval),
218 * ...)
219 */
220 uint32_t round_pull_req;
221};
222
223/**
224 * @brief Closure to #valid_peer_iterator
225 */
226struct PeersIteratorCls
227{
228 /**
229 * Iterator function
230 */
231 PeersIterator iterator;
232
233 /**
234 * Closure to iterator
235 */
236 void *cls;
237};
238
239/**
240 * @brief Context for a channel
241 */
242struct ChannelCtx
243{
244 /**
245 * @brief The channel itself
246 */
247 struct GNUNET_CADET_Channel *channel;
248
249 /**
250 * @brief The peer context associated with the channel
251 */
252 struct PeerContext *peer_ctx;
253
254 /**
255 * @brief When channel destruction needs to be delayed (because it is called
256 * from within the cadet routine of another channel destruction) this task
257 * refers to the respective _SCHEDULER_Task.
258 */
259 struct GNUNET_SCHEDULER_Task *destruction_task;
260};
261
262
263#if ENABLE_MALICIOUS
264
265/**
266 * If type is 2 This struct is used to store the attacked peers in a DLL
267 */
268struct AttackedPeer
269{
270 /**
271 * DLL
272 */
273 struct AttackedPeer *next;
274 struct AttackedPeer *prev;
275
276 /**
277 * PeerID
278 */
279 struct GNUNET_PeerIdentity peer_id;
280};
281
282#endif /* ENABLE_MALICIOUS */
283
284/**
285 * @brief This number determines the number of slots for files that represent
286 * histograms
287 */
288#define HISTOGRAM_FILE_SLOTS 32
289
290/**
291 * @brief The size (in bytes) a file needs to store the histogram
292 *
293 * Per slot: 1 newline, up to 4 chars,
294 * Additionally: 1 null termination
295 */
296#define SIZE_DUMP_FILE (HISTOGRAM_FILE_SLOTS * 5) + 1
297
298/**
299 * @brief One Sub.
300 *
301 * Essentially one instance of brahms that only connects to other instances
302 * with the same (secret) value.
303 */
304struct Sub
305{
306 /**
307 * @brief Hash of the shared value that defines Subs.
308 */
309 struct GNUNET_HashCode hash;
310
311 /**
312 * @brief Port to communicate to other peers.
313 */
314 struct GNUNET_CADET_Port *cadet_port;
315
316 /**
317 * @brief Hashmap of valid peers.
318 */
319 struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
320
321 /**
322 * @brief Filename of the file that stores the valid peers persistently.
323 */
324 char *filename_valid_peers;
325
326 /**
327 * Set of all peers to keep track of them.
328 */
329 struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
330
331 /**
332 * @brief This is the minimum estimate used as sampler size.
333 *
334 * It is configured by the user.
335 */
336 unsigned int sampler_size_est_min;
337
338 /**
339 * The size of sampler we need to be able to satisfy the Brahms protocol's
340 * need of random peers.
341 *
342 * This is one minimum size the sampler grows to.
343 */
344 unsigned int sampler_size_est_need;
345
346 /**
347 * Time interval the do_round task runs in.
348 */
349 struct GNUNET_TIME_Relative round_interval;
350
351 /**
352 * Sampler used for the Brahms protocol itself.
353 */
354 struct RPS_Sampler *sampler;
355
356#ifdef TO_FILE_FULL
357 /**
358 * Name to log view to
359 */
360 char *file_name_view_log;
361#endif /* TO_FILE_FULL */
362
363#ifdef TO_FILE
364#ifdef TO_FILE_FULL
365 /**
366 * Name to log number of observed peers to
367 */
368 char *file_name_observed_log;
369#endif /* TO_FILE_FULL */
370
371 /**
372 * @brief Count the observed peers
373 */
374 uint32_t num_observed_peers;
375
376 /**
377 * @brief Multipeermap (ab-) used to count unique peer_ids
378 */
379 struct GNUNET_CONTAINER_MultiPeerMap *observed_unique_peers;
380#endif /* TO_FILE */
381
382 /**
383 * List to store peers received through pushes temporary.
384 */
385 struct CustomPeerMap *push_map;
386
387 /**
388 * List to store peers received through pulls temporary.
389 */
390 struct CustomPeerMap *pull_map;
391
392 /**
393 * @brief This is the estimate used as view size.
394 *
395 * It is initialised with the minimum
396 */
397 unsigned int view_size_est_need;
398
399 /**
400 * @brief This is the minimum estimate used as view size.
401 *
402 * It is configured by the user.
403 */
404 unsigned int view_size_est_min;
405
406 /**
407 * @brief The view.
408 */
409 struct View *view;
410
411 /**
412 * Identifier for the main task that runs periodically.
413 */
414 struct GNUNET_SCHEDULER_Task *do_round_task;
415
416 /* === stats === */
417
418 /**
419 * @brief Counts the executed rounds.
420 */
421 uint32_t num_rounds;
422
423 /**
424 * @brief This array accumulates the number of received pushes per round.
425 *
426 * Number at index i represents the number of rounds with i observed pushes.
427 */
428 uint32_t push_recv[HISTOGRAM_FILE_SLOTS];
429
430 /**
431 * @brief Histogram of deltas between the expected and actual number of
432 * received pushes.
433 *
434 * As half of the entries are expected to be negative, this is shifted by
435 * #HISTOGRAM_FILE_SLOTS/2.
436 */
437 uint32_t push_delta[HISTOGRAM_FILE_SLOTS];
438
439 /**
440 * @brief Number of pull replies with this delay measured in rounds.
441 *
442 * Number at index i represents the number of pull replies with a delay of i
443 * rounds.
444 */
445 uint32_t pull_delays[HISTOGRAM_FILE_SLOTS];
446};
447
448
449/***********************************************************************
450* Globals
451***********************************************************************/
452
453/**
454 * Our configuration.
455 */
456static const struct GNUNET_CONFIGURATION_Handle *cfg;
457
458/**
459 * Handle to the statistics service.
460 */
461struct GNUNET_STATISTICS_Handle *stats;
462
463/**
464 * Handler to CADET.
465 */
466struct GNUNET_CADET_Handle *cadet_handle;
467
468/**
469 * Handle to CORE
470 */
471struct GNUNET_CORE_Handle *core_handle;
472
473/**
474 * @brief PeerMap to keep track of connected peers.
475 */
476struct GNUNET_CONTAINER_MultiPeerMap *map_single_hop;
477
478/**
479 * Our own identity.
480 */
481static struct GNUNET_PeerIdentity own_identity;
482
483/**
484 * Percentage of total peer number in the view
485 * to send random PUSHes to
486 */
487static float alpha;
488
489/**
490 * Percentage of total peer number in the view
491 * to send random PULLs to
492 */
493static float beta;
494
495/**
496 * Handler to NSE.
497 */
498static struct GNUNET_NSE_Handle *nse;
499
500/**
501 * Handler to PEERINFO.
502 */
503static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
504
505/**
506 * Handle for cancellation of iteration over peers.
507 */
508static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
509
510
511#if ENABLE_MALICIOUS
512/**
513 * Type of malicious peer
514 *
515 * 0 Don't act malicious at all - Default
516 * 1 Try to maximise representation
517 * 2 Try to partition the network
518 * 3 Combined attack
519 */
520static uint32_t mal_type;
521
522/**
523 * Other malicious peers
524 */
525static struct GNUNET_PeerIdentity *mal_peers;
526
527/**
528 * Hashmap of malicious peers used as set.
529 * Used to more efficiently check whether we know that peer.
530 */
531static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
532
533/**
534 * Number of other malicious peers
535 */
536static uint32_t num_mal_peers;
537
538
539/**
540 * If type is 2 this is the DLL of attacked peers
541 */
542static struct AttackedPeer *att_peers_head;
543static struct AttackedPeer *att_peers_tail;
544
545/**
546 * This index is used to point to an attacked peer to
547 * implement the round-robin-ish way to select attacked peers.
548 */
549static struct AttackedPeer *att_peer_index;
550
551/**
552 * Hashmap of attacked peers used as set.
553 * Used to more efficiently check whether we know that peer.
554 */
555static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
556
557/**
558 * Number of attacked peers
559 */
560static uint32_t num_attacked_peers;
561
562/**
563 * If type is 1 this is the attacked peer
564 */
565static struct GNUNET_PeerIdentity attacked_peer;
566
567/**
568 * The limit of PUSHes we can send in one round.
569 * This is an assumption of the Brahms protocol and either implemented
570 * via proof of work
571 * or
572 * assumed to be the bandwidth limitation.
573 */
574static uint32_t push_limit = 10000;
575#endif /* ENABLE_MALICIOUS */
576
577/**
578 * @brief Main Sub.
579 *
580 * This is run in any case by all peers and connects to all peers without
581 * specifying a shared value.
582 */
583static struct Sub *msub;
584
585/**
586 * @brief Maximum number of valid peers to keep.
587 * TODO read from config
588 */
589static const uint32_t num_valid_peers_max = UINT32_MAX;
590
591/***********************************************************************
592* /Globals
593***********************************************************************/
594
595
596static void
597do_round (void *cls);
598
599#if ENABLE_MALICIOUS
600static void
601do_mal_round (void *cls);
602
603#endif /* ENABLE_MALICIOUS */
604
605
606/**
607 * @brief Get the #PeerContext associated with a peer
608 *
609 * @param peer_map The peer map containing the context
610 * @param peer the peer id
611 *
612 * @return the #PeerContext
613 */
614static struct PeerContext *
615get_peer_ctx (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
616 const struct GNUNET_PeerIdentity *peer)
617{
618 struct PeerContext *ctx;
619 int ret;
620
621 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
622 GNUNET_assert (GNUNET_YES == ret);
623 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
624 GNUNET_assert (NULL != ctx);
625 return ctx;
626}
627
628
629/**
630 * @brief Check whether we have information about the given peer.
631 *
632 * FIXME probably deprecated. Make this the new _online.
633 *
634 * @param peer_map The peer map to check for the existence of @a peer
635 * @param peer peer in question
636 *
637 * @return #GNUNET_YES if peer is known
638 * #GNUNET_NO if peer is not known
639 */
640static int
641check_peer_known (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
642 const struct GNUNET_PeerIdentity *peer)
643{
644 if (NULL != peer_map)
645 {
646 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
647 }
648 else
649 {
650 return GNUNET_NO;
651 }
652}
653
654
655/**
656 * @brief Create a new #PeerContext and insert it into the peer map
657 *
658 * @param sub The Sub this context belongs to.
659 * @param peer the peer to create the #PeerContext for
660 *
661 * @return the #PeerContext
662 */
663static struct PeerContext *
664create_peer_ctx (struct Sub *sub,
665 const struct GNUNET_PeerIdentity *peer)
666{
667 struct PeerContext *ctx;
668 int ret;
669
670 GNUNET_assert (GNUNET_NO == check_peer_known (sub->peer_map, peer));
671
672 ctx = GNUNET_new (struct PeerContext);
673 ctx->peer_id = *peer;
674 ctx->sub = sub;
675 ret = GNUNET_CONTAINER_multipeermap_put (sub->peer_map, peer, ctx,
676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
677 GNUNET_assert (GNUNET_OK == ret);
678 if (sub == msub)
679 {
680 GNUNET_STATISTICS_set (stats,
681 "# known peers",
682 GNUNET_CONTAINER_multipeermap_size (sub->peer_map),
683 GNUNET_NO);
684 }
685 return ctx;
686}
687
688
689/**
690 * @brief Create or get a #PeerContext
691 *
692 * @param sub The Sub to which the created context belongs to
693 * @param peer the peer to get the associated context to
694 *
695 * @return the context
696 */
697static struct PeerContext *
698create_or_get_peer_ctx (struct Sub *sub,
699 const struct GNUNET_PeerIdentity *peer)
700{
701 if (GNUNET_NO == check_peer_known (sub->peer_map, peer))
702 {
703 return create_peer_ctx (sub, peer);
704 }
705 return get_peer_ctx (sub->peer_map, peer);
706}
707
708
709/**
710 * @brief Check whether we have a connection to this @a peer
711 *
712 * Also sets the #Peers_ONLINE flag accordingly
713 *
714 * @param peer_ctx Context of the peer of which connectivity is to be checked
715 *
716 * @return #GNUNET_YES if we are connected
717 * #GNUNET_NO otherwise
718 */
719static int
720check_connected (struct PeerContext *peer_ctx)
721{
722 /* If we don't know about this peer we don't know whether it's online */
723 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
724 &peer_ctx->peer_id))
725 {
726 return GNUNET_NO;
727 }
728 /* Get the context */
729 peer_ctx = get_peer_ctx (peer_ctx->sub->peer_map, &peer_ctx->peer_id);
730 /* If we have no channel to this peer we don't know whether it's online */
731 if ((NULL == peer_ctx->send_channel_ctx) &&
732 (NULL == peer_ctx->recv_channel_ctx))
733 {
734 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE);
735 return GNUNET_NO;
736 }
737 /* Otherwise (if we have a channel, we know that it's online */
738 SET_PEER_FLAG (peer_ctx, Peers_ONLINE);
739 return GNUNET_YES;
740}
741
742
743/**
744 * @brief The closure to #get_rand_peer_iterator.
745 */
746struct GetRandPeerIteratorCls
747{
748 /**
749 * @brief The index of the peer to return.
750 * Will be decreased until 0.
751 * Then current peer is returned.
752 */
753 uint32_t index;
754
755 /**
756 * @brief Pointer to peer to return.
757 */
758 const struct GNUNET_PeerIdentity *peer;
759};
760
761
762/**
763 * @brief Iterator function for #get_random_peer_from_peermap.
764 *
765 * Implements #GNUNET_CONTAINER_PeerMapIterator.
766 * Decreases the index until the index is null.
767 * Then returns the current peer.
768 *
769 * @param cls the #GetRandPeerIteratorCls containing index and peer
770 * @param peer current peer
771 * @param value unused
772 *
773 * @return #GNUNET_YES if we should continue to
774 * iterate,
775 * #GNUNET_NO if not.
776 */
777static int
778get_rand_peer_iterator (void *cls,
779 const struct GNUNET_PeerIdentity *peer,
780 void *value)
781{
782 struct GetRandPeerIteratorCls *iterator_cls = cls;
783
784 (void) value;
785
786 if (0 >= iterator_cls->index)
787 {
788 iterator_cls->peer = peer;
789 return GNUNET_NO;
790 }
791 iterator_cls->index--;
792 return GNUNET_YES;
793}
794
795
796/**
797 * @brief Get a random peer from @a peer_map
798 *
799 * @param valid_peers Peer map containing valid peers from which to select a
800 * random one
801 *
802 * @return a random peer
803 */
804static const struct GNUNET_PeerIdentity *
805get_random_peer_from_peermap (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers)
806{
807 struct GetRandPeerIteratorCls *iterator_cls;
808 const struct GNUNET_PeerIdentity *ret;
809
810 iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls);
811 iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
812 GNUNET_CONTAINER_multipeermap_size (
813 valid_peers));
814 (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
815 get_rand_peer_iterator,
816 iterator_cls);
817 ret = iterator_cls->peer;
818 GNUNET_free (iterator_cls);
819 return ret;
820}
821
822
823/**
824 * @brief Add a given @a peer to valid peers.
825 *
826 * If valid peers are already #num_valid_peers_max, delete a peer previously.
827 *
828 * @param peer The peer that is added to the valid peers.
829 * @param valid_peers Peer map of valid peers to which to add the @a peer
830 *
831 * @return #GNUNET_YES if no other peer had to be removed
832 * #GNUNET_NO otherwise
833 */
834static int
835add_valid_peer (const struct GNUNET_PeerIdentity *peer,
836 struct GNUNET_CONTAINER_MultiPeerMap *valid_peers)
837{
838 const struct GNUNET_PeerIdentity *rand_peer;
839 int ret;
840
841 ret = GNUNET_YES;
842 /* Remove random peers until there is space for a new one */
843 while (num_valid_peers_max <=
844 GNUNET_CONTAINER_multipeermap_size (valid_peers))
845 {
846 rand_peer = get_random_peer_from_peermap (valid_peers);
847 GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer);
848 ret = GNUNET_NO;
849 }
850 (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL,
851 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
852 if (valid_peers == msub->valid_peers)
853 {
854 GNUNET_STATISTICS_set (stats,
855 "# valid peers",
856 GNUNET_CONTAINER_multipeermap_size (valid_peers),
857 GNUNET_NO);
858 }
859 return ret;
860}
861
862
863static void
864remove_pending_message (struct PendingMessage *pending_msg, int cancel);
865
866/**
867 * @brief Set the peer flag to living and
868 * call the pending operations on this peer.
869 *
870 * Also adds peer to #valid_peers.
871 *
872 * @param peer_ctx the #PeerContext of the peer to set online
873 */
874static void
875set_peer_online (struct PeerContext *peer_ctx)
876{
877 struct GNUNET_PeerIdentity *peer;
878 unsigned int i;
879
880 peer = &peer_ctx->peer_id;
881 LOG (GNUNET_ERROR_TYPE_DEBUG,
882 "Peer %s is online and valid, calling %i pending operations on it\n",
883 GNUNET_i2s (peer),
884 peer_ctx->num_pending_ops);
885
886 if (NULL != peer_ctx->online_check_pending)
887 {
888 LOG (GNUNET_ERROR_TYPE_DEBUG,
889 "Removing pending online check for peer %s\n",
890 GNUNET_i2s (&peer_ctx->peer_id));
891 // TODO wait until cadet sets mq->cancel_impl
892 // GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev);
893 remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES);
894 peer_ctx->online_check_pending = NULL;
895 }
896
897 SET_PEER_FLAG (peer_ctx, Peers_ONLINE);
898
899 /* Call pending operations */
900 for (i = 0; i < peer_ctx->num_pending_ops; i++)
901 {
902 peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer);
903 }
904 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
905}
906
907
908static void
909cleanup_destroyed_channel (void *cls,
910 const struct GNUNET_CADET_Channel *channel);
911
912/* Declaration of handlers */
913static void
914handle_peer_check (void *cls,
915 const struct GNUNET_MessageHeader *msg);
916
917static void
918handle_peer_push (void *cls,
919 const struct GNUNET_MessageHeader *msg);
920
921static void
922handle_peer_pull_request (void *cls,
923 const struct GNUNET_MessageHeader *msg);
924
925static int
926check_peer_pull_reply (void *cls,
927 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
928
929static void
930handle_peer_pull_reply (void *cls,
931 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
932
933/* End declaration of handlers */
934
935/**
936 * @brief Allocate memory for a new channel context and insert it into DLL
937 *
938 * @param peer_ctx context of the according peer
939 *
940 * @return The channel context
941 */
942static struct ChannelCtx *
943add_channel_ctx (struct PeerContext *peer_ctx)
944{
945 struct ChannelCtx *channel_ctx;
946
947 channel_ctx = GNUNET_new (struct ChannelCtx);
948 channel_ctx->peer_ctx = peer_ctx;
949 return channel_ctx;
950}
951
952
953/**
954 * @brief Free memory and NULL pointers.
955 *
956 * @param channel_ctx The channel context.
957 */
958static void
959remove_channel_ctx (struct ChannelCtx *channel_ctx)
960{
961 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
962
963 if (NULL != channel_ctx->destruction_task)
964 {
965 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task);
966 channel_ctx->destruction_task = NULL;
967 }
968
969 if (NULL == peer_ctx)
970 return;
971 if (channel_ctx == peer_ctx->send_channel_ctx)
972 {
973 peer_ctx->send_channel_ctx = NULL;
974 peer_ctx->mq = NULL;
975 }
976 else if (channel_ctx == peer_ctx->recv_channel_ctx)
977 {
978 peer_ctx->recv_channel_ctx = NULL;
979 }
980 GNUNET_free (channel_ctx);
981}
982
983
984/**
985 * @brief Get the channel of a peer. If not existing, create.
986 *
987 * @param peer_ctx Context of the peer of which to get the channel
988 * @return the #GNUNET_CADET_Channel used to send data to @a peer_ctx
989 */
990struct GNUNET_CADET_Channel *
991get_channel (struct PeerContext *peer_ctx)
992{
993 /* There exists a copy-paste-clone in run() */
994 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
995 GNUNET_MQ_hd_fixed_size (peer_check,
996 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
997 struct GNUNET_MessageHeader,
998 NULL),
999 GNUNET_MQ_hd_fixed_size (peer_push,
1000 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
1001 struct GNUNET_MessageHeader,
1002 NULL),
1003 GNUNET_MQ_hd_fixed_size (peer_pull_request,
1004 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1005 struct GNUNET_MessageHeader,
1006 NULL),
1007 GNUNET_MQ_hd_var_size (peer_pull_reply,
1008 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
1009 struct GNUNET_RPS_P2P_PullReplyMessage,
1010 NULL),
1011 GNUNET_MQ_handler_end ()
1012 };
1013
1014
1015 if (NULL == peer_ctx->send_channel_ctx)
1016 {
1017 LOG (GNUNET_ERROR_TYPE_DEBUG,
1018 "Trying to establish channel to peer %s\n",
1019 GNUNET_i2s (&peer_ctx->peer_id));
1020 peer_ctx->send_channel_ctx = add_channel_ctx (peer_ctx);
1021 peer_ctx->send_channel_ctx->channel =
1022 GNUNET_CADET_channel_create (cadet_handle,
1023 peer_ctx->send_channel_ctx, /* context */
1024 &peer_ctx->peer_id,
1025 &peer_ctx->sub->hash,
1026 NULL, /* WindowSize handler */
1027 &cleanup_destroyed_channel, /* Disconnect handler */
1028 cadet_handlers);
1029 }
1030 GNUNET_assert (NULL != peer_ctx->send_channel_ctx);
1031 GNUNET_assert (NULL != peer_ctx->send_channel_ctx->channel);
1032 return peer_ctx->send_channel_ctx->channel;
1033}
1034
1035
1036/**
1037 * Get the message queue (#GNUNET_MQ_Handle) of a specific peer.
1038 *
1039 * If we already have a message queue open to this client,
1040 * simply return it, otherwise create one.
1041 *
1042 * @param peer_ctx Context of the peer of which to get the mq
1043 * @return the #GNUNET_MQ_Handle
1044 */
1045static struct GNUNET_MQ_Handle *
1046get_mq (struct PeerContext *peer_ctx)
1047{
1048 if (NULL == peer_ctx->mq)
1049 {
1050 peer_ctx->mq = GNUNET_CADET_get_mq (get_channel (peer_ctx));
1051 }
1052 return peer_ctx->mq;
1053}
1054
1055
1056/**
1057 * @brief Add an envelope to a message passed to mq to list of pending messages
1058 *
1059 * @param peer_ctx Context of the peer for which to insert the envelope
1060 * @param ev envelope to the message
1061 * @param type type of the message to be sent
1062 * @return pointer to pending message
1063 */
1064static struct PendingMessage *
1065insert_pending_message (struct PeerContext *peer_ctx,
1066 struct GNUNET_MQ_Envelope *ev,
1067 const char *type)
1068{
1069 struct PendingMessage *pending_msg;
1070
1071 pending_msg = GNUNET_new (struct PendingMessage);
1072 pending_msg->ev = ev;
1073 pending_msg->peer_ctx = peer_ctx;
1074 pending_msg->type = type;
1075 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
1076 peer_ctx->pending_messages_tail,
1077 pending_msg);
1078 return pending_msg;
1079}
1080
1081
1082/**
1083 * @brief Remove a pending message from the respective DLL
1084 *
1085 * @param pending_msg the pending message to remove
1086 * @param cancel whether to cancel the pending message, too
1087 */
1088static void
1089remove_pending_message (struct PendingMessage *pending_msg, int cancel)
1090{
1091 struct PeerContext *peer_ctx;
1092
1093 (void) cancel;
1094
1095 peer_ctx = pending_msg->peer_ctx;
1096 GNUNET_assert (NULL != peer_ctx);
1097 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
1098 peer_ctx->pending_messages_tail,
1099 pending_msg);
1100 // TODO wait for the cadet implementation of message cancellation
1101 // if (GNUNET_YES == cancel)
1102 // {
1103 // GNUNET_MQ_send_cancel (pending_msg->ev);
1104 // }
1105 GNUNET_free (pending_msg);
1106}
1107
1108
1109/**
1110 * @brief This is called in response to the first message we sent as a
1111 * online check.
1112 *
1113 * @param cls #PeerContext of peer with pending online check
1114 */
1115static void
1116mq_online_check_successful (void *cls)
1117{
1118 struct PeerContext *peer_ctx = cls;
1119
1120 if (NULL != peer_ctx->online_check_pending)
1121 {
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "Online check for peer %s was successful\n",
1124 GNUNET_i2s (&peer_ctx->peer_id));
1125 remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES);
1126 peer_ctx->online_check_pending = NULL;
1127 set_peer_online (peer_ctx);
1128 (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers);
1129 }
1130}
1131
1132
1133/**
1134 * Issue a check whether peer is online
1135 *
1136 * @param peer_ctx the context of the peer
1137 */
1138static void
1139check_peer_online (struct PeerContext *peer_ctx)
1140{
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "Get informed about peer %s getting online\n",
1143 GNUNET_i2s (&peer_ctx->peer_id));
1144
1145 struct GNUNET_MQ_Handle *mq;
1146 struct GNUNET_MQ_Envelope *ev;
1147
1148 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE);
1149 peer_ctx->online_check_pending =
1150 insert_pending_message (peer_ctx, ev, "Check online");
1151 mq = get_mq (peer_ctx);
1152 GNUNET_MQ_notify_sent (ev,
1153 mq_online_check_successful,
1154 peer_ctx);
1155 GNUNET_MQ_send (mq, ev);
1156 if (peer_ctx->sub == msub)
1157 {
1158 GNUNET_STATISTICS_update (stats,
1159 "# pending online checks",
1160 1,
1161 GNUNET_NO);
1162 }
1163}
1164
1165
1166/**
1167 * @brief Check whether function of type #PeerOp was already scheduled
1168 *
1169 * The array with pending operations will probably never grow really big, so
1170 * iterating over it should be ok.
1171 *
1172 * @param peer_ctx Context of the peer to check for the operation
1173 * @param peer_op the operation (#PeerOp) on the peer
1174 *
1175 * @return #GNUNET_YES if this operation is scheduled on that peer
1176 * #GNUNET_NO otherwise
1177 */
1178static int
1179check_operation_scheduled (const struct PeerContext *peer_ctx,
1180 const PeerOp peer_op)
1181{
1182 unsigned int i;
1183
1184 for (i = 0; i < peer_ctx->num_pending_ops; i++)
1185 if (peer_op == peer_ctx->pending_ops[i].op)
1186 return GNUNET_YES;
1187 return GNUNET_NO;
1188}
1189
1190
1191/**
1192 * @brief Callback for scheduler to destroy a channel
1193 *
1194 * @param cls Context of the channel
1195 */
1196static void
1197destroy_channel (struct ChannelCtx *channel_ctx)
1198{
1199 struct GNUNET_CADET_Channel *channel;
1200
1201 if (NULL != channel_ctx->destruction_task)
1202 {
1203 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task);
1204 channel_ctx->destruction_task = NULL;
1205 }
1206 GNUNET_assert (channel_ctx->channel != NULL);
1207 channel = channel_ctx->channel;
1208 channel_ctx->channel = NULL;
1209 GNUNET_CADET_channel_destroy (channel);
1210 remove_channel_ctx (channel_ctx);
1211}
1212
1213
1214/**
1215 * @brief Destroy a cadet channel.
1216 *
1217 * This satisfies the function signature of #GNUNET_SCHEDULER_TaskCallback.
1218 *
1219 * @param cls
1220 */
1221static void
1222destroy_channel_cb (void *cls)
1223{
1224 struct ChannelCtx *channel_ctx = cls;
1225
1226 channel_ctx->destruction_task = NULL;
1227 destroy_channel (channel_ctx);
1228}
1229
1230
1231/**
1232 * @brief Schedule the destruction of a channel for immediately afterwards.
1233 *
1234 * In case a channel is to be destroyed from within the callback to the
1235 * destruction of another channel (send channel), we cannot call
1236 * GNUNET_CADET_channel_destroy directly, but need to use this scheduling
1237 * construction.
1238 *
1239 * @param channel_ctx channel to be destroyed.
1240 */
1241static void
1242schedule_channel_destruction (struct ChannelCtx *channel_ctx)
1243{
1244 GNUNET_assert (NULL ==
1245 channel_ctx->destruction_task);
1246 GNUNET_assert (NULL !=
1247 channel_ctx->channel);
1248 channel_ctx->destruction_task =
1249 GNUNET_SCHEDULER_add_now (&destroy_channel_cb,
1250 channel_ctx);
1251}
1252
1253
1254/**
1255 * @brief Remove peer
1256 *
1257 * - Empties the list with pending operations
1258 * - Empties the list with pending messages
1259 * - Cancels potentially existing online check
1260 * - Schedules closing of send and recv channels
1261 * - Removes peer from peer map
1262 *
1263 * @param peer_ctx Context of the peer to be destroyed
1264 * @return #GNUNET_YES if peer was removed
1265 * #GNUNET_NO otherwise
1266 */
1267static int
1268destroy_peer (struct PeerContext *peer_ctx)
1269{
1270 GNUNET_assert (NULL != peer_ctx);
1271 GNUNET_assert (NULL != peer_ctx->sub->peer_map);
1272 if (GNUNET_NO ==
1273 GNUNET_CONTAINER_multipeermap_contains (peer_ctx->sub->peer_map,
1274 &peer_ctx->peer_id))
1275 {
1276 return GNUNET_NO;
1277 }
1278 SET_PEER_FLAG (peer_ctx, Peers_TO_DESTROY);
1279 LOG (GNUNET_ERROR_TYPE_DEBUG,
1280 "Going to remove peer %s\n",
1281 GNUNET_i2s (&peer_ctx->peer_id));
1282 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE);
1283
1284 /* Clear list of pending operations */
1285 // TODO this probably leaks memory
1286 // ('only' the cls to the function. Not sure what to do with it)
1287 GNUNET_array_grow (peer_ctx->pending_ops,
1288 peer_ctx->num_pending_ops,
1289 0);
1290 /* Remove all pending messages */
1291 while (NULL != peer_ctx->pending_messages_head)
1292 {
1293 LOG (GNUNET_ERROR_TYPE_DEBUG,
1294 "Removing unsent %s\n",
1295 peer_ctx->pending_messages_head->type);
1296 /* Cancel pending message, too */
1297 if ((NULL != peer_ctx->online_check_pending) &&
1298 (0 == memcmp (peer_ctx->pending_messages_head,
1299 peer_ctx->online_check_pending,
1300 sizeof(struct PendingMessage))))
1301 {
1302 peer_ctx->online_check_pending = NULL;
1303 if (peer_ctx->sub == msub)
1304 {
1305 GNUNET_STATISTICS_update (stats,
1306 "# pending online checks",
1307 -1,
1308 GNUNET_NO);
1309 }
1310 }
1311 remove_pending_message (peer_ctx->pending_messages_head,
1312 GNUNET_YES);
1313 }
1314
1315 /* If we are still waiting for notification whether this peer is online
1316 * cancel the according task */
1317 if (NULL != peer_ctx->online_check_pending)
1318 {
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Removing pending online check for peer %s\n",
1321 GNUNET_i2s (&peer_ctx->peer_id));
1322 // TODO wait until cadet sets mq->cancel_impl
1323 // GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev);
1324 remove_pending_message (peer_ctx->online_check_pending,
1325 GNUNET_YES);
1326 peer_ctx->online_check_pending = NULL;
1327 }
1328
1329 if (NULL != peer_ctx->send_channel_ctx)
1330 {
1331 /* This is possibly called from within channel destruction */
1332 peer_ctx->send_channel_ctx->peer_ctx = NULL;
1333 schedule_channel_destruction (peer_ctx->send_channel_ctx);
1334 peer_ctx->send_channel_ctx = NULL;
1335 peer_ctx->mq = NULL;
1336 }
1337 if (NULL != peer_ctx->recv_channel_ctx)
1338 {
1339 /* This is possibly called from within channel destruction */
1340 peer_ctx->recv_channel_ctx->peer_ctx = NULL;
1341 schedule_channel_destruction (peer_ctx->recv_channel_ctx);
1342 peer_ctx->recv_channel_ctx = NULL;
1343 }
1344
1345 if (GNUNET_YES !=
1346 GNUNET_CONTAINER_multipeermap_remove_all (peer_ctx->sub->peer_map,
1347 &peer_ctx->peer_id))
1348 {
1349 LOG (GNUNET_ERROR_TYPE_WARNING,
1350 "removing peer from peer_ctx->sub->peer_map failed\n");
1351 }
1352 if (peer_ctx->sub == msub)
1353 {
1354 GNUNET_STATISTICS_set (stats,
1355 "# known peers",
1356 GNUNET_CONTAINER_multipeermap_size (
1357 peer_ctx->sub->peer_map),
1358 GNUNET_NO);
1359 }
1360 GNUNET_free (peer_ctx);
1361 return GNUNET_YES;
1362}
1363
1364
1365/**
1366 * Iterator over hash map entries. Deletes all contexts of peers.
1367 *
1368 * @param cls closure
1369 * @param key current public key
1370 * @param value value in the hash map
1371 * @return #GNUNET_YES if we should continue to iterate,
1372 * #GNUNET_NO if not.
1373 */
1374static int
1375peermap_clear_iterator (void *cls,
1376 const struct GNUNET_PeerIdentity *key,
1377 void *value)
1378{
1379 struct Sub *sub = cls;
1380
1381 (void) value;
1382
1383 destroy_peer (get_peer_ctx (sub->peer_map, key));
1384 return GNUNET_YES;
1385}
1386
1387
1388/**
1389 * @brief This is called once a message is sent.
1390 *
1391 * Removes the pending message
1392 *
1393 * @param cls type of the message that was sent
1394 */
1395static void
1396mq_notify_sent_cb (void *cls)
1397{
1398 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
1399
1400 LOG (GNUNET_ERROR_TYPE_DEBUG,
1401 "%s was sent.\n",
1402 pending_msg->type);
1403 if (pending_msg->peer_ctx->sub == msub)
1404 {
1405 if (0 == strncmp ("PULL REPLY", pending_msg->type, 10))
1406 GNUNET_STATISTICS_update (stats, "# pull replies sent", 1, GNUNET_NO);
1407 if (0 == strncmp ("PULL REQUEST", pending_msg->type, 12))
1408 GNUNET_STATISTICS_update (stats, "# pull requests sent", 1, GNUNET_NO);
1409 if (0 == strncmp ("PUSH", pending_msg->type, 4))
1410 GNUNET_STATISTICS_update (stats, "# pushes sent", 1, GNUNET_NO);
1411 if ((0 == strncmp ("PULL REQUEST", pending_msg->type, 12)) &&
1412 (NULL != map_single_hop) &&
1413 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
1414 &pending_msg->
1415 peer_ctx->peer_id)) )
1416 GNUNET_STATISTICS_update (stats,
1417 "# pull requests sent (multi-hop peer)",
1418 1,
1419 GNUNET_NO);
1420 }
1421 /* Do not cancel message */
1422 remove_pending_message (pending_msg, GNUNET_NO);
1423}
1424
1425
1426/**
1427 * @brief Iterator function for #store_valid_peers.
1428 *
1429 * Implements #GNUNET_CONTAINER_PeerMapIterator.
1430 * Writes single peer to disk.
1431 *
1432 * @param cls the file handle to write to.
1433 * @param peer current peer
1434 * @param value unused
1435 *
1436 * @return #GNUNET_YES if we should continue to
1437 * iterate,
1438 * #GNUNET_NO if not.
1439 */
1440static int
1441store_peer_presistently_iterator (void *cls,
1442 const struct GNUNET_PeerIdentity *peer,
1443 void *value)
1444{
1445 const struct GNUNET_DISK_FileHandle *fh = cls;
1446 char peer_string[128];
1447 int size;
1448 ssize_t ret;
1449
1450 (void) value;
1451
1452 if (NULL == peer)
1453 {
1454 return GNUNET_YES;
1455 }
1456 size = GNUNET_snprintf (peer_string,
1457 sizeof(peer_string),
1458 "%s\n",
1459 GNUNET_i2s_full (peer));
1460 GNUNET_assert (53 == size);
1461 ret = GNUNET_DISK_file_write (fh,
1462 peer_string,
1463 size);
1464 GNUNET_assert (size == ret);
1465 return GNUNET_YES;
1466}
1467
1468
1469/**
1470 * @brief Store the peers currently in #valid_peers to disk.
1471 *
1472 * @param sub Sub for which to store the valid peers
1473 */
1474static void
1475store_valid_peers (const struct Sub *sub)
1476{
1477 struct GNUNET_DISK_FileHandle *fh;
1478 uint32_t number_written_peers;
1479 int ret;
1480
1481 if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7))
1482 {
1483 return;
1484 }
1485
1486 ret = GNUNET_DISK_directory_create_for_file (sub->filename_valid_peers);
1487 if (GNUNET_SYSERR == ret)
1488 {
1489 LOG (GNUNET_ERROR_TYPE_WARNING,
1490 "Not able to create directory for file `%s'\n",
1491 sub->filename_valid_peers);
1492 GNUNET_break (0);
1493 }
1494 else if (GNUNET_NO == ret)
1495 {
1496 LOG (GNUNET_ERROR_TYPE_WARNING,
1497 "Directory for file `%s' exists but is not writable for us\n",
1498 sub->filename_valid_peers);
1499 GNUNET_break (0);
1500 }
1501 fh = GNUNET_DISK_file_open (sub->filename_valid_peers,
1502 GNUNET_DISK_OPEN_WRITE
1503 | GNUNET_DISK_OPEN_CREATE,
1504 GNUNET_DISK_PERM_USER_READ
1505 | GNUNET_DISK_PERM_USER_WRITE);
1506 if (NULL == fh)
1507 {
1508 LOG (GNUNET_ERROR_TYPE_WARNING,
1509 "Not able to write valid peers to file `%s'\n",
1510 sub->filename_valid_peers);
1511 return;
1512 }
1513 LOG (GNUNET_ERROR_TYPE_DEBUG,
1514 "Writing %u valid peers to disk\n",
1515 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1516 number_written_peers =
1517 GNUNET_CONTAINER_multipeermap_iterate (sub->valid_peers,
1518 store_peer_presistently_iterator,
1519 fh);
1520 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1521 GNUNET_assert (number_written_peers ==
1522 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1523}
1524
1525
1526/**
1527 * @brief Convert string representation of peer id to peer id.
1528 *
1529 * Counterpart to #GNUNET_i2s_full.
1530 *
1531 * @param string_repr The string representation of the peer id
1532 *
1533 * @return The peer id
1534 */
1535static const struct GNUNET_PeerIdentity *
1536s2i_full (const char *string_repr)
1537{
1538 struct GNUNET_PeerIdentity *peer;
1539 size_t len;
1540 int ret;
1541
1542 peer = GNUNET_new (struct GNUNET_PeerIdentity);
1543 len = strlen (string_repr);
1544 if (52 > len)
1545 {
1546 LOG (GNUNET_ERROR_TYPE_WARNING,
1547 "Not able to convert string representation of PeerID to PeerID\n"
1548 "String representation: %s (len %lu) - too short\n",
1549 string_repr,
1550 len);
1551 GNUNET_break (0);
1552 }
1553 else if (52 < len)
1554 {
1555 len = 52;
1556 }
1557 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr,
1558 len,
1559 &peer->public_key);
1560 if (GNUNET_OK != ret)
1561 {
1562 LOG (GNUNET_ERROR_TYPE_WARNING,
1563 "Not able to convert string representation of PeerID to PeerID\n"
1564 "String representation: %s\n",
1565 string_repr);
1566 GNUNET_break (0);
1567 }
1568 return peer;
1569}
1570
1571
1572/**
1573 * @brief Restore the peers on disk to #valid_peers.
1574 *
1575 * @param sub Sub for which to restore the valid peers
1576 */
1577static void
1578restore_valid_peers (const struct Sub *sub)
1579{
1580 off_t file_size;
1581 uint32_t num_peers;
1582 struct GNUNET_DISK_FileHandle *fh;
1583 char *buf;
1584 ssize_t size_read;
1585 char *iter_buf;
1586 char *str_repr;
1587 const struct GNUNET_PeerIdentity *peer;
1588
1589 if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7))
1590 {
1591 return;
1592 }
1593
1594 if (GNUNET_OK != GNUNET_DISK_file_test (sub->filename_valid_peers))
1595 {
1596 return;
1597 }
1598 fh = GNUNET_DISK_file_open (sub->filename_valid_peers,
1599 GNUNET_DISK_OPEN_READ,
1600 GNUNET_DISK_PERM_NONE);
1601 GNUNET_assert (NULL != fh);
1602 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size));
1603 num_peers = file_size / 53;
1604 buf = GNUNET_malloc (file_size);
1605 size_read = GNUNET_DISK_file_read (fh, buf, file_size);
1606 GNUNET_assert (size_read == file_size);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG,
1608 "Restoring %" PRIu32 " peers from file `%s'\n",
1609 num_peers,
1610 sub->filename_valid_peers);
1611 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
1612 {
1613 str_repr = GNUNET_strndup (iter_buf, 53);
1614 peer = s2i_full (str_repr);
1615 GNUNET_free (str_repr);
1616 add_valid_peer (peer, sub->valid_peers);
1617 LOG (GNUNET_ERROR_TYPE_DEBUG,
1618 "Restored valid peer %s from disk\n",
1619 GNUNET_i2s_full (peer));
1620 }
1621 iter_buf = NULL;
1622 GNUNET_free (buf);
1623 LOG (GNUNET_ERROR_TYPE_DEBUG,
1624 "num_peers: %" PRIu32 ", _size (sub->valid_peers): %u\n",
1625 num_peers,
1626 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1627 if (num_peers != GNUNET_CONTAINER_multipeermap_size (sub->valid_peers))
1628 {
1629 LOG (GNUNET_ERROR_TYPE_WARNING,
1630 "Number of restored peers does not match file size. Have probably duplicates.\n");
1631 }
1632 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1633 LOG (GNUNET_ERROR_TYPE_DEBUG,
1634 "Restored %u valid peers from disk\n",
1635 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1636}
1637
1638
1639/**
1640 * @brief Delete storage of peers that was created with #initialise_peers ()
1641 *
1642 * @param sub Sub for which the storage is deleted
1643 */
1644static void
1645peers_terminate (struct Sub *sub)
1646{
1647 if (GNUNET_SYSERR ==
1648 GNUNET_CONTAINER_multipeermap_iterate (sub->peer_map,
1649 &peermap_clear_iterator,
1650 sub))
1651 {
1652 LOG (GNUNET_ERROR_TYPE_WARNING,
1653 "Iteration destroying peers was aborted.\n");
1654 }
1655 GNUNET_CONTAINER_multipeermap_destroy (sub->peer_map);
1656 sub->peer_map = NULL;
1657 store_valid_peers (sub);
1658 GNUNET_free (sub->filename_valid_peers);
1659 sub->filename_valid_peers = NULL;
1660 GNUNET_CONTAINER_multipeermap_destroy (sub->valid_peers);
1661 sub->valid_peers = NULL;
1662}
1663
1664
1665/**
1666 * Iterator over #valid_peers hash map entries.
1667 *
1668 * @param cls Closure that contains iterator function and closure
1669 * @param peer current peer id
1670 * @param value value in the hash map - unused
1671 * @return #GNUNET_YES if we should continue to
1672 * iterate,
1673 * #GNUNET_NO if not.
1674 */
1675static int
1676valid_peer_iterator (void *cls,
1677 const struct GNUNET_PeerIdentity *peer,
1678 void *value)
1679{
1680 struct PeersIteratorCls *it_cls = cls;
1681
1682 (void) value;
1683
1684 return it_cls->iterator (it_cls->cls, peer);
1685}
1686
1687
1688/**
1689 * @brief Get all currently known, valid peer ids.
1690 *
1691 * @param valid_peers Peer map containing the valid peers in question
1692 * @param iterator function to call on each peer id
1693 * @param it_cls extra argument to @a iterator
1694 * @return the number of key value pairs processed,
1695 * #GNUNET_SYSERR if it aborted iteration
1696 */
1697static int
1698get_valid_peers (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1699 PeersIterator iterator,
1700 void *it_cls)
1701{
1702 struct PeersIteratorCls *cls;
1703 int ret;
1704
1705 cls = GNUNET_new (struct PeersIteratorCls);
1706 cls->iterator = iterator;
1707 cls->cls = it_cls;
1708 ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
1709 valid_peer_iterator,
1710 cls);
1711 GNUNET_free (cls);
1712 return ret;
1713}
1714
1715
1716/**
1717 * @brief Add peer to known peers.
1718 *
1719 * This function is called on new peer_ids from 'external' sources
1720 * (client seed, cadet get_peers(), ...)
1721 *
1722 * @param sub Sub with the peer map that the @a peer will be added to
1723 * @param peer the new #GNUNET_PeerIdentity
1724 *
1725 * @return #GNUNET_YES if peer was inserted
1726 * #GNUNET_NO otherwise
1727 */
1728static int
1729insert_peer (struct Sub *sub,
1730 const struct GNUNET_PeerIdentity *peer)
1731{
1732 if (GNUNET_YES == check_peer_known (sub->peer_map, peer))
1733 {
1734 return GNUNET_NO; /* We already know this peer - nothing to do */
1735 }
1736 (void) create_peer_ctx (sub, peer);
1737 return GNUNET_YES;
1738}
1739
1740
1741/**
1742 * @brief Check whether flags on a peer are set.
1743 *
1744 * @param peer_map Peer map that is expected to contain the @a peer
1745 * @param peer the peer to check the flag of
1746 * @param flags the flags to check
1747 *
1748 * @return #GNUNET_SYSERR if peer is not known
1749 * #GNUNET_YES if all given flags are set
1750 * #GNUNET_NO otherwise
1751 */
1752static int
1753check_peer_flag (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
1754 const struct GNUNET_PeerIdentity *peer,
1755 enum Peers_PeerFlags flags)
1756{
1757 struct PeerContext *peer_ctx;
1758
1759 if (GNUNET_NO == check_peer_known (peer_map, peer))
1760 {
1761 return GNUNET_SYSERR;
1762 }
1763 peer_ctx = get_peer_ctx (peer_map, peer);
1764 return check_peer_flag_set (peer_ctx, flags);
1765}
1766
1767
1768/**
1769 * @brief Try connecting to a peer to see whether it is online
1770 *
1771 * If not known yet, insert into known peers
1772 *
1773 * @param sub Sub which would contain the @a peer
1774 * @param peer the peer whose online is to be checked
1775 * @return #GNUNET_YES if the check was issued
1776 * #GNUNET_NO otherwise
1777 */
1778static int
1779issue_peer_online_check (struct Sub *sub,
1780 const struct GNUNET_PeerIdentity *peer)
1781{
1782 struct PeerContext *peer_ctx;
1783
1784 (void) insert_peer (sub, peer); // TODO even needed?
1785 peer_ctx = get_peer_ctx (sub->peer_map, peer);
1786 if ((GNUNET_NO == check_peer_flag (sub->peer_map, peer, Peers_ONLINE)) &&
1787 (NULL == peer_ctx->online_check_pending))
1788 {
1789 check_peer_online (peer_ctx);
1790 return GNUNET_YES;
1791 }
1792 return GNUNET_NO;
1793}
1794
1795
1796/**
1797 * @brief Check if peer is removable.
1798 *
1799 * Check if
1800 * - a recv channel exists
1801 * - there are pending messages
1802 * - there is no pending pull reply
1803 *
1804 * @param peer_ctx Context of the peer in question
1805 * @return #GNUNET_YES if peer is removable
1806 * #GNUNET_NO if peer is NOT removable
1807 * #GNUNET_SYSERR if peer is not known
1808 */
1809static int
1810check_removable (const struct PeerContext *peer_ctx)
1811{
1812 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (
1813 peer_ctx->sub->peer_map,
1814 &peer_ctx->peer_id))
1815 {
1816 return GNUNET_SYSERR;
1817 }
1818
1819 if ((NULL != peer_ctx->recv_channel_ctx) ||
1820 (NULL != peer_ctx->pending_messages_head) ||
1821 (GNUNET_YES == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)))
1822 {
1823 return GNUNET_NO;
1824 }
1825 return GNUNET_YES;
1826}
1827
1828
1829/**
1830 * @brief Check whether @a peer is actually a peer.
1831 *
1832 * A valid peer is a peer that we know exists eg. we were connected to once.
1833 *
1834 * @param valid_peers Peer map that would contain the @a peer
1835 * @param peer peer in question
1836 *
1837 * @return #GNUNET_YES if peer is valid
1838 * #GNUNET_NO if peer is not valid
1839 */
1840static int
1841check_peer_valid (const struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1842 const struct GNUNET_PeerIdentity *peer)
1843{
1844 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer);
1845}
1846
1847
1848/**
1849 * @brief Indicate that we want to send to the other peer
1850 *
1851 * This establishes a sending channel
1852 *
1853 * @param peer_ctx Context of the target peer
1854 */
1855static void
1856indicate_sending_intention (struct PeerContext *peer_ctx)
1857{
1858 GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map,
1859 &peer_ctx->peer_id));
1860 (void) get_channel (peer_ctx);
1861}
1862
1863
1864/**
1865 * @brief Check whether other peer has the intention to send/opened channel
1866 * towars us
1867 *
1868 * @param peer_ctx Context of the peer in question
1869 *
1870 * @return #GNUNET_YES if peer has the intention to send
1871 * #GNUNET_NO otherwise
1872 */
1873static int
1874check_peer_send_intention (const struct PeerContext *peer_ctx)
1875{
1876 if (NULL != peer_ctx->recv_channel_ctx)
1877 {
1878 return GNUNET_YES;
1879 }
1880 return GNUNET_NO;
1881}
1882
1883
1884/**
1885 * Handle the channel a peer opens to us.
1886 *
1887 * @param cls The closure - Sub
1888 * @param channel The channel the peer wants to establish
1889 * @param initiator The peer's peer ID
1890 *
1891 * @return initial channel context for the channel
1892 * (can be NULL -- that's not an error)
1893 */
1894static void *
1895handle_inbound_channel (void *cls,
1896 struct GNUNET_CADET_Channel *channel,
1897 const struct GNUNET_PeerIdentity *initiator)
1898{
1899 struct PeerContext *peer_ctx;
1900 struct ChannelCtx *channel_ctx;
1901 struct Sub *sub = cls;
1902
1903 LOG (GNUNET_ERROR_TYPE_DEBUG,
1904 "New channel was established to us (Peer %s).\n",
1905 GNUNET_i2s (initiator));
1906 GNUNET_assert (NULL != channel); /* according to cadet API */
1907 /* Make sure we 'know' about this peer */
1908 peer_ctx = create_or_get_peer_ctx (sub, initiator);
1909 set_peer_online (peer_ctx);
1910 (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers);
1911 channel_ctx = add_channel_ctx (peer_ctx);
1912 channel_ctx->channel = channel;
1913 /* We only accept one incoming channel per peer */
1914 if (GNUNET_YES == check_peer_send_intention (get_peer_ctx (sub->peer_map,
1915 initiator)))
1916 {
1917 LOG (GNUNET_ERROR_TYPE_WARNING,
1918 "Already got one receive channel. Destroying old one.\n");
1919 GNUNET_break_op (0);
1920 destroy_channel (peer_ctx->recv_channel_ctx);
1921 peer_ctx->recv_channel_ctx = channel_ctx;
1922 /* return the channel context */
1923 return channel_ctx;
1924 }
1925 peer_ctx->recv_channel_ctx = channel_ctx;
1926 return channel_ctx;
1927}
1928
1929
1930/**
1931 * @brief Check whether a sending channel towards the given peer exists
1932 *
1933 * @param peer_ctx Context of the peer in question
1934 *
1935 * @return #GNUNET_YES if a sending channel towards that peer exists
1936 * #GNUNET_NO otherwise
1937 */
1938static int
1939check_sending_channel_exists (const struct PeerContext *peer_ctx)
1940{
1941 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
1942 &peer_ctx->peer_id))
1943 { /* If no such peer exists, there is no channel */
1944 return GNUNET_NO;
1945 }
1946 if (NULL == peer_ctx->send_channel_ctx)
1947 {
1948 return GNUNET_NO;
1949 }
1950 return GNUNET_YES;
1951}
1952
1953
1954/**
1955 * @brief Destroy the send channel of a peer e.g. stop indicating a sending
1956 * intention to another peer
1957 *
1958 * @param peer_ctx Context to the peer
1959 * @return #GNUNET_YES if channel was destroyed
1960 * #GNUNET_NO otherwise
1961 */
1962static int
1963destroy_sending_channel (struct PeerContext *peer_ctx)
1964{
1965 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
1966 &peer_ctx->peer_id))
1967 {
1968 return GNUNET_NO;
1969 }
1970 if (NULL != peer_ctx->send_channel_ctx)
1971 {
1972 destroy_channel (peer_ctx->send_channel_ctx);
1973 (void) check_connected (peer_ctx);
1974 return GNUNET_YES;
1975 }
1976 return GNUNET_NO;
1977}
1978
1979
1980/**
1981 * @brief Send a message to another peer.
1982 *
1983 * Keeps track about pending messages so they can be properly removed when the
1984 * peer is destroyed.
1985 *
1986 * @param peer_ctx Context of the peer to which the message is to be sent
1987 * @param ev envelope of the message
1988 * @param type type of the message
1989 */
1990static void
1991send_message (struct PeerContext *peer_ctx,
1992 struct GNUNET_MQ_Envelope *ev,
1993 const char *type)
1994{
1995 struct PendingMessage *pending_msg;
1996 struct GNUNET_MQ_Handle *mq;
1997
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1999 "Sending message to %s of type %s\n",
2000 GNUNET_i2s (&peer_ctx->peer_id),
2001 type);
2002 pending_msg = insert_pending_message (peer_ctx, ev, type);
2003 mq = get_mq (peer_ctx);
2004 GNUNET_MQ_notify_sent (ev,
2005 mq_notify_sent_cb,
2006 pending_msg);
2007 GNUNET_MQ_send (mq, ev);
2008}
2009
2010
2011/**
2012 * @brief Schedule a operation on given peer
2013 *
2014 * Avoids scheduling an operation twice.
2015 *
2016 * @param peer_ctx Context of the peer for which to schedule the operation
2017 * @param peer_op the operation to schedule
2018 * @param cls Closure to @a peer_op
2019 *
2020 * @return #GNUNET_YES if the operation was scheduled
2021 * #GNUNET_NO otherwise
2022 */
2023static int
2024schedule_operation (struct PeerContext *peer_ctx,
2025 const PeerOp peer_op,
2026 void *cls)
2027{
2028 struct PeerPendingOp pending_op;
2029
2030 GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map,
2031 &peer_ctx->peer_id));
2032
2033 // TODO if ONLINE execute immediately
2034
2035 if (GNUNET_NO == check_operation_scheduled (peer_ctx, peer_op))
2036 {
2037 pending_op.op = peer_op;
2038 pending_op.op_cls = cls;
2039 GNUNET_array_append (peer_ctx->pending_ops,
2040 peer_ctx->num_pending_ops,
2041 pending_op);
2042 return GNUNET_YES;
2043 }
2044 return GNUNET_NO;
2045}
2046
2047
2048/***********************************************************************
2049* /Old gnunet-service-rps_peers.c
2050***********************************************************************/
2051
2052
2053/***********************************************************************
2054* Housekeeping with clients
2055***********************************************************************/
2056
2057/**
2058 * Closure used to pass the client and the id to the callback
2059 * that replies to a client's request
2060 */
2061struct ReplyCls
2062{
2063 /**
2064 * DLL
2065 */
2066 struct ReplyCls *next;
2067 struct ReplyCls *prev;
2068
2069 /**
2070 * The identifier of the request
2071 */
2072 uint32_t id;
2073
2074 /**
2075 * The handle to the request
2076 */
2077 struct RPS_SamplerRequestHandle *req_handle;
2078
2079 /**
2080 * The client handle to send the reply to
2081 */
2082 struct ClientContext *cli_ctx;
2083};
2084
2085
2086/**
2087 * Struct used to store the context of a connected client.
2088 */
2089struct ClientContext
2090{
2091 /**
2092 * DLL
2093 */
2094 struct ClientContext *next;
2095 struct ClientContext *prev;
2096
2097 /**
2098 * The message queue to communicate with the client.
2099 */
2100 struct GNUNET_MQ_Handle *mq;
2101
2102 /**
2103 * @brief How many updates this client expects to receive.
2104 */
2105 int64_t view_updates_left;
2106
2107 /**
2108 * @brief Whether this client wants to receive stream updates.
2109 * Either #GNUNET_YES or #GNUNET_NO
2110 */
2111 int8_t stream_update;
2112
2113 /**
2114 * The client handle to send the reply to
2115 */
2116 struct GNUNET_SERVICE_Client *client;
2117
2118 /**
2119 * The #Sub this context belongs to
2120 */
2121 struct Sub *sub;
2122};
2123
2124/**
2125 * DLL with all clients currently connected to us
2126 */
2127struct ClientContext *cli_ctx_head;
2128struct ClientContext *cli_ctx_tail;
2129
2130/***********************************************************************
2131* /Housekeeping with clients
2132***********************************************************************/
2133
2134
2135/***********************************************************************
2136* Util functions
2137***********************************************************************/
2138
2139
2140/**
2141 * Print peerlist to log.
2142 */
2143static void
2144print_peer_list (struct GNUNET_PeerIdentity *list,
2145 unsigned int len)
2146{
2147 unsigned int i;
2148
2149 LOG (GNUNET_ERROR_TYPE_DEBUG,
2150 "Printing peer list of length %u at %p:\n",
2151 len,
2152 list);
2153 for (i = 0; i < len; i++)
2154 {
2155 LOG (GNUNET_ERROR_TYPE_DEBUG,
2156 "%u. peer: %s\n",
2157 i, GNUNET_i2s (&list[i]));
2158 }
2159}
2160
2161
2162/**
2163 * Remove peer from list.
2164 */
2165static void
2166rem_from_list (struct GNUNET_PeerIdentity **peer_list,
2167 unsigned int *list_size,
2168 const struct GNUNET_PeerIdentity *peer)
2169{
2170 unsigned int i;
2171 struct GNUNET_PeerIdentity *tmp;
2172
2173 tmp = *peer_list;
2174
2175 LOG (GNUNET_ERROR_TYPE_DEBUG,
2176 "Removing peer %s from list at %p\n",
2177 GNUNET_i2s (peer),
2178 tmp);
2179
2180 for (i = 0; i < *list_size; i++)
2181 {
2182 if (0 == GNUNET_memcmp (&tmp[i], peer))
2183 {
2184 if (i < *list_size - 1)
2185 { /* Not at the last entry -- shift peers left */
2186 memmove (&tmp[i], &tmp[i + 1],
2187 ((*list_size) - i - 1) * sizeof(struct GNUNET_PeerIdentity));
2188 }
2189 /* Remove last entry (should be now useless PeerID) */
2190 GNUNET_array_grow (tmp, *list_size, (*list_size) - 1);
2191 }
2192 }
2193 *peer_list = tmp;
2194}
2195
2196
2197/**
2198 * Insert PeerID in #view
2199 *
2200 * Called once we know a peer is online.
2201 * Implements #PeerOp
2202 *
2203 * @return GNUNET_OK if peer was actually inserted
2204 * GNUNET_NO if peer was not inserted
2205 */
2206static void
2207insert_in_view_op (void *cls,
2208 const struct GNUNET_PeerIdentity *peer);
2209
2210/**
2211 * Insert PeerID in #view
2212 *
2213 * Called once we know a peer is online.
2214 *
2215 * @param sub Sub in with the view to insert in
2216 * @param peer the peer to insert
2217 *
2218 * @return GNUNET_OK if peer was actually inserted
2219 * GNUNET_NO if peer was not inserted
2220 */
2221static int
2222insert_in_view (struct Sub *sub,
2223 const struct GNUNET_PeerIdentity *peer)
2224{
2225 struct PeerContext *peer_ctx;
2226 int online;
2227 int ret;
2228
2229 online = check_peer_flag (sub->peer_map, peer, Peers_ONLINE);
2230 peer_ctx = get_peer_ctx (sub->peer_map, peer); // TODO indirection needed?
2231 if ((GNUNET_NO == online) ||
2232 (GNUNET_SYSERR == online)) /* peer is not even known */
2233 {
2234 (void) issue_peer_online_check (sub, peer);
2235 (void) schedule_operation (peer_ctx, insert_in_view_op, sub);
2236 return GNUNET_NO;
2237 }
2238 /* Open channel towards peer to keep connection open */
2239 indicate_sending_intention (peer_ctx);
2240 ret = View_put (sub->view, peer);
2241 if (peer_ctx->sub == msub)
2242 {
2243 GNUNET_STATISTICS_set (stats,
2244 "view size",
2245 View_size (peer_ctx->sub->view),
2246 GNUNET_NO);
2247 }
2248 return ret;
2249}
2250
2251
2252/**
2253 * @brief Send view to client
2254 *
2255 * @param cli_ctx the context of the client
2256 * @param view_array the peerids of the view as array (can be empty)
2257 * @param view_size the size of the view array (can be 0)
2258 */
2259static void
2260send_view (const struct ClientContext *cli_ctx,
2261 const struct GNUNET_PeerIdentity *view_array,
2262 uint64_t view_size)
2263{
2264 struct GNUNET_MQ_Envelope *ev;
2265 struct GNUNET_RPS_CS_DEBUG_ViewReply *out_msg;
2266 struct Sub *sub;
2267
2268 if (NULL == view_array)
2269 {
2270 if (NULL == cli_ctx->sub)
2271 sub = msub;
2272 else
2273 sub = cli_ctx->sub;
2274 view_size = View_size (sub->view);
2275 view_array = View_get_as_array (sub->view);
2276 }
2277
2278 ev = GNUNET_MQ_msg_extra (out_msg,
2279 view_size * sizeof(struct GNUNET_PeerIdentity),
2280 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY);
2281 out_msg->num_peers = htonl (view_size);
2282
2283 GNUNET_memcpy (&out_msg[1],
2284 view_array,
2285 view_size * sizeof(struct GNUNET_PeerIdentity));
2286 GNUNET_MQ_send (cli_ctx->mq, ev);
2287}
2288
2289
2290/**
2291 * @brief Send peer from biased stream to client.
2292 *
2293 * TODO merge with send_view, parameterise
2294 *
2295 * @param cli_ctx the context of the client
2296 * @param view_array the peerids of the view as array (can be empty)
2297 * @param view_size the size of the view array (can be 0)
2298 */
2299static void
2300send_stream_peers (const struct ClientContext *cli_ctx,
2301 uint64_t num_peers,
2302 const struct GNUNET_PeerIdentity *peers)
2303{
2304 struct GNUNET_MQ_Envelope *ev;
2305 struct GNUNET_RPS_CS_DEBUG_StreamReply *out_msg;
2306
2307 GNUNET_assert (NULL != peers);
2308
2309 ev = GNUNET_MQ_msg_extra (out_msg,
2310 num_peers * sizeof(struct GNUNET_PeerIdentity),
2311 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY);
2312 out_msg->num_peers = htonl (num_peers);
2313
2314 GNUNET_memcpy (&out_msg[1],
2315 peers,
2316 num_peers * sizeof(struct GNUNET_PeerIdentity));
2317 GNUNET_MQ_send (cli_ctx->mq, ev);
2318}
2319
2320
2321/**
2322 * @brief sends updates to clients that are interested
2323 *
2324 * @param sub Sub for which to notify clients
2325 */
2326static void
2327clients_notify_view_update (const struct Sub *sub)
2328{
2329 struct ClientContext *cli_ctx_iter;
2330 uint64_t num_peers;
2331 const struct GNUNET_PeerIdentity *view_array;
2332
2333 num_peers = View_size (sub->view);
2334 view_array = View_get_as_array (sub->view);
2335 /* check size of view is small enough */
2336 if (GNUNET_MAX_MESSAGE_SIZE < num_peers)
2337 {
2338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2339 "View is too big to send\n");
2340 return;
2341 }
2342
2343 for (cli_ctx_iter = cli_ctx_head;
2344 NULL != cli_ctx_iter;
2345 cli_ctx_iter = cli_ctx_iter->next)
2346 {
2347 if (1 < cli_ctx_iter->view_updates_left)
2348 {
2349 /* Client wants to receive limited amount of updates */
2350 cli_ctx_iter->view_updates_left -= 1;
2351 }
2352 else if (1 == cli_ctx_iter->view_updates_left)
2353 {
2354 /* Last update of view for client */
2355 cli_ctx_iter->view_updates_left = -1;
2356 }
2357 else if (0 > cli_ctx_iter->view_updates_left)
2358 {
2359 /* Client is not interested in updates */
2360 continue;
2361 }
2362 /* else _updates_left == 0 - infinite amount of updates */
2363
2364 /* send view */
2365 send_view (cli_ctx_iter, view_array, num_peers);
2366 }
2367}
2368
2369
2370/**
2371 * @brief sends updates to clients that are interested
2372 *
2373 * @param num_peers Number of peers to send
2374 * @param peers the array of peers to send
2375 */
2376static void
2377clients_notify_stream_peer (const struct Sub *sub,
2378 uint64_t num_peers,
2379 const struct GNUNET_PeerIdentity *peers)
2380// TODO enum StreamPeerSource)
2381{
2382 struct ClientContext *cli_ctx_iter;
2383
2384 LOG (GNUNET_ERROR_TYPE_DEBUG,
2385 "Got peer (%s) from biased stream - update all clients\n",
2386 GNUNET_i2s (peers));
2387
2388 for (cli_ctx_iter = cli_ctx_head;
2389 NULL != cli_ctx_iter;
2390 cli_ctx_iter = cli_ctx_iter->next)
2391 {
2392 if ((GNUNET_YES == cli_ctx_iter->stream_update) &&
2393 ((sub == cli_ctx_iter->sub) || (sub == msub) ))
2394 {
2395 send_stream_peers (cli_ctx_iter, num_peers, peers);
2396 }
2397 }
2398}
2399
2400
2401/**
2402 * Put random peer from sampler into the view as history update.
2403 *
2404 * @param ids Array of Peers to insert into view
2405 * @param num_peers Number of peers to insert
2406 * @param cls Closure - The Sub for which this is to be done
2407 */
2408static void
2409hist_update (const struct GNUNET_PeerIdentity *ids,
2410 uint32_t num_peers,
2411 void *cls)
2412{
2413 unsigned int i;
2414 struct Sub *sub = cls;
2415
2416 for (i = 0; i < num_peers; i++)
2417 {
2418 int inserted;
2419 if (GNUNET_YES != check_peer_known (sub->peer_map, &ids[i]))
2420 {
2421 LOG (GNUNET_ERROR_TYPE_WARNING,
2422 "Peer in history update not known!\n");
2423 continue;
2424 }
2425 inserted = insert_in_view (sub, &ids[i]);
2426 if (GNUNET_OK == inserted)
2427 {
2428 clients_notify_stream_peer (sub, 1, &ids[i]);
2429 }
2430#ifdef TO_FILE_FULL
2431 to_file (sub->file_name_view_log,
2432 "+%s\t(history)",
2433 GNUNET_i2s_full (ids));
2434#endif /* TO_FILE_FULL */
2435 }
2436 clients_notify_view_update (sub);
2437}
2438
2439
2440/**
2441 * Wrapper around #RPS_sampler_resize()
2442 *
2443 * If we do not have enough sampler elements, double current sampler size
2444 * If we have more than enough sampler elements, halv current sampler size
2445 *
2446 * @param sampler The sampler to resize
2447 * @param new_size New size to which to resize
2448 */
2449static void
2450resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
2451{
2452 unsigned int sampler_size;
2453
2454 // TODO statistics
2455 // TODO respect the min, max
2456 sampler_size = RPS_sampler_get_size (sampler);
2457 if (sampler_size > new_size * 4)
2458 { /* Shrinking */
2459 RPS_sampler_resize (sampler, sampler_size / 2);
2460 }
2461 else if (sampler_size < new_size)
2462 { /* Growing */
2463 RPS_sampler_resize (sampler, sampler_size * 2);
2464 }
2465 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
2466}
2467
2468
2469#if ENABLE_MALICIOUS
2470/**
2471 * Add all peers in @a peer_array to @a peer_map used as set.
2472 *
2473 * @param peer_array array containing the peers
2474 * @param num_peers number of peers in @peer_array
2475 * @param peer_map the peermap to use as set
2476 */
2477static void
2478add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
2479 unsigned int num_peers,
2480 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
2481{
2482 unsigned int i;
2483
2484 if (NULL == peer_map)
2485 {
2486 LOG (GNUNET_ERROR_TYPE_WARNING,
2487 "Trying to add peers to non-existing peermap.\n");
2488 return;
2489 }
2490
2491 for (i = 0; i < num_peers; i++)
2492 {
2493 GNUNET_CONTAINER_multipeermap_put (peer_map,
2494 &peer_array[i],
2495 NULL,
2496 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2497 if (msub->peer_map == peer_map)
2498 {
2499 GNUNET_STATISTICS_set (stats,
2500 "# known peers",
2501 GNUNET_CONTAINER_multipeermap_size (peer_map),
2502 GNUNET_NO);
2503 }
2504 }
2505}
2506
2507
2508#endif /* ENABLE_MALICIOUS */
2509
2510
2511/**
2512 * Send a PULL REPLY to @a peer_id
2513 *
2514 * @param peer_ctx Context of the peer to send the reply to
2515 * @param peer_ids the peers to send to @a peer_id
2516 * @param num_peer_ids the number of peers to send to @a peer_id
2517 */
2518static void
2519send_pull_reply (struct PeerContext *peer_ctx,
2520 const struct GNUNET_PeerIdentity *peer_ids,
2521 unsigned int num_peer_ids)
2522{
2523 uint32_t send_size;
2524 struct GNUNET_MQ_Envelope *ev;
2525 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
2526
2527 /* Compute actual size */
2528 send_size = sizeof(struct GNUNET_RPS_P2P_PullReplyMessage)
2529 + num_peer_ids * sizeof(struct GNUNET_PeerIdentity);
2530
2531 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
2532 /* Compute number of peers to send
2533 * If too long, simply truncate */
2534 // TODO select random ones via permutation
2535 // or even better: do good protocol design
2536 send_size =
2537 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE
2538 - sizeof(struct GNUNET_RPS_P2P_PullReplyMessage))
2539 / sizeof(struct GNUNET_PeerIdentity);
2540 else
2541 send_size = num_peer_ids;
2542
2543 LOG (GNUNET_ERROR_TYPE_DEBUG,
2544 "Going to send PULL REPLY with %u peers to %s\n",
2545 send_size, GNUNET_i2s (&peer_ctx->peer_id));
2546
2547 ev = GNUNET_MQ_msg_extra (out_msg,
2548 send_size * sizeof(struct GNUNET_PeerIdentity),
2549 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
2550 out_msg->num_peers = htonl (send_size);
2551 GNUNET_memcpy (&out_msg[1], peer_ids,
2552 send_size * sizeof(struct GNUNET_PeerIdentity));
2553
2554 send_message (peer_ctx, ev, "PULL REPLY");
2555 if (peer_ctx->sub == msub)
2556 {
2557 GNUNET_STATISTICS_update (stats, "# pull reply send issued", 1, GNUNET_NO);
2558 }
2559 // TODO check with send intention: as send_channel is used/opened we indicate
2560 // a sending intention without intending it.
2561 // -> clean peer afterwards?
2562 // -> use recv_channel?
2563}
2564
2565
2566/**
2567 * Insert PeerID in #pull_map
2568 *
2569 * Called once we know a peer is online.
2570 *
2571 * @param cls Closure - Sub with the pull map to insert into
2572 * @param peer Peer to insert
2573 */
2574static void
2575insert_in_pull_map (void *cls,
2576 const struct GNUNET_PeerIdentity *peer)
2577{
2578 struct Sub *sub = cls;
2579
2580 CustomPeerMap_put (sub->pull_map, peer);
2581}
2582
2583
2584/**
2585 * Insert PeerID in #view
2586 *
2587 * Called once we know a peer is online.
2588 * Implements #PeerOp
2589 *
2590 * @param cls Closure - Sub with view to insert peer into
2591 * @param peer the peer to insert
2592 */
2593static void
2594insert_in_view_op (void *cls,
2595 const struct GNUNET_PeerIdentity *peer)
2596{
2597 struct Sub *sub = cls;
2598 int inserted;
2599
2600 inserted = insert_in_view (sub, peer);
2601 if (GNUNET_OK == inserted)
2602 {
2603 clients_notify_stream_peer (sub, 1, peer);
2604 }
2605}
2606
2607
2608/**
2609 * Update sampler with given PeerID.
2610 * Implements #PeerOp
2611 *
2612 * @param cls Closure - Sub containing the sampler to insert into
2613 * @param peer Peer to insert
2614 */
2615static void
2616insert_in_sampler (void *cls,
2617 const struct GNUNET_PeerIdentity *peer)
2618{
2619 struct Sub *sub = cls;
2620
2621 LOG (GNUNET_ERROR_TYPE_DEBUG,
2622 "Updating samplers with peer %s from insert_in_sampler()\n",
2623 GNUNET_i2s (peer));
2624 RPS_sampler_update (sub->sampler, peer);
2625 if (0 < RPS_sampler_count_id (sub->sampler, peer))
2626 {
2627 /* Make sure we 'know' about this peer */
2628 (void) issue_peer_online_check (sub, peer);
2629 /* Establish a channel towards that peer to indicate we are going to send
2630 * messages to it */
2631 // indicate_sending_intention (peer);
2632 }
2633 if (sub == msub)
2634 {
2635 GNUNET_STATISTICS_update (stats,
2636 "# observed peers in gossip",
2637 1,
2638 GNUNET_NO);
2639 }
2640#ifdef TO_FILE
2641 sub->num_observed_peers++;
2642 (void) GNUNET_CONTAINER_multipeermap_put
2643 (sub->observed_unique_peers,
2644 peer,
2645 NULL,
2646 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2647 uint32_t num_observed_unique_peers =
2648 GNUNET_CONTAINER_multipeermap_size (sub->observed_unique_peers);
2649 GNUNET_STATISTICS_set (stats,
2650 "# unique peers in gossip",
2651 num_observed_unique_peers,
2652 GNUNET_NO);
2653#ifdef TO_FILE_FULL
2654 to_file (sub->file_name_observed_log,
2655 "%" PRIu32 " %" PRIu32 " %f\n",
2656 sub->num_observed_peers,
2657 num_observed_unique_peers,
2658 1.0 * num_observed_unique_peers / sub->num_observed_peers)
2659#endif /* TO_FILE_FULL */
2660#endif /* TO_FILE */
2661}
2662
2663
2664/**
2665 * @brief This is called on peers from external sources (cadet, peerinfo, ...)
2666 * If the peer is not known, online check is issued and it is
2667 * scheduled to be inserted in sampler and view.
2668 *
2669 * "External sources" refer to every source except the gossip.
2670 *
2671 * @param sub Sub for which @a peer was received
2672 * @param peer peer to insert/peer received
2673 */
2674static void
2675got_peer (struct Sub *sub,
2676 const struct GNUNET_PeerIdentity *peer)
2677{
2678 /* If we did not know this peer already, insert it into sampler and view */
2679 if (GNUNET_YES == issue_peer_online_check (sub, peer))
2680 {
2681 schedule_operation (get_peer_ctx (sub->peer_map, peer),
2682 &insert_in_sampler, sub);
2683 schedule_operation (get_peer_ctx (sub->peer_map, peer),
2684 &insert_in_view_op, sub);
2685 }
2686 if (sub == msub)
2687 {
2688 GNUNET_STATISTICS_update (stats,
2689 "# learnd peers",
2690 1,
2691 GNUNET_NO);
2692 }
2693}
2694
2695
2696/**
2697 * @brief Checks if there is a sending channel and if it is needed
2698 *
2699 * @param peer_ctx Context of the peer to check
2700 * @return GNUNET_YES if sending channel exists and is still needed
2701 * GNUNET_NO otherwise
2702 */
2703static int
2704check_sending_channel_needed (const struct PeerContext *peer_ctx)
2705{
2706 /* struct GNUNET_CADET_Channel *channel; */
2707 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
2708 &peer_ctx->peer_id))
2709 {
2710 return GNUNET_NO;
2711 }
2712 if (GNUNET_YES == check_sending_channel_exists (peer_ctx))
2713 {
2714 if ((0 < RPS_sampler_count_id (peer_ctx->sub->sampler,
2715 &peer_ctx->peer_id)) ||
2716 (GNUNET_YES == View_contains_peer (peer_ctx->sub->view,
2717 &peer_ctx->peer_id)) ||
2718 (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->push_map,
2719 &peer_ctx->peer_id)) ||
2720 (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->pull_map,
2721 &peer_ctx->peer_id)) ||
2722 (GNUNET_YES == check_peer_flag (peer_ctx->sub->peer_map,
2723 &peer_ctx->peer_id,
2724 Peers_PULL_REPLY_PENDING)))
2725 { /* If we want to keep the connection to peer open */
2726 return GNUNET_YES;
2727 }
2728 return GNUNET_NO;
2729 }
2730 return GNUNET_NO;
2731}
2732
2733
2734/**
2735 * @brief remove peer from our knowledge, the view, push and pull maps and
2736 * samplers.
2737 *
2738 * @param sub Sub with the data structures the peer is to be removed from
2739 * @param peer the peer to remove
2740 */
2741static void
2742remove_peer (struct Sub *sub,
2743 const struct GNUNET_PeerIdentity *peer)
2744{
2745 (void) View_remove_peer (sub->view,
2746 peer);
2747 CustomPeerMap_remove_peer (sub->pull_map,
2748 peer);
2749 CustomPeerMap_remove_peer (sub->push_map,
2750 peer);
2751 RPS_sampler_reinitialise_by_value (sub->sampler,
2752 peer);
2753 /* We want to destroy the peer now.
2754 * Sometimes, it just seems that it's already been removed from the peer_map,
2755 * so check the peer_map first. */
2756 if (GNUNET_YES == check_peer_known (sub->peer_map,
2757 peer))
2758 {
2759 destroy_peer (get_peer_ctx (sub->peer_map,
2760 peer));
2761 }
2762}
2763
2764
2765/**
2766 * @brief Remove data that is not needed anymore.
2767 *
2768 * If the sending channel is no longer needed it is destroyed.
2769 *
2770 * @param sub Sub in which the current peer is to be cleaned
2771 * @param peer the peer whose data is about to be cleaned
2772 */
2773static void
2774clean_peer (struct Sub *sub,
2775 const struct GNUNET_PeerIdentity *peer)
2776{
2777 if (GNUNET_NO == check_sending_channel_needed (get_peer_ctx (sub->peer_map,
2778 peer)))
2779 {
2780 LOG (GNUNET_ERROR_TYPE_DEBUG,
2781 "Going to remove send channel to peer %s\n",
2782 GNUNET_i2s (peer));
2783 #if ENABLE_MALICIOUS
2784 if (0 != GNUNET_memcmp (&attacked_peer,
2785 peer))
2786 (void) destroy_sending_channel (get_peer_ctx (sub->peer_map,
2787 peer));
2788 #else /* ENABLE_MALICIOUS */
2789 (void) destroy_sending_channel (get_peer_ctx (sub->peer_map,
2790 peer));
2791 #endif /* ENABLE_MALICIOUS */
2792 }
2793
2794 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (sub->peer_map,
2795 peer))
2796 {
2797 /* Peer was already removed by callback on destroyed channel */
2798 LOG (GNUNET_ERROR_TYPE_WARNING,
2799 "Peer was removed from our knowledge during cleanup\n");
2800 return;
2801 }
2802
2803 if ((GNUNET_NO == check_peer_send_intention (get_peer_ctx (sub->peer_map,
2804 peer))) &&
2805 (GNUNET_NO == View_contains_peer (sub->view, peer)) &&
2806 (GNUNET_NO == CustomPeerMap_contains_peer (sub->push_map, peer)) &&
2807 (GNUNET_NO == CustomPeerMap_contains_peer (sub->pull_map, peer)) &&
2808 (0 == RPS_sampler_count_id (sub->sampler, peer)) &&
2809 (GNUNET_YES == check_removable (get_peer_ctx (sub->peer_map, peer))))
2810 { /* We can safely remove this peer */
2811 LOG (GNUNET_ERROR_TYPE_DEBUG,
2812 "Going to remove peer %s\n",
2813 GNUNET_i2s (peer));
2814 remove_peer (sub, peer);
2815 return;
2816 }
2817}
2818
2819
2820/**
2821 * @brief This is called when a channel is destroyed.
2822 *
2823 * Removes peer completely from our knowledge if the send_channel was destroyed
2824 * Otherwise simply delete the recv_channel
2825 * Also check if the knowledge about this peer is still needed.
2826 * If not, remove this peer from our knowledge.
2827 *
2828 * @param cls The closure - Context to the channel
2829 * @param channel The channel being closed
2830 */
2831static void
2832cleanup_destroyed_channel (void *cls,
2833 const struct GNUNET_CADET_Channel *channel)
2834{
2835 struct ChannelCtx *channel_ctx = cls;
2836 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
2837
2838 (void) channel;
2839
2840 channel_ctx->channel = NULL;
2841 if ((NULL != peer_ctx) &&
2842 (peer_ctx->send_channel_ctx == channel_ctx) &&
2843 (GNUNET_YES == check_sending_channel_needed (peer_ctx)) )
2844 {
2845 remove_channel_ctx (channel_ctx);
2846 remove_peer (peer_ctx->sub, &peer_ctx->peer_id);
2847 }
2848 else
2849 {
2850 /* We need this if-else construct because we need to make sure the channel
2851 * (context) is cleaned up before removing the peer, but still need to
2852 * compare it while checking the condition */
2853 remove_channel_ctx (channel_ctx);
2854 }
2855}
2856
2857
2858/***********************************************************************
2859* /Util functions
2860***********************************************************************/
2861
2862
2863/***********************************************************************
2864* Sub
2865***********************************************************************/
2866
2867/**
2868 * @brief Create a new Sub
2869 *
2870 * @param hash Hash of value shared among rps instances on other hosts that
2871 * defines a subgroup to sample from.
2872 * @param sampler_size Size of the sampler
2873 * @param round_interval Interval (in average) between two rounds
2874 *
2875 * @return Sub
2876 */
2877struct Sub *
2878new_sub (const struct GNUNET_HashCode *hash,
2879 uint32_t sampler_size,
2880 struct GNUNET_TIME_Relative round_interval)
2881{
2882 struct Sub *sub;
2883
2884 sub = GNUNET_new (struct Sub);
2885
2886 /* With the hash generated from the secret value this service only connects
2887 * to rps instances that share the value */
2888 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
2889 GNUNET_MQ_hd_fixed_size (peer_check,
2890 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2891 struct GNUNET_MessageHeader,
2892 NULL),
2893 GNUNET_MQ_hd_fixed_size (peer_push,
2894 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2895 struct GNUNET_MessageHeader,
2896 NULL),
2897 GNUNET_MQ_hd_fixed_size (peer_pull_request,
2898 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2899 struct GNUNET_MessageHeader,
2900 NULL),
2901 GNUNET_MQ_hd_var_size (peer_pull_reply,
2902 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
2903 struct GNUNET_RPS_P2P_PullReplyMessage,
2904 NULL),
2905 GNUNET_MQ_handler_end ()
2906 };
2907 sub->hash = *hash;
2908 sub->cadet_port =
2909 GNUNET_CADET_open_port (cadet_handle,
2910 &sub->hash,
2911 &handle_inbound_channel, /* Connect handler */
2912 sub, /* cls */
2913 NULL, /* WindowSize handler */
2914 &cleanup_destroyed_channel, /* Disconnect handler */
2915 cadet_handlers);
2916 if (NULL == sub->cadet_port)
2917 {
2918 LOG (GNUNET_ERROR_TYPE_ERROR,
2919 "Cadet port `%s' is already in use.\n",
2920 GNUNET_APPLICATION_PORT_RPS);
2921 GNUNET_assert (0);
2922 }
2923
2924 /* Set up general data structure to keep track about peers */
2925 sub->valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2926 if (GNUNET_OK !=
2927 GNUNET_CONFIGURATION_get_value_filename (cfg,
2928 "rps",
2929 "FILENAME_VALID_PEERS",
2930 &sub->filename_valid_peers))
2931 {
2932 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2933 "rps",
2934 "FILENAME_VALID_PEERS");
2935 }
2936 if (0 != strncmp ("DISABLE", sub->filename_valid_peers, 7))
2937 {
2938 char *tmp_filename_valid_peers;
2939 char str_hash[105];
2940
2941 GNUNET_snprintf (str_hash,
2942 sizeof(str_hash), "%s",
2943 GNUNET_h2s_full (hash));
2944 tmp_filename_valid_peers = sub->filename_valid_peers;
2945 GNUNET_asprintf (&sub->filename_valid_peers,
2946 "%s%s",
2947 tmp_filename_valid_peers,
2948 str_hash);
2949 GNUNET_free (tmp_filename_valid_peers);
2950 }
2951 sub->peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2952
2953 /* Set up the sampler */
2954 sub->sampler_size_est_min = sampler_size;
2955 sub->sampler_size_est_need = sampler_size;;
2956 LOG (GNUNET_ERROR_TYPE_DEBUG, "MINSIZE is %u\n", sub->sampler_size_est_min);
2957 GNUNET_assert (0 != round_interval.rel_value_us);
2958 sub->round_interval = round_interval;
2959 sub->sampler = RPS_sampler_init (sampler_size,
2960 round_interval);
2961
2962 /* Logging of internals */
2963#ifdef TO_FILE_FULL
2964 // FIXME: The service cannot know the index, which is required by this
2965 // function:
2966 // sub->file_name_view_log = store_prefix_file_name (&own_identity, "view");
2967#endif /* TO_FILE_FULL */
2968#ifdef TO_FILE
2969#ifdef TO_FILE_FULL
2970 // FIXME: The service cannot know the index, which is required by this
2971 // function:
2972 // sub->file_name_observed_log = store_prefix_file_name (&own_identity,
2973 // "observed");
2974#endif /* TO_FILE_FULL */
2975 sub->num_observed_peers = 0;
2976 sub->observed_unique_peers = GNUNET_CONTAINER_multipeermap_create (1,
2977 GNUNET_NO);
2978#endif /* TO_FILE */
2979
2980 /* Set up data structures for gossip */
2981 sub->push_map = CustomPeerMap_create (4);
2982 sub->pull_map = CustomPeerMap_create (4);
2983 sub->view_size_est_min = sampler_size;;
2984 sub->view = View_create (sub->view_size_est_min);
2985 if (sub == msub)
2986 {
2987 GNUNET_STATISTICS_set (stats,
2988 "view size aim",
2989 sub->view_size_est_min,
2990 GNUNET_NO);
2991 }
2992
2993 /* Start executing rounds */
2994 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub);
2995
2996 return sub;
2997}
2998
2999
3000#ifdef TO_FILE
3001// /**
3002// * @brief Write all numbers in the given array into the given file
3003// *
3004// * Single numbers divided by a newline
3005// *
3006// * FIXME: The call to store_prefix_file_name expects the index of the peer,
3007// * which cannot be known to the service.
3008// * Write a dedicated function that uses the peer id.
3009// *
3010// * @param hist_array[] the array to dump
3011// * @param file_name file to dump into
3012// */
3013// static void
3014// write_histogram_to_file (const uint32_t hist_array[],
3015// const char *file_name)
3016// {
3017// char collect_str[SIZE_DUMP_FILE + 1] = "";
3018// char *recv_str_iter;
3019// char *file_name_full;
3020//
3021// recv_str_iter = collect_str;
3022// file_name_full = store_prefix_file_name (&own_identity,
3023// file_name);
3024// for (uint32_t i = 0; i < HISTOGRAM_FILE_SLOTS; i++)
3025// {
3026// char collect_str_tmp[8];
3027//
3028// GNUNET_snprintf (collect_str_tmp,
3029// sizeof(collect_str_tmp),
3030// "%" PRIu32 "\n",
3031// hist_array[i]);
3032// recv_str_iter = stpncpy (recv_str_iter,
3033// collect_str_tmp,
3034// 6);
3035// }
3036// (void) stpcpy (recv_str_iter,
3037// "\n");
3038// LOG (GNUNET_ERROR_TYPE_DEBUG,
3039// "Writing push stats to disk\n");
3040// to_file_w_len (file_name_full,
3041// SIZE_DUMP_FILE, "%s",
3042// collect_str);
3043// GNUNET_free (file_name_full);
3044// }
3045
3046
3047#endif /* TO_FILE */
3048
3049
3050/**
3051 * @brief Destroy Sub.
3052 *
3053 * @param sub Sub to destroy
3054 */
3055static void
3056destroy_sub (struct Sub *sub)
3057{
3058 GNUNET_assert (NULL != sub);
3059 GNUNET_assert (NULL != sub->do_round_task);
3060 GNUNET_SCHEDULER_cancel (sub->do_round_task);
3061 sub->do_round_task = NULL;
3062
3063 /* Disconnect from cadet */
3064 GNUNET_CADET_close_port (sub->cadet_port);
3065 sub->cadet_port = NULL;
3066
3067 /* Clean up data structures for peers */
3068 RPS_sampler_destroy (sub->sampler);
3069 sub->sampler = NULL;
3070 View_destroy (sub->view);
3071 sub->view = NULL;
3072 CustomPeerMap_destroy (sub->push_map);
3073 sub->push_map = NULL;
3074 CustomPeerMap_destroy (sub->pull_map);
3075 sub->pull_map = NULL;
3076 peers_terminate (sub);
3077
3078 /* Free leftover data structures */
3079#ifdef TO_FILE_FULL
3080 GNUNET_free (sub->file_name_view_log);
3081 sub->file_name_view_log = NULL;
3082#endif /* TO_FILE_FULL */
3083#ifdef TO_FILE
3084#ifdef TO_FILE_FULL
3085 GNUNET_free (sub->file_name_observed_log);
3086 sub->file_name_observed_log = NULL;
3087#endif /* TO_FILE_FULL */
3088
3089 // FIXME: Currently this calls malfunctionning code
3090 // /* Write push frequencies to disk */
3091 // write_histogram_to_file (sub->push_recv,
3092 // "push_recv");
3093
3094 // /* Write push deltas to disk */
3095 // write_histogram_to_file (sub->push_delta,
3096 // "push_delta");
3097
3098 // /* Write pull delays to disk */
3099 // write_histogram_to_file (sub->pull_delays,
3100 // "pull_delays");
3101
3102 GNUNET_CONTAINER_multipeermap_destroy (sub->observed_unique_peers);
3103 sub->observed_unique_peers = NULL;
3104#endif /* TO_FILE */
3105
3106 GNUNET_free (sub);
3107}
3108
3109
3110/***********************************************************************
3111* /Sub
3112***********************************************************************/
3113
3114
3115/***********************************************************************
3116* Core handlers
3117***********************************************************************/
3118
3119/**
3120 * @brief Callback on initialisation of Core.
3121 *
3122 * @param cls - unused
3123 * @param my_identity - unused
3124 */
3125void
3126core_init (void *cls,
3127 const struct GNUNET_PeerIdentity *my_identity)
3128{
3129 (void) cls;
3130 (void) my_identity;
3131
3132 map_single_hop = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
3133}
3134
3135
3136/**
3137 * @brief Callback for core.
3138 * Method called whenever a given peer connects.
3139 *
3140 * @param cls closure - unused
3141 * @param peer peer identity this notification is about
3142 * @return closure given to #core_disconnects as peer_cls
3143 */
3144void *
3145core_connects (void *cls,
3146 const struct GNUNET_PeerIdentity *peer,
3147 struct GNUNET_MQ_Handle *mq)
3148{
3149 (void) cls;
3150 (void) mq;
3151
3152 GNUNET_assert (GNUNET_YES ==
3153 GNUNET_CONTAINER_multipeermap_put (map_single_hop,
3154 peer,
3155 NULL,
3156 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3157 return NULL;
3158}
3159
3160
3161/**
3162 * @brief Callback for core.
3163 * Method called whenever a peer disconnects.
3164 *
3165 * @param cls closure - unused
3166 * @param peer peer identity this notification is about
3167 * @param peer_cls closure given in #core_connects - unused
3168 */
3169void
3170core_disconnects (void *cls,
3171 const struct GNUNET_PeerIdentity *peer,
3172 void *peer_cls)
3173{
3174 (void) cls;
3175 (void) peer_cls;
3176
3177 GNUNET_CONTAINER_multipeermap_remove_all (map_single_hop, peer);
3178}
3179
3180
3181/***********************************************************************
3182* /Core handlers
3183***********************************************************************/
3184
3185
3186/**
3187 * @brief Destroy the context for a (connected) client
3188 *
3189 * @param cli_ctx Context to destroy
3190 */
3191static void
3192destroy_cli_ctx (struct ClientContext *cli_ctx)
3193{
3194 GNUNET_assert (NULL != cli_ctx);
3195 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
3196 cli_ctx_tail,
3197 cli_ctx);
3198 if (NULL != cli_ctx->sub)
3199 {
3200 destroy_sub (cli_ctx->sub);
3201 cli_ctx->sub = NULL;
3202 }
3203 GNUNET_free (cli_ctx);
3204}
3205
3206
3207/**
3208 * @brief Update sizes in sampler and view on estimate update from nse service
3209 *
3210 * @param sub Sub
3211 * @param logestimate the log(Base 2) value of the current network size estimate
3212 * @param std_dev standard deviation for the estimate
3213 */
3214static void
3215adapt_sizes (struct Sub *sub, double logestimate, double std_dev)
3216{
3217 double estimate;
3218
3219 // double scale; // TODO this might go global/config
3220
3221 LOG (GNUNET_ERROR_TYPE_DEBUG,
3222 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
3223 logestimate, std_dev, RPS_sampler_get_size (sub->sampler));
3224 // scale = .01;
3225 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
3226 // GNUNET_NSE_log_estimate_to_n (logestimate);
3227 estimate = pow (estimate, 1.0 / 3);
3228 // TODO add if std_dev is a number
3229 // estimate += (std_dev * scale);
3230 if (sub->view_size_est_min < ceil (estimate))
3231 {
3232 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
3233 sub->sampler_size_est_need = estimate;
3234 sub->view_size_est_need = estimate;
3235 }
3236 else
3237 {
3238 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
3239 // sub->sampler_size_est_need = sub->view_size_est_min;
3240 sub->view_size_est_need = sub->view_size_est_min;
3241 }
3242 if (sub == msub)
3243 {
3244 GNUNET_STATISTICS_set (stats,
3245 "view size aim",
3246 sub->view_size_est_need,
3247 GNUNET_NO);
3248 }
3249
3250 /* If the NSE has changed adapt the lists accordingly */
3251 resize_wrapper (sub->sampler, sub->sampler_size_est_need);
3252 View_change_len (sub->view, sub->view_size_est_need);
3253}
3254
3255
3256/**
3257 * Function called by NSE.
3258 *
3259 * Updates sizes of sampler list and view and adapt those lists
3260 * accordingly.
3261 *
3262 * implements #GNUNET_NSE_Callback
3263 *
3264 * @param cls Closure - unused
3265 * @param timestamp time when the estimate was received from the server (or created by the server)
3266 * @param logestimate the log(Base 2) value of the current network size estimate
3267 * @param std_dev standard deviation for the estimate
3268 */
3269static void
3270nse_callback (void *cls,
3271 struct GNUNET_TIME_Absolute timestamp,
3272 double logestimate, double std_dev)
3273{
3274 (void) cls;
3275 (void) timestamp;
3276 struct ClientContext *cli_ctx_iter;
3277
3278 adapt_sizes (msub, logestimate, std_dev);
3279 for (cli_ctx_iter = cli_ctx_head;
3280 NULL != cli_ctx_iter;
3281 cli_ctx_iter = cli_ctx_iter->next)
3282 {
3283 if (NULL != cli_ctx_iter->sub)
3284 {
3285 adapt_sizes (cli_ctx_iter->sub, logestimate, std_dev);
3286 }
3287 }
3288}
3289
3290
3291/**
3292 * @brief This function is called, when the client seeds peers.
3293 * It verifies that @a msg is well-formed.
3294 *
3295 * @param cls the closure (#ClientContext)
3296 * @param msg the message
3297 * @return #GNUNET_OK if @a msg is well-formed
3298 * #GNUNET_SYSERR otherwise
3299 */
3300static int
3301check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
3302{
3303 struct ClientContext *cli_ctx = cls;
3304 uint16_t msize = ntohs (msg->header.size);
3305 uint32_t num_peers = ntohl (msg->num_peers);
3306
3307 msize -= sizeof(struct GNUNET_RPS_CS_SeedMessage);
3308 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
3309 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
3310 {
3311 LOG (GNUNET_ERROR_TYPE_ERROR,
3312 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3313 ntohl (msg->num_peers),
3314 (msize / sizeof(struct GNUNET_PeerIdentity)));
3315 GNUNET_break (0);
3316 GNUNET_SERVICE_client_drop (cli_ctx->client);
3317 return GNUNET_SYSERR;
3318 }
3319 return GNUNET_OK;
3320}
3321
3322
3323/**
3324 * Handle seed from the client.
3325 *
3326 * @param cls closure
3327 * @param message the actual message
3328 */
3329static void
3330handle_client_seed (void *cls,
3331 const struct GNUNET_RPS_CS_SeedMessage *msg)
3332{
3333 struct ClientContext *cli_ctx = cls;
3334 struct GNUNET_PeerIdentity *peers;
3335 uint32_t num_peers;
3336 uint32_t i;
3337
3338 num_peers = ntohl (msg->num_peers);
3339 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3340
3341 LOG (GNUNET_ERROR_TYPE_DEBUG,
3342 "Client seeded peers:\n");
3343 print_peer_list (peers, num_peers);
3344
3345 for (i = 0; i < num_peers; i++)
3346 {
3347 LOG (GNUNET_ERROR_TYPE_DEBUG,
3348 "Updating samplers with seed %" PRIu32 ": %s\n",
3349 i,
3350 GNUNET_i2s (&peers[i]));
3351
3352 if (NULL != msub)
3353 got_peer (msub, &peers[i]); /* Condition needed? */
3354 if (NULL != cli_ctx->sub)
3355 got_peer (cli_ctx->sub, &peers[i]);
3356 }
3357 GNUNET_SERVICE_client_continue (cli_ctx->client);
3358}
3359
3360
3361/**
3362 * Handle RPS request from the client.
3363 *
3364 * @param cls Client context
3365 * @param message Message containing the number of updates the client wants to
3366 * receive
3367 */
3368static void
3369handle_client_view_request (void *cls,
3370 const struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg)
3371{
3372 struct ClientContext *cli_ctx = cls;
3373 uint64_t num_updates;
3374
3375 num_updates = ntohl (msg->num_updates);
3376
3377 LOG (GNUNET_ERROR_TYPE_DEBUG,
3378 "Client requested %" PRIu64 " updates of view.\n",
3379 num_updates);
3380
3381 GNUNET_assert (NULL != cli_ctx);
3382 cli_ctx->view_updates_left = num_updates;
3383 send_view (cli_ctx, NULL, 0);
3384 GNUNET_SERVICE_client_continue (cli_ctx->client);
3385}
3386
3387
3388/**
3389 * @brief Handle the cancellation of the view updates.
3390 *
3391 * @param cls The client context
3392 * @param msg Unused
3393 */
3394static void
3395handle_client_view_cancel (void *cls,
3396 const struct GNUNET_MessageHeader *msg)
3397{
3398 struct ClientContext *cli_ctx = cls;
3399
3400 (void) msg;
3401
3402 LOG (GNUNET_ERROR_TYPE_DEBUG,
3403 "Client does not want to receive updates of view any more.\n");
3404
3405 GNUNET_assert (NULL != cli_ctx);
3406 cli_ctx->view_updates_left = 0;
3407 GNUNET_SERVICE_client_continue (cli_ctx->client);
3408 if (GNUNET_YES == cli_ctx->stream_update)
3409 {
3410 destroy_cli_ctx (cli_ctx);
3411 }
3412}
3413
3414
3415/**
3416 * Handle RPS request for biased stream from the client.
3417 *
3418 * @param cls Client context
3419 * @param message unused
3420 */
3421static void
3422handle_client_stream_request (void *cls,
3423 const struct
3424 GNUNET_RPS_CS_DEBUG_StreamRequest *msg)
3425{
3426 struct ClientContext *cli_ctx = cls;
3427
3428 (void) msg;
3429
3430 LOG (GNUNET_ERROR_TYPE_DEBUG,
3431 "Client requested peers from biased stream.\n");
3432 cli_ctx->stream_update = GNUNET_YES;
3433
3434 GNUNET_assert (NULL != cli_ctx);
3435 GNUNET_SERVICE_client_continue (cli_ctx->client);
3436}
3437
3438
3439/**
3440 * @brief Handles the cancellation of the stream of biased peer ids
3441 *
3442 * @param cls The client context
3443 * @param msg unused
3444 */
3445static void
3446handle_client_stream_cancel (void *cls,
3447 const struct GNUNET_MessageHeader *msg)
3448{
3449 struct ClientContext *cli_ctx = cls;
3450
3451 (void) msg;
3452
3453 LOG (GNUNET_ERROR_TYPE_DEBUG,
3454 "Client canceled receiving peers from biased stream.\n");
3455 cli_ctx->stream_update = GNUNET_NO;
3456
3457 GNUNET_assert (NULL != cli_ctx);
3458 GNUNET_SERVICE_client_continue (cli_ctx->client);
3459}
3460
3461
3462/**
3463 * @brief Create and start a Sub.
3464 *
3465 * @param cls Closure - unused
3466 * @param msg Message containing the necessary information
3467 */
3468static void
3469handle_client_start_sub (void *cls,
3470 const struct GNUNET_RPS_CS_SubStartMessage *msg)
3471{
3472 struct ClientContext *cli_ctx = cls;
3473
3474 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested start of a new sub.\n");
3475 if ((NULL != cli_ctx->sub) &&
3476 (0 != memcmp (&cli_ctx->sub->hash,
3477 &msg->hash,
3478 sizeof(struct GNUNET_HashCode))) )
3479 {
3480 LOG (GNUNET_ERROR_TYPE_WARNING,
3481 "Already have a Sub with different share for this client. Remove old one, add new.\n");
3482 destroy_sub (cli_ctx->sub);
3483 cli_ctx->sub = NULL;
3484 }
3485 cli_ctx->sub = new_sub (&msg->hash,
3486 msub->sampler_size_est_min, // TODO make api input?
3487 GNUNET_TIME_relative_ntoh (msg->round_interval));
3488 GNUNET_SERVICE_client_continue (cli_ctx->client);
3489}
3490
3491
3492/**
3493 * @brief Destroy the Sub
3494 *
3495 * @param cls Closure - unused
3496 * @param msg Message containing the hash that identifies the Sub
3497 */
3498static void
3499handle_client_stop_sub (void *cls,
3500 const struct GNUNET_RPS_CS_SubStopMessage *msg)
3501{
3502 struct ClientContext *cli_ctx = cls;
3503
3504 GNUNET_assert (NULL != cli_ctx->sub);
3505 if (0 != memcmp (&cli_ctx->sub->hash, &msg->hash, sizeof(struct
3506 GNUNET_HashCode)))
3507 {
3508 LOG (GNUNET_ERROR_TYPE_WARNING,
3509 "Share of current sub and request differ!\n");
3510 }
3511 destroy_sub (cli_ctx->sub);
3512 cli_ctx->sub = NULL;
3513 GNUNET_SERVICE_client_continue (cli_ctx->client);
3514}
3515
3516
3517/**
3518 * Handle a CHECK_LIVE message from another peer.
3519 *
3520 * This does nothing. But without calling #GNUNET_CADET_receive_done()
3521 * the channel is blocked for all other communication.
3522 *
3523 * @param cls Closure - Context of channel
3524 * @param msg Message - unused
3525 */
3526static void
3527handle_peer_check (void *cls,
3528 const struct GNUNET_MessageHeader *msg)
3529{
3530 const struct ChannelCtx *channel_ctx = cls;
3531 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3532
3533 (void) msg;
3534
3535 LOG (GNUNET_ERROR_TYPE_DEBUG,
3536 "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer));
3537 if (channel_ctx->peer_ctx->sub == msub)
3538 {
3539 GNUNET_STATISTICS_update (stats,
3540 "# pending online checks",
3541 -1,
3542 GNUNET_NO);
3543 }
3544
3545 GNUNET_CADET_receive_done (channel_ctx->channel);
3546}
3547
3548
3549/**
3550 * Handle a PUSH message from another peer.
3551 *
3552 * Check the proof of work and store the PeerID
3553 * in the temporary list for pushed PeerIDs.
3554 *
3555 * @param cls Closure - Context of channel
3556 * @param msg Message - unused
3557 */
3558static void
3559handle_peer_push (void *cls,
3560 const struct GNUNET_MessageHeader *msg)
3561{
3562 const struct ChannelCtx *channel_ctx = cls;
3563 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3564
3565 (void) msg;
3566
3567 // (check the proof of work (?))
3568
3569 LOG (GNUNET_ERROR_TYPE_DEBUG,
3570 "Received PUSH (%s)\n",
3571 GNUNET_i2s (peer));
3572 if (channel_ctx->peer_ctx->sub == msub)
3573 {
3574 GNUNET_STATISTICS_update (stats, "# push message received", 1, GNUNET_NO);
3575 if ((NULL != map_single_hop) &&
3576 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3577 peer)))
3578 {
3579 GNUNET_STATISTICS_update (stats,
3580 "# push message received (multi-hop peer)",
3581 1,
3582 GNUNET_NO);
3583 }
3584 }
3585
3586 #if ENABLE_MALICIOUS
3587 struct AttackedPeer *tmp_att_peer;
3588
3589 if ((1 == mal_type) ||
3590 (3 == mal_type))
3591 { /* Try to maximise representation */
3592 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3593 tmp_att_peer->peer_id = *peer;
3594 if (NULL == att_peer_set)
3595 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
3596 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3597 peer))
3598 {
3599 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3600 att_peers_tail,
3601 tmp_att_peer);
3602 add_peer_array_to_set (peer, 1, att_peer_set);
3603 }
3604 else
3605 {
3606 GNUNET_free (tmp_att_peer);
3607 }
3608 }
3609
3610
3611 else if (2 == mal_type)
3612 {
3613 /* We attack one single well-known peer - simply ignore */
3614 }
3615 #endif /* ENABLE_MALICIOUS */
3616
3617 /* Add the sending peer to the push_map */
3618 CustomPeerMap_put (channel_ctx->peer_ctx->sub->push_map, peer);
3619
3620 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map,
3621 &channel_ctx->peer_ctx->peer_id));
3622 GNUNET_CADET_receive_done (channel_ctx->channel);
3623}
3624
3625
3626/**
3627 * Handle PULL REQUEST request message from another peer.
3628 *
3629 * Reply with the view of PeerIDs.
3630 *
3631 * @param cls Closure - Context of channel
3632 * @param msg Message - unused
3633 */
3634static void
3635handle_peer_pull_request (void *cls,
3636 const struct GNUNET_MessageHeader *msg)
3637{
3638 const struct ChannelCtx *channel_ctx = cls;
3639 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
3640 const struct GNUNET_PeerIdentity *peer = &peer_ctx->peer_id;
3641 const struct GNUNET_PeerIdentity *view_array;
3642
3643 (void) msg;
3644
3645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (
3646 peer));
3647 if (peer_ctx->sub == msub)
3648 {
3649 GNUNET_STATISTICS_update (stats,
3650 "# pull request message received",
3651 1,
3652 GNUNET_NO);
3653 if ((NULL != map_single_hop) &&
3654 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3655 &peer_ctx->peer_id)))
3656 {
3657 GNUNET_STATISTICS_update (stats,
3658 "# pull request message received (multi-hop peer)",
3659 1,
3660 GNUNET_NO);
3661 }
3662 }
3663
3664 #if ENABLE_MALICIOUS
3665 if ((1 == mal_type)
3666 || (3 == mal_type))
3667 { /* Try to maximise representation */
3668 send_pull_reply (peer_ctx, mal_peers, num_mal_peers);
3669 }
3670
3671 else if (2 == mal_type)
3672 { /* Try to partition network */
3673 if (0 == GNUNET_memcmp (&attacked_peer, peer))
3674 {
3675 send_pull_reply (peer_ctx, mal_peers, num_mal_peers);
3676 }
3677 }
3678 #endif /* ENABLE_MALICIOUS */
3679
3680 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map,
3681 &channel_ctx->peer_ctx->peer_id));
3682 GNUNET_CADET_receive_done (channel_ctx->channel);
3683 view_array = View_get_as_array (channel_ctx->peer_ctx->sub->view);
3684 send_pull_reply (peer_ctx,
3685 view_array,
3686 View_size (channel_ctx->peer_ctx->sub->view));
3687}
3688
3689
3690/**
3691 * Check whether we sent a corresponding request and
3692 * whether this reply is the first one.
3693 *
3694 * @param cls Closure - Context of channel
3695 * @param msg Message containing the replied peers
3696 */
3697static int
3698check_peer_pull_reply (void *cls,
3699 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3700{
3701 struct ChannelCtx *channel_ctx = cls;
3702 struct PeerContext *sender_ctx = channel_ctx->peer_ctx;
3703
3704 if (sizeof(struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
3705 {
3706 GNUNET_break_op (0);
3707 return GNUNET_SYSERR;
3708 }
3709
3710 if ((ntohs (msg->header.size) - sizeof(struct
3711 GNUNET_RPS_P2P_PullReplyMessage))
3712 / sizeof(struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
3713 {
3714 LOG (GNUNET_ERROR_TYPE_ERROR,
3715 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3716 ntohl (msg->num_peers),
3717 (ntohs (msg->header.size) - sizeof(struct
3718 GNUNET_RPS_P2P_PullReplyMessage))
3719 / sizeof(struct GNUNET_PeerIdentity));
3720 GNUNET_break_op (0);
3721 return GNUNET_SYSERR;
3722 }
3723
3724 if (GNUNET_YES != check_peer_flag (sender_ctx->sub->peer_map,
3725 &sender_ctx->peer_id,
3726 Peers_PULL_REPLY_PENDING))
3727 {
3728 LOG (GNUNET_ERROR_TYPE_WARNING,
3729 "Received a pull reply from a peer (%s) we didn't request one from!\n",
3730 GNUNET_i2s (&sender_ctx->peer_id));
3731 if (sender_ctx->sub == msub)
3732 {
3733 GNUNET_STATISTICS_update (stats,
3734 "# unrequested pull replies",
3735 1,
3736 GNUNET_NO);
3737 }
3738 }
3739 return GNUNET_OK;
3740}
3741
3742
3743/**
3744 * Handle PULL REPLY message from another peer.
3745 *
3746 * @param cls Closure
3747 * @param msg The message header
3748 */
3749static void
3750handle_peer_pull_reply (void *cls,
3751 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3752{
3753 const struct ChannelCtx *channel_ctx = cls;
3754 const struct GNUNET_PeerIdentity *sender = &channel_ctx->peer_ctx->peer_id;
3755 const struct GNUNET_PeerIdentity *peers;
3756 struct Sub *sub = channel_ctx->peer_ctx->sub;
3757 uint32_t i;
3758
3759#if ENABLE_MALICIOUS
3760 struct AttackedPeer *tmp_att_peer;
3761#endif /* ENABLE_MALICIOUS */
3762
3763 sub->pull_delays[sub->num_rounds - channel_ctx->peer_ctx->round_pull_req]++;
3764 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (
3765 sender));
3766 if (channel_ctx->peer_ctx->sub == msub)
3767 {
3768 GNUNET_STATISTICS_update (stats,
3769 "# pull reply messages received",
3770 1,
3771 GNUNET_NO);
3772 if ((NULL != map_single_hop) &&
3773 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3774 &channel_ctx->
3775 peer_ctx->peer_id)) )
3776 {
3777 GNUNET_STATISTICS_update (stats,
3778 "# pull reply messages received (multi-hop peer)",
3779 1,
3780 GNUNET_NO);
3781 }
3782 }
3783
3784 #if ENABLE_MALICIOUS
3785 // We shouldn't even receive pull replies as we're not sending
3786 if (2 == mal_type)
3787 {
3788 }
3789 #endif /* ENABLE_MALICIOUS */
3790
3791 /* Do actual logic */
3792 peers = (const struct GNUNET_PeerIdentity *) &msg[1];
3793
3794 LOG (GNUNET_ERROR_TYPE_DEBUG,
3795 "PULL REPLY received, got following %u peers:\n",
3796 ntohl (msg->num_peers));
3797
3798 for (i = 0; i < ntohl (msg->num_peers); i++)
3799 {
3800 LOG (GNUNET_ERROR_TYPE_DEBUG,
3801 "%u. %s\n",
3802 i,
3803 GNUNET_i2s (&peers[i]));
3804
3805 #if ENABLE_MALICIOUS
3806 if ((NULL != att_peer_set) &&
3807 ((1 == mal_type) || (3 == mal_type) ))
3808 { /* Add attacked peer to local list */
3809 // TODO check if we sent a request and this was the first reply
3810 if ((GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3811 &peers[i]))
3812 && (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
3813 &peers[i])) )
3814 {
3815 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3816 tmp_att_peer->peer_id = peers[i];
3817 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3818 att_peers_tail,
3819 tmp_att_peer);
3820 add_peer_array_to_set (&peers[i], 1, att_peer_set);
3821 }
3822 continue;
3823 }
3824 #endif /* ENABLE_MALICIOUS */
3825 /* Make sure we 'know' about this peer */
3826 (void) insert_peer (channel_ctx->peer_ctx->sub,
3827 &peers[i]);
3828
3829 if (GNUNET_YES == check_peer_valid (channel_ctx->peer_ctx->sub->valid_peers,
3830 &peers[i]))
3831 {
3832 CustomPeerMap_put (channel_ctx->peer_ctx->sub->pull_map,
3833 &peers[i]);
3834 }
3835 else
3836 {
3837 schedule_operation (channel_ctx->peer_ctx,
3838 insert_in_pull_map,
3839 channel_ctx->peer_ctx->sub); /* cls */
3840 (void) issue_peer_online_check (channel_ctx->peer_ctx->sub,
3841 &peers[i]);
3842 }
3843 }
3844
3845 UNSET_PEER_FLAG (get_peer_ctx (channel_ctx->peer_ctx->sub->peer_map,
3846 sender),
3847 Peers_PULL_REPLY_PENDING);
3848 clean_peer (channel_ctx->peer_ctx->sub,
3849 sender);
3850
3851 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map,
3852 sender));
3853 GNUNET_CADET_receive_done (channel_ctx->channel);
3854}
3855
3856
3857/**
3858 * Compute a random delay.
3859 * A uniformly distributed value between mean + spread and mean - spread.
3860 *
3861 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
3862 * It would return a random value between 2 and 6 min.
3863 *
3864 * @param mean the mean time until the next round
3865 * @param spread the inverse amount of deviation from the mean
3866 */
3867static struct GNUNET_TIME_Relative
3868compute_rand_delay (struct GNUNET_TIME_Relative mean,
3869 unsigned int spread)
3870{
3871 struct GNUNET_TIME_Relative half_interval;
3872 struct GNUNET_TIME_Relative ret;
3873 unsigned int rand_delay;
3874 unsigned int max_rand_delay;
3875
3876 if (0 == spread)
3877 {
3878 LOG (GNUNET_ERROR_TYPE_WARNING,
3879 "Not accepting spread of 0\n");
3880 GNUNET_break (0);
3881 GNUNET_assert (0);
3882 }
3883 GNUNET_assert (0 != mean.rel_value_us);
3884
3885 /* Compute random time value between spread * mean and spread * mean */
3886 half_interval = GNUNET_TIME_relative_divide (mean, spread);
3887
3888 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us
3889 / mean.rel_value_us * (2 / spread);
3890 /**
3891 * Compute random value between (0 and 1) * round_interval
3892 * via multiplying round_interval with a 'fraction' (0 to value)/value
3893 */
3894 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3895 max_rand_delay);
3896 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay);
3897 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
3898 ret = GNUNET_TIME_relative_add (ret, half_interval);
3899
3900 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
3901 LOG (GNUNET_ERROR_TYPE_WARNING,
3902 "Returning FOREVER_REL\n");
3903
3904 return ret;
3905}
3906
3907
3908/**
3909 * Send single pull request
3910 *
3911 * @param peer_ctx Context to the peer to send request to
3912 */
3913static void
3914send_pull_request (struct PeerContext *peer_ctx)
3915{
3916 struct GNUNET_MQ_Envelope *ev;
3917
3918 GNUNET_assert (GNUNET_NO == check_peer_flag (peer_ctx->sub->peer_map,
3919 &peer_ctx->peer_id,
3920 Peers_PULL_REPLY_PENDING));
3921 SET_PEER_FLAG (peer_ctx,
3922 Peers_PULL_REPLY_PENDING);
3923 peer_ctx->round_pull_req = peer_ctx->sub->num_rounds;
3924
3925 LOG (GNUNET_ERROR_TYPE_DEBUG,
3926 "Going to send PULL REQUEST to peer %s.\n",
3927 GNUNET_i2s (&peer_ctx->peer_id));
3928
3929 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
3930 send_message (peer_ctx,
3931 ev,
3932 "PULL REQUEST");
3933 if (peer_ctx->sub)
3934 {
3935 GNUNET_STATISTICS_update (stats,
3936 "# pull request send issued",
3937 1,
3938 GNUNET_NO);
3939 if ((NULL != map_single_hop) &&
3940 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3941 &peer_ctx->peer_id)))
3942 {
3943 GNUNET_STATISTICS_update (stats,
3944 "# pull request send issued (multi-hop peer)",
3945 1,
3946 GNUNET_NO);
3947 }
3948 }
3949}
3950
3951
3952/**
3953 * Send single push
3954 *
3955 * @param peer_ctx Context of peer to send push to
3956 */
3957static void
3958send_push (struct PeerContext *peer_ctx)
3959{
3960 struct GNUNET_MQ_Envelope *ev;
3961
3962 LOG (GNUNET_ERROR_TYPE_DEBUG,
3963 "Going to send PUSH to peer %s.\n",
3964 GNUNET_i2s (&peer_ctx->peer_id));
3965
3966 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
3967 send_message (peer_ctx, ev, "PUSH");
3968 if (peer_ctx->sub)
3969 {
3970 GNUNET_STATISTICS_update (stats,
3971 "# push send issued",
3972 1,
3973 GNUNET_NO);
3974 if ((NULL != map_single_hop) &&
3975 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3976 &peer_ctx->peer_id)))
3977 {
3978 GNUNET_STATISTICS_update (stats,
3979 "# push send issued (multi-hop peer)",
3980 1,
3981 GNUNET_NO);
3982 }
3983 }
3984}
3985
3986
3987#if ENABLE_MALICIOUS
3988
3989
3990/**
3991 * @brief This function is called, when the client tells us to act malicious.
3992 * It verifies that @a msg is well-formed.
3993 *
3994 * @param cls the closure (#ClientContext)
3995 * @param msg the message
3996 * @return #GNUNET_OK if @a msg is well-formed
3997 */
3998static int
3999check_client_act_malicious (void *cls,
4000 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
4001{
4002 struct ClientContext *cli_ctx = cls;
4003 uint16_t msize = ntohs (msg->header.size);
4004 uint32_t num_peers = ntohl (msg->num_peers);
4005
4006 msize -= sizeof(struct GNUNET_RPS_CS_ActMaliciousMessage);
4007 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
4008 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
4009 {
4010 LOG (GNUNET_ERROR_TYPE_ERROR,
4011 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
4012 ntohl (msg->num_peers),
4013 (msize / sizeof(struct GNUNET_PeerIdentity)));
4014 GNUNET_break (0);
4015 GNUNET_SERVICE_client_drop (cli_ctx->client);
4016 return GNUNET_SYSERR;
4017 }
4018 return GNUNET_OK;
4019}
4020
4021
4022/**
4023 * Turn RPS service to act malicious.
4024 *
4025 * @param cls Closure
4026 * @param client The client that sent the message
4027 * @param msg The message header
4028 */
4029static void
4030handle_client_act_malicious (void *cls,
4031 const struct
4032 GNUNET_RPS_CS_ActMaliciousMessage *msg)
4033{
4034 struct ClientContext *cli_ctx = cls;
4035 struct GNUNET_PeerIdentity *peers;
4036 uint32_t num_mal_peers_sent;
4037 uint32_t num_mal_peers_old;
4038 struct Sub *sub = cli_ctx->sub;
4039
4040 if (NULL == sub)
4041 sub = msub;
4042 /* Do actual logic */
4043 peers = (struct GNUNET_PeerIdentity *) &msg[1];
4044 mal_type = ntohl (msg->type);
4045 if (NULL == mal_peer_set)
4046 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
4047
4048 LOG (GNUNET_ERROR_TYPE_DEBUG,
4049 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
4050 mal_type,
4051 ntohl (msg->num_peers));
4052
4053 if (1 == mal_type)
4054 { /* Try to maximise representation */
4055 /* Add other malicious peers to those we already know */
4056
4057 num_mal_peers_sent = ntohl (msg->num_peers);
4058 num_mal_peers_old = num_mal_peers;
4059 GNUNET_array_grow (mal_peers,
4060 num_mal_peers,
4061 num_mal_peers + num_mal_peers_sent);
4062 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
4063 peers,
4064 num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity));
4065
4066 /* Add all mal peers to mal_peer_set */
4067 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
4068 num_mal_peers_sent,
4069 mal_peer_set);
4070
4071 /* Substitute do_round () with do_mal_round () */
4072 GNUNET_assert (NULL != sub->do_round_task);
4073 GNUNET_SCHEDULER_cancel (sub->do_round_task);
4074 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub);
4075 }
4076
4077 else if ((2 == mal_type) ||
4078 (3 == mal_type))
4079 { /* Try to partition the network */
4080 /* Add other malicious peers to those we already know */
4081
4082 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
4083 num_mal_peers_old = num_mal_peers;
4084 GNUNET_assert (GNUNET_MAX_MALLOC_CHECKED > num_mal_peers_sent);
4085 GNUNET_array_grow (mal_peers,
4086 num_mal_peers,
4087 num_mal_peers + num_mal_peers_sent);
4088 if ((NULL != mal_peers) &&
4089 (0 != num_mal_peers) )
4090 {
4091 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
4092 peers,
4093 num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity));
4094
4095 /* Add all mal peers to mal_peer_set */
4096 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
4097 num_mal_peers_sent,
4098 mal_peer_set);
4099 }
4100
4101 /* Store the one attacked peer */
4102 GNUNET_memcpy (&attacked_peer,
4103 &msg->attacked_peer,
4104 sizeof(struct GNUNET_PeerIdentity));
4105 /* Set the flag of the attacked peer to valid to avoid problems */
4106 if (GNUNET_NO == check_peer_known (sub->peer_map, &attacked_peer))
4107 {
4108 (void) issue_peer_online_check (sub, &attacked_peer);
4109 }
4110
4111 LOG (GNUNET_ERROR_TYPE_DEBUG,
4112 "Attacked peer is %s\n",
4113 GNUNET_i2s (&attacked_peer));
4114
4115 /* Substitute do_round () with do_mal_round () */
4116 if (NULL != sub->do_round_task)
4117 {
4118 /* Probably in shutdown */
4119 GNUNET_SCHEDULER_cancel (sub->do_round_task);
4120 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub);
4121 }
4122 }
4123 else if (0 == mal_type)
4124 { /* Stop acting malicious */
4125 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
4126
4127 /* Substitute do_mal_round () with do_round () */
4128 GNUNET_SCHEDULER_cancel (sub->do_round_task);
4129 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub);
4130 }
4131 else
4132 {
4133 GNUNET_break (0);
4134 GNUNET_SERVICE_client_continue (cli_ctx->client);
4135 }
4136 GNUNET_SERVICE_client_continue (cli_ctx->client);
4137}
4138
4139
4140/**
4141 * Send out PUSHes and PULLs maliciously.
4142 *
4143 * This is executed regylary.
4144 *
4145 * @param cls Closure - Sub
4146 */
4147static void
4148do_mal_round (void *cls)
4149{
4150 uint32_t num_pushes;
4151 uint32_t i;
4152 struct GNUNET_TIME_Relative time_next_round;
4153 struct AttackedPeer *tmp_att_peer;
4154 struct Sub *sub = cls;
4155
4156 LOG (GNUNET_ERROR_TYPE_DEBUG,
4157 "Going to execute next round maliciously type %" PRIu32 ".\n",
4158 mal_type);
4159 sub->do_round_task = NULL;
4160 GNUNET_assert (mal_type <= 3);
4161 /* Do malicious actions */
4162 if (1 == mal_type)
4163 { /* Try to maximise representation */
4164 /* The maximum of pushes we're going to send this round */
4165 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
4166 num_attacked_peers),
4167 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
4168
4169 LOG (GNUNET_ERROR_TYPE_DEBUG,
4170 "Going to send %" PRIu32 " pushes\n",
4171 num_pushes);
4172
4173 /* Send PUSHes to attacked peers */
4174 for (i = 0; i < num_pushes; i++)
4175 {
4176 if (att_peers_tail == att_peer_index)
4177 att_peer_index = att_peers_head;
4178 else
4179 att_peer_index = att_peer_index->next;
4180
4181 send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id));
4182 }
4183
4184 /* Send PULLs to some peers to learn about additional peers to attack */
4185 tmp_att_peer = att_peer_index;
4186 for (i = 0; i < num_pushes * alpha; i++)
4187 {
4188 if (att_peers_tail == tmp_att_peer)
4189 tmp_att_peer = att_peers_head;
4190 else
4191 att_peer_index = tmp_att_peer->next;
4192
4193 send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id));
4194 }
4195 }
4196
4197
4198 else if (2 == mal_type)
4199 { /**
4200 * Try to partition the network
4201 * Send as many pushes to the attacked peer as possible
4202 * That is one push per round as it will ignore more.
4203 */
4204 (void) issue_peer_online_check (sub, &attacked_peer);
4205 if (GNUNET_YES == check_peer_flag (sub->peer_map,
4206 &attacked_peer,
4207 Peers_ONLINE))
4208 send_push (get_peer_ctx (sub->peer_map, &attacked_peer));
4209 }
4210
4211
4212 if (3 == mal_type)
4213 { /* Combined attack */
4214 /* Send PUSH to attacked peers */
4215 if (GNUNET_YES == check_peer_known (sub->peer_map, &attacked_peer))
4216 {
4217 (void) issue_peer_online_check (sub, &attacked_peer);
4218 if (GNUNET_YES == check_peer_flag (sub->peer_map,
4219 &attacked_peer,
4220 Peers_ONLINE))
4221 {
4222 LOG (GNUNET_ERROR_TYPE_DEBUG,
4223 "Goding to send push to attacked peer (%s)\n",
4224 GNUNET_i2s (&attacked_peer));
4225 send_push (get_peer_ctx (sub->peer_map, &attacked_peer));
4226 }
4227 }
4228 (void) issue_peer_online_check (sub, &attacked_peer);
4229
4230 /* The maximum of pushes we're going to send this round */
4231 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
4232 num_attacked_peers),
4233 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
4234
4235 LOG (GNUNET_ERROR_TYPE_DEBUG,
4236 "Going to send %" PRIu32 " pushes\n",
4237 num_pushes);
4238
4239 for (i = 0; i < num_pushes; i++)
4240 {
4241 if (att_peers_tail == att_peer_index)
4242 att_peer_index = att_peers_head;
4243 else
4244 att_peer_index = att_peer_index->next;
4245
4246 send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id));
4247 }
4248
4249 /* Send PULLs to some peers to learn about additional peers to attack */
4250 tmp_att_peer = att_peer_index;
4251 for (i = 0; i < num_pushes * alpha; i++)
4252 {
4253 if (att_peers_tail == tmp_att_peer)
4254 tmp_att_peer = att_peers_head;
4255 else
4256 att_peer_index = tmp_att_peer->next;
4257
4258 send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id));
4259 }
4260 }
4261
4262 /* Schedule next round */
4263 time_next_round = compute_rand_delay (sub->round_interval, 2);
4264
4265 GNUNET_assert (NULL == sub->do_round_task);
4266 sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
4267 &do_mal_round, sub);
4268 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
4269}
4270
4271
4272#endif /* ENABLE_MALICIOUS */
4273
4274
4275/**
4276 * Send out PUSHes and PULLs, possibly update #view, samplers.
4277 *
4278 * This is executed regylary.
4279 *
4280 * @param cls Closure - Sub
4281 */
4282static void
4283do_round (void *cls)
4284{
4285 unsigned int i;
4286 const struct GNUNET_PeerIdentity *view_array;
4287 unsigned int *permut;
4288 unsigned int a_peers; /* Number of peers we send pushes to */
4289 unsigned int b_peers; /* Number of peers we send pull requests to */
4290 uint32_t first_border;
4291 uint32_t second_border;
4292 struct GNUNET_PeerIdentity peer;
4293 struct GNUNET_PeerIdentity *update_peer;
4294 struct Sub *sub = cls;
4295
4296 sub->num_rounds++;
4297 LOG (GNUNET_ERROR_TYPE_DEBUG,
4298 "Going to execute next round.\n");
4299 if (sub == msub)
4300 {
4301 GNUNET_STATISTICS_update (stats, "# rounds", 1, GNUNET_NO);
4302 }
4303 sub->do_round_task = NULL;
4304#ifdef TO_FILE_FULL
4305 to_file (sub->file_name_view_log,
4306 "___ new round ___");
4307#endif /* TO_FILE_FULL */
4308 view_array = View_get_as_array (sub->view);
4309 for (i = 0; i < View_size (sub->view); i++)
4310 {
4311 LOG (GNUNET_ERROR_TYPE_DEBUG,
4312 "\t%s\n", GNUNET_i2s (&view_array[i]));
4313#ifdef TO_FILE_FULL
4314 to_file (sub->file_name_view_log,
4315 "=%s\t(do round)",
4316 GNUNET_i2s_full (&view_array[i]));
4317#endif /* TO_FILE_FULL */
4318 }
4319
4320
4321 /* Send pushes and pull requests */
4322 if (0 < View_size (sub->view))
4323 {
4324 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
4325 View_size (sub->view));
4326
4327 /* Send PUSHes */
4328 a_peers = ceil (alpha * View_size (sub->view));
4329
4330 LOG (GNUNET_ERROR_TYPE_DEBUG,
4331 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
4332 a_peers, alpha, View_size (sub->view));
4333 for (i = 0; i < a_peers; i++)
4334 {
4335 peer = view_array[permut[i]];
4336 // FIXME if this fails schedule/loop this for later
4337 send_push (get_peer_ctx (sub->peer_map, &peer));
4338 }
4339
4340 /* Send PULL requests */
4341 b_peers = ceil (beta * View_size (sub->view));
4342 first_border = a_peers;
4343 second_border = a_peers + b_peers;
4344 if (second_border > View_size (sub->view))
4345 {
4346 first_border = View_size (sub->view) - b_peers;
4347 second_border = View_size (sub->view);
4348 }
4349 LOG (GNUNET_ERROR_TYPE_DEBUG,
4350 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
4351 b_peers, beta, View_size (sub->view));
4352 for (i = first_border; i < second_border; i++)
4353 {
4354 peer = view_array[permut[i]];
4355 if (GNUNET_NO == check_peer_flag (sub->peer_map,
4356 &peer,
4357 Peers_PULL_REPLY_PENDING))
4358 { // FIXME if this fails schedule/loop this for later
4359 send_pull_request (get_peer_ctx (sub->peer_map, &peer));
4360 }
4361 }
4362
4363 GNUNET_free (permut);
4364 permut = NULL;
4365 }
4366
4367
4368 /* Update view */
4369 /* TODO see how many peers are in push-/pull- list! */
4370
4371 if ((CustomPeerMap_size (sub->push_map) <= alpha * sub->view_size_est_need) &&
4372 (0 < CustomPeerMap_size (sub->push_map)) &&
4373 (0 < CustomPeerMap_size (sub->pull_map)))
4374 { /* If conditions for update are fulfilled, update */
4375 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
4376
4377 uint32_t final_size;
4378 uint32_t peers_to_clean_size;
4379 struct GNUNET_PeerIdentity *peers_to_clean;
4380
4381 peers_to_clean = NULL;
4382 peers_to_clean_size = 0;
4383 GNUNET_array_grow (peers_to_clean,
4384 peers_to_clean_size,
4385 View_size (sub->view));
4386 GNUNET_memcpy (peers_to_clean,
4387 view_array,
4388 View_size (sub->view) * sizeof(struct GNUNET_PeerIdentity));
4389
4390 /* Seems like recreating is the easiest way of emptying the peermap */
4391 View_clear (sub->view);
4392#ifdef TO_FILE_FULL
4393 to_file (sub->file_name_view_log,
4394 "--- emptied ---");
4395#endif /* TO_FILE_FULL */
4396
4397 first_border = GNUNET_MIN (ceil (alpha * sub->view_size_est_need),
4398 CustomPeerMap_size (sub->push_map));
4399 second_border = first_border
4400 + GNUNET_MIN (floor (beta * sub->view_size_est_need),
4401 CustomPeerMap_size (sub->pull_map));
4402 final_size = second_border
4403 + ceil ((1 - (alpha + beta)) * sub->view_size_est_need);
4404 LOG (GNUNET_ERROR_TYPE_DEBUG,
4405 "first border: %" PRIu32 ", second border: %" PRIu32 ", final size: %"
4406 PRIu32 "\n",
4407 first_border,
4408 second_border,
4409 final_size);
4410
4411 /* Update view with peers received through PUSHes */
4412 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
4413 CustomPeerMap_size (sub->push_map));
4414 for (i = 0; i < first_border; i++)
4415 {
4416 int inserted;
4417 inserted = insert_in_view (sub,
4418 CustomPeerMap_get_peer_by_index (sub->push_map,
4419 permut[i]));
4420 if (GNUNET_OK == inserted)
4421 {
4422 clients_notify_stream_peer (sub,
4423 1,
4424 CustomPeerMap_get_peer_by_index (
4425 sub->push_map, permut[i]));
4426 }
4427#ifdef TO_FILE_FULL
4428 to_file (sub->file_name_view_log,
4429 "+%s\t(push list)",
4430 GNUNET_i2s_full (&view_array[i]));
4431#endif /* TO_FILE_FULL */
4432 // TODO change the peer_flags accordingly
4433 }
4434 GNUNET_free (permut);
4435 permut = NULL;
4436
4437 /* Update view with peers received through PULLs */
4438 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
4439 CustomPeerMap_size (sub->pull_map));
4440 for (i = first_border; i < second_border; i++)
4441 {
4442 int inserted;
4443 inserted = insert_in_view (sub,
4444 CustomPeerMap_get_peer_by_index (sub->pull_map,
4445 permut[i
4446 -
4447 first_border
4448 ]));
4449 if (GNUNET_OK == inserted)
4450 {
4451 clients_notify_stream_peer (sub,
4452 1,
4453 CustomPeerMap_get_peer_by_index (
4454 sub->pull_map,
4455 permut[i
4456 - first_border]));
4457 }
4458#ifdef TO_FILE_FULL
4459 to_file (sub->file_name_view_log,
4460 "+%s\t(pull list)",
4461 GNUNET_i2s_full (&view_array[i]));
4462#endif /* TO_FILE_FULL */
4463 // TODO change the peer_flags accordingly
4464 }
4465 GNUNET_free (permut);
4466 permut = NULL;
4467
4468 /* Update view with peers from history */
4469 RPS_sampler_get_n_rand_peers (sub->sampler,
4470 final_size - second_border,
4471 hist_update,
4472 sub);
4473 // TODO change the peer_flags accordingly
4474
4475 for (i = 0; i < View_size (sub->view); i++)
4476 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
4477
4478 /* Clean peers that were removed from the view */
4479 for (i = 0; i < peers_to_clean_size; i++)
4480 {
4481#ifdef TO_FILE_FULL
4482 to_file (sub->file_name_view_log,
4483 "-%s",
4484 GNUNET_i2s_full (&peers_to_clean[i]));
4485#endif /* TO_FILE_FULL */
4486 clean_peer (sub, &peers_to_clean[i]);
4487 }
4488
4489 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
4490 clients_notify_view_update (sub);
4491 }
4492 else
4493 {
4494 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
4495 if (sub == msub)
4496 {
4497 GNUNET_STATISTICS_update (stats, "# rounds blocked", 1, GNUNET_NO);
4498 if ((CustomPeerMap_size (sub->push_map) > alpha
4499 * sub->view_size_est_need) &&
4500 ! (0 >= CustomPeerMap_size (sub->pull_map)))
4501 GNUNET_STATISTICS_update (stats, "# rounds blocked - too many pushes",
4502 1, GNUNET_NO);
4503 if ((CustomPeerMap_size (sub->push_map) > alpha
4504 * sub->view_size_est_need) &&
4505 (0 >= CustomPeerMap_size (sub->pull_map)))
4506 GNUNET_STATISTICS_update (stats,
4507 "# rounds blocked - too many pushes, no pull replies",
4508 1, GNUNET_NO);
4509 if ((0 >= CustomPeerMap_size (sub->push_map)) &&
4510 ! (0 >= CustomPeerMap_size (sub->pull_map)))
4511 GNUNET_STATISTICS_update (stats, "# rounds blocked - no pushes", 1,
4512 GNUNET_NO);
4513 if ((0 >= CustomPeerMap_size (sub->push_map)) &&
4514 (0 >= CustomPeerMap_size (sub->pull_map)))
4515 GNUNET_STATISTICS_update (stats,
4516 "# rounds blocked - no pushes, no pull replies",
4517 1, GNUNET_NO);
4518 if ((0 >= CustomPeerMap_size (sub->pull_map)) &&
4519 (CustomPeerMap_size (sub->push_map) > alpha
4520 * sub->view_size_est_need) &&
4521 (0 >= CustomPeerMap_size (sub->push_map)) )
4522 GNUNET_STATISTICS_update (stats, "# rounds blocked - no pull replies",
4523 1, GNUNET_NO);
4524 }
4525 }
4526 // TODO independent of that also get some peers from CADET_get_peers()?
4527 if (CustomPeerMap_size (sub->push_map) < HISTOGRAM_FILE_SLOTS)
4528 {
4529 sub->push_recv[CustomPeerMap_size (sub->push_map)]++;
4530 }
4531 else
4532 {
4533 LOG (GNUNET_ERROR_TYPE_WARNING,
4534 "Push map size too big for histogram (%u, %u)\n",
4535 CustomPeerMap_size (sub->push_map),
4536 HISTOGRAM_FILE_SLOTS);
4537 }
4538 // FIXME check bounds of histogram
4539 sub->push_delta[(int32_t) (CustomPeerMap_size (sub->push_map)
4540 - (alpha * sub->view_size_est_need))
4541 + (HISTOGRAM_FILE_SLOTS / 2)]++;
4542 if (sub == msub)
4543 {
4544 GNUNET_STATISTICS_set (stats,
4545 "# peers in push map at end of round",
4546 CustomPeerMap_size (sub->push_map),
4547 GNUNET_NO);
4548 GNUNET_STATISTICS_set (stats,
4549 "# peers in pull map at end of round",
4550 CustomPeerMap_size (sub->pull_map),
4551 GNUNET_NO);
4552 GNUNET_STATISTICS_set (stats,
4553 "# peers in view at end of round",
4554 View_size (sub->view),
4555 GNUNET_NO);
4556 GNUNET_STATISTICS_set (stats,
4557 "# expected pushes",
4558 alpha * sub->view_size_est_need,
4559 GNUNET_NO);
4560 GNUNET_STATISTICS_set (stats,
4561 "delta expected - received pushes",
4562 CustomPeerMap_size (sub->push_map) - (alpha
4563 * sub->
4564 view_size_est_need),
4565 GNUNET_NO);
4566 }
4567
4568 LOG (GNUNET_ERROR_TYPE_DEBUG,
4569 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (sub->view%u) = %.2f)\n",
4570 CustomPeerMap_size (sub->push_map),
4571 CustomPeerMap_size (sub->pull_map),
4572 alpha,
4573 View_size (sub->view),
4574 alpha * View_size (sub->view));
4575
4576 /* Update samplers */
4577 for (i = 0; i < CustomPeerMap_size (sub->push_map); i++)
4578 {
4579 update_peer = CustomPeerMap_get_peer_by_index (sub->push_map, i);
4580 LOG (GNUNET_ERROR_TYPE_DEBUG,
4581 "Updating with peer %s from push list\n",
4582 GNUNET_i2s (update_peer));
4583 insert_in_sampler (sub, update_peer);
4584 clean_peer (sub, update_peer); /* This cleans only if it is not in the view */
4585 }
4586
4587 for (i = 0; i < CustomPeerMap_size (sub->pull_map); i++)
4588 {
4589 LOG (GNUNET_ERROR_TYPE_DEBUG,
4590 "Updating with peer %s from pull list\n",
4591 GNUNET_i2s (CustomPeerMap_get_peer_by_index (sub->pull_map, i)));
4592 insert_in_sampler (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i));
4593 /* This cleans only if it is not in the view */
4594 clean_peer (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i));
4595 }
4596
4597
4598 /* Empty push/pull lists */
4599 CustomPeerMap_clear (sub->push_map);
4600 CustomPeerMap_clear (sub->pull_map);
4601
4602 if (sub == msub)
4603 {
4604 GNUNET_STATISTICS_set (stats,
4605 "view size",
4606 View_size (sub->view),
4607 GNUNET_NO);
4608 }
4609
4610 struct GNUNET_TIME_Relative time_next_round;
4611
4612 time_next_round = compute_rand_delay (sub->round_interval, 2);
4613
4614 /* Schedule next round */
4615 sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
4616 &do_round, sub);
4617 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
4618}
4619
4620
4621/**
4622 * This is called from GNUNET_CADET_get_peers().
4623 *
4624 * It is called on every peer(ID) that cadet somehow has contact with.
4625 * We use those to initialise the sampler.
4626 *
4627 * implements #GNUNET_CADET_PeersCB
4628 *
4629 * @param cls Closure - Sub
4630 * @param peer Peer, or NULL on "EOF".
4631 * @param tunnel Do we have a tunnel towards this peer?
4632 * @param n_paths Number of known paths towards this peer.
4633 * @param best_path How long is the best path?
4634 * (0 = unknown, 1 = ourselves, 2 = neighbor)
4635 */
4636void
4637init_peer_cb (void *cls,
4638 const struct GNUNET_PeerIdentity *peer,
4639 int tunnel, /* "Do we have a tunnel towards this peer?" */
4640 unsigned int n_paths, /* "Number of known paths towards this peer" */
4641 unsigned int best_path) /* "How long is the best path?
4642 * (0 = unknown, 1 = ourselves, 2 = neighbor)" */
4643{
4644 struct Sub *sub = cls;
4645
4646 (void) tunnel;
4647 (void) n_paths;
4648 (void) best_path;
4649
4650 if (NULL != peer)
4651 {
4652 LOG (GNUNET_ERROR_TYPE_DEBUG,
4653 "Got peer_id %s from cadet\n",
4654 GNUNET_i2s (peer));
4655 got_peer (sub, peer);
4656 }
4657}
4658
4659
4660/**
4661 * @brief Iterator function over stored, valid peers.
4662 *
4663 * We initialise the sampler with those.
4664 *
4665 * @param cls Closure - Sub
4666 * @param peer the peer id
4667 * @return #GNUNET_YES if we should continue to
4668 * iterate,
4669 * #GNUNET_NO if not.
4670 */
4671static int
4672valid_peers_iterator (void *cls,
4673 const struct GNUNET_PeerIdentity *peer)
4674{
4675 struct Sub *sub = cls;
4676
4677 if (NULL != peer)
4678 {
4679 LOG (GNUNET_ERROR_TYPE_DEBUG,
4680 "Got stored, valid peer %s\n",
4681 GNUNET_i2s (peer));
4682 got_peer (sub, peer);
4683 }
4684 return GNUNET_YES;
4685}
4686
4687
4688/**
4689 * Iterator over peers from peerinfo.
4690 *
4691 * @param cls Closure - Sub
4692 * @param peer id of the peer, NULL for last call
4693 * @param hello hello message for the peer (can be NULL)
4694 * @param error message
4695 */
4696void
4697process_peerinfo_peers (void *cls,
4698 const struct GNUNET_PeerIdentity *peer,
4699 const struct GNUNET_HELLO_Message *hello,
4700 const char *err_msg)
4701{
4702 struct Sub *sub = cls;
4703
4704 (void) hello;
4705 (void) err_msg;
4706
4707 if (NULL != peer)
4708 {
4709 LOG (GNUNET_ERROR_TYPE_DEBUG,
4710 "Got peer_id %s from peerinfo\n",
4711 GNUNET_i2s (peer));
4712 got_peer (sub, peer);
4713 }
4714}
4715
4716
4717/**
4718 * Task run during shutdown.
4719 *
4720 * @param cls Closure - unused
4721 */
4722static void
4723shutdown_task (void *cls)
4724{
4725 (void) cls;
4726 struct ClientContext *client_ctx;
4727
4728 LOG (GNUNET_ERROR_TYPE_DEBUG,
4729 "RPS service is going down\n");
4730
4731 /* Clean all clients */
4732 for (client_ctx = cli_ctx_head;
4733 NULL != cli_ctx_head;
4734 client_ctx = cli_ctx_head)
4735 {
4736 destroy_cli_ctx (client_ctx);
4737 }
4738 if (NULL != msub)
4739 {
4740 destroy_sub (msub);
4741 msub = NULL;
4742 }
4743
4744 /* Disconnect from other services */
4745 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
4746 GNUNET_PEERINFO_disconnect (peerinfo_handle);
4747 peerinfo_handle = NULL;
4748 GNUNET_NSE_disconnect (nse);
4749 if (NULL != map_single_hop)
4750 {
4751 /* core_init was called - core was initialised */
4752 /* disconnect first, so no callback tries to access missing peermap */
4753 GNUNET_CORE_disconnect (core_handle);
4754 core_handle = NULL;
4755 GNUNET_CONTAINER_multipeermap_destroy (map_single_hop);
4756 map_single_hop = NULL;
4757 }
4758
4759 if (NULL != stats)
4760 {
4761 GNUNET_STATISTICS_destroy (stats,
4762 GNUNET_NO);
4763 stats = NULL;
4764 }
4765 GNUNET_CADET_disconnect (cadet_handle);
4766 cadet_handle = NULL;
4767#if ENABLE_MALICIOUS
4768 struct AttackedPeer *tmp_att_peer;
4769 GNUNET_array_grow (mal_peers,
4770 num_mal_peers,
4771 0);
4772 if (NULL != mal_peer_set)
4773 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
4774 if (NULL != att_peer_set)
4775 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
4776 while (NULL != att_peers_head)
4777 {
4778 tmp_att_peer = att_peers_head;
4779 GNUNET_CONTAINER_DLL_remove (att_peers_head,
4780 att_peers_tail,
4781 tmp_att_peer);
4782 GNUNET_free (tmp_att_peer);
4783 }
4784#endif /* ENABLE_MALICIOUS */
4785 close_all_files ();
4786}
4787
4788
4789/**
4790 * Handle client connecting to the service.
4791 *
4792 * @param cls unused
4793 * @param client the new client
4794 * @param mq the message queue of @a client
4795 * @return @a client
4796 */
4797static void *
4798client_connect_cb (void *cls,
4799 struct GNUNET_SERVICE_Client *client,
4800 struct GNUNET_MQ_Handle *mq)
4801{
4802 struct ClientContext *cli_ctx;
4803
4804 (void) cls;
4805
4806 LOG (GNUNET_ERROR_TYPE_DEBUG,
4807 "Client connected\n");
4808 if (NULL == client)
4809 return client; /* Server was destroyed before a client connected. Shutting down */
4810 cli_ctx = GNUNET_new (struct ClientContext);
4811 cli_ctx->mq = mq;
4812 cli_ctx->view_updates_left = -1;
4813 cli_ctx->stream_update = GNUNET_NO;
4814 cli_ctx->client = client;
4815 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
4816 cli_ctx_tail,
4817 cli_ctx);
4818 return cli_ctx;
4819}
4820
4821
4822/**
4823 * Callback called when a client disconnected from the service
4824 *
4825 * @param cls closure for the service
4826 * @param c the client that disconnected
4827 * @param internal_cls should be equal to @a c
4828 */
4829static void
4830client_disconnect_cb (void *cls,
4831 struct GNUNET_SERVICE_Client *client,
4832 void *internal_cls)
4833{
4834 struct ClientContext *cli_ctx = internal_cls;
4835
4836 (void) cls;
4837 GNUNET_assert (client == cli_ctx->client);
4838 if (NULL == client)
4839 { /* shutdown task - destroy all clients */
4840 while (NULL != cli_ctx_head)
4841 destroy_cli_ctx (cli_ctx_head);
4842 }
4843 else
4844 { /* destroy this client */
4845 LOG (GNUNET_ERROR_TYPE_DEBUG,
4846 "Client disconnected. Destroy its context.\n");
4847 destroy_cli_ctx (cli_ctx);
4848 }
4849}
4850
4851
4852/**
4853 * Handle random peer sampling clients.
4854 *
4855 * @param cls closure
4856 * @param c configuration to use
4857 * @param service the initialized service
4858 */
4859static void
4860run (void *cls,
4861 const struct GNUNET_CONFIGURATION_Handle *c,
4862 struct GNUNET_SERVICE_Handle *service)
4863{
4864 struct GNUNET_TIME_Relative round_interval;
4865 long long unsigned int sampler_size;
4866 char hash_port_string[] = GNUNET_APPLICATION_PORT_RPS;
4867 struct GNUNET_HashCode hash;
4868
4869 (void) cls;
4870 (void) service;
4871
4872 GNUNET_log_setup ("rps",
4873 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG),
4874 NULL);
4875 cfg = c;
4876 /* Get own ID */
4877 GNUNET_CRYPTO_get_peer_identity (cfg,
4878 &own_identity); // TODO check return value
4879 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4880 "STARTING SERVICE (rps) for peer [%s]\n",
4881 GNUNET_i2s (&own_identity));
4882#if ENABLE_MALICIOUS
4883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4884 "Malicious execution compiled in.\n");
4885#endif /* ENABLE_MALICIOUS */
4886
4887 /* Get time interval from the configuration */
4888 if (GNUNET_OK !=
4889 GNUNET_CONFIGURATION_get_value_time (cfg,
4890 "RPS",
4891 "ROUNDINTERVAL",
4892 &round_interval))
4893 {
4894 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4895 "RPS", "ROUNDINTERVAL");
4896 GNUNET_SCHEDULER_shutdown ();
4897 return;
4898 }
4899
4900 /* Get initial size of sampler/view from the configuration */
4901 if (GNUNET_OK !=
4902 GNUNET_CONFIGURATION_get_value_number (cfg,
4903 "RPS",
4904 "MINSIZE",
4905 &sampler_size))
4906 {
4907 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4908 "RPS", "MINSIZE");
4909 GNUNET_SCHEDULER_shutdown ();
4910 return;
4911 }
4912
4913 cadet_handle = GNUNET_CADET_connect (cfg);
4914 GNUNET_assert (NULL != cadet_handle);
4915 core_handle = GNUNET_CORE_connect (cfg,
4916 NULL, /* cls */
4917 core_init, /* init */
4918 core_connects, /* connects */
4919 core_disconnects, /* disconnects */
4920 NULL); /* handlers */
4921 GNUNET_assert (NULL != core_handle);
4922
4923
4924 alpha = 0.45;
4925 beta = 0.45;
4926
4927
4928 /* Set up main Sub */
4929 GNUNET_CRYPTO_hash (hash_port_string,
4930 strlen (hash_port_string),
4931 &hash);
4932 msub = new_sub (&hash,
4933 sampler_size, /* Will be overwritten by config */
4934 round_interval);
4935
4936
4937 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
4938
4939 /* connect to NSE */
4940 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
4941
4942 // LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
4943 // GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, msub);
4944 // TODO send push/pull to each of those peers?
4945 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
4946 restore_valid_peers (msub);
4947 get_valid_peers (msub->valid_peers, valid_peers_iterator, msub);
4948
4949 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
4950 GNUNET_NO,
4951 process_peerinfo_peers,
4952 msub);
4953
4954 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
4955
4956 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
4957 stats = GNUNET_STATISTICS_create ("rps", cfg);
4958}
4959
4960
4961/**
4962 * Define "main" method using service macro.
4963 */
4964GNUNET_SERVICE_MAIN
4965 ("rps",
4966 GNUNET_SERVICE_OPTION_NONE,
4967 &run,
4968 &client_connect_cb,
4969 &client_disconnect_cb,
4970 NULL,
4971 GNUNET_MQ_hd_var_size (client_seed,
4972 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
4973 struct GNUNET_RPS_CS_SeedMessage,
4974 NULL),
4975#if ENABLE_MALICIOUS
4976 GNUNET_MQ_hd_var_size (client_act_malicious,
4977 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
4978 struct GNUNET_RPS_CS_ActMaliciousMessage,
4979 NULL),
4980#endif /* ENABLE_MALICIOUS */
4981 GNUNET_MQ_hd_fixed_size (client_view_request,
4982 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST,
4983 struct GNUNET_RPS_CS_DEBUG_ViewRequest,
4984 NULL),
4985 GNUNET_MQ_hd_fixed_size (client_view_cancel,
4986 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL,
4987 struct GNUNET_MessageHeader,
4988 NULL),
4989 GNUNET_MQ_hd_fixed_size (client_stream_request,
4990 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST,
4991 struct GNUNET_RPS_CS_DEBUG_StreamRequest,
4992 NULL),
4993 GNUNET_MQ_hd_fixed_size (client_stream_cancel,
4994 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL,
4995 struct GNUNET_MessageHeader,
4996 NULL),
4997 GNUNET_MQ_hd_fixed_size (client_start_sub,
4998 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START,
4999 struct GNUNET_RPS_CS_SubStartMessage,
5000 NULL),
5001 GNUNET_MQ_hd_fixed_size (client_stop_sub,
5002 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP,
5003 struct GNUNET_RPS_CS_SubStopMessage,
5004 NULL),
5005 GNUNET_MQ_handler_end ());
5006
5007/* end of gnunet-service-rps.c */
diff --git a/src/rps/gnunet-service-rps_custommap.c b/src/rps/gnunet-service-rps_custommap.c
deleted file mode 100644
index b1e73be17..000000000
--- a/src/rps/gnunet-service-rps_custommap.c
+++ /dev/null
@@ -1,356 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-service-rps_custommap.c
23 * @brief utilities for managing (information about) peers
24 * @author Julius Bünger
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet-service-rps_custommap.h"
29#include <inttypes.h>
30
31#define LOG(kind, ...) GNUNET_log_from (kind, "rps-peers", __VA_ARGS__)
32
33
34/**
35 * Peer map to store peers with specialised use-cases (push_list, pull_list,
36 * view, ...)
37 *
38 * It is aimed for use as unordered list-like structures that can be indexed.
39 * Main use-case:
40 *
41 * permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
42 * CustomPeerMap_size (peer_map));
43 * for (i = 0; i < some_border; i++)
44 * some_array[i] = *CustomPeerMap_get_peer_by_index (peer_map, permut[i]);
45 * for (i = some_border; i < CustomPeerMap_size (peer_map); i++)
46 * other_array[i-some_border] =
47 * *CustomPeerMap_get_peer_by_index (peer_map, permut[i]);
48 *
49 * This list is expected to
50 * - be altered in small steps frequently
51 * - be cleared regularly
52 * - often being queried whether a peer is contained
53 * - alter indices of peers
54 * - contain continuous indices 0 <= i < len
55 * - not contain duplicate peers
56 */
57struct CustomPeerMap
58{
59 /**
60 * Multihashmap to be able to access a random index
61 */
62 struct GNUNET_CONTAINER_MultiHashMap32 *hash_map;
63
64 /**
65 * Peermap to quickly check whether a peer is contained
66 */
67 struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
68};
69
70
71/**
72 * Create an empty peermap.
73 *
74 * @param len the initial length for the internal maps
75 *
76 * @return the newly created custom peer map
77 */
78struct CustomPeerMap *
79CustomPeerMap_create (unsigned int len)
80{
81 struct CustomPeerMap *c_peer_map;
82
83 c_peer_map = GNUNET_new (struct CustomPeerMap);
84 c_peer_map->hash_map = GNUNET_CONTAINER_multihashmap32_create (len);
85 c_peer_map->peer_map = GNUNET_CONTAINER_multipeermap_create (len,
86 GNUNET_NO);
87 return c_peer_map;
88}
89
90
91/**
92 * Get the size of the custom peer map
93 *
94 * @param c_peer_map the custom peer map to look in
95 *
96 * @return size of the map
97 */
98unsigned int
99CustomPeerMap_size (const struct CustomPeerMap *c_peer_map)
100{
101 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
102 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
103 return GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map);
104}
105
106
107/**
108 * Insert peer into the custom peer map
109 *
110 * @param c_peer_map the custom peer map to insert peer
111 * @param peer the peer to insert
112 *
113 * @return GNUNET_OK if map did not contain peer previously
114 * GNUNET_NO if map did contain peer previously
115 */
116int
117CustomPeerMap_put (const struct CustomPeerMap *c_peer_map,
118 const struct GNUNET_PeerIdentity *peer)
119{
120 uint32_t *index;
121 struct GNUNET_PeerIdentity *p;
122
123 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
124 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
125 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map,
126 peer))
127 {
128 /* Need to store the index of the peer in the peermap to be able to remove
129 * it properly */
130 index = GNUNET_new (uint32_t);
131 *index = CustomPeerMap_size (c_peer_map);
132 p = GNUNET_new (struct GNUNET_PeerIdentity);
133 *p = *peer;
134 GNUNET_assert (p != peer);
135 GNUNET_assert (0 == memcmp (p,
136 peer,
137 sizeof(struct GNUNET_PeerIdentity)));
138 GNUNET_CONTAINER_multipeermap_put (c_peer_map->peer_map,
139 p,
140 index,
141 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
142 GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map,
143 *index,
144 p,
145 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
146 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (
147 c_peer_map->hash_map) ==
148 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
149 return GNUNET_OK;
150 }
151 return GNUNET_NO;
152}
153
154
155/**
156 * Check whether custom peer map contains a peer
157 *
158 * @param c_peer_map the custom peer map to look in
159 * @param peer the peer to check for
160 *
161 * @return GNUNET_OK if map contains peer
162 * GNUNET_NO otherwise
163 */
164int
165CustomPeerMap_contains_peer (const struct CustomPeerMap *c_peer_map,
166 const struct GNUNET_PeerIdentity *peer)
167{
168 return GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map, peer);
169}
170
171
172/**
173 * Get index of peer in custom peer map
174 *
175 * @param c_peer_map the custom peer map to look in
176 * @param peer the peer to get the index from
177 *
178 * @return the index
179 */
180static uint32_t *
181CustomPeerMap_get_index_pointer (const struct CustomPeerMap *c_peer_map,
182 const struct GNUNET_PeerIdentity *peer)
183{
184 uint32_t *index;
185
186 GNUNET_assert (GNUNET_YES == CustomPeerMap_contains_peer (c_peer_map, peer));
187 index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, peer);
188 return index;
189}
190
191
192/**
193 * Remove peer from custom peer map
194 *
195 * @param c_peer_map the custom peer map to remove the peer from
196 * @param peer the peer to remove
197 *
198 * @return GNUNET_OK if map contained peer and removed it successfully
199 * GNUNET_NO if map does not contain peer
200 */
201int
202CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map,
203 const struct GNUNET_PeerIdentity *peer)
204{
205 uint32_t *index;
206 struct GNUNET_PeerIdentity *p;
207 uint32_t *last_index;
208 struct GNUNET_PeerIdentity *last_p;
209 int ret;
210
211 if (GNUNET_NO == CustomPeerMap_contains_peer (c_peer_map,
212 peer))
213 {
214 return GNUNET_NO;
215 }
216 index = CustomPeerMap_get_index_pointer (c_peer_map,
217 peer);
218 GNUNET_assert (*index < CustomPeerMap_size (c_peer_map));
219 /* Need to get the pointer stored in the hashmap to free it */
220 p = GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map,
221 *index);
222 GNUNET_assert (NULL != p);
223 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map,
224 *index);
225 // TODO wrong peerid?
226 GNUNET_CONTAINER_multipeermap_remove_all (c_peer_map->peer_map,
227 peer);
228 if (*index != CustomPeerMap_size (c_peer_map))
229 { /* fill 'gap' with peer at last index */
230 last_p =
231 GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map,
232 CustomPeerMap_size (c_peer_map));
233 GNUNET_assert (NULL != last_p);
234 last_index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map,
235 last_p);
236 GNUNET_assert (NULL != last_index);
237 GNUNET_assert (CustomPeerMap_size (c_peer_map) == *last_index);
238 ret = GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map,
239 *index,
240 last_p,
241 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
242 GNUNET_assert (GNUNET_OK == ret);
243 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map,
244 *last_index);
245 *last_index = *index;
246 }
247 GNUNET_free (index);
248 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
249 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
250 GNUNET_free (p);
251 return GNUNET_OK;
252}
253
254
255/**
256 * Get a peer by index
257 *
258 * @param c_peer_map the custom peer map to look in
259 * @param index the index of the peer to get
260 *
261 * @return peer to the corresponding index.
262 * if this index is not known, return NULL
263 */
264struct GNUNET_PeerIdentity *
265CustomPeerMap_get_peer_by_index (const struct CustomPeerMap *c_peer_map,
266 uint32_t index)
267{
268 if (GNUNET_YES ==
269 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index))
270 {
271 return GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, index);
272 }
273 return NULL;
274}
275
276
277/**
278 * Remove peer from custom peer map by index
279 *
280 * @param c_peer_map the custom peer map to remove the peer from
281 * @param index the index of the peer to remove
282 *
283 * @return GNUNET_OK if map contained peer and removed it successfully
284 * GNUNET_NO if map does not contain (index of) peer
285 */
286int
287CustomPeerMap_remove_peer_by_index (const struct CustomPeerMap *c_peer_map,
288 uint32_t index)
289{
290 uint32_t *index_p;
291 struct GNUNET_PeerIdentity *peer;
292
293 if (index >= CustomPeerMap_size (c_peer_map))
294 {
295 return GNUNET_NO;
296 }
297 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
298 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
299 if (GNUNET_NO ==
300 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index))
301 {
302 return GNUNET_NO;
303 }
304 peer = CustomPeerMap_get_peer_by_index (c_peer_map, index);
305 GNUNET_assert (NULL != peer);
306 index_p = CustomPeerMap_get_index_pointer (c_peer_map, peer);
307 GNUNET_assert (index == *index_p);
308 CustomPeerMap_remove_peer (c_peer_map, peer);
309 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
310 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
311 return GNUNET_OK;
312}
313
314
315/**
316 * Clear the custom peer map
317 *
318 * @param c_peer_map the custom peer map to look in
319 *
320 * @return size of the map
321 */
322void
323CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map)
324{
325 while (0 < CustomPeerMap_size (c_peer_map))
326 {
327 GNUNET_assert (GNUNET_YES ==
328 GNUNET_CONTAINER_multihashmap32_contains (
329 c_peer_map->hash_map,
330 CustomPeerMap_size (
331 c_peer_map) - 1));
332 GNUNET_assert (GNUNET_OK ==
333 CustomPeerMap_remove_peer_by_index (c_peer_map,
334 CustomPeerMap_size (
335 c_peer_map) - 1));
336 }
337 GNUNET_assert (0 == CustomPeerMap_size (c_peer_map));
338}
339
340
341/**
342 * Destroy peermap.
343 *
344 * @param c_peer_map the map to destroy
345 */
346void
347CustomPeerMap_destroy (struct CustomPeerMap *c_peer_map)
348{
349 CustomPeerMap_clear (c_peer_map);
350 GNUNET_CONTAINER_multihashmap32_destroy (c_peer_map->hash_map);
351 GNUNET_CONTAINER_multipeermap_destroy (c_peer_map->peer_map);
352 GNUNET_free (c_peer_map);
353}
354
355
356/* end of gnunet-service-rps_custommap.c */
diff --git a/src/rps/gnunet-service-rps_custommap.h b/src/rps/gnunet-service-rps_custommap.h
deleted file mode 100644
index e7f669c63..000000000
--- a/src/rps/gnunet-service-rps_custommap.h
+++ /dev/null
@@ -1,159 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-service-rps_custommap.h
23 * @brief utilities for managing (information about) peers
24 * @author Julius Bünger
25 */
26#include "gnunet_util_lib.h"
27#include <inttypes.h>
28
29
30/**
31 * Peer map to store peers with specialised use-cases (push_list, pull_list,
32 * view, ...)
33 *
34 * It is aimed for use as unordered list-like structures that can be indexed.
35 * Main use-case:
36 *
37 * permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
38 * CustomPeerMap_size (peer_map));
39 * for (i = 0; i < some_border; i++)
40 * some_array[i] = *CustomPeerMap_get_peer_by_index (peer_map, permut[i]);
41 * for (i = some_border; i < CustomPeerMap_size (peer_map); i++)
42 * other_array[i-some_border] =
43 * *CustomPeerMap_get_peer_by_index (peer_map, permut[i]);
44 *
45 * This list is expected to
46 * - be altered in small steps frequently
47 * - be cleared regularly
48 * - often being queried whether a peer is contained
49 * - alter indices of peers
50 * - contain continuous indices 0 <= i < len
51 * - not contain duplicate peers
52 */
53struct CustomPeerMap;
54
55
56/**
57 * Create an empty peermap.
58 *
59 * @param len the initial length for the internal maps
60 *
61 * @return the newly created custom peer map
62 */
63struct CustomPeerMap *
64CustomPeerMap_create (unsigned int len);
65
66/**
67 * Get the size of the custom peer map
68 *
69 * @param c_peer_map the custom peer map to look in
70 *
71 * @return size of the map
72 */
73unsigned int
74CustomPeerMap_size (const struct CustomPeerMap *c_peer_map);
75
76/**
77 * Insert peer into the custom peer map
78 *
79 * @param c_peer_map the custom peer map to insert peer
80 * @param peer the peer to insert
81 *
82 * @return GNUNET_OK if map did not contain peer previously
83 * GNUNET_NO if map did contain peer previously
84 */
85int
86CustomPeerMap_put (const struct CustomPeerMap *c_peer_map,
87 const struct GNUNET_PeerIdentity *peer);
88
89/**
90 * Check whether custom peer map contains a peer
91 *
92 * @param c_peer_map the custom peer map to look in
93 * @param peer the peer to check for
94 *
95 * @return GNUNET_OK if map contains peer
96 * GNUNET_NO otherwise
97 */
98int
99CustomPeerMap_contains_peer (const struct CustomPeerMap *c_peer_map,
100 const struct GNUNET_PeerIdentity *peer);
101
102/**
103 * Remove peer from custom peer map
104 *
105 * @param c_peer_map the custom peer map to remove the peer from
106 * @param peer the peer to remove
107 *
108 * @return GNUNET_OK if map contained peer and removed it successfully
109 * GNUNET_NO if map does not contain peer
110 */
111int
112CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map,
113 const struct GNUNET_PeerIdentity *peer);
114
115/**
116 * Get a peer by index
117 *
118 * @param c_peer_map the custom peer map to look in
119 * @param index the index of the peer to get
120 *
121 * @return peer to the corresponding index.
122 * if this index is not known, return NULL
123 */
124struct GNUNET_PeerIdentity *
125CustomPeerMap_get_peer_by_index (const struct CustomPeerMap *c_peer_map,
126 uint32_t index);
127
128/**
129 * Remove peer from custom peer map by index
130 *
131 * @param c_peer_map the custom peer map to remove the peer from
132 * @param index the index of the peer to remove
133 *
134 * @return GNUNET_OK if map contained peer and removed it successfully
135 * GNUNET_NO if map does not contain (index of) peer
136 */
137int
138CustomPeerMap_remove_peer_by_index (const struct CustomPeerMap *c_peer_map,
139 uint32_t index);
140
141/**
142 * Clear the custom peer map
143 *
144 * @param c_peer_map the custom peer map to look in
145 *
146 * @return size of the map
147 */
148void
149CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map);
150
151/**
152 * Destroy peermap.
153 *
154 * @param c_peer_map the map to destroy
155 */
156void
157CustomPeerMap_destroy (struct CustomPeerMap *c_peer_map);
158
159/* end of gnunet-service-rps_custommap.h */
diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c
deleted file mode 100644
index 598cc887e..000000000
--- a/src/rps/gnunet-service-rps_sampler.c
+++ /dev/null
@@ -1,268 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
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#include "gnunet_statistics_service.h"
29#include "rps.h"
30
31#include "rps-sampler_common.h"
32#include "gnunet-service-rps_sampler.h"
33#include "gnunet-service-rps_sampler_elem.h"
34
35#include <math.h>
36#include <inttypes.h>
37
38#include "rps-test_util.h"
39
40#define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler", __VA_ARGS__)
41
42
43// multiple 'clients'?
44
45// TODO check for overflows
46
47// TODO align message structs
48
49// hist_size_init, hist_size_max
50
51/***********************************************************************
52* WARNING: This section needs to be reviewed regarding the use of
53* functions providing (pseudo)randomness!
54***********************************************************************/
55
56// TODO care about invalid input of the caller (size 0 or less...)
57
58/**
59 * @brief Callback called each time a new peer was put into the sampler
60 *
61 * @param cls A possibly given closure
62 */
63typedef void
64(*SamplerNotifyUpdateCB) (void *cls);
65
66/**
67 * @brief Context for a callback. Contains callback and closure.
68 *
69 * Meant to be an entry in an DLL.
70 */
71struct SamplerNotifyUpdateCTX
72{
73 /**
74 * @brief The Callback to call on updates
75 */
76 SamplerNotifyUpdateCB notify_cb;
77
78 /**
79 * @brief The according closure.
80 */
81 void *cls;
82
83 /**
84 * @brief Next element in DLL.
85 */
86 struct SamplerNotifyUpdateCTX *next;
87
88 /**
89 * @brief Previous element in DLL.
90 */
91 struct SamplerNotifyUpdateCTX *prev;
92};
93
94
95/**
96 * Type of function used to differentiate between modified and not modified
97 * Sampler.
98 */
99typedef void
100(*RPS_get_peers_type) (void *cls);
101
102/**
103 * Get one random peer out of the sampled peers.
104 *
105 * We might want to reinitialise this sampler after giving the
106 * corrsponding peer to the client.
107 * Only used internally
108 */
109static void
110sampler_get_rand_peer (void *cls);
111
112
113/**
114 * Closure to _get_n_rand_peers_ready_cb()
115 */
116struct RPS_SamplerRequestHandle
117{
118 /**
119 * DLL
120 */
121 struct RPS_SamplerRequestHandle *next;
122 struct RPS_SamplerRequestHandle *prev;
123
124 /**
125 * Number of peers we are waiting for.
126 */
127 uint32_t num_peers;
128
129 /**
130 * Number of peers we currently have.
131 */
132 uint32_t cur_num_peers;
133
134 /**
135 * Pointer to the array holding the ids.
136 */
137 struct GNUNET_PeerIdentity *ids;
138
139 /**
140 * Head and tail for the DLL to store the tasks for single requests
141 */
142 struct GetPeerCls *gpc_head;
143 struct GetPeerCls *gpc_tail;
144
145 /**
146 * Sampler.
147 */
148 struct RPS_Sampler *sampler;
149
150 /**
151 * Callback to be called when all ids are available.
152 */
153 RPS_sampler_n_rand_peers_ready_cb callback;
154
155 /**
156 * Closure given to the callback
157 */
158 void *cls;
159};
160
161///**
162// * Global sampler variable.
163// */
164// struct RPS_Sampler *sampler;
165
166
167/**
168 * The minimal size for the extended sampler elements.
169 */
170static size_t min_size;
171
172/**
173 * The maximal size the extended sampler elements should grow to.
174 */
175static size_t max_size;
176
177/**
178 * The size the extended sampler elements currently have.
179 */
180// static size_t extra_size;
181
182/**
183 * Inedex to the sampler element that is the next to be returned
184 */
185static uint32_t client_get_index;
186
187
188/**
189 * Initialise a tuple of sampler elements.
190 *
191 * @param init_size the size the sampler is initialised with
192 * @param max_round_interval maximum time a round takes
193 * @return a handle to a sampler that consists of sampler elements.
194 */
195struct RPS_Sampler *
196RPS_sampler_init (size_t init_size,
197 struct GNUNET_TIME_Relative max_round_interval)
198{
199 struct RPS_Sampler *sampler;
200
201 /* Initialise context around extended sampler */
202 min_size = 10; // TODO make input to _samplers_init()
203 max_size = 1000; // TODO make input to _samplers_init()
204
205 sampler = GNUNET_new (struct RPS_Sampler);
206
207 sampler->max_round_interval = max_round_interval;
208 sampler->get_peers = sampler_get_rand_peer;
209 // sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
210 // GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
211 RPS_sampler_resize (sampler, init_size);
212
213 client_get_index = 0;
214
215 // GNUNET_assert (init_size == sampler->sampler_size);
216 return sampler;
217}
218
219
220/**
221 * Get one random peer out of the sampled peers.
222 *
223 * We might want to reinitialise this sampler after giving the
224 * corrsponding peer to the client.
225 * Only used internally
226 */
227static void
228sampler_get_rand_peer (void *cls)
229{
230 struct GetPeerCls *gpc = cls;
231 uint32_t r_index;
232 struct RPS_Sampler *sampler;
233
234 gpc->get_peer_task = NULL;
235 gpc->notify_ctx = NULL;
236 sampler = gpc->req_handle->sampler;
237
238 /**;
239 * Choose the r_index of the peer we want to return
240 * at random from the interval of the gossip list
241 */
242 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
243 sampler->sampler_size);
244
245 if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
246 {
247 // LOG (GNUNET_ERROR_TYPE_DEBUG,
248 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
249
250 gpc->notify_ctx =
251 sampler_notify_on_update (sampler,
252 &sampler_get_rand_peer,
253 gpc);
254 return;
255 }
256
257 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
258 gpc->req_handle->gpc_tail,
259 gpc);
260 *gpc->id = sampler->sampler_elements[r_index]->peer_id;
261 gpc->cont (gpc->cont_cls, gpc->id, 0,
262 sampler->sampler_elements[r_index]->num_peers);
263
264 GNUNET_free (gpc);
265}
266
267
268/* end of gnunet-service-rps.c */
diff --git a/src/rps/gnunet-service-rps_sampler.h b/src/rps/gnunet-service-rps_sampler.h
deleted file mode 100644
index 410823bea..000000000
--- a/src/rps/gnunet-service-rps_sampler.h
+++ /dev/null
@@ -1,157 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-service-rps_sampler.h
23 * @brief sampler implementation
24 * @author Julius Bünger
25 */
26
27#ifndef RPS_SAMPLER_H
28#define RPS_SAMPLER_H
29#include <inttypes.h>
30#include "rps-sampler_common.h"
31
32
33/**
34 * A sampler sampling a stream of PeerIDs.
35 */
36struct RPS_Sampler;
37
38/**
39 * A handle to cancel a request.
40 */
41struct RPS_SamplerRequestHandle;
42
43
44/**
45 * Get the size of the sampler.
46 *
47 * @param sampler the sampler to return the size of.
48 * @return the size of the sampler
49 */
50unsigned int
51RPS_sampler_get_size (struct RPS_Sampler *sampler);
52
53
54/**
55 * Grow or shrink the size of the sampler.
56 *
57 * @param sampler the sampler to resize.
58 * @param new_size the new size of the sampler (not 0)
59 */
60void
61RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
62
63
64/**
65 * Initialise a tuple of samplers.
66 *
67 * @param init_size the size the sampler is initialised with
68 * @param max_round_interval maximum time a round takes
69 * @return a handle to a sampler that consists of sampler elements.
70 */
71struct RPS_Sampler *
72RPS_sampler_init (size_t init_size,
73 struct GNUNET_TIME_Relative max_round_interval);
74
75
76/**
77 * Update every sampler element of this sampler with given peer
78 *
79 * @param sampler the sampler to update.
80 * @param id the PeerID that is put in the sampler
81 */
82void
83RPS_sampler_update (struct RPS_Sampler *sampler,
84 const struct GNUNET_PeerIdentity *id);
85
86
87/**
88 * Reinitialise all previously initialised sampler elements with the given
89 * value.
90 *
91 * Used to get rid of a PeerID.
92 *
93 * FIXME: This should also consider currently pending requests
94 * (Pending requests already collect peerids. As long as not all
95 * requested IDs have been collected, they are kept.
96 * Ideally, the @p id should be removed from all pending requests. This
97 * seems quite complicated.)
98 *
99 * @param sampler the sampler to reinitialise a sampler in.
100 * @param id the id of the samplers to update.
101 */
102void
103RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
104 const struct GNUNET_PeerIdentity *id);
105
106
107/**
108 * Get n random peers out of the sampled peers.
109 *
110 * We might want to reinitialise this sampler after giving the
111 * corrsponding peer to the client.
112 * Random with or without consumption?
113 *
114 * @param sampler the sampler to get peers from.
115 * @param cb callback that will be called once the ids are ready.
116 * @param cls closure given to @a cb
117 * @param for_client #GNUNET_YES if result is used for client,
118 * #GNUNET_NO if used internally
119 * @param num_peers the number of peers requested
120 */
121struct RPS_SamplerRequestHandle *
122RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
123 uint32_t num_peers,
124 RPS_sampler_n_rand_peers_ready_cb cb,
125 void *cls);
126
127/**
128 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
129 *
130 * @param req_handle the handle to the request
131 */
132void
133RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
134
135
136/**
137 * Counts how many Samplers currently hold a given PeerID.
138 *
139 * @param sampler the sampler to count ids in.
140 * @param id the PeerID to count.
141 * @return the number of occurrences of id.
142 */
143uint32_t
144RPS_sampler_count_id (struct RPS_Sampler *sampler,
145 const struct GNUNET_PeerIdentity *id);
146
147
148/**
149 * Cleans the samplers.
150 *
151 * @param sampler the sampler to destroy.
152 */
153void
154RPS_sampler_destroy (struct RPS_Sampler *sampler);
155
156#endif
157/* end of gnunet-service-rps.c */
diff --git a/src/rps/gnunet-service-rps_sampler_elem.c b/src/rps/gnunet-service-rps_sampler_elem.c
deleted file mode 100644
index aa8b72445..000000000
--- a/src/rps/gnunet-service-rps_sampler_elem.c
+++ /dev/null
@@ -1,170 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
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/***********************************************************************
39* WARNING: This section needs to be reviewed regarding the use of
40* functions providing (pseudo)randomness!
41***********************************************************************/
42
43
44/**
45 * Reinitialise a previously initialised sampler element.
46 *
47 * @param sampler_el The sampler element to (re-) initialise
48 */
49void
50RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_elem)
51{
52 sampler_elem->is_empty = EMPTY;
53
54 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
55 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
56 &(sampler_elem->auth_key.key),
57 GNUNET_CRYPTO_HASH_LENGTH);
58
59 sampler_elem->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
60
61 sampler_elem->birth = GNUNET_TIME_absolute_get ();
62 sampler_elem->num_peers = 0;
63 sampler_elem->num_change = 0;
64}
65
66
67/**
68 * Create a sampler element and initialise it.
69 *
70 * In this implementation this means choosing an auth_key for later use in
71 * a hmac at random.
72 *
73 * @return a newly created RPS_SamplerElement which currently holds no id.
74 */
75struct RPS_SamplerElement *
76RPS_sampler_elem_create (void)
77{
78 struct RPS_SamplerElement *s;
79
80 s = GNUNET_new (struct RPS_SamplerElement);
81
82 RPS_sampler_elem_reinit (s);
83
84 return s;
85}
86
87
88/**
89 * Destroy a sampler element.
90 *
91 * @param sampler_elem the element to destroy
92 */
93void
94RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem)
95{
96 GNUNET_free (sampler_elem);
97}
98
99
100/**
101 * Update a sampler element with a PeerID
102 *
103 * @param sampler_elem The sampler element to update
104 * @param new_ID The PeerID to update with
105 */
106void
107RPS_sampler_elem_next (struct RPS_SamplerElement *sampler_elem,
108 const struct GNUNET_PeerIdentity *new_ID)
109{
110 struct GNUNET_HashCode other_hash;
111
112 sampler_elem->num_peers++;
113
114 if (0 == GNUNET_memcmp (new_ID, &(sampler_elem->peer_id)))
115 {
116 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
117 GNUNET_i2s (&(sampler_elem->peer_id)));
118 }
119 else
120 {
121 GNUNET_CRYPTO_hmac (&sampler_elem->auth_key,
122 new_ID,
123 sizeof(struct GNUNET_PeerIdentity),
124 &other_hash);
125
126 if (EMPTY == sampler_elem->is_empty)
127 {
128 LOG (GNUNET_ERROR_TYPE_DEBUG,
129 "Got PeerID %s; Simply accepting (was empty previously).\n",
130 GNUNET_i2s (new_ID));
131 sampler_elem->peer_id = *new_ID;
132 sampler_elem->peer_id_hash = other_hash;
133
134 sampler_elem->num_change++;
135 }
136 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash,
137 &sampler_elem->peer_id_hash))
138 {
139 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
140 GNUNET_i2s (&sampler_elem->peer_id));
141 sampler_elem->peer_id = *new_ID;
142 sampler_elem->peer_id_hash = other_hash;
143
144 sampler_elem->num_change++;
145 }
146 else
147 {
148 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
149 GNUNET_i2s (&sampler_elem->peer_id));
150 }
151 }
152 sampler_elem->is_empty = NOT_EMPTY;
153}
154
155
156/**
157 * Set the min-wise independent function of the given sampler element.
158 *
159 * @param sampler_elem the sampler element
160 * @param auth_key the key to use
161 */
162void
163RPS_sampler_elem_set (struct RPS_SamplerElement *sampler_elem,
164 struct GNUNET_CRYPTO_AuthKey auth_key)
165{
166 sampler_elem->auth_key = auth_key;
167}
168
169
170/* end of gnunet-service-rps.c */
diff --git a/src/rps/gnunet-service-rps_sampler_elem.h b/src/rps/gnunet-service-rps_sampler_elem.h
deleted file mode 100644
index 98959a88f..000000000
--- a/src/rps/gnunet-service-rps_sampler_elem.h
+++ /dev/null
@@ -1,153 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-service-rps_sampler_elem.h
23 * @brief sampler element implementation
24 * @author Julius Bünger
25 */
26
27#ifndef RPS_SAMPLER_ELEM_H
28#define RPS_SAMPLER_ELEM_H
29#include <inttypes.h>
30
31
32/***********************************************************************
33* WARNING: This section needs to be reviewed regarding the use of
34* functions providing (pseudo)randomness!
35***********************************************************************/
36
37/**
38 * Used to indicate whether a sampler element is empty.
39 */
40enum RPS_SamplerEmpty
41{
42 NOT_EMPTY = 0x0,
43 EMPTY = 0x1
44};
45
46/**
47 * A sampler element sampling one PeerID at a time.
48 */
49struct RPS_SamplerElement
50{
51 /**
52 * Min-wise linear permutation used by this sampler.
53 *
54 * This is an key later used by a hmac.
55 */
56 struct GNUNET_CRYPTO_AuthKey auth_key;
57
58 /**
59 * The PeerID this sampler currently samples.
60 */
61 struct GNUNET_PeerIdentity peer_id;
62
63 /**
64 * The according hash value of this PeerID.
65 */
66 struct GNUNET_HashCode peer_id_hash;
67
68
69 /**
70 * Time of last request.
71 */
72 struct GNUNET_TIME_Absolute last_client_request;
73
74 /**
75 * Flag that indicates that we are not holding a valid PeerID right now.
76 */
77 enum RPS_SamplerEmpty is_empty;
78
79 /**
80 * 'Birth'
81 */
82 struct GNUNET_TIME_Absolute birth;
83
84 /**
85 * How many times a PeerID was put in this sampler.
86 */
87 uint32_t num_peers;
88
89 /**
90 * How many times this sampler changed the peer_id.
91 */
92 uint32_t num_change;
93
94 /**
95 * The file name this sampler element should log to
96 */
97 char *file_name;
98};
99
100
101/**
102 * Reinitialise a previously initialised sampler element.
103 *
104 * @param sampler_el The sampler element to (re-) initialise
105 */
106void
107RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_elem);
108
109
110/**
111 * Create a sampler element and initialise it.
112 *
113 * In this implementation this means choosing an auth_key for later use in
114 * a hmac at random.
115 *
116 * @return a newly created RPS_SamplerElement which currently holds no id.
117 */
118struct RPS_SamplerElement *
119RPS_sampler_elem_create (void);
120
121
122/**
123 * Destroy a sampler element.
124 *
125 * @param sampler_elem the element to destroy
126 */
127void
128RPS_sampler_elem_destroy (struct RPS_SamplerElement *sampler_elem);
129
130
131/**
132 * Update a sampler element with a PeerID
133 *
134 * @param sampler_elem The sampler element to update
135 * @param new_ID The PeerID to update with
136 */
137void
138RPS_sampler_elem_next (struct RPS_SamplerElement *sampler_elem,
139 const struct GNUNET_PeerIdentity *new_ID);
140
141/**
142 * Set the min-wise independent function of the given sampler element.
143 *
144 * @param sampler_elem the sampler element
145 * @param auth_key the key to use
146 */
147void
148RPS_sampler_elem_set (struct RPS_SamplerElement *sampler_elem,
149 struct GNUNET_CRYPTO_AuthKey auth_key);
150
151
152#endif /* RPS_SAMPLER_ELEM_H */
153/* end of gnunet-service-rps.c */
diff --git a/src/rps/gnunet-service-rps_view.c b/src/rps/gnunet-service-rps_view.c
deleted file mode 100644
index d58b995a3..000000000
--- a/src/rps/gnunet-service-rps_view.c
+++ /dev/null
@@ -1,294 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-service-rps_view.c
23 * @brief wrapper around the "local view"
24 * @author Julius Bünger
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet-service-rps_view.h"
29#include <inttypes.h>
30
31struct View
32{
33 /**
34 * Array containing the peers
35 */
36 struct GNUNET_PeerIdentity *array;
37
38 /**
39 * (Maximum) length of the view
40 */
41 uint32_t length;
42
43 /**
44 * Multipeermap containing the peers
45 */
46 struct GNUNET_CONTAINER_MultiPeerMap *mpm;
47};
48
49
50/**
51 * Create an empty view.
52 *
53 * @param len the maximum length for the view
54 * @return The newly created view
55 */
56struct View *
57View_create (uint32_t len)
58{
59 struct View *view;
60
61 view = GNUNET_new (struct View);
62 view->length = len;
63 view->array = GNUNET_new_array (len, struct GNUNET_PeerIdentity);
64 view->mpm =
65 GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be
66 * set to _YES */
67 return view;
68}
69
70
71/**
72 * Change length of view
73 *
74 * If size is decreased, peers with higher indices are removed.
75 *
76 * @param view The view that is changed
77 * @param len the (maximum) length for the view
78 */
79void
80View_change_len (struct View *view,
81 uint32_t len)
82{
83 uint32_t i;
84 uint32_t *index;
85
86 if (GNUNET_CONTAINER_multipeermap_size (view->mpm) < len)
87 { /* Simply shrink */
88 /* We might simply clear and free the left over space */
89 GNUNET_array_grow (view->array, view->length, len);
90 }
91 else /* We have to remove elements */
92 {
93 /* TODO find a way to preserve indices */
94 for (i = 0; i < len; i++)
95 {
96 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
97 GNUNET_assert (NULL != index);
98 GNUNET_free (index);
99 }
100 GNUNET_array_grow (view->array, view->length, len);
101 GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
102 view->mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
103 for (i = 0; i < len; i++)
104 {
105 index = GNUNET_new (uint32_t);
106 *index = i;
107 GNUNET_CONTAINER_multipeermap_put (view->mpm, &view->array[i], index,
108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
109 }
110 }
111 GNUNET_assert (view->length == len);
112}
113
114
115/**
116 * Get the view as an array
117 *
118 * @param view The view of which the array representation is of interest
119 * @return the view in array representation
120 */
121const struct GNUNET_PeerIdentity *
122View_get_as_array (const struct View *view)
123{
124 return view->array;
125}
126
127
128/**
129 * Get the size of the view
130 *
131 * @param view The view of which the size should be returned
132 * @return current number of actually contained peers
133 */
134unsigned int
135View_size (const struct View *view)
136{
137 return GNUNET_CONTAINER_multipeermap_size (view->mpm);
138}
139
140
141/**
142 * Insert peer into the view
143 *
144 * @param view The view to put the peer into
145 * @param peer the peer to insert
146 *
147 * @return GNUNET_OK if peer was actually inserted
148 * GNUNET_NO if peer was not inserted
149 */
150int
151View_put (struct View *view,
152 const struct GNUNET_PeerIdentity *peer)
153{
154 uint32_t *index;
155
156 if ((view->length <= View_size (view)) || /* If array is 'full' */
157 (GNUNET_YES == View_contains_peer (view, peer)))
158 {
159 return GNUNET_NO;
160 }
161 else
162 {
163 index = GNUNET_new (uint32_t);
164 *index = (uint32_t) View_size (view);
165 view->array[*index] = *peer;
166 GNUNET_CONTAINER_multipeermap_put (view->mpm, peer, index,
167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
168 return GNUNET_OK;
169 }
170}
171
172
173/**
174 * Check whether view contains a peer
175 *
176 * @param view The which is checked for a peer
177 * @param peer the peer to check for
178 *
179 * @return GNUNET_OK if view contains peer
180 * GNUNET_NO otherwise
181 */
182int
183View_contains_peer (const struct View *view,
184 const struct GNUNET_PeerIdentity *peer)
185{
186 return GNUNET_CONTAINER_multipeermap_contains (view->mpm, peer);
187}
188
189
190/**
191 * Remove peer from view
192 *
193 * @param view The view of which to remove the peer
194 * @param peer the peer to remove
195 *
196 * @return GNUNET_OK if view contained peer and removed it successfully
197 * GNUNET_NO if view does not contain peer
198 */
199int
200View_remove_peer (struct View *view,
201 const struct GNUNET_PeerIdentity *peer)
202{
203 uint32_t *index;
204 uint32_t *swap_index;
205 uint32_t last_index;
206
207 if (GNUNET_NO == View_contains_peer (view, peer))
208 {
209 return GNUNET_NO;
210 }
211 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, peer);
212 GNUNET_assert (NULL != index);
213 last_index = View_size (view) - 1;
214 if (*index < last_index)
215 { /* Fill the 'gap' in the array with the last peer */
216 view->array[*index] = view->array[last_index];
217 GNUNET_assert (GNUNET_YES == View_contains_peer (view,
218 &view->array[last_index]));
219 swap_index = GNUNET_CONTAINER_multipeermap_get (view->mpm,
220 &view->array[last_index]);
221 GNUNET_assert (NULL != swap_index);
222 *swap_index = *index;
223 GNUNET_free (index);
224 }
225 GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, peer);
226 return GNUNET_OK;
227}
228
229
230/**
231 * Get a peer by index
232 *
233 * @param view the view of which to get the peer
234 * @param index the index of the peer to get
235 *
236 * @return peer to the corresponding index.
237 * NULL if this index is not known
238 */
239const struct GNUNET_PeerIdentity *
240View_get_peer_by_index (const struct View *view,
241 uint32_t index)
242{
243 if (index < GNUNET_CONTAINER_multipeermap_size (view->mpm))
244 {
245 return &view->array[index];
246 }
247 else
248 {
249 return NULL;
250 }
251}
252
253
254/**
255 * Clear the view
256 *
257 * @param view The view to clear
258 */
259void
260View_clear (struct View *view)
261{
262 for (uint32_t i = 0; 0 < View_size (view); i++)
263 { /* Need to free indices stored at peers */
264 uint32_t *index;
265
266 GNUNET_assert (GNUNET_YES ==
267 GNUNET_CONTAINER_multipeermap_contains (view->mpm,
268 &view->array[i]));
269 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
270 GNUNET_assert (NULL != index);
271 GNUNET_free (index);
272 GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, &view->array[i]);
273 }
274 GNUNET_assert (0 == View_size (view));
275}
276
277
278/**
279 * Destroy view.
280 *
281 * @param view the view to destroy
282 */
283void
284View_destroy (struct View *view)
285{
286 View_clear (view);
287 GNUNET_free (view->array);
288 view->array = NULL;
289 GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
290 GNUNET_free (view);
291}
292
293
294/* end of gnunet-service-rps_view.c */
diff --git a/src/rps/gnunet-service-rps_view.h b/src/rps/gnunet-service-rps_view.h
deleted file mode 100644
index 4d42272c1..000000000
--- a/src/rps/gnunet-service-rps_view.h
+++ /dev/null
@@ -1,145 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/gnunet-service-rps_view.h
23 * @brief wrapper around the "local view"
24 * @author Julius Bünger
25 */
26#include "gnunet_util_lib.h"
27#include <inttypes.h>
28
29struct View;
30
31/**
32 * Create an empty view.
33 *
34 * @param len the maximum length for the view
35 * @return The newly created view
36 */
37struct View *
38View_create (unsigned int len);
39
40
41/**
42 * Change length of view
43 *
44 * If size is decreased, peers with higher indices are removed.
45 *
46 * @param view The view that is changed
47 * @param len the (maximum) length for the view
48 */
49void
50View_change_len (struct View *view,
51 unsigned int len);
52
53/**
54 * Get the view as an array
55 *
56 * @return the view in array representation
57 */
58const struct GNUNET_PeerIdentity *
59View_get_as_array (const struct View *view);
60
61
62/**
63 * Get the size of the view
64 *
65 * @param view The view of which the size should be returned
66 * @return current number of actually contained peers
67 */
68unsigned int
69View_size (const struct View *view);
70
71
72/**
73 * Insert peer into the view
74 *
75 * @param view The view to put the peer into
76 * @param peer the peer to insert
77 *
78 * @return GNUNET_OK if peer was actually inserted
79 * GNUNET_NO if peer was not inserted
80 */
81int
82View_put (struct View *view,
83 const struct GNUNET_PeerIdentity *peer);
84
85
86/**
87 * Check whether view contains a peer
88 *
89 * @param view The which is checked for a peer
90 * @param peer the peer to check for
91 *
92 * @return GNUNET_OK if view contains peer
93 * GNUNET_NO otherwise
94 */
95int
96View_contains_peer (const struct View *view,
97 const struct GNUNET_PeerIdentity *peer);
98
99
100/**
101 * Remove peer from view
102 *
103 * @param view The view of which to remove the peer
104 * @param peer the peer to remove
105 *
106 * @return GNUNET_OK if view contained peer and removed it successfully
107 * GNUNET_NO if view does not contain peer
108 */
109int
110View_remove_peer (struct View *view,
111 const struct GNUNET_PeerIdentity *peer);
112
113
114/**
115 * Get a peer by index
116 *
117 * @param view the view of which to get the peer
118 * @param index the index of the peer to get
119 *
120 * @return peer to the corresponding index.
121 * NULL if this index is not known
122 */
123const struct GNUNET_PeerIdentity *
124View_get_peer_by_index (const struct View *view,
125 uint32_t index);
126
127
128/**
129 * Clear the view
130 *
131 * @param view The view to clear
132 */
133void
134View_clear (struct View *view);
135
136
137/**
138 * Destroy view.
139 *
140 * @param view the view to destroy
141 */
142void
143View_destroy (struct View *view);
144
145/* end of gnunet-service-rps_view.h */
diff --git a/src/rps/profiler_rps.conf b/src/rps/profiler_rps.conf
deleted file mode 100644
index 89e5e35f2..000000000
--- a/src/rps/profiler_rps.conf
+++ /dev/null
@@ -1,139 +0,0 @@
1[rps]
2#PREFIX = valgrind --leak-check=full --show-leak-kinds=all --log-file=/tmp/rps/valgrind!gnunet-service-rps!%p
3#PREFIX = valgrind --log-file=/tmp/rps/valgrind!gnunet-service-rps!%p
4#PREFIX = valgrind
5UNIXPATH = $GNUNET_TMP/gnunet-service-rps.sock
6HOME = $SERVICEHOME
7# PORT = 2106
8#@UNIXONLY@ PORT = 2087
9IMMEDIATE_START = YES
10START_ON_DEMAND = NO
11NOARMBIND = YES
12#OPTIONS=-l /tmp/rps_profiler_logs/rps-[]-%Y-%m-%d.log
13
14# This is the timeinterval between the rounds
15ROUNDINTERVAL = 2 s
16FILENAME_VALID_PEERS = $GNUNET_DATA_HOME/rps/valid_peers.txt
17
18# This is the 'estimate' in the beginning.
19# This determines the size of the peers we keep in memory
20# until we receive the first estimate from NSE.
21# Keep in mind, that (networksize)^(1/3) should be enough.
22# So, 50 is enough for a network of size 50^3 = 125000
23MINSIZE = 4
24
25DESIRED_PROBABILITY = 0.75
26
27DEFICIENCY_FACTOR = 0.4
28
29
30[testbed]
31HOSTNAME = localhost
32
33# MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS = 100
34
35#OVERLAY_TOPOLOGY = CLIQUE
36
37#OVERLAY_TOPOLOGY = SCALE_FREE
38#SCALE_FREE_TOPOLOGY_CAP = 100
39#SCALE_FREE_TOPOLOGY_M = 2
40
41OVERLAY_TOPOLOGY = RANDOM
42## We take half of the links a complete graph would have, so (n * n-1)/4
43## for n = 128, this would be
44OVERLAY_RANDOM_LINKS = 4064
45
46#OVERLAY_TOPOLOGY = SMALL_WORLD
47#OVERLAY_RANDOM_LINKS = 25
48
49SETUP_TIMEOUT = 1 h
50
51[nse]
52WORKBITS = 0
53
54[nat]
55# Use addresses from the local network interfaces (including loopback, but also others)
56USE_LOCALADDR = YES
57ENABLE_UPNP = NO
58
59# Do we use addresses from localhost address ranges? (::1, 127.0.0.0/8)
60RETURN_LOCAL_ADDRESSES = YES
61
62[transport]
63PLUGINS = unix
64
65[ats]
66# Network specific inbound/outbound quotas
67UNSPECIFIED_QUOTA_IN = unlimited
68UNSPECIFIED_QUOTA_OUT = unlimited
69# LOOPBACK
70LOOPBACK_QUOTA_IN = unlimited
71LOOPBACK_QUOTA_OUT = unlimited
72# LAN
73LAN_QUOTA_IN = unlimited
74LAN_QUOTA_OUT = unlimited
75#WAN
76WAN_QUOTA_OUT = unlimited
77WAN_QUOTA_IN = unlimited
78# WLAN
79WLAN_QUOTA_IN = unlimited
80WLAN_QUOTA_OUT = unlimited
81# BLUETOOTH
82BLUETOOTH_QUOTA_IN = unlimited
83BLUETOOTH_QUOTA_OUT = unlimited
84
85[dht]
86DISABLE_TRY_CONNECT = YES
87
88[cadet]
89OPTIONS=-l /tmp/rps_profiler_logs/cadet-[]-%Y-%m-%d.log
90
91#[arm]
92#GLOBAL_POSTFIX=-l /tmp/rps_profiler_logs/other-[]-%Y-%m-%d.log
93
94#[statistics]
95#IMMEDIATE_START = NO
96#START_ON_DEMAND = NO
97
98[peerinfo]
99NO_IO = YES
100
101[hostlist]
102IMMEDIATE_START = NO
103START_ON_DEMAND = NO
104
105[zonemaster]
106IMMEDIATE_START = NO
107START_ON_DEMAND = NO
108
109[namecache]
110IMMEDIATE_START = NO
111START_ON_DEMAND = NO
112
113[namestore]
114IMMEDIATE_START = NO
115START_ON_DEMAND = NO
116
117[topology]
118IMMEDIATE_START = NO
119START_ON_DEMAND = NO
120
121[vpn]
122IMMEDIATE_START = NO
123START_ON_DEMAND = NO
124
125[revocation]
126IMMEDIATE_START = NO
127START_ON_DEMAND = NO
128
129[gns]
130IMMEDIATE_START = NO
131START_ON_DEMAND = NO
132
133[fs]
134IMMEDIATE_START = NO
135START_ON_DEMAND = NO
136
137[zonemaster-monitor]
138IMMEDIATE_START = NO
139START_ON_DEMAND = NO
diff --git a/src/rps/rps-sampler_client.c b/src/rps/rps-sampler_client.c
deleted file mode 100644
index f6e98ce29..000000000
--- a/src/rps/rps-sampler_client.c
+++ /dev/null
@@ -1,439 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
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#include "gnunet_statistics_service.h"
29#include "rps.h"
30
31#include "rps-sampler_common.h"
32#include "gnunet-service-rps_sampler.h"
33#include "gnunet-service-rps_sampler_elem.h"
34
35#include <math.h>
36#include <inttypes.h>
37
38#include "rps-test_util.h"
39
40#define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler", __VA_ARGS__)
41
42
43// multiple 'clients'?
44
45// TODO check for overflows
46
47// TODO align message structs
48
49// hist_size_init, hist_size_max
50
51/***********************************************************************
52* WARNING: This section needs to be reviewed regarding the use of
53* functions providing (pseudo)randomness!
54***********************************************************************/
55
56// TODO care about invalid input of the caller (size 0 or less...)
57
58/**
59 * @brief Callback called each time a new peer was put into the sampler
60 *
61 * @param cls A possibly given closure
62 */
63typedef void
64(*SamplerNotifyUpdateCB) (void *cls);
65
66/**
67 * @brief Context for a callback. Contains callback and closure.
68 *
69 * Meant to be an entry in an DLL.
70 */
71struct SamplerNotifyUpdateCTX
72{
73 /**
74 * @brief The Callback to call on updates
75 */
76 SamplerNotifyUpdateCB notify_cb;
77
78 /**
79 * @brief The according closure.
80 */
81 void *cls;
82
83 /**
84 * @brief Next element in DLL.
85 */
86 struct SamplerNotifyUpdateCTX *next;
87
88 /**
89 * @brief Previous element in DLL.
90 */
91 struct SamplerNotifyUpdateCTX *prev;
92};
93
94
95/**
96 * Type of function used to differentiate between modified and not modified
97 * Sampler.
98 */
99typedef void
100(*RPS_get_peers_type) (void *cls);
101
102
103/**
104 * Get one random peer out of the sampled peers.
105 *
106 * We might want to reinitialise this sampler after giving the
107 * corrsponding peer to the client.
108 */
109static void
110sampler_mod_get_rand_peer (void *cls);
111
112
113/**
114 * Closure to _get_n_rand_peers_ready_cb()
115 */
116struct RPS_SamplerRequestHandle
117{
118 /**
119 * DLL
120 */
121 struct RPS_SamplerRequestHandle *next;
122 struct RPS_SamplerRequestHandle *prev;
123
124 /**
125 * Number of peers we are waiting for.
126 */
127 uint32_t num_peers;
128
129 /**
130 * Number of peers we currently have.
131 */
132 uint32_t cur_num_peers;
133
134 /**
135 * Pointer to the array holding the ids.
136 */
137 struct GNUNET_PeerIdentity *ids;
138
139 /**
140 * Head and tail for the DLL to store the tasks for single requests
141 */
142 struct GetPeerCls *gpc_head;
143 struct GetPeerCls *gpc_tail;
144
145 /**
146 * Sampler.
147 */
148 struct RPS_Sampler *sampler;
149
150 /**
151 * Callback to be called when all ids are available.
152 */
153 RPS_sampler_n_rand_peers_ready_cb callback;
154
155 /**
156 * Closure given to the callback
157 */
158 void *cls;
159};
160
161
162/**
163 * Closure to _get_rand_peer_info()
164 */
165struct RPS_SamplerRequestHandleSingleInfo
166{
167 /**
168 * DLL
169 */
170 struct RPS_SamplerRequestHandleSingleInfo *next;
171 struct RPS_SamplerRequestHandleSingleInfo *prev;
172
173 /**
174 * Pointer to the id
175 */
176 struct GNUNET_PeerIdentity *id;
177
178 /**
179 * Head and tail for the DLL to store the tasks for single requests
180 */
181 struct GetPeerCls *gpc_head;
182 struct GetPeerCls *gpc_tail;
183
184 /**
185 * Sampler.
186 */
187 struct RPS_Sampler *sampler;
188
189 /**
190 * Callback to be called when all ids are available.
191 */
192 RPS_sampler_sinlge_info_ready_cb callback;
193
194 /**
195 * Closure given to the callback
196 */
197 void *cls;
198};
199
200
201///**
202// * Global sampler variable.
203// */
204// struct RPS_Sampler *sampler;
205
206
207/**
208 * The minimal size for the extended sampler elements.
209 */
210static size_t min_size;
211
212/**
213 * The maximal size the extended sampler elements should grow to.
214 */
215static size_t max_size;
216
217/**
218 * The size the extended sampler elements currently have.
219 */
220// static size_t extra_size;
221
222/**
223 * Inedex to the sampler element that is the next to be returned
224 */
225static uint32_t client_get_index;
226
227
228/**
229 * Initialise a modified tuple of sampler elements.
230 *
231 * @param init_size the size the sampler is initialised with
232 * @param max_round_interval maximum time a round takes
233 * @return a handle to a sampler that consists of sampler elements.
234 */
235struct RPS_Sampler *
236RPS_sampler_mod_init (size_t init_size,
237 struct GNUNET_TIME_Relative max_round_interval)
238{
239 struct RPS_Sampler *sampler;
240
241 /* Initialise context around extended sampler */
242 min_size = 10; // TODO make input to _samplers_init()
243 max_size = 1000; // TODO make input to _samplers_init()
244
245 sampler = GNUNET_new (struct RPS_Sampler);
246 sampler->max_round_interval = max_round_interval;
247 sampler->get_peers = sampler_mod_get_rand_peer;
248 // sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
249 // GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
250
251 client_get_index = 0;
252
253 // GNUNET_assert (init_size == sampler->sampler_size);
254
255 RPS_sampler_resize (sampler, init_size);
256
257 return sampler;
258}
259
260
261// /**
262// * @brief Compute the probability that we already observed all peers from a
263// * biased stream of peer ids.
264// *
265// * Deficiency factor:
266// * As introduced by Brahms: Factor between the number of unique ids in a
267// * truly random stream and number of unique ids in the gossip stream.
268// *
269// * @param num_peers_estim The estimated number of peers in the network
270// * @param num_peers_observed The number of peers the given element has observed
271// * @param deficiency_factor A factor that catches the 'bias' of a random stream
272// * of peer ids
273// *
274// * @return The estimated probability
275// */
276// static double
277// prob_observed_n_peers (uint32_t num_peers_estim,
278// uint32_t num_peers_observed,
279// double deficiency_factor)
280// {
281// uint32_t num_peers = num_peers_estim * (1 / deficiency_factor);
282// uint64_t sum = 0;
283//
284// for (uint32_t i = 0; i < num_peers; i++)
285// {
286// uint64_t a = pow (-1, num_peers - i);
287// uint64_t b = binom (num_peers, i);
288// uint64_t c = pow (i, num_peers_observed);
289// sum += a * b * c;
290// }
291//
292// return sum / (double) pow (num_peers, num_peers_observed);
293// }
294
295
296/**
297 * Get one random peer out of the sampled peers.
298 *
299 * This reinitialises the queried sampler element.
300 */
301static void
302sampler_mod_get_rand_peer (void *cls)
303{
304 struct GetPeerCls *gpc = cls;
305 struct RPS_SamplerElement *s_elem;
306 struct GNUNET_TIME_Relative last_request_diff;
307 struct RPS_Sampler *sampler;
308 double prob_observed_n;
309 uint32_t num_observed;
310
311 gpc->get_peer_task = NULL;
312 gpc->notify_ctx = NULL;
313 GNUNET_assert ((NULL != gpc->req_handle) ||
314 (NULL != gpc->req_single_info_handle));
315 if (NULL != gpc->req_handle)
316 sampler = gpc->req_handle->sampler;
317 else
318 sampler = gpc->req_single_info_handle->sampler;
319
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
321
322 /* Cycle the #client_get_index one step further */
323 client_get_index = (client_get_index + 1) % sampler->sampler_size;
324
325 s_elem = sampler->sampler_elements[client_get_index];
326 *gpc->id = s_elem->peer_id;
327 GNUNET_assert (NULL != s_elem);
328
329 if (EMPTY == s_elem->is_empty)
330 {
331 LOG (GNUNET_ERROR_TYPE_DEBUG,
332 "Sampler_mod element empty, rescheduling.\n");
333 GNUNET_assert (NULL == gpc->notify_ctx);
334 gpc->notify_ctx =
335 sampler_notify_on_update (sampler,
336 &sampler_mod_get_rand_peer,
337 gpc);
338 return;
339 }
340
341 /* Check whether we may use this sampler to give it back to the client */
342 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
343 s_elem->last_client_request.abs_value_us)
344 {
345 // TODO remove this condition at least for the client sampler
346 last_request_diff =
347 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
348 GNUNET_TIME_absolute_get ());
349 /* We're not going to give it back now if it was
350 * already requested by a client this round */
351 if (last_request_diff.rel_value_us <
352 sampler->max_round_interval.rel_value_us)
353 {
354 LOG (GNUNET_ERROR_TYPE_DEBUG,
355 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
356 ///* How many time remains untile the next round has started? */
357 // inv_last_request_diff =
358 // GNUNET_TIME_absolute_get_difference (last_request_diff,
359 // sampler->max_round_interval);
360 // add a little delay
361 /* Schedule it one round later */
362 GNUNET_assert (NULL == gpc->notify_ctx);
363 gpc->notify_ctx =
364 sampler_notify_on_update (sampler,
365 &sampler_mod_get_rand_peer,
366 gpc);
367 return;
368 }
369 }
370 if (2 > s_elem->num_peers)
371 {
372 LOG (GNUNET_ERROR_TYPE_DEBUG,
373 "This s_elem saw less than two peers -- scheduling for later\n");
374 GNUNET_assert (NULL == gpc->notify_ctx);
375 gpc->notify_ctx =
376 sampler_notify_on_update (sampler,
377 &sampler_mod_get_rand_peer,
378 gpc);
379 return;
380 }
381 /* compute probability */
382 /* FIXME: Currently disabled due to numerical limitations */
383 prob_observed_n = 0; // Inititialise to some value
384 // prob_observed_n = prob_observed_n_peers (sampler->num_peers_estim,
385 // s_elem->num_peers,
386 // sampler->deficiency_factor);
387 // LOG (GNUNET_ERROR_TYPE_DEBUG,
388 // "Computed sample - prob %f, %" PRIu32 " peers, n: %" PRIu32 ", roh: %f\n",
389 // prob_observed_n,
390 // s_elem->num_peers,
391 // sampler->num_peers_estim,
392 // sampler->deficiency_factor);
393 ///* check if probability is above desired */
394 // if (prob_observed_n < sampler->desired_probability)
395 // {
396 // LOG (GNUNET_ERROR_TYPE_DEBUG,
397 // "Probability of having observed all peers (%f) too small ( < %f).\n",
398 // prob_observed_n,
399 // sampler->desired_probability);
400 // GNUNET_assert (NULL == gpc->notify_ctx);
401 // gpc->notify_ctx =
402 // sampler_notify_on_update (sampler,
403 // &sampler_mod_get_rand_peer,
404 // gpc);
405 // return;
406 // }
407 /* More reasons to wait could be added here */
408
409// GNUNET_STATISTICS_set (stats,
410// "# client sampler element input",
411// s_elem->num_peers,
412// GNUNET_NO);
413// GNUNET_STATISTICS_set (stats,
414// "# client sampler element change",
415// s_elem->num_change,
416// GNUNET_NO);
417
418 num_observed = s_elem->num_peers;
419 RPS_sampler_elem_reinit (s_elem);
420 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
421
422 if (NULL != gpc->req_handle)
423 {
424 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
425 gpc->req_handle->gpc_tail,
426 gpc);
427 }
428 else
429 {
430 GNUNET_CONTAINER_DLL_remove (gpc->req_single_info_handle->gpc_head,
431 gpc->req_single_info_handle->gpc_tail,
432 gpc);
433 }
434 gpc->cont (gpc->cont_cls, gpc->id, prob_observed_n, num_observed);
435 GNUNET_free (gpc);
436}
437
438
439/* end of gnunet-service-rps.c */
diff --git a/src/rps/rps-sampler_client.h b/src/rps/rps-sampler_client.h
deleted file mode 100644
index 7a674371d..000000000
--- a/src/rps/rps-sampler_client.h
+++ /dev/null
@@ -1,156 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/rps-sampler_client.h
23 * @brief client sampler implementation
24 * @author Julius Bünger
25 */
26
27#ifndef RPS_SAMPLER_CLIENT_H
28#define RPS_SAMPLER_CLIENT_H
29#include <inttypes.h>
30#include "rps-sampler_common.h"
31
32
33/**
34 * A sampler sampling a stream of PeerIDs.
35 */
36struct RPS_Sampler;
37
38/**
39 * A handle to cancel a request.
40 */
41struct RPS_SamplerRequestHandle;
42
43/**
44 * Closure to _get_rand_peer_info()
45 */
46struct RPS_SamplerRequestHandleSingleInfo;
47
48
49/**
50 * Get the size of the sampler.
51 *
52 * @param sampler the sampler to return the size of.
53 * @return the size of the sampler
54 */
55unsigned int
56RPS_sampler_get_size (struct RPS_Sampler *sampler);
57
58
59/**
60 * Grow or shrink the size of the sampler.
61 *
62 * @param sampler the sampler to resize.
63 * @param new_size the new size of the sampler (not 0)
64 */
65void
66RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
67
68
69/**
70 * Initialise a modified tuple of sampler elements.
71 *
72 * @param init_size the size the sampler is initialised with
73 * @param max_round_interval maximum time a round takes
74 * @return a handle to a sampler that consists of sampler elements.
75 */
76struct RPS_Sampler *
77RPS_sampler_mod_init (size_t init_size,
78 struct GNUNET_TIME_Relative max_round_interval);
79
80
81/**
82 * Update every sampler element of this sampler with given peer
83 *
84 * @param sampler the sampler to update.
85 * @param id the PeerID that is put in the sampler
86 */
87void
88RPS_sampler_update (struct RPS_Sampler *sampler,
89 const struct GNUNET_PeerIdentity *id);
90
91
92/**
93 * Reinitialise all previously initialised sampler elements with the given
94 * value.
95 *
96 * Used to get rid of a PeerID.
97 *
98 * @param sampler the sampler to reinitialise a sampler in.
99 * @param id the id of the samplers to update.
100 */
101void
102RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
103 const struct GNUNET_PeerIdentity *id);
104
105
106/**
107 * Get n random peers out of the sampled peers.
108 *
109 * We might want to reinitialise this sampler after giving the
110 * corrsponding peer to the client.
111 * Random with or without consumption?
112 *
113 * @param sampler the sampler to get peers from.
114 * @param cb callback that will be called once the ids are ready.
115 * @param cls closure given to @a cb
116 * @param num_peers the number of peers requested
117 */
118struct RPS_SamplerRequestHandle *
119RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
120 uint32_t num_peers,
121 RPS_sampler_n_rand_peers_ready_cb cb,
122 void *cls);
123
124
125/**
126 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
127 *
128 * @param req_handle the handle to the request
129 */
130void
131RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
132
133
134/**
135 * Counts how many Samplers currently hold a given PeerID.
136 *
137 * @param sampler the sampler to count ids in.
138 * @param id the PeerID to count.
139 *
140 * @return the number of occurrences of id.
141 */
142uint32_t
143RPS_sampler_count_id (struct RPS_Sampler *sampler,
144 const struct GNUNET_PeerIdentity *id);
145
146
147/**
148 * Cleans the samplers.
149 *
150 * @param sampler the sampler to destroy.
151 */
152void
153RPS_sampler_destroy (struct RPS_Sampler *sampler);
154
155#endif /* RPS_SAMPLER_CLIENT_H */
156/* end of gnunet-service-rps.c */
diff --git a/src/rps/rps-sampler_common.c b/src/rps/rps-sampler_common.c
deleted file mode 100644
index a30ba06a1..000000000
--- a/src/rps/rps-sampler_common.c
+++ /dev/null
@@ -1,764 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/rps-sampler_common.c
23 * @brief Code common to client and service sampler
24 * @author Julius Bünger
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_statistics_service.h"
29
30#include "rps-sampler_common.h"
31#include "gnunet-service-rps_sampler_elem.h"
32
33#include <math.h>
34#include <inttypes.h>
35
36#include "rps-test_util.h"
37
38#define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler_common", __VA_ARGS__)
39
40/**
41 * @brief Context for a callback. Contains callback and closure.
42 *
43 * Meant to be an entry in an DLL.
44 */
45struct SamplerNotifyUpdateCTX
46{
47 /**
48 * @brief The Callback to call on updates
49 */
50 SamplerNotifyUpdateCB notify_cb;
51
52 /**
53 * @brief The according closure.
54 */
55 void *cls;
56
57 /**
58 * @brief Next element in DLL.
59 */
60 struct SamplerNotifyUpdateCTX *next;
61
62 /**
63 * @brief Previous element in DLL.
64 */
65 struct SamplerNotifyUpdateCTX *prev;
66};
67
68
69/**
70 * Closure to _get_n_rand_peers_ready_cb()
71 */
72struct RPS_SamplerRequestHandle
73{
74 /**
75 * DLL
76 */
77 struct RPS_SamplerRequestHandle *next;
78 struct RPS_SamplerRequestHandle *prev;
79
80 /**
81 * Number of peers we are waiting for.
82 */
83 uint32_t num_peers;
84
85 /**
86 * Number of peers we currently have.
87 */
88 uint32_t cur_num_peers;
89
90 /**
91 * Pointer to the array holding the ids.
92 */
93 struct GNUNET_PeerIdentity *ids;
94
95 /**
96 * Head and tail for the DLL to store the tasks for single requests
97 */
98 struct GetPeerCls *gpc_head;
99 struct GetPeerCls *gpc_tail;
100
101 /**
102 * Sampler.
103 */
104 struct RPS_Sampler *sampler;
105
106 /**
107 * Callback to be called when all ids are available.
108 */
109 RPS_sampler_n_rand_peers_ready_cb callback;
110
111 /**
112 * Closure given to the callback
113 */
114 void *cls;
115};
116
117
118/**
119 * Closure to _get_rand_peer_info()
120 */
121struct RPS_SamplerRequestHandleSingleInfo
122{
123 /**
124 * DLL
125 */
126 struct RPS_SamplerRequestHandleSingleInfo *next;
127 struct RPS_SamplerRequestHandleSingleInfo *prev;
128
129 /**
130 * Pointer to the id
131 */
132 struct GNUNET_PeerIdentity *id;
133
134 /**
135 * Head and tail for the DLL to store the tasks for single requests
136 */
137 struct GetPeerCls *gpc_head;
138 struct GetPeerCls *gpc_tail;
139
140 /**
141 * Sampler.
142 */
143 struct RPS_Sampler *sampler;
144
145 /**
146 * Callback to be called when all ids are available.
147 */
148 RPS_sampler_sinlge_info_ready_cb callback;
149
150 /**
151 * Closure given to the callback
152 */
153 void *cls;
154};
155
156
157/**
158 * @brief Update the current estimate of the network size stored at the sampler
159 *
160 * Used for computing the condition when to return elements to the client
161 *
162 * Only used/useful with the client sampler
163 * (Maybe move to rps-sampler_client.{h|c} ?)
164 *
165 * @param sampler The sampler to update
166 * @param num_peers The estimated value
167 */
168void
169RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler,
170 uint32_t num_peers)
171{
172 sampler->num_peers_estim = num_peers;
173}
174
175
176/**
177 * @brief Set the probability that is needed at least with what a sampler
178 * element has to have observed all elements from the network.
179 *
180 * Only used/useful with the client sampler
181 * (Maybe move to rps-sampler_client.{h|c} ?)
182 *
183 * @param sampler
184 * @param desired_probability
185 */
186void
187RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler,
188 double desired_probability)
189{
190 sampler->desired_probability = desired_probability;
191}
192
193
194/**
195 * @brief Set the deficiency factor.
196 *
197 * Only used/useful with the client sampler
198 * (Maybe move to rps-sampler_client.{h|c} ?)
199 *
200 * @param sampler
201 * @param desired_probability
202 */
203void
204RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
205 double deficiency_factor)
206{
207 sampler->deficiency_factor = deficiency_factor;
208}
209
210
211/**
212 * @brief Add a callback that will be called when the next peer is inserted
213 * into the sampler
214 *
215 * @param sampler The sampler on which update it will be called
216 * @param notify_cb The callback
217 * @param cls Closure given to the callback
218 *
219 * @return The context containing callback and closure
220 */
221struct SamplerNotifyUpdateCTX *
222sampler_notify_on_update (struct RPS_Sampler *sampler,
223 SamplerNotifyUpdateCB notify_cb,
224 void *cls)
225{
226 struct SamplerNotifyUpdateCTX *notify_ctx;
227
228 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Inserting new context for notification\n");
230 notify_ctx = GNUNET_new (struct SamplerNotifyUpdateCTX);
231 notify_ctx->notify_cb = notify_cb;
232 notify_ctx->cls = cls;
233 GNUNET_CONTAINER_DLL_insert (sampler->notify_ctx_head,
234 sampler->notify_ctx_tail,
235 notify_ctx);
236 return notify_ctx;
237}
238
239
240/**
241 * Get the size of the sampler.
242 *
243 * @param sampler the sampler to return the size of.
244 * @return the size of the sampler
245 */
246unsigned int
247RPS_sampler_get_size (struct RPS_Sampler *sampler)
248{
249 return sampler->sampler_size;
250}
251
252
253/**
254 * @brief Notify about update of the sampler.
255 *
256 * Call the callbacks that are waiting for notification on updates to the
257 * sampler.
258 *
259 * @param sampler The sampler the updates are waiting for
260 */
261static void
262notify_update (struct RPS_Sampler *sampler)
263{
264 struct SamplerNotifyUpdateCTX *tmp_notify_head;
265 struct SamplerNotifyUpdateCTX *tmp_notify_tail;
266
267 LOG (GNUNET_ERROR_TYPE_DEBUG,
268 "Calling callbacks waiting for update notification.\n");
269 tmp_notify_head = sampler->notify_ctx_head;
270 tmp_notify_tail = sampler->notify_ctx_tail;
271 sampler->notify_ctx_head = NULL;
272 sampler->notify_ctx_tail = NULL;
273 for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head;
274 NULL != tmp_notify_head;
275 notify_iter = tmp_notify_head)
276 {
277 GNUNET_assert (NULL != notify_iter->notify_cb);
278 GNUNET_CONTAINER_DLL_remove (tmp_notify_head,
279 tmp_notify_tail,
280 notify_iter);
281 notify_iter->notify_cb (notify_iter->cls);
282 GNUNET_free (notify_iter);
283 }
284}
285
286
287/**
288 * Update every sampler element of this sampler with given peer
289 *
290 * @param sampler the sampler to update.
291 * @param id the PeerID that is put in the sampler
292 */
293void
294RPS_sampler_update (struct RPS_Sampler *sampler,
295 const struct GNUNET_PeerIdentity *id)
296{
297 for (uint32_t i = 0; i < sampler->sampler_size; i++)
298 {
299 RPS_sampler_elem_next (sampler->sampler_elements[i],
300 id);
301 }
302 notify_update (sampler);
303}
304
305
306/**
307 * Reinitialise all previously initialised sampler elements with the given value.
308 *
309 * Used to get rid of a PeerID.
310 *
311 * FIXME: This should also consider currently pending requests
312 * (Pending requests already collect peerids. As long as not all
313 * requested IDs have been collected, they are kept.
314 * Ideally, the @p id should be removed from all pending requests. This
315 * seems quite complicated.)
316 *
317 * @param sampler the sampler to reinitialise a sampler element in.
318 * @param id the id of the sampler elements to update.
319 */
320void
321RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
322 const struct GNUNET_PeerIdentity *id)
323{
324 uint32_t i;
325
326 for (i = 0; i < sampler->sampler_size; i++)
327 {
328 if (0 == GNUNET_memcmp (id,
329 &(sampler->sampler_elements[i]->peer_id)))
330 {
331 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
332 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
333 }
334 }
335}
336
337
338/**
339 * Counts how many Samplers currently hold a given PeerID.
340 *
341 * @param sampler the sampler to count ids in.
342 * @param id the PeerID to count.
343 *
344 * @return the number of occurrences of id.
345 */
346uint32_t
347RPS_sampler_count_id (struct RPS_Sampler *sampler,
348 const struct GNUNET_PeerIdentity *id)
349{
350 uint32_t count;
351 uint32_t i;
352
353 count = 0;
354 for (i = 0; i < sampler->sampler_size; i++)
355 {
356 if ((0 == GNUNET_memcmp (&sampler->sampler_elements[i]->peer_id, id))
357 && (EMPTY != sampler->sampler_elements[i]->is_empty) )
358 count++;
359 }
360 return count;
361}
362
363
364/**
365 * Grow or shrink the size of the sampler.
366 *
367 * @param sampler the sampler to resize.
368 * @param new_size the new size of the sampler
369 */
370static void
371sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
372{
373 unsigned int old_size;
374 uint32_t i;
375
376 // TODO check min and max size
377
378 old_size = sampler->sampler_size;
379
380 if (old_size > new_size)
381 { /* Shrinking */
382 LOG (GNUNET_ERROR_TYPE_DEBUG,
383 "Shrinking sampler %d -> %d\n",
384 old_size,
385 new_size);
386
387 for (i = new_size; i < old_size; i++)
388 {
389 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
390 }
391
392 GNUNET_array_grow (sampler->sampler_elements,
393 sampler->sampler_size,
394 new_size);
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "sampler->sampler_elements now points to %p\n",
397 sampler->sampler_elements);
398 }
399 else if (old_size < new_size)
400 { /* Growing */
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "Growing sampler %d -> %d\n",
403 old_size,
404 new_size);
405
406 GNUNET_array_grow (sampler->sampler_elements,
407 sampler->sampler_size,
408 new_size);
409
410 for (i = old_size; i < new_size; i++)
411 { /* Add new sampler elements */
412 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
413 }
414 }
415 else
416 {
417 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
418 return;
419 }
420
421 GNUNET_assert (sampler->sampler_size == new_size);
422}
423
424
425/**
426 * Grow or shrink the size of the sampler.
427 *
428 * @param sampler the sampler to resize.
429 * @param new_size the new size of the sampler
430 */
431void
432RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
433{
434 GNUNET_assert (0 < new_size);
435 sampler_resize (sampler, new_size);
436}
437
438
439/**
440 * Empty the sampler.
441 *
442 * @param sampler the sampler to empty.
443 * @param new_size the new size of the sampler
444 */
445static void
446sampler_empty (struct RPS_Sampler *sampler)
447{
448 sampler_resize (sampler, 0);
449}
450
451
452/**
453 * Callback to _get_rand_peer() used by _get_n_rand_peers().
454 *
455 * Implements #RPS_sampler_rand_peer_ready_cont
456 *
457 * Checks whether all n peers are available. If they are,
458 * give those back.
459 * @param cls Closure
460 * @param id Peer ID
461 * @param probability The probability with which this sampler has seen all ids
462 * @param num_observed How many ids this sampler has observed
463 */
464static void
465check_n_peers_ready (void *cls,
466 const struct GNUNET_PeerIdentity *id,
467 double probability,
468 uint32_t num_observed)
469{
470 struct RPS_SamplerRequestHandle *req_handle = cls;
471
472 (void) id;
473 RPS_sampler_n_rand_peers_ready_cb tmp_cb;
474 struct GNUNET_PeerIdentity *peers;
475 uint32_t num_peers;
476 void *cb_cls;
477 (void) probability;
478 (void) num_observed;
479
480 req_handle->cur_num_peers++;
481 LOG (GNUNET_ERROR_TYPE_DEBUG,
482 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
483 req_handle->cur_num_peers, req_handle->num_peers);
484
485 if (req_handle->num_peers == req_handle->cur_num_peers)
486 { /* All peers are ready -- return those to the client */
487 GNUNET_assert (NULL != req_handle->callback);
488
489 LOG (GNUNET_ERROR_TYPE_DEBUG,
490 "returning %" PRIX32 " peers to the client\n",
491 req_handle->num_peers);
492
493 /* Copy pointers and peers temporarily as they
494 * might be deleted from within the callback */
495 tmp_cb = req_handle->callback;
496 num_peers = req_handle->num_peers;
497 peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
498 GNUNET_memcpy (peers,
499 req_handle->ids,
500 num_peers * sizeof(struct GNUNET_PeerIdentity));
501 cb_cls = req_handle->cls;
502 RPS_sampler_request_cancel (req_handle);
503 req_handle = NULL;
504 tmp_cb (peers, num_peers, cb_cls);
505 GNUNET_free (peers);
506 }
507}
508
509
510/**
511 * Callback to _get_rand_peer() used by _get_rand_peer_info().
512 *
513 * Implements #RPS_sampler_rand_peer_ready_cont
514 *
515 * @param cls Closure
516 * @param id Peer ID
517 * @param probability The probability with which this sampler has seen all ids
518 * @param num_observed How many ids this sampler has observed
519 */
520static void
521check_peer_info_ready (void *cls,
522 const struct GNUNET_PeerIdentity *id,
523 double probability,
524 uint32_t num_observed)
525{
526 struct RPS_SamplerRequestHandleSingleInfo *req_handle = cls;
527
528 (void) id;
529 RPS_sampler_sinlge_info_ready_cb tmp_cb;
530 struct GNUNET_PeerIdentity *peer;
531 void *cb_cls;
532 (void) probability;
533 (void) num_observed;
534
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 "Got single peer with additional info\n");
537
538 GNUNET_assert (NULL != req_handle->callback);
539
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "returning single peer with info to the client\n");
542
543 /* Copy pointers and peers temporarily as they
544 * might be deleted from within the callback */
545 tmp_cb = req_handle->callback;
546 peer = GNUNET_new (struct GNUNET_PeerIdentity);
547 GNUNET_memcpy (peer,
548 req_handle->id,
549 sizeof(struct GNUNET_PeerIdentity));
550 cb_cls = req_handle->cls;
551 RPS_sampler_request_single_info_cancel (req_handle);
552 req_handle = NULL;
553 tmp_cb (peer, cb_cls, probability, num_observed);
554 GNUNET_free (peer);
555}
556
557
558/**
559 * Get n random peers out of the sampled peers.
560 *
561 * We might want to reinitialise this sampler after giving the
562 * corrsponding peer to the client.
563 * Random with or without consumption?
564 *
565 * @param sampler the sampler to get peers from.
566 * @param cb callback that will be called once the ids are ready.
567 * @param cls closure given to @a cb
568 * @param num_peers the number of peers requested
569 */
570struct RPS_SamplerRequestHandle *
571RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
572 uint32_t num_peers,
573 RPS_sampler_n_rand_peers_ready_cb cb,
574 void *cls)
575{
576 uint32_t i;
577 struct RPS_SamplerRequestHandle *req_handle;
578 struct GetPeerCls *gpc;
579
580 GNUNET_assert (0 != sampler->sampler_size);
581 if (0 == num_peers)
582 return NULL;
583
584 // TODO check if we have too much (distinct) sampled peers
585 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
586 req_handle->num_peers = num_peers;
587 req_handle->cur_num_peers = 0;
588 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
589 req_handle->sampler = sampler;
590 req_handle->callback = cb;
591 req_handle->cls = cls;
592 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
593 sampler->req_handle_tail,
594 req_handle);
595
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
598
599 for (i = 0; i < num_peers; i++)
600 {
601 gpc = GNUNET_new (struct GetPeerCls);
602 gpc->req_handle = req_handle;
603 gpc->req_single_info_handle = NULL;
604 gpc->cont = check_n_peers_ready;
605 gpc->cont_cls = req_handle;
606 gpc->id = &req_handle->ids[i];
607
608 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
609 req_handle->gpc_tail,
610 gpc);
611 // maybe add a little delay
612 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
613 gpc);
614 }
615 return req_handle;
616}
617
618
619/**
620 * Get one random peer with additional information.
621 *
622 * @param sampler the sampler to get peers from.
623 * @param cb callback that will be called once the ids are ready.
624 * @param cls closure given to @a cb
625 */
626struct RPS_SamplerRequestHandleSingleInfo *
627RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler,
628 RPS_sampler_sinlge_info_ready_cb cb,
629 void *cls)
630{
631 struct RPS_SamplerRequestHandleSingleInfo *req_handle;
632 struct GetPeerCls *gpc;
633
634 GNUNET_assert (0 != sampler->sampler_size);
635
636 // TODO check if we have too much (distinct) sampled peers
637 req_handle = GNUNET_new (struct RPS_SamplerRequestHandleSingleInfo);
638 req_handle->id = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity));
639 req_handle->sampler = sampler;
640 req_handle->callback = cb;
641 req_handle->cls = cls;
642 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_single_head,
643 sampler->req_handle_single_tail,
644 req_handle);
645
646 gpc = GNUNET_new (struct GetPeerCls);
647 gpc->req_handle = NULL;
648 gpc->req_single_info_handle = req_handle;
649 gpc->cont = check_peer_info_ready;
650 gpc->cont_cls = req_handle;
651 gpc->id = req_handle->id;
652
653 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
654 req_handle->gpc_tail,
655 gpc);
656 // maybe add a little delay
657 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
658 gpc);
659 return req_handle;
660}
661
662
663/**
664 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
665 *
666 * @param req_handle the handle to the request
667 */
668void
669RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
670{
671 struct GetPeerCls *i;
672
673 while (NULL != (i = req_handle->gpc_head))
674 {
675 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
676 req_handle->gpc_tail,
677 i);
678 if (NULL != i->get_peer_task)
679 {
680 GNUNET_SCHEDULER_cancel (i->get_peer_task);
681 }
682 if (NULL != i->notify_ctx)
683 {
684 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->notify_ctx_head,
685 req_handle->sampler->notify_ctx_tail,
686 i->notify_ctx);
687 GNUNET_free (i->notify_ctx);
688 i->notify_ctx = NULL;
689 }
690 GNUNET_free (i);
691 }
692 GNUNET_free (req_handle->ids);
693 req_handle->ids = NULL;
694 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
695 req_handle->sampler->req_handle_tail,
696 req_handle);
697 GNUNET_free (req_handle);
698}
699
700
701/**
702 * Cancel a request issued through #RPS_sampler_sinlge_info_ready_cb.
703 *
704 * @param req_handle the handle to the request
705 */
706void
707RPS_sampler_request_single_info_cancel (
708 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle)
709{
710 struct GetPeerCls *i;
711
712 while (NULL != (i = req_single_info_handle->gpc_head))
713 {
714 GNUNET_CONTAINER_DLL_remove (req_single_info_handle->gpc_head,
715 req_single_info_handle->gpc_tail,
716 i);
717 if (NULL != i->get_peer_task)
718 {
719 GNUNET_SCHEDULER_cancel (i->get_peer_task);
720 }
721 if (NULL != i->notify_ctx)
722 {
723 GNUNET_CONTAINER_DLL_remove (
724 req_single_info_handle->sampler->notify_ctx_head,
725 req_single_info_handle->sampler->
726 notify_ctx_tail,
727 i->notify_ctx);
728 GNUNET_free (i->notify_ctx);
729 i->notify_ctx = NULL;
730 }
731 GNUNET_free (i);
732 }
733 GNUNET_free (req_single_info_handle->id);
734 req_single_info_handle->id = NULL;
735 GNUNET_CONTAINER_DLL_remove (
736 req_single_info_handle->sampler->req_handle_single_head,
737 req_single_info_handle->sampler->
738 req_handle_single_tail,
739 req_single_info_handle);
740 GNUNET_free (req_single_info_handle);
741}
742
743
744/**
745 * Cleans the sampler.
746 */
747void
748RPS_sampler_destroy (struct RPS_Sampler *sampler)
749{
750 if (NULL != sampler->req_handle_head)
751 {
752 LOG (GNUNET_ERROR_TYPE_WARNING,
753 "There are still pending requests. Going to remove them.\n");
754 while (NULL != sampler->req_handle_head)
755 {
756 RPS_sampler_request_cancel (sampler->req_handle_head);
757 }
758 }
759 sampler_empty (sampler);
760 GNUNET_free (sampler);
761}
762
763
764/* end of rps-sampler_common.c */
diff --git a/src/rps/rps-sampler_common.h b/src/rps/rps-sampler_common.h
deleted file mode 100644
index c60d37217..000000000
--- a/src/rps/rps-sampler_common.h
+++ /dev/null
@@ -1,391 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/rps-sampler_common.h
23 * @brief Code common to client and service sampler
24 * @author Julius Bünger
25 */
26
27#ifndef RPS_SAMPLER_COMMON_H
28#define RPS_SAMPLER_COMMON_H
29
30#include "platform.h"
31#include "gnunet_util_lib.h"
32#include "gnunet_statistics_service.h"
33
34#include "gnunet-service-rps_sampler_elem.h"
35
36#include <math.h>
37#include <inttypes.h>
38
39#include "rps-test_util.h"
40
41
42/**
43 * Callback that is called from _get_rand_peer() when the PeerID is ready.
44 *
45 * @param cls the closure given alongside this function.
46 * @param id the PeerID that was returned
47 * @param probability The probability with which this sampler has seen all ids
48 * @param num_observed How many ids this sampler has observed
49 */
50typedef void
51(*RPS_sampler_rand_peer_ready_cont) (void *cls,
52 const struct GNUNET_PeerIdentity *id,
53 double probability,
54 uint32_t num_observed);
55
56
57/**
58 * Type of function used to differentiate between modified and not modified
59 * Sampler.
60 */
61typedef void
62(*RPS_get_peers_type) (void *cls);
63
64
65/**
66 * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
67 *
68 * @param cls the closure given alongside this function.
69 * @param ids the PeerIDs that were returned
70 * to be freed
71 */
72typedef void
73(*RPS_sampler_n_rand_peers_ready_cb) (const struct GNUNET_PeerIdentity *ids,
74 uint32_t num_peers,
75 void *cls);
76
77
78/**
79 * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
80 *
81 * @param cls the closure given alongside this function.
82 * @param probability Probability with which all IDs have been observed
83 * @param num_observed Number of observed IDs
84 * @param ids the PeerIDs that were returned
85 * to be freed
86 */
87typedef void
88(*RPS_sampler_sinlge_info_ready_cb) (const struct GNUNET_PeerIdentity *ids,
89 void *cls,
90 double probability,
91 uint32_t num_observed);
92
93
94/**
95 * @brief Callback called each time a new peer was put into the sampler
96 *
97 * @param cls A possibly given closure
98 */
99typedef void
100(*SamplerNotifyUpdateCB) (void *cls);
101
102
103/**
104 * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
105 */
106struct GetPeerCls
107{
108 /**
109 * DLL
110 */
111 struct GetPeerCls *next;
112 struct GetPeerCls *prev;
113
114 /**
115 * The #RPS_SamplerRequestHandle this single request belongs to.
116 */
117 struct RPS_SamplerRequestHandle *req_handle;
118
119 /**
120 * The #RPS_SamplerRequestHandleSingleInfo this single request belongs to.
121 */
122 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle;
123
124 /**
125 * The task for this function.
126 */
127 struct GNUNET_SCHEDULER_Task *get_peer_task;
128
129 /**
130 * @brief Context to the given callback.
131 */
132 struct SamplerNotifyUpdateCTX *notify_ctx;
133
134 /**
135 * The callback
136 */
137 RPS_sampler_rand_peer_ready_cont cont;
138
139 /**
140 * The closure to the callback @e cont
141 */
142 void *cont_cls;
143
144 /**
145 * The address of the id to be stored at
146 */
147 struct GNUNET_PeerIdentity *id;
148};
149
150
151/**
152 * Sampler with its own array of SamplerElements
153 */
154struct RPS_Sampler
155{
156 /**
157 * Number of sampler elements we hold.
158 */
159 unsigned int sampler_size;
160 // size_t size;
161
162 /**
163 * All sampler elements in one array.
164 */
165 struct RPS_SamplerElement **sampler_elements;
166
167 /**
168 * Maximum time a round takes
169 *
170 * Used in the context of RPS
171 */
172 struct GNUNET_TIME_Relative max_round_interval;
173
174 /**
175 * @brief The estimated total number of peers in the network
176 */
177 uint32_t num_peers_estim;
178
179 /**
180 * @brief The desired probability with which we want to have observed all
181 * peers.
182 */
183 double desired_probability;
184
185 /**
186 * @brief A factor that catches the 'bias' of a random stream of peer ids.
187 *
188 * As introduced by Brahms: Factor between the number of unique ids in a
189 * truly random stream and number of unique ids in the gossip stream.
190 */
191 double deficiency_factor;
192
193 /**
194 * Stores the function to return peers. Which one it is depends on whether
195 * the Sampler is the modified one or not.
196 */
197 RPS_get_peers_type get_peers;
198
199 /**
200 * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
201 */
202 struct RPS_SamplerRequestHandle *req_handle_head;
203 struct RPS_SamplerRequestHandle *req_handle_tail;
204
205 /**
206 * Head and tail for the DLL to store the #RPS_SamplerRequestHandleSingleInfo
207 */
208 struct RPS_SamplerRequestHandleSingleInfo *req_handle_single_head;
209 struct RPS_SamplerRequestHandleSingleInfo *req_handle_single_tail;
210
211 struct SamplerNotifyUpdateCTX *notify_ctx_head;
212 struct SamplerNotifyUpdateCTX *notify_ctx_tail;
213};
214
215
216/**
217 * @brief Update the current estimate of the network size stored at the sampler
218 *
219 * Used for computing the condition when to return elements to the client
220 *
221 * @param sampler The sampler to update
222 * @param num_peers The estimated value
223 */
224void
225RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler,
226 uint32_t num_peers);
227
228
229/**
230 * @brief Set the probability that is needed at least with what a sampler
231 * element has to have observed all elements from the network.
232 *
233 * Only used/useful with the client sampler
234 * (Maybe move to rps-sampler_client.{h|c} ?)
235 *
236 * @param sampler
237 * @param desired_probability
238 */
239void
240RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler,
241 double desired_probability);
242
243
244/**
245 * @brief Set the deficiency factor.
246 *
247 * Only used/useful with the client sampler
248 * (Maybe move to rps-sampler_client.{h|c} ?)
249 *
250 * @param sampler
251 * @param desired_probability
252 */
253void
254RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
255 double deficiency_factor);
256
257
258/**
259 * @brief Add a callback that will be called when the next peer is inserted
260 * into the sampler
261 *
262 * @param sampler The sampler on which update it will be called
263 * @param notify_cb The callback
264 * @param cls Closure given to the callback
265 *
266 * @return The context containing callback and closure
267 */
268struct SamplerNotifyUpdateCTX *
269sampler_notify_on_update (struct RPS_Sampler *sampler,
270 SamplerNotifyUpdateCB notify_cb,
271 void *cls);
272
273
274/**
275 * Update every sampler element of this sampler with given peer
276 *
277 * @param sampler the sampler to update.
278 * @param id the PeerID that is put in the sampler
279 */
280void
281RPS_sampler_update (struct RPS_Sampler *sampler,
282 const struct GNUNET_PeerIdentity *id);
283
284
285/**
286 * Reinitialise all previously initialised sampler elements with the given value.
287 *
288 * Used to get rid of a PeerID.
289 *
290 * @param sampler the sampler to reinitialise a sampler element in.
291 * @param id the id of the sampler elements to update.
292 */
293void
294RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
295 const struct GNUNET_PeerIdentity *id);
296
297
298/**
299 * Get the size of the sampler.
300 *
301 * @param sampler the sampler to return the size of.
302 * @return the size of the sampler
303 */
304unsigned int
305RPS_sampler_get_size (struct RPS_Sampler *sampler);
306
307
308/**
309 * Grow or shrink the size of the sampler.
310 *
311 * @param sampler the sampler to resize.
312 * @param new_size the new size of the sampler
313 */
314void
315RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
316
317
318/**
319 * Get n random peers out of the sampled peers.
320 *
321 * We might want to reinitialise this sampler after giving the
322 * corrsponding peer to the client.
323 * Random with or without consumption?
324 *
325 * @param sampler the sampler to get peers from.
326 * @param cb callback that will be called once the ids are ready.
327 * @param cls closure given to @a cb
328 * @param for_client #GNUNET_YES if result is used for client,
329 * #GNUNET_NO if used internally
330 * @param num_peers the number of peers requested
331 */
332struct RPS_SamplerRequestHandle *
333RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
334 uint32_t num_peers,
335 RPS_sampler_n_rand_peers_ready_cb cb,
336 void *cls);
337
338
339/**
340 * Get one random peer with additional information.
341 *
342 * @param sampler the sampler to get peers from.
343 * @param cb callback that will be called once the ids are ready.
344 * @param cls closure given to @a cb
345 */
346struct RPS_SamplerRequestHandleSingleInfo *
347RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler,
348 RPS_sampler_sinlge_info_ready_cb cb,
349 void *cls);
350
351
352/**
353 * Counts how many Samplers currently hold a given PeerID.
354 *
355 * @param sampler the sampler to count ids in.
356 * @param id the PeerID to count.
357 *
358 * @return the number of occurrences of id.
359 */
360uint32_t
361RPS_sampler_count_id (struct RPS_Sampler *sampler,
362 const struct GNUNET_PeerIdentity *id);
363
364
365/**
366 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
367 *
368 * @param req_handle the handle to the request
369 */
370void
371RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
372
373
374/**
375 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
376 *
377 * @param req_handle the handle to the request
378 */
379void
380RPS_sampler_request_single_info_cancel (
381 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle);
382
383
384/**
385 * Cleans the sampler.
386 */
387void
388RPS_sampler_destroy (struct RPS_Sampler *sampler);
389
390#endif /* RPS_SAMPLER_COMMON_H */
391/* end of rps-sampler_common.h */
diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c
deleted file mode 100644
index 8fd82f7b3..000000000
--- a/src/rps/rps-test_util.c
+++ /dev/null
@@ -1,550 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/rps-test_util.c
23 * @brief Some utils facilitating the view into the internals for the sampler
24 * needed for evaluation
25 *
26 * @author Julius Bünger
27 */
28
29#include "platform.h"
30#include "gnunet_util_lib.h"
31#include "rps-test_util.h"
32
33#include <inttypes.h>
34
35#define LOG(kind, ...) GNUNET_log_from (kind, "rps-test_util", __VA_ARGS__)
36
37#define B2B_PAT "%c%c%c%c%c%c%c%c"
38#define B2B(byte) \
39 (byte & 0x80 ? '1' : '0'), \
40 (byte & 0x40 ? '1' : '0'), \
41 (byte & 0x20 ? '1' : '0'), \
42 (byte & 0x10 ? '1' : '0'), \
43 (byte & 0x08 ? '1' : '0'), \
44 (byte & 0x04 ? '1' : '0'), \
45 (byte & 0x02 ? '1' : '0'), \
46 (byte & 0x01 ? '1' : '0')
47
48#ifdef TO_FILE
49
50
51/**
52 * @brief buffer for storing the unaligned bits for the next write
53 */
54static char buf_unaligned;
55
56/**
57 * @brief number of bits in unaligned buffer
58 */
59static unsigned num_bits_buf_unaligned;
60
61static struct GNUNET_CONTAINER_MultiHashMap *open_files;
62
63
64/**
65 * @brief Get file handle
66 *
67 * If necessary, create file handle and store it with the other file handles.
68 *
69 * @param name Name of the file
70 *
71 * @return File handle
72 */
73struct GNUNET_DISK_FileHandle *
74get_file_handle (const char *name)
75{
76 struct GNUNET_HashCode hash;
77 struct GNUNET_DISK_FileHandle *fh;
78
79 if (NULL == open_files)
80 {
81 open_files = GNUNET_CONTAINER_multihashmap_create (16,
82 GNUNET_NO);
83 LOG (GNUNET_ERROR_TYPE_DEBUG,
84 "Created map of open files.\n");
85 }
86 GNUNET_CRYPTO_hash (name,
87 strlen (name),
88 &hash);
89 if (NULL != (fh = GNUNET_CONTAINER_multihashmap_get (open_files,
90 &hash)))
91 return fh;
92 fh = GNUNET_DISK_file_open (name,
93 GNUNET_DISK_OPEN_WRITE
94 | GNUNET_DISK_OPEN_CREATE
95 | GNUNET_DISK_OPEN_APPEND,
96 GNUNET_DISK_PERM_USER_READ
97 | GNUNET_DISK_PERM_USER_WRITE
98 | GNUNET_DISK_PERM_GROUP_READ);
99 if (NULL == fh)
100 {
101 LOG (GNUNET_ERROR_TYPE_ERROR,
102 "Opening file `%s' failed.\n",
103 name);
104 GNUNET_assert (0);
105 }
106 GNUNET_assert (GNUNET_YES ==
107 GNUNET_CONTAINER_multihashmap_put (open_files,
108 &hash,
109 fh,
110 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
111 return fh;
112}
113
114
115/**
116 * @brief Closes the file of the current entry
117 *
118 * Implements #GNUNET_CONTAINER_HashMapIterator
119 *
120 * @param cls unused
121 * @param key unused
122 * @param value the file handle
123 *
124 * @return #GNUNET_YES if we should continue to
125 * iterate,
126 * #GNUNET_NO if not.
127 */
128int
129close_files_iter (void *cls,
130 const struct GNUNET_HashCode *key,
131 void *value)
132{
133 (void) cls;
134 (void) key;
135 struct GNUNET_DISK_FileHandle *fh = value;
136
137 if (NULL != fh)
138 {
139 GNUNET_DISK_file_close (fh);
140 }
141 return GNUNET_YES;
142}
143
144
145/**
146 * @brief Close all files that were opened with #get_file_handle
147 *
148 * @return Success of iterating over files
149 */
150int
151close_all_files ()
152{
153 int ret;
154
155 if (NULL != open_files)
156 {
157 ret = GNUNET_CONTAINER_multihashmap_iterate (open_files,
158 close_files_iter,
159 NULL);
160 GNUNET_CONTAINER_multihashmap_destroy (open_files);
161 open_files = NULL;
162 return ret;
163 }
164 return GNUNET_YES;
165}
166
167
168void
169to_file_raw (const char *file_name, const char *buf, size_t size_buf)
170{
171 struct GNUNET_DISK_FileHandle *f;
172 size_t size_written;
173
174 if (NULL == (f = GNUNET_DISK_file_open (file_name,
175 GNUNET_DISK_OPEN_APPEND
176 | GNUNET_DISK_OPEN_WRITE
177 | GNUNET_DISK_OPEN_CREATE,
178 GNUNET_DISK_PERM_USER_READ
179 | GNUNET_DISK_PERM_USER_WRITE
180 | GNUNET_DISK_PERM_GROUP_READ
181 | GNUNET_DISK_PERM_OTHER_READ)))
182 {
183 LOG (GNUNET_ERROR_TYPE_WARNING,
184 "Not able to open file %s\n",
185 file_name);
186 return;
187 }
188
189 size_written = GNUNET_DISK_file_write (f, buf, size_buf);
190 if (size_buf != size_written)
191 {
192 LOG (GNUNET_ERROR_TYPE_WARNING,
193 "Unable to write to file! (Size: %llu, size_written: %llu)\n",
194 (unsigned long long) size_buf,
195 (unsigned long long) size_written);
196
197 if (GNUNET_YES != GNUNET_DISK_file_close (f))
198 LOG (GNUNET_ERROR_TYPE_WARNING,
199 "Unable to close file\n");
200
201 return;
202 }
203 LOG (GNUNET_ERROR_TYPE_WARNING,
204 "Wrote %llu bytes raw.\n",
205 (unsigned long long) size_written);
206 if (GNUNET_YES != GNUNET_DISK_file_close (f))
207 LOG (GNUNET_ERROR_TYPE_WARNING,
208 "Unable to close file\n");
209}
210
211
212void
213to_file_raw_unaligned (const char *file_name,
214 const char *buf,
215 size_t size_buf,
216 unsigned bits_needed)
217{
218 // TODO endianness!
219 GNUNET_assert (size_buf >= (bits_needed / 8));
220 // if (0 == num_bits_buf_unaligned)
221 // {
222 // if (0 == (bits_needed % 8))
223 // {
224 // to_file_raw (file_name, buf, size_buf);
225 // return;
226 // }
227 // to_file_raw (file_name, buf, size_buf - 1);
228 // buf_unaligned = buf[size_buf - 1];
229 // num_bits_buf_unaligned = bits_needed % 8;
230 // return;
231 // }
232 LOG (GNUNET_ERROR_TYPE_DEBUG,
233 "Was asked to write %u bits\n", bits_needed);
234
235 char buf_write[size_buf + 1];
236 const unsigned bytes_iter = (0 != bits_needed % 8 ?
237 (bits_needed / 8) + 1 :
238 bits_needed / 8);
239 // TODO what if no iteration happens?
240 unsigned size_buf_write = 0;
241 LOG (GNUNET_ERROR_TYPE_DEBUG,
242 "num_bits_buf_unaligned: %u\n",
243 num_bits_buf_unaligned);
244 LOG (GNUNET_ERROR_TYPE_DEBUG,
245 "ua args: size_buf: %llu, bits_needed: %u -> iter: %u\n",
246 (unsigned long long) size_buf,
247 bits_needed,
248 bytes_iter);
249 buf_write[0] = buf_unaligned;
250 /* Iterate over input bytes */
251 for (unsigned i = 0; i < bytes_iter; i++)
252 {
253 /* Number of bits needed in this iteration - 8 for all except last iter */
254 unsigned num_bits_needed_iter;
255 /* Mask for bits to actually use */
256 unsigned mask_bits_needed_iter;
257 char byte_input;
258 /* Number of bits needed to align unaligned byte */
259 unsigned num_bits_to_align;
260 /* Number of bits that are to be moved */
261 unsigned num_bits_to_move;
262 /* Mask for bytes to be moved */
263 char mask_input_to_move;
264 /* Masked bits to be moved */
265 char bits_to_move;
266 /* The amount of bits needed to fit the bits to shift to the nearest spot */
267 unsigned distance_shift_bits;
268 /* Shifted bits on the move */
269 char bits_moving;
270 /* (unaligned) byte being filled with bits */
271 char byte_to_fill;
272 /* mask for needed bits of the input byte that have not been moved */
273 char mask_input_leftover;
274 /* needed bits of the input byte that have not been moved */
275 char byte_input_leftover;
276 unsigned num_bits_leftover;
277 // unsigned num_bits_discard;
278 char byte_unaligned_new;
279
280 if ((bits_needed - (i * 8)) <= 8)
281 {
282 /* last iteration */
283 num_bits_needed_iter = bits_needed - (i * 8);
284 }
285 else
286 {
287 num_bits_needed_iter = 8;
288 }
289 LOG (GNUNET_ERROR_TYPE_DEBUG,
290 "number of bits needed in this iteration: %u\n",
291 num_bits_needed_iter);
292 mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
293 LOG (GNUNET_ERROR_TYPE_DEBUG,
294 "mask needed bits (current iter): "B2B_PAT "\n",
295 B2B (mask_bits_needed_iter));
296 LOG (GNUNET_ERROR_TYPE_DEBUG,
297 "Unaligned byte: "B2B_PAT " (%u bits)\n",
298 B2B (buf_unaligned),
299 num_bits_buf_unaligned);
300 byte_input = buf[i];
301 LOG (GNUNET_ERROR_TYPE_DEBUG,
302 "next whole input byte: "B2B_PAT "\n",
303 B2B (byte_input));
304 byte_input &= mask_bits_needed_iter;
305 num_bits_to_align = 8 - num_bits_buf_unaligned;
306 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 "input byte, needed bits: "B2B_PAT "\n",
308 B2B (byte_input));
309 LOG (GNUNET_ERROR_TYPE_DEBUG,
310 "number of bits needed to align unaligned bit: %u\n",
311 num_bits_to_align);
312 num_bits_to_move = GNUNET_MIN (num_bits_to_align, num_bits_needed_iter);
313 LOG (GNUNET_ERROR_TYPE_DEBUG,
314 "number of bits of new byte to move: %u\n",
315 num_bits_to_move);
316 mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "mask of bits of new byte to take for moving: "B2B_PAT "\n",
319 B2B (mask_input_to_move));
320 bits_to_move = byte_input & mask_input_to_move;
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "masked bits of new byte to take for moving: "B2B_PAT "\n",
323 B2B (bits_to_move));
324 distance_shift_bits = num_bits_buf_unaligned;
325 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 "distance needed to shift bits to their correct spot: %u\n",
327 distance_shift_bits);
328 bits_moving = bits_to_move << distance_shift_bits;
329 LOG (GNUNET_ERROR_TYPE_DEBUG,
330 "shifted, masked bits of new byte being moved: "B2B_PAT "\n",
331 B2B (bits_moving));
332 byte_to_fill = buf_unaligned | bits_moving;
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "byte being filled: "B2B_PAT "\n",
335 B2B (byte_to_fill));
336 LOG (GNUNET_ERROR_TYPE_DEBUG,
337 "pending bytes: %u\n",
338 num_bits_buf_unaligned + num_bits_needed_iter);
339 if (num_bits_buf_unaligned + num_bits_needed_iter >= 8)
340 {
341 /* buf_unaligned was aligned by filling
342 * -> can be written to storage */
343 buf_write[i] = byte_to_fill;
344 size_buf_write++;
345
346 /* store the leftover, unaligned bits in buffer */
347 mask_input_leftover = mask_bits_needed_iter & (~mask_input_to_move);
348 LOG (GNUNET_ERROR_TYPE_DEBUG,
349 "mask of leftover bits of new byte: "B2B_PAT "\n",
350 B2B (mask_input_leftover));
351 byte_input_leftover = byte_input & mask_input_leftover;
352 LOG (GNUNET_ERROR_TYPE_DEBUG,
353 "masked, leftover bits of new byte: "B2B_PAT "\n",
354 B2B (byte_input_leftover));
355 num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
356 LOG (GNUNET_ERROR_TYPE_DEBUG,
357 "number of unaligned bits left: %u\n",
358 num_bits_leftover);
359 // num_bits_discard = 8 - num_bits_needed_iter;
360 byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
361 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "new unaligned byte: "B2B_PAT "\n",
363 B2B (byte_unaligned_new));
364 buf_unaligned = byte_unaligned_new;
365 num_bits_buf_unaligned = num_bits_leftover % 8;
366 }
367 else
368 {
369 /* unaligned buffer still unaligned but 'fuller' */
370 buf_unaligned = byte_to_fill;
371 num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
372 }
373 }
374 to_file_raw (file_name, buf_write, size_buf_write);
375 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
376}
377
378
379char *
380auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
381{
382 int size;
383 size_t name_buf_size;
384 char *end;
385 char *buf;
386 char *name_buf;
387 size_t keylen = (sizeof(struct GNUNET_CRYPTO_AuthKey)) * 8;
388
389 name_buf_size = 512 * sizeof(char);
390 name_buf = GNUNET_malloc (name_buf_size);
391
392 if (keylen % 5 > 0)
393 keylen += 5 - keylen % 5;
394 keylen /= 5;
395 buf = GNUNET_malloc (keylen + 1);
396
397 end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
398 sizeof(struct GNUNET_CRYPTO_AuthKey),
399 buf,
400 keylen);
401
402 if (NULL == end)
403 {
404 GNUNET_free (buf);
405 GNUNET_break (0);
406 }
407 else
408 {
409 *end = '\0';
410 }
411
412 size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
413 if (0 > size)
414 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
415
416 GNUNET_free (buf);
417
418 return name_buf;
419}
420
421
422#endif /* TO_FILE */
423
424
425struct GNUNET_CRYPTO_AuthKey
426string_to_auth_key (const char *str)
427{
428 struct GNUNET_CRYPTO_AuthKey auth_key;
429
430 if (GNUNET_OK !=
431 GNUNET_STRINGS_string_to_data (str,
432 strlen (str),
433 &auth_key.key,
434 sizeof(struct GNUNET_CRYPTO_AuthKey)))
435 {
436 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
437 }
438
439 return auth_key;
440}
441
442
443/**
444 * @brief Try to ensure that `/tmp/rps` exists.
445 *
446 * @return #GNUNET_YES on success
447 * #GNUNET_SYSERR on failure
448 */
449static int
450ensure_folder_exist (void)
451{
452 if (GNUNET_OK !=
453 GNUNET_DISK_directory_create ("/tmp/rps"))
454 {
455 LOG (GNUNET_ERROR_TYPE_ERROR,
456 "Could not create directory `/tmp/rps'\n");
457 return GNUNET_SYSERR;
458 }
459 return GNUNET_YES;
460}
461
462
463char *
464store_prefix_file_name (const unsigned int index,
465 const char *prefix)
466{
467 int len_file_name;
468 int out_size;
469 char *file_name;
470 char index_str[64];
471
472 if (GNUNET_SYSERR == ensure_folder_exist ())
473 return NULL;
474 out_size = GNUNET_snprintf (index_str,
475 64,
476 "%u",
477 index);
478 if ((64 < out_size) ||
479 (0 > out_size) )
480 {
481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
482 "Failed to write string to buffer (size: %i, out_size: %i)\n",
483 64,
484 out_size);
485 }
486 len_file_name = (strlen (prefix)
487 + strlen (index_str)
488 + 11)
489 * sizeof(char);
490 file_name = GNUNET_malloc (len_file_name);
491 out_size = GNUNET_snprintf (file_name,
492 len_file_name,
493 "/tmp/rps/%s-%s",
494 prefix,
495 index_str);
496 if ((len_file_name < out_size) ||
497 (0 > out_size) )
498 {
499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
500 "Failed to write string to buffer (size: %i, out_size: %i)\n",
501 len_file_name,
502 out_size);
503 }
504 return file_name;
505}
506
507
508/**
509 * @brief Factorial
510 *
511 * @param x Number of which to compute the factorial
512 *
513 * @return Factorial of @a x
514 */
515uint32_t
516fac (uint32_t x)
517{
518 if (1 >= x)
519 {
520 return x;
521 }
522 return x * fac (x - 1);
523}
524
525
526/**
527 * @brief Binomial coefficient (n choose k)
528 *
529 * @param n
530 * @param k
531 *
532 * @return Binomial coefficient of @a n and @a k
533 */
534uint32_t
535binom (uint32_t n, uint32_t k)
536{
537 // GNUNET_assert (n >= k);
538 if (k > n)
539 return 0;
540 /* if (0 > n) return 0; - always false */
541 /* if (0 > k) return 0; - always false */
542 if (0 == k)
543 return 1;
544 return fac (n)
545 /
546 fac (k) * fac (n - k);
547}
548
549
550/* end of gnunet-service-rps.c */
diff --git a/src/rps/rps-test_util.h b/src/rps/rps-test_util.h
deleted file mode 100644
index 9ff13bdcf..000000000
--- a/src/rps/rps-test_util.h
+++ /dev/null
@@ -1,144 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/rps-test_util.h
23 * @brief Some utils facilitating the view into the internals for the sampler
24 * needed for evaluation
25 * @author Julius Bünger
26 */
27
28#ifndef RPS_TEST_UTIL_H
29#define RPS_TEST_UTIL_H
30
31#define TO_FILE 0
32
33
34char *
35auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key);
36
37struct GNUNET_CRYPTO_AuthKey
38string_to_auth_key (const char *str);
39
40
41/**
42 * @brief Get file handle
43 *
44 * If necessary, create file handle and store it with the other file handles.
45 *
46 * @param name Name of the file
47 *
48 * @return File handle
49 */
50struct GNUNET_DISK_FileHandle *
51get_file_handle (const char *name);
52
53/**
54 * @brief Close all files that were opened with #get_file_handle
55 *
56 * @return Success of iterating over files
57 */
58int
59close_all_files ();
60
61/**
62 * This function is used to facilitate writing important information to disk
63 */
64#ifdef TO_FILE
65#define to_file(file_name, ...) do { \
66 char tmp_buf[512] = ""; \
67 int size; \
68 if (NULL == file_name) break; \
69 size = GNUNET_snprintf (tmp_buf, sizeof(tmp_buf), __VA_ARGS__); \
70 if (0 > size) \
71 { \
72 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \
73 "Failed to create tmp_buf\n"); \
74 break; \
75 } \
76 (void) strncat (tmp_buf, "\n", 511); \
77 GNUNET_DISK_file_write (get_file_handle (file_name), \
78 tmp_buf, \
79 strnlen (tmp_buf, 512)); \
80} while (0);
81
82
83#define to_file_w_len(file_name, len, ...) do { char tmp_buf [len]; \
84 int size; \
85 memset (tmp_buf, 0, len); \
86 size = GNUNET_snprintf (tmp_buf, \
87 sizeof( \
88 tmp_buf), \
89 __VA_ARGS__); \
90 if (0 > size) \
91 { \
92 GNUNET_log ( \
93 GNUNET_ERROR_TYPE_WARNING, \
94 "Failed to create tmp_buf\n"); \
95 break; \
96 } \
97 (void) strncat (tmp_buf, "\n", 2); \
98 GNUNET_DISK_file_write ( \
99 get_file_handle (file_name), \
100 tmp_buf, \
101 strnlen ( \
102 tmp_buf, len)); \
103} while (0);
104#else /* TO_FILE */
105# define to_file(file_name, ...)
106# define to_file_w_len(file_name, len, ...)
107#endif /* TO_FILE */
108
109char *
110store_prefix_file_name (const unsigned int index,
111 const char *prefix);
112
113void
114to_file_raw (const char *file_name, const char *buf, size_t size_buf);
115
116void
117to_file_raw_unaligned (const char *file_name,
118 const char *buf,
119 size_t size_buf,
120 unsigned bits_needed);
121
122
123/**
124 * @brief Factorial
125 *
126 * @param x Number of which to compute the factorial
127 *
128 * @return Factorial of @a x
129 */
130uint32_t fac (uint32_t x);
131
132
133/**
134 * @brief Binomial coefficient (n choose k)
135 *
136 * @param n
137 * @param k
138 *
139 * @return Binomial coefficient of @a n and @a k
140 */
141uint32_t binom (uint32_t n, uint32_t k);
142
143#endif /* RPS_TEST_UTIL_H */
144/* end of gnunet-service-rps.c */
diff --git a/src/rps/rps.conf.in b/src/rps/rps.conf.in
deleted file mode 100644
index 8c7098747..000000000
--- a/src/rps/rps.conf.in
+++ /dev/null
@@ -1,38 +0,0 @@
1[rps]
2START_ON_DEMAND = @START_ON_DEMAND@
3IMMEDIATE_START = YES
4BINARY = gnunet-service-rps
5UNIXPATH = $GNUNET_RUNTIME_DIR/gnunet-service-rps.sock
6HOME = $SERVICEHOME
7UNIX_MATCH_UID = NO
8UNIX_MATCH_GID = YES
9HOSTNAME = localhost
10ACCEPT_FROM = 127.0.0.1;
11ACCEPT_FROM6 = ::1;
12# PORT = 2119
13@UNIXONLY@ PORT = 2119
14
15# This is the timeinterval between the rounds
16ROUNDINTERVAL = 30 s
17
18# This is the file in which valid peers are stored
19FILENAME_VALID_PEERS = $GNUNET_DATA_HOME/rps/valid_peers.txt
20# Disable storage of valid peers
21#FILENAME_VALID_PEERS = DISABLE
22
23# This is the 'estimate' in the beginning.
24# This determines the size of the peers we keep in memory
25# until we receive the first estimate from NSE.
26# Keep in mind, that (networksize)^(1/3) should be enough.
27# So, 50 is enough for a network of size 50^3 = 125000
28MINSIZE = 10
29
30# The probability with which we want a sampler element to have observed all
31# peer ids in the network at least
32DESIRED_PROBABILITY = 0.9
33
34# A factor that catches the 'bias' of a random stream of peer ids.
35#
36# As introduced by Brahms: Factor between the number of unique ids in a
37# truly random stream and number of unique ids in the gossip stream.
38DEFICIENCY_FACTOR = 0.4
diff --git a/src/rps/rps.h b/src/rps/rps.h
deleted file mode 100644
index 202f881c9..000000000
--- a/src/rps/rps.h
+++ /dev/null
@@ -1,336 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2012-2013 GNUnet e.V.
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file rps/rps.h
22 * @brief example IPC messages between RPS API and GNS service
23 * @author Julius Bünger
24 */
25
26#include "gnunet_rps_service.h"
27
28/**
29 * Mesh port used by RPS.
30 */
31#define GNUNET_RPS_CADET_PORT 31337
32
33
34GNUNET_NETWORK_STRUCT_BEGIN
35
36/***********************************************************************
37* P2P Messages
38***********************************************************************/
39
40/**
41 * P2P Message to send PeerIDs to other peer.
42 */
43struct GNUNET_RPS_P2P_PullReplyMessage
44{
45 /**
46 * Header including size and type in NBO
47 */
48 struct GNUNET_MessageHeader header;
49
50 /**
51 * Number of PeerIDs sent
52 */
53 uint32_t num_peers GNUNET_PACKED;
54
55 /* Followed by num_peers * GNUNET_PeerIdentity */
56};
57
58
59/***********************************************************************
60* Client-Service Messages
61***********************************************************************/
62
63/**
64 * Message from client to service with seed of peers.
65 */
66struct GNUNET_RPS_CS_SeedMessage
67{
68 /**
69 * Header including size and type in NBO
70 */
71 struct GNUNET_MessageHeader header;
72
73 /**
74 * Number of peers
75 */
76 uint32_t num_peers GNUNET_PACKED;
77
78 /* Followed by num_peers * GNUNET_PeerIdentity */
79};
80
81#if ENABLE_MALICIOUS
82/**
83 * Message from client to service to turn service malicious.
84 */
85struct GNUNET_RPS_CS_ActMaliciousMessage
86{
87 /**
88 * Header including size and type in NBO
89 */
90 struct GNUNET_MessageHeader header;
91
92 /**
93 * If the type is 2 this is the attacked peer,
94 * empty otherwise.
95 */
96 struct GNUNET_PeerIdentity attacked_peer;
97
98 /**
99 * Type of malicious behaviour.
100 *
101 * 0 No malicious behaviour at all
102 * 1 Try to maximise representation
103 * 2 Try to partition the network
104 */
105 uint32_t type GNUNET_PACKED;
106
107 /**
108 * Number of peers
109 */
110 uint32_t num_peers GNUNET_PACKED;
111
112 /* Followed by num_peers * GNUNET_PeerIdentity when the type of malicious
113 behaviour is 1 */
114};
115#endif /* ENABLE_MALICIOUS */
116
117
118/**
119 * Message from client to service telling it to start a new sub
120 */
121struct GNUNET_RPS_CS_SubStartMessage
122{
123 /**
124 * Header including size and type in NBO
125 */
126 struct GNUNET_MessageHeader header;
127
128 /**
129 * For alignment.
130 */
131 uint32_t reserved GNUNET_PACKED;
132
133 /**
134 * Mean interval between two rounds
135 */
136 struct GNUNET_TIME_RelativeNBO round_interval;
137
138 /**
139 * Length of the shared value represented as string.
140 */
141 struct GNUNET_HashCode hash GNUNET_PACKED;
142};
143
144
145/**
146 * Message from client to service telling it to stop a new sub
147 */
148struct GNUNET_RPS_CS_SubStopMessage
149{
150 /**
151 * Header including size and type in NBO
152 */
153 struct GNUNET_MessageHeader header;
154
155 /**
156 * Length of the shared value represented as string.
157 */
158 struct GNUNET_HashCode hash GNUNET_PACKED;
159};
160
161
162/* Debug messages */
163
164/**
165 * Message from client to service indicating that
166 * clients wants to get updates of the view
167 */
168struct GNUNET_RPS_CS_DEBUG_ViewRequest
169{
170 /**
171 * Header including size and type in NBO
172 */
173 struct GNUNET_MessageHeader header;
174
175 /**
176 * Number of updates
177 * 0 for sending updates until cancellation
178 */
179 uint32_t num_updates GNUNET_PACKED;
180};
181
182/**
183 * Message from service to client containing current update of view
184 */
185struct GNUNET_RPS_CS_DEBUG_ViewReply
186{
187 /**
188 * Header including size and type in NBO
189 */
190 struct GNUNET_MessageHeader header;
191
192 /**
193 * Identifyer of the message.
194 */
195 uint32_t id GNUNET_PACKED;
196
197 /**
198 * Number of peers in the view
199 */
200 uint64_t num_peers GNUNET_PACKED;
201};
202/* Followed by num_peers * GNUNET_PeerIdentity */
203
204/**
205 * Message from client to service indicating that
206 * clients wants to get stream of biased peers
207 */
208struct GNUNET_RPS_CS_DEBUG_StreamRequest
209{
210 /**
211 * Header including size and type in NBO
212 */
213 struct GNUNET_MessageHeader header;
214};
215
216/**
217 * Message from service to client containing peer from biased stream
218 */
219struct GNUNET_RPS_CS_DEBUG_StreamReply
220{
221 /**
222 * Header including size and type in NBO
223 */
224 struct GNUNET_MessageHeader header;
225
226 /**
227 * Number of peers
228 */
229 uint64_t num_peers GNUNET_PACKED;
230
231 // TODO maybe source of peer (pull/push list, peerinfo, ...)
232
233 /* Followed by num_peers * GNUNET_PeerIdentity */
234};
235
236GNUNET_NETWORK_STRUCT_END
237
238/***********************************************************************
239* Defines from old gnunet-service-rps_peers.h
240***********************************************************************/
241
242/**
243 * Different flags indicating the status of another peer.
244 */
245enum Peers_PeerFlags
246{
247 /**
248 * If we are waiting for a reply from that peer (sent a pull request).
249 */
250 Peers_PULL_REPLY_PENDING = 0x01,
251
252 /* IN_OTHER_GOSSIP_LIST = 0x02, unneeded? */
253 /* IN_OWN_SAMPLER_LIST = 0x04, unneeded? */
254 /* IN_OWN_GOSSIP_LIST = 0x08, unneeded? */
255
256 /**
257 * We set this bit when we know the peer is online.
258 */
259 Peers_ONLINE = 0x20,
260
261 /**
262 * We set this bit when we are going to destroy the channel to this peer.
263 * When cleanup_channel is called, we know that we wanted to destroy it.
264 * Otherwise the channel to the other peer was destroyed.
265 */
266 Peers_TO_DESTROY = 0x40,
267};
268
269/**
270 * Keep track of the status of a channel.
271 *
272 * This is needed in order to know what to do with a channel when it's
273 * destroyed.
274 */
275enum Peers_ChannelFlags
276{
277 /**
278 * We destroyed the channel because the other peer established a second one.
279 */
280 Peers_CHANNEL_ESTABLISHED_TWICE = 0x1,
281
282 /**
283 * The channel was removed because it was not needed any more. This should be
284 * the sending channel.
285 */
286 Peers_CHANNEL_CLEAN = 0x2,
287
288 /**
289 * We destroyed the channel because the other peer established a second one.
290 */
291 Peers_CHANNEL_DESTROING = 0x4,
292};
293
294
295/**
296 * @brief The role of a channel. Sending or receiving.
297 */
298enum Peers_ChannelRole
299{
300 /**
301 * Channel is used for sending
302 */
303 Peers_CHANNEL_ROLE_SENDING = 0x01,
304
305 /**
306 * Channel is used for receiving
307 */
308 Peers_CHANNEL_ROLE_RECEIVING = 0x02,
309};
310
311/**
312 * @brief Functions of this type can be used to be stored at a peer for later execution.
313 *
314 * @param cls closure
315 * @param peer peer to execute function on
316 */
317typedef void (*PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
318
319/**
320 * @brief Iterator over valid peers.
321 *
322 * @param cls closure
323 * @param peer current public peer id
324 * @return #GNUNET_YES if we should continue to
325 * iterate,
326 * #GNUNET_NO if not.
327 */
328typedef int
329(*PeersIterator) (void *cls,
330 const struct GNUNET_PeerIdentity *peer);
331
332
333/**
334 * Handle to the statistics service.
335 */
336extern struct GNUNET_STATISTICS_Handle *stats;
diff --git a/src/rps/rps_api.c b/src/rps/rps_api.c
deleted file mode 100644
index bb4c3597e..000000000
--- a/src/rps/rps_api.c
+++ /dev/null
@@ -1,1320 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/rps_api.c
23 * @brief API for rps
24 * @author Julius Bünger
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "rps.h"
29#include "gnunet_rps_service.h"
30#include "rps-sampler_client.h"
31
32#include "gnunet_nse_service.h"
33
34#include <inttypes.h>
35
36#define LOG(kind, ...) GNUNET_log_from (kind, "rps-api", __VA_ARGS__)
37
38/**
39 * Handle for a request to get peers from biased stream of ids
40 */
41struct GNUNET_RPS_StreamRequestHandle
42{
43 /**
44 * The client issuing the request.
45 */
46 struct GNUNET_RPS_Handle *rps_handle;
47
48 /**
49 * The callback to be called when we receive an answer.
50 */
51 GNUNET_RPS_NotifyReadyCB ready_cb;
52
53 /**
54 * The closure for the callback.
55 */
56 void *ready_cb_cls;
57
58 /**
59 * @brief Scheduler task for scheduled callback
60 */
61 struct GNUNET_SCHEDULER_Task *callback_task;
62
63 /**
64 * @brief Next element of the DLL
65 */
66 struct GNUNET_RPS_StreamRequestHandle *next;
67
68 /**
69 * @brief Previous element of the DLL
70 */
71 struct GNUNET_RPS_StreamRequestHandle *prev;
72};
73
74
75/**
76 * Handler to handle requests from a client.
77 */
78struct GNUNET_RPS_Handle
79{
80 /**
81 * The handle to the client configuration.
82 */
83 const struct GNUNET_CONFIGURATION_Handle *cfg;
84
85 /**
86 * The message queue to the client.
87 */
88 struct GNUNET_MQ_Handle *mq;
89
90 /**
91 * @brief Callback called on each update of the view
92 */
93 GNUNET_RPS_NotifyReadyCB view_update_cb;
94
95 /**
96 * @brief Closure to each requested update of the view
97 */
98 void *view_update_cls;
99
100 /**
101 * @brief Closure to each requested peer from the biased stream
102 */
103 void *stream_input_cls;
104
105 /**
106 * @brief Head of the DLL of stream requests
107 */
108 struct GNUNET_RPS_StreamRequestHandle *stream_requests_head;
109
110 /**
111 * @brief Tail of the DLL of stream requests
112 */
113 struct GNUNET_RPS_StreamRequestHandle *stream_requests_tail;
114
115 /**
116 * @brief Handle to nse service
117 */
118 struct GNUNET_NSE_Handle *nse;
119
120 /**
121 * @brief Pointer to the head element in DLL of request handles
122 */
123 struct GNUNET_RPS_Request_Handle *rh_head;
124
125 /**
126 * @brief Pointer to the tail element in DLL of request handles
127 */
128 struct GNUNET_RPS_Request_Handle *rh_tail;
129
130 /**
131 * @brief Pointer to the head element in DLL of single request handles
132 */
133 struct GNUNET_RPS_Request_Handle_Single_Info *rhs_head;
134
135 /**
136 * @brief Pointer to the tail element in DLL of single request handles
137 */
138 struct GNUNET_RPS_Request_Handle_Single_Info *rhs_tail;
139
140 /**
141 * @brief The desired probability with which we want to have observed all
142 * peers.
143 */
144 float desired_probability;
145
146 /**
147 * @brief A factor that catches the 'bias' of a random stream of peer ids.
148 *
149 * As introduced by Brahms: Factor between the number of unique ids in a
150 * truly random stream and number of unique ids in the gossip stream.
151 */
152 float deficiency_factor;
153};
154
155
156/**
157 * Handler for a single request from a client.
158 */
159struct GNUNET_RPS_Request_Handle
160{
161 /**
162 * The client issuing the request.
163 */
164 struct GNUNET_RPS_Handle *rps_handle;
165
166 /**
167 * The number of requested peers.
168 */
169 uint32_t num_requests;
170
171 /**
172 * @brief The Sampler for the client request
173 */
174 struct RPS_Sampler *sampler;
175
176 /**
177 * @brief Request handle of the request to the sampler - needed to cancel the request
178 */
179 struct RPS_SamplerRequestHandle *sampler_rh;
180
181 /**
182 * @brief Request handle of the request of the biased stream of peers -
183 * needed to cancel the request
184 */
185 struct GNUNET_RPS_StreamRequestHandle *srh;
186
187 /**
188 * The callback to be called when we receive an answer.
189 */
190 GNUNET_RPS_NotifyReadyCB ready_cb;
191
192 /**
193 * The closure for the callback.
194 */
195 void *ready_cb_cls;
196
197 /**
198 * @brief Pointer to next element in DLL
199 */
200 struct GNUNET_RPS_Request_Handle *next;
201
202 /**
203 * @brief Pointer to previous element in DLL
204 */
205 struct GNUNET_RPS_Request_Handle *prev;
206};
207
208
209/**
210 * Handler for a single request from a client.
211 */
212struct GNUNET_RPS_Request_Handle_Single_Info
213{
214 /**
215 * The client issuing the request.
216 */
217 struct GNUNET_RPS_Handle *rps_handle;
218
219 /**
220 * @brief The Sampler for the client request
221 */
222 struct RPS_Sampler *sampler;
223
224 /**
225 * @brief Request handle of the request to the sampler - needed to cancel the request
226 */
227 struct RPS_SamplerRequestHandleSingleInfo *sampler_rh;
228
229 /**
230 * @brief Request handle of the request of the biased stream of peers -
231 * needed to cancel the request
232 */
233 struct GNUNET_RPS_StreamRequestHandle *srh;
234
235 /**
236 * The callback to be called when we receive an answer.
237 */
238 GNUNET_RPS_NotifyReadySingleInfoCB ready_cb;
239
240 /**
241 * The closure for the callback.
242 */
243 void *ready_cb_cls;
244
245 /**
246 * @brief Pointer to next element in DLL
247 */
248 struct GNUNET_RPS_Request_Handle_Single_Info *next;
249
250 /**
251 * @brief Pointer to previous element in DLL
252 */
253 struct GNUNET_RPS_Request_Handle_Single_Info *prev;
254};
255
256
257/**
258 * Struct used to pack the callback, its closure (provided by the caller)
259 * and the connection handler to the service to pass it to a callback function.
260 */
261struct cb_cls_pack
262{
263 /**
264 * Callback provided by the client
265 */
266 GNUNET_RPS_NotifyReadyCB cb;
267
268 /**
269 * Closure provided by the client
270 */
271 void *cls;
272
273 /**
274 * Handle to the service connection
275 */
276 struct GNUNET_CLIENT_Connection *service_conn;
277};
278
279
280/**
281 * @brief Peers received from the biased stream to be passed to all
282 * srh_handlers
283 */
284static struct GNUNET_PeerIdentity *srh_callback_peers;
285
286/**
287 * @brief Number of peers in the biased stream that are to be passed to all
288 * srh_handlers
289 */
290static uint64_t srh_callback_num_peers;
291
292
293/**
294 * @brief Create a new handle for a stream request
295 *
296 * @param rps_handle The rps handle
297 * @param num_peers The number of desired peers
298 * @param ready_cb The callback to be called, once all peers are ready
299 * @param cls The colsure to provide to the callback
300 *
301 * @return The handle to the stream request
302 */
303static struct GNUNET_RPS_StreamRequestHandle *
304new_stream_request (struct GNUNET_RPS_Handle *rps_handle,
305 GNUNET_RPS_NotifyReadyCB ready_cb,
306 void *cls)
307{
308 struct GNUNET_RPS_StreamRequestHandle *srh;
309
310 srh = GNUNET_new (struct GNUNET_RPS_StreamRequestHandle);
311 srh->rps_handle = rps_handle;
312 srh->ready_cb = ready_cb;
313 srh->ready_cb_cls = cls;
314 GNUNET_CONTAINER_DLL_insert (rps_handle->stream_requests_head,
315 rps_handle->stream_requests_tail,
316 srh);
317
318 return srh;
319}
320
321
322/**
323 * @brief Remove the given stream request from the list of requests and memory
324 *
325 * @param srh The request to be removed
326 */
327static void
328remove_stream_request (struct GNUNET_RPS_StreamRequestHandle *srh)
329{
330 struct GNUNET_RPS_Handle *rps_handle = srh->rps_handle;
331
332 GNUNET_assert (NULL != srh);
333 if (NULL != srh->callback_task)
334 {
335 GNUNET_SCHEDULER_cancel (srh->callback_task);
336 srh->callback_task = NULL;
337 }
338 GNUNET_CONTAINER_DLL_remove (rps_handle->stream_requests_head,
339 rps_handle->stream_requests_tail,
340 srh);
341 GNUNET_free (srh);
342}
343
344
345/**
346 * @brief Called once the sampler has collected all requested peers.
347 *
348 * Calls the callback provided by the client with the corresponding cls.
349 *
350 * @param peers The array of @a num_peers that has been returned.
351 * @param num_peers The number of peers that have been returned
352 * @param cls The #GNUNET_RPS_Request_Handle
353 */
354static void
355peers_ready_cb (const struct GNUNET_PeerIdentity *peers,
356 uint32_t num_peers,
357 void *cls)
358{
359 struct GNUNET_RPS_Request_Handle *rh = cls;
360
361 rh->sampler_rh = NULL;
362 rh->ready_cb (rh->ready_cb_cls,
363 num_peers,
364 peers);
365 GNUNET_RPS_request_cancel (rh);
366}
367
368
369/**
370 * @brief Called once the sampler has collected the requested peer.
371 *
372 * Calls the callback provided by the client with the corresponding cls.
373 *
374 * @param peers The array of @a num_peers that has been returned.
375 * @param num_peers The number of peers that have been returned
376 * @param cls The #GNUNET_RPS_Request_Handle
377 * @param probability Probability with which all IDs have been observed
378 * @param num_observed Number of observed IDs
379 */
380static void
381peer_info_ready_cb (const struct GNUNET_PeerIdentity *peers,
382 void *cls,
383 double probability,
384 uint32_t num_observed)
385{
386 struct GNUNET_RPS_Request_Handle_Single_Info *rh = cls;
387
388 rh->sampler_rh = NULL;
389 rh->ready_cb (rh->ready_cb_cls,
390 peers,
391 probability,
392 num_observed);
393 GNUNET_RPS_request_single_info_cancel (rh);
394}
395
396
397/**
398 * @brief Callback to collect the peers from the biased stream and put those
399 * into the sampler.
400 *
401 * @param cls The #GNUNET_RPS_Request_Handle
402 * @param num_peers The number of peer that have been returned
403 * @param peers The array of @a num_peers that have been returned
404 */
405static void
406collect_peers_cb (void *cls,
407 uint64_t num_peers,
408 const struct GNUNET_PeerIdentity *peers)
409{
410 struct GNUNET_RPS_Request_Handle *rh = cls;
411
412 LOG (GNUNET_ERROR_TYPE_DEBUG,
413 "Service sent %" PRIu64 " peers from stream\n",
414 num_peers);
415 for (uint64_t i = 0; i < num_peers; i++)
416 {
417 RPS_sampler_update (rh->sampler, &peers[i]);
418 }
419}
420
421
422/**
423 * @brief Callback to collect the peers from the biased stream and put those
424 * into the sampler.
425 *
426 * This version is for the modified #GNUNET_RPS_Request_Handle_Single_Info
427 *
428 * @param cls The #GNUNET_RPS_Request_Handle
429 * @param num_peers The number of peer that have been returned
430 * @param peers The array of @a num_peers that have been returned
431 */
432static void
433collect_peers_info_cb (void *cls,
434 uint64_t num_peers,
435 const struct GNUNET_PeerIdentity *peers)
436{
437 struct GNUNET_RPS_Request_Handle_Single_Info *rhs = cls;
438
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 "Service sent %" PRIu64 " peers from stream\n",
441 num_peers);
442 for (uint64_t i = 0; i < num_peers; i++)
443 {
444 RPS_sampler_update (rhs->sampler, &peers[i]);
445 }
446}
447
448
449/* Get internals for debugging/profiling purposes */
450
451/**
452 * Request updates of view
453 *
454 * @param rps_handle handle to the rps service
455 * @param num_req_peers number of peers we want to receive
456 * (0 for infinite updates)
457 * @param cls a closure that will be given to the callback
458 * @param ready_cb the callback called when the peers are available
459 */
460void
461GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
462 uint32_t num_updates,
463 GNUNET_RPS_NotifyReadyCB view_update_cb,
464 void *cls)
465{
466 struct GNUNET_MQ_Envelope *ev;
467 struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
468
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Client requests %" PRIu32 " view updates\n",
471 num_updates);
472 rps_handle->view_update_cb = view_update_cb;
473 rps_handle->view_update_cls = cls;
474
475 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
476 msg->num_updates = htonl (num_updates);
477 GNUNET_MQ_send (rps_handle->mq, ev);
478}
479
480
481void
482GNUNET_RPS_view_request_cancel (struct GNUNET_RPS_Handle *rps_handle)
483{
484 struct GNUNET_MQ_Envelope *ev;
485
486 GNUNET_assert (NULL != rps_handle->view_update_cb);
487
488 rps_handle->view_update_cb = NULL;
489
490 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL);
491 GNUNET_MQ_send (rps_handle->mq, ev);
492}
493
494
495/**
496 * Request biased stream of peers that are being put into the sampler
497 *
498 * @param rps_handle handle to the rps service
499 * @param cls a closure that will be given to the callback
500 * @param ready_cb the callback called when the peers are available
501 */
502struct GNUNET_RPS_StreamRequestHandle *
503GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
504 GNUNET_RPS_NotifyReadyCB stream_input_cb,
505 void *cls)
506{
507 struct GNUNET_RPS_StreamRequestHandle *srh;
508 struct GNUNET_MQ_Envelope *ev;
509 struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
510
511 srh = new_stream_request (rps_handle,
512 stream_input_cb,
513 cls);
514 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requests biased stream updates\n");
515
516 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST);
517 GNUNET_MQ_send (rps_handle->mq, ev);
518 return srh;
519}
520
521
522/**
523 * This function is called, when the service updates the view.
524 * It verifies that @a msg is well-formed.
525 *
526 * @param cls the closure
527 * @param msg the message
528 * @return #GNUNET_OK if @a msg is well-formed
529 */
530static int
531check_view_update (void *cls,
532 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
533{
534 uint16_t msize = ntohs (msg->header.size);
535 uint32_t num_peers = ntohl (msg->num_peers);
536
537 (void) cls;
538
539 msize -= sizeof(struct GNUNET_RPS_CS_DEBUG_ViewReply);
540 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
541 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
542 {
543 GNUNET_break (0);
544 return GNUNET_SYSERR;
545 }
546 return GNUNET_OK;
547}
548
549
550/**
551 * This function is called, when the service updated its view.
552 * It calls the callback the caller provided
553 * and disconnects afterwards.
554 *
555 * @param msg the message
556 */
557static void
558handle_view_update (void *cls,
559 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
560{
561 struct GNUNET_RPS_Handle *h = cls;
562 struct GNUNET_PeerIdentity *peers;
563
564 /* Give the peers back */
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "New view of %" PRIu32 " peers:\n",
567 ntohl (msg->num_peers));
568
569 peers = (struct GNUNET_PeerIdentity *) &msg[1];
570 GNUNET_assert (NULL != h);
571 GNUNET_assert (NULL != h->view_update_cb);
572 h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
573}
574
575
576/**
577 * @brief Send message to service that this client does not want to receive
578 * further updates from the biased peer stream
579 *
580 * @param rps_handle The handle representing the service to the client
581 */
582static void
583cancel_stream (struct GNUNET_RPS_Handle *rps_handle)
584{
585 struct GNUNET_MQ_Envelope *ev;
586
587 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL);
588 GNUNET_MQ_send (rps_handle->mq, ev);
589}
590
591
592/**
593 * @brief Cancel a specific request for updates from the biased peer stream
594 *
595 * @param srh The request handle to cancel
596 */
597void
598GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh)
599{
600 struct GNUNET_RPS_Handle *rps_handle;
601
602 rps_handle = srh->rps_handle;
603 remove_stream_request (srh);
604 if (NULL == rps_handle->stream_requests_head)
605 cancel_stream (rps_handle);
606}
607
608
609/**
610 * This function is called, when the service sends another peer from the biased
611 * stream.
612 * It calls the callback the caller provided
613 * and disconnects afterwards.
614 *
615 * TODO merge with check_view_update
616 *
617 * @param msg the message
618 */
619static int
620check_stream_input (void *cls,
621 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
622{
623 uint16_t msize = ntohs (msg->header.size);
624 uint32_t num_peers = ntohl (msg->num_peers);
625
626 (void) cls;
627
628 msize -= sizeof(struct GNUNET_RPS_CS_DEBUG_StreamReply);
629 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
630 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
631 {
632 GNUNET_break (0);
633 return GNUNET_SYSERR;
634 }
635 return GNUNET_OK;
636}
637
638
639/**
640 * @brief Called by the scheduler to call the callbacks of the srh handlers
641 *
642 * @param cls Stream request handle
643 */
644static void
645srh_callback_scheduled (void *cls)
646{
647 struct GNUNET_RPS_StreamRequestHandle *srh = cls;
648
649 srh->callback_task = NULL;
650 srh->ready_cb (srh->ready_cb_cls,
651 srh_callback_num_peers,
652 srh_callback_peers);
653}
654
655
656/**
657 * This function is called, when the service sends another peer from the biased
658 * stream.
659 * It calls the callback the caller provided
660 * and disconnects afterwards.
661 *
662 * @param msg the message
663 */
664static void
665handle_stream_input (void *cls,
666 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
667{
668 struct GNUNET_RPS_Handle *h = cls;
669 // const struct GNUNET_PeerIdentity *peers;
670 uint64_t num_peers;
671 struct GNUNET_RPS_StreamRequestHandle *srh_iter;
672 struct GNUNET_RPS_StreamRequestHandle *srh_next;
673
674 // peers = (struct GNUNET_PeerIdentity *) &msg[1];
675 num_peers = ntohl (msg->num_peers);
676 srh_callback_num_peers = num_peers;
677 GNUNET_free (srh_callback_peers);
678 srh_callback_peers = GNUNET_new_array (num_peers,
679 struct GNUNET_PeerIdentity);
680 GNUNET_memcpy (srh_callback_peers,
681 &msg[1],
682 num_peers * sizeof(struct GNUNET_PeerIdentity));
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Received %" PRIu64 " peer(s) from stream input.\n",
685 num_peers);
686 for (srh_iter = h->stream_requests_head;
687 NULL != srh_iter;
688 srh_iter = srh_next)
689 {
690 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling srh \n");
691 /* Store next pointer - srh might be removed/freed in callback */
692 srh_next = srh_iter->next;
693 if (NULL != srh_iter->callback_task)
694 GNUNET_SCHEDULER_cancel (srh_iter->callback_task);
695 srh_iter->callback_task =
696 GNUNET_SCHEDULER_add_now (&srh_callback_scheduled,
697 srh_iter);
698 }
699
700 if (NULL == h->stream_requests_head)
701 {
702 cancel_stream (h);
703 }
704}
705
706
707/**
708 * Reconnect to the service
709 */
710static void
711reconnect (struct GNUNET_RPS_Handle *h);
712
713
714/**
715 * Error handler for mq.
716 *
717 * This function is called when mq encounters an error.
718 * Until now mq doesn't provide useful error messages.
719 *
720 * @param cls the closure
721 * @param error error code without specyfied meaning
722 */
723static void
724mq_error_handler (void *cls,
725 enum GNUNET_MQ_Error error)
726{
727 struct GNUNET_RPS_Handle *h = cls;
728
729 // TODO LOG
730 LOG (GNUNET_ERROR_TYPE_WARNING,
731 "Problem with message queue. error: %i\n\
732 1: READ,\n\
733 2: WRITE,\n\
734 4: TIMEOUT\n",
735 // TODO: write GNUNET_MQ_strerror (error)
736 error);
737 reconnect (h);
738 /* Resend all pending request as the service destroyed its knowledge
739 * about them */
740}
741
742
743/**
744 * @brief Create the hash value from the share value that defines the sub
745 * (-group)
746 *
747 * @param share_val Share value
748 * @param hash[out] Pointer to the location in which the hash will be stored.
749 */
750static void
751hash_from_share_val (const char *share_val,
752 struct GNUNET_HashCode *hash)
753{
754 GNUNET_CRYPTO_kdf (hash,
755 sizeof(struct GNUNET_HashCode),
756 "rps",
757 strlen ("rps"),
758 share_val,
759 strlen (share_val),
760 NULL, 0);
761}
762
763
764/**
765 * @brief Callback for network size estimate - called with new estimates about
766 * the network size, updates all samplers with the new estimate
767 *
768 * Implements #GNUNET_NSE_Callback
769 *
770 * @param cls the rps handle
771 * @param timestamp unused
772 * @param logestimate the estimate
773 * @param std_dev the standard distribution
774 */
775static void
776nse_cb (void *cls,
777 struct GNUNET_TIME_Absolute timestamp,
778 double logestimate,
779 double std_dev)
780{
781 struct GNUNET_RPS_Handle *h = cls;
782
783 (void) timestamp;
784 (void) std_dev;
785
786 for (struct GNUNET_RPS_Request_Handle *rh_iter = h->rh_head;
787 NULL != rh_iter && NULL != rh_iter->next;
788 rh_iter = rh_iter->next)
789 {
790 RPS_sampler_update_with_nw_size (rh_iter->sampler,
791 GNUNET_NSE_log_estimate_to_n (
792 logestimate));
793 }
794 for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head;
795 NULL != rhs_iter && NULL != rhs_iter->next;
796 rhs_iter = rhs_iter->next)
797 {
798 RPS_sampler_update_with_nw_size (rhs_iter->sampler,
799 GNUNET_NSE_log_estimate_to_n (
800 logestimate));
801 }
802}
803
804
805/**
806 * Reconnect to the service
807 */
808static void
809reconnect (struct GNUNET_RPS_Handle *h)
810{
811 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
812 GNUNET_MQ_hd_var_size (view_update,
813 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
814 struct GNUNET_RPS_CS_DEBUG_ViewReply,
815 h),
816 GNUNET_MQ_hd_var_size (stream_input,
817 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
818 struct GNUNET_RPS_CS_DEBUG_StreamReply,
819 h),
820 GNUNET_MQ_handler_end ()
821 };
822
823 if (NULL != h->mq)
824 GNUNET_MQ_destroy (h->mq);
825 h->mq = GNUNET_CLIENT_connect (h->cfg,
826 "rps",
827 mq_handlers,
828 &mq_error_handler,
829 h);
830 if (NULL != h->nse)
831 GNUNET_NSE_disconnect (h->nse);
832 h->nse = GNUNET_NSE_connect (h->cfg, &nse_cb, h);
833}
834
835
836/**
837 * Connect to the rps service
838 *
839 * @param cfg configuration to use
840 * @return a handle to the service, NULL on error
841 */
842struct GNUNET_RPS_Handle *
843GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
844{
845 struct GNUNET_RPS_Handle *h;
846
847 h = GNUNET_new (struct GNUNET_RPS_Handle);
848 h->cfg = cfg;
849 if (GNUNET_OK !=
850 GNUNET_CONFIGURATION_get_value_float (cfg,
851 "RPS",
852 "DESIRED_PROBABILITY",
853 &h->desired_probability))
854 {
855 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
856 "RPS", "DESIRED_PROBABILITY");
857 GNUNET_free (h);
858 return NULL;
859 }
860 if ((0 > h->desired_probability) ||
861 (1 < h->desired_probability) )
862 {
863 LOG (GNUNET_ERROR_TYPE_ERROR,
864 "The desired probability must be in the interval [0;1]\n");
865 GNUNET_free (h);
866 return NULL;
867 }
868 if (GNUNET_OK !=
869 GNUNET_CONFIGURATION_get_value_float (cfg,
870 "RPS",
871 "DEFICIENCY_FACTOR",
872 &h->deficiency_factor))
873 {
874 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
875 "RPS", "DEFICIENCY_FACTOR");
876 GNUNET_free (h);
877 return NULL;
878 }
879 if ((0 > h->desired_probability) ||
880 (1 < h->desired_probability) )
881 {
882 LOG (GNUNET_ERROR_TYPE_ERROR,
883 "The deficiency factor must be in the interval [0;1]\n");
884 GNUNET_free (h);
885 return NULL;
886 }
887 reconnect (h);
888 if (NULL == h->mq)
889 {
890 GNUNET_free (h);
891 return NULL;
892 }
893 return h;
894}
895
896
897/**
898 * @brief Start a sub with the given shared value
899 *
900 * @param h Handle to rps
901 * @param shared_value The shared value that defines the members of the sub (-gorup)
902 */
903void
904GNUNET_RPS_sub_start (struct GNUNET_RPS_Handle *h,
905 const char *shared_value)
906{
907 struct GNUNET_RPS_CS_SubStartMessage *msg;
908 struct GNUNET_MQ_Envelope *ev;
909
910 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START);
911 hash_from_share_val (shared_value, &msg->hash);
912 msg->round_interval = GNUNET_TIME_relative_hton ( // TODO read from config!
913 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30));
914 GNUNET_assert (0 != msg->round_interval.rel_value_us__);
915
916 GNUNET_MQ_send (h->mq, ev);
917}
918
919
920/**
921 * @brief Stop a sub with the given shared value
922 *
923 * @param h Handle to rps
924 * @param shared_value The shared value that defines the members of the sub (-gorup)
925 */
926void
927GNUNET_RPS_sub_stop (struct GNUNET_RPS_Handle *h,
928 const char *shared_value)
929{
930 struct GNUNET_RPS_CS_SubStopMessage *msg;
931 struct GNUNET_MQ_Envelope *ev;
932
933 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP);
934 hash_from_share_val (shared_value, &msg->hash);
935
936 GNUNET_MQ_send (h->mq, ev);
937}
938
939
940/**
941 * Request n random peers.
942 *
943 * @param rps_handle handle to the rps service
944 * @param num_req_peers number of peers we want to receive
945 * @param ready_cb the callback called when the peers are available
946 * @param cls closure given to the callback
947 * @return a handle to cancel this request
948 */
949struct GNUNET_RPS_Request_Handle *
950GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
951 uint32_t num_req_peers,
952 GNUNET_RPS_NotifyReadyCB ready_cb,
953 void *cls)
954{
955 struct GNUNET_RPS_Request_Handle *rh;
956
957 LOG (GNUNET_ERROR_TYPE_INFO,
958 "Client requested %" PRIu32 " peers\n",
959 num_req_peers);
960 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
961 rh->rps_handle = rps_handle;
962 rh->num_requests = num_req_peers;
963 rh->sampler = RPS_sampler_mod_init (num_req_peers,
964 GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
965 RPS_sampler_set_desired_probability (rh->sampler,
966 rps_handle->desired_probability);
967 RPS_sampler_set_deficiency_factor (rh->sampler,
968 rps_handle->deficiency_factor);
969 rh->sampler_rh = RPS_sampler_get_n_rand_peers (rh->sampler,
970 num_req_peers,
971 peers_ready_cb,
972 rh);
973 rh->srh = GNUNET_RPS_stream_request (rps_handle,
974 collect_peers_cb,
975 rh); /* cls */
976 rh->ready_cb = ready_cb;
977 rh->ready_cb_cls = cls;
978 GNUNET_CONTAINER_DLL_insert (rps_handle->rh_head,
979 rps_handle->rh_tail,
980 rh);
981
982 return rh;
983}
984
985
986/**
987 * Request one random peer, getting additional information.
988 *
989 * @param rps_handle handle to the rps service
990 * @param ready_cb the callback called when the peers are available
991 * @param cls closure given to the callback
992 * @return a handle to cancel this request
993 */
994struct GNUNET_RPS_Request_Handle_Single_Info *
995GNUNET_RPS_request_peer_info (struct GNUNET_RPS_Handle *rps_handle,
996 GNUNET_RPS_NotifyReadySingleInfoCB ready_cb,
997 void *cls)
998{
999 struct GNUNET_RPS_Request_Handle_Single_Info *rhs;
1000 uint32_t num_req_peers = 1;
1001
1002 LOG (GNUNET_ERROR_TYPE_INFO,
1003 "Client requested peer with additional info\n");
1004 rhs = GNUNET_new (struct GNUNET_RPS_Request_Handle_Single_Info);
1005 rhs->rps_handle = rps_handle;
1006 rhs->sampler = RPS_sampler_mod_init (num_req_peers,
1007 GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
1008 RPS_sampler_set_desired_probability (rhs->sampler,
1009 rps_handle->desired_probability);
1010 RPS_sampler_set_deficiency_factor (rhs->sampler,
1011 rps_handle->deficiency_factor);
1012 rhs->sampler_rh = RPS_sampler_get_rand_peer_info (rhs->sampler,
1013 peer_info_ready_cb,
1014 rhs);
1015 rhs->srh = GNUNET_RPS_stream_request (rps_handle,
1016 collect_peers_info_cb,
1017 rhs); /* cls */
1018 rhs->ready_cb = ready_cb;
1019 rhs->ready_cb_cls = cls;
1020 GNUNET_CONTAINER_DLL_insert (rps_handle->rhs_head,
1021 rps_handle->rhs_tail,
1022 rhs);
1023
1024 return rhs;
1025}
1026
1027
1028/**
1029 * Seed rps service with peerIDs.
1030 *
1031 * @param h handle to the rps service
1032 * @param n number of peers to seed
1033 * @param ids the ids of the peers seeded
1034 */
1035void
1036GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
1037 uint32_t n,
1038 const struct GNUNET_PeerIdentity *ids)
1039{
1040 size_t size_needed;
1041 uint32_t num_peers_max;
1042 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
1043 struct GNUNET_MQ_Envelope *ev;
1044 struct GNUNET_RPS_CS_SeedMessage *msg;
1045
1046 LOG (GNUNET_ERROR_TYPE_DEBUG,
1047 "Client wants to seed %" PRIu32 " peers:\n",
1048 n);
1049 for (unsigned int i = 0; i < n; i++)
1050 LOG (GNUNET_ERROR_TYPE_DEBUG,
1051 "%u. peer: %s\n",
1052 i,
1053 GNUNET_i2s (&ids[i]));
1054
1055 /* The actual size the message occupies */
1056 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1057 + n * sizeof(struct GNUNET_PeerIdentity);
1058 /* The number of peers that fits in one message together with
1059 * the respective header */
1060 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE
1061 - sizeof(struct GNUNET_RPS_CS_SeedMessage))
1062 / sizeof(struct GNUNET_PeerIdentity);
1063 tmp_peer_pointer = ids;
1064
1065 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1066 {
1067 ev = GNUNET_MQ_msg_extra (msg,
1068 num_peers_max * sizeof(struct
1069 GNUNET_PeerIdentity),
1070 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
1071 msg->num_peers = htonl (num_peers_max);
1072 GNUNET_memcpy (&msg[1],
1073 tmp_peer_pointer,
1074 num_peers_max * sizeof(struct GNUNET_PeerIdentity));
1075 GNUNET_MQ_send (h->mq,
1076 ev);
1077 n -= num_peers_max;
1078 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1079 + n * sizeof(struct GNUNET_PeerIdentity);
1080 /* Set pointer to beginning of next block of num_peers_max peers */
1081 tmp_peer_pointer = &ids[num_peers_max];
1082 }
1083
1084 ev = GNUNET_MQ_msg_extra (msg,
1085 n * sizeof(struct GNUNET_PeerIdentity),
1086 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
1087 msg->num_peers = htonl (n);
1088 GNUNET_memcpy (&msg[1],
1089 tmp_peer_pointer,
1090 n * sizeof(struct GNUNET_PeerIdentity));
1091 GNUNET_MQ_send (h->mq,
1092 ev);
1093}
1094
1095
1096#if ENABLE_MALICIOUS
1097/**
1098 * Turn RPS service to act malicious.
1099 *
1100 * @param h handle to the rps service
1101 * @param type which type of malicious peer to turn to.
1102 * 0 Don't act malicious at all
1103 * 1 Try to maximise representation
1104 * 2 Try to partition the network
1105 * (isolate one peer from the rest)
1106 * @param n number of @a ids
1107 * @param ids the ids of the malicious peers
1108 * if @type is 2 the last id is the id of the
1109 * peer to be isolated from the rest
1110 */
1111void
1112GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
1113 uint32_t type,
1114 uint32_t num_peers,
1115 const struct GNUNET_PeerIdentity *peer_ids,
1116 const struct GNUNET_PeerIdentity *target_peer)
1117{
1118 size_t size_needed;
1119 uint32_t num_peers_max;
1120 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
1121 struct GNUNET_MQ_Envelope *ev;
1122 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
1123
1124 unsigned int i;
1125
1126 LOG (GNUNET_ERROR_TYPE_DEBUG,
1127 "Client turns malicious (type %" PRIu32 ") with %" PRIu32
1128 " other peers:\n",
1129 type,
1130 num_peers);
1131 for (i = 0; i < num_peers; i++)
1132 LOG (GNUNET_ERROR_TYPE_DEBUG,
1133 "%u. peer: %s\n",
1134 i,
1135 GNUNET_i2s (&peer_ids[i]));
1136
1137 /* The actual size the message would occupy */
1138 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1139 + num_peers * sizeof(struct GNUNET_PeerIdentity);
1140 /* The number of peers that fit in one message together with
1141 * the respective header */
1142 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE
1143 - sizeof(struct GNUNET_RPS_CS_SeedMessage))
1144 / sizeof(struct GNUNET_PeerIdentity);
1145 tmp_peer_pointer = peer_ids;
1146
1147 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1148 {
1149 LOG (GNUNET_ERROR_TYPE_DEBUG,
1150 "Too many peers to send at once, sending %" PRIu32
1151 " (all we can so far)\n",
1152 num_peers_max);
1153 ev = GNUNET_MQ_msg_extra (msg,
1154 num_peers_max * sizeof(struct
1155 GNUNET_PeerIdentity),
1156 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
1157 msg->type = htonl (type);
1158 msg->num_peers = htonl (num_peers_max);
1159 if ((2 == type) ||
1160 (3 == type))
1161 msg->attacked_peer = peer_ids[num_peers];
1162 GNUNET_memcpy (&msg[1],
1163 tmp_peer_pointer,
1164 num_peers_max * sizeof(struct GNUNET_PeerIdentity));
1165
1166 GNUNET_MQ_send (h->mq, ev);
1167
1168 num_peers -= num_peers_max;
1169 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1170 + num_peers * sizeof(struct GNUNET_PeerIdentity);
1171 /* Set pointer to beginning of next block of num_peers_max peers */
1172 tmp_peer_pointer = &peer_ids[num_peers_max];
1173 }
1174
1175 ev = GNUNET_MQ_msg_extra (msg,
1176 num_peers * sizeof(struct GNUNET_PeerIdentity),
1177 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
1178 msg->type = htonl (type);
1179 msg->num_peers = htonl (num_peers);
1180 if ((2 == type) ||
1181 (3 == type))
1182 msg->attacked_peer = *target_peer;
1183 GNUNET_memcpy (&msg[1],
1184 tmp_peer_pointer,
1185 num_peers * sizeof(struct GNUNET_PeerIdentity));
1186
1187 GNUNET_MQ_send (h->mq, ev);
1188}
1189
1190
1191#endif /* ENABLE_MALICIOUS */
1192
1193
1194/**
1195 * Cancel an issued request.
1196 *
1197 * @param rh request handle of request to cancel
1198 */
1199void
1200GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
1201{
1202 struct GNUNET_RPS_Handle *h;
1203
1204 h = rh->rps_handle;
1205 GNUNET_assert (NULL != rh);
1206 GNUNET_assert (NULL != rh->srh);
1207 GNUNET_assert (h == rh->srh->rps_handle);
1208 GNUNET_RPS_stream_cancel (rh->srh);
1209 rh->srh = NULL;
1210 if (NULL == h->stream_requests_head)
1211 cancel_stream (h);
1212 if (NULL != rh->sampler_rh)
1213 {
1214 RPS_sampler_request_cancel (rh->sampler_rh);
1215 }
1216 RPS_sampler_destroy (rh->sampler);
1217 rh->sampler = NULL;
1218 GNUNET_CONTAINER_DLL_remove (h->rh_head,
1219 h->rh_tail,
1220 rh);
1221 GNUNET_free (rh);
1222}
1223
1224
1225/**
1226 * Cancel an issued single info request.
1227 *
1228 * @param rhs request handle of request to cancel
1229 */
1230void
1231GNUNET_RPS_request_single_info_cancel (
1232 struct GNUNET_RPS_Request_Handle_Single_Info *rhs)
1233{
1234 struct GNUNET_RPS_Handle *h;
1235
1236 h = rhs->rps_handle;
1237 GNUNET_assert (NULL != rhs);
1238 GNUNET_assert (NULL != rhs->srh);
1239 GNUNET_assert (h == rhs->srh->rps_handle);
1240 GNUNET_RPS_stream_cancel (rhs->srh);
1241 rhs->srh = NULL;
1242 if (NULL == h->stream_requests_head)
1243 cancel_stream (h);
1244 if (NULL != rhs->sampler_rh)
1245 {
1246 RPS_sampler_request_single_info_cancel (rhs->sampler_rh);
1247 }
1248 RPS_sampler_destroy (rhs->sampler);
1249 rhs->sampler = NULL;
1250 GNUNET_CONTAINER_DLL_remove (h->rhs_head,
1251 h->rhs_tail,
1252 rhs);
1253 GNUNET_free (rhs);
1254}
1255
1256
1257/**
1258 * Disconnect from the rps service
1259 *
1260 * @param h the handle to the rps service
1261 */
1262void
1263GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
1264{
1265 if (NULL != h->stream_requests_head)
1266 {
1267 struct GNUNET_RPS_StreamRequestHandle *srh_next;
1268
1269 LOG (GNUNET_ERROR_TYPE_WARNING,
1270 "Still waiting for replies\n");
1271 for (struct GNUNET_RPS_StreamRequestHandle *srh_iter =
1272 h->stream_requests_head;
1273 NULL != srh_iter;
1274 srh_iter = srh_next)
1275 {
1276 srh_next = srh_iter->next;
1277 GNUNET_RPS_stream_cancel (srh_iter);
1278 }
1279 }
1280 if (NULL != h->rh_head)
1281 {
1282 LOG (GNUNET_ERROR_TYPE_WARNING,
1283 "Not all requests were cancelled!\n");
1284 for (struct GNUNET_RPS_Request_Handle *rh_iter = h->rh_head;
1285 h->rh_head != NULL;
1286 rh_iter = h->rh_head)
1287 {
1288 GNUNET_RPS_request_cancel (rh_iter);
1289 }
1290 }
1291 if (NULL != h->rhs_head)
1292 {
1293 LOG (GNUNET_ERROR_TYPE_WARNING,
1294 "Not all requests were cancelled!\n");
1295 for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head;
1296 h->rhs_head != NULL;
1297 rhs_iter = h->rhs_head)
1298 {
1299 GNUNET_RPS_request_single_info_cancel (rhs_iter);
1300 }
1301 }
1302 if (NULL != srh_callback_peers)
1303 {
1304 GNUNET_free (srh_callback_peers);
1305 srh_callback_peers = NULL;
1306 }
1307 if (NULL != h->view_update_cb)
1308 {
1309 LOG (GNUNET_ERROR_TYPE_WARNING,
1310 "Still waiting for view updates\n");
1311 GNUNET_RPS_view_request_cancel (h);
1312 }
1313 if (NULL != h->nse)
1314 GNUNET_NSE_disconnect (h->nse);
1315 GNUNET_MQ_destroy (h->mq);
1316 GNUNET_free (h);
1317}
1318
1319
1320/* end of rps_api.c */
diff --git a/src/rps/test_rps.c b/src/rps/test_rps.c
deleted file mode 100644
index ab132ec8a..000000000
--- a/src/rps/test_rps.c
+++ /dev/null
@@ -1,3183 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file rps/test_rps.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_testbed_service.h"
30
31#include "gnunet_rps_service.h"
32#include "rps-test_util.h"
33#include "gnunet-service-rps_sampler_elem.h"
34
35#include <inttypes.h>
36
37
38/**
39 * How many peers do we start?
40 */
41static uint32_t num_peers;
42
43/**
44 * How long do we run the test?
45 * In seconds.
46 */
47static uint32_t timeout_s;
48
49/**
50 * How long do we run the test?
51 */
52// #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
53static struct GNUNET_TIME_Relative timeout;
54
55
56/**
57 * Portion of malicious peers
58 */
59static double portion = .1;
60
61/**
62 * Type of malicious peer to test
63 */
64static unsigned int mal_type = 0;
65
66/**
67 * Handles to all of the running peers
68 */
69static struct GNUNET_TESTBED_Peer **testbed_peers;
70
71/**
72 * @brief Indicates whether peer should go off- or online
73 */
74enum PEER_ONLINE_DELTA
75{
76 /**
77 * @brief Indicates peer going online
78 */
79 PEER_GO_ONLINE = 1,
80 /**
81 * @brief Indicates peer going offline
82 */
83 PEER_GO_OFFLINE = -1,
84};
85
86/**
87 * Operation map entry
88 */
89struct OpListEntry
90{
91 /**
92 * DLL next ptr
93 */
94 struct OpListEntry *next;
95
96 /**
97 * DLL prev ptr
98 */
99 struct OpListEntry *prev;
100
101 /**
102 * The testbed operation
103 */
104 struct GNUNET_TESTBED_Operation *op;
105
106 /**
107 * Depending on whether we start or stop RPS service at the peer, set this to
108 * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
109 */
110 enum PEER_ONLINE_DELTA delta;
111
112 /**
113 * Index of the regarding peer
114 */
115 unsigned int index;
116};
117
118/**
119 * OpList DLL head
120 */
121static struct OpListEntry *oplist_head;
122
123/**
124 * OpList DLL tail
125 */
126static struct OpListEntry *oplist_tail;
127
128
129/**
130 * A pending reply: A request was sent and the reply is pending.
131 */
132struct PendingReply
133{
134 /**
135 * DLL next,prev ptr
136 */
137 struct PendingReply *next;
138 struct PendingReply *prev;
139
140 /**
141 * Handle to the request we are waiting for
142 */
143 struct GNUNET_RPS_Request_Handle *req_handle;
144
145 /**
146 * The peer that requested
147 */
148 struct RPSPeer *rps_peer;
149};
150
151
152/**
153 * A pending request: A request was not made yet but is scheduled for later.
154 */
155struct PendingRequest
156{
157 /**
158 * DLL next,prev ptr
159 */
160 struct PendingRequest *next;
161 struct PendingRequest *prev;
162
163 /**
164 * Handle to the request we are waiting for
165 */
166 struct GNUNET_SCHEDULER_Task *request_task;
167
168 /**
169 * The peer that requested
170 */
171 struct RPSPeer *rps_peer;
172};
173
174
175/**
176 * Information we track for each peer.
177 */
178struct RPSPeer
179{
180 /**
181 * Index of the peer.
182 */
183 uint32_t index;
184
185 /**
186 * Handle for RPS connect operation.
187 */
188 struct GNUNET_TESTBED_Operation *op;
189
190 /**
191 * Handle to RPS service.
192 */
193 struct GNUNET_RPS_Handle *rps_handle;
194
195 /**
196 * Handle to stream requests
197 */
198 struct GNUNET_RPS_StreamRequestHandle *rps_srh;
199
200 /**
201 * ID of the peer.
202 */
203 struct GNUNET_PeerIdentity *peer_id;
204
205 /**
206 * A request handle to check for an request
207 */
208 // struct GNUNET_RPS_Request_Handle *req_handle;
209
210 /**
211 * Peer on- or offline?
212 */
213 int online;
214
215 /**
216 * Number of Peer IDs to request during the whole test
217 */
218 unsigned int num_ids_to_request;
219
220 /**
221 * Pending requests DLL
222 */
223 struct PendingRequest *pending_req_head;
224 struct PendingRequest *pending_req_tail;
225
226 /**
227 * Number of pending requests
228 */
229 unsigned int num_pending_reqs;
230
231 /**
232 * Pending replies DLL
233 */
234 struct PendingReply *pending_rep_head;
235 struct PendingReply *pending_rep_tail;
236
237 /**
238 * Number of pending replies
239 */
240 unsigned int num_pending_reps;
241
242 /**
243 * Number of received PeerIDs
244 */
245 unsigned int num_recv_ids;
246
247 /**
248 * Pending operation on that peer
249 */
250 const struct OpListEntry *entry_op_manage;
251
252 /**
253 * Testbed operation to connect to statistics service
254 */
255 struct GNUNET_TESTBED_Operation *stat_op;
256
257 /**
258 * Handle to the statistics service
259 */
260 struct GNUNET_STATISTICS_Handle *stats_h;
261
262 /**
263 * @brief flags to indicate which statistics values have been already
264 * collected from the statistics service.
265 * Used to check whether we are able to shutdown.
266 */
267 uint32_t stat_collected_flags;
268
269 /**
270 * @brief File name of the file the stats are finally written to
271 */
272 const char *file_name_stats;
273
274 /**
275 * @brief File name of the file the stats are finally written to
276 */
277 const char *file_name_probs;
278
279 /**
280 * @brief The current view
281 */
282 struct GNUNET_PeerIdentity *cur_view;
283
284 /**
285 * @brief Number of peers in the #cur_view.
286 */
287 uint32_t cur_view_count;
288
289 /**
290 * @brief Number of occurrences in other peer's view
291 */
292 uint32_t count_in_views;
293
294 /**
295 * @brief statistics values
296 */
297 uint64_t num_rounds;
298 uint64_t num_blocks;
299 uint64_t num_blocks_many_push;
300 uint64_t num_blocks_no_push;
301 uint64_t num_blocks_no_pull;
302 uint64_t num_blocks_many_push_no_pull;
303 uint64_t num_blocks_no_push_no_pull;
304 uint64_t num_issued_push;
305 uint64_t num_issued_pull_req;
306 uint64_t num_issued_pull_rep;
307 uint64_t num_sent_push;
308 uint64_t num_sent_pull_req;
309 uint64_t num_sent_pull_rep;
310 uint64_t num_recv_push;
311 uint64_t num_recv_pull_req;
312 uint64_t num_recv_pull_rep;
313};
314
315enum STAT_TYPE
316{
317 STAT_TYPE_ROUNDS = 0x1, /* 1 */
318 STAT_TYPE_BLOCKS = 0x2, /* 2 */
319 STAT_TYPE_BLOCKS_MANY_PUSH = 0x4, /* 3 */
320 STAT_TYPE_BLOCKS_NO_PUSH = 0x8, /* 4 */
321 STAT_TYPE_BLOCKS_NO_PULL = 0x10, /* 5 */
322 STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL = 0x20, /* 6 */
323 STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL = 0x40, /* 7 */
324 STAT_TYPE_ISSUED_PUSH_SEND = 0x80, /* 8 */
325 STAT_TYPE_ISSUED_PULL_REQ = 0x100, /* 9 */
326 STAT_TYPE_ISSUED_PULL_REP = 0x200, /* 10 */
327 STAT_TYPE_SENT_PUSH_SEND = 0x400, /* 11 */
328 STAT_TYPE_SENT_PULL_REQ = 0x800, /* 12 */
329 STAT_TYPE_SENT_PULL_REP = 0x1000, /* 13 */
330 STAT_TYPE_RECV_PUSH_SEND = 0x2000, /* 14 */
331 STAT_TYPE_RECV_PULL_REQ = 0x4000, /* 15 */
332 STAT_TYPE_RECV_PULL_REP = 0x8000, /* 16 */
333 STAT_TYPE_MAX = 0x80000000, /* 32 */
334};
335
336struct STATcls
337{
338 struct RPSPeer *rps_peer;
339 enum STAT_TYPE stat_type;
340};
341
342
343/**
344 * Information for all the peers.
345 */
346static struct RPSPeer *rps_peers;
347
348/**
349 * Peermap to get the index of a given peer ID quick.
350 */
351static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
352
353/**
354 * IDs of the peers.
355 */
356static struct GNUNET_PeerIdentity *rps_peer_ids;
357
358/**
359 * ID of the targeted peer.
360 */
361static struct GNUNET_PeerIdentity *target_peer;
362
363/**
364 * ID of the peer that requests for the evaluation.
365 */
366static struct RPSPeer *eval_peer;
367
368/**
369 * Number of online peers.
370 */
371static unsigned int num_peers_online;
372
373/**
374 * @brief The added sizes of the peer's views
375 */
376static unsigned int view_sizes;
377
378/**
379 * Return value from 'main'.
380 */
381static int ok;
382
383/**
384 * Identifier for the churn task that runs periodically
385 */
386static struct GNUNET_SCHEDULER_Task *post_test_task;
387
388/**
389 * Identifier for the churn task that runs periodically
390 */
391static struct GNUNET_SCHEDULER_Task *shutdown_task;
392
393/**
394 * Identifier for the churn task that runs periodically
395 */
396static struct GNUNET_SCHEDULER_Task *churn_task;
397
398/**
399 * Called to initialise the given RPSPeer
400 */
401typedef void (*InitPeer) (struct RPSPeer *rps_peer);
402
403/**
404 * @brief Called directly after connecting to the service
405 *
406 * @param rps_peer Specific peer the function is called on
407 * @param h the handle to the rps service
408 */
409typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
410
411/**
412 * @brief Executes functions to test the api/service for a given peer
413 *
414 * Called from within #rps_connect_complete_cb ()
415 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
416 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
417 *
418 * @param rps_peer the peer the task runs on
419 */
420typedef void (*MainTest) (struct RPSPeer *rps_peer);
421
422/**
423 * Callback called once the requested random peers are available
424 */
425typedef void (*ReplyHandle) (void *cls,
426 uint64_t n,
427 const struct GNUNET_PeerIdentity *recv_peers);
428
429/**
430 * Called directly before disconnecting from the service
431 */
432typedef void (*PostTest) (struct RPSPeer *peer);
433
434/**
435 * Function called after disconnect to evaluate test success
436 */
437typedef int (*EvaluationCallback) (void);
438
439/**
440 * @brief Do we have Churn?
441 */
442enum OPTION_CHURN
443{
444 /**
445 * @brief If we have churn this is set
446 */
447 HAVE_CHURN,
448 /**
449 * @brief If we have no churn this is set
450 */
451 HAVE_NO_CHURN,
452};
453
454/**
455 * @brief Is it ok to quit the test before the timeout?
456 */
457enum OPTION_QUICK_QUIT
458{
459 /**
460 * @brief It is ok for the test to quit before the timeout triggers
461 */
462 HAVE_QUICK_QUIT,
463
464 /**
465 * @brief It is NOT ok for the test to quit before the timeout triggers
466 */
467 HAVE_NO_QUICK_QUIT,
468};
469
470/**
471 * @brief Do we collect statistics at the end?
472 */
473enum OPTION_COLLECT_STATISTICS
474{
475 /**
476 * @brief We collect statistics at the end
477 */
478 COLLECT_STATISTICS,
479
480 /**
481 * @brief We do not collect statistics at the end
482 */
483 NO_COLLECT_STATISTICS,
484};
485
486/**
487 * @brief Do we collect views during run?
488 */
489enum OPTION_COLLECT_VIEW
490{
491 /**
492 * @brief We collect view during run
493 */
494 COLLECT_VIEW,
495
496 /**
497 * @brief We do not collect the view during run
498 */
499 NO_COLLECT_VIEW,
500};
501
502/**
503 * Structure to define a single test
504 */
505struct SingleTestRun
506{
507 /**
508 * Name of the test
509 */
510 char *name;
511
512 /**
513 * Called with a single peer in order to initialise that peer
514 */
515 InitPeer init_peer;
516
517 /**
518 * Called directly after connecting to the service
519 */
520 PreTest pre_test;
521
522 /**
523 * Main function for each peer
524 */
525 MainTest main_test;
526
527 /**
528 * Callback called once the requested peers are available
529 */
530 ReplyHandle reply_handle;
531
532 /**
533 * Called directly before disconnecting from the service
534 */
535 PostTest post_test;
536
537 /**
538 * Function to evaluate the test results
539 */
540 EvaluationCallback eval_cb;
541
542 /**
543 * Request interval
544 */
545 uint32_t request_interval;
546
547 /**
548 * Number of Requests to make.
549 */
550 uint32_t num_requests;
551
552 /**
553 * Run with (-out) churn
554 */
555 enum OPTION_CHURN have_churn;
556
557 /**
558 * Quit test before timeout?
559 */
560 enum OPTION_QUICK_QUIT have_quick_quit;
561
562 /**
563 * Collect statistics at the end?
564 */
565 enum OPTION_COLLECT_STATISTICS have_collect_statistics;
566
567 /**
568 * Collect view during run?
569 */
570 enum OPTION_COLLECT_VIEW have_collect_view;
571
572 /**
573 * @brief Mark which values from the statistics service to collect at the end
574 * of the run
575 */
576 uint32_t stat_collect_flags;
577} cur_test_run;
578
579/**
580 * Did we finish the test?
581 */
582static int post_test;
583
584/**
585 * Are we shutting down?
586 */
587static int in_shutdown;
588
589/**
590 * Append arguments to file
591 */
592static void
593tofile_ (const char *file_name, const char *line)
594{
595 struct GNUNET_DISK_FileHandle *f;
596 /* char output_buffer[512]; */
597 size_t size;
598 /* int size; */
599 size_t size2;
600
601 if (NULL == (f = GNUNET_DISK_file_open (file_name,
602 GNUNET_DISK_OPEN_APPEND
603 | GNUNET_DISK_OPEN_WRITE
604 | GNUNET_DISK_OPEN_CREATE,
605 GNUNET_DISK_PERM_USER_READ
606 | GNUNET_DISK_PERM_USER_WRITE
607 | GNUNET_DISK_PERM_GROUP_READ
608 | GNUNET_DISK_PERM_OTHER_READ)))
609 {
610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
611 "Not able to open file %s\n",
612 file_name);
613 return;
614 }
615 /* size = GNUNET_snprintf (output_buffer,
616 sizeof (output_buffer),
617 "%llu %s\n",
618 GNUNET_TIME_absolute_get ().abs_value_us,
619 line);
620 if (0 > size)
621 {
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623 "Failed to write string to buffer (size: %i)\n",
624 size);
625 return;
626 } */size = strlen (line) * sizeof(char);
627
628 size2 = GNUNET_DISK_file_write (f, line, size);
629 if (size != size2)
630 {
631 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
632 "Unable to write to file! (Size: %lu, size2: %lu)\n",
633 size,
634 size2);
635 if (GNUNET_YES != GNUNET_DISK_file_close (f))
636 {
637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638 "Unable to close file\n");
639 }
640 return;
641 }
642
643 if (GNUNET_YES != GNUNET_DISK_file_close (f))
644 {
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 "Unable to close file\n");
647 }
648}
649
650
651/**
652 * This function is used to facilitate writing important information to disk
653 */
654#define tofile(file_name, ...) do { \
655 char tmp_buf[512]; \
656 int size; \
657 size = GNUNET_snprintf (tmp_buf, sizeof(tmp_buf), __VA_ARGS__); \
658 if (0 > size) \
659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \
660 "Failed to create tmp_buf\n"); \
661 else \
662 tofile_ (file_name, tmp_buf); \
663} while (0);
664
665
666/**
667 * Write the ids and their according index in the given array to a file
668 * Unused
669 */
670/* static void
671 ids_to_file (char *file_name,
672 struct GNUNET_PeerIdentity *peer_ids,
673 unsigned int num_peer_ids)
674 {
675 unsigned int i;
676
677 for (i=0 ; i < num_peer_ids ; i++)
678 {
679 to_file (file_name,
680 "%u\t%s",
681 i,
682 GNUNET_i2s_full (&peer_ids[i]));
683 }
684 } */
685
686/**
687 * Task run on timeout to collect statistics and potentially shut down.
688 */
689static void
690post_test_op (void *cls);
691
692
693/**
694 * Test the success of a single test
695 */
696static int
697evaluate (void)
698{
699 unsigned int i;
700 int tmp_ok;
701
702 tmp_ok = 1;
703
704 for (i = 0; i < num_peers; i++)
705 {
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
708 i,
709 GNUNET_i2s (rps_peers[i].peer_id),
710 rps_peers[i].num_recv_ids,
711 rps_peers[i].num_ids_to_request,
712 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
713 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
714 }
715 return tmp_ok ? 0 : 1;
716}
717
718
719/**
720 * Creates an oplist entry and adds it to the oplist DLL
721 */
722static struct OpListEntry *
723make_oplist_entry ()
724{
725 struct OpListEntry *entry;
726
727 entry = GNUNET_new (struct OpListEntry);
728 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
729 return entry;
730}
731
732
733/**
734 * @brief Checks if given peer already received its statistics value from the
735 * statistics service.
736 *
737 * @param rps_peer the peer to check for
738 *
739 * @return #GNUNET_YES if so
740 * #GNUNET_NO otherwise
741 */
742static int
743check_statistics_collect_completed_single_peer (
744 const struct RPSPeer *rps_peer)
745{
746 if (cur_test_run.stat_collect_flags !=
747 (cur_test_run.stat_collect_flags
748 & rps_peer->stat_collected_flags))
749 {
750 return GNUNET_NO;
751 }
752 return GNUNET_YES;
753}
754
755
756/**
757 * @brief Checks if all peers already received their statistics value from the
758 * statistics service.
759 *
760 * @return #GNUNET_YES if so
761 * #GNUNET_NO otherwise
762 */
763static int
764check_statistics_collect_completed ()
765{
766 uint32_t i;
767
768 for (i = 0; i < num_peers; i++)
769 {
770 if (GNUNET_NO == check_statistics_collect_completed_single_peer (
771 &rps_peers[i]))
772 {
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "At least Peer %" PRIu32
775 " did not yet receive all statistics values\n",
776 i);
777 return GNUNET_NO;
778 }
779 }
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "All peers received their statistics values\n");
782 return GNUNET_YES;
783}
784
785
786/**
787 * Task run on timeout to shut everything down.
788 */
789static void
790shutdown_op (void *cls)
791{
792 unsigned int i;
793
794 (void) cls;
795
796 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
797 "Shutdown task scheduled, going down.\n");
798 in_shutdown = GNUNET_YES;
799 if (NULL != post_test_task)
800 {
801 GNUNET_SCHEDULER_cancel (post_test_task);
802 post_test_op (NULL);
803 }
804 if (NULL != churn_task)
805 {
806 GNUNET_SCHEDULER_cancel (churn_task);
807 churn_task = NULL;
808 }
809 for (i = 0; i < num_peers; i++)
810 {
811 if (NULL != rps_peers[i].rps_handle)
812 {
813 GNUNET_RPS_disconnect (rps_peers[i].rps_handle);
814 }
815 if (NULL != rps_peers[i].op)
816 {
817 GNUNET_TESTBED_operation_done (rps_peers[i].op);
818 }
819 }
820}
821
822
823/**
824 * Task run on timeout to collect statistics and potentially shut down.
825 */
826static void
827post_test_op (void *cls)
828{
829 unsigned int i;
830
831 (void) cls;
832
833 post_test_task = NULL;
834 post_test = GNUNET_YES;
835 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
836 "Post test task scheduled, going down.\n");
837 if (NULL != churn_task)
838 {
839 GNUNET_SCHEDULER_cancel (churn_task);
840 churn_task = NULL;
841 }
842 for (i = 0; i < num_peers; i++)
843 {
844 if (NULL != cur_test_run.post_test)
845 {
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n",
847 i);
848 cur_test_run.post_test (&rps_peers[i]);
849 }
850 if (NULL != rps_peers[i].op)
851 {
852 GNUNET_TESTBED_operation_done (rps_peers[i].op);
853 rps_peers[i].op = NULL;
854 }
855 }
856 /* If we do not collect statistics, shut down directly */
857 if ((NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics) ||
858 (GNUNET_YES == check_statistics_collect_completed ()) )
859 {
860 GNUNET_SCHEDULER_shutdown ();
861 }
862}
863
864
865/**
866 * Seed peers.
867 */
868static void
869seed_peers (void *cls)
870{
871 struct RPSPeer *peer = cls;
872 unsigned int amount;
873 unsigned int i;
874
875 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
876 {
877 return;
878 }
879
880 GNUNET_assert (NULL != peer->rps_handle);
881
882 // TODO if malicious don't seed mal peers
883 amount = round (.5 * num_peers);
884
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
886 for (i = 0; i < amount; i++)
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
888 i,
889 GNUNET_i2s (&rps_peer_ids[i]));
890
891 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
892}
893
894
895/**
896 * Seed peers.
897 */
898static void
899seed_peers_big (void *cls)
900{
901 struct RPSPeer *peer = cls;
902 unsigned int seed_msg_size;
903 uint32_t num_peers_max;
904 unsigned int amount;
905 unsigned int i;
906
907 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
908 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size)
909 / sizeof(struct GNUNET_PeerIdentity);
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "Peers that fit in one seed msg; %u\n",
912 num_peers_max);
913 amount = num_peers_max + (0.5 * num_peers_max);
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "Seeding many (%u) peers:\n",
916 amount);
917 struct GNUNET_PeerIdentity ids_to_seed[amount];
918 for (i = 0; i < amount; i++)
919 {
920 ids_to_seed[i] = *peer->peer_id;
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
922 i,
923 GNUNET_i2s (&ids_to_seed[i]));
924 }
925
926 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
927}
928
929
930/**
931 * Get the id of peer i.
932 */
933void
934info_cb (void *cb_cls,
935 struct GNUNET_TESTBED_Operation *op,
936 const struct GNUNET_TESTBED_PeerInformation *pinfo,
937 const char *emsg)
938{
939 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
940
941 (void) op;
942
943 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
944 {
945 return;
946 }
947
948 if ((NULL == pinfo) || (NULL != emsg))
949 {
950 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
951 GNUNET_TESTBED_operation_done (entry->op);
952 return;
953 }
954
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Peer %u is %s\n",
957 entry->index,
958 GNUNET_i2s (pinfo->result.id));
959
960 rps_peer_ids[entry->index] = *(pinfo->result.id);
961 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
962
963 GNUNET_assert (GNUNET_OK ==
964 GNUNET_CONTAINER_multipeermap_put (peer_map,
965 &rps_peer_ids[entry->index],
966 &rps_peers[entry->index],
967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
968 tofile ("/tmp/rps/peer_ids",
969 "%u\t%s\n",
970 entry->index,
971 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
972
973 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
974 GNUNET_TESTBED_operation_done (entry->op);
975 GNUNET_free (entry);
976}
977
978
979/**
980 * Callback to be called when RPS service connect operation is completed
981 *
982 * @param cls the callback closure from functions generating an operation
983 * @param op the operation that has been finished
984 * @param ca_result the RPS service handle returned from rps_connect_adapter
985 * @param emsg error message in case the operation has failed; will be NULL if
986 * operation has executed successfully.
987 */
988static void
989rps_connect_complete_cb (void *cls,
990 struct GNUNET_TESTBED_Operation *op,
991 void *ca_result,
992 const char *emsg)
993{
994 struct RPSPeer *rps_peer = cls;
995 struct GNUNET_RPS_Handle *rps = ca_result;
996
997 GNUNET_assert (NULL != ca_result);
998
999 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1000 {
1001 return;
1002 }
1003
1004 rps_peer->rps_handle = rps;
1005 rps_peer->online = GNUNET_YES;
1006 num_peers_online++;
1007
1008 GNUNET_assert (op == rps_peer->op);
1009 if (NULL != emsg)
1010 {
1011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1012 "Failed to connect to RPS service: %s\n",
1013 emsg);
1014 ok = 1;
1015 GNUNET_SCHEDULER_shutdown ();
1016 return;
1017 }
1018
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
1020
1021 cur_test_run.main_test (rps_peer);
1022}
1023
1024
1025/**
1026 * Adapter function called to establish a connection to
1027 * the RPS service.
1028 *
1029 * @param cls closure
1030 * @param cfg configuration of the peer to connect to; will be available until
1031 * GNUNET_TESTBED_operation_done() is called on the operation returned
1032 * from GNUNET_TESTBED_service_connect()
1033 * @return service handle to return in 'op_result', NULL on error
1034 */
1035static void *
1036rps_connect_adapter (void *cls,
1037 const struct GNUNET_CONFIGURATION_Handle *cfg)
1038{
1039 struct GNUNET_RPS_Handle *h;
1040
1041 h = GNUNET_RPS_connect (cfg);
1042 GNUNET_assert (NULL != h);
1043
1044 if (NULL != cur_test_run.pre_test)
1045 cur_test_run.pre_test (cls, h);
1046 GNUNET_assert (NULL != h);
1047
1048 return h;
1049}
1050
1051
1052/**
1053 * Called to open a connection to the peer's statistics
1054 *
1055 * @param cls peer context
1056 * @param cfg configuration of the peer to connect to; will be available until
1057 * GNUNET_TESTBED_operation_done() is called on the operation returned
1058 * from GNUNET_TESTBED_service_connect()
1059 * @return service handle to return in 'op_result', NULL on error
1060 */
1061static void *
1062stat_connect_adapter (void *cls,
1063 const struct GNUNET_CONFIGURATION_Handle *cfg)
1064{
1065 struct RPSPeer *peer = cls;
1066
1067 peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1068 return peer->stats_h;
1069}
1070
1071
1072/**
1073 * Called to disconnect from peer's statistics service
1074 *
1075 * @param cls peer context
1076 * @param op_result service handle returned from the connect adapter
1077 */
1078static void
1079stat_disconnect_adapter (void *cls, void *op_result)
1080{
1081 struct RPSPeer *peer = cls;
1082
1083 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1084 // (peer->stats_h, "core", "# peers connected",
1085 // stat_iterator, peer));
1086 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1087 // (peer->stats_h, "nse", "# peers connected",
1088 // stat_iterator, peer));
1089 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1090 peer->stats_h = NULL;
1091}
1092
1093
1094/**
1095 * Called after successfully opening a connection to a peer's statistics
1096 * service; we register statistics monitoring for CORE and NSE here.
1097 *
1098 * @param cls the callback closure from functions generating an operation
1099 * @param op the operation that has been finished
1100 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1101 * @param emsg error message in case the operation has failed; will be NULL if
1102 * operation has executed successfully.
1103 */
1104static void
1105stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1106 void *ca_result, const char *emsg)
1107{
1108 // struct GNUNET_STATISTICS_Handle *sh = ca_result;
1109 // struct RPSPeer *peer = (struct RPSPeer *) cls;
1110 (void) cls;
1111 (void) op;
1112 (void) ca_result;
1113
1114 if (NULL != emsg)
1115 {
1116 GNUNET_break (0);
1117 return;
1118 }
1119 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1120 // (sh, "core", "# peers connected",
1121 // stat_iterator, peer));
1122 // GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1123 // (sh, "nse", "# peers connected",
1124 // stat_iterator, peer));
1125}
1126
1127
1128/**
1129 * Adapter function called to destroy connection to
1130 * RPS service.
1131 *
1132 * @param cls closure
1133 * @param op_result service handle returned from the connect adapter
1134 */
1135static void
1136rps_disconnect_adapter (void *cls,
1137 void *op_result)
1138{
1139 struct RPSPeer *peer = cls;
1140 struct GNUNET_RPS_Handle *h = op_result;
1141
1142 if (NULL != peer->rps_srh)
1143 {
1144 GNUNET_RPS_stream_cancel (peer->rps_srh);
1145 peer->rps_srh = NULL;
1146 }
1147 GNUNET_assert (NULL != peer);
1148 GNUNET_RPS_disconnect (h);
1149 peer->rps_handle = NULL;
1150}
1151
1152
1153/***********************************************************************
1154* Definition of tests
1155***********************************************************************/
1156
1157// TODO check whether tests can be stopped earlier
1158static int
1159default_eval_cb (void)
1160{
1161 return evaluate ();
1162}
1163
1164
1165static int
1166no_eval (void)
1167{
1168 return 0;
1169}
1170
1171
1172/**
1173 * Initialise given RPSPeer
1174 */
1175static void
1176default_init_peer (struct RPSPeer *rps_peer)
1177{
1178 rps_peer->num_ids_to_request = 1;
1179}
1180
1181
1182/**
1183 * Callback to call on receipt of a reply
1184 *
1185 * @param cls closure
1186 * @param n number of peers
1187 * @param recv_peers the received peers
1188 */
1189static void
1190default_reply_handle (void *cls,
1191 uint64_t n,
1192 const struct GNUNET_PeerIdentity *recv_peers)
1193{
1194 struct RPSPeer *rps_peer;
1195 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1196 unsigned int i;
1197
1198 rps_peer = pending_rep->rps_peer;
1199 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1200 rps_peer->pending_rep_tail,
1201 pending_rep);
1202 rps_peer->num_pending_reps--;
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "[%s] got %" PRIu64 " peers:\n",
1205 GNUNET_i2s (rps_peer->peer_id),
1206 n);
1207
1208 for (i = 0; i < n; i++)
1209 {
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "%u: %s\n",
1212 i,
1213 GNUNET_i2s (&recv_peers[i]));
1214
1215 rps_peer->num_recv_ids++;
1216 }
1217
1218 if ((0 == evaluate ()) && (HAVE_QUICK_QUIT == cur_test_run.have_quick_quit))
1219 {
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
1221 GNUNET_assert (NULL != post_test_task);
1222 GNUNET_SCHEDULER_cancel (post_test_task);
1223 post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1224 GNUNET_assert (NULL != post_test_task);
1225 }
1226}
1227
1228
1229/**
1230 * Request random peers.
1231 */
1232static void
1233request_peers (void *cls)
1234{
1235 struct PendingRequest *pending_req = cls;
1236 struct RPSPeer *rps_peer;
1237 struct PendingReply *pending_rep;
1238
1239 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1240 return;
1241 rps_peer = pending_req->rps_peer;
1242 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1243 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1244 rps_peer->pending_req_tail,
1245 pending_req);
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Requesting one peer\n");
1248 pending_rep = GNUNET_new (struct PendingReply);
1249 pending_rep->rps_peer = rps_peer;
1250 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1251 1,
1252 cur_test_run.reply_handle,
1253 pending_rep);
1254 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1255 rps_peer->pending_rep_tail,
1256 pending_rep);
1257 rps_peer->num_pending_reps++;
1258 rps_peer->num_pending_reqs--;
1259}
1260
1261
1262static void
1263cancel_pending_req (struct PendingRequest *pending_req)
1264{
1265 struct RPSPeer *rps_peer;
1266
1267 rps_peer = pending_req->rps_peer;
1268 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1269 rps_peer->pending_req_tail,
1270 pending_req);
1271 rps_peer->num_pending_reqs--;
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Cancelling pending request\n");
1274 GNUNET_SCHEDULER_cancel (pending_req->request_task);
1275 GNUNET_free (pending_req);
1276}
1277
1278
1279static void
1280cancel_request (struct PendingReply *pending_rep)
1281{
1282 struct RPSPeer *rps_peer;
1283
1284 rps_peer = pending_rep->rps_peer;
1285 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1286 rps_peer->pending_rep_tail,
1287 pending_rep);
1288 rps_peer->num_pending_reps--;
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Cancelling request\n");
1291 GNUNET_RPS_request_cancel (pending_rep->req_handle);
1292 GNUNET_free (pending_rep);
1293}
1294
1295
1296/**
1297 * Cancel a request.
1298 */
1299static void
1300cancel_request_cb (void *cls)
1301{
1302 struct RPSPeer *rps_peer = cls;
1303 struct PendingReply *pending_rep;
1304
1305 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1306 return;
1307 pending_rep = rps_peer->pending_rep_head;
1308 GNUNET_assert (1 <= rps_peer->num_pending_reps);
1309 cancel_request (pending_rep);
1310}
1311
1312
1313/**
1314 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1315 * issued, nor replied
1316 */
1317void
1318schedule_missing_requests (struct RPSPeer *rps_peer)
1319{
1320 unsigned int i;
1321 struct PendingRequest *pending_req;
1322
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Scheduling %u - %u missing requests\n",
1325 rps_peer->num_ids_to_request,
1326 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1327 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1328 rps_peer->num_ids_to_request);
1329 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1330 i < rps_peer->num_ids_to_request; i++)
1331 {
1332 pending_req = GNUNET_new (struct PendingRequest);
1333 pending_req->rps_peer = rps_peer;
1334 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1335 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1336 cur_test_run.request_interval * i),
1337 request_peers,
1338 pending_req);
1339 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1340 rps_peer->pending_req_tail,
1341 pending_req);
1342 rps_peer->num_pending_reqs++;
1343 }
1344}
1345
1346
1347void
1348cancel_pending_req_rep (struct RPSPeer *rps_peer)
1349{
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Cancelling all (pending) requests.\n");
1352 while (NULL != rps_peer->pending_req_head)
1353 cancel_pending_req (rps_peer->pending_req_head);
1354 GNUNET_assert (0 == rps_peer->num_pending_reqs);
1355 while (NULL != rps_peer->pending_rep_head)
1356 cancel_request (rps_peer->pending_rep_head);
1357 GNUNET_assert (0 == rps_peer->num_pending_reps);
1358}
1359
1360
1361/***********************************
1362* MALICIOUS
1363***********************************/
1364
1365/**
1366 * Initialise only non-mal RPSPeers
1367 */
1368static void
1369mal_init_peer (struct RPSPeer *rps_peer)
1370{
1371 if (rps_peer->index >= round (portion * num_peers))
1372 rps_peer->num_ids_to_request = 1;
1373}
1374
1375
1376/**
1377 * @brief Set peers to (non-)malicious before execution
1378 *
1379 * Of signature #PreTest
1380 *
1381 * @param rps_peer the peer to set (non-) malicious
1382 * @param h the handle to the service
1383 */
1384static void
1385mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1386{
1387 #if ENABLE_MALICIOUS
1388 uint32_t num_mal_peers;
1389
1390 GNUNET_assert ((1 >= portion) &&
1391 (0 < portion));
1392 num_mal_peers = round (portion * num_peers);
1393
1394 if (rps_peer->index < num_mal_peers)
1395 {
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "%u. peer [%s] of %" PRIu32
1398 " malicious peers turning malicious\n",
1399 rps_peer->index,
1400 GNUNET_i2s (rps_peer->peer_id),
1401 num_mal_peers);
1402
1403 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1404 rps_peer_ids, target_peer);
1405 }
1406 #endif /* ENABLE_MALICIOUS */
1407}
1408
1409
1410static void
1411mal_cb (struct RPSPeer *rps_peer)
1412{
1413 uint32_t num_mal_peers;
1414
1415 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1416 {
1417 return;
1418 }
1419
1420 #if ENABLE_MALICIOUS
1421 GNUNET_assert ((1 >= portion) &&
1422 (0 < portion));
1423 num_mal_peers = round (portion * num_peers);
1424
1425 if (rps_peer->index >= num_mal_peers)
1426 { /* It's useless to ask a malicious peer about a random sample -
1427 it's not sampling */
1428 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
1429 GNUNET_TIME_UNIT_SECONDS, 2),
1430 seed_peers, rps_peer);
1431 schedule_missing_requests (rps_peer);
1432 }
1433 #endif /* ENABLE_MALICIOUS */
1434}
1435
1436
1437/***********************************
1438* SINGLE_REQUEST
1439***********************************/
1440static void
1441single_req_cb (struct RPSPeer *rps_peer)
1442{
1443 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1444 {
1445 return;
1446 }
1447
1448 schedule_missing_requests (rps_peer);
1449}
1450
1451
1452/***********************************
1453* DELAYED_REQUESTS
1454***********************************/
1455static void
1456delay_req_cb (struct RPSPeer *rps_peer)
1457{
1458 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1459 {
1460 return;
1461 }
1462
1463 schedule_missing_requests (rps_peer);
1464}
1465
1466
1467/***********************************
1468* SEED
1469***********************************/
1470static void
1471seed_cb (struct RPSPeer *rps_peer)
1472{
1473 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1474 {
1475 return;
1476 }
1477
1478 GNUNET_SCHEDULER_add_delayed (
1479 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1480 seed_peers, rps_peer);
1481}
1482
1483
1484/***********************************
1485* SEED_BIG
1486***********************************/
1487static void
1488seed_big_cb (struct RPSPeer *rps_peer)
1489{
1490 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1491 {
1492 return;
1493 }
1494
1495 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1496 GNUNET_SCHEDULER_add_delayed (
1497 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1498 seed_peers_big, rps_peer);
1499}
1500
1501
1502/***********************************
1503* SINGLE_PEER_SEED
1504***********************************/
1505static void
1506single_peer_seed_cb (struct RPSPeer *rps_peer)
1507{
1508 (void) rps_peer;
1509 // TODO
1510}
1511
1512
1513/***********************************
1514* SEED_REQUEST
1515***********************************/
1516static void
1517seed_req_cb (struct RPSPeer *rps_peer)
1518{
1519 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1520 {
1521 return;
1522 }
1523
1524 GNUNET_SCHEDULER_add_delayed (
1525 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1526 seed_peers, rps_peer);
1527 schedule_missing_requests (rps_peer);
1528}
1529
1530
1531// TODO start big mal
1532
1533/***********************************
1534* REQUEST_CANCEL
1535***********************************/
1536static void
1537req_cancel_cb (struct RPSPeer *rps_peer)
1538{
1539 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1540 {
1541 return;
1542 }
1543
1544 schedule_missing_requests (rps_peer);
1545 GNUNET_SCHEDULER_add_delayed (
1546 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1547 (cur_test_run.request_interval + 1)),
1548 cancel_request_cb, rps_peer);
1549}
1550
1551
1552/***********************************
1553* CHURN
1554***********************************/
1555
1556static void
1557churn (void *cls);
1558
1559/**
1560 * @brief Starts churn
1561 *
1562 * Has signature of #MainTest
1563 *
1564 * This is not implemented too nicely as this is called for each peer, but we
1565 * only need to call it once. (Yes we check that we only schedule the task
1566 * once.)
1567 *
1568 * @param rps_peer The peer it's called for
1569 */
1570static void
1571churn_test_cb (struct RPSPeer *rps_peer)
1572{
1573 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1574 {
1575 return;
1576 }
1577
1578 /* Start churn */
1579 if ((HAVE_CHURN == cur_test_run.have_churn) && (NULL == churn_task))
1580 {
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "Starting churn task\n");
1583 churn_task = GNUNET_SCHEDULER_add_delayed (
1584 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1585 churn,
1586 NULL);
1587 }
1588 else
1589 {
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Not starting churn task\n");
1592 }
1593
1594 schedule_missing_requests (rps_peer);
1595}
1596
1597
1598/***********************************
1599* SUB
1600***********************************/
1601
1602static void
1603got_stream_peer_cb (void *cls,
1604 uint64_t num_peers,
1605 const struct GNUNET_PeerIdentity *peers)
1606{
1607 const struct RPSPeer *rps_peer = cls;
1608
1609 for (uint64_t i = 0; i < num_peers; i++)
1610 {
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Peer %" PRIu32 " received [%s] from stream.\n",
1613 rps_peer->index,
1614 GNUNET_i2s (&peers[i]));
1615 if ((0 != rps_peer->index) &&
1616 (0 == memcmp (&peers[i],
1617 &rps_peers[0].peer_id,
1618 sizeof(struct GNUNET_PeerIdentity))))
1619 {
1620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1621 "Received a peer id outside sub\n");
1622 ok = 1;
1623 }
1624 else if ((0 == rps_peer->index) &&
1625 (0 != memcmp (&peers[i],
1626 &rps_peers[0].peer_id,
1627 sizeof(struct GNUNET_PeerIdentity))))
1628 {
1629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1630 "Received a peer id outside sub (lonely)\n");
1631 ok = 1;
1632 }
1633 }
1634}
1635
1636
1637static void
1638sub_post (struct RPSPeer *rps_peer)
1639{
1640 if (0 != rps_peer->index)
1641 GNUNET_RPS_sub_stop (rps_peer->rps_handle, "test");
1642 else
1643 GNUNET_RPS_sub_stop (rps_peer->rps_handle, "lonely");
1644}
1645
1646
1647static void
1648sub_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1649{
1650 (void) rps_peer;
1651
1652 if (0 != rps_peer->index)
1653 GNUNET_RPS_sub_start (h, "test");
1654 else
1655 GNUNET_RPS_sub_start (h, "lonely"); /* have a group of one */
1656 rps_peer->rps_srh = GNUNET_RPS_stream_request (h,
1657 &got_stream_peer_cb,
1658 rps_peer);
1659}
1660
1661
1662/***********************************
1663* PROFILER
1664***********************************/
1665
1666/**
1667 * Callback to be called when RPS service is started or stopped at peers
1668 *
1669 * @param cls NULL
1670 * @param op the operation handle
1671 * @param emsg NULL on success; otherwise an error description
1672 */
1673static void
1674churn_cb (void *cls,
1675 struct GNUNET_TESTBED_Operation *op,
1676 const char *emsg)
1677{
1678 (void) op;
1679 // FIXME
1680 struct OpListEntry *entry = cls;
1681
1682 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1683 {
1684 return;
1685 }
1686
1687 GNUNET_TESTBED_operation_done (entry->op);
1688 if (NULL != emsg)
1689 {
1690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1691 "Failed to start/stop RPS at a peer\n");
1692 GNUNET_SCHEDULER_shutdown ();
1693 return;
1694 }
1695 GNUNET_assert (0 != entry->delta);
1696
1697 num_peers_online += entry->delta;
1698
1699 if (PEER_GO_OFFLINE == entry->delta)
1700 { /* Peer hopefully just went offline */
1701 if (GNUNET_YES != rps_peers[entry->index].online)
1702 {
1703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1704 "peer %s was expected to go offline but is still marked as online\n",
1705 GNUNET_i2s (rps_peers[entry->index].peer_id));
1706 GNUNET_break (0);
1707 }
1708 else
1709 {
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "peer %s probably went offline as expected\n",
1712 GNUNET_i2s (rps_peers[entry->index].peer_id));
1713 }
1714 rps_peers[entry->index].online = GNUNET_NO;
1715 }
1716
1717 else if (PEER_GO_ONLINE < entry->delta)
1718 { /* Peer hopefully just went online */
1719 if (GNUNET_NO != rps_peers[entry->index].online)
1720 {
1721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1722 "peer %s was expected to go online but is still marked as offline\n",
1723 GNUNET_i2s (rps_peers[entry->index].peer_id));
1724 GNUNET_break (0);
1725 }
1726 else
1727 {
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1729 "peer %s probably went online as expected\n",
1730 GNUNET_i2s (rps_peers[entry->index].peer_id));
1731 if (NULL != cur_test_run.pre_test)
1732 {
1733 cur_test_run.pre_test (&rps_peers[entry->index],
1734 rps_peers[entry->index].rps_handle);
1735 schedule_missing_requests (&rps_peers[entry->index]);
1736 }
1737 }
1738 rps_peers[entry->index].online = GNUNET_YES;
1739 }
1740 else
1741 {
1742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1743 "Invalid value for delta: %i\n", entry->delta);
1744 GNUNET_break (0);
1745 }
1746
1747 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1748 rps_peers[entry->index].entry_op_manage = NULL;
1749 GNUNET_free (entry);
1750 // if (num_peers_in_round[current_round] == peers_running)
1751 // run_round ();
1752}
1753
1754
1755/**
1756 * @brief Set the rps-service up or down for a specific peer
1757 *
1758 * @param i index of action
1759 * @param j index of peer
1760 * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1761 * @param prob_go_on_off the probability of the action
1762 */
1763static void
1764manage_service_wrapper (unsigned int i, unsigned int j,
1765 enum PEER_ONLINE_DELTA delta,
1766 double prob_go_on_off)
1767{
1768 struct OpListEntry *entry = NULL;
1769 uint32_t prob;
1770
1771 /* make sure that management operation is not already scheduled */
1772 if (NULL != rps_peers[j].entry_op_manage)
1773 {
1774 return;
1775 }
1776
1777 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1778 UINT32_MAX);
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "%u. selected peer (%u: %s) is %s.\n",
1781 i,
1782 j,
1783 GNUNET_i2s (rps_peers[j].peer_id),
1784 (PEER_GO_ONLINE == delta) ? "online" : "offline");
1785 if (prob < prob_go_on_off * UINT32_MAX)
1786 {
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "%s goes %s\n",
1789 GNUNET_i2s (rps_peers[j].peer_id),
1790 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1791
1792 if (PEER_GO_OFFLINE == delta)
1793 cancel_pending_req_rep (&rps_peers[j]);
1794 entry = make_oplist_entry ();
1795 entry->delta = delta;
1796 entry->index = j;
1797 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1798 testbed_peers[j],
1799 "rps",
1800 &churn_cb,
1801 entry,
1802 (PEER_GO_OFFLINE == delta) ?
1803 0 : 1);
1804 rps_peers[j].entry_op_manage = entry;
1805 }
1806}
1807
1808
1809static void
1810churn (void *cls)
1811{
1812 (void) cls;
1813 unsigned int i;
1814 unsigned int j;
1815 double portion_online;
1816 unsigned int *permut;
1817 double prob_go_offline;
1818 double portion_go_online;
1819 double portion_go_offline;
1820
1821 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1822 {
1823 return;
1824 }
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "Churn function executing\n");
1827
1828 churn_task = NULL; /* Should be invalid by now */
1829
1830 /* Compute the probability for an online peer to go offline
1831 * this round */
1832 portion_online = num_peers_online * 1.0 / num_peers;
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 "Portion online: %f\n",
1835 portion_online);
1836 portion_go_online = ((1 - portion_online) * .5 * .66);
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "Portion that should go online: %f\n",
1839 portion_go_online);
1840 portion_go_offline = (portion_online + portion_go_online) - .75;
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "Portion that probably goes offline: %f\n",
1843 portion_go_offline);
1844 prob_go_offline = portion_go_offline / (portion_online * .5);
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "Probability of a selected online peer to go offline: %f\n",
1847 prob_go_offline);
1848
1849 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1850 (unsigned int) num_peers);
1851
1852 /* Go over 50% randomly chosen peers */
1853 for (i = 0; i < .5 * num_peers; i++)
1854 {
1855 j = permut[i];
1856
1857 /* If online, shut down with certain probability */
1858 if (GNUNET_YES == rps_peers[j].online)
1859 {
1860 manage_service_wrapper (i, j, -1, prob_go_offline);
1861 }
1862
1863 /* If offline, restart with certain probability */
1864 else if (GNUNET_NO == rps_peers[j].online)
1865 {
1866 manage_service_wrapper (i, j, 1, 0.66);
1867 }
1868 }
1869
1870 GNUNET_free (permut);
1871
1872 churn_task = GNUNET_SCHEDULER_add_delayed (
1873 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1874 churn,
1875 NULL);
1876}
1877
1878
1879/**
1880 * Initialise given RPSPeer
1881 */
1882static void
1883profiler_init_peer (struct RPSPeer *rps_peer)
1884{
1885 if (num_peers - 1 == rps_peer->index)
1886 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1887}
1888
1889
1890/**
1891 * Callback to call on receipt of a reply
1892 *
1893 * @param cls closure
1894 * @param n number of peers
1895 * @param recv_peers the received peers
1896 */
1897static void
1898profiler_reply_handle (void *cls,
1899 uint64_t n,
1900 const struct GNUNET_PeerIdentity *recv_peers)
1901{
1902 struct RPSPeer *rps_peer;
1903 struct RPSPeer *rcv_rps_peer;
1904 char *file_name;
1905 char *file_name_dh;
1906 unsigned int i;
1907 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1908
1909 rps_peer = pending_rep->rps_peer;
1910 file_name = "/tmp/rps/received_ids";
1911 file_name_dh = "/tmp/rps/diehard_input";
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "[%s] got %" PRIu64 " peers:\n",
1914 GNUNET_i2s (rps_peer->peer_id),
1915 n);
1916 for (i = 0; i < n; i++)
1917 {
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 "%u: %s\n",
1920 i,
1921 GNUNET_i2s (&recv_peers[i]));
1922 tofile (file_name,
1923 "%s\n",
1924 GNUNET_i2s_full (&recv_peers[i]));
1925 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1926 GNUNET_assert (NULL != rcv_rps_peer);
1927 tofile (file_name_dh,
1928 "%" PRIu32 "\n",
1929 (uint32_t) rcv_rps_peer->index);
1930 }
1931 default_reply_handle (cls, n, recv_peers);
1932}
1933
1934
1935static void
1936profiler_cb (struct RPSPeer *rps_peer)
1937{
1938 if ((GNUNET_YES == in_shutdown) || (GNUNET_YES == post_test))
1939 {
1940 return;
1941 }
1942
1943 /* Start churn */
1944 if ((HAVE_CHURN == cur_test_run.have_churn) && (NULL == churn_task))
1945 {
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "Starting churn task\n");
1948 churn_task = GNUNET_SCHEDULER_add_delayed (
1949 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1950 churn,
1951 NULL);
1952 }
1953 else
1954 {
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 "Not starting churn task\n");
1957 }
1958
1959 /* Only request peer ids at one peer.
1960 * (It's the before-last because last one is target of the focused attack.)
1961 */
1962 if (eval_peer == rps_peer)
1963 schedule_missing_requests (rps_peer);
1964}
1965
1966
1967/**
1968 * Function called from #profiler_eval with a filename.
1969 *
1970 * @param cls closure
1971 * @param filename complete filename (absolute path)
1972 * @return #GNUNET_OK to continue to iterate,
1973 * #GNUNET_NO to stop iteration with no error,
1974 * #GNUNET_SYSERR to abort iteration with error!
1975 */
1976int
1977file_name_cb (void *cls, const char *filename)
1978{
1979 (void) cls;
1980
1981 if (NULL != strstr (filename, "sampler_el"))
1982 {
1983 struct RPS_SamplerElement *s_elem;
1984 struct GNUNET_CRYPTO_AuthKey auth_key;
1985 const char *key_char;
1986 uint32_t i;
1987
1988 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1989 tofile (filename, "--------------------------\n");
1990
1991 auth_key = string_to_auth_key (key_char);
1992 s_elem = RPS_sampler_elem_create ();
1993 RPS_sampler_elem_set (s_elem, auth_key);
1994
1995 for (i = 0; i < num_peers; i++)
1996 {
1997 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1998 }
1999 RPS_sampler_elem_destroy (s_elem);
2000 }
2001 return GNUNET_OK;
2002}
2003
2004
2005/**
2006 * This is run after the test finished.
2007 *
2008 * Compute all perfect samples.
2009 */
2010int
2011profiler_eval (void)
2012{
2013 /* Compute perfect sample for each sampler element */
2014 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
2015 {
2016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
2017 }
2018
2019 return evaluate ();
2020}
2021
2022
2023/**
2024 * @brief is b in view of a?
2025 *
2026 * @param a
2027 * @param b
2028 *
2029 * @return
2030 */
2031static int
2032is_in_view (uint32_t a, uint32_t b)
2033{
2034 uint32_t i;
2035
2036 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2037 {
2038 if (0 == memcmp (rps_peers[b].peer_id,
2039 &rps_peers[a].cur_view[i],
2040 sizeof(struct GNUNET_PeerIdentity)))
2041 {
2042 return GNUNET_YES;
2043 }
2044 }
2045 return GNUNET_NO;
2046}
2047
2048
2049static uint32_t
2050get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
2051{
2052 uint32_t i;
2053
2054 for (i = 0; i < num_peers; i++)
2055 {
2056 if (0 == memcmp (pid,
2057 rps_peers[i].peer_id,
2058 sizeof(struct GNUNET_PeerIdentity)))
2059 {
2060 return i;
2061 }
2062 }
2063 // return 0; /* Should not happen - make compiler happy */
2064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2065 "No known _PeerIdentity %s!\n",
2066 GNUNET_i2s_full (pid));
2067 GNUNET_assert (0);
2068}
2069
2070
2071/**
2072 * @brief Counts number of peers in view of a that have b in their view
2073 *
2074 * @param a
2075 * @param uint32_tb
2076 *
2077 * @return
2078 */
2079static uint32_t
2080count_containing_views (uint32_t a, uint32_t b)
2081{
2082 uint32_t i;
2083 uint32_t peer_idx;
2084 uint32_t count = 0;
2085
2086 for (i = 0; i < rps_peers[a].cur_view_count; i++)
2087 {
2088 peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
2089 if (GNUNET_YES == is_in_view (peer_idx, b))
2090 {
2091 count++;
2092 }
2093 }
2094 return count;
2095}
2096
2097
2098/**
2099 * @brief Computes the probability for each other peer to be selected by the
2100 * sampling process based on the views of all peers
2101 *
2102 * @param peer_idx index of the peer that is about to sample
2103 */
2104static void
2105compute_probabilities (uint32_t peer_idx)
2106{
2107 // double probs[num_peers] = { 0 };
2108 double probs[num_peers];
2109 size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof(char);
2110 char *probs_as_str = GNUNET_malloc (probs_as_str_size);
2111 char *probs_as_str_cpy;
2112 uint32_t i;
2113 double prob_push;
2114 double prob_pull;
2115 uint32_t view_size;
2116 uint32_t cont_views;
2117 uint32_t number_of_being_in_pull_events;
2118 int tmp;
2119 uint32_t count_non_zero_prob = 0;
2120
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2122 "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
2123 /* Firstly without knowledge of old views */
2124 for (i = 0; i < num_peers; i++)
2125 {
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 "\tfor peer %" PRIu32 ":\n", i);
2128 view_size = rps_peers[i].cur_view_count;
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2130 "\t\tview_size: %" PRIu32 "\n", view_size);
2131 /* For peer i the probability of being sampled is
2132 * evenly distributed among all possibly observed peers. */
2133 /* We could have observed a peer in three cases:
2134 * 1. peer sent a push
2135 * 2. peer was contained in a pull reply
2136 * 3. peer was in history (sampler) - ignored for now */
2137 /* 1. Probability of having received a push from peer i */
2138 if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
2139 (1 <= (0.45 * view_size)))
2140 {
2141 prob_push = 1.0 * binom (0.45 * view_size, 1)
2142 /
2143 binom (view_size, 0.45 * view_size);
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2146 peer_idx,
2147 i,
2148 prob_push);
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 "\t\tposs choices from view: %" PRIu32 ", containing i: %"
2151 PRIu32 "\n",
2152 binom (view_size, 0.45 * view_size),
2153 binom (0.45 * view_size, 1));
2154 }
2155 else
2156 {
2157 prob_push = 0;
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2160 peer_idx,
2161 i);
2162 }
2163 /* 2. Probability of peer i being contained in pulls */
2164 view_size = rps_peers[peer_idx].cur_view_count;
2165 cont_views = count_containing_views (peer_idx, i);
2166 number_of_being_in_pull_events =
2167 (binom (view_size, 0.45 * view_size)
2168 - binom (view_size - cont_views, 0.45 * view_size));
2169 if (0 != number_of_being_in_pull_events)
2170 {
2171 prob_pull = number_of_being_in_pull_events
2172 /
2173 (1.0 * binom (view_size, 0.45 * view_size));
2174 }
2175 else
2176 {
2177 prob_pull = 0;
2178 }
2179 probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2181 "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2182 " peers in its view who know %" PRIu32 " prob: %f\n",
2183 peer_idx,
2184 cont_views,
2185 view_size,
2186 i,
2187 prob_pull);
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2190 binom (view_size, 0.45 * view_size));
2191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2192 "\t\tnumber of possible pull combinations without %" PRIu32
2193 ": %" PRIu32 "\n",
2194 i,
2195 binom (view_size - cont_views, 0.45 * view_size));
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 "\t\tnumber of possible pull combinations with %" PRIu32
2198 ": %" PRIu32 "\n",
2199 i,
2200 number_of_being_in_pull_events);
2201
2202 if (0 != probs[i])
2203 count_non_zero_prob++;
2204 }
2205 /* normalize */
2206 if (0 != count_non_zero_prob)
2207 {
2208 for (i = 0; i < num_peers; i++)
2209 {
2210 probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2211 }
2212 }
2213 else
2214 {
2215 for (i = 0; i < num_peers; i++)
2216 {
2217 probs[i] = 0;
2218 }
2219 }
2220 /* str repr */
2221 for (i = 0; i < num_peers; i++)
2222 {
2223 probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2224 tmp = GNUNET_snprintf (probs_as_str,
2225 probs_as_str_size,
2226 "%s %7.6f", probs_as_str_cpy, probs[i]);
2227 GNUNET_free (probs_as_str_cpy);
2228 GNUNET_assert (0 <= tmp);
2229 }
2230
2231 to_file_w_len (rps_peers[peer_idx].file_name_probs,
2232 probs_as_str_size,
2233 probs_as_str);
2234 GNUNET_free (probs_as_str);
2235}
2236
2237
2238/**
2239 * @brief This counts the number of peers in which views a given peer occurs.
2240 *
2241 * It also stores this value in the rps peer.
2242 *
2243 * @param peer_idx the index of the peer to count the representation
2244 *
2245 * @return the number of occurrences
2246 */
2247static uint32_t
2248count_peer_in_views_2 (uint32_t peer_idx)
2249{
2250 uint32_t i, j;
2251 uint32_t count = 0;
2252
2253 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2254 {
2255 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2256 {
2257 if (0 == memcmp (rps_peers[peer_idx].peer_id,
2258 &rps_peers[i].cur_view[j],
2259 sizeof(struct GNUNET_PeerIdentity)))
2260 {
2261 count++;
2262 break;
2263 }
2264 }
2265 }
2266 rps_peers[peer_idx].count_in_views = count;
2267 return count;
2268}
2269
2270
2271static uint32_t
2272cumulated_view_sizes ()
2273{
2274 uint32_t i;
2275
2276 view_sizes = 0;
2277 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2278 {
2279 view_sizes += rps_peers[i].cur_view_count;
2280 }
2281 return view_sizes;
2282}
2283
2284
2285static void
2286count_peer_in_views (uint32_t *count_peers)
2287{
2288 uint32_t i, j;
2289
2290 for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2291 {
2292 for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2293 {
2294 if (0 == memcmp (rps_peers[i].peer_id,
2295 &rps_peers[i].cur_view[j],
2296 sizeof(struct GNUNET_PeerIdentity)))
2297 {
2298 count_peers[i]++;
2299 }
2300 }
2301 }
2302}
2303
2304
2305void
2306compute_diversity ()
2307{
2308 uint32_t i;
2309 /* ith entry represents the number of occurrences in other peer's views */
2310 uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2311 uint32_t views_total_size;
2312 double expected;
2313 /* deviation from expected number of peers */
2314 double *deviation = GNUNET_new_array (num_peers, double);
2315
2316 views_total_size = 0;
2317 expected = 0;
2318
2319 /* For each peer count its representation in other peer's views*/
2320 for (i = 0; i < num_peers; i++) /* Peer to count */
2321 {
2322 views_total_size += rps_peers[i].cur_view_count;
2323 count_peer_in_views (count_peers);
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32
2326 "\n",
2327 i,
2328 GNUNET_i2s (rps_peers[i].peer_id),
2329 count_peers[i]);
2330 }
2331
2332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333 "size of all views combined: %" PRIu32 "\n",
2334 views_total_size);
2335 expected = ((double) 1 / num_peers) * views_total_size;
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "Expected number of occurrences of each peer in all views: %f\n",
2338 expected);
2339 for (i = 0; i < num_peers; i++) /* Peer to count */
2340 {
2341 deviation[i] = expected - count_peers[i];
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 "Deviation from expectation: %f\n", deviation[i]);
2344 }
2345 GNUNET_free (count_peers);
2346 GNUNET_free (deviation);
2347}
2348
2349
2350void
2351print_view_sizes ()
2352{
2353 uint32_t i;
2354
2355 for (i = 0; i < num_peers; i++) /* Peer to count */
2356 {
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2358 "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2359 i,
2360 GNUNET_i2s (rps_peers[i].peer_id),
2361 rps_peers[i].cur_view_count);
2362 }
2363}
2364
2365
2366void
2367all_views_updated_cb ()
2368{
2369 compute_diversity ();
2370 print_view_sizes ();
2371}
2372
2373
2374void
2375view_update_cb (void *cls,
2376 uint64_t view_size,
2377 const struct GNUNET_PeerIdentity *peers)
2378{
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "View was updated (%" PRIu64 ")\n", view_size);
2381 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2382 to_file ("/tmp/rps/view_sizes.txt",
2383 "%" PRIu32 " %" PRIu64 "",
2384 rps_peer->index,
2385 view_size);
2386 for (uint64_t i = 0; i < view_size; i++)
2387 {
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2389 "\t%s\n", GNUNET_i2s (&peers[i]));
2390 }
2391 GNUNET_array_grow (rps_peer->cur_view,
2392 rps_peer->cur_view_count,
2393 view_size);
2394 // *rps_peer->cur_view = *peers;
2395 GNUNET_memcpy (rps_peer->cur_view,
2396 peers,
2397 view_size * sizeof(struct GNUNET_PeerIdentity));
2398 to_file ("/tmp/rps/count_in_views.txt",
2399 "%" PRIu32 " %" PRIu32 "",
2400 rps_peer->index,
2401 count_peer_in_views_2 (rps_peer->index));
2402 cumulated_view_sizes ();
2403 if (0 != view_size)
2404 {
2405 to_file ("/tmp/rps/repr.txt",
2406 "%" PRIu32 /* index */
2407 " %" PRIu32 /* occurrence in views */
2408 " %" PRIu32 /* view sizes */
2409 " %f" /* fraction of repr in views */
2410 " %f" /* average view size */
2411 " %f" /* prob of occurrence in view slot */
2412 " %f" "", /* exp frac of repr in views */
2413 rps_peer->index,
2414 count_peer_in_views_2 (rps_peer->index),
2415 view_sizes,
2416 count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2417 view_sizes / (view_size * 1.0), /* average view size */
2418 1.0 / view_size, /* prob of occurrence in view slot */
2419 (1.0 / view_size) * (view_sizes / view_size) /* expected fraction of repr in views */
2420 );
2421 }
2422 compute_probabilities (rps_peer->index);
2423 all_views_updated_cb ();
2424}
2425
2426
2427static void
2428pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2429{
2430 rps_peer->file_name_probs =
2431 store_prefix_file_name (rps_peer->index, "probs");
2432 GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2433}
2434
2435
2436void
2437write_final_stats (void)
2438{
2439 uint32_t i;
2440
2441 for (i = 0; i < num_peers; i++)
2442 {
2443 to_file ("/tmp/rps/final_stats.dat",
2444 "%" PRIu32 " " /* index */
2445 "%s %" /* id */
2446 PRIu64 " %" /* rounds */
2447 PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
2448 " %" /* blocking */
2449 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* issued */
2450 PRIu64 " %" PRIu64 " %" PRIu64 " %" /* sent */
2451 PRIu64 " %" PRIu64 " %" PRIu64 /* recv */,
2452 i,
2453 GNUNET_i2s (rps_peers[i].peer_id),
2454 rps_peers[i].num_rounds,
2455 rps_peers[i].num_blocks,
2456 rps_peers[i].num_blocks_many_push,
2457 rps_peers[i].num_blocks_no_push,
2458 rps_peers[i].num_blocks_no_pull,
2459 rps_peers[i].num_blocks_many_push_no_pull,
2460 rps_peers[i].num_blocks_no_push_no_pull,
2461 rps_peers[i].num_issued_push,
2462 rps_peers[i].num_issued_pull_req,
2463 rps_peers[i].num_issued_pull_rep,
2464 rps_peers[i].num_sent_push,
2465 rps_peers[i].num_sent_pull_req,
2466 rps_peers[i].num_sent_pull_rep,
2467 rps_peers[i].num_recv_push,
2468 rps_peers[i].num_recv_pull_req,
2469 rps_peers[i].num_recv_pull_rep);
2470 }
2471}
2472
2473
2474/**
2475 * Continuation called by #GNUNET_STATISTICS_get() functions.
2476 *
2477 * Remembers that this specific statistics value was received for this peer.
2478 * Checks whether all peers received their statistics yet.
2479 * Issues the shutdown.
2480 *
2481 * @param cls closure
2482 * @param success #GNUNET_OK if statistics were
2483 * successfully obtained, #GNUNET_SYSERR if not.
2484 */
2485void
2486post_test_shutdown_ready_cb (void *cls,
2487 int success)
2488{
2489 struct STATcls *stat_cls = (struct STATcls *) cls;
2490 struct RPSPeer *rps_peer = stat_cls->rps_peer;
2491
2492 if (GNUNET_OK == success)
2493 {
2494 /* set flag that we we got the value */
2495 rps_peer->stat_collected_flags |= stat_cls->stat_type;
2496 }
2497 else
2498 {
2499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2500 "Peer %u did not receive statistics value\n",
2501 rps_peer->index);
2502 GNUNET_free (stat_cls);
2503 GNUNET_break (0);
2504 }
2505
2506 if ((NULL != rps_peer->stat_op) &&
2507 (GNUNET_YES == check_statistics_collect_completed_single_peer (
2508 rps_peer)) )
2509 {
2510 GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2511 }
2512
2513 write_final_stats ();
2514 if (GNUNET_YES == check_statistics_collect_completed ())
2515 {
2516 // write_final_stats ();
2517 GNUNET_free (stat_cls);
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 "Shutting down\n");
2520 GNUNET_SCHEDULER_shutdown ();
2521 }
2522 else
2523 {
2524 GNUNET_free (stat_cls);
2525 }
2526}
2527
2528
2529/**
2530 * @brief Converts string representation to the corresponding #STAT_TYPE enum.
2531 *
2532 * @param stat_str string representation of statistics specifier
2533 *
2534 * @return corresponding enum
2535 */
2536enum STAT_TYPE
2537stat_str_2_type (const char *stat_str)
2538{
2539 if (0 == strncmp ("# rounds blocked - no pull replies", stat_str, strlen (
2540 "# rounds blocked - no pull replies")))
2541 {
2542 return STAT_TYPE_BLOCKS_NO_PULL;
2543 }
2544 else if (0 == strncmp ("# rounds blocked - too many pushes, no pull replies",
2545 stat_str, strlen (
2546 "# rounds blocked - too many pushes, no pull replies")))
2547 {
2548 return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
2549 }
2550 else if (0 == strncmp ("# rounds blocked - too many pushes", stat_str,
2551 strlen ("# rounds blocked - too many pushes")))
2552 {
2553 return STAT_TYPE_BLOCKS_MANY_PUSH;
2554 }
2555 else if (0 == strncmp ("# rounds blocked - no pushes, no pull replies",
2556 stat_str, strlen (
2557 "# rounds blocked - no pushes, no pull replies")))
2558 {
2559 return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
2560 }
2561 else if (0 == strncmp ("# rounds blocked - no pushes", stat_str, strlen (
2562 "# rounds blocked - no pushes")))
2563 {
2564 return STAT_TYPE_BLOCKS_NO_PUSH;
2565 }
2566 else if (0 == strncmp ("# rounds blocked", stat_str, strlen (
2567 "# rounds blocked")))
2568 {
2569 return STAT_TYPE_BLOCKS;
2570 }
2571 else if (0 == strncmp ("# rounds", stat_str, strlen ("# rounds")))
2572 {
2573 return STAT_TYPE_ROUNDS;
2574 }
2575 else if (0 == strncmp ("# push send issued", stat_str, strlen (
2576 "# push send issued")))
2577 {
2578 return STAT_TYPE_ISSUED_PUSH_SEND;
2579 }
2580 else if (0 == strncmp ("# pull request send issued", stat_str, strlen (
2581 "# pull request send issued")))
2582 {
2583 return STAT_TYPE_ISSUED_PULL_REQ;
2584 }
2585 else if (0 == strncmp ("# pull reply send issued", stat_str, strlen (
2586 "# pull reply send issued")))
2587 {
2588 return STAT_TYPE_ISSUED_PULL_REP;
2589 }
2590 else if (0 == strncmp ("# pushes sent", stat_str, strlen ("# pushes sent")))
2591 {
2592 return STAT_TYPE_SENT_PUSH_SEND;
2593 }
2594 else if (0 == strncmp ("# pull requests sent", stat_str, strlen (
2595 "# pull requests sent")))
2596 {
2597 return STAT_TYPE_SENT_PULL_REQ;
2598 }
2599 else if (0 == strncmp ("# pull replies sent", stat_str, strlen (
2600 "# pull replies sent")))
2601 {
2602 return STAT_TYPE_SENT_PULL_REP;
2603 }
2604 else if (0 == strncmp ("# push message received", stat_str, strlen (
2605 "# push message received")))
2606 {
2607 return STAT_TYPE_RECV_PUSH_SEND;
2608 }
2609 else if (0 == strncmp ("# pull request message received", stat_str, strlen (
2610 "# pull request message received")))
2611 {
2612 return STAT_TYPE_RECV_PULL_REQ;
2613 }
2614 else if (0 == strncmp ("# pull reply messages received", stat_str, strlen (
2615 "# pull reply messages received")))
2616 {
2617 return STAT_TYPE_RECV_PULL_REP;
2618 }
2619 return STAT_TYPE_MAX;
2620}
2621
2622
2623/**
2624 * @brief Converts #STAT_TYPE enum to the equivalent string representation that
2625 * is stored with the statistics service.
2626 *
2627 * @param stat_type #STAT_TYPE enum
2628 *
2629 * @return string representation that matches statistics value
2630 */
2631char*
2632stat_type_2_str (enum STAT_TYPE stat_type)
2633{
2634 switch (stat_type)
2635 {
2636 case STAT_TYPE_ROUNDS:
2637 return "# rounds";
2638
2639 case STAT_TYPE_BLOCKS:
2640 return "# rounds blocked";
2641
2642 case STAT_TYPE_BLOCKS_MANY_PUSH:
2643 return "# rounds blocked - too many pushes";
2644
2645 case STAT_TYPE_BLOCKS_NO_PUSH:
2646 return "# rounds blocked - no pushes";
2647
2648 case STAT_TYPE_BLOCKS_NO_PULL:
2649 return "# rounds blocked - no pull replies";
2650
2651 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2652 return "# rounds blocked - too many pushes, no pull replies";
2653
2654 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2655 return "# rounds blocked - no pushes, no pull replies";
2656
2657 case STAT_TYPE_ISSUED_PUSH_SEND:
2658 return "# push send issued";
2659
2660 case STAT_TYPE_ISSUED_PULL_REQ:
2661 return "# pull request send issued";
2662
2663 case STAT_TYPE_ISSUED_PULL_REP:
2664 return "# pull reply send issued";
2665
2666 case STAT_TYPE_SENT_PUSH_SEND:
2667 return "# pushes sent";
2668
2669 case STAT_TYPE_SENT_PULL_REQ:
2670 return "# pull requests sent";
2671
2672 case STAT_TYPE_SENT_PULL_REP:
2673 return "# pull replies sent";
2674
2675 case STAT_TYPE_RECV_PUSH_SEND:
2676 return "# push message received";
2677
2678 case STAT_TYPE_RECV_PULL_REQ:
2679 return "# pull request message received";
2680
2681 case STAT_TYPE_RECV_PULL_REP:
2682 return "# pull reply messages received";
2683
2684 case STAT_TYPE_MAX:
2685 default:
2686 return "ERROR";
2687 ;
2688 }
2689}
2690
2691
2692/**
2693 * Callback function to process statistic values.
2694 *
2695 * @param cls closure
2696 * @param subsystem name of subsystem that created the statistic
2697 * @param name the name of the datum
2698 * @param value the current value
2699 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2700 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2701 */
2702int
2703stat_iterator (void *cls,
2704 const char *subsystem,
2705 const char *name,
2706 uint64_t value,
2707 int is_persistent)
2708{
2709 (void) subsystem;
2710 (void) is_persistent;
2711 const struct STATcls *stat_cls = (const struct STATcls *) cls;
2712 struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
2714 // stat_type_2_str (stat_cls->stat_type),
2715 name,
2716 value);
2717 to_file (rps_peer->file_name_stats,
2718 "%s: %" PRIu64 "\n",
2719 name,
2720 value);
2721 switch (stat_str_2_type (name))
2722 {
2723 case STAT_TYPE_ROUNDS:
2724 rps_peer->num_rounds = value;
2725 break;
2726
2727 case STAT_TYPE_BLOCKS:
2728 rps_peer->num_blocks = value;
2729 break;
2730
2731 case STAT_TYPE_BLOCKS_MANY_PUSH:
2732 rps_peer->num_blocks_many_push = value;
2733 break;
2734
2735 case STAT_TYPE_BLOCKS_NO_PUSH:
2736 rps_peer->num_blocks_no_push = value;
2737 break;
2738
2739 case STAT_TYPE_BLOCKS_NO_PULL:
2740 rps_peer->num_blocks_no_pull = value;
2741 break;
2742
2743 case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2744 rps_peer->num_blocks_many_push_no_pull = value;
2745 break;
2746
2747 case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2748 rps_peer->num_blocks_no_push_no_pull = value;
2749 break;
2750
2751 case STAT_TYPE_ISSUED_PUSH_SEND:
2752 rps_peer->num_issued_push = value;
2753 break;
2754
2755 case STAT_TYPE_ISSUED_PULL_REQ:
2756 rps_peer->num_issued_pull_req = value;
2757 break;
2758
2759 case STAT_TYPE_ISSUED_PULL_REP:
2760 rps_peer->num_issued_pull_rep = value;
2761 break;
2762
2763 case STAT_TYPE_SENT_PUSH_SEND:
2764 rps_peer->num_sent_push = value;
2765 break;
2766
2767 case STAT_TYPE_SENT_PULL_REQ:
2768 rps_peer->num_sent_pull_req = value;
2769 break;
2770
2771 case STAT_TYPE_SENT_PULL_REP:
2772 rps_peer->num_sent_pull_rep = value;
2773 break;
2774
2775 case STAT_TYPE_RECV_PUSH_SEND:
2776 rps_peer->num_recv_push = value;
2777 break;
2778
2779 case STAT_TYPE_RECV_PULL_REQ:
2780 rps_peer->num_recv_pull_req = value;
2781 break;
2782
2783 case STAT_TYPE_RECV_PULL_REP:
2784 rps_peer->num_recv_pull_rep = value;
2785 break;
2786
2787 case STAT_TYPE_MAX:
2788 default:
2789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2790 "Unknown statistics string: %s\n",
2791 name);
2792 break;
2793 }
2794 return GNUNET_OK;
2795}
2796
2797
2798void
2799post_profiler (struct RPSPeer *rps_peer)
2800{
2801 if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2802 {
2803 return;
2804 }
2805
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2807 "Going to request statistic values with mask 0x%" PRIx32 "\n",
2808 cur_test_run.stat_collect_flags);
2809
2810 struct STATcls *stat_cls;
2811 uint32_t stat_type;
2812 for (stat_type = STAT_TYPE_ROUNDS;
2813 stat_type < STAT_TYPE_MAX;
2814 stat_type = stat_type << 1)
2815 {
2816 if (stat_type & cur_test_run.stat_collect_flags)
2817 {
2818 stat_cls = GNUNET_malloc (sizeof(struct STATcls));
2819 stat_cls->rps_peer = rps_peer;
2820 stat_cls->stat_type = stat_type;
2821 rps_peer->file_name_stats =
2822 store_prefix_file_name (rps_peer->peer_id, "stats");
2823 GNUNET_STATISTICS_get (rps_peer->stats_h,
2824 "rps",
2825 stat_type_2_str (stat_type),
2826 post_test_shutdown_ready_cb,
2827 stat_iterator,
2828 (struct STATcls *) stat_cls);
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2830 "Requested statistics for %s (peer %" PRIu32 ")\n",
2831 stat_type_2_str (stat_type),
2832 rps_peer->index);
2833 }
2834 }
2835}
2836
2837
2838/***********************************************************************
2839* /Definition of tests
2840***********************************************************************/
2841
2842
2843/**
2844 * Actual "main" function for the testcase.
2845 *
2846 * @param cls closure
2847 * @param h the run handle
2848 * @param n_peers number of peers in 'peers'
2849 * @param peers handle to peers run in the testbed
2850 * @param links_succeeded the number of overlay link connection attempts that
2851 * succeeded
2852 * @param links_failed the number of overlay link connection attempts that
2853 * failed
2854 */
2855static void
2856run (void *cls,
2857 struct GNUNET_TESTBED_RunHandle *h,
2858 unsigned int n_peers,
2859 struct GNUNET_TESTBED_Peer **peers,
2860 unsigned int links_succeeded,
2861 unsigned int links_failed)
2862{
2863 (void) cls;
2864 (void) h;
2865 (void) links_failed;
2866 unsigned int i;
2867 struct OpListEntry *entry;
2868
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2870
2871 /* Check whether we timed out */
2872 if ((n_peers != num_peers) ||
2873 (NULL == peers) ||
2874 (0 == links_succeeded) )
2875 {
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 "Going down due to args (eg. timeout)\n");
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n",
2880 num_peers);
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n",
2883 links_succeeded);
2884 GNUNET_SCHEDULER_shutdown ();
2885 return;
2886 }
2887
2888
2889 /* Initialize peers */
2890 testbed_peers = peers;
2891 num_peers_online = 0;
2892 for (i = 0; i < num_peers; i++)
2893 {
2894 entry = make_oplist_entry ();
2895 entry->index = i;
2896 rps_peers[i].index = i;
2897 if (NULL != cur_test_run.init_peer)
2898 cur_test_run.init_peer (&rps_peers[i]);
2899 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2900 {
2901 rps_peers->cur_view_count = 0;
2902 rps_peers->cur_view = NULL;
2903 }
2904 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2905 GNUNET_TESTBED_PIT_IDENTITY,
2906 &info_cb,
2907 entry);
2908 }
2909
2910 /* Bring peers up */
2911 GNUNET_assert (num_peers == n_peers);
2912 for (i = 0; i < n_peers; i++)
2913 {
2914 rps_peers[i].index = i;
2915 rps_peers[i].op =
2916 GNUNET_TESTBED_service_connect (&rps_peers[i],
2917 peers[i],
2918 "rps",
2919 &rps_connect_complete_cb,
2920 &rps_peers[i],
2921 &rps_connect_adapter,
2922 &rps_disconnect_adapter,
2923 &rps_peers[i]);
2924 /* Connect all peers to statistics service */
2925 if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2926 {
2927 rps_peers[i].stat_op =
2928 GNUNET_TESTBED_service_connect (NULL,
2929 peers[i],
2930 "statistics",
2931 stat_complete_cb,
2932 &rps_peers[i],
2933 &stat_connect_adapter,
2934 &stat_disconnect_adapter,
2935 &rps_peers[i]);
2936 }
2937 }
2938
2939 if (NULL != churn_task)
2940 GNUNET_SCHEDULER_cancel (churn_task);
2941 post_test_task = GNUNET_SCHEDULER_add_delayed (timeout, &post_test_op, NULL);
2942 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2943 (timeout_s * 1.2) + 0.1 * num_peers);
2944 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
2945}
2946
2947
2948/**
2949 * Entry point for the testcase, sets up the testbed.
2950 *
2951 * @param argc unused
2952 * @param argv unused
2953 * @return 0 on success
2954 */
2955int
2956main (int argc, char *argv[])
2957{
2958 int ret_value;
2959
2960 (void) argc;
2961
2962 /* Defaults for tests */
2963 num_peers = 5;
2964 cur_test_run.name = "test-rps-default";
2965 cur_test_run.init_peer = default_init_peer;
2966 cur_test_run.pre_test = NULL;
2967 cur_test_run.reply_handle = default_reply_handle;
2968 cur_test_run.eval_cb = default_eval_cb;
2969 cur_test_run.post_test = NULL;
2970 cur_test_run.have_churn = HAVE_CHURN;
2971 cur_test_run.have_collect_statistics = NO_COLLECT_STATISTICS;
2972 cur_test_run.stat_collect_flags = 0;
2973 cur_test_run.have_collect_view = NO_COLLECT_VIEW;
2974 churn_task = NULL;
2975 timeout_s = 30;
2976
2977 if (strstr (argv[0], "malicious") != NULL)
2978 {
2979 cur_test_run.pre_test = mal_pre;
2980 cur_test_run.main_test = mal_cb;
2981 cur_test_run.init_peer = mal_init_peer;
2982 timeout_s = 40;
2983
2984 if (strstr (argv[0], "_1") != NULL)
2985 {
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
2987 cur_test_run.name = "test-rps-malicious_1";
2988 mal_type = 1;
2989 }
2990 else if (strstr (argv[0], "_2") != NULL)
2991 {
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
2993 cur_test_run.name = "test-rps-malicious_2";
2994 mal_type = 2;
2995 }
2996 else if (strstr (argv[0], "_3") != NULL)
2997 {
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
2999 cur_test_run.name = "test-rps-malicious_3";
3000 mal_type = 3;
3001 }
3002 }
3003
3004 else if (strstr (argv[0], "_single_req") != NULL)
3005 {
3006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
3007 cur_test_run.name = "test-rps-single-req";
3008 cur_test_run.main_test = single_req_cb;
3009 cur_test_run.have_churn = HAVE_NO_CHURN;
3010 }
3011
3012 else if (strstr (argv[0], "_delayed_reqs") != NULL)
3013 {
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
3015 cur_test_run.name = "test-rps-delayed-reqs";
3016 cur_test_run.main_test = delay_req_cb;
3017 cur_test_run.have_churn = HAVE_NO_CHURN;
3018 }
3019
3020 else if (strstr (argv[0], "_seed_big") != NULL)
3021 {
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3023 "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
3024 num_peers = 1;
3025 cur_test_run.name = "test-rps-seed-big";
3026 cur_test_run.main_test = seed_big_cb;
3027 cur_test_run.eval_cb = no_eval;
3028 cur_test_run.have_churn = HAVE_NO_CHURN;
3029 timeout_s = 10;
3030 }
3031
3032 else if (strstr (argv[0], "_single_peer_seed") != NULL)
3033 {
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3035 "Test seeding and requesting on a single peer\n");
3036 cur_test_run.name = "test-rps-single-peer-seed";
3037 cur_test_run.main_test = single_peer_seed_cb;
3038 cur_test_run.have_churn = HAVE_NO_CHURN;
3039 }
3040
3041 else if (strstr (argv[0], "_seed_request") != NULL)
3042 {
3043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3044 "Test seeding and requesting on multiple peers\n");
3045 cur_test_run.name = "test-rps-seed-request";
3046 cur_test_run.main_test = seed_req_cb;
3047 cur_test_run.have_churn = HAVE_NO_CHURN;
3048 }
3049
3050 else if (strstr (argv[0], "_seed") != NULL)
3051 {
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
3053 cur_test_run.name = "test-rps-seed";
3054 cur_test_run.main_test = seed_cb;
3055 cur_test_run.eval_cb = no_eval;
3056 cur_test_run.have_churn = HAVE_NO_CHURN;
3057 }
3058
3059 else if (strstr (argv[0], "_req_cancel") != NULL)
3060 {
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
3062 cur_test_run.name = "test-rps-req-cancel";
3063 num_peers = 1;
3064 cur_test_run.main_test = req_cancel_cb;
3065 cur_test_run.eval_cb = no_eval;
3066 cur_test_run.have_churn = HAVE_NO_CHURN;
3067 timeout_s = 10;
3068 }
3069
3070 else if (strstr (argv[0], "_churn") != NULL)
3071 {
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
3073 cur_test_run.name = "test-rps-churn";
3074 num_peers = 5;
3075 cur_test_run.init_peer = default_init_peer;
3076 cur_test_run.main_test = churn_test_cb;
3077 cur_test_run.reply_handle = default_reply_handle;
3078 cur_test_run.eval_cb = default_eval_cb;
3079 cur_test_run.have_churn = HAVE_NO_CHURN;
3080 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
3081 timeout_s = 40;
3082 }
3083
3084 else if (strstr (argv[0], "_sub") != NULL)
3085 {
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test subs\n");
3087 cur_test_run.name = "test-rps-sub";
3088 num_peers = 5;
3089 // cur_test_run.init_peer = &default_init_peer;
3090 cur_test_run.pre_test = &sub_pre;
3091 cur_test_run.main_test = &single_req_cb;
3092 // cur_test_run.reply_handle = default_reply_handle;
3093 cur_test_run.post_test = &sub_post;
3094 // cur_test_run.eval_cb = default_eval_cb;
3095 cur_test_run.have_churn = HAVE_NO_CHURN;
3096 cur_test_run.have_quick_quit = HAVE_QUICK_QUIT;
3097 }
3098
3099 else if (strstr (argv[0], "profiler") != NULL)
3100 {
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
3102 cur_test_run.name = "test-rps-profiler";
3103 num_peers = 16;
3104 mal_type = 3;
3105 cur_test_run.init_peer = profiler_init_peer;
3106 // cur_test_run.pre_test = mal_pre;
3107 cur_test_run.pre_test = pre_profiler;
3108 cur_test_run.main_test = profiler_cb;
3109 cur_test_run.reply_handle = profiler_reply_handle;
3110 cur_test_run.eval_cb = profiler_eval;
3111 cur_test_run.post_test = post_profiler;
3112 cur_test_run.request_interval = 2;
3113 cur_test_run.num_requests = 5;
3114 // cur_test_run.have_churn = HAVE_CHURN;
3115 cur_test_run.have_churn = HAVE_NO_CHURN;
3116 cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
3117 cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
3118 cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS
3119 | STAT_TYPE_BLOCKS
3120 | STAT_TYPE_BLOCKS_MANY_PUSH
3121 | STAT_TYPE_BLOCKS_NO_PUSH
3122 | STAT_TYPE_BLOCKS_NO_PULL
3123 | STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL
3124 | STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL
3125 | STAT_TYPE_ISSUED_PUSH_SEND
3126 | STAT_TYPE_ISSUED_PULL_REQ
3127 | STAT_TYPE_ISSUED_PULL_REP
3128 | STAT_TYPE_SENT_PUSH_SEND
3129 | STAT_TYPE_SENT_PULL_REQ
3130 | STAT_TYPE_SENT_PULL_REP
3131 | STAT_TYPE_RECV_PUSH_SEND
3132 | STAT_TYPE_RECV_PULL_REQ
3133 | STAT_TYPE_RECV_PULL_REP;
3134 cur_test_run.have_collect_view = COLLECT_VIEW;
3135 timeout_s = 150;
3136
3137 /* 'Clean' directory */
3138 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
3139 GNUNET_DISK_directory_create ("/tmp/rps/");
3140 }
3141 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_s);
3142
3143 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
3144 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
3145 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
3146 if ((2 == mal_type) ||
3147 (3 == mal_type))
3148 target_peer = &rps_peer_ids[num_peers - 2];
3149 if (profiler_eval == cur_test_run.eval_cb)
3150 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
3151 malicious peer if not careful
3152 with the malicious portion */
3153
3154 ok = 1;
3155 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
3156 "test_rps.conf",
3157 num_peers,
3158 0, NULL, NULL,
3159 &run, NULL);
3160 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3161 "_test_run returned.\n");
3162 if (GNUNET_OK != ret_value)
3163 {
3164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3165 "Test did not run successfully!\n");
3166 }
3167
3168 ret_value = cur_test_run.eval_cb ();
3169
3170 if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
3171 {
3172 GNUNET_array_grow (rps_peers->cur_view,
3173 rps_peers->cur_view_count,
3174 0);
3175 }
3176 GNUNET_free (rps_peers);
3177 GNUNET_free (rps_peer_ids);
3178 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
3179 return ret_value;
3180}
3181
3182
3183/* end of test_rps.c */
diff --git a/src/rps/test_rps.conf b/src/rps/test_rps.conf
deleted file mode 100644
index 2b1101786..000000000
--- a/src/rps/test_rps.conf
+++ /dev/null
@@ -1,135 +0,0 @@
1[rps]
2#PREFIX = valgrind --leak-check=full --show-leak-kinds=all --log-file=/tmp/rps/valgrind!gnunet-service-rps!%p
3#PREFIX = valgrind --log-file=/tmp/rps/valgrind!gnunet-service-rps!%p
4#PREFIX = valgrind
5UNIXPATH = $GNUNET_TMP/gnunet-service-rps.sock
6HOME = $SERVICEHOME
7# PORT = 2106
8#@UNIXONLY@ PORT = 2087
9IMMEDIATE_START = YES
10START_ON_DEMAND = NO
11NOARMBIND = YES
12#OPTIONS=-l /tmp/rps_profiler_logs/rps-[]-%Y-%m-%d.log
13
14# This is the timeinterval between the rounds
15ROUNDINTERVAL = 100 ms
16FILENAME_VALID_PEERS = $GNUNET_DATA_HOME/rps/valid_peers.txt
17
18# This is the 'estimate' in the beginning.
19# This determines the size of the peers we keep in memory
20# until we receive the first estimate from NSE.
21# Keep in mind, that (networksize)^(1/3) should be enough.
22# So, 50 is enough for a network of size 50^3 = 125000
23MINSIZE = 4
24
25DESIRED_PROBABILITY = 0.75
26
27DEFICIENCY_FACTOR = 0.4
28
29
30
31[testbed]
32HOSTNAME = localhost
33
34# MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS = 100
35
36#OVERLAY_TOPOLOGY = CLIQUE
37
38OVERLAY_TOPOLOGY = SCALE_FREE
39SCALE_FREE_TOPOLOGY_CAP = 100
40SCALE_FREE_TOPOLOGY_M = 2
41
42#OVERLAY_TOPOLOGY = RANDOM
43#OVERLAY_RANDOM_LINKS = 25
44
45#OVERLAY_TOPOLOGY = SMALL_WORLD
46#OVERLAY_RANDOM_LINKS = 25
47
48SETUP_TIMEOUT = 2 m
49
50[nse]
51WORKBITS = 0
52
53[nat]
54# Use addresses from the local network interfaces (including loopback, but also others)
55USE_LOCALADDR = YES
56ENABLE_UPNP = NO
57
58# Do we use addresses from localhost address ranges? (::1, 127.0.0.0/8)
59RETURN_LOCAL_ADDRESSES = YES
60
61[transport]
62PLUGINS = unix
63
64[ats]
65# Network specific inbound/outbound quotas
66UNSPECIFIED_QUOTA_IN = unlimited
67UNSPECIFIED_QUOTA_OUT = unlimited
68# LOOPBACK
69LOOPBACK_QUOTA_IN = unlimited
70LOOPBACK_QUOTA_OUT = unlimited
71# LAN
72LAN_QUOTA_IN = unlimited
73LAN_QUOTA_OUT = unlimited
74#WAN
75WAN_QUOTA_OUT = unlimited
76WAN_QUOTA_IN = unlimited
77# WLAN
78WLAN_QUOTA_IN = unlimited
79WLAN_QUOTA_OUT = unlimited
80# BLUETOOTH
81BLUETOOTH_QUOTA_IN = unlimited
82BLUETOOTH_QUOTA_OUT = unlimited
83
84[dht]
85DISABLE_TRY_CONNECT = YES
86
87[cadet]
88#OPTIONS=-l /tmp/rps_profiler_logs/cadet-[]-%Y-%m-%d.log
89#PREFIX = valgrind --log-file=/tmp/rps/valgrind_gnunet-service-cadet_%p
90
91#[arm]
92#GLOBAL_POSTFIX=-l /tmp/rps_profiler_logs/other-[]-%Y-%m-%d.log
93
94#[statistics]
95#IMMEDIATE_START = NO
96#START_ON_DEMAND = NO
97
98[peerinfo]
99NO_IO = YES
100
101[hostlist]
102IMMEDIATE_START = NO
103START_ON_DEMAND = NO
104
105[zonemaster]
106IMMEDIATE_START = NO
107START_ON_DEMAND = NO
108
109[namecache]
110IMMEDIATE_START = NO
111START_ON_DEMAND = NO
112
113[namestore]
114IMMEDIATE_START = NO
115START_ON_DEMAND = NO
116
117[topology]
118IMMEDIATE_START = NO
119START_ON_DEMAND = NO
120
121[vpn]
122IMMEDIATE_START = NO
123START_ON_DEMAND = NO
124
125[revocation]
126IMMEDIATE_START = NO
127START_ON_DEMAND = NO
128
129[gns]
130IMMEDIATE_START = NO
131START_ON_DEMAND = NO
132
133[fs]
134IMMEDIATE_START = NO
135START_ON_DEMAND = NO
diff --git a/src/rps/test_rps_api.c b/src/rps/test_rps_api.c
deleted file mode 100644
index 06dad2887..000000000
--- a/src/rps/test_rps_api.c
+++ /dev/null
@@ -1,87 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file rps/test_rps_api.c
22 * @brief testcase for rps_api.c
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_rps_service.h"
27
28
29static int ok = 1;
30
31
32static void
33run (void *cls,
34 char *const *args,
35 const char *cfgfile,
36 const struct GNUNET_CONFIGURATION_Handle *cfg)
37{
38 ok = 0;
39}
40
41
42static int
43check ()
44{
45 char *const argv[] = { "test-rps-api", NULL };
46 struct GNUNET_GETOPT_CommandLineOption options[] = {
47 GNUNET_GETOPT_OPTION_END
48 };
49 struct GNUNET_OS_Process *proc;
50 char *path = GNUNET_OS_get_libexec_binary_path ("gnunet-service-rps");
51
52 if (NULL == path)
53 {
54 fprintf (stderr, "Service executable not found `%s'\n",
55 "gnunet-service-rps");
56 return;
57 }
58
59 proc = GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
60 NULL, NULL, path, "gnunet-service-rps", NULL);
61
62 GNUNET_free (path);
63 GNUNET_assert (NULL != proc);
64 GNUNET_PROGRAM_run (1, argv, "test-rps-api", "nohelp",
65 options, &run, &ok);
66 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
67 {
68 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
69 ok = 1;
70 }
71 GNUNET_OS_process_wait (proc);
72 GNUNET_OS_process_destroy (proc);
73 return ok;
74}
75
76
77int
78main (int argc, char *argv[])
79{
80 GNUNET_log_setup ("test_statistics_api",
81 "WARNING",
82 NULL);
83 return check ();
84}
85
86
87/* end of test_rps_api.c */
diff --git a/src/rps/test_service_rps_custommap.c b/src/rps/test_service_rps_custommap.c
deleted file mode 100644
index 3d885ca6b..000000000
--- a/src/rps/test_service_rps_custommap.c
+++ /dev/null
@@ -1,127 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file rps/test_service_rps_custommap.c
22 * @brief testcase for gnunet-service-rps_peers.c
23 */
24#include <platform.h>
25#include "gnunet-service-rps_custommap.h"
26
27#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
28 if (NULL != c_m) CustomPeerMap_destroy (c_m); return 1; }
29#define CHECK(c) { if (! (c)) ABORT (); }
30
31
32static int
33check ()
34{
35 struct CustomPeerMap *c_m;
36 struct GNUNET_PeerIdentity k1;
37 struct GNUNET_PeerIdentity k2;
38 unsigned int j;
39
40 CHECK (NULL != (c_m = CustomPeerMap_create (4)));
41 memset (&k1, 0, sizeof(k1));
42 memset (&k2, 1, sizeof(k2));
43 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k1));
44 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k2));
45 CHECK (GNUNET_NO == CustomPeerMap_remove_peer (c_m, &k1));
46 CHECK (GNUNET_NO == CustomPeerMap_remove_peer (c_m, &k2));
47 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 0));
48 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 0));
49 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 1));
50 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 1));
51 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 0));
52 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 0));
53 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 1));
54 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 1));
55 CustomPeerMap_clear (c_m); /* See if assertions trigger */
56 CHECK (0 == CustomPeerMap_size (c_m));
57
58 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k1));
59 CHECK (1 == CustomPeerMap_size (c_m));
60 CHECK (GNUNET_NO == CustomPeerMap_put (c_m, &k1));
61 CHECK (1 == CustomPeerMap_size (c_m));
62 CHECK (GNUNET_YES == CustomPeerMap_contains_peer (c_m, &k1));
63 CHECK (GNUNET_OK == CustomPeerMap_remove_peer (c_m, &k1));
64 CHECK (0 == CustomPeerMap_size (c_m));
65 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k1));
66 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k2));
67
68 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k1));
69 CHECK (1 == CustomPeerMap_size (c_m));
70 for (j = 0; j < 16; j++)
71 {
72 CHECK (GNUNET_NO == CustomPeerMap_put (c_m, &k1));
73 }
74 CHECK (1 == CustomPeerMap_size (c_m));
75 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k2));
76 CHECK (2 == CustomPeerMap_size (c_m));
77 for (j = 0; j < 16; j++)
78 {
79 CHECK (GNUNET_NO == CustomPeerMap_put (c_m, &k2));
80 }
81 CHECK (2 == CustomPeerMap_size (c_m));
82
83 /* iterate */
84 for (j = 0; j < CustomPeerMap_size (c_m); j++)
85 {
86 CHECK (NULL != CustomPeerMap_get_peer_by_index (c_m, j));
87 }
88 CHECK ((0 == memcmp (CustomPeerMap_get_peer_by_index (c_m, 0),
89 &k1, sizeof(k1))));
90 CHECK ((0 == memcmp (CustomPeerMap_get_peer_by_index (c_m, 1),
91 &k2, sizeof(k2))));
92 CHECK (GNUNET_OK == CustomPeerMap_remove_peer (c_m, &k1));
93 CHECK (1 == CustomPeerMap_size (c_m));
94 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k1));
95 CHECK (GNUNET_YES == CustomPeerMap_contains_peer (c_m, &k2));
96 CHECK (NULL != CustomPeerMap_get_peer_by_index (c_m, 0));
97
98 CustomPeerMap_clear (c_m);
99 CHECK (0 == CustomPeerMap_size (c_m));
100
101 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k1));
102 CHECK (1 == CustomPeerMap_size (c_m));
103 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k2));
104 CHECK (2 == CustomPeerMap_size (c_m));
105 CustomPeerMap_clear (c_m);
106 CHECK (0 == CustomPeerMap_size (c_m));
107
108 CustomPeerMap_destroy (c_m);
109
110 return 0;
111}
112
113
114int
115main (int argc, char *argv[])
116{
117 (void) argc;
118 (void) argv;
119
120 GNUNET_log_setup ("test_service_rps_peers",
121 "WARNING",
122 NULL);
123 return check ();
124}
125
126
127/* end of test_service_rps_custommap.c */
diff --git a/src/rps/test_service_rps_sampler_elem.c b/src/rps/test_service_rps_sampler_elem.c
deleted file mode 100644
index 8fef5adaf..000000000
--- a/src/rps/test_service_rps_sampler_elem.c
+++ /dev/null
@@ -1,213 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file rps/test_service_rps_sampler_elem.c
22 * @brief testcase for gnunet-service-rps_sampler_elem.c
23 */
24#include <platform.h>
25#include "gnunet_util_lib.h"
26#include "gnunet-service-rps_sampler_elem.h"
27
28#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
29 return 1; }
30#define CHECK(c) { if (! (c)) ABORT (); }
31
32
33static int
34check ()
35{
36 struct GNUNET_PeerIdentity pid0;
37 struct GNUNET_PeerIdentity pid1;
38 struct RPS_SamplerElement *s_elem;
39 struct GNUNET_CRYPTO_AuthKey auth_key;
40 struct GNUNET_CRYPTO_AuthKey auth_key2;
41 struct GNUNET_HashCode hash_code;
42 struct GNUNET_HashCode hash_code2;
43
44 memset (&pid0, 1, sizeof(pid0));
45 memset (&pid1, 0, sizeof(pid1));
46
47 /* Check if creation and destruction of an
48 * (empty) sampler element works */
49 s_elem = RPS_sampler_elem_create ();
50 CHECK (NULL != s_elem);
51 CHECK (EMPTY == s_elem->is_empty);
52 CHECK (NULL != &s_elem->auth_key);
53 auth_key = s_elem->auth_key;
54 RPS_sampler_elem_destroy (s_elem);
55
56
57 /* Check creation of another sampler element
58 * yields another (random) key */
59 s_elem = RPS_sampler_elem_create ();
60 CHECK (NULL != s_elem);
61 CHECK (EMPTY == s_elem->is_empty);
62 CHECK (NULL != &s_elem->auth_key);
63 CHECK (auth_key.key != s_elem->auth_key.key);
64 CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
65 GNUNET_CRYPTO_HASH_LENGTH));
66 auth_key = s_elem->auth_key;
67
68 /* Check that reinitialisation
69 * yields another (random) key */
70 RPS_sampler_elem_reinit (s_elem);
71 CHECK (NULL != s_elem);
72 CHECK (EMPTY == s_elem->is_empty);
73 CHECK (NULL != &s_elem->auth_key);
74 CHECK (auth_key.key != s_elem->auth_key.key);
75 CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
76 GNUNET_CRYPTO_HASH_LENGTH));
77 RPS_sampler_elem_destroy (s_elem);
78
79
80 /* Check that input of single peer id
81 * sets valid values */
82 s_elem = RPS_sampler_elem_create ();
83 CHECK (EMPTY == s_elem->is_empty);
84 CHECK (NULL != &s_elem->auth_key);
85 CHECK (auth_key.key != s_elem->auth_key.key);
86 /* This fails only with minimal chance */
87 CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
88 GNUNET_CRYPTO_HASH_LENGTH));
89 auth_key = s_elem->auth_key;
90
91 /* Check also that the hash of the peer id changed
92 * Also fails with minimal probability */
93 hash_code = s_elem->peer_id_hash;
94 RPS_sampler_elem_next (s_elem, &pid0);
95 CHECK (0 == memcmp (&pid0,
96 &s_elem->peer_id,
97 sizeof(struct GNUNET_PeerIdentity)));
98 CHECK (0 != memcmp (&hash_code,
99 &s_elem->peer_id_hash,
100 sizeof(struct GNUNET_HashCode)));
101 hash_code = s_elem->peer_id_hash;
102
103 /* We can only check that the peer id is one of both inputs */
104 RPS_sampler_elem_next (s_elem, &pid1);
105 CHECK ((0 == memcmp (&pid0,
106 &s_elem->peer_id,
107 sizeof(struct GNUNET_PeerIdentity))) ||
108 (0 == memcmp (&pid1,
109 &s_elem->peer_id,
110 sizeof(struct GNUNET_PeerIdentity))));
111
112 /* Check that hash stayed the same when peer id did not change */
113 if (0 == memcmp (&pid0,
114 &s_elem->peer_id,
115 sizeof(struct GNUNET_PeerIdentity)))
116 {
117 CHECK (0 == memcmp (&hash_code,
118 &s_elem->peer_id_hash,
119 sizeof(struct GNUNET_HashCode)));
120 }
121 else /* Check that hash changed */
122 {
123 CHECK (0 != memcmp (&hash_code,
124 &s_elem->peer_id_hash,
125 sizeof(struct GNUNET_HashCode)));
126 }
127
128 /* Check multiple inputs of same id
129 * hash should not change anymore */
130 hash_code2 = s_elem->peer_id_hash;
131 RPS_sampler_elem_next (s_elem, &pid0);
132 CHECK (0 == memcmp (&hash_code2,
133 &s_elem->peer_id_hash,
134 sizeof(struct GNUNET_HashCode)));
135 RPS_sampler_elem_next (s_elem, &pid1);
136 CHECK (0 == memcmp (&hash_code2,
137 &s_elem->peer_id_hash,
138 sizeof(struct GNUNET_HashCode)));
139 RPS_sampler_elem_next (s_elem, &pid0);
140 CHECK (0 == memcmp (&hash_code2,
141 &s_elem->peer_id_hash,
142 sizeof(struct GNUNET_HashCode)));
143 RPS_sampler_elem_next (s_elem, &pid0);
144 CHECK (0 == memcmp (&hash_code2,
145 &s_elem->peer_id_hash,
146 sizeof(struct GNUNET_HashCode)));
147 RPS_sampler_elem_next (s_elem, &pid0);
148 CHECK (0 == memcmp (&hash_code2,
149 &s_elem->peer_id_hash,
150 sizeof(struct GNUNET_HashCode)));
151 RPS_sampler_elem_next (s_elem, &pid1);
152 CHECK (0 == memcmp (&hash_code2,
153 &s_elem->peer_id_hash,
154 sizeof(struct GNUNET_HashCode)));
155 RPS_sampler_elem_next (s_elem, &pid1);
156 CHECK (0 == memcmp (&hash_code2,
157 &s_elem->peer_id_hash,
158 sizeof(struct GNUNET_HashCode)));
159 RPS_sampler_elem_next (s_elem, &pid1);
160 CHECK (0 == memcmp (&hash_code2,
161 &s_elem->peer_id_hash,
162 sizeof(struct GNUNET_HashCode)));
163
164 /* Check whether pid stayed the same all the time */
165 if (0 == memcmp (&hash_code,
166 &hash_code2,
167 sizeof(struct GNUNET_HashCode)))
168 {
169 CHECK (0 == memcmp (&pid0,
170 &s_elem->peer_id,
171 sizeof(struct GNUNET_PeerIdentity)));
172 }
173 else
174 {
175 CHECK (0 == memcmp (&pid1,
176 &s_elem->peer_id,
177 sizeof(struct GNUNET_PeerIdentity)));
178 }
179 RPS_sampler_elem_destroy (s_elem);
180
181 /* Check _set() */
182 s_elem = RPS_sampler_elem_create ();
183 CHECK (NULL != s_elem);
184 CHECK (EMPTY == s_elem->is_empty);
185 CHECK (NULL != &s_elem->auth_key);
186 auth_key = s_elem->auth_key;
187 memset (&auth_key2, 0, sizeof(auth_key2));
188 RPS_sampler_elem_set (s_elem, auth_key2);
189 CHECK (0 == memcmp (auth_key2.key,
190 s_elem->auth_key.key,
191 GNUNET_CRYPTO_HASH_LENGTH));
192 RPS_sampler_elem_destroy (s_elem);
193
194
195 /* TODO: deterministic tests (use _set() to set auth_key) */
196 return 0;
197}
198
199
200int
201main (int argc, char *argv[])
202{
203 (void) argc;
204 (void) argv;
205
206 GNUNET_log_setup ("test_service_rps_peers",
207 "WARNING",
208 NULL);
209 return check ();
210}
211
212
213/* end of test_service_rps_peers.c */
diff --git a/src/rps/test_service_rps_view.c b/src/rps/test_service_rps_view.c
deleted file mode 100644
index 5a8c00849..000000000
--- a/src/rps/test_service_rps_view.c
+++ /dev/null
@@ -1,145 +0,0 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file rps/test_service_rps_view.c
22 * @brief testcase for gnunet-service-rps_view.c
23 */
24#include <platform.h>
25#include "gnunet-service-rps_view.h"
26
27#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
28 View_destroy (view); return 1; }
29#define CHECK(c) { if (! (c)) ABORT (); }
30
31
32static int
33check ()
34{
35 struct View *view;
36 struct GNUNET_PeerIdentity k1;
37 struct GNUNET_PeerIdentity k2;
38 const struct GNUNET_PeerIdentity *array;
39 unsigned int j;
40
41 view = View_create (3);
42 memset (&k1, 0, sizeof(k1));
43 memset (&k2, 1, sizeof(k2));
44 CHECK (GNUNET_NO == View_contains_peer (view, &k1));
45 CHECK (GNUNET_NO == View_contains_peer (view, &k2));
46 CHECK (GNUNET_NO == View_remove_peer (view, &k1));
47 CHECK (GNUNET_NO == View_remove_peer (view, &k2));
48 CHECK (NULL == View_get_peer_by_index (view, 0));
49 CHECK (NULL == View_get_peer_by_index (view, 1));
50 View_clear (view); /* See if assertions trigger */
51 CHECK (0 == View_size (view));
52
53 CHECK (GNUNET_OK == View_put (view, &k1));
54 CHECK (1 == View_size (view));
55 CHECK (GNUNET_NO == View_put (view, &k1));
56 CHECK (1 == View_size (view));
57 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
58 CHECK (GNUNET_OK == View_remove_peer (view, &k1));
59 CHECK (0 == View_size (view));
60 CHECK (GNUNET_NO == View_contains_peer (view, &k1));
61 CHECK (GNUNET_NO == View_contains_peer (view, &k2));
62
63 CHECK (GNUNET_OK == View_put (view, &k1));
64 CHECK (1 == View_size (view));
65 for (j = 0; j < 16; j++)
66 {
67 CHECK (GNUNET_NO == View_put (view, &k1));
68 }
69 CHECK (1 == View_size (view));
70 CHECK (GNUNET_OK == View_put (view, &k2));
71 CHECK (2 == View_size (view));
72 for (j = 0; j < 16; j++)
73 {
74 CHECK (GNUNET_NO == View_put (view, &k2));
75 }
76 CHECK (2 == View_size (view));
77
78 /* iterate */
79 for (j = 0; j < View_size (view); j++)
80 {
81 CHECK (NULL != View_get_peer_by_index (view, j));
82 }
83 CHECK ((0 == memcmp (View_get_peer_by_index (view, 0),
84 &k1, sizeof(k1))));
85 CHECK ((0 == memcmp (View_get_peer_by_index (view, 1),
86 &k2, sizeof(k2))));
87 CHECK (GNUNET_OK == View_remove_peer (view, &k1));
88 CHECK (1 == View_size (view));
89 CHECK (GNUNET_NO == View_contains_peer (view, &k1));
90 CHECK (GNUNET_YES == View_contains_peer (view, &k2));
91 CHECK (NULL != View_get_peer_by_index (view, 0));
92 CHECK (NULL == View_get_peer_by_index (view, 1));
93
94 View_clear (view);
95 CHECK (0 == View_size (view));
96
97 CHECK (GNUNET_OK == View_put (view, &k1));
98 CHECK (1 == View_size (view));
99 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
100 CHECK (GNUNET_OK == View_put (view, &k2));
101 CHECK (2 == View_size (view));
102 CHECK (GNUNET_YES == View_contains_peer (view, &k2));
103 array = View_get_as_array (view);
104 CHECK (0 == memcmp (&array[0], &k1, sizeof(k1)));
105 CHECK (0 == memcmp (&array[1], &k2, sizeof(k2)));
106 View_clear (view);
107 CHECK (0 == View_size (view));
108
109 /*View_change_len () */
110 CHECK (GNUNET_OK == View_put (view, &k1));
111 CHECK (GNUNET_OK == View_put (view, &k2));
112 CHECK (2 == View_size (view));
113 View_change_len (view, 4);
114 CHECK (2 == View_size (view));
115 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
116 CHECK (GNUNET_YES == View_contains_peer (view, &k2));
117 array = View_get_as_array (view);
118 CHECK (0 == memcmp (&array[0], &k1, sizeof(k1)));
119 CHECK (0 == memcmp (&array[1], &k2, sizeof(k2)));
120 View_change_len (view, 1);
121 CHECK (1 == View_size (view));
122 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
123 CHECK (GNUNET_NO == View_contains_peer (view, &k2));
124 array = View_get_as_array (view);
125 CHECK (0 == memcmp (&array[0], &k1, sizeof(k1)));
126 View_clear (view);
127 CHECK (0 == View_size (view));
128
129 View_destroy (view);
130
131 return 0;
132}
133
134
135int
136main ()
137{
138 GNUNET_log_setup ("test_service_rps_peers",
139 "WARNING",
140 NULL);
141 return check ();
142}
143
144
145/* end of test_service_rps_view.c */