aboutsummaryrefslogtreecommitdiff
path: root/src/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib')
-rw-r--r--src/contrib/Makefile.am4
-rw-r--r--src/contrib/service/Makefile.am9
-rw-r--r--src/contrib/service/abd/.gitignore2
-rw-r--r--src/contrib/service/abd/Makefile.am106
-rw-r--r--src/contrib/service/abd/abd.conf.in5
-rw-r--r--src/contrib/service/abd/abd.h251
-rw-r--r--src/contrib/service/abd/abd_api.c563
-rw-r--r--src/contrib/service/abd/abd_serialization.c516
-rw-r--r--src/contrib/service/abd/abd_serialization.h167
-rw-r--r--src/contrib/service/abd/delegate_misc.c279
-rw-r--r--src/contrib/service/abd/delegate_misc.h36
-rw-r--r--src/contrib/service/abd/gnunet-abd.c1108
-rw-r--r--src/contrib/service/abd/gnunet-service-abd.c1778
-rw-r--r--src/contrib/service/abd/plugin_gnsrecord_abd.c357
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_and.sh98
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_and2.sh94
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_and3.sh96
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_and4.sh83
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_bw.sh87
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_bw_link.sh92
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_bw_link2.sh93
-rwxr-xr-xsrc/contrib/service/abd/test_abd_bi_fw.sh92
-rw-r--r--src/contrib/service/abd/test_abd_defaults.conf24
-rwxr-xr-xsrc/contrib/service/abd/test_abd_issue.sh46
-rw-r--r--src/contrib/service/abd/test_abd_lookup.conf28
-rwxr-xr-xsrc/contrib/service/abd/test_abd_own.sh139
-rwxr-xr-xsrc/contrib/service/abd/test_abd_verify.sh87
-rwxr-xr-xsrc/contrib/service/abd/test_abd_verify_and.sh86
-rwxr-xr-xsrc/contrib/service/abd/test_abd_verify_simple.sh56
-rw-r--r--src/contrib/service/auction/.gitignore5
-rw-r--r--src/contrib/service/auction/Makefile.am75
-rw-r--r--src/contrib/service/auction/auction.conf4
-rw-r--r--src/contrib/service/auction/auction.h77
-rw-r--r--src/contrib/service/auction/gnunet-auction-create.c219
-rw-r--r--src/contrib/service/auction/gnunet-auction-info.c86
-rw-r--r--src/contrib/service/auction/gnunet-auction-join.c86
-rw-r--r--src/contrib/service/auction/gnunet-service-auction.c155
-rw-r--r--src/contrib/service/auction/test_auction_api.c44
-rwxr-xr-xsrc/contrib/service/auction/test_auction_create.sh80
-rw-r--r--src/contrib/service/rps/.gitignore16
-rw-r--r--src/contrib/service/rps/Makefile.am167
-rw-r--r--src/contrib/service/rps/gnunet-rps-profiler.c3199
-rw-r--r--src/contrib/service/rps/gnunet-rps.c294
-rw-r--r--src/contrib/service/rps/gnunet-service-rps.c5009
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_custommap.c356
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_custommap.h159
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_sampler.c268
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_sampler.h157
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_sampler_elem.c170
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_sampler_elem.h153
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_view.c294
-rw-r--r--src/contrib/service/rps/gnunet-service-rps_view.h145
-rw-r--r--src/contrib/service/rps/profiler_rps.conf139
-rw-r--r--src/contrib/service/rps/rps-sampler_client.c439
-rw-r--r--src/contrib/service/rps/rps-sampler_client.h156
-rw-r--r--src/contrib/service/rps/rps-sampler_common.c742
-rw-r--r--src/contrib/service/rps/rps-sampler_common.h389
-rw-r--r--src/contrib/service/rps/rps-test_util.c550
-rw-r--r--src/contrib/service/rps/rps-test_util.h144
-rw-r--r--src/contrib/service/rps/rps.conf.in38
-rw-r--r--src/contrib/service/rps/rps.h336
-rw-r--r--src/contrib/service/rps/rps_api.c1320
-rw-r--r--src/contrib/service/rps/test_rps.c3183
-rw-r--r--src/contrib/service/rps/test_rps.conf135
-rw-r--r--src/contrib/service/rps/test_rps_api.c87
-rw-r--r--src/contrib/service/rps/test_service_rps_custommap.c128
-rw-r--r--src/contrib/service/rps/test_service_rps_sampler_elem.c214
-rw-r--r--src/contrib/service/rps/test_service_rps_view.c146
68 files changed, 25746 insertions, 0 deletions
diff --git a/src/contrib/Makefile.am b/src/contrib/Makefile.am
new file mode 100644
index 000000000..516fb872c
--- /dev/null
+++ b/src/contrib/Makefile.am
@@ -0,0 +1,4 @@
1SUBDIRS = \
2 #lib \
3 #plugin \
4 service
diff --git a/src/contrib/service/Makefile.am b/src/contrib/service/Makefile.am
new file mode 100644
index 000000000..25c3670d9
--- /dev/null
+++ b/src/contrib/service/Makefile.am
@@ -0,0 +1,9 @@
1if HAVE_EXPERIMENTAL
2 EXP_DIR = \
3 rps \
4 abd \
5 auction
6endif
7
8SUBDIRS = \
9 $(EXP_DIR)
diff --git a/src/contrib/service/abd/.gitignore b/src/contrib/service/abd/.gitignore
new file mode 100644
index 000000000..ee1eb7837
--- /dev/null
+++ b/src/contrib/service/abd/.gitignore
@@ -0,0 +1,2 @@
1gnunet-abd
2gnunet-service-abd
diff --git a/src/contrib/service/abd/Makefile.am b/src/contrib/service/abd/Makefile.am
new file mode 100644
index 000000000..6fa2dc848
--- /dev/null
+++ b/src/contrib/service/abd/Makefile.am
@@ -0,0 +1,106 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4EXTRA_DIST = \
5 test_abd_defaults.conf \
6 test_abd_lookup.conf \
7 $(check_SCRIPTS) \
8 $(pkgdata_DATA)
9
10
11if USE_COVERAGE
12 AM_CFLAGS = --coverage -O0
13endif
14
15pkgcfgdir = $(pkgdatadir)/config.d/
16
17libexecdir= $(pkglibdir)/libexec/
18
19plugindir = $(libdir)/gnunet
20
21pkgcfg_DATA = \
22 abd.conf
23
24
25# /usr/lib - compiles a layer which can be used to be communicagte with the service
26lib_LTLIBRARIES = \
27 libgnunetabd.la
28
29# /usr/lib/gnunet/libexec - Business logic . Separate process
30libexec_PROGRAMS = \
31 gnunet-service-abd
32
33bin_PROGRAMS = \
34 gnunet-abd
35
36plugin_LTLIBRARIES = \
37 libgnunet_plugin_gnsrecord_abd.la
38
39
40gnunet_abd_SOURCES = \
41 gnunet-abd.c
42gnunet_abd_LDADD = \
43 libgnunetabd.la \
44 $(top_builddir)/src/lib/util/libgnunetutil.la \
45 $(top_builddir)/src/lib/gnsrecord/libgnunetgnsrecord.la \
46 $(top_builddir)/src/identity/libgnunetidentity.la \
47 $(top_builddir)/src/namestore/libgnunetnamestore.la \
48 $(GN_LIBINTL)
49
50
51libgnunet_plugin_gnsrecord_abd_la_SOURCES = \
52 plugin_gnsrecord_abd.c
53libgnunet_plugin_gnsrecord_abd_la_LIBADD = \
54 libgnunetabd.la \
55 $(top_builddir)/src/lib/util/libgnunetutil.la \
56 $(top_builddir)/src/identity/libgnunetidentity.la \
57 $(LTLIBINTL)
58libgnunet_plugin_gnsrecord_abd_la_LDFLAGS = \
59 $(GN_PLUGIN_LDFLAGS)
60
61
62
63gnunet_service_abd_SOURCES = \
64 gnunet-service-abd.c
65gnunet_service_abd_LDADD = \
66 libgnunetabd.la \
67 $(top_builddir)/src/lib/util/libgnunetutil.la \
68 $(top_builddir)/src/gns/libgnunetgns.la \
69 $(top_builddir)/src/namestore/libgnunetnamestore.la \
70 $(top_builddir)/src/identity/libgnunetidentity.la \
71 $(top_builddir)/src/statistics/libgnunetstatistics.la \
72 $(GN_LIBINTL)
73
74
75libgnunetabd_la_SOURCES = \
76 abd_api.c abd.h\
77 abd_serialization.c \
78 abd_serialization.h \
79 delegate_misc.c \
80 delegate_misc.h
81libgnunetabd_la_LIBADD = \
82 $(top_builddir)/src/lib/util/libgnunetutil.la \
83 $(top_builddir)/src/identity/libgnunetidentity.la \
84 $(XLIB)
85libgnunetabd_la_LDFLAGS = \
86 $(GN_LIB_LDFLAGS)
87
88
89
90check_SCRIPTS = \
91 test_abd_issue.sh
92 # test_abd_verify_simple.sh \
93 # test_abd_verify.sh \
94 # test_abd_verify_and.sh
95
96if ENABLE_TEST_RUN
97if HAVE_SQLITE
98 AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;
99 TESTS = $(check_SCRIPTS)
100endif
101endif
102
103
104
105
106
diff --git a/src/contrib/service/abd/abd.conf.in b/src/contrib/service/abd/abd.conf.in
new file mode 100644
index 000000000..7baf95fc8
--- /dev/null
+++ b/src/contrib/service/abd/abd.conf.in
@@ -0,0 +1,5 @@
1[abd]
2BINARY = gnunet-service-abd
3UNIXPATH = $GNUNET_USER_RUNTIME_DIR/gnunet-service-abd.sock
4RUN_PER_USER = YES
5OPTIONS = -L DEBUG
diff --git a/src/contrib/service/abd/abd.h b/src/contrib/service/abd/abd.h
new file mode 100644
index 000000000..26b8e222e
--- /dev/null
+++ b/src/contrib/service/abd/abd.h
@@ -0,0 +1,251 @@
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 abd/abd.h
22 * @brief IPC messages between ABD API and ABD service
23 * @author Martin Schanzenbach
24 */
25#ifndef ABD_H
26#define ABD_H
27
28#include "gnunet_abd_service.h"
29
30GNUNET_NETWORK_STRUCT_BEGIN
31
32/**
33 * Message from client to Credential service to collect credentials.
34 */
35struct CollectMessage
36{
37 /**
38 * Header of type #GNUNET_MESSAGE_TYPE_ABD_VERIFY
39 */
40 struct GNUNET_MessageHeader header;
41
42 /**
43 * Subject public key
44 */
45 struct GNUNET_CRYPTO_PrivateKey subject_key;
46
47 /**
48 * Trust anchor
49 */
50 struct GNUNET_CRYPTO_PublicKey issuer_key;
51
52 /**
53 * Length of the issuer attribute
54 */
55 uint16_t issuer_attribute_len;
56
57 /**
58 * Direction of the resolution algo
59 */
60 uint16_t resolution_algo;
61
62 /**
63 * Unique identifier for this request (for key collisions).
64 */
65 uint32_t id GNUNET_PACKED;
66
67 /* Followed by the zero-terminated attribute */
68};
69
70
71/**
72 * Message from client to Credential service to verify attributes.
73 */
74struct VerifyMessage
75{
76 /**
77 * Header of type #GNUNET_MESSAGE_TYPE_ABD_VERIFY
78 */
79 struct GNUNET_MessageHeader header;
80
81 /**
82 * Subject public key
83 */
84 struct GNUNET_CRYPTO_PublicKey subject_key;
85
86 /**
87 * Trust anchor
88 */
89 struct GNUNET_CRYPTO_PublicKey issuer_key;
90
91 /**
92 * Number of delegates
93 */
94 uint32_t d_count;
95
96 /**
97 * Length of the issuer attribute
98 */
99 uint16_t issuer_attribute_len;
100
101 /**
102 * Direction of the resolution algo
103 */
104 uint16_t resolution_algo;
105
106 /**
107 * Unique identifier for this request (for key collisions).
108 */
109 uint32_t id GNUNET_PACKED;
110
111 /* Followed by the zero-terminated attribute and credentials to look up */
112};
113
114
115/**
116 * Message from ABD service to client: new results.
117 */
118struct DelegationChainResultMessage
119{
120 /**
121 * Header of type #GNUNET_MESSAGE_TYPE_ABD_VERIFY_RESULT
122 */
123 struct GNUNET_MessageHeader header;
124
125 /**
126 * Unique identifier for this request (for key collisions).
127 */
128 uint32_t id GNUNET_PACKED;
129
130 /**
131 * Indicates if credential has been found at all
132 */
133 uint32_t del_found GNUNET_PACKED;
134
135 /**
136 * The number of delegations in the response
137 */
138 uint32_t d_count GNUNET_PACKED;
139
140 /**
141 * The number of credentials in the response
142 */
143 uint32_t c_count GNUNET_PACKED;
144
145 /* followed by ad_count GNUNET_ABD_RecordData structs*/
146};
147
148/**
149 * Message from ABD service to client: new results.
150 */
151struct DelegationChainIntermediateMessage
152{
153 /**
154 * Header of type #GNUNET_MESSAGE_TYPE_ABD_INTERMEDIATE_RESULT
155 */
156 struct GNUNET_MessageHeader header;
157
158 /**
159 * Unique identifier for this request (for key collisions).
160 */
161 uint32_t id GNUNET_PACKED;
162
163 uint16_t is_bw GNUNET_PACKED;
164
165 uint32_t size GNUNET_PACKED;
166};
167
168struct DelegationRecordData
169{
170 /**
171 * Subject key
172 */
173 struct GNUNET_CRYPTO_PublicKey subject_key;
174
175 /**
176 * Subject attributes
177 */
178 uint32_t subject_attribute_len GNUNET_PACKED;
179};
180
181
182struct ChainEntry
183{
184 /**
185 * Issuer key
186 */
187 struct GNUNET_CRYPTO_PublicKey issuer_key;
188
189 /**
190 * Subject key
191 */
192 struct GNUNET_CRYPTO_PublicKey subject_key;
193
194 /**
195 * Issuer attributes
196 */
197 uint32_t issuer_attribute_len GNUNET_PACKED;
198
199 /**
200 * Subject attributes
201 */
202 uint32_t subject_attribute_len GNUNET_PACKED;
203};
204
205
206struct DelegateEntry
207{
208 /**
209 * The signature for this credential by the issuer
210 */
211 struct GNUNET_CRYPTO_Signature signature;
212
213 /**
214 * Signature meta
215 */
216 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
217
218 /**
219 * Public key of the issuer
220 */
221 struct GNUNET_CRYPTO_PublicKey issuer_key;
222
223 /**
224 * Public key of the subject this credential was issued to
225 */
226 struct GNUNET_CRYPTO_PublicKey subject_key;
227
228 /**
229 * Expiration time of this credential
230 */
231 uint64_t expiration GNUNET_PACKED;
232
233 /**
234 * Issuer subject attribute length
235 */
236 uint32_t issuer_attribute_len;
237
238 /**
239 * Issuer attribute length
240 */
241 uint32_t subject_attribute_len;
242
243 /**
244 * Followed by the subject attribute string
245 */
246};
247
248
249GNUNET_NETWORK_STRUCT_END
250
251#endif
diff --git a/src/contrib/service/abd/abd_api.c b/src/contrib/service/abd/abd_api.c
new file mode 100644
index 000000000..3682f41ba
--- /dev/null
+++ b/src/contrib/service/abd/abd_api.c
@@ -0,0 +1,563 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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 abd/abd_api.c
22 * @brief library to access the ABD service
23 * @author Martin Schanzenbach
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_constants.h"
28#include "gnunet_arm_service.h"
29#include "gnunet_protocols.h"
30#include "gnunet_signatures.h"
31#include "abd.h"
32#include "abd_serialization.h"
33#include "gnunet_abd_service.h"
34#include "gnunet_identity_service.h"
35
36
37#define LOG(kind, ...) GNUNET_log_from (kind, "abd-api", __VA_ARGS__)
38
39/**
40 * Handle to a verify request
41 */
42struct GNUNET_ABD_Request
43{
44
45 /**
46 * DLL
47 */
48 struct GNUNET_ABD_Request *next;
49
50 /**
51 * DLL
52 */
53 struct GNUNET_ABD_Request *prev;
54
55 /**
56 * handle to abd service
57 */
58 struct GNUNET_ABD_Handle *abd_handle;
59
60 /**
61 * processor to call on verify result
62 */
63 GNUNET_ABD_CredentialResultProcessor verify_proc;
64
65 /**
66 * @e verify_proc closure
67 */
68 void *proc_cls;
69
70 /**
71 * processor to call on intermediate result
72 */
73 GNUNET_ABD_IntermediateResultProcessor int_proc;
74
75 /**
76 * @e verify_proc2 closure
77 */
78 void *proc2_cls;
79
80 /**
81 * Envelope with the message for this queue entry.
82 */
83 struct GNUNET_MQ_Envelope *env;
84
85 /**
86 * request id
87 */
88 uint32_t r_id;
89};
90
91
92/**
93 * Connection to the ABD service.
94 */
95struct GNUNET_ABD_Handle
96{
97
98 /**
99 * Configuration to use.
100 */
101 const struct GNUNET_CONFIGURATION_Handle *cfg;
102
103 /**
104 * Connection to service (if available).
105 */
106 struct GNUNET_MQ_Handle *mq;
107
108 /**
109 * Head of linked list of active verify requests.
110 */
111 struct GNUNET_ABD_Request *request_head;
112
113 /**
114 * Tail of linked list of active verify requests.
115 */
116 struct GNUNET_ABD_Request *request_tail;
117
118 /**
119 * Reconnect task
120 */
121 struct GNUNET_SCHEDULER_Task *reconnect_task;
122
123 /**
124 * How long do we wait until we try to reconnect?
125 */
126 struct GNUNET_TIME_Relative reconnect_backoff;
127
128 /**
129 * Request Id generator. Incremented by one for each request.
130 */
131 uint32_t r_id_gen;
132};
133
134
135/**
136 * Reconnect to ABD service.
137 *
138 * @param handle the handle to the ABD service
139 */
140static void
141reconnect (struct GNUNET_ABD_Handle *handle);
142
143
144/**
145 * Reconnect to ABD
146 *
147 * @param cls the handle
148 */
149static void
150reconnect_task (void *cls)
151{
152 struct GNUNET_ABD_Handle *handle = cls;
153
154 handle->reconnect_task = NULL;
155 reconnect (handle);
156}
157
158
159/**
160 * Disconnect from service and then reconnect.
161 *
162 * @param handle our handle
163 */
164static void
165force_reconnect (struct GNUNET_ABD_Handle *handle)
166{
167 GNUNET_MQ_destroy (handle->mq);
168 handle->mq = NULL;
169 handle->reconnect_backoff =
170 GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
171 handle->reconnect_task =
172 GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
173 &reconnect_task,
174 handle);
175}
176
177
178/**
179 * Generic error handler, called with the appropriate error code and
180 * the same closure specified at the creation of the message queue.
181 * Not every message queue implementation supports an error handler.
182 *
183 * @param cls closure with the `struct GNUNET_ABD_Handle *`
184 * @param error error code
185 */
186static void
187mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
188{
189 struct GNUNET_ABD_Handle *handle = cls;
190
191 force_reconnect (handle);
192}
193
194
195/**
196 * Check validity of message received from the ABD service
197 *
198 * @param cls the `struct GNUNET_ABD_Handle *`
199 * @param vr_msg the incoming message
200 */
201static int
202check_result (void *cls, const struct DelegationChainResultMessage *vr_msg)
203{
204 // TODO
205 return GNUNET_OK;
206}
207
208
209/**
210 * Handler for messages received from the ABD service
211 *
212 * @param cls the `struct GNUNET_ABD_Handle *`
213 * @param vr_msg the incoming message
214 */
215static void
216handle_result (void *cls, const struct DelegationChainResultMessage *vr_msg)
217{
218 struct GNUNET_ABD_Handle *handle = cls;
219 uint32_t r_id = ntohl (vr_msg->id);
220 struct GNUNET_ABD_Request *vr;
221 size_t mlen = ntohs (vr_msg->header.size) - sizeof (*vr_msg);
222 uint32_t d_count = ntohl (vr_msg->d_count);
223 uint32_t c_count = ntohl (vr_msg->c_count);
224 struct GNUNET_ABD_Delegation d_chain[d_count];
225 struct GNUNET_ABD_Delegate dels[c_count];
226 GNUNET_ABD_CredentialResultProcessor proc;
227 void *proc_cls;
228
229 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 "Received verify reply from ABD service\n");
231 for (vr = handle->request_head; NULL != vr; vr = vr->next)
232 if (vr->r_id == r_id)
233 break;
234 if (NULL == vr)
235 return;
236 proc = vr->verify_proc;
237 proc_cls = vr->proc_cls;
238 GNUNET_CONTAINER_DLL_remove (handle->request_head, handle->request_tail, vr);
239 GNUNET_MQ_discard (vr->env);
240 GNUNET_free (vr);
241 GNUNET_assert (
242 GNUNET_OK ==
243 GNUNET_ABD_delegation_chain_deserialize (mlen,
244 (const char *) &vr_msg[1],
245 d_count,
246 d_chain,
247 c_count,
248 dels));
249 if (GNUNET_NO == ntohl (vr_msg->del_found))
250 {
251 proc (proc_cls, 0, NULL, 0,
252 NULL);
253 }
254 else
255 {
256 proc (proc_cls, d_count, d_chain, c_count, dels);
257 }
258}
259
260
261static int
262check_intermediate (void *cls, const struct
263 DelegationChainIntermediateMessage *vr_msg)
264{
265 // TODO
266 return GNUNET_OK;
267}
268
269
270static void
271handle_intermediate (void *cls, const struct
272 DelegationChainIntermediateMessage *vr_msg)
273{
274 struct GNUNET_ABD_Handle *handle = cls;
275 uint32_t r_id = ntohl (vr_msg->id);
276 uint32_t size = ntohl (vr_msg->size);
277 bool is_bw = ntohs (vr_msg->is_bw);
278 struct GNUNET_ABD_Request *vr;
279 GNUNET_ABD_IntermediateResultProcessor proc;
280 void *proc_cls;
281 struct GNUNET_ABD_Delegation *dd;
282
283
284 LOG (GNUNET_ERROR_TYPE_DEBUG,
285 "Received intermediate reply from ABD service\n");
286
287 for (vr = handle->request_head; NULL != vr; vr = vr->next)
288 if (vr->r_id == r_id)
289 break;
290 if (NULL == vr)
291 return;
292
293 proc = vr->int_proc;
294 proc_cls = vr->proc2_cls;
295
296 dd = GNUNET_new (struct GNUNET_ABD_Delegation);
297 GNUNET_assert (
298 GNUNET_OK ==
299 GNUNET_ABD_delegation_chain_deserialize (size,
300 (const char *) &vr_msg[1],
301 1,
302 dd,
303 0,
304 NULL));
305 proc (proc_cls, dd, is_bw);
306}
307
308
309/**
310 * Reconnect to ABD service.
311 *
312 * @param handle the handle to the ABD service
313 */
314static void
315reconnect (struct GNUNET_ABD_Handle *handle)
316{
317 struct GNUNET_MQ_MessageHandler handlers[] =
318 {GNUNET_MQ_hd_var_size (result,
319 GNUNET_MESSAGE_TYPE_ABD_VERIFY_RESULT,
320 struct DelegationChainResultMessage,
321 handle),
322 GNUNET_MQ_hd_var_size (result,
323 GNUNET_MESSAGE_TYPE_ABD_COLLECT_RESULT,
324 struct DelegationChainResultMessage,
325 handle),
326 GNUNET_MQ_hd_var_size (intermediate,
327 GNUNET_MESSAGE_TYPE_ABD_INTERMEDIATE_RESULT,
328 struct DelegationChainIntermediateMessage,
329 handle),
330 GNUNET_MQ_handler_end ()};
331 struct GNUNET_ABD_Request *vr;
332
333 GNUNET_assert (NULL == handle->mq);
334 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to ABD\n");
335 handle->mq = GNUNET_CLIENT_connect (handle->cfg,
336 "abd",
337 handlers,
338 &mq_error_handler,
339 handle);
340 if (NULL == handle->mq)
341 return;
342 for (vr = handle->request_head; NULL != vr; vr = vr->next)
343 GNUNET_MQ_send_copy (handle->mq, vr->env);
344}
345
346
347/**
348 * Initialize the connection with the ABD service.
349 *
350 * @param cfg configuration to use
351 * @return handle to the ABD service, or NULL on error
352 */
353struct GNUNET_ABD_Handle *
354GNUNET_ABD_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
355{
356 struct GNUNET_ABD_Handle *handle;
357
358 handle = GNUNET_new (struct GNUNET_ABD_Handle);
359 handle->cfg = cfg;
360 reconnect (handle);
361 if (NULL == handle->mq)
362 {
363 GNUNET_free (handle);
364 return NULL;
365 }
366 return handle;
367}
368
369
370/**
371 * Shutdown connection with the ABD service.
372 *
373 * @param handle handle of the ABD connection to stop
374 */
375void
376GNUNET_ABD_disconnect (struct GNUNET_ABD_Handle *handle)
377{
378 if (NULL != handle->mq)
379 {
380 GNUNET_MQ_destroy (handle->mq);
381 handle->mq = NULL;
382 }
383 if (NULL != handle->reconnect_task)
384 {
385 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
386 handle->reconnect_task = NULL;
387 }
388 GNUNET_assert (NULL == handle->request_head);
389 GNUNET_free (handle);
390}
391
392
393/**
394 * Cancel pending verify request
395 *
396 * @param lr the verify request to cancel
397 */
398void
399GNUNET_ABD_request_cancel (struct GNUNET_ABD_Request *lr)
400{
401 struct GNUNET_ABD_Handle *handle = lr->abd_handle;
402
403 GNUNET_CONTAINER_DLL_remove (handle->request_head, handle->request_tail, lr);
404 GNUNET_MQ_discard (lr->env);
405 GNUNET_free (lr);
406}
407
408
409/**
410 * Performs attribute collection.
411 * Collects all abds of subject to fulfill the
412 * attribute, if possible
413 *
414 * @param handle handle to the Credential service
415 * @param issuer_key the issuer public key
416 * @param issuer_attribute the issuer attribute
417 * @param subject_key the subject public key
418 * @param proc function to call on result
419 * @param proc_cls closure for processor
420 * @return handle to the queued request
421 */
422struct GNUNET_ABD_Request *
423GNUNET_ABD_collect (
424 struct GNUNET_ABD_Handle *handle,
425 const struct GNUNET_CRYPTO_PublicKey *issuer_key,
426 const char *issuer_attribute,
427 const struct GNUNET_CRYPTO_PrivateKey *subject_key,
428 enum GNUNET_ABD_AlgoDirectionFlags direction,
429 GNUNET_ABD_CredentialResultProcessor proc,
430 void *proc_cls,
431 GNUNET_ABD_IntermediateResultProcessor proc2,
432 void *proc2_cls)
433{
434 /* IPC to shorten abd names, return shorten_handle */
435 struct CollectMessage *c_msg;
436 struct GNUNET_ABD_Request *vr;
437 size_t nlen;
438
439 if (NULL == issuer_attribute)
440 {
441 GNUNET_break (0);
442 return NULL;
443 }
444
445 // DEBUG LOG
446 LOG (GNUNET_ERROR_TYPE_DEBUG,
447 "Trying to collect `%s' in ABD\n",
448 issuer_attribute);
449 nlen = strlen (issuer_attribute) + 1;
450 if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*vr))
451 {
452 GNUNET_break (0);
453 return NULL;
454 }
455 vr = GNUNET_new (struct GNUNET_ABD_Request);
456 vr->abd_handle = handle;
457 vr->verify_proc = proc;
458 vr->proc_cls = proc_cls;
459 vr->int_proc = proc2;
460 vr->proc2_cls = proc2_cls;
461 vr->r_id = handle->r_id_gen++;
462 vr->env =
463 GNUNET_MQ_msg_extra (c_msg, nlen, GNUNET_MESSAGE_TYPE_ABD_COLLECT);
464 c_msg->id = htonl (vr->r_id);
465 c_msg->subject_key = *subject_key;
466 c_msg->issuer_key = *issuer_key;
467 c_msg->issuer_attribute_len = htons (strlen (issuer_attribute));
468 c_msg->resolution_algo = htons (direction);
469
470 GNUNET_memcpy (&c_msg[1], issuer_attribute, strlen (issuer_attribute));
471 GNUNET_CONTAINER_DLL_insert (handle->request_head, handle->request_tail, vr);
472 if (NULL != handle->mq)
473 GNUNET_MQ_send_copy (handle->mq, vr->env);
474 return vr;
475}
476
477
478/**
479 * Performs attribute verification.
480 * Checks if there is a delegation chain from
481 * attribute ``issuer_attribute'' issued by the issuer
482 * with public key ``issuer_key'' maps to the attribute
483 * ``subject_attribute'' claimed by the subject with key
484 * ``subject_key''
485 *
486 * @param handle handle to the Credential service
487 * @param issuer_key the issuer public key
488 * @param issuer_attribute the issuer attribute
489 * @param subject_key the subject public key
490 * @param delegate_count number of delegates provided
491 * @param delegates subject delegates
492 * @param proc function to call on result
493 * @param proc_cls closure for processor
494 * @return handle to the queued request
495 */
496struct GNUNET_ABD_Request *
497GNUNET_ABD_verify (
498 struct GNUNET_ABD_Handle *handle,
499 const struct GNUNET_CRYPTO_PublicKey *issuer_key,
500 const char *issuer_attribute,
501 const struct GNUNET_CRYPTO_PublicKey *subject_key,
502 uint32_t delegate_count,
503 const struct GNUNET_ABD_Delegate *delegates,
504 enum GNUNET_ABD_AlgoDirectionFlags direction,
505 GNUNET_ABD_CredentialResultProcessor proc,
506 void *proc_cls,
507 GNUNET_ABD_IntermediateResultProcessor proc2,
508 void *proc2_cls)
509{
510 /* IPC to shorten abd names, return shorten_handle */
511 struct VerifyMessage *v_msg;
512 struct GNUNET_ABD_Request *vr;
513 size_t nlen;
514 size_t clen;
515
516 if ((NULL == issuer_attribute) || (NULL == delegates))
517 {
518 GNUNET_break (0);
519 return NULL;
520 }
521
522 clen = GNUNET_ABD_delegates_get_size (delegate_count, delegates);
523
524 // DEBUG LOG
525 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 "Trying to verify `%s' in ABD\n",
527 issuer_attribute);
528 nlen = strlen (issuer_attribute) + 1 + clen;
529 if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*vr))
530 {
531 GNUNET_break (0);
532 return NULL;
533 }
534 vr = GNUNET_new (struct GNUNET_ABD_Request);
535 vr->abd_handle = handle;
536 vr->verify_proc = proc;
537 vr->proc_cls = proc_cls;
538 vr->int_proc = proc2;
539 vr->proc2_cls = proc2_cls;
540 vr->r_id = handle->r_id_gen++;
541 vr->env =
542 GNUNET_MQ_msg_extra (v_msg, nlen, GNUNET_MESSAGE_TYPE_ABD_VERIFY);
543 v_msg->id = htonl (vr->r_id);
544 v_msg->subject_key = *subject_key;
545 v_msg->d_count = htonl (delegate_count);
546 v_msg->issuer_key = *issuer_key;
547 v_msg->issuer_attribute_len = htons (strlen (issuer_attribute));
548 v_msg->resolution_algo = htons (direction);
549
550 GNUNET_memcpy (&v_msg[1], issuer_attribute, strlen (issuer_attribute));
551 GNUNET_ABD_delegates_serialize (delegate_count,
552 delegates,
553 clen,
554 ((char *) &v_msg[1])
555 + strlen (issuer_attribute) + 1);
556 GNUNET_CONTAINER_DLL_insert (handle->request_head, handle->request_tail, vr);
557 if (NULL != handle->mq)
558 GNUNET_MQ_send_copy (handle->mq, vr->env);
559 return vr;
560}
561
562
563/* end of abd_api.c */
diff --git a/src/contrib/service/abd/abd_serialization.c b/src/contrib/service/abd/abd_serialization.c
new file mode 100644
index 000000000..14960e804
--- /dev/null
+++ b/src/contrib/service/abd/abd_serialization.c
@@ -0,0 +1,516 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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/**
23 * @file abd/abd_serialization.c
24 * @brief API to serialize and deserialize delegation chains
25 * and abds
26 * @author Martin Schanzenbach
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_constants.h"
31#include "gnunet_abd_service.h"
32#include "gnunet_signatures.h"
33#include "abd.h"
34
35/**
36 * Calculate how many bytes we will need to serialize
37 * the given delegation chain
38 *
39 * @param ds_count number of delegation chain entries
40 * @param dsr array of #GNUNET_ABD_DelegationSet
41 * @return the required size to serialize
42 */
43size_t
44GNUNET_ABD_delegation_set_get_size (
45 unsigned int ds_count,
46 const struct GNUNET_ABD_DelegationSet *dsr)
47{
48 unsigned int i;
49 size_t ret;
50
51 ret = sizeof (struct DelegationRecordData) * (ds_count);
52
53 for (i = 0; i < ds_count; i++)
54 {
55 GNUNET_assert ((ret + dsr[i].subject_attribute_len) >= ret);
56 ret += dsr[i].subject_attribute_len;
57 }
58 return ret;
59}
60
61
62/**
63 * Serizalize the given delegation chain entries and abd
64 *
65 * @param d_count number of delegation chain entries
66 * @param dsr array of #GNUNET_ABD_DelegationSet
67 * @param dest_size size of the destination
68 * @param dest where to store the result
69 * @return the size of the data, -1 on failure
70 */
71ssize_t
72GNUNET_ABD_delegation_set_serialize (
73 unsigned int d_count,
74 const struct GNUNET_ABD_DelegationSet *dsr,
75 size_t dest_size,
76 char *dest)
77{
78 struct DelegationRecordData rec;
79 unsigned int i;
80 size_t off;
81
82 off = 0;
83 for (i = 0; i < d_count; i++)
84 {
85 rec.subject_attribute_len = htonl ((uint32_t) dsr[i].subject_attribute_len);
86 rec.subject_key = dsr[i].subject_key;
87 if (off + sizeof (rec) > dest_size)
88 return -1;
89 GNUNET_memcpy (&dest[off], &rec, sizeof (rec));
90 off += sizeof (rec);
91 if (0 == dsr[i].subject_attribute_len)
92 continue;
93 if (off + dsr[i].subject_attribute_len > dest_size)
94 return -1;
95 GNUNET_memcpy (&dest[off],
96 dsr[i].subject_attribute,
97 dsr[i].subject_attribute_len);
98 off += dsr[i].subject_attribute_len;
99 }
100 return off;
101}
102
103
104/**
105 * Deserialize the given destination
106 *
107 * @param len size of the serialized delegation chain and cred
108 * @param src the serialized data
109 * @param d_count the number of delegation chain entries
110 * @param dsr where to put the delegation chain entries
111 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
112 */
113int
114GNUNET_ABD_delegation_set_deserialize (
115 size_t len,
116 const char *src,
117 unsigned int d_count,
118 struct GNUNET_ABD_DelegationSet *dsr)
119{
120 struct DelegationRecordData rec;
121 unsigned int i;
122 size_t off;
123
124 off = 0;
125 for (i = 0; i < d_count; i++)
126 {
127 if (off + sizeof (rec) > len)
128 return GNUNET_SYSERR;
129 GNUNET_memcpy (&rec, &src[off], sizeof (rec));
130 dsr[i].subject_key = rec.subject_key;
131 off += sizeof (rec);
132 dsr[i].subject_attribute_len = ntohl ((uint32_t) rec.subject_attribute_len);
133 if (off + dsr[i].subject_attribute_len > len)
134 return GNUNET_SYSERR;
135 dsr[i].subject_attribute = (char *) &src[off];
136 off += dsr[i].subject_attribute_len;
137 }
138 return GNUNET_OK;
139}
140
141
142/**
143 * Calculate how many bytes we will need to serialize
144 * the abds
145 *
146 * @param c_count number of abd entries
147 * @param cd a #GNUNET_ABD_Credential
148 * @return the required size to serialize
149 */
150size_t
151GNUNET_ABD_delegates_get_size (
152 unsigned int c_count,
153 const struct GNUNET_ABD_Delegate *cd)
154{
155 unsigned int i;
156 size_t ret;
157
158 ret = sizeof (struct DelegateEntry) * (c_count);
159
160 for (i = 0; i < c_count; i++)
161 {
162 GNUNET_assert ((ret + cd[i].issuer_attribute_len
163 + cd[i].subject_attribute_len) >= ret);
164 // subject_attribute_len should be 0
165 ret += cd[i].issuer_attribute_len + cd[i].subject_attribute_len;
166 }
167 return ret;
168}
169
170
171/**
172 * Serizalize the given abds
173 *
174 * @param c_count number of abd entries
175 * @param cd a #GNUNET_ABD_Credential
176 * @param dest_size size of the destination
177 * @param dest where to store the result
178 * @return the size of the data, -1 on failure
179 */
180ssize_t
181GNUNET_ABD_delegates_serialize (
182 unsigned int c_count,
183 const struct GNUNET_ABD_Delegate *cd,
184 size_t dest_size,
185 char *dest)
186{
187 struct DelegateEntry c_rec;
188 unsigned int i;
189 size_t off;
190
191 off = 0;
192 for (i = 0; i < c_count; i++)
193 {
194 c_rec.subject_attribute_len = htonl (cd[i].subject_attribute_len);
195 c_rec.issuer_attribute_len = htonl (cd[i].issuer_attribute_len);
196 c_rec.issuer_key = cd[i].issuer_key;
197 c_rec.subject_key = cd[i].subject_key;
198 c_rec.signature = cd[i].signature;
199 c_rec.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DELEGATE);
200 c_rec.purpose.size =
201 htonl ((sizeof (struct DelegateEntry) + cd[i].issuer_attribute_len)
202 - sizeof (struct GNUNET_CRYPTO_Signature));
203 c_rec.expiration = GNUNET_htonll (cd[i].expiration.abs_value_us);
204 if (off + sizeof (c_rec) > dest_size)
205 return -1;
206 GNUNET_memcpy (&dest[off], &c_rec, sizeof (c_rec));
207 off += sizeof (c_rec);
208 if (off + cd[i].issuer_attribute_len > dest_size)
209 return -1;
210 GNUNET_memcpy (&dest[off],
211 cd[i].issuer_attribute,
212 cd[i].issuer_attribute_len);
213 off += cd[i].issuer_attribute_len;
214 }
215
216 return off;
217}
218
219
220/**
221 * Deserialize the given destination
222 *
223 * @param len size of the serialized creds
224 * @param src the serialized data
225 * @param c_count the number of abd entries
226 * @param cd where to put the abd data
227 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
228 */
229int
230GNUNET_ABD_delegates_deserialize (size_t len,
231 const char *src,
232 unsigned int c_count,
233 struct GNUNET_ABD_Delegate *cd)
234{
235 struct DelegateEntry c_rec;
236 unsigned int i;
237 size_t off;
238
239 off = 0;
240 for (i = 0; i < c_count; i++)
241 {
242 if (off + sizeof (c_rec) > len)
243 return GNUNET_SYSERR;
244 GNUNET_memcpy (&c_rec, &src[off], sizeof (c_rec));
245 cd[i].issuer_attribute_len = ntohl ((uint32_t) c_rec.issuer_attribute_len);
246 cd[i].issuer_key = c_rec.issuer_key;
247 cd[i].subject_key = c_rec.subject_key;
248 cd[i].signature = c_rec.signature;
249 cd[i].expiration.abs_value_us = GNUNET_ntohll (c_rec.expiration);
250 off += sizeof (c_rec);
251 if (off + cd[i].issuer_attribute_len > len)
252 return GNUNET_SYSERR;
253 cd[i].issuer_attribute = &src[off];
254 off += cd[i].issuer_attribute_len;
255 cd[i].subject_attribute_len = 0;
256 }
257 return GNUNET_OK;
258}
259
260
261/**
262 * Calculate how many bytes we will need to serialize
263 * the given delegation chain and abd
264 *
265 * @param d_count number of delegation chain entries
266 * @param dd array of #GNUNET_ABD_Delegation
267 * @param c_count number of abd entries
268 * @param cd a #GNUNET_ABD_Credential
269 * @return the required size to serialize
270 */
271size_t
272GNUNET_ABD_delegation_chain_get_size (
273 unsigned int d_count,
274 const struct GNUNET_ABD_Delegation *dd,
275 unsigned int c_count,
276 const struct GNUNET_ABD_Delegate *cd)
277{
278 unsigned int i;
279 size_t ret;
280
281 ret = sizeof (struct ChainEntry) * (d_count);
282
283 for (i = 0; i < d_count; i++)
284 {
285 GNUNET_assert (
286 (ret + dd[i].issuer_attribute_len + dd[i].subject_attribute_len) >= ret);
287 ret += dd[i].issuer_attribute_len + dd[i].subject_attribute_len;
288 }
289 return ret + GNUNET_ABD_delegates_get_size (c_count, cd);
290}
291
292
293/**
294 * Serizalize the given delegation chain entries and abd
295 *
296 * @param d_count number of delegation chain entries
297 * @param dd array of #GNUNET_ABD_Delegation
298 * @param c_count number of abd entries
299 * @param cd a #GNUNET_ABD_Credential
300 * @param dest_size size of the destination
301 * @param dest where to store the result
302 * @return the size of the data, -1 on failure
303 */
304ssize_t
305GNUNET_ABD_delegation_chain_serialize (
306 unsigned int d_count,
307 const struct GNUNET_ABD_Delegation *dd,
308 unsigned int c_count,
309 const struct GNUNET_ABD_Delegate *cd,
310 size_t dest_size,
311 char *dest)
312{
313 struct ChainEntry rec;
314 unsigned int i;
315 size_t off;
316
317 off = 0;
318 for (i = 0; i < d_count; i++)
319 {
320 rec.issuer_attribute_len = htonl ((uint32_t) dd[i].issuer_attribute_len);
321 rec.subject_attribute_len = htonl ((uint32_t) dd[i].subject_attribute_len);
322 rec.issuer_key = dd[i].issuer_key;
323 rec.subject_key = dd[i].subject_key;
324 if (off + sizeof (rec) > dest_size)
325 return -1;
326 GNUNET_memcpy (&dest[off], &rec, sizeof (rec));
327 off += sizeof (rec);
328 if (off + dd[i].issuer_attribute_len > dest_size)
329 return -1;
330 GNUNET_memcpy (&dest[off],
331 dd[i].issuer_attribute,
332 dd[i].issuer_attribute_len);
333 off += dd[i].issuer_attribute_len;
334 if (0 == dd[i].subject_attribute_len)
335 continue;
336 if (off + dd[i].subject_attribute_len > dest_size)
337 return -1;
338 GNUNET_memcpy (&dest[off],
339 dd[i].subject_attribute,
340 dd[i].subject_attribute_len);
341 off += dd[i].subject_attribute_len;
342 }
343 return off + GNUNET_ABD_delegates_serialize (c_count,
344 cd,
345 dest_size - off,
346 &dest[off]);
347}
348
349
350/**
351 * Deserialize the given destination
352 *
353 * @param len size of the serialized delegation chain and cred
354 * @param src the serialized data
355 * @param d_count the number of delegation chain entries
356 * @param dd where to put the delegation chain entries
357 * @param c_count the number of abd entries
358 * @param cd where to put the abd data
359 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
360 */
361int
362GNUNET_ABD_delegation_chain_deserialize (
363 size_t len,
364 const char *src,
365 unsigned int d_count,
366 struct GNUNET_ABD_Delegation *dd,
367 unsigned int c_count,
368 struct GNUNET_ABD_Delegate *cd)
369{
370 struct ChainEntry rec;
371 unsigned int i;
372 size_t off;
373
374 off = 0;
375 for (i = 0; i < d_count; i++)
376 {
377 if (off + sizeof (rec) > len)
378 return GNUNET_SYSERR;
379 GNUNET_memcpy (&rec, &src[off], sizeof (rec));
380 dd[i].issuer_attribute_len = ntohl ((uint32_t) rec.issuer_attribute_len);
381 dd[i].issuer_key = rec.issuer_key;
382 dd[i].subject_key = rec.subject_key;
383 off += sizeof (rec);
384 if (off + dd[i].issuer_attribute_len > len)
385 return GNUNET_SYSERR;
386 dd[i].issuer_attribute = &src[off];
387 off += dd[i].issuer_attribute_len;
388 dd[i].subject_attribute_len = ntohl ((uint32_t) rec.subject_attribute_len);
389 if (off + dd[i].subject_attribute_len > len)
390 return GNUNET_SYSERR;
391 dd[i].subject_attribute = &src[off];
392 off += dd[i].subject_attribute_len;
393 }
394 return GNUNET_ABD_delegates_deserialize (len - off,
395 &src[off],
396 c_count,
397 cd);
398}
399
400
401int
402GNUNET_ABD_delegate_serialize (struct GNUNET_ABD_Delegate *dele,
403 char **data)
404{
405 size_t size;
406 struct DelegateEntry *cdata;
407 int attr_len;
408
409 // +1 for \0
410 if (0 == dele->subject_attribute_len)
411 {
412 attr_len = dele->issuer_attribute_len + 1;
413 }
414 else
415 {
416 attr_len = dele->issuer_attribute_len + dele->subject_attribute_len + 2;
417 }
418 size = sizeof (struct DelegateEntry) + attr_len;
419
420 char tmp_str[attr_len];
421 GNUNET_memcpy (tmp_str, dele->issuer_attribute, dele->issuer_attribute_len);
422 if (0 != dele->subject_attribute_len)
423 {
424 tmp_str[dele->issuer_attribute_len] = '\0';
425 GNUNET_memcpy (tmp_str + dele->issuer_attribute_len + 1,
426 dele->subject_attribute,
427 dele->subject_attribute_len);
428 }
429 tmp_str[attr_len - 1] = '\0';
430
431 *data = GNUNET_malloc (size);
432 cdata = (struct DelegateEntry *) *data;
433 cdata->subject_key = dele->subject_key;
434 cdata->issuer_key = dele->issuer_key;
435 cdata->expiration = GNUNET_htonll (dele->expiration.abs_value_us);
436 cdata->signature = dele->signature;
437 cdata->issuer_attribute_len = htonl (dele->issuer_attribute_len + 1);
438 if (0 == dele->subject_attribute_len)
439 {
440 cdata->subject_attribute_len = htonl (0);
441 }
442 else
443 {
444 cdata->subject_attribute_len = htonl (dele->subject_attribute_len + 1);
445 }
446 cdata->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DELEGATE);
447 cdata->purpose.size =
448 htonl (size - sizeof (struct GNUNET_CRYPTO_Signature));
449
450 GNUNET_memcpy (&cdata[1], tmp_str, attr_len);
451
452 if (GNUNET_OK !=
453 GNUNET_CRYPTO_signature_verify_ (GNUNET_SIGNATURE_PURPOSE_DELEGATE,
454 &cdata->purpose,
455 &cdata->signature,
456 &cdata->issuer_key))
457 {
458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Serialize: Invalid delegate\n");
459 return 0;
460 }
461 return size;
462}
463
464
465struct GNUNET_ABD_Delegate *
466GNUNET_ABD_delegate_deserialize (const char *data, size_t data_size)
467{
468 struct GNUNET_ABD_Delegate *dele;
469 struct DelegateEntry *cdata;
470 char *attr_combo_str;
471
472 if (data_size < sizeof (struct DelegateEntry))
473 return NULL;
474 cdata = (struct DelegateEntry *) data;
475 if (GNUNET_OK !=
476 GNUNET_CRYPTO_signature_verify_ (GNUNET_SIGNATURE_PURPOSE_DELEGATE,
477 &cdata->purpose,
478 &cdata->signature,
479 &cdata->issuer_key))
480 {
481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Deserialize: Invalid delegate\n");
482 return NULL;
483 }
484 attr_combo_str = (char *) &cdata[1];
485 int iss_len = ntohl (cdata->issuer_attribute_len);
486 int sub_len = ntohl (cdata->subject_attribute_len);
487 int attr_combo_len = iss_len + sub_len;
488
489 dele =
490 GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_combo_len);
491
492 dele->issuer_key = cdata->issuer_key;
493 dele->subject_key = cdata->subject_key;
494 GNUNET_memcpy (&dele[1], attr_combo_str, attr_combo_len);
495 dele->signature = cdata->signature;
496
497 // Set the pointers for the attributes
498 dele->issuer_attribute = (char *) &dele[1];
499 dele->issuer_attribute_len = iss_len;
500 dele->subject_attribute_len = sub_len;
501 if (0 == sub_len)
502 {
503 dele->subject_attribute = NULL;
504 }
505 else
506 {
507 dele->subject_attribute = (char *) &dele[1] + iss_len;
508 }
509
510 dele->expiration.abs_value_us = GNUNET_ntohll (cdata->expiration);
511
512 return dele;
513}
514
515
516/* end of abd_serialization.c */
diff --git a/src/contrib/service/abd/abd_serialization.h b/src/contrib/service/abd/abd_serialization.h
new file mode 100644
index 000000000..21dc45a7b
--- /dev/null
+++ b/src/contrib/service/abd/abd_serialization.h
@@ -0,0 +1,167 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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/**
23 * @file abd/abd_serialization.h
24 * @brief API to serialize and deserialize delegation chains
25 * and abds
26 * @author Martin Schanzenbach
27 */
28#ifndef ABD_SERIALIZATION_H
29#define ABD_SERIALIZATION_H
30
31#include "platform.h"
32#include "gnunet_util_lib.h"
33#include "gnunet_constants.h"
34#include "gnunet_abd_service.h"
35
36/**
37 * Calculate how many bytes we will need to serialize
38 * the given delegation record
39 *
40 * @param ds_count number of delegation chain entries
41 * @param dsr array of #GNUNET_ABD_Delegation
42 * @return the required size to serialize
43 */
44size_t
45GNUNET_ABD_delegation_set_get_size (
46 unsigned int ds_count,
47 const struct GNUNET_ABD_DelegationSet *dsr);
48
49/**
50 * Serizalize the given delegation record entries
51 *
52 * @param d_count number of delegation chain entries
53 * @param dsr array of #GNUNET_ABD_Delegation
54 * @param dest_size size of the destination
55 * @param dest where to store the result
56 * @return the size of the data, -1 on failure
57 */
58ssize_t
59GNUNET_ABD_delegation_set_serialize (
60 unsigned int d_count,
61 const struct GNUNET_ABD_DelegationSet *dsr,
62 size_t dest_size,
63 char *dest);
64
65
66/**
67 * Deserialize the given destination
68 *
69 * @param len size of the serialized delegation recird
70 * @param src the serialized data
71 * @param d_count the number of delegation chain entries
72 * @param dsr where to put the delegation chain entries
73 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
74 */
75int
76GNUNET_ABD_delegation_set_deserialize (
77 size_t len,
78 const char *src,
79 unsigned int d_count,
80 struct GNUNET_ABD_DelegationSet *dsr);
81
82/**
83 * Calculate how many bytes we will need to serialize
84 * the given delegation chain and abd
85 *
86 * @param d_count number of delegation chain entries
87 * @param dd array of #GNUNET_ABD_Delegation
88 * @param c_count number of abd entries
89 * @param cd a #GNUNET_ABD_Delegate
90 * @return the required size to serialize
91 */
92size_t
93GNUNET_ABD_delegation_chain_get_size (
94 unsigned int d_count,
95 const struct GNUNET_ABD_Delegation *dd,
96 unsigned int c_count,
97 const struct GNUNET_ABD_Delegate *cd);
98
99/**
100 * Serizalize the given delegation chain entries and abd
101 *
102 * @param d_count number of delegation chain entries
103 * @param dd array of #GNUNET_ABD_Delegation
104 * @param c_count number of abd entries
105 * @param cd a #GNUNET_ABD_Delegate
106 * @param dest_size size of the destination
107 * @param dest where to store the result
108 * @return the size of the data, -1 on failure
109 */
110ssize_t
111GNUNET_ABD_delegation_chain_serialize (
112 unsigned int d_count,
113 const struct GNUNET_ABD_Delegation *dd,
114 unsigned int c_count,
115 const struct GNUNET_ABD_Delegate *cd,
116 size_t dest_size,
117 char *dest);
118
119
120/**
121 * Deserialize the given destination
122 *
123 * @param len size of the serialized delegation chain and cred
124 * @param src the serialized data
125 * @param d_count the number of delegation chain entries
126 * @param dd where to put the delegation chain entries
127 * @param c_count number of abd entries
128 * @param cd where to put the abd data
129 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
130 */
131int
132GNUNET_ABD_delegation_chain_deserialize (
133 size_t len,
134 const char *src,
135 unsigned int d_count,
136 struct GNUNET_ABD_Delegation *dd,
137 unsigned int c_count,
138 struct GNUNET_ABD_Delegate *cd);
139
140size_t
141GNUNET_ABD_delegates_get_size (
142 unsigned int c_count,
143 const struct GNUNET_ABD_Delegate *cd);
144
145ssize_t
146GNUNET_ABD_delegates_serialize (
147 unsigned int c_count,
148 const struct GNUNET_ABD_Delegate *cd,
149 size_t dest_size,
150 char *dest);
151
152
153int
154GNUNET_ABD_delegates_deserialize (size_t len,
155 const char *src,
156 unsigned int c_count,
157 struct GNUNET_ABD_Delegate *cd);
158
159int
160GNUNET_ABD_delegate_serialize (struct GNUNET_ABD_Delegate *cred,
161 char **data);
162
163struct GNUNET_ABD_Delegate *
164GNUNET_ABD_delegate_deserialize (const char *data, size_t data_size);
165
166#endif
167/* end of abd_serialization.h */
diff --git a/src/contrib/service/abd/delegate_misc.c b/src/contrib/service/abd/delegate_misc.c
new file mode 100644
index 000000000..446c0b8d2
--- /dev/null
+++ b/src/contrib/service/abd/delegate_misc.c
@@ -0,0 +1,279 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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/**
23 * @file abd/delegate_misc.c
24 * @brief Misc API for delegate
25 *
26 * @author Martin Schanzenbach
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_constants.h"
31#include "gnunet_abd_service.h"
32#include "gnunet_signatures.h"
33#include "abd.h"
34#include <inttypes.h>
35
36char *
37GNUNET_ABD_delegate_to_string (
38 const struct GNUNET_ABD_Delegate *cred)
39{
40 char *cred_str;
41 char *subject_pkey;
42 char *issuer_pkey;
43 char *signature;
44
45 subject_pkey = GNUNET_CRYPTO_public_key_to_string (&cred->subject_key);
46 issuer_pkey = GNUNET_CRYPTO_public_key_to_string (&cred->issuer_key);
47 GNUNET_STRINGS_base64_encode ((char *) &cred->signature,
48 sizeof (struct GNUNET_CRYPTO_Signature),
49 &signature);
50 if (0 == cred->subject_attribute_len)
51 {
52 GNUNET_asprintf (&cred_str,
53 "%s.%s -> %s | %s | %" SCNu64,
54 issuer_pkey,
55 cred->issuer_attribute,
56 subject_pkey,
57 signature,
58 cred->expiration.abs_value_us);
59 }
60 else
61 {
62 GNUNET_asprintf (&cred_str,
63 "%s.%s -> %s.%s | %s | %" SCNu64,
64 issuer_pkey,
65 cred->issuer_attribute,
66 subject_pkey,
67 cred->subject_attribute,
68 signature,
69 cred->expiration.abs_value_us);
70 }
71 GNUNET_free (subject_pkey);
72 GNUNET_free (issuer_pkey);
73 GNUNET_free (signature);
74
75 return cred_str;
76}
77
78
79struct GNUNET_ABD_Delegate *
80GNUNET_ABD_delegate_from_string (const char *s)
81{
82 struct GNUNET_ABD_Delegate *dele;
83 size_t enclen = (sizeof (struct GNUNET_CRYPTO_PublicKey)) * 8;
84 if (enclen % 5 > 0)
85 enclen += 5 - enclen % 5;
86 enclen /= 5; /* 260/5 = 52 */
87 char subject_pkey[enclen + 1];
88 char issuer_pkey[enclen + 1];
89 char iss_attr[253 + 1];
90 // Needs to be initialized, in case of Type 1 credential (A.a <- B)
91 char sub_attr[253 + 1] = "";
92 char signature[256]; // TODO max payload size
93
94 struct GNUNET_CRYPTO_Signature *sig;
95 struct GNUNET_TIME_Absolute etime_abs;
96
97 // If it's A.a <- B.b...
98 if (6 != sscanf (s,
99 "%58s.%253s -> %58s.%253s | %s | %" SCNu64,
100 issuer_pkey,
101 iss_attr,
102 subject_pkey,
103 sub_attr,
104 signature,
105 &etime_abs.abs_value_us))
106 {
107 // Try if it's A.a <- B
108 if (5 != sscanf (s,
109 "%58s.%253s -> %58s | %s | %" SCNu64,
110 issuer_pkey,
111 iss_attr,
112 subject_pkey,
113 signature,
114 &etime_abs.abs_value_us))
115 {
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 "Unable to parse DEL record string `%s'\n",
118 s);
119 return NULL;
120 }
121 }
122
123 // +1 for \0
124 int attr_len;
125 if (strcmp (sub_attr, "") == 0)
126 {
127 attr_len = strlen (iss_attr) + 1;
128 }
129 else
130 {
131 attr_len = strlen (iss_attr) + strlen (sub_attr) + 2;
132 }
133 dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len);
134
135 char tmp_str[attr_len];
136 GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr));
137 if (strcmp (sub_attr, "") != 0)
138 {
139 tmp_str[strlen (iss_attr)] = '\0';
140 GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1,
141 sub_attr,
142 strlen (sub_attr));
143 }
144 tmp_str[attr_len - 1] = '\0';
145
146 if (GNUNET_SYSERR ==
147 GNUNET_CRYPTO_public_key_from_string (subject_pkey,
148 &dele->subject_key))
149 {
150 GNUNET_free (dele);
151 return NULL;
152 }
153 if (GNUNET_SYSERR ==
154 GNUNET_CRYPTO_public_key_from_string (issuer_pkey,
155 &dele->issuer_key))
156 {
157 GNUNET_free (dele);
158 return NULL;
159 }
160 GNUNET_assert (sizeof (struct GNUNET_CRYPTO_Signature) ==
161 GNUNET_STRINGS_base64_decode (signature,
162 strlen (signature),
163 (void **) &sig));
164 dele->signature = *sig;
165 dele->expiration = etime_abs;
166 GNUNET_free (sig);
167
168 GNUNET_memcpy (&dele[1], tmp_str, attr_len);
169
170 dele->issuer_attribute = (char *) &dele[1];
171 dele->issuer_attribute_len = strlen (iss_attr);
172 if (strcmp (sub_attr, "") == 0)
173 {
174 dele->subject_attribute = NULL;
175 dele->subject_attribute_len = 0;
176 }
177 else
178 {
179 dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1;
180 dele->subject_attribute_len = strlen (sub_attr);
181 }
182
183 return dele;
184}
185
186
187/**
188 * Issue an attribute to a subject
189 *
190 * @param issuer the ego that should be used to issue the attribute
191 * @param subject the subject of the attribute
192 * @param iss_attr the name of the attribute
193 * @param expiration the TTL of the credential
194 * @return handle to the queued request
195 */
196
197struct GNUNET_ABD_Delegate *
198GNUNET_ABD_delegate_issue (
199 const struct GNUNET_CRYPTO_PrivateKey *issuer,
200 struct GNUNET_CRYPTO_PublicKey *subject,
201 const char *iss_attr,
202 const char *sub_attr,
203 struct GNUNET_TIME_Absolute *expiration)
204{
205 struct DelegateEntry *del;
206 struct GNUNET_ABD_Delegate *dele;
207 size_t size;
208 int attr_len;
209
210 if (NULL == sub_attr)
211 {
212 // +1 for \0
213 attr_len = strlen (iss_attr) + 1;
214 }
215 else
216 {
217 // +2 for both strings need to be terminated with \0
218 attr_len = strlen (iss_attr) + strlen (sub_attr) + 2;
219 }
220 size = sizeof (struct DelegateEntry) + attr_len;
221
222 char tmp_str[attr_len];
223 GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr));
224 if (NULL != sub_attr)
225 {
226 tmp_str[strlen (iss_attr)] = '\0';
227 GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1,
228 sub_attr,
229 strlen (sub_attr));
230 }
231 tmp_str[attr_len - 1] = '\0';
232
233 del = GNUNET_malloc (size);
234 del->purpose.size =
235 htonl (size - sizeof (struct GNUNET_CRYPTO_Signature));
236 del->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DELEGATE);
237 GNUNET_CRYPTO_key_get_public (issuer, &del->issuer_key);
238 del->subject_key = *subject;
239 del->expiration = GNUNET_htonll (expiration->abs_value_us);
240 del->issuer_attribute_len = htonl (strlen (iss_attr) + 1);
241 if (NULL == sub_attr)
242 {
243 del->subject_attribute_len = htonl (0);
244 }
245 else
246 {
247 del->subject_attribute_len = htonl (strlen (sub_attr) + 1);
248 }
249
250 GNUNET_memcpy (&del[1], tmp_str, attr_len);
251
252 GNUNET_CRYPTO_sign_ (issuer, &del->purpose, &del->signature);
253
254 dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len);
255 dele->signature = del->signature;
256 dele->expiration = *expiration;
257 GNUNET_CRYPTO_key_get_public (issuer, &dele->issuer_key);
258
259 dele->subject_key = *subject;
260
261 // Copy the combined string at the part in the memory where the struct ends
262 GNUNET_memcpy (&dele[1], tmp_str, attr_len);
263
264 dele->issuer_attribute = (char *) &dele[1];
265 dele->issuer_attribute_len = strlen (iss_attr);
266 if (NULL == sub_attr)
267 {
268 dele->subject_attribute = NULL;
269 dele->subject_attribute_len = 0;
270 }
271 else
272 {
273 dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1;
274 dele->subject_attribute_len = strlen (sub_attr);
275 }
276
277 GNUNET_free (del);
278 return dele;
279}
diff --git a/src/contrib/service/abd/delegate_misc.h b/src/contrib/service/abd/delegate_misc.h
new file mode 100644
index 000000000..0a4ddd9eb
--- /dev/null
+++ b/src/contrib/service/abd/delegate_misc.h
@@ -0,0 +1,36 @@
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 abd/delegate_misc.h
22 * @brief Delegate helper functions
23 */
24#ifndef DELEGATE_MISC_H
25#define DELEGATE_MISC_H
26
27#include "gnunet_abd_service.h"
28
29char *
30GNUNET_ABD_delegate_to_string (
31 const struct GNUNET_ABD_Delegate *cred);
32
33struct GNUNET_ABD_Delegate *
34GNUNET_ABD_delegate_from_string (const char *str);
35
36#endif
diff --git a/src/contrib/service/abd/gnunet-abd.c b/src/contrib/service/abd/gnunet-abd.c
new file mode 100644
index 000000000..1ed1adbe7
--- /dev/null
+++ b/src/contrib/service/abd/gnunet-abd.c
@@ -0,0 +1,1108 @@
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 gnunet-abd.c
22 * @brief command line tool to access command line Credential service
23 * @author Martin Schanzenbach
24 */
25#include "platform.h"
26#include <gnunet_util_lib.h>
27#include <gnunet_abd_service.h>
28#include <gnunet_gnsrecord_lib.h>
29#include <gnunet_namestore_service.h>
30#include "delegate_misc.h"
31#include "abd_serialization.h"
32
33/**
34 * Configuration we are using.
35 */
36static const struct GNUNET_CONFIGURATION_Handle *cfg;
37
38/**
39 * Handle to the namestore.
40 */
41static struct GNUNET_NAMESTORE_Handle *ns;
42
43/**
44 * Private key for the our zone.
45 */
46static struct GNUNET_CRYPTO_PrivateKey zone_pkey;
47
48/**
49 * EgoLookup
50 */
51static struct GNUNET_IDENTITY_EgoLookup *el;
52
53/**
54 * Handle to Credential service.
55 */
56static struct GNUNET_ABD_Handle *abd;
57
58/**
59 * Desired timeout for the lookup (default is no timeout).
60 */
61static struct GNUNET_TIME_Relative timeout;
62
63/**
64 * Handle to verify request
65 */
66static struct GNUNET_ABD_Request *verify_request;
67
68/**
69 * Handle to collect request
70 */
71static struct GNUNET_ABD_Request *collect_request;
72
73/**
74 * Task scheduled to handle timeout.
75 */
76static struct GNUNET_SCHEDULER_Task *tt;
77
78/**
79 * Return value of the commandline.
80 */
81static int ret = 0;
82
83/**
84 * Subject pubkey string
85 */
86static char *subject;
87
88/**
89 * Subject delegate string
90 */
91static char *subject_delegate;
92
93/**
94 * Credential TTL
95 */
96static char *expiration;
97
98/**
99 * Subject key
100 */
101struct GNUNET_CRYPTO_PublicKey subject_pkey;
102
103/**
104 * Issuer key
105 */
106struct GNUNET_CRYPTO_PublicKey issuer_pkey;
107
108
109/**
110 * Issuer pubkey string
111 */
112static char *issuer_key;
113
114/**
115 * ego
116 */
117static char *ego_name;
118
119/**
120 * Issuer attribute
121 */
122static char *issuer_attr;
123
124/**
125 * Verify mode
126 */
127static int verify;
128
129/**
130 * Collect mode
131 */
132static int collect;
133
134/**
135 * Create mode
136 */
137static int create_is;
138
139/**
140 * Create mode
141 */
142static int create_ss;
143
144/**
145 * Create mode
146 */
147static int sign_ss;
148
149/**
150 * Signed issue credentials
151 */
152static char *import;
153
154/**
155 * Is record private
156 */
157static int is_private;
158
159/**
160 * Search direction: forward
161 */
162static int forward;
163
164/**
165 * Search direction: backward
166 */
167static int backward;
168
169/**
170 * API enum, filled and passed for collect/verify
171 */
172enum GNUNET_ABD_AlgoDirectionFlags direction = 0;
173
174/**
175 * Queue entry for the 'add' operation.
176 */
177static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
178
179/**
180 * Value in binary format.
181 */
182static void *data;
183
184/**
185 * Number of bytes in #data.
186 */
187static size_t data_size;
188
189/**
190 * Type string converted to DNS type value.
191 */
192static uint32_t type;
193
194/**
195 * Type of the record to add/remove, NULL to remove all.
196 */
197static char *typestring;
198/**
199 * Expiration string converted to numeric value.
200 */
201static uint64_t etime;
202
203/**
204 * Is expiration time relative or absolute time?
205 */
206static int etime_is_rel = GNUNET_SYSERR;
207
208/**
209 * Record label for storing delegations
210 */
211static char *record_label;
212
213/**
214 * Task run on shutdown. Cleans up everything.
215 *
216 * @param cls unused
217 */
218static void
219do_shutdown (void *cls)
220{
221 if (NULL != verify_request)
222 {
223 GNUNET_ABD_request_cancel (verify_request);
224 verify_request = NULL;
225 }
226 if (NULL != abd)
227 {
228 GNUNET_ABD_disconnect (abd);
229 abd = NULL;
230 }
231 if (NULL != tt)
232 {
233 GNUNET_SCHEDULER_cancel (tt);
234 tt = NULL;
235 }
236 if (NULL != el)
237 {
238 GNUNET_IDENTITY_ego_lookup_cancel (el);
239 el = NULL;
240 }
241 if (NULL != add_qe)
242 {
243 GNUNET_NAMESTORE_cancel (add_qe);
244 add_qe = NULL;
245 }
246 if (NULL != ns)
247 {
248 GNUNET_NAMESTORE_disconnect (ns);
249 ns = NULL;
250 }
251}
252
253
254/**
255 * Task run on timeout. Triggers shutdown.
256 *
257 * @param cls unused
258 */
259static void
260do_timeout (void *cls)
261{
262 tt = NULL;
263 GNUNET_SCHEDULER_shutdown ();
264}
265
266
267static void
268handle_intermediate_result (void *cls,
269 struct GNUNET_ABD_Delegation *dd,
270 bool is_bw)
271{
272 char *prefix = "";
273 if (is_bw)
274 prefix = "Backward -";
275 else
276 prefix = "Forward -";
277
278 printf ("%s Intermediate result: %s.%s <- %s.%s\n",
279 prefix,
280 GNUNET_CRYPTO_public_key_to_string (&dd->issuer_key),
281 dd->issuer_attribute,
282 GNUNET_CRYPTO_public_key_to_string (&dd->subject_key),
283 dd->subject_attribute);
284}
285
286
287static void
288handle_collect_result (void *cls,
289 unsigned int d_count,
290 struct GNUNET_ABD_Delegation *dc,
291 unsigned int c_count,
292 struct GNUNET_ABD_Delegate *dele)
293{
294 int i;
295 char *line;
296
297 verify_request = NULL;
298 if (NULL != dele)
299 {
300 for (i = 0; i < c_count; i++)
301 {
302 line = GNUNET_ABD_delegate_to_string (&dele[i]);
303 printf ("%s\n", line);
304 GNUNET_free (line);
305 }
306 }
307 else
308 {
309 printf ("Received NULL\n");
310 }
311
312 GNUNET_SCHEDULER_shutdown ();
313}
314
315
316static void
317handle_verify_result (void *cls,
318 unsigned int d_count,
319 struct GNUNET_ABD_Delegation *dc,
320 unsigned int c_count,
321 struct GNUNET_ABD_Delegate *dele)
322{
323 int i;
324 char *iss_key;
325 char *sub_key;
326
327 verify_request = NULL;
328 if (NULL == dele)
329 ret = 1;
330 else
331 {
332 printf ("Delegation Chain:\n");
333 for (i = 0; i < d_count; i++)
334 {
335 iss_key = GNUNET_CRYPTO_public_key_to_string (&dc[i].issuer_key);
336 sub_key = GNUNET_CRYPTO_public_key_to_string (&dc[i].subject_key);
337
338 if (0 != dc[i].subject_attribute_len)
339 {
340 printf ("(%d) %s.%s <- %s.%s\n",
341 i,
342 iss_key,
343 dc[i].issuer_attribute,
344 sub_key,
345 dc[i].subject_attribute);
346 }
347 else
348 {
349 printf ("(%d) %s.%s <- %s\n",
350 i,
351 iss_key,
352 dc[i].issuer_attribute,
353 sub_key);
354 }
355 GNUNET_free (iss_key);
356 GNUNET_free (sub_key);
357 }
358 printf ("\nDelegate(s):\n");
359 for (i = 0; i < c_count; i++)
360 {
361 iss_key = GNUNET_CRYPTO_public_key_to_string (&dele[i].issuer_key);
362 sub_key = GNUNET_CRYPTO_public_key_to_string (&dele[i].subject_key);
363 printf ("%s.%s <- %s\n", iss_key, dele[i].issuer_attribute, sub_key);
364 GNUNET_free (iss_key);
365 GNUNET_free (sub_key);
366 }
367 printf ("Successful.\n");
368 }
369
370 GNUNET_SCHEDULER_shutdown ();
371}
372
373
374/**
375 * Callback invoked from identity service with ego information.
376 * An @a ego of NULL means the ego was not found.
377 *
378 * @param cls closure with the configuration
379 * @param ego an ego known to identity service, or NULL
380 */
381static void
382identity_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego)
383{
384 const struct GNUNET_CRYPTO_PrivateKey *privkey;
385
386 el = NULL;
387 if (NULL == ego)
388 {
389 if (NULL != ego_name)
390 {
391 fprintf (stderr,
392 _ ("Ego `%s' not known to identity service\n"),
393 ego_name);
394 }
395 GNUNET_SCHEDULER_shutdown ();
396 return;
397 }
398
399 if (GNUNET_YES == collect)
400 {
401
402 if (GNUNET_OK !=
403 GNUNET_CRYPTO_public_key_from_string (issuer_key,
404 &issuer_pkey))
405 {
406 fprintf (stderr,
407 _ ("Issuer public key `%s' is not well-formed\n"),
408 issuer_key);
409 GNUNET_SCHEDULER_shutdown ();
410 }
411 privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
412
413 collect_request = GNUNET_ABD_collect (abd,
414 &issuer_pkey,
415 issuer_attr,
416 privkey,
417 direction,
418 &handle_collect_result,
419 NULL,
420 &handle_intermediate_result,
421 NULL);
422 return;
423 }
424 GNUNET_SCHEDULER_shutdown ();
425}
426
427
428/**
429 * Parse expiration time.
430 *
431 * @param expirationstring text to parse
432 * @param[out] etime_is_rel set to #GNUNET_YES if time is relative
433 * @param[out] etime set to expiration time (abs or rel)
434 * @return #GNUNET_OK on success
435 */
436static int
437parse_expiration (const char *expirationstring,
438 int *etime_is_rel,
439 uint64_t *etime)
440{
441 // copied from namestore/gnunet-namestore.c
442 struct GNUNET_TIME_Relative etime_rel;
443 struct GNUNET_TIME_Absolute etime_abs;
444
445 if (0 == strcmp (expirationstring, "never"))
446 {
447 *etime = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
448 *etime_is_rel = GNUNET_NO;
449 return GNUNET_OK;
450 }
451 if (GNUNET_OK ==
452 GNUNET_STRINGS_fancy_time_to_relative (expirationstring, &etime_rel))
453 {
454 *etime_is_rel = GNUNET_YES;
455 *etime = etime_rel.rel_value_us;
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Storing record with relative expiration time of %s\n",
458 GNUNET_STRINGS_relative_time_to_string (etime_rel, GNUNET_NO));
459 return GNUNET_OK;
460 }
461 if (GNUNET_OK ==
462 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring, &etime_abs))
463 {
464 *etime_is_rel = GNUNET_NO;
465 *etime = etime_abs.abs_value_us;
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "Storing record with absolute expiration time of %s\n",
468 GNUNET_STRINGS_absolute_time_to_string (etime_abs));
469 return GNUNET_OK;
470 }
471 return GNUNET_SYSERR;
472}
473
474
475/**
476 * Function called if lookup fails.
477 */
478static void
479error_cb (void *cls)
480{
481 fprintf (stderr, "Error occurred during lookup, shutting down.\n");
482 GNUNET_SCHEDULER_shutdown ();
483 return;
484}
485
486
487static void
488add_continuation (void *cls, int32_t success, const char *emsg)
489{
490 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
491 *qe = NULL;
492
493 if (GNUNET_OK == success)
494 printf ("Adding successful.\n");
495 else
496 fprintf (stderr, "Error occurred during adding, shutting down.\n");
497
498 GNUNET_SCHEDULER_shutdown ();
499}
500
501
502static void
503get_existing_record (void *cls,
504 const struct GNUNET_CRYPTO_PrivateKey *zone_key,
505 const char *rec_name,
506 unsigned int rd_count,
507 const struct GNUNET_GNSRECORD_Data *rd)
508{
509 struct GNUNET_GNSRECORD_Data *rde;
510 struct GNUNET_GNSRECORD_Data*rdn =
511 GNUNET_malloc (sizeof(*rdn) * (rd_count + 1));
512
513 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
514 GNUNET_memcpy (&rdn[1], rd,
515 rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
516 rde = &rdn[0];
517 rde->data = data;
518 rde->data_size = data_size;
519 rde->record_type = type;
520
521 // Set flags
522 if (GNUNET_YES == is_private)
523 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
524 rde->expiration_time = etime;
525 if (GNUNET_YES == etime_is_rel)
526 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
527 else if (GNUNET_NO != etime_is_rel)
528 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
529
530 GNUNET_assert (NULL != rec_name);
531 add_qe = GNUNET_NAMESTORE_records_store (ns,
532 &zone_pkey,
533 rec_name,
534 rd_count + 1,
535 rde,
536 &add_continuation,
537 &add_qe);
538 GNUNET_free (rdn);
539 return;
540}
541
542
543static void
544store_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego)
545{
546 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
547 struct GNUNET_ABD_Delegate *cred;
548 struct GNUNET_CRYPTO_PublicKey zone_pubkey;
549 char *subject_pubkey_str;
550 char *zone_pubkey_str;
551
552 el = NULL;
553
554 ns = GNUNET_NAMESTORE_connect (cfg);
555 if (NULL == ns)
556 {
557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558 _ ("Failed to connect to namestore\n"));
559 GNUNET_SCHEDULER_shutdown ();
560 return;
561 }
562
563 if (NULL == ego)
564 {
565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
566 _("Ego does not exist!\n"));
567 GNUNET_SCHEDULER_shutdown ();
568 return;
569 }
570
571 // Key handling
572 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
573
574 if (GNUNET_GNSRECORD_TYPE_DELEGATE == type)
575 {
576 // Parse import
577 cred = GNUNET_ABD_delegate_from_string (import);
578
579 if (NULL == cred)
580 {
581 fprintf (stderr,
582 "%s is not a valid credential\n", import);
583 GNUNET_SCHEDULER_shutdown();
584 return;
585 }
586
587 // Get import subject public key string
588 subject_pubkey_str =
589 GNUNET_CRYPTO_public_key_to_string (&cred->subject_key);
590
591 // Get zone public key string
592 GNUNET_IDENTITY_ego_get_public_key (ego, &zone_pubkey);
593 zone_pubkey_str =
594 GNUNET_CRYPTO_public_key_to_string (&zone_pubkey);
595
596 // Check if the subject key in the signed import matches the zone's key it is issued to
597 if (strcmp (zone_pubkey_str, subject_pubkey_str) != 0)
598 {
599 fprintf (stderr,
600 "Import signed delegate does not match this ego's public key.\n");
601 GNUNET_free (cred);
602 GNUNET_SCHEDULER_shutdown ();
603 return;
604 }
605
606 // Expiration
607 etime = cred->expiration.abs_value_us;
608 etime_is_rel = GNUNET_NO;
609
610 // Prepare the data to be store in the record
611 data_size = GNUNET_ABD_delegate_serialize (cred, (char **) &data);
612 GNUNET_free (cred);
613 }
614 else
615 {
616 // For all other types e.g. GNUNET_GNSRECORD_TYPE_ATTRIBUTE
617 if (GNUNET_OK !=
618 GNUNET_GNSRECORD_string_to_value (type, subject, &data, &data_size))
619 {
620 if (typestring == NULL)
621 {
622 fputs ("Value for unknown record type not well-formed.\n", stderr);
623 }
624 else if (subject == NULL)
625 {
626 fprintf (stderr,
627 "Value for record type `%s' not well-formed.\n",
628 typestring);
629 }
630 else
631 {
632 fprintf (stderr,
633 "Value `%s' invalid for record type `%s'\n",
634 subject,
635 typestring);
636 }
637 GNUNET_SCHEDULER_shutdown ();
638 return;
639 }
640
641 // Take care of expiration
642 if (NULL == expiration)
643 {
644 fprintf (stderr, "Missing option -e for operation 'create'\n");
645 GNUNET_SCHEDULER_shutdown ();
646 return;
647 }
648 if (GNUNET_OK != parse_expiration (expiration, &etime_is_rel, &etime))
649 {
650 fprintf (stderr, "Invalid time format `%s'\n", expiration);
651 GNUNET_SCHEDULER_shutdown ();
652 return;
653 }
654 }
655
656 // Start lookup
657 add_qe = GNUNET_NAMESTORE_records_lookup (ns,
658 &zone_pkey,
659 record_label,
660 &error_cb,
661 NULL,
662 &get_existing_record,
663 NULL);
664 return;
665}
666
667
668static void
669sign_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego)
670{
671 const struct GNUNET_CRYPTO_PrivateKey *privkey;
672 struct GNUNET_ABD_Delegate *dele;
673 struct GNUNET_TIME_Absolute etime_abs;
674 char *res;
675
676 el = NULL;
677
678 // work on expiration time
679 if (NULL == expiration)
680 {
681 fprintf (stderr, "Please specify a TTL\n");
682 GNUNET_SCHEDULER_shutdown ();
683 return;
684 }
685 else if (GNUNET_OK !=
686 GNUNET_STRINGS_fancy_time_to_absolute (expiration, &etime_abs))
687 {
688 fprintf (stderr,
689 "%s is not a valid ttl! Only absolute times are accepted!\n",
690 expiration);
691 GNUNET_SCHEDULER_shutdown ();
692 return;
693 }
694
695 // If contains a space - split it by the first space only - assume first entry is subject followed by attribute(s)
696 char *subject_pubkey_str;
697 char *subject_attr = NULL;
698 char *token;
699
700 // Subject Public Key
701 token = strtok (subject, " ");
702 subject_pubkey_str = token;
703 // Subject Attribute(s)
704 token = strtok (NULL, " ");
705 if (NULL != token)
706 {
707 subject_attr = token;
708 }
709
710 // work on keys
711 privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
712
713 if (NULL == subject_pubkey_str)
714 {
715 fprintf (stderr,
716 "Subject pubkey not given\n");
717 GNUNET_SCHEDULER_shutdown ();
718 return;
719 }
720 if (GNUNET_OK !=
721 GNUNET_CRYPTO_public_key_from_string (subject_pubkey_str,
722 &subject_pkey))
723 {
724 fprintf (stderr,
725 "Subject public key `%s' is not well-formed\n",
726 subject_pubkey_str);
727 GNUNET_SCHEDULER_shutdown ();
728 return;
729 }
730
731 // Sign delegate
732 dele = GNUNET_ABD_delegate_issue (privkey,
733 &subject_pkey,
734 issuer_attr,
735 subject_attr,
736 &etime_abs);
737 res = GNUNET_ABD_delegate_to_string (dele);
738 GNUNET_free (dele);
739 printf ("%s\n", res);
740
741 GNUNET_free (ego_name);
742 ego_name = NULL;
743
744 GNUNET_SCHEDULER_shutdown ();
745}
746
747
748/**
749 * Main function that will be run.
750 *
751 * @param cls closure
752 * @param args remaining command-line arguments
753 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
754 * @param c configuration
755 */
756static void
757run (void *cls,
758 char *const *args,
759 const char *cfgfile,
760 const struct GNUNET_CONFIGURATION_Handle *c)
761{
762 cfg = c;
763
764 tt = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, NULL);
765 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
766
767 // Check relevant cmdline parameters
768 if (GNUNET_YES == create_is)
769 {
770 if (NULL == ego_name)
771 {
772 fprintf (stderr, "Missing option '-ego'\n");
773 GNUNET_SCHEDULER_shutdown ();
774 return;
775 }
776 if (NULL == issuer_attr)
777 {
778 fprintf (stderr, "Missing option '-attribute' for issuer attribute\n");
779 GNUNET_SCHEDULER_shutdown ();
780 return;
781 }
782 if (NULL == subject)
783 {
784 fprintf (stderr, "Missing option -subject for operation 'create'.'\n");
785 GNUNET_SCHEDULER_shutdown ();
786 return;
787 }
788
789 // Lookup ego, on success call store_cb and store as ATTRIBUTE type
790 type = GNUNET_GNSRECORD_TYPE_ATTRIBUTE;
791 record_label = issuer_attr;
792 el = GNUNET_IDENTITY_ego_lookup (cfg, ego_name, &store_cb, (void *) cfg);
793 return;
794 }
795
796 if (GNUNET_YES == create_ss)
797 {
798
799 // check if signed parameter has been passed in cmd line call
800 if (NULL == import)
801 {
802 fprintf (stderr, "'import' required\n");
803 GNUNET_SCHEDULER_shutdown ();
804 return;
805 }
806
807 type = GNUNET_GNSRECORD_TYPE_DELEGATE;
808 record_label = GNUNET_GNS_EMPTY_LABEL_AT;
809 // Store subject side
810 el = GNUNET_IDENTITY_ego_lookup (cfg, ego_name, &store_cb, (void *) cfg);
811
812 return;
813 }
814
815 if (GNUNET_YES == sign_ss)
816 {
817 if (NULL == ego_name)
818 {
819 fprintf (stderr, "ego required\n");
820 GNUNET_SCHEDULER_shutdown ();
821 return;
822 }
823 if (NULL == subject)
824 {
825 fprintf (stderr, "Subject public key needed\n");
826 GNUNET_SCHEDULER_shutdown ();
827 return;
828 }
829
830 // lookup ego and call function sign_cb on success
831 el = GNUNET_IDENTITY_ego_lookup (cfg, ego_name, &sign_cb, (void *) cfg);
832 return;
833 }
834
835 if ((GNUNET_NO == forward) && (GNUNET_NO == backward))
836 {
837 // set default: bidirectional
838 forward = GNUNET_YES;
839 backward = GNUNET_YES;
840 }
841 if (GNUNET_YES == forward)
842 direction |= GNUNET_ABD_FLAG_FORWARD;
843 if (GNUNET_YES == backward)
844 direction |= GNUNET_ABD_FLAG_BACKWARD;
845
846 if (GNUNET_YES == collect)
847 {
848 if (NULL == issuer_key)
849 {
850 fprintf (stderr, _ ("Issuer public key not well-formed\n"));
851 GNUNET_SCHEDULER_shutdown ();
852 return;
853 }
854
855 abd = GNUNET_ABD_connect (cfg);
856
857 if (NULL == abd)
858 {
859 fprintf (stderr, _ ("Failed to connect to ABD\n"));
860 GNUNET_SCHEDULER_shutdown ();
861 return;
862 }
863 if (NULL == issuer_attr)
864 {
865 fprintf (stderr, _ ("You must provide issuer the attribute\n"));
866 GNUNET_SCHEDULER_shutdown ();
867 return;
868 }
869
870 if (NULL == ego_name)
871 {
872 fprintf (stderr, _ ("ego required\n"));
873 GNUNET_SCHEDULER_shutdown ();
874 return;
875 }
876 el = GNUNET_IDENTITY_ego_lookup (cfg, ego_name, &identity_cb, (void *) cfg);
877 return;
878 }
879
880 if (NULL == subject)
881 {
882 fprintf (stderr, _ ("Subject public key needed\n"));
883 GNUNET_SCHEDULER_shutdown ();
884 return;
885 }
886 if (GNUNET_OK != GNUNET_CRYPTO_public_key_from_string (subject,
887 &subject_pkey))
888 {
889 fprintf (stderr,
890 _ ("Subject public key `%s' is not well-formed\n"),
891 subject);
892 GNUNET_SCHEDULER_shutdown ();
893 return;
894 }
895
896 if (GNUNET_YES == verify)
897 {
898 if (NULL == issuer_key)
899 {
900 fprintf (stderr, _ ("Issuer public key not well-formed\n"));
901 GNUNET_SCHEDULER_shutdown ();
902 return;
903 }
904 if (GNUNET_OK !=
905 GNUNET_CRYPTO_public_key_from_string (issuer_key,
906 &issuer_pkey))
907 {
908 fprintf (stderr,
909 _ ("Issuer public key `%s' is not well-formed\n"),
910 issuer_key);
911 GNUNET_SCHEDULER_shutdown ();
912 return;
913 }
914 abd = GNUNET_ABD_connect (cfg);
915
916 if (NULL == abd)
917 {
918 fprintf (stderr, _ ("Failed to connect to ABD\n"));
919 GNUNET_SCHEDULER_shutdown ();
920 return;
921 }
922 if ((NULL == issuer_attr) || (NULL == subject_delegate))
923 {
924 fprintf (stderr, _ ("You must provide issuer and subject attributes\n"));
925 GNUNET_SCHEDULER_shutdown ();
926 return;
927 }
928
929 // Subject credentials are comma separated
930 char *tmp = GNUNET_strdup (subject_delegate);
931 char *tok = strtok (tmp, ",");
932 if (NULL == tok)
933 {
934 fprintf (stderr, "Invalid subject credentials\n");
935 GNUNET_free (tmp);
936 GNUNET_SCHEDULER_shutdown ();
937 return;
938 }
939 int count = 1;
940 int i;
941 while (NULL != (tok = strtok (NULL, ",")))
942 count++;
943 struct GNUNET_ABD_Delegate*delegates =
944 GNUNET_malloc (sizeof(*delegates) * count);
945 struct GNUNET_ABD_Delegate *dele;
946 GNUNET_free (tmp);
947 tmp = GNUNET_strdup (subject_delegate);
948 tok = strtok (tmp, ",");
949 for (i = 0; i < count; i++)
950 {
951 dele = GNUNET_ABD_delegate_from_string (tok);
952 GNUNET_memcpy (&delegates[i],
953 dele,
954 sizeof (struct GNUNET_ABD_Delegate));
955 delegates[i].issuer_attribute = GNUNET_strdup (dele->issuer_attribute);
956 tok = strtok (NULL, ",");
957 GNUNET_free (dele);
958 }
959
960 verify_request = GNUNET_ABD_verify (abd,
961 &issuer_pkey,
962 issuer_attr,
963 &subject_pkey,
964 count,
965 delegates,
966 direction,
967 &handle_verify_result,
968 NULL,
969 &handle_intermediate_result,
970 NULL);
971 for (i = 0; i < count; i++)
972 {
973 GNUNET_free_nz ((char *) delegates[i].issuer_attribute);
974 delegates[i].issuer_attribute = NULL;
975 }
976 GNUNET_free (tmp);
977 GNUNET_free (delegates);
978 }
979 else
980 {
981 fprintf (stderr,
982 _ (
983 "Please specify name to lookup, subject key and issuer key!\n"));
984 GNUNET_SCHEDULER_shutdown ();
985 }
986 return;
987}
988
989
990/**
991 * The main function for gnunet-gns.
992 *
993 * @param argc number of arguments from the command line
994 * @param argv command line arguments
995 * @return 0 ok, 1 on error
996 */
997int
998main (int argc, char *const *argv)
999{
1000 struct GNUNET_GETOPT_CommandLineOption options[] =
1001 {GNUNET_GETOPT_option_flag ('V',
1002 "verify",
1003 gettext_noop (
1004 "verify credential against attribute"),
1005 &verify),
1006 GNUNET_GETOPT_option_string (
1007 's',
1008 "subject",
1009 "PKEY",
1010 gettext_noop (
1011 "The public key of the subject to lookup the"
1012 "credential for, or for issuer side storage: subject and its attributes"),
1013 &subject),
1014 GNUNET_GETOPT_option_string (
1015 'd',
1016 "delegate",
1017 "DELE",
1018 gettext_noop ("The private, signed delegate presented by the subject"),
1019 &subject_delegate),
1020 GNUNET_GETOPT_option_string (
1021 'i',
1022 "issuer",
1023 "PKEY",
1024 gettext_noop (
1025 "The public key of the authority to verify the credential against"),
1026 &issuer_key),
1027 GNUNET_GETOPT_option_string ('e',
1028 "ego",
1029 "EGO",
1030 gettext_noop ("The ego/zone name to use"),
1031 &ego_name),
1032 GNUNET_GETOPT_option_string (
1033 'a',
1034 "attribute",
1035 "ATTR",
1036 gettext_noop ("The issuer attribute to verify against or to issue"),
1037 &issuer_attr),
1038 GNUNET_GETOPT_option_string ('T',
1039 "ttl",
1040 "EXP",
1041 gettext_noop (
1042 "The time to live for the credential."
1043 "e.g. 5m, 6h, \"1990-12-30 12:00:00\""),
1044 &expiration),
1045 GNUNET_GETOPT_option_flag ('g',
1046 "collect",
1047 gettext_noop ("collect credentials"),
1048 &collect),
1049 GNUNET_GETOPT_option_flag ('U',
1050 "createIssuerSide",
1051 gettext_noop (
1052 "Create and issue a credential issuer side."),
1053 &create_is),
1054 GNUNET_GETOPT_option_flag ('C',
1055 "createSubjectSide",
1056 gettext_noop (
1057 "Issue a credential subject side."),
1058 &create_ss),
1059 GNUNET_GETOPT_option_flag (
1060 'S',
1061 "signSubjectSide",
1062 gettext_noop ("Create, sign and return a credential subject side."),
1063 &sign_ss),
1064 GNUNET_GETOPT_option_string (
1065 'x',
1066 "import",
1067 "IMP",
1068 gettext_noop (
1069 "Import signed credentials that should be issued to a zone/ego"),
1070 &import),
1071 GNUNET_GETOPT_option_flag ('P',
1072 "private",
1073 gettext_noop ("Create private record entry."),
1074 &is_private),
1075 GNUNET_GETOPT_option_flag (
1076 'F',
1077 "forward",
1078 gettext_noop (
1079 "Indicates that the collect/verify process is done via forward search."),
1080 &forward),
1081 GNUNET_GETOPT_option_flag (
1082 'B',
1083 "backward",
1084 gettext_noop (
1085 "Indicates that the collect/verify process is done via forward search."),
1086 &backward),
1087 GNUNET_GETOPT_OPTION_END};
1088
1089
1090 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1091 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1092 return 2;
1093
1094 GNUNET_log_setup ("gnunet-abd", "WARNING", NULL);
1095 if (GNUNET_OK != GNUNET_PROGRAM_run (argc,
1096 argv,
1097 "gnunet-abd",
1098 _ ("GNUnet abd resolver tool"),
1099 options,
1100 &run,
1101 NULL))
1102 ret = 1;
1103 GNUNET_free_nz ((void *) argv);
1104 return ret;
1105}
1106
1107
1108/* end of gnunet-abd.c */
diff --git a/src/contrib/service/abd/gnunet-service-abd.c b/src/contrib/service/abd/gnunet-service-abd.c
new file mode 100644
index 000000000..5cbc105f6
--- /dev/null
+++ b/src/contrib/service/abd/gnunet-service-abd.c
@@ -0,0 +1,1778 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2011-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 abd/gnunet-service-abd.c
22 * @brief GNUnet Credential Service (main service)
23 * @author Martin Schanzenbach
24 */
25#include "platform.h"
26
27#include "gnunet_util_lib.h"
28
29#include "abd.h"
30#include "abd_serialization.h"
31#include "gnunet_abd_service.h"
32#include "gnunet_protocols.h"
33#include "gnunet_signatures.h"
34#include "gnunet_statistics_service.h"
35#include <gnunet_gns_service.h>
36#include <gnunet_gnsrecord_lib.h>
37#include <gnunet_identity_service.h>
38#include <gnunet_namestore_service.h>
39
40
41#define GNUNET_ABD_MAX_LENGTH 255
42
43struct VerifyRequestHandle;
44
45struct DelegationSetQueueEntry;
46
47
48struct DelegationChainEntry
49{
50 /**
51 * DLL
52 */
53 struct DelegationChainEntry *next;
54
55 /**
56 * DLL
57 */
58 struct DelegationChainEntry *prev;
59
60 /**
61 * The issuer
62 */
63 struct GNUNET_CRYPTO_PublicKey issuer_key;
64
65 /**
66 * The subject
67 */
68 struct GNUNET_CRYPTO_PublicKey subject_key;
69
70 /**
71 * The issued attribute
72 */
73 char *issuer_attribute;
74
75 /**
76 * The delegated attribute
77 */
78 char *subject_attribute;
79};
80
81/**
82 * DLL for record
83 */
84struct DelegateRecordEntry
85{
86 /**
87 * DLL
88 */
89 struct DelegateRecordEntry *next;
90
91 /**
92 * DLL
93 */
94 struct DelegateRecordEntry *prev;
95
96 /**
97 * Number of references in delegation chains
98 */
99 uint32_t refcount;
100
101 /**
102 * Payload
103 */
104 struct GNUNET_ABD_Delegate *delegate;
105};
106
107/**
108 * DLL used for delegations
109 * Used for OR delegations
110 */
111struct DelegationQueueEntry
112{
113 /**
114 * DLL
115 */
116 struct DelegationQueueEntry *next;
117
118 /**
119 * DLL
120 */
121 struct DelegationQueueEntry *prev;
122
123 /**
124 * Parent set
125 */
126 struct DelegationSetQueueEntry *parent_set;
127
128 /**
129 * Required solutions
130 */
131 uint32_t required_solutions;
132};
133
134/**
135 * DLL for delegation sets
136 * Used for AND delegation set
137 */
138struct DelegationSetQueueEntry
139{
140 /**
141 * DLL
142 */
143 struct DelegationSetQueueEntry *next;
144
145 /**
146 * DLL
147 */
148 struct DelegationSetQueueEntry *prev;
149
150 /**
151 * GNS handle
152 */
153 struct GNUNET_GNS_LookupRequest *lookup_request;
154
155 /**
156 * Verify handle
157 */
158 struct VerifyRequestHandle *handle;
159
160 /**
161 * Parent attribute delegation
162 */
163 struct DelegationQueueEntry *parent;
164
165 /**
166 * Issuer key
167 */
168 struct GNUNET_CRYPTO_PublicKey *issuer_key;
169
170 /**
171 * Queue entries of this set
172 */
173 struct DelegationQueueEntry *queue_entries_head;
174
175 /**
176 * Queue entries of this set
177 */
178 struct DelegationQueueEntry *queue_entries_tail;
179
180 /**
181 * Parent QueueEntry
182 */
183 struct DelegationQueueEntry *parent_queue_entry;
184
185 /**
186 * Issuer attribute delegated to
187 */
188 char *issuer_attribute;
189
190 /**
191 * The current attribute to look up
192 */
193 char *lookup_attribute;
194
195 /**
196 * Trailing attribute context
197 */
198 char *attr_trailer;
199
200 /**
201 * Still to resolve delegation as string
202 */
203 char *unresolved_attribute_delegation;
204
205 /**
206 * The delegation chain entry
207 */
208 struct DelegationChainEntry *delegation_chain_entry;
209
210 /**
211 * True if added by backward resolution
212 */
213 bool from_bw;
214};
215
216
217/**
218 * Handle to a lookup operation from api
219 */
220struct VerifyRequestHandle
221{
222 /**
223 * True if created by a collect request.
224 */
225 bool is_collect;
226 /**
227 * We keep these in a DLL.
228 */
229 struct VerifyRequestHandle *next;
230
231 /**
232 * We keep these in a DLL.
233 */
234 struct VerifyRequestHandle *prev;
235
236 /**
237 * Handle to the requesting client
238 */
239 struct GNUNET_SERVICE_Client *client;
240
241 /**
242 * Size of delegation tree
243 */
244 uint32_t delegation_chain_size;
245
246 /**
247 * Children of this attribute
248 */
249 struct DelegationChainEntry *delegation_chain_head;
250
251 /**
252 * Children of this attribute
253 */
254 struct DelegationChainEntry *delegation_chain_tail;
255
256 /**
257 * List for bidirectional matching
258 */
259 struct DelegationSetQueueEntry *dsq_head;
260
261 /**
262 * List for bidirectional matching
263 */
264 struct DelegationSetQueueEntry *dsq_tail;
265
266 /**
267 * Issuer public key
268 */
269 struct GNUNET_CRYPTO_PublicKey issuer_key;
270
271 /**
272 * Issuer attribute
273 */
274 char *issuer_attribute;
275
276 /**
277 * Subject public key
278 */
279 struct GNUNET_CRYPTO_PublicKey subject_key;
280
281 /**
282 * Delegate DLL
283 */
284 struct DelegateRecordEntry *del_chain_head;
285
286 /**
287 * Delegate DLL
288 */
289 struct DelegateRecordEntry *del_chain_tail;
290
291 /**
292 * Delegate DLL size
293 */
294 uint32_t del_chain_size;
295
296 /**
297 * Current Delegation Pointer
298 */
299 struct DelegationQueueEntry *current_delegation;
300
301 /**
302 * request id
303 */
304 uint32_t request_id;
305
306 /**
307 * Pending lookups
308 */
309 uint64_t pending_lookups;
310
311 /**
312 * Direction of the resolution algo
313 */
314 enum GNUNET_ABD_AlgoDirectionFlags resolution_algo;
315
316 /**
317 * Delegate iterator for lookup
318 */
319 struct GNUNET_NAMESTORE_QueueEntry *dele_qe;
320};
321
322
323/**
324 * Head of the DLL.
325 */
326static struct VerifyRequestHandle *vrh_head = NULL;
327
328/**
329 * Tail of the DLL.
330 */
331static struct VerifyRequestHandle *vrh_tail = NULL;
332
333/**
334 * Handle to the statistics service
335 */
336static struct GNUNET_STATISTICS_Handle *statistics;
337
338/**
339 * Handle to GNS service.
340 */
341static struct GNUNET_GNS_Handle *gns;
342
343/**
344 * Handle to namestore service
345 */
346static struct GNUNET_NAMESTORE_Handle *namestore;
347
348static void
349print_deleset (struct DelegationSetQueueEntry *dsentry, char *text)
350{
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "%s %s.%s <- %s.%s\n",
353 text,
354 GNUNET_CRYPTO_public_key_to_string (
355 &dsentry->delegation_chain_entry->issuer_key),
356 dsentry->delegation_chain_entry->issuer_attribute,
357 GNUNET_CRYPTO_public_key_to_string (
358 &dsentry->delegation_chain_entry->subject_key),
359 dsentry->delegation_chain_entry->subject_attribute);
360}
361
362
363static void
364cleanup_dsq_entry (struct DelegationSetQueueEntry *ds_entry)
365{
366 GNUNET_free (ds_entry->issuer_key);
367 GNUNET_free (ds_entry->issuer_attribute);
368 GNUNET_free (ds_entry->attr_trailer);
369 // those fields are only set/used in bw search
370 if (ds_entry->from_bw)
371 {
372 GNUNET_free (ds_entry->lookup_attribute);
373 GNUNET_free (ds_entry->unresolved_attribute_delegation);
374 }
375 if (NULL != ds_entry->lookup_request)
376 {
377 GNUNET_GNS_lookup_cancel (ds_entry->lookup_request);
378 ds_entry->lookup_request = NULL;
379 }
380 if (NULL != ds_entry->delegation_chain_entry)
381 {
382 GNUNET_free (
383 ds_entry->delegation_chain_entry->subject_attribute);
384 GNUNET_free (ds_entry->delegation_chain_entry->issuer_attribute);
385 GNUNET_free (ds_entry->delegation_chain_entry);
386 }
387 // Free DQ entries
388 for (struct DelegationQueueEntry *dq_entry = ds_entry->queue_entries_head;
389 NULL != ds_entry->queue_entries_head;
390 dq_entry = ds_entry->queue_entries_head)
391 {
392 GNUNET_CONTAINER_DLL_remove (ds_entry->queue_entries_head,
393 ds_entry->queue_entries_tail,
394 dq_entry);
395 GNUNET_free (dq_entry);
396 }
397 GNUNET_free (ds_entry);
398}
399
400
401static void
402cleanup_handle (struct VerifyRequestHandle *vrh)
403{
404 struct DelegateRecordEntry *del_entry;
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up...\n");
406
407 if (NULL != vrh->dsq_head)
408 {
409 for (struct DelegationSetQueueEntry *ds_entry = vrh->dsq_head; NULL !=
410 vrh->dsq_head;
411 ds_entry = vrh->dsq_head)
412 {
413 GNUNET_CONTAINER_DLL_remove (vrh->dsq_head, vrh->dsq_tail, ds_entry);
414 cleanup_dsq_entry (ds_entry);
415 }
416 }
417 if (NULL != vrh->del_chain_head)
418 {
419 for (del_entry = vrh->del_chain_head; NULL != vrh->del_chain_head;
420 del_entry = vrh->del_chain_head)
421 {
422 GNUNET_CONTAINER_DLL_remove (vrh->del_chain_head,
423 vrh->del_chain_tail,
424 del_entry);
425 GNUNET_free (del_entry->delegate);
426 GNUNET_free (del_entry);
427 }
428 }
429 GNUNET_free (vrh->issuer_attribute);
430 GNUNET_free (vrh);
431}
432
433
434static void
435shutdown_task (void *cls)
436{
437 struct VerifyRequestHandle *vrh;
438
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down!\n");
440
441 while (NULL != (vrh = vrh_head))
442 {
443 // ABD_resolver_lookup_cancel (clh->lookup);
444 GNUNET_CONTAINER_DLL_remove (vrh_head, vrh_tail, vrh);
445 cleanup_handle (vrh);
446 }
447
448 if (NULL != gns)
449 {
450 GNUNET_GNS_disconnect (gns);
451 gns = NULL;
452 }
453 if (NULL != namestore)
454 {
455 GNUNET_NAMESTORE_disconnect (namestore);
456 namestore = NULL;
457 }
458 if (NULL != statistics)
459 {
460 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
461 statistics = NULL;
462 }
463}
464
465
466static void
467send_intermediate_response (struct VerifyRequestHandle *vrh, struct
468 DelegationChainEntry *ch_entry, bool is_bw)
469{
470 struct DelegationChainIntermediateMessage *rmsg;
471 struct GNUNET_MQ_Envelope *env;
472 struct GNUNET_ABD_Delegation *dd;
473 size_t size;
474
475 // Don't report immediate results during collect
476 if (vrh->is_collect)
477 return;
478
479 dd = GNUNET_new (struct GNUNET_ABD_Delegation);
480 dd->issuer_key = ch_entry->issuer_key;
481 dd->subject_key = ch_entry->subject_key;
482 dd->issuer_attribute = ch_entry->issuer_attribute;
483 dd->issuer_attribute_len = strlen (ch_entry->issuer_attribute) + 1;
484 dd->subject_attribute_len = 0;
485 dd->subject_attribute = NULL;
486 if (NULL != ch_entry->subject_attribute)
487 {
488 dd->subject_attribute = ch_entry->subject_attribute;
489 dd->subject_attribute_len = strlen (ch_entry->subject_attribute) + 1;
490 }
491
492
493 size = GNUNET_ABD_delegation_chain_get_size (1,
494 dd,
495 0,
496 NULL);
497
498 env = GNUNET_MQ_msg_extra (rmsg,
499 size,
500 GNUNET_MESSAGE_TYPE_ABD_INTERMEDIATE_RESULT);
501 // Assign id so that client can find associated request
502 rmsg->id = vrh->request_id;
503 rmsg->is_bw = htons (is_bw);
504 rmsg->size = htonl (size);
505
506 GNUNET_assert (
507 -1 != GNUNET_ABD_delegation_chain_serialize (1,
508 dd,
509 0,
510 NULL,
511 size,
512 (char *) &rmsg[1]));
513 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (vrh->client), env);
514
515 GNUNET_free (dd);
516}
517
518
519static void
520send_lookup_response (struct VerifyRequestHandle *vrh)
521{
522 struct GNUNET_MQ_Envelope *env;
523 struct DelegationChainResultMessage *rmsg;
524 struct DelegationChainEntry *dce;
525 struct GNUNET_ABD_Delegation dd[vrh->delegation_chain_size];
526 struct GNUNET_ABD_Delegate dele[vrh->del_chain_size];
527 struct DelegateRecordEntry *del;
528 struct DelegateRecordEntry *tmp;
529 size_t size;
530
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending response\n");
532 dce = vrh->delegation_chain_head;
533 for (uint32_t i = 0; i < vrh->delegation_chain_size; i++)
534 {
535 dd[i].issuer_key = dce->issuer_key;
536 dd[i].subject_key = dce->subject_key;
537 dd[i].issuer_attribute = dce->issuer_attribute;
538 dd[i].issuer_attribute_len = strlen (dce->issuer_attribute) + 1;
539 dd[i].subject_attribute_len = 0;
540 dd[i].subject_attribute = NULL;
541 if (NULL != dce->subject_attribute)
542 {
543 dd[i].subject_attribute = dce->subject_attribute;
544 dd[i].subject_attribute_len = strlen (dce->subject_attribute) + 1;
545 }
546 dce = dce->next;
547 }
548
549 // Remove all not needed credentials
550 for (del = vrh->del_chain_head; NULL != del;)
551 {
552 if (del->refcount > 0)
553 {
554 del = del->next;
555 continue;
556 }
557 tmp = del;
558 del = del->next;
559 GNUNET_CONTAINER_DLL_remove (vrh->del_chain_head, vrh->del_chain_tail, tmp);
560 GNUNET_free (tmp->delegate);
561 GNUNET_free (tmp);
562 vrh->del_chain_size--;
563 }
564
565 // Get serialized record data
566 // Append at the end of rmsg
567 del = vrh->del_chain_head;
568 for (uint32_t i = 0; i < vrh->del_chain_size; i++)
569 {
570 dele[i].issuer_key = del->delegate->issuer_key;
571 dele[i].subject_key = del->delegate->subject_key;
572 dele[i].issuer_attribute_len = strlen (del->delegate->issuer_attribute) + 1;
573 dele[i].issuer_attribute = del->delegate->issuer_attribute;
574 dele[i].subject_attribute_len = del->delegate->subject_attribute_len;
575 dele[i].subject_attribute = del->delegate->subject_attribute;
576 dele[i].expiration = del->delegate->expiration;
577 dele[i].signature = del->delegate->signature;
578 del = del->next;
579 }
580 size =
581 GNUNET_ABD_delegation_chain_get_size (vrh->delegation_chain_size,
582 dd,
583 vrh->del_chain_size,
584 dele);
585 env = GNUNET_MQ_msg_extra (rmsg,
586 size,
587 GNUNET_MESSAGE_TYPE_ABD_VERIFY_RESULT);
588 // Assign id so that client can find associated request
589 rmsg->id = vrh->request_id;
590 rmsg->d_count = htonl (vrh->delegation_chain_size);
591 rmsg->c_count = htonl (vrh->del_chain_size);
592
593 if (0 < vrh->del_chain_size)
594 rmsg->del_found = htonl (GNUNET_YES);
595 else
596 rmsg->del_found = htonl (GNUNET_NO);
597
598 GNUNET_assert (
599 -1 !=
600 GNUNET_ABD_delegation_chain_serialize (vrh->delegation_chain_size,
601 dd,
602 vrh->del_chain_size,
603 dele,
604 size,
605 (char *) &rmsg[1]));
606
607 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (vrh->client), env);
608 GNUNET_CONTAINER_DLL_remove (vrh_head, vrh_tail, vrh);
609 cleanup_handle (vrh);
610 GNUNET_STATISTICS_update (statistics,
611 "Completed verifications",
612 1,
613 GNUNET_NO);
614}
615
616
617static char *
618partial_match (char *tmp_trail,
619 char *tmp_subattr,
620 char *parent_trail,
621 char *issuer_attribute)
622{
623 char *saveptr1, *saveptr2;
624 char *trail_token;
625 char *sub_token;
626 char *attr_trailer;
627
628 // tok both, parent->attr_trailer and del->sub_attr to see how far they match,
629 // take rest of parent trailer (only when del->sub_attr token is null), and
630 // create new/actual trailer with del->iss_attr
631 trail_token = strtok_r (tmp_trail, ".", &saveptr1);
632 sub_token = strtok_r (tmp_subattr, ".", &saveptr2);
633 while (NULL != trail_token && NULL != sub_token)
634 {
635 if (0 == strcmp (trail_token, sub_token))
636 {
637 // good, matches, remove
638 }
639 else
640 {
641 // not relevant for solving the chain, end for iteration here
642 return NULL;
643 }
644
645 trail_token = strtok_r (NULL, ".", &saveptr1);
646 sub_token = strtok_r (NULL, ".", &saveptr2);
647 }
648 // skip this entry and go to next for if:
649 // 1. at some point the attr of the trailer and the subject dont match
650 // 2. the trailer is NULL, but the subject has more attributes
651 // Reason: This will lead to "startzone.attribute" but we're looking for a solution
652 // for "<- startzone"
653 if (NULL == trail_token)
654 {
655 return NULL;
656 }
657
658 // do not have to check sub_token == NULL, if both would be NULL
659 // at the same time, the complete match part above should have triggered already
660
661 // otherwise, above while only ends when sub_token == NULL
662 GNUNET_asprintf (&attr_trailer, "%s", trail_token);
663 trail_token = strtok_r (NULL, ".", &saveptr1);
664 while (NULL != trail_token)
665 {
666 GNUNET_asprintf (&attr_trailer, "%s.%s", parent_trail, trail_token);
667 trail_token = strtok_r (NULL, ".", &saveptr1);
668 }
669 GNUNET_asprintf (&attr_trailer, "%s.%s", issuer_attribute, attr_trailer);
670 return attr_trailer;
671}
672
673
674static int
675handle_bidirectional_match (struct DelegationSetQueueEntry *actual_entry,
676 struct DelegationSetQueueEntry *match_entry,
677 struct VerifyRequestHandle *vrh)
678{
679 struct DelegationSetQueueEntry *old_fw_parent;
680 struct DelegationSetQueueEntry *fw_entry = actual_entry;
681 struct DelegationSetQueueEntry *last_entry = match_entry;
682 // parent fixing, combine backward and forward chain parts
683 while (NULL != fw_entry->parent_queue_entry)
684 {
685 old_fw_parent = fw_entry->parent_queue_entry->parent_set;
686 // set parent
687 fw_entry->parent_queue_entry->parent_set = last_entry;
688
689 last_entry = fw_entry;
690 fw_entry = old_fw_parent;
691 }
692 // set last entry of chain as actual_entry
693 // actual_entry = last_entry;
694 // set refcount, loop all delegations
695 for (struct DelegateRecordEntry *del_entry = vrh->del_chain_head;
696 del_entry != NULL;
697 del_entry = del_entry->next)
698 {
699 if (0 != memcmp (&last_entry->delegation_chain_entry->subject_key,
700 &del_entry->delegate->issuer_key,
701 sizeof (struct GNUNET_CRYPTO_PublicKey)))
702 continue;
703 if (0 != strcmp (last_entry->delegation_chain_entry->subject_attribute,
704 del_entry->delegate->issuer_attribute))
705 continue;
706
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found delegate.\n");
708 // increase refcount of the start delegation
709 del_entry->refcount++;
710 }
711 // backtrack
712 for (struct DelegationSetQueueEntry *tmp_set = last_entry;
713 NULL != tmp_set->parent_queue_entry;
714 tmp_set = tmp_set->parent_queue_entry->parent_set)
715 {
716 tmp_set->parent_queue_entry->required_solutions--;
717
718 // add new found entry to vrh
719 vrh->delegation_chain_size++;
720 GNUNET_CONTAINER_DLL_insert (vrh->delegation_chain_head,
721 vrh->delegation_chain_tail,
722 tmp_set->delegation_chain_entry);
723
724 // if one node on the path still needs solutions, this current
725 // patch cannot fulfill the conditions and therefore stops here
726 // however, it is in the vrh and can be used by the other paths
727 // related to this path/collection/verification
728 if (0 < tmp_set->parent_queue_entry->required_solutions)
729 {
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Chain requires more solutions, waiting...\n");
732 return GNUNET_NO;
733 }
734 }
735 return GNUNET_YES;
736}
737
738
739static void
740forward_resolution (void *cls,
741 uint32_t rd_count,
742 const struct GNUNET_GNSRECORD_Data *rd)
743{
744 struct VerifyRequestHandle *vrh;
745 struct DelegationSetQueueEntry *current_set;
746 struct DelegationSetQueueEntry *ds_entry;
747 struct DelegationQueueEntry *dq_entry;
748 struct GNUNET_ABD_Delegate *del;
749
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %d entries.\n", rd_count);
751
752 current_set = cls;
753 // set handle to NULL (as el = NULL)
754 current_set->lookup_request = NULL;
755 vrh = current_set->handle;
756 vrh->pending_lookups--;
757
758 // Loop record entries
759 for (uint32_t i = 0; i < rd_count; i++)
760 {
761 if (GNUNET_GNSRECORD_TYPE_DELEGATE != rd[i].record_type)
762 continue;
763
764 // Start deserialize into Delegate
765 del = GNUNET_ABD_delegate_deserialize (rd[i].data, rd[i].data_size);
766
767 if (NULL == del)
768 continue;
769
770 // Start: Create DQ Entry
771 dq_entry = GNUNET_new (struct DelegationQueueEntry);
772 // AND delegations are not possible, only 1 solution
773 dq_entry->required_solutions = 1;
774 dq_entry->parent_set = current_set;
775
776 // Insert it into the current set
777 GNUNET_CONTAINER_DLL_insert (current_set->queue_entries_head,
778 current_set->queue_entries_tail,
779 dq_entry);
780
781 // Start: Create DS Entry
782 ds_entry = GNUNET_new (struct DelegationSetQueueEntry);
783 GNUNET_CONTAINER_DLL_insert (vrh->dsq_head, vrh->dsq_tail, ds_entry);
784 ds_entry->from_bw = false;
785
786 // (1) A.a <- A.b.c
787 // (2) A.b <- D.d
788 // (3) D.d <- E
789 // (4) E.c <- F.c
790 // (5) F.c <- G
791 // Possibilities:
792 // 1. complete match: trailer = 0, validate
793 // 2. partial match: replace
794 // 3. new solution: replace, add trailer
795
796 // At resolution chain start trailer of parent is NULL
797 if (NULL == current_set->attr_trailer)
798 {
799 // for (5) F.c <- G, remember .c when going upwards
800 ds_entry->attr_trailer = GNUNET_strdup (del->issuer_attribute);
801 }
802 else
803 {
804 if (0 == del->subject_attribute_len)
805 {
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found: New solution\n");
807 // new solution
808 // create new trailer del->issuer_attribute, ds_entry->attr_trailer
809 GNUNET_asprintf (&ds_entry->attr_trailer,
810 "%s.%s",
811 del->issuer_attribute,
812 current_set->attr_trailer);
813 }
814 else if (0 == strcmp (del->subject_attribute, current_set->attr_trailer))
815 {
816 // complete match
817 // new trailer == issuer attribute (e.g. (5) to (4))
818 ds_entry->attr_trailer = GNUNET_strdup (del->issuer_attribute);
819 }
820 else
821 {
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found: Partial match\n");
823 // partial match
824
825 char *trail = partial_match (GNUNET_strdup (current_set->attr_trailer),
826 GNUNET_strdup (del->subject_attribute),
827 current_set->attr_trailer,
828 GNUNET_strdup (del->issuer_attribute));
829
830 // if null: skip this record entry (reasons: mismatch or overmatch, both not relevant)
831 if (NULL == trail)
832 {
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "Entry not relevant, discarding: %s.%s <- %s.%s\n",
835 GNUNET_CRYPTO_public_key_to_string (
836 &del->issuer_key),
837 del->issuer_attribute,
838 GNUNET_CRYPTO_public_key_to_string (
839 &del->subject_key),
840 del->subject_attribute);
841 GNUNET_free (del);
842 continue;
843 }
844 else
845 ds_entry->attr_trailer = trail;
846 }
847 }
848
849
850 // Start: Credential Chain Entry
851 // issuer key is subject key, who needs to be contacted to resolve this (forward, therefore subject)
852 ds_entry->issuer_key = GNUNET_new (struct GNUNET_CRYPTO_PublicKey);
853 GNUNET_memcpy (ds_entry->issuer_key,
854 &del->subject_key,
855 sizeof (struct GNUNET_CRYPTO_PublicKey));
856
857 ds_entry->delegation_chain_entry = GNUNET_new (struct DelegationChainEntry);
858 ds_entry->delegation_chain_entry->subject_key = del->subject_key;
859 if (0 < del->subject_attribute_len)
860 ds_entry->delegation_chain_entry->subject_attribute =
861 GNUNET_strdup (del->subject_attribute);
862 ds_entry->delegation_chain_entry->issuer_key = del->issuer_key;
863 ds_entry->delegation_chain_entry->issuer_attribute =
864 GNUNET_strdup (del->issuer_attribute);
865
866 // Found new entry, repoting intermediate result
867 send_intermediate_response (vrh, ds_entry->delegation_chain_entry, false);
868
869 // current delegation as parent
870 ds_entry->parent_queue_entry = dq_entry;
871
872 // Check for solution
873 // if: issuer key we looking for
874 if (0 == memcmp (&del->issuer_key,
875 &vrh->issuer_key,
876 sizeof (struct GNUNET_CRYPTO_PublicKey)))
877 {
878 // if: issuer attr we looking for
879 if (0 == strcmp (del->issuer_attribute, vrh->issuer_attribute))
880 {
881 // if: complete match, meaning new trailer == issuer attr
882 if (0 == strcmp (vrh->issuer_attribute, ds_entry->attr_trailer))
883 {
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found: Solution\n");
885
886 // Add found solution into delegation_chain
887 struct DelegationSetQueueEntry *tmp_set;
888 for (tmp_set = ds_entry; NULL != tmp_set->parent_queue_entry;
889 tmp_set = tmp_set->parent_queue_entry->parent_set)
890 {
891 if (NULL != tmp_set->delegation_chain_entry)
892 {
893 vrh->delegation_chain_size++;
894 GNUNET_CONTAINER_DLL_insert (vrh->delegation_chain_head,
895 vrh->delegation_chain_tail,
896 tmp_set->delegation_chain_entry);
897 }
898 }
899
900 // Increase refcount for this delegate
901 for (struct DelegateRecordEntry *del_entry = vrh->del_chain_head;
902 del_entry != NULL;
903 del_entry = del_entry->next)
904 {
905 if (0 == memcmp (&del_entry->delegate->issuer_key,
906 &vrh->delegation_chain_head->subject_key,
907 sizeof (struct GNUNET_CRYPTO_PublicKey)))
908 {
909 if (0 == strcmp (del_entry->delegate->issuer_attribute,
910 vrh->delegation_chain_head->subject_attribute))
911 {
912 del_entry->refcount++;
913 }
914 }
915 }
916
917 send_lookup_response (vrh);
918 GNUNET_free (del);
919 return;
920 }
921 }
922 }
923
924 // Check for bidirectional crossmatch
925 for (struct DelegationSetQueueEntry *del_entry = vrh->dsq_head;
926 del_entry != NULL;
927 del_entry = del_entry->next)
928 {
929 // only check entries not by backward algorithm
930 if (del_entry->from_bw)
931 {
932 // key of list entry matches actual key
933 if (0 == memcmp (&del_entry->delegation_chain_entry->subject_key,
934 &ds_entry->delegation_chain_entry->issuer_key,
935 sizeof (struct GNUNET_CRYPTO_PublicKey)))
936 {
937 // compare entry subject attributes to this trailer (iss attr + old trailer)
938 if (0 == strcmp (del_entry->unresolved_attribute_delegation,
939 ds_entry->attr_trailer))
940 {
941 print_deleset (del_entry, "Forward:");
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "Forward: Found match with above!\n");
944
945 GNUNET_free (del);
946 // one node on the path still needs solutions: return
947 if (GNUNET_NO ==
948 handle_bidirectional_match (ds_entry, del_entry, vrh))
949 return;
950
951 send_lookup_response (vrh);
952 return;
953 }
954 }
955 }
956 }
957
958 // Starting a new GNS lookup
959 vrh->pending_lookups++;
960 ds_entry->handle = vrh;
961
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "Starting to look up trailer %s in zone %s\n",
964 ds_entry->attr_trailer,
965 GNUNET_CRYPTO_public_key_to_string (&del->issuer_key));
966
967 ds_entry->lookup_request =
968 GNUNET_GNS_lookup (gns,
969 GNUNET_GNS_EMPTY_LABEL_AT,
970 &del->issuer_key,
971 GNUNET_GNSRECORD_TYPE_DELEGATE,
972 GNUNET_GNS_LO_DEFAULT,
973 &forward_resolution,
974 ds_entry);
975 GNUNET_free (del);
976 }
977
978 if (0 == vrh->pending_lookups)
979 {
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "We are all out of attributes...\n");
981 send_lookup_response (vrh);
982 return;
983 }
984}
985
986
987static void
988backward_resolution (void *cls,
989 uint32_t rd_count,
990 const struct GNUNET_GNSRECORD_Data *rd)
991{
992 struct VerifyRequestHandle *vrh;
993 const struct GNUNET_ABD_DelegationRecord *sets;
994 struct DelegateRecordEntry *del_pointer;
995 struct DelegationSetQueueEntry *current_set;
996 struct DelegationSetQueueEntry *ds_entry;
997 struct DelegationSetQueueEntry *tmp_set;
998 struct DelegationQueueEntry *dq_entry;
999 char *expanded_attr;
1000 char *lookup_attribute;
1001
1002 current_set = cls;
1003 current_set->lookup_request = NULL;
1004 vrh = current_set->handle;
1005 vrh->pending_lookups--;
1006
1007 // Each OR
1008 for (uint32_t i = 0; i < rd_count; i++)
1009 {
1010 if (GNUNET_GNSRECORD_TYPE_ATTRIBUTE != rd[i].record_type)
1011 continue;
1012
1013 sets = rd[i].data;
1014 struct GNUNET_ABD_DelegationSet set[ntohl (sets->set_count)];
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "Found new attribute delegation with %d sets. Creating new Job...\n",
1017 ntohl (sets->set_count));
1018
1019 if (GNUNET_OK !=
1020 GNUNET_ABD_delegation_set_deserialize (GNUNET_ntohll (
1021 sets->data_size),
1022 (const char *) &sets[1],
1023 ntohl (sets->set_count),
1024 set))
1025 {
1026 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to deserialize!\n");
1027 continue;
1028 }
1029 dq_entry = GNUNET_new (struct DelegationQueueEntry);
1030 dq_entry->required_solutions = ntohl (sets->set_count);
1031 dq_entry->parent_set = current_set;
1032
1033 GNUNET_CONTAINER_DLL_insert (current_set->queue_entries_head,
1034 current_set->queue_entries_tail,
1035 dq_entry);
1036 // Each AND
1037 for (uint32_t j = 0; j < ntohl (sets->set_count); j++)
1038 {
1039 ds_entry = GNUNET_new (struct DelegationSetQueueEntry);
1040 GNUNET_CONTAINER_DLL_insert (vrh->dsq_head, vrh->dsq_tail, ds_entry);
1041 ds_entry->from_bw = true;
1042
1043 if (NULL != current_set->attr_trailer)
1044 {
1045 if (0 == set[j].subject_attribute_len)
1046 {
1047 GNUNET_asprintf (&expanded_attr, "%s", current_set->attr_trailer);
1048 }
1049 else
1050 {
1051 GNUNET_asprintf (&expanded_attr,
1052 "%s.%s",
1053 set[j].subject_attribute,
1054 current_set->attr_trailer);
1055 }
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expanded to %s\n", expanded_attr);
1057 ds_entry->unresolved_attribute_delegation = expanded_attr;
1058 }
1059 else
1060 {
1061 if (0 != set[j].subject_attribute_len)
1062 {
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Not Expanding %s\n",
1065 set[j].subject_attribute);
1066 ds_entry->unresolved_attribute_delegation =
1067 GNUNET_strdup (set[j].subject_attribute);
1068 }
1069 }
1070
1071 // Add a credential chain entry
1072 ds_entry->delegation_chain_entry =
1073 GNUNET_new (struct DelegationChainEntry);
1074 ds_entry->delegation_chain_entry->subject_key = set[j].subject_key;
1075 ds_entry->issuer_key = GNUNET_new (struct GNUNET_CRYPTO_PublicKey);
1076 GNUNET_memcpy (ds_entry->issuer_key,
1077 &set[j].subject_key,
1078 sizeof (struct GNUNET_CRYPTO_PublicKey));
1079 if (0 < set[j].subject_attribute_len)
1080 ds_entry->delegation_chain_entry->subject_attribute =
1081 GNUNET_strdup (set[j].subject_attribute);
1082 ds_entry->delegation_chain_entry->issuer_key = *current_set->issuer_key;
1083 ds_entry->delegation_chain_entry->issuer_attribute =
1084 GNUNET_strdup (current_set->lookup_attribute);
1085
1086 // Found new entry, repoting intermediate result
1087 send_intermediate_response (vrh, ds_entry->delegation_chain_entry, true);
1088
1089 ds_entry->parent_queue_entry = dq_entry; // current_delegation;
1090
1091 /**
1092 * Check if this delegation already matches one of our credentials
1093 */
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking for cred match\n");
1095
1096 for (del_pointer = vrh->del_chain_head; del_pointer != NULL;
1097 del_pointer = del_pointer->next)
1098 {
1099 // If key and attribute match credential: continue and backtrack
1100 if (0 != memcmp (&set[j].subject_key,
1101 &del_pointer->delegate->issuer_key,
1102 sizeof (struct GNUNET_CRYPTO_PublicKey)))
1103 continue;
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "Checking if %s matches %s\n",
1106 ds_entry->unresolved_attribute_delegation,
1107 del_pointer->delegate->issuer_attribute);
1108
1109 if (0 != strcmp (ds_entry->unresolved_attribute_delegation,
1110 del_pointer->delegate->issuer_attribute))
1111 continue;
1112
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found issuer\n");
1114 // increase refcount of the start delegation
1115 del_pointer->refcount++;
1116
1117 // Backtrack
1118 for (tmp_set = ds_entry; NULL != tmp_set->parent_queue_entry;
1119 tmp_set = tmp_set->parent_queue_entry->parent_set)
1120 {
1121 tmp_set->parent_queue_entry->required_solutions--;
1122 if (NULL != tmp_set->delegation_chain_entry)
1123 {
1124 vrh->delegation_chain_size++;
1125 GNUNET_CONTAINER_DLL_insert (vrh->delegation_chain_head,
1126 vrh->delegation_chain_tail,
1127 tmp_set->delegation_chain_entry);
1128 }
1129 if (0 < tmp_set->parent_queue_entry->required_solutions)
1130 break;
1131 }
1132
1133 // if the break above is not called the condition of the for is met
1134 if (NULL == tmp_set->parent_queue_entry)
1135 {
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All solutions found\n");
1137 // Found match
1138 send_lookup_response (vrh);
1139 return;
1140 }
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not all solutions found yet.\n");
1142 continue;
1143 }
1144
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "Building new lookup request from %s\n",
1147 ds_entry->unresolved_attribute_delegation);
1148 // Continue with next/new backward resolution
1149 char issuer_attribute_name[strlen (
1150 ds_entry->unresolved_attribute_delegation)
1151 + 1];
1152 strcpy (issuer_attribute_name, ds_entry->unresolved_attribute_delegation);
1153 char *next_attr = strtok (issuer_attribute_name, ".");
1154 if (NULL == next_attr)
1155 {
1156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1157 "Failed to parse next attribute\n");
1158 continue;
1159 }
1160 GNUNET_asprintf (&lookup_attribute, "%s", next_attr);
1161 GNUNET_asprintf (&ds_entry->lookup_attribute, "%s", next_attr);
1162 if (strlen (next_attr) ==
1163 strlen (ds_entry->unresolved_attribute_delegation))
1164 {
1165 ds_entry->attr_trailer = NULL;
1166 }
1167 else
1168 {
1169 next_attr += strlen (next_attr) + 1;
1170 ds_entry->attr_trailer = GNUNET_strdup (next_attr);
1171 }
1172
1173 // Check for bidirectional crossmatch
1174 for (struct DelegationSetQueueEntry *del_entry = vrh->dsq_head;
1175 del_entry != NULL;
1176 del_entry = del_entry->next)
1177 {
1178 // only check entries added by forward algorithm
1179 if (! del_entry->from_bw)
1180 {
1181 // key of list entry matches actual key
1182 if (0 == memcmp (&del_entry->delegation_chain_entry->issuer_key,
1183 &ds_entry->delegation_chain_entry->subject_key,
1184 sizeof (struct GNUNET_CRYPTO_PublicKey)))
1185 {
1186 // compare entry subject attributes to this trailer (iss attr + old trailer)
1187 if (0 == strcmp (del_entry->attr_trailer,
1188 ds_entry->unresolved_attribute_delegation))
1189 {
1190 print_deleset (del_entry, "Backward:");
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "Backward: Found match with above!\n");
1193
1194 // if one node on the path still needs solutions: return
1195 if (GNUNET_NO ==
1196 handle_bidirectional_match (del_entry, ds_entry, vrh))
1197 break;
1198
1199 // Send lookup response
1200 send_lookup_response (vrh);
1201 return;
1202 }
1203 }
1204 }
1205 }
1206
1207 // Starting a new GNS lookup
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Looking up %s\n",
1210 ds_entry->lookup_attribute);
1211 if (NULL != ds_entry->attr_trailer)
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "%s still to go...\n",
1214 ds_entry->attr_trailer);
1215
1216 vrh->pending_lookups++;
1217 ds_entry->handle = vrh;
1218 ds_entry->lookup_request =
1219 GNUNET_GNS_lookup (gns,
1220 lookup_attribute,
1221 ds_entry->issuer_key, // issuer_key,
1222 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
1223 GNUNET_GNS_LO_DEFAULT,
1224 &backward_resolution,
1225 ds_entry);
1226
1227 GNUNET_free (lookup_attribute);
1228 }
1229 }
1230
1231 if (0 == vrh->pending_lookups)
1232 {
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "We are all out of attributes...\n");
1234 send_lookup_response (vrh);
1235 return;
1236 }
1237}
1238
1239
1240/**
1241 * Result from GNS lookup.
1242 *
1243 * @param cls the closure (our client lookup handle)
1244 */
1245static int
1246delegation_chain_bw_resolution_start (void *cls)
1247{
1248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Start Backward Resolution...\n");
1249
1250 struct VerifyRequestHandle *vrh = cls;
1251 struct DelegationSetQueueEntry *ds_entry;
1252 struct DelegateRecordEntry *del_entry;
1253
1254 if (0 == vrh->del_chain_size)
1255 {
1256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No delegates found\n");
1257 send_lookup_response (vrh);
1258 return 1;
1259 }
1260
1261 // Pre-check with vrh->dele_chain_.. if match issuer_key
1262 // Backward: check every cred entry if match issuer key
1263 // otherwise: start at issuer and go down till match
1264 // A.a <- ...
1265 // X.x <- C
1266 // Y.y <- C
1267 // if not X.x or Y.y == A.a start at A
1268 for (del_entry = vrh->del_chain_head; del_entry != NULL;
1269 del_entry = del_entry->next)
1270 {
1271 if (0 != memcmp (&del_entry->delegate->issuer_key,
1272 &vrh->issuer_key,
1273 sizeof (struct GNUNET_CRYPTO_PublicKey)))
1274 continue;
1275 if (0 !=
1276 strcmp (del_entry->delegate->issuer_attribute, vrh->issuer_attribute))
1277 continue;
1278 del_entry->refcount++;
1279 // Found match prematurely
1280 send_lookup_response (vrh);
1281 return 1;
1282 }
1283
1284
1285 // Check for attributes from the issuer and follow the chain
1286 // till you get the required subject's attributes
1287 char issuer_attribute_name[strlen (vrh->issuer_attribute) + 1];
1288 strcpy (issuer_attribute_name, vrh->issuer_attribute);
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Looking up %s\n",
1291 issuer_attribute_name);
1292 ds_entry = GNUNET_new (struct DelegationSetQueueEntry);
1293 GNUNET_CONTAINER_DLL_insert (vrh->dsq_head, vrh->dsq_tail, ds_entry);
1294 ds_entry->from_bw = true;
1295 ds_entry->issuer_key = GNUNET_new (struct GNUNET_CRYPTO_PublicKey);
1296 GNUNET_memcpy (ds_entry->issuer_key,
1297 &vrh->issuer_key,
1298 sizeof (struct GNUNET_CRYPTO_PublicKey));
1299 ds_entry->issuer_attribute = GNUNET_strdup (vrh->issuer_attribute);
1300
1301 ds_entry->delegation_chain_entry = GNUNET_new (struct DelegationChainEntry);
1302 ds_entry->delegation_chain_entry->issuer_key = vrh->issuer_key;
1303 ds_entry->delegation_chain_entry->issuer_attribute =
1304 GNUNET_strdup (vrh->issuer_attribute);
1305
1306 ds_entry->handle = vrh;
1307 ds_entry->lookup_attribute = GNUNET_strdup (vrh->issuer_attribute);
1308 ds_entry->unresolved_attribute_delegation = NULL;
1309 vrh->pending_lookups = 1;
1310
1311 // Start with backward resolution
1312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start Backward Resolution\n");
1313
1314 ds_entry->lookup_request = GNUNET_GNS_lookup (gns,
1315 issuer_attribute_name,
1316 &vrh->issuer_key, // issuer_key,
1317 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
1318 GNUNET_GNS_LO_DEFAULT,
1319 &backward_resolution,
1320 ds_entry);
1321 return 0;
1322}
1323
1324
1325static int
1326delegation_chain_fw_resolution_start (void *cls)
1327{
1328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Start Forward Resolution...\n");
1329
1330 struct VerifyRequestHandle *vrh = cls;
1331 struct DelegationSetQueueEntry *ds_entry;
1332 struct DelegateRecordEntry *del_entry;
1333
1334 // set to 0 and increase on each lookup: for fw multiple lookups (may be) started
1335 vrh->pending_lookups = 0;
1336
1337 if (0 == vrh->del_chain_size)
1338 {
1339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No delegations found\n");
1340 send_lookup_response (vrh);
1341 return 1;
1342 }
1343
1344 // Pre-check with vrh->dele_chain_.. if match issuer_key
1345 // otherwise FW: start multiple lookups for each vrh->dele_chain
1346 // A.a <- ...
1347 // X.x <- C
1348 // Y.y <- C
1349 // if not X.x or Y.y == A.a start at X and at Y
1350 for (del_entry = vrh->del_chain_head; del_entry != NULL;
1351 del_entry = del_entry->next)
1352 {
1353 if (0 != memcmp (&del_entry->delegate->issuer_key,
1354 &vrh->issuer_key,
1355 sizeof (struct GNUNET_CRYPTO_PublicKey)))
1356 continue;
1357 if (0 !=
1358 strcmp (del_entry->delegate->issuer_attribute, vrh->issuer_attribute))
1359 continue;
1360 del_entry->refcount++;
1361 // Found match prematurely
1362 send_lookup_response (vrh);
1363 return 1;
1364 }
1365
1366 // None match, therefore start for every delegation found a lookup chain
1367 // Return and end collect process on first chain iss <-> sub found
1368
1369 // ds_entry created belongs to the first lookup, vrh still has the
1370 // issuer+attr we look for
1371 for (del_entry = vrh->del_chain_head; del_entry != NULL;
1372 del_entry = del_entry->next)
1373 {
1374
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Looking for %s.%s\n",
1377 GNUNET_CRYPTO_public_key_to_string (
1378 &del_entry->delegate->issuer_key),
1379 del_entry->delegate->issuer_attribute);
1380
1381 ds_entry = GNUNET_new (struct DelegationSetQueueEntry);
1382 GNUNET_CONTAINER_DLL_insert (vrh->dsq_head, vrh->dsq_tail, ds_entry);
1383 ds_entry->from_bw = false;
1384 ds_entry->issuer_key = GNUNET_new (struct GNUNET_CRYPTO_PublicKey);
1385 GNUNET_memcpy (ds_entry->issuer_key,
1386 &del_entry->delegate->subject_key,
1387 sizeof (struct GNUNET_CRYPTO_PublicKey));
1388
1389 ds_entry->delegation_chain_entry = GNUNET_new (struct DelegationChainEntry);
1390 ds_entry->delegation_chain_entry->subject_key =
1391 del_entry->delegate->subject_key;
1392 ds_entry->delegation_chain_entry->subject_attribute = NULL;
1393 ds_entry->delegation_chain_entry->issuer_key =
1394 del_entry->delegate->issuer_key;
1395 ds_entry->delegation_chain_entry->issuer_attribute =
1396 GNUNET_strdup (del_entry->delegate->issuer_attribute);
1397
1398 ds_entry->attr_trailer =
1399 GNUNET_strdup (del_entry->delegate->issuer_attribute);
1400 ds_entry->handle = vrh;
1401
1402 vrh->pending_lookups++;
1403 // Start with forward resolution
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start Forward Resolution\n");
1405
1406 ds_entry->lookup_request =
1407 GNUNET_GNS_lookup (gns,
1408 GNUNET_GNS_EMPTY_LABEL_AT,
1409 &del_entry->delegate->issuer_key, // issuer_key,
1410 GNUNET_GNSRECORD_TYPE_DELEGATE,
1411 GNUNET_GNS_LO_DEFAULT,
1412 &forward_resolution,
1413 ds_entry);
1414 }
1415 return 0;
1416}
1417
1418
1419static int
1420check_verify (void *cls, const struct VerifyMessage *v_msg)
1421{
1422 size_t msg_size;
1423 const char *attr;
1424
1425 msg_size = ntohs (v_msg->header.size);
1426 if (msg_size < sizeof (struct VerifyMessage))
1427 {
1428 GNUNET_break (0);
1429 return GNUNET_SYSERR;
1430 }
1431 if (ntohs (v_msg->issuer_attribute_len) > GNUNET_ABD_MAX_LENGTH)
1432 {
1433 GNUNET_break (0);
1434 return GNUNET_SYSERR;
1435 }
1436 attr = (const char *) &v_msg[1];
1437
1438 if (strlen (attr) > GNUNET_ABD_MAX_LENGTH)
1439 {
1440 GNUNET_break (0);
1441 return GNUNET_SYSERR;
1442 }
1443 return GNUNET_OK;
1444}
1445
1446
1447static void
1448handle_verify (void *cls, const struct VerifyMessage *v_msg)
1449{
1450 struct VerifyRequestHandle *vrh;
1451 struct GNUNET_SERVICE_Client *client = cls;
1452 struct DelegateRecordEntry *del_entry;
1453 uint32_t delegate_count;
1454 uint32_t delegate_data_size;
1455 char attr[GNUNET_ABD_MAX_LENGTH + 1];
1456 char issuer_attribute[GNUNET_ABD_MAX_LENGTH + 1];
1457 char *attrptr = attr;
1458 char *delegate_data;
1459 const char *utf_in;
1460
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received VERIFY message\n");
1462 utf_in = (const char *) &v_msg[1];
1463 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
1464 GNUNET_memcpy (issuer_attribute, attr, ntohs (v_msg->issuer_attribute_len));
1465 issuer_attribute[ntohs (v_msg->issuer_attribute_len)] = '\0';
1466 vrh = GNUNET_new (struct VerifyRequestHandle);
1467 vrh->is_collect = false;
1468 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
1469 vrh->client = client;
1470 vrh->request_id = v_msg->id;
1471 vrh->issuer_key = v_msg->issuer_key;
1472 vrh->subject_key = v_msg->subject_key;
1473 vrh->issuer_attribute = GNUNET_strdup (issuer_attribute);
1474 vrh->resolution_algo = ntohs (v_msg->resolution_algo);
1475
1476 vrh->del_chain_head = NULL;
1477 vrh->del_chain_tail = NULL;
1478 vrh->dsq_head = NULL;
1479 vrh->dsq_tail = NULL;
1480 vrh->del_chain_head = NULL;
1481 vrh->del_chain_tail = NULL;
1482
1483 GNUNET_SERVICE_client_continue (vrh->client);
1484 if (0 == strlen (issuer_attribute))
1485 {
1486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer attribute provided!\n");
1487 send_lookup_response (vrh);
1488 return;
1489 }
1490
1491 // Parse delegates from verifaction message
1492 delegate_count = ntohl (v_msg->d_count);
1493 delegate_data_size = ntohs (v_msg->header.size)
1494 - sizeof (struct VerifyMessage)
1495 - ntohs (v_msg->issuer_attribute_len) - 1;
1496 struct GNUNET_ABD_Delegate delegates[delegate_count];
1497 memset (delegates,
1498 0,
1499 sizeof (struct GNUNET_ABD_Delegate) * delegate_count);
1500 delegate_data = (char *) &v_msg[1] + ntohs (v_msg->issuer_attribute_len) + 1;
1501 if (GNUNET_OK != GNUNET_ABD_delegates_deserialize (delegate_data_size,
1502 delegate_data,
1503 delegate_count,
1504 delegates))
1505 {
1506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot deserialize delegates!\n");
1507 send_lookup_response (vrh);
1508 return;
1509 }
1510
1511 // Prepare vrh delegation chain for later validation
1512 for (uint32_t i = 0; i < delegate_count; i++)
1513 {
1514 del_entry = GNUNET_new (struct DelegateRecordEntry);
1515 del_entry->delegate =
1516 GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate)
1517 + delegates[i].issuer_attribute_len + 1);
1518 GNUNET_memcpy (del_entry->delegate,
1519 &delegates[i],
1520 sizeof (struct GNUNET_ABD_Delegate));
1521 GNUNET_memcpy (&del_entry->delegate[1],
1522 delegates[i].issuer_attribute,
1523 delegates[i].issuer_attribute_len);
1524 del_entry->delegate->issuer_attribute_len =
1525 delegates[i].issuer_attribute_len;
1526 del_entry->delegate->issuer_attribute = (char *) &del_entry->delegate[1];
1527 GNUNET_CONTAINER_DLL_insert_tail (vrh->del_chain_head,
1528 vrh->del_chain_tail,
1529 del_entry);
1530 vrh->del_chain_size++;
1531 }
1532
1533 // Switch resolution algo
1534 if (GNUNET_ABD_FLAG_BACKWARD & vrh->resolution_algo &&
1535 GNUNET_ABD_FLAG_FORWARD & vrh->resolution_algo)
1536 {
1537 if (1 == delegation_chain_fw_resolution_start (vrh))
1538 return;
1539 delegation_chain_bw_resolution_start (vrh);
1540 }
1541 else if (GNUNET_ABD_FLAG_BACKWARD & vrh->resolution_algo)
1542 {
1543 delegation_chain_bw_resolution_start (vrh);
1544 }
1545 else if (GNUNET_ABD_FLAG_FORWARD & vrh->resolution_algo)
1546 {
1547 delegation_chain_fw_resolution_start (vrh);
1548 }
1549}
1550
1551
1552static void
1553handle_delegate_collection_error_cb (void *cls)
1554{
1555 struct VerifyRequestHandle *vrh = cls;
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Got disconnected from namestore database.\n");
1558 vrh->dele_qe = NULL;
1559 send_lookup_response (vrh);
1560}
1561
1562
1563static void
1564delegate_collection_finished (void *cls)
1565{
1566 struct VerifyRequestHandle *vrh = cls;
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done collecting delegates.\n");
1568
1569 // if both are set: bidirectional search, meaning start both chain resolutions
1570 if (GNUNET_ABD_FLAG_BACKWARD & vrh->resolution_algo &&
1571 GNUNET_ABD_FLAG_FORWARD & vrh->resolution_algo)
1572 {
1573 // if premature match found don't start bw resolution
1574 if (1 == delegation_chain_fw_resolution_start (vrh))
1575 return;
1576 delegation_chain_bw_resolution_start (vrh);
1577 }
1578 else if (GNUNET_ABD_FLAG_BACKWARD & vrh->resolution_algo)
1579 {
1580 delegation_chain_bw_resolution_start (vrh);
1581 }
1582 else if (GNUNET_ABD_FLAG_FORWARD & vrh->resolution_algo)
1583 {
1584 delegation_chain_fw_resolution_start (vrh);
1585 }
1586}
1587
1588
1589static void
1590handle_delegate_collection_cb (void *cls,
1591 const struct GNUNET_CRYPTO_PrivateKey *key,
1592 const char *label,
1593 unsigned int rd_count,
1594 const struct GNUNET_GNSRECORD_Data *rd)
1595{
1596 struct VerifyRequestHandle *vrh = cls;
1597 struct DelegateRecordEntry *del_entry;
1598 vrh->dele_qe = NULL;
1599
1600 for (uint32_t i = 0; i < rd_count; i++)
1601 {
1602 if (GNUNET_GNSRECORD_TYPE_DELEGATE != rd[i].record_type)
1603 continue;
1604 // only add the entries that are explicitly marked as private
1605 // and therefore symbolize the end of a chain
1606 if (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
1607 continue;
1608 del_entry = GNUNET_new (struct DelegateRecordEntry);
1609 del_entry->delegate = GNUNET_ABD_delegate_deserialize (rd[i].data, rd[i].data_size);
1610 if (NULL == del_entry->delegate)
1611 {
1612 GNUNET_free (del_entry);
1613 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid delegate found\n");
1614 continue;
1615 }
1616 GNUNET_CONTAINER_DLL_insert_tail (vrh->del_chain_head,
1617 vrh->del_chain_tail,
1618 del_entry);
1619 vrh->del_chain_size++;
1620 }
1621
1622 delegate_collection_finished (vrh);
1623}
1624
1625
1626static void
1627handle_collect (void *cls, const struct CollectMessage *c_msg)
1628{
1629 char attr[GNUNET_ABD_MAX_LENGTH + 1];
1630 char issuer_attribute[GNUNET_ABD_MAX_LENGTH + 1];
1631 struct VerifyRequestHandle *vrh;
1632 struct GNUNET_SERVICE_Client *client = cls;
1633 char *attrptr = attr;
1634 const char *utf_in;
1635
1636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received COLLECT message\n");
1637
1638 utf_in = (const char *) &c_msg[1];
1639 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
1640
1641 GNUNET_memcpy (issuer_attribute, attr, ntohs (c_msg->issuer_attribute_len));
1642 issuer_attribute[ntohs (c_msg->issuer_attribute_len)] = '\0';
1643 vrh = GNUNET_new (struct VerifyRequestHandle);
1644 vrh->is_collect = true;
1645 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
1646 vrh->client = client;
1647 vrh->request_id = c_msg->id;
1648 vrh->issuer_key = c_msg->issuer_key;
1649 GNUNET_CRYPTO_key_get_public (&c_msg->subject_key, &vrh->subject_key);
1650 vrh->issuer_attribute = GNUNET_strdup (issuer_attribute);
1651 vrh->resolution_algo = ntohs (c_msg->resolution_algo);
1652
1653 vrh->del_chain_head = NULL;
1654 vrh->del_chain_tail = NULL;
1655 vrh->dsq_head = NULL;
1656 vrh->dsq_tail = NULL;
1657 vrh->del_chain_head = NULL;
1658 vrh->del_chain_tail = NULL;
1659
1660 if (0 == strlen (issuer_attribute))
1661 {
1662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer attribute provided!\n");
1663 send_lookup_response (vrh);
1664 return;
1665 }
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting delegates for subject\n");
1667
1668 // Get all delegates from subject
1669 vrh->dele_qe =
1670 GNUNET_NAMESTORE_records_lookup (namestore,
1671 &c_msg->subject_key,
1672 GNUNET_GNS_EMPTY_LABEL_AT,
1673 &handle_delegate_collection_error_cb,
1674 vrh,
1675 &handle_delegate_collection_cb,
1676 vrh);
1677 GNUNET_SERVICE_client_continue (vrh->client);
1678}
1679
1680
1681static int
1682check_collect (void *cls, const struct CollectMessage *c_msg)
1683{
1684 size_t msg_size;
1685 const char *attr;
1686
1687 msg_size = ntohs (c_msg->header.size);
1688 if (msg_size < sizeof (struct CollectMessage))
1689 {
1690 GNUNET_break (0);
1691 return GNUNET_SYSERR;
1692 }
1693 if (ntohs (c_msg->issuer_attribute_len) > GNUNET_ABD_MAX_LENGTH)
1694 {
1695 GNUNET_break (0);
1696 return GNUNET_SYSERR;
1697 }
1698 attr = (const char *) &c_msg[1];
1699
1700 if (('\0' != attr[msg_size - sizeof (struct CollectMessage) - 1]) ||
1701 (strlen (attr) > GNUNET_ABD_MAX_LENGTH))
1702 {
1703 GNUNET_break (0);
1704 return GNUNET_SYSERR;
1705 }
1706 return GNUNET_OK;
1707}
1708
1709
1710static void
1711client_disconnect_cb (void *cls,
1712 struct GNUNET_SERVICE_Client *client,
1713 void *app_ctx)
1714{
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected\n", client);
1716}
1717
1718
1719static void *
1720client_connect_cb (void *cls,
1721 struct GNUNET_SERVICE_Client *client,
1722 struct GNUNET_MQ_Handle *mq)
1723{
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
1725 return client;
1726}
1727
1728
1729/**
1730 * Process Credential requests.
1731 *
1732 * @param cls closure
1733 * @param c configuration to use
1734 * @param handle service handle
1735 */
1736static void
1737run (void *cls,
1738 const struct GNUNET_CONFIGURATION_Handle *c,
1739 struct GNUNET_SERVICE_Handle *handle)
1740{
1741
1742 gns = GNUNET_GNS_connect (c);
1743 if (NULL == gns)
1744 {
1745 fprintf (stderr, _ ("Failed to connect to GNS\n"));
1746 }
1747 namestore = GNUNET_NAMESTORE_connect (c);
1748 if (NULL == namestore)
1749 {
1750 fprintf (stderr, _ ("Failed to connect to namestore\n"));
1751 }
1752
1753 statistics = GNUNET_STATISTICS_create ("abd", c);
1754 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1755}
1756
1757
1758/**
1759 * Define "main" method using service macro
1760 */
1761GNUNET_SERVICE_MAIN (
1762 "abd",
1763 GNUNET_SERVICE_OPTION_NONE,
1764 &run,
1765 &client_connect_cb,
1766 &client_disconnect_cb,
1767 NULL,
1768 GNUNET_MQ_hd_var_size (verify,
1769 GNUNET_MESSAGE_TYPE_ABD_VERIFY,
1770 struct VerifyMessage,
1771 NULL),
1772 GNUNET_MQ_hd_var_size (collect,
1773 GNUNET_MESSAGE_TYPE_ABD_COLLECT,
1774 struct CollectMessage,
1775 NULL),
1776 GNUNET_MQ_handler_end ());
1777
1778/* end of gnunet-service-abd.c */
diff --git a/src/contrib/service/abd/plugin_gnsrecord_abd.c b/src/contrib/service/abd/plugin_gnsrecord_abd.c
new file mode 100644
index 000000000..43abe578f
--- /dev/null
+++ b/src/contrib/service/abd/plugin_gnsrecord_abd.c
@@ -0,0 +1,357 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 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/**
22 * @file abd/plugin_gnsrecord_abd.c
23 * @brief gnsrecord plugin to provide the API for ABD records
24 * @author Martin Schanzenbach
25 */
26#include "platform.h"
27
28#include "gnunet_util_lib.h"
29
30#include "delegate_misc.h"
31#include "abd_serialization.h"
32#include "gnunet_abd_service.h"
33#include "gnunet_gnsrecord_lib.h"
34#include "gnunet_gnsrecord_plugin.h"
35#include "gnunet_signatures.h"
36/**
37 * Convert the 'value' of a record to a string.
38 *
39 * @param cls closure, unused
40 * @param type type of the record
41 * @param data value in binary encoding
42 * @param data_size number of bytes in @a data
43 * @return NULL on error, otherwise human-readable representation of the value
44 */
45static char *
46abd_value_to_string (void *cls,
47 uint32_t type,
48 const void *data,
49 size_t data_size)
50{
51 const char *cdata;
52
53 switch (type)
54 {
55 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
56 {
57 struct GNUNET_ABD_DelegationRecord sets;
58 char *attr_str;
59 char *subject_pkey;
60 char *tmp_str;
61 int i;
62 if (data_size < sizeof (struct GNUNET_ABD_DelegationRecord))
63 return NULL; /* malformed */
64
65 GNUNET_memcpy (&sets, data, sizeof (sets));
66 cdata = data;
67
68 struct GNUNET_ABD_DelegationSet set[ntohl (sets.set_count)];
69 if (GNUNET_OK !=
70 GNUNET_ABD_delegation_set_deserialize (GNUNET_ntohll (
71 sets.data_size),
72 &cdata[sizeof (sets)],
73 ntohl (sets.set_count),
74 set))
75 return NULL;
76
77 for (i = 0; i < ntohl (sets.set_count); i++)
78 {
79 subject_pkey =
80 GNUNET_CRYPTO_public_key_to_string (&set[i].subject_key);
81
82 if (0 == set[i].subject_attribute_len)
83 {
84 if (0 == i)
85 {
86 GNUNET_asprintf (&attr_str, "%s", subject_pkey);
87 }
88 else
89 {
90 GNUNET_asprintf (&tmp_str, "%s,%s", attr_str, subject_pkey);
91 GNUNET_free (attr_str);
92 attr_str = tmp_str;
93 }
94 }
95 else
96 {
97 if (0 == i)
98 {
99 GNUNET_asprintf (&attr_str,
100 "%s %s",
101 subject_pkey,
102 set[i].subject_attribute);
103 }
104 else
105 {
106 GNUNET_asprintf (&tmp_str,
107 "%s,%s %s",
108 attr_str,
109 subject_pkey,
110 set[i].subject_attribute);
111 GNUNET_free (attr_str);
112 attr_str = tmp_str;
113 }
114 }
115 GNUNET_free (subject_pkey);
116 }
117 return attr_str;
118 }
119 case GNUNET_GNSRECORD_TYPE_DELEGATE:
120 {
121 struct GNUNET_ABD_Delegate *cred;
122 char *cred_str;
123
124 cred = GNUNET_ABD_delegate_deserialize (data, data_size);
125 cred_str = GNUNET_ABD_delegate_to_string (cred);
126 GNUNET_free (cred);
127 return cred_str;
128 }
129 default:
130 return NULL;
131 }
132}
133
134
135/**
136 * Convert human-readable version of a 'value' of a record to the binary
137 * representation.
138 *
139 * @param cls closure, unused
140 * @param type type of the record
141 * @param s human-readable string
142 * @param data set to value in binary encoding (will be allocated)
143 * @param data_size set to number of bytes in @a data
144 * @return #GNUNET_OK on success
145 */
146static int
147abd_string_to_value (void *cls,
148 uint32_t type,
149 const char *s,
150 void **data,
151 size_t *data_size)
152{
153 if (NULL == s)
154 return GNUNET_SYSERR;
155 switch (type)
156 {
157 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
158 {
159 struct GNUNET_ABD_DelegationRecord *sets;
160 char attr_str[253 + 1];
161 char subject_pkey[58 + 1];
162 char *token;
163 char *tmp_str;
164 int matches = 0;
165 int entries;
166 size_t tmp_data_size;
167 int i;
168
169 tmp_str = GNUNET_strdup (s);
170 token = strtok (tmp_str, ",");
171 entries = 0;
172 tmp_data_size = 0;
173 *data_size = sizeof (struct GNUNET_ABD_DelegationRecord);
174 while (NULL != token)
175 {
176 // also fills the variables subject_pley and attr_str if "regex"-like match
177 matches = sscanf (token, "%s %s", subject_pkey, attr_str);
178
179 if (0 == matches)
180 {
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 _ ("Unable to parse ATTR record string `%s'\n"),
183 s);
184 GNUNET_free (tmp_str);
185 return GNUNET_SYSERR;
186 }
187
188 entries++;
189 token = strtok (NULL, ",");
190 }
191 GNUNET_free (tmp_str);
192
193 tmp_str = GNUNET_strdup (s);
194 token = strtok (tmp_str, ",");
195 if (NULL == token)
196 {
197 GNUNET_free (tmp_str);
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
199 return GNUNET_SYSERR;
200 }
201
202 struct GNUNET_ABD_DelegationSet set[entries];
203 // sets memory to be 0, starting at *set for the size of struct * entries
204 memset (set, 0, sizeof (struct GNUNET_ABD_DelegationSet) * entries);
205 for (i = 0; i < entries; i++)
206 {
207 matches = sscanf (token, "%s %s", subject_pkey, attr_str);
208
209 // sets the public key for the set entry
210 if (GNUNET_SYSERR ==
211 GNUNET_CRYPTO_public_key_from_string (subject_pkey,
212 &set[i].subject_key))
213 {
214 GNUNET_free (tmp_str);
215 return GNUNET_SYSERR;
216 }
217
218 // If not just key, also set subject attribute (Not A.a <- B but A.a <- B.b)
219 if (2 == matches)
220 {
221 set[i].subject_attribute_len = strlen (attr_str) + 1;
222 set[i].subject_attribute = GNUNET_strdup (attr_str);
223 }
224 // If more entries, then token string can take the next entry (separated by ',') by calling strtok again
225 token = strtok (NULL, ",");
226 }
227 tmp_data_size = GNUNET_ABD_delegation_set_get_size (entries, set);
228
229 if (-1 == tmp_data_size)
230 {
231 GNUNET_free (tmp_str);
232 return GNUNET_SYSERR;
233 }
234 *data_size += tmp_data_size;
235 *data = sets = GNUNET_malloc (*data_size);
236 GNUNET_ABD_delegation_set_serialize (entries,
237 set,
238 tmp_data_size,
239 (char *) &sets[1]);
240 for (i = 0; i < entries; i++)
241 {
242 if (0 != set[i].subject_attribute_len)
243 {
244 GNUNET_free_nz ((char *) set[i].subject_attribute);
245 set[i].subject_attribute = NULL;
246 }
247 }
248 sets->set_count = htonl (entries);
249 sets->data_size = GNUNET_htonll (tmp_data_size);
250
251 GNUNET_free (tmp_str);
252 return GNUNET_OK;
253 }
254 case GNUNET_GNSRECORD_TYPE_DELEGATE:
255 {
256 struct GNUNET_ABD_Delegate *cred;
257 cred = GNUNET_ABD_delegate_from_string (s);
258
259 *data_size = GNUNET_ABD_delegate_serialize (cred, (char **) data);
260 GNUNET_free (cred);
261 return GNUNET_OK;
262 }
263 default:
264 return GNUNET_SYSERR;
265 }
266}
267
268
269/**
270 * Mapping of record type numbers to human-readable
271 * record type names.
272 */
273static struct
274{
275 const char *name;
276 uint32_t number;
277} name_map[] = {{"ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE},
278 {"DEL", GNUNET_GNSRECORD_TYPE_DELEGATE},
279 {NULL, UINT32_MAX}};
280
281
282/**
283 * Convert a type name (e.g. "AAAA") to the corresponding number.
284 *
285 * @param cls closure, unused
286 * @param gns_typename name to convert
287 * @return corresponding number, UINT32_MAX on error
288 */
289static uint32_t
290abd_typename_to_number (void *cls, const char *gns_typename)
291{
292 unsigned int i;
293
294 i = 0;
295 while ((name_map[i].name != NULL) &&
296 (0 != strcasecmp (gns_typename, name_map[i].name)))
297 i++;
298 return name_map[i].number;
299}
300
301
302/**
303 * Convert a type number to the corresponding type string (e.g. 1 to "A")
304 *
305 * @param cls closure, unused
306 * @param type number of a type to convert
307 * @return corresponding typestring, NULL on error
308 */
309static const char *
310abd_number_to_typename (void *cls, uint32_t type)
311{
312 unsigned int i;
313
314 i = 0;
315 while ((name_map[i].name != NULL) && (type != name_map[i].number))
316 i++;
317 return name_map[i].name;
318}
319
320
321/**
322 * Entry point for the plugin.
323 *
324 * @param cls NULL
325 * @return the exported block API
326 */
327void *
328libgnunet_plugin_gnsrecord_abd_init (void *cls)
329{
330 struct GNUNET_GNSRECORD_PluginFunctions *api;
331
332 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
333 api->value_to_string = &abd_value_to_string;
334 api->string_to_value = &abd_string_to_value;
335 api->typename_to_number = &abd_typename_to_number;
336 api->number_to_typename = &abd_number_to_typename;
337 return api;
338}
339
340
341/**
342 * Exit point from the plugin.
343 *
344 * @param cls the return value from #libgnunet_plugin_block_test_init
345 * @return NULL
346 */
347void *
348libgnunet_plugin_gnsrecord_abd_done (void *cls)
349{
350 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
351
352 GNUNET_free (api);
353 return NULL;
354}
355
356
357/* end of plugin_gnsrecord_abd.c */
diff --git a/src/contrib/service/abd/test_abd_bi_and.sh b/src/contrib/service/abd/test_abd_bi_and.sh
new file mode 100755
index 000000000..99c89a344
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_and.sh
@@ -0,0 +1,98 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
22gnunet-arm -s -c test_abd_lookup.conf
23
24gnunet-identity -C a -c test_abd_lookup.conf
25gnunet-identity -C b -c test_abd_lookup.conf
26gnunet-identity -C c -c test_abd_lookup.conf
27gnunet-identity -C d -c test_abd_lookup.conf
28gnunet-identity -C e -c test_abd_lookup.conf
29gnunet-identity -C f -c test_abd_lookup.conf
30gnunet-identity -C g -c test_abd_lookup.conf
31gnunet-identity -C h -c test_abd_lookup.conf
32AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
33BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
34CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
35DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
36EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
37FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
38GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
39HKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep h | awk '{print $3}')
40gnunet-identity -d
41
42# (1) (A.a) <- B.b
43# (2) (B.b) <- C.c AND G.g
44# (3) C.c <- (D.D)
45# (4) D.d <- (E.e)
46# (5) E.e <- (F) priv
47# (6) (G.g) <- H.h
48# (7) H.h <- (F) priv
49
50# BIDIRECTIONAL
51gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
52gnunet-namestore -D -z a
53gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$CKEY c, $GKEY g" --ttl=5m -c test_abd_lookup.conf
54gnunet-namestore -D -z b
55gnunet-abd --createIssuerSide --ego=g --attribute="g" --subject="$HKEY h" --ttl=5m -c test_abd_lookup.conf
56gnunet-namestore -D -z b
57
58SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$DKEY d" --ttl="2049-12-12 10:00:00"`
59gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
60gnunet-namestore -D -z d
61SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY e" --ttl="2049-12-12 10:00:00"`
62gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
63gnunet-namestore -D -z e
64SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="e" --subject="$FKEY" --ttl="2049-12-12 10:00:00"`
65gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
66gnunet-namestore -D -z f
67SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=h --attribute="h" --subject="$FKEY" --ttl="2049-12-12 10:00:00"`
68gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
69gnunet-namestore -D -z h
70
71# Starting to resolve
72echo "+++ Starting to Resolve +++"
73
74DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=f --forward --backward -c test_abd_lookup.conf | paste -d, -s - -`
75echo $DELS
76echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate=\'$DELS\' --forward --backward -c test_abd_lookup.conf
77gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate="$DELS" --forward --backward -c test_abd_lookup.conf
78
79RES=$?
80
81# Cleanup properly
82gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
83gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
84gnunet-namestore -z g -d -n "g" -t ATTR -c test_abd_lookup.conf
85gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
86gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
87gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
88
89gnunet-arm -e -c test_abd_lookup.conf
90
91if [ "$RES" == 0 ]
92then
93 exit 0
94else
95 echo "FAIL: Failed to verify credential."
96 exit 1
97fi
98
diff --git a/src/contrib/service/abd/test_abd_bi_and2.sh b/src/contrib/service/abd/test_abd_bi_and2.sh
new file mode 100755
index 000000000..8d1601ef4
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_and2.sh
@@ -0,0 +1,94 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
22gnunet-arm -s -c test_abd_lookup.conf
23
24gnunet-identity -C a -c test_abd_lookup.conf
25gnunet-identity -C b -c test_abd_lookup.conf
26gnunet-identity -C c -c test_abd_lookup.conf
27gnunet-identity -C d -c test_abd_lookup.conf
28gnunet-identity -C e -c test_abd_lookup.conf
29gnunet-identity -C f -c test_abd_lookup.conf
30gnunet-identity -C g -c test_abd_lookup.conf
31gnunet-identity -C h -c test_abd_lookup.conf
32AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
33BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
34CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
35DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
36EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
37FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
38GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
39HKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep h | awk '{print $3}')
40gnunet-identity -d
41
42# (1) (A.a) <- B.b
43# (2) (B.b) <- C.c AND G.g
44# (3) C.c <- (D.D)
45# (4) D.d <- (E.e)
46# (5) E.e <- (F) priv
47# (6) G.g <- (F) priv
48
49# BIDIRECTIONAL
50gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
51gnunet-namestore -D -z a
52gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$CKEY c, $GKEY g" --ttl=5m -c test_abd_lookup.conf
53gnunet-namestore -D -z b
54
55SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$DKEY d" --ttl="2019-12-12 10:00:00"`
56gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
57gnunet-namestore -D -z d
58SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY e" --ttl="2019-12-12 10:00:00"`
59gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
60gnunet-namestore -D -z e
61SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="e" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
62gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
63gnunet-namestore -D -z f
64SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=g --attribute="g" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
65gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
66gnunet-namestore -D -z h
67
68# Starting to resolve
69echo "+++ Starting to Resolve +++"
70
71DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=f -c test_abd_lookup.conf | paste -d, -s - -`
72echo $DELS
73echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate=\'$DELS\' -c test_abd_lookup.conf
74gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate="$DELS" -c test_abd_lookup.conf
75
76RES=$?
77
78# Cleanup properly
79gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
80gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
81gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
82gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
83gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
84
85gnunet-arm -e -c test_abd_lookup.conf
86
87if [ "$RES" == 0 ]
88then
89 exit 0
90else
91 echo "FAIL: Failed to verify credential."
92 exit 1
93fi
94
diff --git a/src/contrib/service/abd/test_abd_bi_and3.sh b/src/contrib/service/abd/test_abd_bi_and3.sh
new file mode 100755
index 000000000..cde2a020b
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_and3.sh
@@ -0,0 +1,96 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
21gnunet-arm -s -c test_abd_lookup.conf
22
23gnunet-identity -C a -c test_abd_lookup.conf
24gnunet-identity -C b -c test_abd_lookup.conf
25gnunet-identity -C c -c test_abd_lookup.conf
26gnunet-identity -C d -c test_abd_lookup.conf
27gnunet-identity -C e -c test_abd_lookup.conf
28gnunet-identity -C f -c test_abd_lookup.conf
29gnunet-identity -C g -c test_abd_lookup.conf
30gnunet-identity -C h -c test_abd_lookup.conf
31AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
32BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
33CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
34DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
35EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
36FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
37GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
38HKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep h | awk '{print $3}')
39gnunet-identity -d
40
41# (1) (A.a) <- B.b
42# (2) (B.b) <- C.c AND G.g
43# (3) C.c <- (D.d)
44# (4) D.d <- (E.e)
45# (5) E.e <- (F) priv
46# (6) G.g <- (H.h)
47# (7) H.h <- (F) priv
48
49# BIDIRECTIONAL
50gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
51gnunet-namestore -D -z a
52gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$CKEY c, $GKEY g" --ttl=5m -c test_abd_lookup.conf
53gnunet-namestore -D -z b
54
55SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$DKEY d" --ttl="2019-12-12 10:00:00"`
56gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
57gnunet-namestore -D -z d
58SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY e" --ttl="2019-12-12 10:00:00"`
59gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
60gnunet-namestore -D -z e
61SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=g --attribute="g" --subject="$HKEY h" --ttl="2019-12-12 10:00:00"`
62gnunet-abd --createSubjectSide --ego=h --import="$SIGNED"
63gnunet-namestore -D -z h
64SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="e" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
65gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
66SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=h --attribute="h" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
67gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
68gnunet-namestore -D -z f
69
70# Starting to resolve
71echo "+++ Starting to Resolve +++"
72
73DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=f -c test_abd_lookup.conf | paste -d, -s - -`
74echo $DELS
75echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate=\'$DELS\' -c test_abd_lookup.conf
76gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate="$DELS" -c test_abd_lookup.conf
77
78RES=$?
79
80# Cleanup properly
81gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
82gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
83gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
84gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
85gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
86gnunet-namestore -z h -d -n "@" -t DEL -c test_abd_lookup.conf
87
88gnunet-arm -e -c test_abd_lookup.conf
89
90if [ "$RES" == 0 ]
91then
92 exit 0
93else
94 echo "FAIL: Failed to verify credential."
95 exit 1
96fi
diff --git a/src/contrib/service/abd/test_abd_bi_and4.sh b/src/contrib/service/abd/test_abd_bi_and4.sh
new file mode 100755
index 000000000..e8cd6efd6
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_and4.sh
@@ -0,0 +1,83 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
22gnunet-arm -s -c test_abd_lookup.conf
23
24gnunet-identity -C a -c test_abd_lookup.conf
25gnunet-identity -C b -c test_abd_lookup.conf
26gnunet-identity -C c -c test_abd_lookup.conf
27gnunet-identity -C d -c test_abd_lookup.conf
28gnunet-identity -C e -c test_abd_lookup.conf
29gnunet-identity -C f -c test_abd_lookup.conf
30gnunet-identity -C g -c test_abd_lookup.conf
31gnunet-identity -C h -c test_abd_lookup.conf
32AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
33BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
34CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
35DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
36EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
37FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
38GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
39HKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep h | awk '{print $3}')
40gnunet-identity -d
41
42# (1) (A.a) <- B.b
43# (2) (B.b) <- C.c AND G.g
44# (3) C.c <- (F) priv
45# (4) G.g <- (F) priv
46
47# BIDIRECTIONAL
48gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
49gnunet-namestore -D -z a
50gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$CKEY c, $GKEY g" --ttl=5m -c test_abd_lookup.conf
51gnunet-namestore -D -z b
52
53SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=g --attribute="g" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
54gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
55SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
56gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
57gnunet-namestore -D -z f
58
59# Starting to resolve
60echo "+++ Starting to Resolve +++"
61
62DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=f --backward -c test_abd_lookup.conf | paste -d, -s - -`
63echo $DELS
64echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate=\'$DELS\' --backward -c test_abd_lookup.conf
65gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate="$DELS" --backward -c test_abd_lookup.conf
66
67RES=$?
68
69# Cleanup properly
70gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
71gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
72gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
73
74gnunet-arm -e -c test_abd_lookup.conf
75
76if [ "$RES" == 0 ]
77then
78 exit 0
79else
80 echo "FAIL: Failed to verify credential."
81 exit 1
82fi
83
diff --git a/src/contrib/service/abd/test_abd_bi_bw.sh b/src/contrib/service/abd/test_abd_bi_bw.sh
new file mode 100755
index 000000000..6b0e51722
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_bw.sh
@@ -0,0 +1,87 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
22gnunet-arm -s -c test_abd_lookup.conf
23
24gnunet-identity -C a -c test_abd_lookup.conf
25gnunet-identity -C b -c test_abd_lookup.conf
26gnunet-identity -C c -c test_abd_lookup.conf
27gnunet-identity -C d -c test_abd_lookup.conf
28gnunet-identity -C e -c test_abd_lookup.conf
29gnunet-identity -C f -c test_abd_lookup.conf
30gnunet-identity -C g -c test_abd_lookup.conf
31AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
32BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
33CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
34DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
35EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
36FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
37GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
38gnunet-identity -d
39
40# (1) (A.a) <- B.b
41# (2) (B.b) <- C.c
42# (3) C.c <- (D.D)
43# (4) D.d <- (E.e)
44# (5) E.e <- (F)
45
46# BIDIRECTIONAL
47gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
48gnunet-namestore -D -z a
49gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$CKEY c" --ttl=5m -c test_abd_lookup.conf
50gnunet-namestore -D -z b
51
52SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$DKEY d" --ttl="2019-12-12 10:00:00"`
53gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
54gnunet-namestore -D -z d
55SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY e" --ttl="2019-12-12 10:00:00"`
56gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
57gnunet-namestore -D -z e
58SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="e" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
59gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
60gnunet-namestore -D -z f
61
62# Starting to resolve
63echo "+++ Starting to Resolve +++"
64
65DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=f --forward --backward -c test_abd_lookup.conf | paste -d, -s - -`
66echo $DELS
67echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate=\'$DELS\' --forward --backward -c test_abd_lookup.conf
68gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate="$DELS" --forward --backward -c test_abd_lookup.conf
69
70RES=$?
71
72# Cleanup properly
73gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
74gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
75gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
76gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
77gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
78
79gnunet-arm -e -c test_abd_lookup.conf
80
81if [ "$RES" == 0 ]
82then
83 exit 0
84else
85 echo "FAIL: Failed to verify credential."
86 exit 1
87fi
diff --git a/src/contrib/service/abd/test_abd_bi_bw_link.sh b/src/contrib/service/abd/test_abd_bi_bw_link.sh
new file mode 100755
index 000000000..f60f78827
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_bw_link.sh
@@ -0,0 +1,92 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
22gnunet-arm -s -c test_abd_lookup.conf
23
24gnunet-identity -C a -c test_abd_lookup.conf
25gnunet-identity -C b -c test_abd_lookup.conf
26gnunet-identity -C c -c test_abd_lookup.conf
27gnunet-identity -C d -c test_abd_lookup.conf
28gnunet-identity -C e -c test_abd_lookup.conf
29gnunet-identity -C f -c test_abd_lookup.conf
30gnunet-identity -C g -c test_abd_lookup.conf
31AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
32BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
33CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
34DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
35EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
36FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
37GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
38gnunet-identity -d
39
40# (1) (A.a) <- B.b
41# (2) (B.b) <- G.g.c
42# (3) (G.g) <- C
43# (3) C.c <- (D.D)
44# (4) D.d <- (E.e)
45# (5) E.e <- (F)
46
47# BIDIRECTIONAL
48gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
49gnunet-namestore -D -z a
50gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$GKEY g.c" --ttl=5m -c test_abd_lookup.conf
51gnunet-namestore -D -z b
52gnunet-abd --createIssuerSide --ego=g --attribute="g" --subject="$CKEY" --ttl=5m -c test_abd_lookup.conf
53gnunet-namestore -D -z b
54
55SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$DKEY d" --ttl="2019-12-12 10:00:00"`
56gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
57gnunet-namestore -D -z d
58SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY e" --ttl="2019-12-12 10:00:00"`
59gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
60gnunet-namestore -D -z e
61SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="e" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
62gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
63gnunet-namestore -D -z f
64
65# Starting to resolve
66echo "+++ Starting to Resolve +++"
67
68DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=f --forward --backward -c test_abd_lookup.conf | paste -d, -s - -`
69echo $DELS
70echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate=\'$DELS\' --forward --backward -c test_abd_lookup.conf
71gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate="$DELS" --forward --backward -c test_abd_lookup.conf
72
73RES=$?
74
75# Cleanup properly
76gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
77gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
78gnunet-namestore -z g -d -n "g" -t ATTR -c test_abd_lookup.conf
79gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
80gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
81gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
82
83gnunet-arm -e -c test_abd_lookup.conf
84
85if [ "$RES" == 0 ]
86then
87 exit 0
88else
89 echo "FAIL: Failed to verify credential."
90 exit 1
91fi
92
diff --git a/src/contrib/service/abd/test_abd_bi_bw_link2.sh b/src/contrib/service/abd/test_abd_bi_bw_link2.sh
new file mode 100755
index 000000000..b0fb49b8e
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_bw_link2.sh
@@ -0,0 +1,93 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
22gnunet-arm -s -c test_abd_lookup.conf
23
24gnunet-identity -C a -c test_abd_lookup.conf
25gnunet-identity -C b -c test_abd_lookup.conf
26gnunet-identity -C c -c test_abd_lookup.conf
27gnunet-identity -C d -c test_abd_lookup.conf
28gnunet-identity -C e -c test_abd_lookup.conf
29gnunet-identity -C f -c test_abd_lookup.conf
30gnunet-identity -C g -c test_abd_lookup.conf
31AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
32BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
33CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
34DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
35EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
36FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
37GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
38gnunet-identity -d
39
40# (1) (A.a) <- B.b
41# (2) (B.b) <- G.g.c
42# (3) G.g <- (C)
43# (3) C.c <- (D.d)
44# (4) D.d <- (E.e)
45# (5) E.e <- (F)
46
47# BIDIRECTIONAL
48gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
49gnunet-namestore -D -z a
50gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$GKEY g.c" --ttl=5m -c test_abd_lookup.conf
51gnunet-namestore -D -z b
52
53SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=g --attribute="g" --subject="$CKEY" --ttl="2019-12-12 10:00:00"`
54gnunet-abd --createSubjectSide --ego=c --import="$SIGNED"
55gnunet-namestore -D -z c
56SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$DKEY d" --ttl="2019-12-12 10:00:00"`
57gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
58gnunet-namestore -D -z d
59SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY e" --ttl="2019-12-12 10:00:00"`
60gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
61gnunet-namestore -D -z e
62SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="e" --subject="$FKEY" --ttl="2019-12-12 10:00:00"`
63gnunet-abd --createSubjectSide --ego=f --import="$SIGNED" --private
64gnunet-namestore -D -z f
65
66# Starting to resolve
67echo "+++ Starting to Resolve +++"
68
69DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=f --forward --backward -c test_abd_lookup.conf | paste -d, -s - -`
70echo $DELS
71echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate=\'$DELS\' --forward --backward -c test_abd_lookup.conf
72gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$FKEY --delegate="$DELS" --forward --backward -c test_abd_lookup.conf
73
74RES=$?
75
76# Cleanup properly
77gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
78gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
79gnunet-namestore -z c -d -n "@" -t DEL -c test_abd_lookup.conf
80gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
81gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
82gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
83
84gnunet-arm -e -c test_abd_lookup.conf
85
86if [ "$RES" == 0 ]
87then
88 exit 0
89else
90 echo "FAIL: Failed to verify credential."
91 exit 1
92fi
93
diff --git a/src/contrib/service/abd/test_abd_bi_fw.sh b/src/contrib/service/abd/test_abd_bi_fw.sh
new file mode 100755
index 000000000..75a940fbe
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_bi_fw.sh
@@ -0,0 +1,92 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
22gnunet-arm -s -c test_abd_lookup.conf
23
24gnunet-identity -C a -c test_abd_lookup.conf
25gnunet-identity -C b -c test_abd_lookup.conf
26gnunet-identity -C c -c test_abd_lookup.conf
27gnunet-identity -C d -c test_abd_lookup.conf
28gnunet-identity -C e -c test_abd_lookup.conf
29gnunet-identity -C f -c test_abd_lookup.conf
30gnunet-identity -C g -c test_abd_lookup.conf
31AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
32BKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep b | awk '{print $3}')
33CKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep c | awk '{print $3}')
34DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
35EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
36FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
37GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
38gnunet-identity -d
39
40# (1) (A.a) <- B.b
41# (2) (B.b) <- C.c
42# (3) C.c <- (D.D)
43# (4) D.d <- (E.e)
44# (5) E.e <- (F.f)
45# (6) F.f <- (G)
46
47# BIDIRECTIONAL
48gnunet-abd --createIssuerSide --ego=a --attribute="a" --subject="$BKEY b" --ttl=5m -c test_abd_lookup.conf
49gnunet-namestore -D -z a
50gnunet-abd --createIssuerSide --ego=b --attribute="b" --subject="$CKEY c" --ttl=5m -c test_abd_lookup.conf
51gnunet-namestore -D -z b
52
53SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=c --attribute="c" --subject="$DKEY d" --ttl="2019-12-12 10:00:00"`
54gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
55gnunet-namestore -D -z d
56SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY e" --ttl="2019-12-12 10:00:00"`
57gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
58gnunet-namestore -D -z e
59SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="e" --subject="$FKEY f" --ttl="2019-12-12 10:00:00"`
60gnunet-abd --createSubjectSide --ego=f --import="$SIGNED"
61gnunet-namestore -D -z f
62SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=f --attribute="f" --subject="$GKEY" --ttl="2019-12-12 10:00:00"`
63gnunet-abd --createSubjectSide --ego=g --import="$SIGNED" --private
64gnunet-namestore -D -z g
65
66# Starting to resolve
67echo "+++ Starting to Resolve +++"
68
69DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=g --forward --backward -c test_abd_lookup.conf | paste -d, -s - -`
70echo $DELS
71echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$GKEY --delegate=\'$DELS\' --forward --backward -c test_abd_lookup.conf
72gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$GKEY --delegate="$DELS" --forward --backward -c test_abd_lookup.conf
73
74RES=$?
75
76# Cleanup properly
77gnunet-namestore -z a -d -n "a" -t ATTR -c test_abd_lookup.conf
78gnunet-namestore -z b -d -n "b" -t ATTR -c test_abd_lookup.conf
79gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
80gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
81gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
82gnunet-namestore -z g -d -n "@" -t DEL -c test_abd_lookup.conf
83
84gnunet-arm -e -c test_abd_lookup.conf
85
86if [ "$RES" == 0 ]
87then
88 exit 0
89else
90 echo "FAIL: Failed to verify credential."
91 exit 1
92fi
diff --git a/src/contrib/service/abd/test_abd_defaults.conf b/src/contrib/service/abd/test_abd_defaults.conf
new file mode 100644
index 000000000..7b4cb576d
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_defaults.conf
@@ -0,0 +1,24 @@
1@INLINE@ ../../contrib/conf/gnunet/no_forcestart.conf
2
3[PATHS]
4GNUNET_TEST_HOME = $GNUNET_TMP/test-gnunet-abd-testing/
5
6[namestore-sqlite]
7FILENAME = $GNUNET_TEST_HOME/namestore/sqlite_test.db
8
9[namecache-sqlite]
10FILENAME=$GNUNET_TEST_HOME/namecache/namecache.db
11
12[identity]
13# Directory where we store information about our egos
14EGODIR = $GNUNET_TEST_HOME/identity/egos/
15
16[dhtcache]
17DATABASE = heap
18
19[transport]
20PLUGINS = tcp
21
22[transport-tcp]
23BINDTO = 127.0.0.1
24
diff --git a/src/contrib/service/abd/test_abd_issue.sh b/src/contrib/service/abd/test_abd_issue.sh
new file mode 100755
index 000000000..8076aa921
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_issue.sh
@@ -0,0 +1,46 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18# (1) PKEY1.user -> PKEY2.resu.user
19# (2) PKEY2.resu -> PKEY3
20# (3) PKEY3.user -> PKEY4
21
22
23which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 30"
24
25TEST_ATTR="test"
26gnunet-arm -s -c test_abd_lookup.conf
27gnunet-identity -C testissuer -c test_abd_lookup.conf
28gnunet-identity -C testsubject -c test_abd_lookup.conf
29SUBJECT_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep testsubject | awk '{print $3}')
30ISSUER_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep testissuer | awk '{print $3}')
31
32# Get abd and store it with subject (3)
33SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=testissuer --attribute=$TEST_ATTR --subject=$SUBJECT_KEY --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf`
34
35STATUS=$?
36
37if test $? != 0
38then
39 echo "Error issuing..."
40 exit 1
41fi
42#Try import
43$DO_TIMEOUT gnunet-abd --createSubjectSide --ego=testsubject --import="$SIGNED" --private -c test_abd_lookup.conf
44RES=$?
45gnunet-arm -e -c test_abd_lookup.conf
46exit $RES
diff --git a/src/contrib/service/abd/test_abd_lookup.conf b/src/contrib/service/abd/test_abd_lookup.conf
new file mode 100644
index 000000000..4072ba23b
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_lookup.conf
@@ -0,0 +1,28 @@
1@INLINE@ test_abd_defaults.conf
2
3[PATHS]
4GNUNET_TEST_HOME = $GNUNET_TMP/test-gnunet-abd-peer-1/
5
6[dht]
7START_ON_DEMAND = YES
8
9[transport]
10PLUGINS =
11
12[abd]
13START_ON_DEMAND = YES
14#PREFIX = valgrind --leak-check=full --track-origins=yes --log-file=/tmp/credlog
15
16[rest]
17PREFIX = valgrind --leak-check=full --track-origins=yes --log-file=$GNUNET_TMP/restlog
18
19[gns]
20#PREFIX = valgrind --leak-check=full --track-origins=yes
21START_ON_DEMAND = YES
22AUTO_IMPORT_PKEY = YES
23MAX_PARALLEL_BACKGROUND_QUERIES = 10
24DEFAULT_LOOKUP_TIMEOUT = 15 s
25RECORD_PUT_INTERVAL = 1 h
26ZONE_PUBLISH_TIME_WINDOW = 1 h
27DNS_ROOT=PD67SGHF3E0447TU9HADIVU9OM7V4QHTOG0EBU69TFRI2LG63DR0
28
diff --git a/src/contrib/service/abd/test_abd_own.sh b/src/contrib/service/abd/test_abd_own.sh
new file mode 100755
index 000000000..3fc210284
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_own.sh
@@ -0,0 +1,139 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18
19
20which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 10"
21gnunet-arm -s -c test_abd_lookup.conf
22
23gnunet-identity -C a -c test_abd_lookup.conf
24gnunet-identity -C d -c test_abd_lookup.conf
25gnunet-identity -C e -c test_abd_lookup.conf
26gnunet-identity -C f -c test_abd_lookup.conf
27gnunet-identity -C g -c test_abd_lookup.conf
28AKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep a | awk '{print $3}')
29DKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep d | awk '{print $3}')
30EKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep e | awk '{print $3}')
31FKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep f | awk '{print $3}')
32GKEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep g | awk '{print $3}')
33
34############################################################################################
35# (1) EPub.discount <- EOrg.preferred
36# (2) EOrg.preferred <- StateU.student
37# (3) StateU.student <- RegistrarB.student
38# (4) RegistrarB.student <- Alice
39
40gnunet-identity -C epub -c test_abd_lookup.conf
41gnunet-identity -C eorg -c test_abd_lookup.conf
42gnunet-identity -C stateu -c test_abd_lookup.conf
43gnunet-identity -C registrarb -c test_abd_lookup.conf
44gnunet-identity -C alice -c test_abd_lookup.conf
45
46EPUB_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep epub | awk '{print $3}')
47EORG_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep eorg | awk '{print $3}')
48STATEU_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep stateu | awk '{print $3}')
49REGISTRARB_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep registrarb | awk '{print $3}')
50ALICE_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep alice | awk '{print $3}')
51
52
53DISC_ATTR="discount"
54PREF_ATTR="preferred"
55STATE_STUD_ATTR="student"
56REG_STUD_ATTR="student"
57END_ATTR="end"
58
59gnunet-identity -d
60
61# FORWARD, subject side stored (different constellations)
62SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=a --attribute="a" --subject="$AKEY b.c" --ttl="2019-12-12 10:00:00"`
63gnunet-abd --createSubjectSide --ego=a --import="$SIGNED"
64gnunet-namestore -D -z a
65
66SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=a --attribute="b" --subject="$DKEY d" --ttl="2019-12-12 10:00:00"`
67gnunet-abd --createSubjectSide --ego=d --import="$SIGNED"
68gnunet-namestore -D -z d
69
70SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="d" --subject="$EKEY" --ttl="2019-12-12 10:00:00"`
71gnunet-abd --createSubjectSide --ego=e --import="$SIGNED"
72gnunet-namestore -D -z e
73
74SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="c" --subject="$FKEY c" --ttl="2019-12-12 10:00:00"`
75gnunet-abd --createSubjectSide --ego=f --import="$SIGNED"
76SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=e --attribute="k" --subject="$FKEY c.k" --ttl="2019-12-12 10:00:00"`
77gnunet-abd --createSubjectSide --ego=f --import="$SIGNED"
78gnunet-namestore -D -z f
79
80SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=f --attribute="c" --subject="$GKEY" --ttl="2019-12-12 10:00:00"`
81gnunet-abd --createSubjectSide --ego=g --import="$SIGNED" --private
82SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=a --attribute="c" --subject="$GKEY" --ttl="2019-12-12 10:00:00"`
83gnunet-abd --createSubjectSide --ego=g --import="$SIGNED" --private
84SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=d --attribute="h.o" --subject="$GKEY" --ttl="2019-12-12 10:00:00"`
85gnunet-abd --createSubjectSide --ego=g --import="$SIGNED"
86gnunet-namestore -D -z g
87
88
89# BACKWARD, issuer side stored
90# (1) EPub assigns the attribute "discount" to all entities that have been assigned "preferred" by EOrg
91gnunet-abd --createIssuerSide --ego=epub --attribute=$DISC_ATTR --subject="$EORG_KEY $PREF_ATTR" --ttl=5m -c test_abd_lookup.conf
92
93# (2) EOrg assigns the attribute "preferred" to all entities that have been assigned "student" by StateU
94gnunet-abd --createIssuerSide --ego=eorg --attribute=$PREF_ATTR --subject="$STATEU_KEY $STATE_STUD_ATTR" --ttl=5m -c test_abd_lookup.conf
95
96# (3) StateU assigns the attribute "student" to all entities that have been asssigned "student" by RegistrarB
97gnunet-abd --createIssuerSide --ego=stateu --attribute=$STATE_STUD_ATTR --subject="$REGISTRARB_KEY $REG_STUD_ATTR" --ttl=5m -c test_abd_lookup.conf
98
99# (4) RegistrarB issues Alice the credential "student"
100SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=registrarb --attribute="$REG_STUD_ATTR" --subject="$ALICE_KEY" --ttl="2019-12-12 10:00:00"`
101gnunet-abd --createSubjectSide --ego=alice --import="$SIGNED" --private
102
103# Starting to resolve
104echo "+++ Starting to Resolve +++"
105
106# FORWARD
107#DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$AKEY --attribute="a" --ego=g --forward -c test_abd_lookup.conf | paste -d, -s - -`
108#echo $DELS
109#echo gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$GKEY --delegate=\'$DELS\' --forward -c test_abd_lookup.conf
110#RES_DELS=`gnunet-abd --verify --issuer=$AKEY --attribute="a" --subject=$GKEY --delegate="$DELS" --forward -c test_abd_lookup.conf`
111
112# BACKWARD
113DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$EPUB_KEY --attribute=$DISC_ATTR --ego=alice --backward -c test_abd_lookup.conf | paste -d, -s - -`
114echo $DELS
115echo gnunet-abd --verify --issuer=$EPUB_KEY --attribute=$DISC_ATTR --subject=$ALICE_KEY --delegate=\'$DELS\' --backward -c test_abd_lookup.conf
116gnunet-abd --verify --issuer=$EPUB_KEY --attribute=$DISC_ATTR --subject=$ALICE_KEY --delegate="$DELS" --backward -c test_abd_lookup.conf
117
118RES=$?
119
120# Cleanup properly
121gnunet-namestore -z epub -d -n $DISC_ATTR -t ATTR -c test_abd_lookup.conf
122gnunet-namestore -z eorg -d -n $PREF_ATTR -t ATTR -c test_abd_lookup.conf
123gnunet-namestore -z stateu -d -n $STATE_STUD_ATTR -t ATTR -c test_abd_lookup.conf
124#gnunet-namestore -z a -d -n "@" -t DEL -c test_abd_lookup.conf
125#gnunet-namestore -z d -d -n "@" -t DEL -c test_abd_lookup.conf
126#gnunet-namestore -z e -d -n "@" -t DEL -c test_abd_lookup.conf
127#gnunet-namestore -z f -d -n "@" -t DEL -c test_abd_lookup.conf
128#gnunet-namestore -z g -d -n "@" -t DEL -c test_abd_lookup.conf
129
130
131gnunet-arm -e -c test_abd_lookup.conf
132
133if [ "$RES" == 0 ]
134then
135 exit 0
136else
137 echo "FAIL: Failed to verify credential."
138 exit 1
139fi
diff --git a/src/contrib/service/abd/test_abd_verify.sh b/src/contrib/service/abd/test_abd_verify.sh
new file mode 100755
index 000000000..9ece91c62
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_verify.sh
@@ -0,0 +1,87 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18# (1) Service.user -> GNU.project.member
19# (2) GNU.project -> GNUnet
20# (3) GNUnet.member -> GNUnet.developer
21# (4) GNUnet.member -> GNUnet.user
22# (5) GNUnet.developer -> Alice
23
24
25which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 30"
26gnunet-arm -s -c test_abd_lookup.conf
27gnunet-identity -C service -c test_abd_lookup.conf
28gnunet-identity -C alice -c test_abd_lookup.conf
29gnunet-identity -C gnu -c test_abd_lookup.conf
30gnunet-identity -C gnunet -c test_abd_lookup.conf
31
32GNU_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep gnu | grep -v gnunet | awk '{print $3}')
33ALICE_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep alice | awk '{print $3}')
34GNUNET_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep gnunet | awk '{print $3}')
35SERVICE_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep service | awk '{print $3}')
36
37USER_ATTR="user"
38GNU_PROJECT_ATTR="project"
39MEMBER_ATTR="member"
40DEVELOPER_ATTR="developer"
41DEV_ATTR="developer"
42TEST_CREDENTIAL="mygnunetcreds"
43
44gnunet-identity -d
45
46# (1) A service assigns the attribute "user" to all entities that have been assigned "member" by entities that werde assigned "project" from GNU
47gnunet-abd --createIssuerSide --ego=service --attribute="$USER_ATTR" --subject="$GNU_KEY $GNU_PROJECT_ATTR.$MEMBER_ATTR" --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf
48gnunet-namestore -D -z service
49
50# (2) GNU recognized GNUnet as a GNU project and delegates the "project" attribute
51gnunet-abd --createIssuerSide --ego=gnu --attribute="$GNU_PROJECT_ATTR" --subject="$GNUNET_KEY" --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf
52gnunet-namestore -D -z gnu
53
54# (3+4) GNUnet assigns the attribute "member" to all entities gnunet has also assigned "developer" or "user"
55gnunet-abd --createIssuerSide --ego=gnunet --attribute="$MEMBER_ATTR" --subject="$GNUNET_KEY $DEVELOPER_ATTR" --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf
56gnunet-abd --createIssuerSide --ego=gnunet --attribute="$MEMBER_ATTR" --subject="$GNUNET_KEY $USER_ATTR" --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf
57gnunet-namestore -D -z gnunet
58
59# (5) GNUnet signes the delegate and Alice stores it
60SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=gnunet --attribute=$DEV_ATTR --subject=$ALICE_KEY --ttl="2019-12-12 10:00:00"`
61gnunet-abd --createSubjectSide --ego=alice --import="$SIGNED" --private
62gnunet-namestore -D -z alice
63
64# Starting to resolve
65echo "+++ Starting to Resolve +++"
66
67DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$SERVICE_KEY --attribute=$USER_ATTR --ego=alice --backward -c test_abd_lookup.conf | paste -d, -s - -`
68echo $DELS
69echo gnunet-abd --verify --issuer=$SERVICE_KEY --attribute=$USER_ATTR --subject=$ALICE_KEY --delegate=\'$DELS\' --backward -c test_abd_lookup.conf
70gnunet-abd --verify --issuer=$SERVICE_KEY --attribute=$USER_ATTR --subject=$ALICE_KEY --delegate="$DELS" --backward -c test_abd_lookup.conf
71
72RES=$?
73
74# Cleanup properly
75gnunet-namestore -z alice -d -n "@" -t DEL -c test_abd_lookup.conf
76gnunet-namestore -z gnu -d -n $GNU_PROJECT_ATTR -t ATTR -c test_abd_lookup.conf
77gnunet-namestore -z gnunet -d -n $MEMBER_ATTR -t ATTR -c test_abd_lookup.conf
78gnunet-namestore -z service -d -n $USER_ATTR -t ATTR -c test_abd_lookup.conf
79gnunet-arm -e -c test_abd_lookup.conf
80
81if [ "$RES" == 0 ]
82then
83 exit 0
84else
85 echo "FAIL: Failed to verify credential."
86 exit 1
87fi
diff --git a/src/contrib/service/abd/test_abd_verify_and.sh b/src/contrib/service/abd/test_abd_verify_and.sh
new file mode 100755
index 000000000..c6287055e
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_verify_and.sh
@@ -0,0 +1,86 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18# (1) Service.user -> GNU.project.member
19# (2) GNU.project -> GNUnet
20# (3) GNUnet.member -> GNUnet.developer AND GNUnet.user
21# (4) GNUnet.developer -> Alice
22
23
24which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 30"
25gnunet-arm -s -c test_abd_lookup.conf
26gnunet-identity -C service -c test_abd_lookup.conf
27gnunet-identity -C alice -c test_abd_lookup.conf
28gnunet-identity -C gnu -c test_abd_lookup.conf
29gnunet-identity -C gnunet -c test_abd_lookup.conf
30
31GNU_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep gnu | grep -v gnunet | awk '{print $3}')
32ALICE_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep alice | awk '{print $3}')
33GNUNET_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep gnunet | awk '{print $3}')
34SERVICE_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep service | awk '{print $3}')
35
36USER_ATTR="user"
37GNU_PROJECT_ATTR="project"
38MEMBER_ATTR="member"
39DEVELOPER_ATTR="developer"
40DEV_ATTR="developer"
41
42gnunet-identity -d
43
44# (1) A service assigns the attribute "user" to all entities that have been assigned "member" by entities that werde assigned "project" from GNU
45gnunet-abd --createIssuerSide --ego=service --attribute="$USER_ATTR" --subject="$GNU_KEY $GNU_PROJECT_ATTR.$MEMBER_ATTR" --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf
46gnunet-namestore -D -z service
47
48# (2) GNU recognized GNUnet as a GNU project and delegates the "project" attribute
49gnunet-abd --createIssuerSide --ego=gnu --attribute="$GNU_PROJECT_ATTR" --subject="$GNUNET_KEY" --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf
50gnunet-namestore -D -z gnu
51
52# (3+4) GNUnet assigns the attribute "member" to all entities gnunet has also assigned "developer" or "user"
53gnunet-abd --createIssuerSide --ego=gnunet --attribute="$MEMBER_ATTR" --subject="$GNUNET_KEY $DEVELOPER_ATTR, $GNUNET_KEY $USER_ATTR" --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf
54gnunet-namestore -D -z gnunet
55
56# (5) GNUnet signes the delegates and Alice stores it
57SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=gnunet --attribute=$DEV_ATTR --subject=$ALICE_KEY --ttl="2019-12-12 10:00:00"`
58gnunet-abd --createSubjectSide --ego=alice --import="$SIGNED" --private
59SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=gnunet --attribute=$USER_ATTR --subject=$ALICE_KEY --ttl="2019-12-12 10:00:00"`
60gnunet-abd --createSubjectSide --ego=alice --import="$SIGNED" --private
61gnunet-namestore -D -z alice
62
63# Starting to resolve
64echo "+++ Starting to Resolve +++"
65
66DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$SERVICE_KEY --attribute=$USER_ATTR --ego=alice --backward -c test_abd_lookup.conf | paste -d, -s - -`
67echo $DELS
68echo gnunet-abd --verify --issuer=$SERVICE_KEY --attribute=$USER_ATTR --subject=$ALICE_KEY --delegate=\'$DELS\' --backward -c test_abd_lookup.conf
69gnunet-abd --verify --issuer=$SERVICE_KEY --attribute=$USER_ATTR --subject=$ALICE_KEY --delegate="$DELS" --backward -c test_abd_lookup.conf
70
71RES=$?
72
73# Cleanup properly
74gnunet-namestore -z alice -d -n "@" -t DEL -c test_abd_lookup.conf
75gnunet-namestore -z gnu -d -n $GNU_PROJECT_ATTR -t ATTR -c test_abd_lookup.conf
76gnunet-namestore -z gnunet -d -n $MEMBER_ATTR -t ATTR -c test_abd_lookup.conf
77gnunet-namestore -z service -d -n $USER_ATTR -t ATTR -c test_abd_lookup.conf
78gnunet-arm -e -c test_abd_lookup.conf
79
80if [ "$RES" == 0 ]
81then
82 exit 0
83else
84 echo "FAIL: Failed to verify credentials."
85 exit 1
86fi
diff --git a/src/contrib/service/abd/test_abd_verify_simple.sh b/src/contrib/service/abd/test_abd_verify_simple.sh
new file mode 100755
index 000000000..e917f8793
--- /dev/null
+++ b/src/contrib/service/abd/test_abd_verify_simple.sh
@@ -0,0 +1,56 @@
1#!/usr/bin/env bash
2trap "gnunet-arm -e -c test_abd_lookup.conf" SIGINT
3
4LOCATION=$(which gnunet-config)
5if [ -z $LOCATION ]
6then
7 LOCATION="gnunet-config"
8fi
9$LOCATION --version 1> /dev/null
10if test $? != 0
11then
12 echo "GNUnet command line tools cannot be found, check environmental variables PATH and GNUNET_PREFIX"
13 exit 77
14fi
15
16rm -rf `gnunet-config -c test_abd_lookup.conf -s PATHS -o GNUNET_HOME -f`
17
18# (1) Issuer.user -> Subject
19
20
21which timeout > /dev/null 2>&1 && DO_TIMEOUT="timeout 30"
22gnunet-arm -s -c test_abd_lookup.conf
23gnunet-identity -C testissuer -c test_abd_lookup.conf
24gnunet-identity -C testsubject -c test_abd_lookup.conf
25
26TEST_ATTR="user"
27SUBJECT_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep testsubject | awk '{print $3}')
28ISSUER_KEY=$(gnunet-identity -d -c test_abd_lookup.conf | grep testissuer | awk '{print $3}')
29
30gnunet-identity -d
31
32# Create delegate (1)
33SIGNED=`$DO_TIMEOUT gnunet-abd --signSubjectSide --ego=testissuer --attribute=$TEST_ATTR --subject=$SUBJECT_KEY --ttl="2019-12-12 10:00:00" -c test_abd_lookup.conf`
34gnunet-abd --createSubjectSide --ego=testsubject --import="$SIGNED" --private
35gnunet-namestore -D -z testsubject
36
37# Starting to resolve
38echo "+++ Starting to Resolve +++"
39
40DELS=`$DO_TIMEOUT gnunet-abd --collect --issuer=$ISSUER_KEY --attribute=$TEST_ATTR --ego=testsubject -c test_abd_lookup.conf | paste -d, -s - -`
41echo $DELS
42gnunet-abd --verify --issuer=$ISSUER_KEY --attribute=$TEST_ATTR --subject=$SUBJECT_KEY --delegate="$DELS" -c test_abd_lookup.conf
43
44RES=$?
45
46# Cleanup properly
47gnunet-namestore -z testsubject -d -n "@" -t DEL -c test_abd_lookup.conf
48gnunet-arm -e -c test_abd_lookup.conf
49
50if [ "$RES" == 0 ]
51then
52 exit 0
53else
54 echo "FAIL: Failed to verify credential."
55 exit 1
56fi \ No newline at end of file
diff --git a/src/contrib/service/auction/.gitignore b/src/contrib/service/auction/.gitignore
new file mode 100644
index 000000000..5c3ec20cb
--- /dev/null
+++ b/src/contrib/service/auction/.gitignore
@@ -0,0 +1,5 @@
1gnunet-auction-create
2gnunet-auction-info
3gnunet-auction-join
4gnunet-service-auction
5test_auction_api
diff --git a/src/contrib/service/auction/Makefile.am b/src/contrib/service/auction/Makefile.am
new file mode 100644
index 000000000..ec0ffaf33
--- /dev/null
+++ b/src/contrib/service/auction/Makefile.am
@@ -0,0 +1,75 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4pkgcfgdir = $(pkgdatadir)/config.d/
5
6libexecdir = $(pkglibdir)/libexec/
7
8
9pkgcfg_DATA = \
10 auction.conf
11
12if USE_COVERAGE
13 AM_CFLAGS = -fprofile-arcs -ftest-coverage
14endif
15
16
17libexec_PROGRAMS = \
18 gnunet-service-auction
19
20gnunet_service_auction_SOURCES = \
21 gnunet-service-auction.c
22gnunet_service_auction_LDADD = \
23 $(top_builddir)/src/lib/util/libgnunetutil.la \
24 -ljansson \
25 $(GN_LIBINTL)
26
27
28bin_PROGRAMS = \
29 gnunet-auction-create \
30 gnunet-auction-info \
31 gnunet-auction-join
32
33gnunet_auction_create_SOURCES = \
34 gnunet-auction-create.c
35gnunet_auction_create_LDADD = \
36 $(top_builddir)/src/lib/util/libgnunetutil.la \
37 -ljansson \
38 $(GN_LIBINTL)
39
40gnunet_auction_info_SOURCES = \
41 gnunet-auction-info.c
42gnunet_auction_info_LDADD = \
43 $(top_builddir)/src/lib/util/libgnunetutil.la \
44 -ljansson \
45 $(GN_LIBINTL)
46
47gnunet_auction_join_SOURCES = \
48 gnunet-auction-join.c
49gnunet_auction_join_LDADD = \
50 $(top_builddir)/src/lib/util/libgnunetutil.la \
51 -ljansson \
52 $(GN_LIBINTL)
53
54
55check_PROGRAMS = \
56 test_auction_api
57
58test_auction_api_SOURCES = \
59 test_auction_api.c
60test_auction_api_LDADD = \
61 $(top_builddir)/src/lib/util/libgnunetutil.la
62
63
64check_SCRIPTS = \
65 test_auction_create.sh
66
67EXTRA_DIST = \
68 auction.h \
69 auction.conf \
70 $(check_SCRIPTS)
71
72if ENABLE_TEST_RUN
73 AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME;
74 TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
75endif
diff --git a/src/contrib/service/auction/auction.conf b/src/contrib/service/auction/auction.conf
new file mode 100644
index 000000000..deb9ac1fc
--- /dev/null
+++ b/src/contrib/service/auction/auction.conf
@@ -0,0 +1,4 @@
1[auction]
2START_ON_DEMAND = NO
3BINARY = gnunet-service-auction
4UNIXPATH = $GNUNET_RUNTIME_DIR/gnunet-service-auction.sock
diff --git a/src/contrib/service/auction/auction.h b/src/contrib/service/auction/auction.h
new file mode 100644
index 000000000..dc8b933f6
--- /dev/null
+++ b/src/contrib/service/auction/auction.h
@@ -0,0 +1,77 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001-2011 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 * @author Markus Teich
23 * @file auction/auction.h
24 *
25 * @brief Common type definitions for the auction service and API.
26 */
27#ifndef AUCTION_H
28#define AUCTION_H
29
30#include "gnunet_common.h"
31
32GNUNET_NETWORK_STRUCT_BEGIN
33
34/**
35 * Auction creation request sent from the client to the service
36 */
37struct GNUNET_AUCTION_ClientCreateMessage
38{
39 /**
40 * Type: GNUNET_MESSAGE_TYPE_AUCTION_CLIENT_CREATE
41 */
42 struct GNUNET_MessageHeader header;
43
44 /**
45 * When should the auction start
46 */
47 struct GNUNET_TIME_AbsoluteNBO time_start;
48
49 /**
50 * How long is each round allowed to be maximally
51 */
52 struct GNUNET_TIME_RelativeNBO time_round;
53
54 /**
55 * Auction parameter m.
56 * 0 for first price auctions.
57 * >0 for M+1st price auctions.
58 */
59 uint16_t m GNUNET_PACKED;
60
61 /**
62 * Should the auction outcome be public?
63 * 0 for private outcome auctions.
64 * 1 for public outcome auctions.
65 */
66 uint16_t outcome_public GNUNET_PACKED;
67
68 /**
69 * TODO: Price mapping.
70 */
71
72 /* DESCRIPTION text copied to end of this message */
73};
74
75GNUNET_NETWORK_STRUCT_END
76
77#endif
diff --git a/src/contrib/service/auction/gnunet-auction-create.c b/src/contrib/service/auction/gnunet-auction-create.c
new file mode 100644
index 000000000..93947b89c
--- /dev/null
+++ b/src/contrib/service/auction/gnunet-auction-create.c
@@ -0,0 +1,219 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 auction/gnunet-auction-create.c
23 * @brief tool to create a new auction
24 * @author Markus Teich
25 */
26#include "platform.h"
27
28#include <float.h>
29
30#include "gnunet_util_lib.h"
31#include <jansson.h>
32/* #include "gnunet_auction_service.h" */
33
34#define FIRST_PRICE 0
35#define OUTCOME_PRIVATE 0
36#define OUTCOME_PUBLIC 1
37
38static int ret; /** Final status code. */
39static char *fndesc; /** filename of the item description */
40static char *fnprices; /** filename of the price map */
41static struct GNUNET_TIME_Relative dround; /** max round duration */
42static struct GNUNET_TIME_Relative dstart; /** time until auction starts */
43static unsigned int m = FIRST_PRICE; /** auction parameter m */
44static int outcome = OUTCOME_PRIVATE; /** outcome */
45static int interactive; /** keep running in foreground */
46
47
48/**
49 * Main function that will be run by the scheduler.
50 *
51 * @param cls closure
52 * @param args remaining command-line arguments
53 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
54 * @param cfg configuration
55 */
56static void
57run (void *cls,
58 char *const *args,
59 const char *cfgfile,
60 const struct GNUNET_CONFIGURATION_Handle *cfg)
61{
62 unsigned int i;
63 double cur, prev = DBL_MAX;
64 json_t *pmap;
65 json_t *parray;
66 json_t *pnode;
67 json_error_t jerr;
68
69 /* cmdline parsing */
70 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dstart.rel_value_us)
71 {
72 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
73 "required argument --regtime missing or invalid (zero)\n");
74 goto fail;
75 }
76 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dround.rel_value_us)
77 {
78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
79 "required argument --roundtime missing or invalid (zero)\n");
80 goto fail;
81 }
82 if (! fndesc)
83 {
84 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
85 "required argument --description missing\n");
86 goto fail;
87 }
88 if (! fnprices)
89 {
90 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
91 "required argument --pricemap missing\n");
92 goto fail;
93 }
94
95 /* parse and check pricemap validity */
96 if (! (pmap = json_load_file (fnprices, JSON_DECODE_INT_AS_REAL, &jerr)))
97 {
98 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99 "parsing pricemap json at %d:%d: %s\n",
100 jerr.line, jerr.column, jerr.text);
101 goto fail;
102 }
103 if (-1 == json_unpack_ex (pmap, &jerr, JSON_VALIDATE_ONLY,
104 "{s:s, s:[]}", "currency", "prices"))
105 {
106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107 "validating pricemap: %s\n", jerr.text);
108 goto fail;
109 }
110 if (! (parray = json_object_get (pmap, "prices")) || ! json_is_array (parray))
111 {
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113 "could not get `prices` array node from pricemap\n");
114 goto fail;
115 }
116 if (0 == json_array_size (parray))
117 {
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "empty pricemap array\n");
119 goto fail;
120 }
121 json_array_foreach (parray, i, pnode)
122 {
123 if (-1 == json_unpack_ex (pnode, &jerr, 0, "F", &cur))
124 {
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126 "validating pricearray index %d: %s\n", i, jerr.text);
127 goto fail;
128 }
129 if (prev <= cur)
130 {
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 "validating pricearray index %d: "
133 "prices must be strictly monotonically decreasing\n",
134 i);
135 goto fail;
136 }
137 prev = cur;
138 }
139
140 return;
141
142fail:
143 ret = 1;
144 return;
145}
146
147
148/**
149 * The main function.
150 *
151 * @param argc number of arguments from the command line
152 * @param argv command line arguments
153 * @return 0 ok, 1 on error
154 */
155int
156main (int argc, char *const *argv)
157{
158 struct GNUNET_GETOPT_CommandLineOption options[] = {
159 GNUNET_GETOPT_option_filename ('d',
160 "description",
161 "FILE",
162 gettext_noop (
163 "description of the item to be sold"),
164 &fndesc),
165
166 GNUNET_GETOPT_option_filename ('p',
167 "pricemap",
168 "FILE",
169 gettext_noop ("mapping of possible prices"),
170 &fnprices),
171
172 GNUNET_GETOPT_option_relative_time ('r',
173 "roundtime",
174 "DURATION",
175 gettext_noop ("max duration per round"),
176 &dround),
177
178 GNUNET_GETOPT_option_relative_time ('s',
179 "regtime",
180 "DURATION",
181 gettext_noop (
182 "duration until auction starts"),
183 &dstart),
184 GNUNET_GETOPT_option_uint ('m',
185 "m",
186 "NUMBER",
187 gettext_noop ("number of items to sell\n"
188 "0 for first price auction\n"
189 ">0 for vickrey/M+1st price auction"),
190 &m),
191
192 GNUNET_GETOPT_option_flag ('u',
193 "public",
194 gettext_noop ("public auction outcome"),
195 &outcome),
196
197 GNUNET_GETOPT_option_flag ('i',
198 "interactive",
199 gettext_noop (
200 "keep running in foreground until auction completes"),
201 &interactive),
202
203 GNUNET_GETOPT_OPTION_END
204 };
205
206 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
207 return 2;
208
209 ret = (GNUNET_OK ==
210 GNUNET_PROGRAM_run (argc, argv,
211 "gnunet-auction-create",
212 gettext_noop ("create a new auction and "
213 "start listening for bidders"),
214 options,
215 &run,
216 NULL)) ? ret : 1;
217 GNUNET_free_nz ((void *) argv);
218 return ret;
219}
diff --git a/src/contrib/service/auction/gnunet-auction-info.c b/src/contrib/service/auction/gnunet-auction-info.c
new file mode 100644
index 000000000..dded45ce6
--- /dev/null
+++ b/src/contrib/service/auction/gnunet-auction-info.c
@@ -0,0 +1,86 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 auction/gnunet-auction.c
23 * @brief auction for writing a tool
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28/* #include "gnunet_auction_service.h" */
29
30/**
31 * Final status code.
32 */
33static int ret;
34
35
36/**
37 * Main function that will be run by the scheduler.
38 *
39 * @param cls closure
40 * @param args remaining command-line arguments
41 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
42 * @param cfg configuration
43 */
44static void
45run (void *cls,
46 char *const *args,
47 const char *cfgfile,
48 const struct GNUNET_CONFIGURATION_Handle *cfg)
49{
50 /* main code here */
51}
52
53
54/**
55 * The main function.
56 *
57 * @param argc number of arguments from the command line
58 * @param argv command line arguments
59 * @return 0 ok, 1 on error
60 */
61int
62main (int argc, char *const *argv)
63{
64 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
65 /* FIMXE: add options here */
66 GNUNET_GETOPT_OPTION_END
67 };
68
69 if (GNUNET_OK !=
70 GNUNET_STRINGS_get_utf8_args (argc, argv,
71 &argc, &argv))
72 return 2;
73
74 ret = (GNUNET_OK ==
75 GNUNET_PROGRAM_run (argc, argv,
76 "gnunet-auction",
77 gettext_noop ("help text"),
78 options,
79 &run,
80 NULL)) ? ret : 1;
81 GNUNET_free_nz ((void *) argv);
82 return ret;
83}
84
85
86/* end of gnunet-auction.c */
diff --git a/src/contrib/service/auction/gnunet-auction-join.c b/src/contrib/service/auction/gnunet-auction-join.c
new file mode 100644
index 000000000..dded45ce6
--- /dev/null
+++ b/src/contrib/service/auction/gnunet-auction-join.c
@@ -0,0 +1,86 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 auction/gnunet-auction.c
23 * @brief auction for writing a tool
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28/* #include "gnunet_auction_service.h" */
29
30/**
31 * Final status code.
32 */
33static int ret;
34
35
36/**
37 * Main function that will be run by the scheduler.
38 *
39 * @param cls closure
40 * @param args remaining command-line arguments
41 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
42 * @param cfg configuration
43 */
44static void
45run (void *cls,
46 char *const *args,
47 const char *cfgfile,
48 const struct GNUNET_CONFIGURATION_Handle *cfg)
49{
50 /* main code here */
51}
52
53
54/**
55 * The main function.
56 *
57 * @param argc number of arguments from the command line
58 * @param argv command line arguments
59 * @return 0 ok, 1 on error
60 */
61int
62main (int argc, char *const *argv)
63{
64 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
65 /* FIMXE: add options here */
66 GNUNET_GETOPT_OPTION_END
67 };
68
69 if (GNUNET_OK !=
70 GNUNET_STRINGS_get_utf8_args (argc, argv,
71 &argc, &argv))
72 return 2;
73
74 ret = (GNUNET_OK ==
75 GNUNET_PROGRAM_run (argc, argv,
76 "gnunet-auction",
77 gettext_noop ("help text"),
78 options,
79 &run,
80 NULL)) ? ret : 1;
81 GNUNET_free_nz ((void *) argv);
82 return ret;
83}
84
85
86/* end of gnunet-auction.c */
diff --git a/src/contrib/service/auction/gnunet-service-auction.c b/src/contrib/service/auction/gnunet-service-auction.c
new file mode 100644
index 000000000..c20f0bdbe
--- /dev/null
+++ b/src/contrib/service/auction/gnunet-service-auction.c
@@ -0,0 +1,155 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009 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 auction/gnunet-service-auction.c
23 * @brief service for executing auctions
24 * @author Markus Teich
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
29#include "auction.h"
30
31/**
32 * Check AUCTION CREATE messages from the client.
33 *
34 * @param cls the client we received this message from
35 * @param msg the actual message received
36 * @return #GNUNET_OK (always)
37 */
38static int
39check_create (void *cls, const struct GNUNET_AUCTION_ClientCreateMessage *msg)
40{
41 /* always well-formed due to arbitrary length description */
42 return GNUNET_OK;
43}
44
45
46/**
47 * Handler for CREATE messages.
48 *
49 * @param cls the client we received this message from
50 * @param msg the actual message received
51 */
52static void
53handle_create (void *cls, const struct GNUNET_AUCTION_ClientCreateMessage *msg)
54{
55 struct GNUNET_SERVICE_Client *client = cls;
56// struct GNUNET_MQ_Handle *mq;
57// struct GNUNET_MQ_Envelope *env;
58// struct GNUNET_AUCTION_blabla em;
59 //uint16_t size;
60
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "Received CREATE message from client\n");
63
64 //size = ntohs (msg->header.size);
65
66 /**TODO: create auction and return auction object */
67// mq = GNUNET_SERVICE_client_get_mq (client);
68// setup_info_message (&em);
69// env = GNUNET_MQ_msg_copy (&em.header);
70// GNUNET_MQ_send (mq, env);
71
72 GNUNET_SERVICE_client_continue (client);
73}
74
75
76/**
77 * Task run during shutdown.
78 *
79 * @param cls unused
80 */
81static void
82cleanup_task (void *cls)
83{
84 /* FIXME: do clean up here */
85}
86
87
88/**
89 * Callback called when a client connects to the service.
90 *
91 * @param cls closure for the service
92 * @param c the new client that connected to the service
93 * @param mq the message queue used to send messages to the client
94 * @return @a c
95 */
96static void *
97client_connect_cb (void *cls,
98 struct GNUNET_SERVICE_Client *c,
99 struct GNUNET_MQ_Handle *mq)
100{
101 return c;
102}
103
104
105/**
106 * Callback called when a client disconnected from the service
107 *
108 * @param cls closure for the service
109 * @param c the client that disconnected
110 * @param internal_cls should be equal to @a c
111 */
112static void
113client_disconnect_cb (void *cls,
114 struct GNUNET_SERVICE_Client *c,
115 void *internal_cls)
116{
117 GNUNET_assert (c == internal_cls);
118}
119
120
121/**
122 * Process auction requests.
123 *
124 * @param cls closure
125 * @param cfg configuration to use
126 * @param service the initialized service
127 */
128static void
129run (void *cls,
130 const struct GNUNET_CONFIGURATION_Handle *cfg,
131 struct GNUNET_SERVICE_Handle *service)
132{
133 /* FIXME: do setup here */
134 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
135}
136
137
138/**
139 * Define "main" method using service macro.
140 */
141GNUNET_SERVICE_MAIN
142 ("auction",
143 GNUNET_SERVICE_OPTION_NONE,
144 &run,
145 &client_connect_cb,
146 &client_disconnect_cb,
147 NULL,
148 GNUNET_MQ_hd_var_size (create,
149 GNUNET_MESSAGE_TYPE_AUCTION_CLIENT_CREATE,
150 struct GNUNET_AUCTION_ClientCreateMessage,
151 NULL),
152 GNUNET_MQ_handler_end ())
153
154
155/* end of gnunet-service-auction.c */
diff --git a/src/contrib/service/auction/test_auction_api.c b/src/contrib/service/auction/test_auction_api.c
new file mode 100644
index 000000000..1eac9b5c4
--- /dev/null
+++ b/src/contrib/service/auction/test_auction_api.c
@@ -0,0 +1,44 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009 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 auction/test_auction_api.c
22 * @brief testcase for auction.c
23 */
24#include "platform.h"
25
26static int
27check ()
28{
29 return 0;
30}
31
32
33int
34main (int argc, char *argv[])
35{
36 int ret;
37
38 ret = check ();
39
40 return ret;
41}
42
43
44/* end of test_auction_api.c */
diff --git a/src/contrib/service/auction/test_auction_create.sh b/src/contrib/service/auction/test_auction_create.sh
new file mode 100755
index 000000000..b0f4de738
--- /dev/null
+++ b/src/contrib/service/auction/test_auction_create.sh
@@ -0,0 +1,80 @@
1#! /bin/sh
2
3trap 'rm -f test.json' EXIT
4
5
6# missing required cmdline args
7gnunet-auction-create -r 1 -d foo -p test.json && exit 1
8gnunet-auction-create -s 1 -d foo -p test.json && exit 1
9gnunet-auction-create -s 1 -r 1 -p test.json && exit 1
10gnunet-auction-create -s 1 -r 1 -d foo && exit 1
11
12
13# no pricemap
14rm -f test.json
15gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
16
17
18# json errors
19cat <<DOG >test.json
20[,]
21DOG
22gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
23
24cat <<DOG >test.json
25bla
26DOG
27gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
28
29
30# unexpected structures
31cat <<DOG >test.json
32{"foo": "bar"}
33DOG
34gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
35
36cat <<DOG >test.json
37{"currency": "foo"}
38DOG
39gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
40
41cat <<DOG >test.json
42{"prices": []}
43DOG
44gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
45
46cat <<DOG >test.json
47{"currency": "foo", "prices": "bar"}
48DOG
49gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
50
51
52# wrong array content
53cat <<DOG >test.json
54{"currency": "foo", "prices": []}
55DOG
56gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
57
58cat <<DOG >test.json
59{"currency": "foo", "prices": ["bar"]}
60DOG
61gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
62
63cat <<DOG >test.json
64{"currency": "foo", "prices": [null]}
65DOG
66gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
67
68cat <<DOG >test.json
69{"currency": "foo", "prices": [1, 2]}
70DOG
71gnunet-auction-create -s 1 -r 1 -d foo -p test.json && exit 1
72
73
74# correct example
75cat <<DOG >test.json
76{"currency": "foo", "prices": [2, 1]}
77DOG
78gnunet-auction-create -s 1 -r 1 -d foo -p test.json || exit 1
79
80rm -f test.json
diff --git a/src/contrib/service/rps/.gitignore b/src/contrib/service/rps/.gitignore
new file mode 100644
index 000000000..9e78e2ca0
--- /dev/null
+++ b/src/contrib/service/rps/.gitignore
@@ -0,0 +1,16 @@
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/contrib/service/rps/Makefile.am b/src/contrib/service/rps/Makefile.am
new file mode 100644
index 000000000..2ee315d11
--- /dev/null
+++ b/src/contrib/service/rps/Makefile.am
@@ -0,0 +1,167 @@
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/lib/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/lib/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
48#noinst_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/lib/util/libgnunetutil.la \
64 $(top_builddir)/src/peerstore/libgnunetpeerstore.la \
65 $(top_builddir)/src/cadet/libgnunetcadet.la \
66 $(top_builddir)/src/nse/libgnunetnse.la \
67 $(top_builddir)/src/statistics/libgnunetstatistics.la \
68 $(top_builddir)/src/service/core/libgnunetcore.la \
69 $(LIBGCRYPT_LIBS) \
70 -lm -lgcrypt \
71 $(GN_LIBINTL)
72
73
74check_PROGRAMS = \
75 test_service_rps_view \
76 test_service_rps_custommap \
77 test_service_rps_sampler_elem
78 # test_rps_single_req \
79 # test_rps_churn
80 # test_rps_sub \
81 # test_rps_seed_request
82#if ENABLE_MALICIOUS
83#check_PROGRAMS += \
84# test_rps_malicious_1 \
85# test_rps_malicious_2 \
86# test_rps_malicious_3
87#endif
88
89rps_test_src = \
90 test_rps.c \
91 rps-test_util.h rps-test_util.c \
92 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
93 rps-sampler_common.h rps-sampler_common.c \
94 gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c
95
96#ld_rps_test_lib = \
97# libgnunetrps.la \
98# $(top_builddir)/src/lib/util/libgnunetutil.la \
99# $(top_builddir)/src/statistics/libgnunetstatistics.la \
100# $(top_builddir)/src/testbed/libgnunettestbed.la \
101# -lm
102
103if ENABLE_TEST_RUN
104AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME;
105TESTS = $(check_PROGRAMS)
106endif
107
108test_service_rps_view_SOURCES = \
109 gnunet-service-rps_view.h gnunet-service-rps_view.c \
110 test_service_rps_view.c
111test_service_rps_view_LDADD = $(top_builddir)/src/lib/util/libgnunetutil.la
112
113test_service_rps_custommap_SOURCES = \
114 gnunet-service-rps_custommap.h gnunet-service-rps_custommap.c \
115 test_service_rps_custommap.c
116test_service_rps_custommap_LDADD = \
117 $(top_builddir)/src/lib/util/libgnunetutil.la
118
119test_service_rps_sampler_elem_SOURCES = \
120 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
121 rps-test_util.h rps-test_util.c \
122 test_service_rps_sampler_elem.c
123test_service_rps_sampler_elem_LDADD = $(top_builddir)/src/lib/util/libgnunetutil.la
124
125#test_rps_single_req_SOURCES = $(rps_test_src)
126#test_rps_single_req_LDADD = $(ld_rps_test_lib)
127#
128#test_rps_seed_request_SOURCES = $(rps_test_src)
129#test_rps_seed_request_LDADD = $(ld_rps_test_lib)
130#
131#test_rps_req_cancel_SOURCES = $(rps_test_src)
132#test_rps_req_cancel_LDADD = $(ld_rps_test_lib)
133#
134#test_rps_sub_SOURCES = $(rps_test_src)
135#test_rps_sub_LDADD = $(ld_rps_test_lib)
136#
137#test_rps_seed_big_SOURCES = $(rps_test_src)
138#test_rps_seed_big_LDADD = $(ld_rps_test_lib)
139#
140#test_rps_churn_SOURCES = $(rps_test_src)
141#test_rps_churn_LDADD = $(ld_rps_test_lib)
142#
143#test_rps_malicious_1_SOURCES = $(rps_test_src)
144#test_rps_malicious_1_LDADD = $(ld_rps_test_lib)
145#
146#test_rps_malicious_2_SOURCES = $(rps_test_src)
147#test_rps_malicious_2_LDADD = $(ld_rps_test_lib)
148#
149#test_rps_malicious_3_SOURCES = $(rps_test_src)
150#test_rps_malicious_3_LDADD = $(ld_rps_test_lib)
151
152#gnunet_rps_profiler_SOURCES = \
153# gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
154# rps-sampler_common.h rps-sampler_common.c \
155# gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c \
156# rps-test_util.h rps-test_util.c \
157# gnunet-rps-profiler.c
158#gnunet_rps_profiler_LDADD = \
159# $(top_builddir)/src/statistics/libgnunetstatistics.la \
160# libgnunetrps.la \
161# $(top_builddir)/src/lib/util/libgnunetutil.la \
162# $(top_builddir)/src/testbed/libgnunettestbed.la \
163# -lm
164
165
166EXTRA_DIST = \
167 test_rps.conf
diff --git a/src/contrib/service/rps/gnunet-rps-profiler.c b/src/contrib/service/rps/gnunet-rps-profiler.c
new file mode 100644
index 000000000..df10ad5da
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-rps-profiler.c
@@ -0,0 +1,3199 @@
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 b
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/contrib/service/rps/gnunet-rps.c b/src/contrib/service/rps/gnunet-rps.c
new file mode 100644
index 000000000..f90fc0d88
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-rps.c
@@ -0,0 +1,294 @@
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 num_peers 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/contrib/service/rps/gnunet-service-rps.c b/src/contrib/service/rps/gnunet-service-rps.c
new file mode 100644
index 000000000..76e33c87b
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps.c
@@ -0,0 +1,5009 @@
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_peerstore_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#include "gnunet_constants.h"
40
41#include <math.h>
42#include <inttypes.h>
43#include <string.h>
44
45#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
46
47// TODO check for overflows
48
49// TODO align message structs
50
51// TODO connect to friends
52
53// TODO blacklist? (-> mal peer detection on top of brahms)
54
55// hist_size_init, hist_size_max
56
57/***********************************************************************
58* Old gnunet-service-rps_peers.c
59***********************************************************************/
60
61/**
62 * Set a peer flag of given peer context.
63 */
64#define SET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask))
65
66/**
67 * Get peer flag of given peer context.
68 */
69#define check_peer_flag_set(peer_ctx, mask) \
70 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
71
72/**
73 * Unset flag of given peer context.
74 */
75#define UNSET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask))
76
77/**
78 * Get channel flag of given channel context.
79 */
80#define check_channel_flag_set(channel_flags, mask) \
81 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
82
83/**
84 * Unset flag of given channel context.
85 */
86#define unset_channel_flag(channel_flags, mask) ((*channel_flags) &= ~(mask))
87
88
89/**
90 * Pending operation on peer consisting of callback and closure
91 *
92 * When an operation cannot be executed right now this struct is used to store
93 * the callback and closure for later execution.
94 */
95struct PeerPendingOp
96{
97 /**
98 * Callback
99 */
100 PeerOp op;
101
102 /**
103 * Closure
104 */
105 void *op_cls;
106};
107
108/**
109 * List containing all messages that are yet to be send
110 *
111 * This is used to keep track of all messages that have not been sent yet. When
112 * a peer is to be removed the pending messages can be removed properly.
113 */
114struct PendingMessage
115{
116 /**
117 * DLL next, prev
118 */
119 struct PendingMessage *next;
120 struct PendingMessage *prev;
121
122 /**
123 * The envelope to the corresponding message
124 */
125 struct GNUNET_MQ_Envelope *ev;
126
127 /**
128 * The corresponding context
129 */
130 struct PeerContext *peer_ctx;
131
132 /**
133 * The message type
134 */
135 const char *type;
136};
137
138/**
139 * @brief Context for a channel
140 */
141struct ChannelCtx;
142
143/**
144 * Struct used to keep track of other peer's status
145 *
146 * This is stored in a multipeermap.
147 * It contains information such as cadet channels, a message queue for sending,
148 * status about the channels, the pending operations on this peer and some flags
149 * about the status of the peer itself. (online, valid, ...)
150 */
151struct PeerContext
152{
153 /**
154 * The Sub this context belongs to.
155 */
156 struct Sub *sub;
157
158 /**
159 * Message queue open to client
160 */
161 struct GNUNET_MQ_Handle *mq;
162
163 /**
164 * Channel open to client.
165 */
166 struct ChannelCtx *send_channel_ctx;
167
168 /**
169 * Channel open from client.
170 */
171 struct ChannelCtx *recv_channel_ctx;
172
173 /**
174 * Array of pending operations on this peer.
175 */
176 struct PeerPendingOp *pending_ops;
177
178 /**
179 * Handle to the callback given to cadet_ntfy_tmt_rdy()
180 *
181 * To be canceled on shutdown.
182 */
183 struct PendingMessage *online_check_pending;
184
185 /**
186 * Number of pending operations.
187 */
188 unsigned int num_pending_ops;
189
190 /**
191 * Identity of the peer
192 */
193 struct GNUNET_PeerIdentity peer_id;
194
195 /**
196 * Flags indicating status of peer
197 */
198 uint32_t peer_flags;
199
200 /**
201 * Last time we received something from that peer.
202 */
203 struct GNUNET_TIME_Absolute last_message_recv;
204
205 /**
206 * Last time we received a keepalive message.
207 */
208 struct GNUNET_TIME_Absolute last_keepalive;
209
210 /**
211 * DLL with all messages that are yet to be sent
212 */
213 struct PendingMessage *pending_messages_head;
214 struct PendingMessage *pending_messages_tail;
215
216 /**
217 * This is pobably followed by 'statistical' data (when we first saw
218 * it, how did we get its ID, how many pushes (in a timeinterval),
219 * ...)
220 */
221 uint32_t round_pull_req;
222};
223
224/**
225 * @brief Closure to #valid_peer_iterator
226 */
227struct PeersIteratorCls
228{
229 /**
230 * Iterator function
231 */
232 PeersIterator iterator;
233
234 /**
235 * Closure to iterator
236 */
237 void *cls;
238};
239
240/**
241 * @brief Context for a channel
242 */
243struct ChannelCtx
244{
245 /**
246 * @brief The channel itself
247 */
248 struct GNUNET_CADET_Channel *channel;
249
250 /**
251 * @brief The peer context associated with the channel
252 */
253 struct PeerContext *peer_ctx;
254
255 /**
256 * @brief When channel destruction needs to be delayed (because it is called
257 * from within the cadet routine of another channel destruction) this task
258 * refers to the respective _SCHEDULER_Task.
259 */
260 struct GNUNET_SCHEDULER_Task *destruction_task;
261};
262
263
264#if ENABLE_MALICIOUS
265
266/**
267 * If type is 2 This struct is used to store the attacked peers in a DLL
268 */
269struct AttackedPeer
270{
271 /**
272 * DLL
273 */
274 struct AttackedPeer *next;
275 struct AttackedPeer *prev;
276
277 /**
278 * PeerID
279 */
280 struct GNUNET_PeerIdentity peer_id;
281};
282
283#endif /* ENABLE_MALICIOUS */
284
285/**
286 * @brief This number determines the number of slots for files that represent
287 * histograms
288 */
289#define HISTOGRAM_FILE_SLOTS 32
290
291/**
292 * @brief The size (in bytes) a file needs to store the histogram
293 *
294 * Per slot: 1 newline, up to 4 chars,
295 * Additionally: 1 null termination
296 */
297#define SIZE_DUMP_FILE (HISTOGRAM_FILE_SLOTS * 5) + 1
298
299/**
300 * @brief One Sub.
301 *
302 * Essentially one instance of brahms that only connects to other instances
303 * with the same (secret) value.
304 */
305struct Sub
306{
307 /**
308 * @brief Hash of the shared value that defines Subs.
309 */
310 struct GNUNET_HashCode hash;
311
312 /**
313 * @brief Port to communicate to other peers.
314 */
315 struct GNUNET_CADET_Port *cadet_port;
316
317 /**
318 * @brief Hashmap of valid peers.
319 */
320 struct GNUNET_CONTAINER_MultiPeerMap *valid_peers;
321
322 /**
323 * @brief Filename of the file that stores the valid peers persistently.
324 */
325 char *filename_valid_peers;
326
327 /**
328 * Set of all peers to keep track of them.
329 */
330 struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
331
332 /**
333 * @brief This is the minimum estimate used as sampler size.
334 *
335 * It is configured by the user.
336 */
337 unsigned int sampler_size_est_min;
338
339 /**
340 * The size of sampler we need to be able to satisfy the Brahms protocol's
341 * need of random peers.
342 *
343 * This is one minimum size the sampler grows to.
344 */
345 unsigned int sampler_size_est_need;
346
347 /**
348 * Time interval the do_round task runs in.
349 */
350 struct GNUNET_TIME_Relative round_interval;
351
352 /**
353 * Sampler used for the Brahms protocol itself.
354 */
355 struct RPS_Sampler *sampler;
356
357#ifdef TO_FILE_FULL
358 /**
359 * Name to log view to
360 */
361 char *file_name_view_log;
362#endif /* TO_FILE_FULL */
363
364#ifdef TO_FILE
365#ifdef TO_FILE_FULL
366 /**
367 * Name to log number of observed peers to
368 */
369 char *file_name_observed_log;
370#endif /* TO_FILE_FULL */
371
372 /**
373 * @brief Count the observed peers
374 */
375 uint32_t num_observed_peers;
376
377 /**
378 * @brief Multipeermap (ab-) used to count unique peer_ids
379 */
380 struct GNUNET_CONTAINER_MultiPeerMap *observed_unique_peers;
381#endif /* TO_FILE */
382
383 /**
384 * List to store peers received through pushes temporary.
385 */
386 struct CustomPeerMap *push_map;
387
388 /**
389 * List to store peers received through pulls temporary.
390 */
391 struct CustomPeerMap *pull_map;
392
393 /**
394 * @brief This is the estimate used as view size.
395 *
396 * It is initialised with the minimum
397 */
398 unsigned int view_size_est_need;
399
400 /**
401 * @brief This is the minimum estimate used as view size.
402 *
403 * It is configured by the user.
404 */
405 unsigned int view_size_est_min;
406
407 /**
408 * @brief The view.
409 */
410 struct View *view;
411
412 /**
413 * Identifier for the main task that runs periodically.
414 */
415 struct GNUNET_SCHEDULER_Task *do_round_task;
416
417 /* === stats === */
418
419 /**
420 * @brief Counts the executed rounds.
421 */
422 uint32_t num_rounds;
423
424 /**
425 * @brief This array accumulates the number of received pushes per round.
426 *
427 * Number at index i represents the number of rounds with i observed pushes.
428 */
429 uint32_t push_recv[HISTOGRAM_FILE_SLOTS];
430
431 /**
432 * @brief Histogram of deltas between the expected and actual number of
433 * received pushes.
434 *
435 * As half of the entries are expected to be negative, this is shifted by
436 * #HISTOGRAM_FILE_SLOTS/2.
437 */
438 uint32_t push_delta[HISTOGRAM_FILE_SLOTS];
439
440 /**
441 * @brief Number of pull replies with this delay measured in rounds.
442 *
443 * Number at index i represents the number of pull replies with a delay of i
444 * rounds.
445 */
446 uint32_t pull_delays[HISTOGRAM_FILE_SLOTS];
447};
448
449
450/***********************************************************************
451* Globals
452***********************************************************************/
453
454/**
455 * Our configuration.
456 */
457static const struct GNUNET_CONFIGURATION_Handle *cfg;
458
459/**
460 * Handle to the statistics service.
461 */
462struct GNUNET_STATISTICS_Handle *stats;
463
464/**
465 * Handler to CADET.
466 */
467struct GNUNET_CADET_Handle *cadet_handle;
468
469/**
470 * Handle to CORE
471 */
472struct GNUNET_CORE_Handle *core_handle;
473
474/**
475 * @brief PeerMap to keep track of connected peers.
476 */
477struct GNUNET_CONTAINER_MultiPeerMap *map_single_hop;
478
479/**
480 * Our own identity.
481 */
482static struct GNUNET_PeerIdentity own_identity;
483
484/**
485 * Percentage of total peer number in the view
486 * to send random PUSHes to
487 */
488static float alpha;
489
490/**
491 * Percentage of total peer number in the view
492 * to send random PULLs to
493 */
494static float beta;
495
496/**
497 * Handler to NSE.
498 */
499static struct GNUNET_NSE_Handle *nse;
500
501/**
502 * Handle to the PEERSTORE service.
503 */
504static struct GNUNET_PEERSTORE_Handle *peerstore;
505
506/**
507 * Our peerstore notification context. We use notification
508 * to instantly learn about new peers as they are discovered.
509 */
510static struct GNUNET_PEERSTORE_NotifyContext *peerstore_notify;
511
512
513#if ENABLE_MALICIOUS
514/**
515 * Type of malicious peer
516 *
517 * 0 Don't act malicious at all - Default
518 * 1 Try to maximise representation
519 * 2 Try to partition the network
520 * 3 Combined attack
521 */
522static uint32_t mal_type;
523
524/**
525 * Other malicious peers
526 */
527static struct GNUNET_PeerIdentity *mal_peers;
528
529/**
530 * Hashmap of malicious peers used as set.
531 * Used to more efficiently check whether we know that peer.
532 */
533static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
534
535/**
536 * Number of other malicious peers
537 */
538static uint32_t num_mal_peers;
539
540
541/**
542 * If type is 2 this is the DLL of attacked peers
543 */
544static struct AttackedPeer *att_peers_head;
545static struct AttackedPeer *att_peers_tail;
546
547/**
548 * This index is used to point to an attacked peer to
549 * implement the round-robin-ish way to select attacked peers.
550 */
551static struct AttackedPeer *att_peer_index;
552
553/**
554 * Hashmap of attacked peers used as set.
555 * Used to more efficiently check whether we know that peer.
556 */
557static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
558
559/**
560 * Number of attacked peers
561 */
562static uint32_t num_attacked_peers;
563
564/**
565 * If type is 1 this is the attacked peer
566 */
567static struct GNUNET_PeerIdentity attacked_peer;
568
569/**
570 * The limit of PUSHes we can send in one round.
571 * This is an assumption of the Brahms protocol and either implemented
572 * via proof of work
573 * or
574 * assumed to be the bandwidth limitation.
575 */
576static uint32_t push_limit = 10000;
577#endif /* ENABLE_MALICIOUS */
578
579/**
580 * @brief Main Sub.
581 *
582 * This is run in any case by all peers and connects to all peers without
583 * specifying a shared value.
584 */
585static struct Sub *msub;
586
587/**
588 * @brief Maximum number of valid peers to keep.
589 * TODO read from config
590 */
591static const uint32_t num_valid_peers_max = UINT32_MAX;
592
593/***********************************************************************
594* /Globals
595***********************************************************************/
596
597
598static void
599do_round (void *cls);
600
601#if ENABLE_MALICIOUS
602static void
603do_mal_round (void *cls);
604
605#endif /* ENABLE_MALICIOUS */
606
607
608/**
609 * @brief Get the #PeerContext associated with a peer
610 *
611 * @param peer_map The peer map containing the context
612 * @param peer the peer id
613 *
614 * @return the #PeerContext
615 */
616static struct PeerContext *
617get_peer_ctx (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
618 const struct GNUNET_PeerIdentity *peer)
619{
620 struct PeerContext *ctx;
621 int ret;
622
623 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
624 GNUNET_assert (GNUNET_YES == ret);
625 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
626 GNUNET_assert (NULL != ctx);
627 return ctx;
628}
629
630
631/**
632 * @brief Check whether we have information about the given peer.
633 *
634 * FIXME probably deprecated. Make this the new _online.
635 *
636 * @param peer_map The peer map to check for the existence of @a peer
637 * @param peer peer in question
638 *
639 * @return #GNUNET_YES if peer is known
640 * #GNUNET_NO if peer is not known
641 */
642static int
643check_peer_known (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
644 const struct GNUNET_PeerIdentity *peer)
645{
646 if (NULL != peer_map)
647 {
648 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
649 }
650 else
651 {
652 return GNUNET_NO;
653 }
654}
655
656
657/**
658 * @brief Create a new #PeerContext and insert it into the peer map
659 *
660 * @param sub The Sub this context belongs to.
661 * @param peer the peer to create the #PeerContext for
662 *
663 * @return the #PeerContext
664 */
665static struct PeerContext *
666create_peer_ctx (struct Sub *sub,
667 const struct GNUNET_PeerIdentity *peer)
668{
669 struct PeerContext *ctx;
670 int ret;
671
672 GNUNET_assert (GNUNET_NO == check_peer_known (sub->peer_map, peer));
673
674 ctx = GNUNET_new (struct PeerContext);
675 ctx->peer_id = *peer;
676 ctx->sub = sub;
677 ret = GNUNET_CONTAINER_multipeermap_put (sub->peer_map, peer, ctx,
678 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
679 GNUNET_assert (GNUNET_OK == ret);
680 if (sub == msub)
681 {
682 GNUNET_STATISTICS_set (stats,
683 "# known peers",
684 GNUNET_CONTAINER_multipeermap_size (sub->peer_map),
685 GNUNET_NO);
686 }
687 return ctx;
688}
689
690
691/**
692 * @brief Create or get a #PeerContext
693 *
694 * @param sub The Sub to which the created context belongs to
695 * @param peer the peer to get the associated context to
696 *
697 * @return the context
698 */
699static struct PeerContext *
700create_or_get_peer_ctx (struct Sub *sub,
701 const struct GNUNET_PeerIdentity *peer)
702{
703 if (GNUNET_NO == check_peer_known (sub->peer_map, peer))
704 {
705 return create_peer_ctx (sub, peer);
706 }
707 return get_peer_ctx (sub->peer_map, peer);
708}
709
710
711/**
712 * @brief Check whether we have a connection to this @a peer
713 *
714 * Also sets the #Peers_ONLINE flag accordingly
715 *
716 * @param peer_ctx Context of the peer of which connectivity is to be checked
717 *
718 * @return #GNUNET_YES if we are connected
719 * #GNUNET_NO otherwise
720 */
721static int
722check_connected (struct PeerContext *peer_ctx)
723{
724 /* If we don't know about this peer we don't know whether it's online */
725 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
726 &peer_ctx->peer_id))
727 {
728 return GNUNET_NO;
729 }
730 /* Get the context */
731 peer_ctx = get_peer_ctx (peer_ctx->sub->peer_map, &peer_ctx->peer_id);
732 /* If we have no channel to this peer we don't know whether it's online */
733 if ((NULL == peer_ctx->send_channel_ctx) &&
734 (NULL == peer_ctx->recv_channel_ctx))
735 {
736 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE);
737 return GNUNET_NO;
738 }
739 /* Otherwise (if we have a channel, we know that it's online */
740 SET_PEER_FLAG (peer_ctx, Peers_ONLINE);
741 return GNUNET_YES;
742}
743
744
745/**
746 * @brief The closure to #get_rand_peer_iterator.
747 */
748struct GetRandPeerIteratorCls
749{
750 /**
751 * @brief The index of the peer to return.
752 * Will be decreased until 0.
753 * Then current peer is returned.
754 */
755 uint32_t index;
756
757 /**
758 * @brief Pointer to peer to return.
759 */
760 const struct GNUNET_PeerIdentity *peer;
761};
762
763
764/**
765 * @brief Iterator function for #get_random_peer_from_peermap.
766 *
767 * Implements #GNUNET_CONTAINER_PeerMapIterator.
768 * Decreases the index until the index is null.
769 * Then returns the current peer.
770 *
771 * @param cls the #GetRandPeerIteratorCls containing index and peer
772 * @param peer current peer
773 * @param value unused
774 *
775 * @return #GNUNET_YES if we should continue to
776 * iterate,
777 * #GNUNET_NO if not.
778 */
779static int
780get_rand_peer_iterator (void *cls,
781 const struct GNUNET_PeerIdentity *peer,
782 void *value)
783{
784 struct GetRandPeerIteratorCls *iterator_cls = cls;
785
786 (void) value;
787
788 if (0 >= iterator_cls->index)
789 {
790 iterator_cls->peer = peer;
791 return GNUNET_NO;
792 }
793 iterator_cls->index--;
794 return GNUNET_YES;
795}
796
797
798/**
799 * @brief Get a random peer from @a peer_map
800 *
801 * @param valid_peers Peer map containing valid peers from which to select a
802 * random one
803 *
804 * @return a random peer
805 */
806static const struct GNUNET_PeerIdentity *
807get_random_peer_from_peermap (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers)
808{
809 struct GetRandPeerIteratorCls *iterator_cls;
810 const struct GNUNET_PeerIdentity *ret;
811
812 iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls);
813 iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
814 GNUNET_CONTAINER_multipeermap_size (
815 valid_peers));
816 (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
817 get_rand_peer_iterator,
818 iterator_cls);
819 ret = iterator_cls->peer;
820 GNUNET_free (iterator_cls);
821 return ret;
822}
823
824
825/**
826 * @brief Add a given @a peer to valid peers.
827 *
828 * If valid peers are already #num_valid_peers_max, delete a peer previously.
829 *
830 * @param peer The peer that is added to the valid peers.
831 * @param valid_peers Peer map of valid peers to which to add the @a peer
832 *
833 * @return #GNUNET_YES if no other peer had to be removed
834 * #GNUNET_NO otherwise
835 */
836static int
837add_valid_peer (const struct GNUNET_PeerIdentity *peer,
838 struct GNUNET_CONTAINER_MultiPeerMap *valid_peers)
839{
840 const struct GNUNET_PeerIdentity *rand_peer;
841 int ret;
842
843 ret = GNUNET_YES;
844 /* Remove random peers until there is space for a new one */
845 while (num_valid_peers_max <=
846 GNUNET_CONTAINER_multipeermap_size (valid_peers))
847 {
848 rand_peer = get_random_peer_from_peermap (valid_peers);
849 GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer);
850 ret = GNUNET_NO;
851 }
852 (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL,
853 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
854 if (valid_peers == msub->valid_peers)
855 {
856 GNUNET_STATISTICS_set (stats,
857 "# valid peers",
858 GNUNET_CONTAINER_multipeermap_size (valid_peers),
859 GNUNET_NO);
860 }
861 return ret;
862}
863
864
865static void
866remove_pending_message (struct PendingMessage *pending_msg, int cancel);
867
868/**
869 * @brief Set the peer flag to living and
870 * call the pending operations on this peer.
871 *
872 * Also adds peer to #valid_peers.
873 *
874 * @param peer_ctx the #PeerContext of the peer to set online
875 */
876static void
877set_peer_online (struct PeerContext *peer_ctx)
878{
879 struct GNUNET_PeerIdentity *peer;
880 unsigned int i;
881
882 peer = &peer_ctx->peer_id;
883 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "Peer %s is online and valid, calling %i pending operations on it\n",
885 GNUNET_i2s (peer),
886 peer_ctx->num_pending_ops);
887
888 if (NULL != peer_ctx->online_check_pending)
889 {
890 LOG (GNUNET_ERROR_TYPE_DEBUG,
891 "Removing pending online check for peer %s\n",
892 GNUNET_i2s (&peer_ctx->peer_id));
893 // TODO wait until cadet sets mq->cancel_impl
894 // GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev);
895 remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES);
896 peer_ctx->online_check_pending = NULL;
897 }
898
899 SET_PEER_FLAG (peer_ctx, Peers_ONLINE);
900
901 /* Call pending operations */
902 for (i = 0; i < peer_ctx->num_pending_ops; i++)
903 {
904 peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer);
905 }
906 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
907}
908
909
910static void
911cleanup_destroyed_channel (void *cls,
912 const struct GNUNET_CADET_Channel *channel);
913
914/* Declaration of handlers */
915static void
916handle_peer_check (void *cls,
917 const struct GNUNET_MessageHeader *msg);
918
919static void
920handle_peer_push (void *cls,
921 const struct GNUNET_MessageHeader *msg);
922
923static void
924handle_peer_pull_request (void *cls,
925 const struct GNUNET_MessageHeader *msg);
926
927static int
928check_peer_pull_reply (void *cls,
929 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
930
931static void
932handle_peer_pull_reply (void *cls,
933 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
934
935/* End declaration of handlers */
936
937/**
938 * @brief Allocate memory for a new channel context and insert it into DLL
939 *
940 * @param peer_ctx context of the according peer
941 *
942 * @return The channel context
943 */
944static struct ChannelCtx *
945add_channel_ctx (struct PeerContext *peer_ctx)
946{
947 struct ChannelCtx *channel_ctx;
948
949 channel_ctx = GNUNET_new (struct ChannelCtx);
950 channel_ctx->peer_ctx = peer_ctx;
951 return channel_ctx;
952}
953
954
955/**
956 * @brief Free memory and NULL pointers.
957 *
958 * @param channel_ctx The channel context.
959 */
960static void
961remove_channel_ctx (struct ChannelCtx *channel_ctx)
962{
963 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
964
965 if (NULL != channel_ctx->destruction_task)
966 {
967 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task);
968 channel_ctx->destruction_task = NULL;
969 }
970
971 if (NULL == peer_ctx)
972 return;
973 if (channel_ctx == peer_ctx->send_channel_ctx)
974 {
975 peer_ctx->send_channel_ctx = NULL;
976 peer_ctx->mq = NULL;
977 }
978 else if (channel_ctx == peer_ctx->recv_channel_ctx)
979 {
980 peer_ctx->recv_channel_ctx = NULL;
981 }
982 GNUNET_free (channel_ctx);
983}
984
985
986/**
987 * @brief Get the channel of a peer. If not existing, create.
988 *
989 * @param peer_ctx Context of the peer of which to get the channel
990 * @return the #GNUNET_CADET_Channel used to send data to @a peer_ctx
991 */
992struct GNUNET_CADET_Channel *
993get_channel (struct PeerContext *peer_ctx)
994{
995 /* There exists a copy-paste-clone in run() */
996 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
997 GNUNET_MQ_hd_fixed_size (peer_check,
998 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
999 struct GNUNET_MessageHeader,
1000 NULL),
1001 GNUNET_MQ_hd_fixed_size (peer_push,
1002 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
1003 struct GNUNET_MessageHeader,
1004 NULL),
1005 GNUNET_MQ_hd_fixed_size (peer_pull_request,
1006 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1007 struct GNUNET_MessageHeader,
1008 NULL),
1009 GNUNET_MQ_hd_var_size (peer_pull_reply,
1010 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
1011 struct GNUNET_RPS_P2P_PullReplyMessage,
1012 NULL),
1013 GNUNET_MQ_handler_end ()
1014 };
1015
1016
1017 if (NULL == peer_ctx->send_channel_ctx)
1018 {
1019 LOG (GNUNET_ERROR_TYPE_DEBUG,
1020 "Trying to establish channel to peer %s\n",
1021 GNUNET_i2s (&peer_ctx->peer_id));
1022 peer_ctx->send_channel_ctx = add_channel_ctx (peer_ctx);
1023 peer_ctx->send_channel_ctx->channel =
1024 GNUNET_CADET_channel_create (cadet_handle,
1025 peer_ctx->send_channel_ctx, /* context */
1026 &peer_ctx->peer_id,
1027 &peer_ctx->sub->hash,
1028 NULL, /* WindowSize handler */
1029 &cleanup_destroyed_channel, /* Disconnect handler */
1030 cadet_handlers);
1031 }
1032 GNUNET_assert (NULL != peer_ctx->send_channel_ctx);
1033 GNUNET_assert (NULL != peer_ctx->send_channel_ctx->channel);
1034 return peer_ctx->send_channel_ctx->channel;
1035}
1036
1037
1038/**
1039 * Get the message queue (#GNUNET_MQ_Handle) of a specific peer.
1040 *
1041 * If we already have a message queue open to this client,
1042 * simply return it, otherwise create one.
1043 *
1044 * @param peer_ctx Context of the peer of which to get the mq
1045 * @return the #GNUNET_MQ_Handle
1046 */
1047static struct GNUNET_MQ_Handle *
1048get_mq (struct PeerContext *peer_ctx)
1049{
1050 if (NULL == peer_ctx->mq)
1051 {
1052 peer_ctx->mq = GNUNET_CADET_get_mq (get_channel (peer_ctx));
1053 }
1054 return peer_ctx->mq;
1055}
1056
1057
1058/**
1059 * @brief Add an envelope to a message passed to mq to list of pending messages
1060 *
1061 * @param peer_ctx Context of the peer for which to insert the envelope
1062 * @param ev envelope to the message
1063 * @param type type of the message to be sent
1064 * @return pointer to pending message
1065 */
1066static struct PendingMessage *
1067insert_pending_message (struct PeerContext *peer_ctx,
1068 struct GNUNET_MQ_Envelope *ev,
1069 const char *type)
1070{
1071 struct PendingMessage *pending_msg;
1072
1073 pending_msg = GNUNET_new (struct PendingMessage);
1074 pending_msg->ev = ev;
1075 pending_msg->peer_ctx = peer_ctx;
1076 pending_msg->type = type;
1077 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
1078 peer_ctx->pending_messages_tail,
1079 pending_msg);
1080 return pending_msg;
1081}
1082
1083
1084/**
1085 * @brief Remove a pending message from the respective DLL
1086 *
1087 * @param pending_msg the pending message to remove
1088 * @param cancel whether to cancel the pending message, too
1089 */
1090static void
1091remove_pending_message (struct PendingMessage *pending_msg, int cancel)
1092{
1093 struct PeerContext *peer_ctx;
1094
1095 (void) cancel;
1096
1097 peer_ctx = pending_msg->peer_ctx;
1098 GNUNET_assert (NULL != peer_ctx);
1099 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
1100 peer_ctx->pending_messages_tail,
1101 pending_msg);
1102 // TODO wait for the cadet implementation of message cancellation
1103 // if (GNUNET_YES == cancel)
1104 // {
1105 // GNUNET_MQ_send_cancel (pending_msg->ev);
1106 // }
1107 GNUNET_free (pending_msg);
1108}
1109
1110
1111/**
1112 * @brief This is called in response to the first message we sent as a
1113 * online check.
1114 *
1115 * @param cls #PeerContext of peer with pending online check
1116 */
1117static void
1118mq_online_check_successful (void *cls)
1119{
1120 struct PeerContext *peer_ctx = cls;
1121
1122 if (NULL != peer_ctx->online_check_pending)
1123 {
1124 LOG (GNUNET_ERROR_TYPE_DEBUG,
1125 "Online check for peer %s was successful\n",
1126 GNUNET_i2s (&peer_ctx->peer_id));
1127 remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES);
1128 peer_ctx->online_check_pending = NULL;
1129 set_peer_online (peer_ctx);
1130 (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers);
1131 }
1132}
1133
1134
1135/**
1136 * Issue a check whether peer is online
1137 *
1138 * @param peer_ctx the context of the peer
1139 */
1140static void
1141check_peer_online (struct PeerContext *peer_ctx)
1142{
1143 LOG (GNUNET_ERROR_TYPE_DEBUG,
1144 "Get informed about peer %s getting online\n",
1145 GNUNET_i2s (&peer_ctx->peer_id));
1146
1147 struct GNUNET_MQ_Handle *mq;
1148 struct GNUNET_MQ_Envelope *ev;
1149
1150 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE);
1151 peer_ctx->online_check_pending =
1152 insert_pending_message (peer_ctx, ev, "Check online");
1153 mq = get_mq (peer_ctx);
1154 GNUNET_MQ_notify_sent (ev,
1155 mq_online_check_successful,
1156 peer_ctx);
1157 GNUNET_MQ_send (mq, ev);
1158 if (peer_ctx->sub == msub)
1159 {
1160 GNUNET_STATISTICS_update (stats,
1161 "# pending online checks",
1162 1,
1163 GNUNET_NO);
1164 }
1165}
1166
1167
1168/**
1169 * @brief Check whether function of type #PeerOp was already scheduled
1170 *
1171 * The array with pending operations will probably never grow really big, so
1172 * iterating over it should be ok.
1173 *
1174 * @param peer_ctx Context of the peer to check for the operation
1175 * @param peer_op the operation (#PeerOp) on the peer
1176 *
1177 * @return #GNUNET_YES if this operation is scheduled on that peer
1178 * #GNUNET_NO otherwise
1179 */
1180static int
1181check_operation_scheduled (const struct PeerContext *peer_ctx,
1182 const PeerOp peer_op)
1183{
1184 unsigned int i;
1185
1186 for (i = 0; i < peer_ctx->num_pending_ops; i++)
1187 if (peer_op == peer_ctx->pending_ops[i].op)
1188 return GNUNET_YES;
1189 return GNUNET_NO;
1190}
1191
1192
1193/**
1194 * @brief Callback for scheduler to destroy a channel
1195 *
1196 * @param channel_ctx Context of the channel
1197 */
1198static void
1199destroy_channel (struct ChannelCtx *channel_ctx)
1200{
1201 struct GNUNET_CADET_Channel *channel;
1202
1203 if (NULL != channel_ctx->destruction_task)
1204 {
1205 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task);
1206 channel_ctx->destruction_task = NULL;
1207 }
1208 GNUNET_assert (channel_ctx->channel != NULL);
1209 channel = channel_ctx->channel;
1210 channel_ctx->channel = NULL;
1211 GNUNET_CADET_channel_destroy (channel);
1212 remove_channel_ctx (channel_ctx);
1213}
1214
1215
1216/**
1217 * @brief Destroy a cadet channel.
1218 *
1219 * This satisfies the function signature of #GNUNET_SCHEDULER_TaskCallback.
1220 *
1221 * @param cls
1222 */
1223static void
1224destroy_channel_cb (void *cls)
1225{
1226 struct ChannelCtx *channel_ctx = cls;
1227
1228 channel_ctx->destruction_task = NULL;
1229 destroy_channel (channel_ctx);
1230}
1231
1232
1233/**
1234 * @brief Schedule the destruction of a channel for immediately afterwards.
1235 *
1236 * In case a channel is to be destroyed from within the callback to the
1237 * destruction of another channel (send channel), we cannot call
1238 * GNUNET_CADET_channel_destroy directly, but need to use this scheduling
1239 * construction.
1240 *
1241 * @param channel_ctx channel to be destroyed.
1242 */
1243static void
1244schedule_channel_destruction (struct ChannelCtx *channel_ctx)
1245{
1246 GNUNET_assert (NULL ==
1247 channel_ctx->destruction_task);
1248 GNUNET_assert (NULL !=
1249 channel_ctx->channel);
1250 channel_ctx->destruction_task =
1251 GNUNET_SCHEDULER_add_now (&destroy_channel_cb,
1252 channel_ctx);
1253}
1254
1255
1256/**
1257 * @brief Remove peer
1258 *
1259 * - Empties the list with pending operations
1260 * - Empties the list with pending messages
1261 * - Cancels potentially existing online check
1262 * - Schedules closing of send and recv channels
1263 * - Removes peer from peer map
1264 *
1265 * @param peer_ctx Context of the peer to be destroyed
1266 * @return #GNUNET_YES if peer was removed
1267 * #GNUNET_NO otherwise
1268 */
1269static int
1270destroy_peer (struct PeerContext *peer_ctx)
1271{
1272 GNUNET_assert (NULL != peer_ctx);
1273 GNUNET_assert (NULL != peer_ctx->sub->peer_map);
1274 if (GNUNET_NO ==
1275 GNUNET_CONTAINER_multipeermap_contains (peer_ctx->sub->peer_map,
1276 &peer_ctx->peer_id))
1277 {
1278 return GNUNET_NO;
1279 }
1280 SET_PEER_FLAG (peer_ctx, Peers_TO_DESTROY);
1281 LOG (GNUNET_ERROR_TYPE_DEBUG,
1282 "Going to remove peer %s\n",
1283 GNUNET_i2s (&peer_ctx->peer_id));
1284 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE);
1285
1286 /* Clear list of pending operations */
1287 // TODO this probably leaks memory
1288 // ('only' the cls to the function. Not sure what to do with it)
1289 GNUNET_array_grow (peer_ctx->pending_ops,
1290 peer_ctx->num_pending_ops,
1291 0);
1292 /* Remove all pending messages */
1293 while (NULL != peer_ctx->pending_messages_head)
1294 {
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Removing unsent %s\n",
1297 peer_ctx->pending_messages_head->type);
1298 /* Cancel pending message, too */
1299 if ((NULL != peer_ctx->online_check_pending) &&
1300 (0 == memcmp (peer_ctx->pending_messages_head,
1301 peer_ctx->online_check_pending,
1302 sizeof(struct PendingMessage))))
1303 {
1304 peer_ctx->online_check_pending = NULL;
1305 if (peer_ctx->sub == msub)
1306 {
1307 GNUNET_STATISTICS_update (stats,
1308 "# pending online checks",
1309 -1,
1310 GNUNET_NO);
1311 }
1312 }
1313 remove_pending_message (peer_ctx->pending_messages_head,
1314 GNUNET_YES);
1315 }
1316
1317 /* If we are still waiting for notification whether this peer is online
1318 * cancel the according task */
1319 if (NULL != peer_ctx->online_check_pending)
1320 {
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Removing pending online check for peer %s\n",
1323 GNUNET_i2s (&peer_ctx->peer_id));
1324 // TODO wait until cadet sets mq->cancel_impl
1325 // GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev);
1326 remove_pending_message (peer_ctx->online_check_pending,
1327 GNUNET_YES);
1328 peer_ctx->online_check_pending = NULL;
1329 }
1330
1331 if (NULL != peer_ctx->send_channel_ctx)
1332 {
1333 /* This is possibly called from within channel destruction */
1334 peer_ctx->send_channel_ctx->peer_ctx = NULL;
1335 schedule_channel_destruction (peer_ctx->send_channel_ctx);
1336 peer_ctx->send_channel_ctx = NULL;
1337 peer_ctx->mq = NULL;
1338 }
1339 if (NULL != peer_ctx->recv_channel_ctx)
1340 {
1341 /* This is possibly called from within channel destruction */
1342 peer_ctx->recv_channel_ctx->peer_ctx = NULL;
1343 schedule_channel_destruction (peer_ctx->recv_channel_ctx);
1344 peer_ctx->recv_channel_ctx = NULL;
1345 }
1346
1347 if (GNUNET_YES !=
1348 GNUNET_CONTAINER_multipeermap_remove_all (peer_ctx->sub->peer_map,
1349 &peer_ctx->peer_id))
1350 {
1351 LOG (GNUNET_ERROR_TYPE_WARNING,
1352 "removing peer from peer_ctx->sub->peer_map failed\n");
1353 }
1354 if (peer_ctx->sub == msub)
1355 {
1356 GNUNET_STATISTICS_set (stats,
1357 "# known peers",
1358 GNUNET_CONTAINER_multipeermap_size (
1359 peer_ctx->sub->peer_map),
1360 GNUNET_NO);
1361 }
1362 GNUNET_free (peer_ctx);
1363 return GNUNET_YES;
1364}
1365
1366
1367/**
1368 * Iterator over hash map entries. Deletes all contexts of peers.
1369 *
1370 * @param cls closure
1371 * @param key current public key
1372 * @param value value in the hash map
1373 * @return #GNUNET_YES if we should continue to iterate,
1374 * #GNUNET_NO if not.
1375 */
1376static int
1377peermap_clear_iterator (void *cls,
1378 const struct GNUNET_PeerIdentity *key,
1379 void *value)
1380{
1381 struct Sub *sub = cls;
1382
1383 (void) value;
1384
1385 destroy_peer (get_peer_ctx (sub->peer_map, key));
1386 return GNUNET_YES;
1387}
1388
1389
1390/**
1391 * @brief This is called once a message is sent.
1392 *
1393 * Removes the pending message
1394 *
1395 * @param cls type of the message that was sent
1396 */
1397static void
1398mq_notify_sent_cb (void *cls)
1399{
1400 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
1401
1402 LOG (GNUNET_ERROR_TYPE_DEBUG,
1403 "%s was sent.\n",
1404 pending_msg->type);
1405 if (pending_msg->peer_ctx->sub == msub)
1406 {
1407 if (0 == strncmp ("PULL REPLY", pending_msg->type, 10))
1408 GNUNET_STATISTICS_update (stats, "# pull replies sent", 1, GNUNET_NO);
1409 if (0 == strncmp ("PULL REQUEST", pending_msg->type, 12))
1410 GNUNET_STATISTICS_update (stats, "# pull requests sent", 1, GNUNET_NO);
1411 if (0 == strncmp ("PUSH", pending_msg->type, 4))
1412 GNUNET_STATISTICS_update (stats, "# pushes sent", 1, GNUNET_NO);
1413 if ((0 == strncmp ("PULL REQUEST", pending_msg->type, 12)) &&
1414 (NULL != map_single_hop) &&
1415 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
1416 &pending_msg->
1417 peer_ctx->peer_id)) )
1418 GNUNET_STATISTICS_update (stats,
1419 "# pull requests sent (multi-hop peer)",
1420 1,
1421 GNUNET_NO);
1422 }
1423 /* Do not cancel message */
1424 remove_pending_message (pending_msg, GNUNET_NO);
1425}
1426
1427
1428/**
1429 * @brief Iterator function for #store_valid_peers.
1430 *
1431 * Implements #GNUNET_CONTAINER_PeerMapIterator.
1432 * Writes single peer to disk.
1433 *
1434 * @param cls the file handle to write to.
1435 * @param peer current peer
1436 * @param value unused
1437 *
1438 * @return #GNUNET_YES if we should continue to
1439 * iterate,
1440 * #GNUNET_NO if not.
1441 */
1442static int
1443store_peer_presistently_iterator (void *cls,
1444 const struct GNUNET_PeerIdentity *peer,
1445 void *value)
1446{
1447 const struct GNUNET_DISK_FileHandle *fh = cls;
1448 char peer_string[128];
1449 int size;
1450 ssize_t ret;
1451
1452 (void) value;
1453
1454 if (NULL == peer)
1455 {
1456 return GNUNET_YES;
1457 }
1458 size = GNUNET_snprintf (peer_string,
1459 sizeof(peer_string),
1460 "%s\n",
1461 GNUNET_i2s_full (peer));
1462 GNUNET_assert (53 == size);
1463 ret = GNUNET_DISK_file_write (fh,
1464 peer_string,
1465 size);
1466 GNUNET_assert (size == ret);
1467 return GNUNET_YES;
1468}
1469
1470
1471/**
1472 * @brief Store the peers currently in #valid_peers to disk.
1473 *
1474 * @param sub Sub for which to store the valid peers
1475 */
1476static void
1477store_valid_peers (const struct Sub *sub)
1478{
1479 struct GNUNET_DISK_FileHandle *fh;
1480 uint32_t number_written_peers;
1481 int ret;
1482
1483 if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7))
1484 {
1485 return;
1486 }
1487
1488 ret = GNUNET_DISK_directory_create_for_file (sub->filename_valid_peers);
1489 if (GNUNET_SYSERR == ret)
1490 {
1491 LOG (GNUNET_ERROR_TYPE_WARNING,
1492 "Not able to create directory for file `%s'\n",
1493 sub->filename_valid_peers);
1494 GNUNET_break (0);
1495 }
1496 else if (GNUNET_NO == ret)
1497 {
1498 LOG (GNUNET_ERROR_TYPE_WARNING,
1499 "Directory for file `%s' exists but is not writable for us\n",
1500 sub->filename_valid_peers);
1501 GNUNET_break (0);
1502 }
1503 fh = GNUNET_DISK_file_open (sub->filename_valid_peers,
1504 GNUNET_DISK_OPEN_WRITE
1505 | GNUNET_DISK_OPEN_CREATE,
1506 GNUNET_DISK_PERM_USER_READ
1507 | GNUNET_DISK_PERM_USER_WRITE);
1508 if (NULL == fh)
1509 {
1510 LOG (GNUNET_ERROR_TYPE_WARNING,
1511 "Not able to write valid peers to file `%s'\n",
1512 sub->filename_valid_peers);
1513 return;
1514 }
1515 LOG (GNUNET_ERROR_TYPE_DEBUG,
1516 "Writing %u valid peers to disk\n",
1517 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1518 number_written_peers =
1519 GNUNET_CONTAINER_multipeermap_iterate (sub->valid_peers,
1520 store_peer_presistently_iterator,
1521 fh);
1522 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1523 GNUNET_assert (number_written_peers ==
1524 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1525}
1526
1527
1528/**
1529 * @brief Convert string representation of peer id to peer id.
1530 *
1531 * Counterpart to #GNUNET_i2s_full.
1532 *
1533 * @param string_repr The string representation of the peer id
1534 *
1535 * @return The peer id
1536 */
1537static const struct GNUNET_PeerIdentity *
1538s2i_full (const char *string_repr)
1539{
1540 struct GNUNET_PeerIdentity *peer;
1541 size_t len;
1542 int ret;
1543
1544 peer = GNUNET_new (struct GNUNET_PeerIdentity);
1545 len = strlen (string_repr);
1546 if (52 > len)
1547 {
1548 LOG (GNUNET_ERROR_TYPE_WARNING,
1549 "Not able to convert string representation of PeerID to PeerID\n"
1550 "String representation: %s (len %lu) - too short\n",
1551 string_repr,
1552 len);
1553 GNUNET_break (0);
1554 }
1555 else if (52 < len)
1556 {
1557 len = 52;
1558 }
1559 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr,
1560 len,
1561 &peer->public_key);
1562 if (GNUNET_OK != ret)
1563 {
1564 LOG (GNUNET_ERROR_TYPE_WARNING,
1565 "Not able to convert string representation of PeerID to PeerID\n"
1566 "String representation: %s\n",
1567 string_repr);
1568 GNUNET_break (0);
1569 }
1570 return peer;
1571}
1572
1573
1574/**
1575 * @brief Restore the peers on disk to #valid_peers.
1576 *
1577 * @param sub Sub for which to restore the valid peers
1578 */
1579static void
1580restore_valid_peers (const struct Sub *sub)
1581{
1582 off_t file_size;
1583 uint32_t num_peers;
1584 struct GNUNET_DISK_FileHandle *fh;
1585 char *buf;
1586 ssize_t size_read;
1587 char *iter_buf;
1588 char *str_repr;
1589 const struct GNUNET_PeerIdentity *peer;
1590
1591 if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7))
1592 {
1593 return;
1594 }
1595
1596 if (GNUNET_OK != GNUNET_DISK_file_test (sub->filename_valid_peers))
1597 {
1598 return;
1599 }
1600 fh = GNUNET_DISK_file_open (sub->filename_valid_peers,
1601 GNUNET_DISK_OPEN_READ,
1602 GNUNET_DISK_PERM_NONE);
1603 GNUNET_assert (NULL != fh);
1604 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size));
1605 num_peers = file_size / 53;
1606 buf = GNUNET_malloc (file_size);
1607 size_read = GNUNET_DISK_file_read (fh, buf, file_size);
1608 GNUNET_assert (size_read == file_size);
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Restoring %" PRIu32 " peers from file `%s'\n",
1611 num_peers,
1612 sub->filename_valid_peers);
1613 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
1614 {
1615 str_repr = GNUNET_strndup (iter_buf, 53);
1616 peer = s2i_full (str_repr);
1617 GNUNET_free (str_repr);
1618 add_valid_peer (peer, sub->valid_peers);
1619 LOG (GNUNET_ERROR_TYPE_DEBUG,
1620 "Restored valid peer %s from disk\n",
1621 GNUNET_i2s_full (peer));
1622 }
1623 iter_buf = NULL;
1624 GNUNET_free (buf);
1625 LOG (GNUNET_ERROR_TYPE_DEBUG,
1626 "num_peers: %" PRIu32 ", _size (sub->valid_peers): %u\n",
1627 num_peers,
1628 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1629 if (num_peers != GNUNET_CONTAINER_multipeermap_size (sub->valid_peers))
1630 {
1631 LOG (GNUNET_ERROR_TYPE_WARNING,
1632 "Number of restored peers does not match file size. Have probably duplicates.\n");
1633 }
1634 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
1635 LOG (GNUNET_ERROR_TYPE_DEBUG,
1636 "Restored %u valid peers from disk\n",
1637 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers));
1638}
1639
1640
1641/**
1642 * @brief Delete storage of peers that was created with #initialise_peers ()
1643 *
1644 * @param sub Sub for which the storage is deleted
1645 */
1646static void
1647peers_terminate (struct Sub *sub)
1648{
1649 if (GNUNET_SYSERR ==
1650 GNUNET_CONTAINER_multipeermap_iterate (sub->peer_map,
1651 &peermap_clear_iterator,
1652 sub))
1653 {
1654 LOG (GNUNET_ERROR_TYPE_WARNING,
1655 "Iteration destroying peers was aborted.\n");
1656 }
1657 GNUNET_CONTAINER_multipeermap_destroy (sub->peer_map);
1658 sub->peer_map = NULL;
1659 store_valid_peers (sub);
1660 GNUNET_free (sub->filename_valid_peers);
1661 sub->filename_valid_peers = NULL;
1662 GNUNET_CONTAINER_multipeermap_destroy (sub->valid_peers);
1663 sub->valid_peers = NULL;
1664}
1665
1666
1667/**
1668 * Iterator over #valid_peers hash map entries.
1669 *
1670 * @param cls Closure that contains iterator function and closure
1671 * @param peer current peer id
1672 * @param value value in the hash map - unused
1673 * @return #GNUNET_YES if we should continue to
1674 * iterate,
1675 * #GNUNET_NO if not.
1676 */
1677static int
1678valid_peer_iterator (void *cls,
1679 const struct GNUNET_PeerIdentity *peer,
1680 void *value)
1681{
1682 struct PeersIteratorCls *it_cls = cls;
1683
1684 (void) value;
1685
1686 return it_cls->iterator (it_cls->cls, peer);
1687}
1688
1689
1690/**
1691 * @brief Get all currently known, valid peer ids.
1692 *
1693 * @param valid_peers Peer map containing the valid peers in question
1694 * @param iterator function to call on each peer id
1695 * @param it_cls extra argument to @a iterator
1696 * @return the number of key value pairs processed,
1697 * #GNUNET_SYSERR if it aborted iteration
1698 */
1699static int
1700get_valid_peers (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1701 PeersIterator iterator,
1702 void *it_cls)
1703{
1704 struct PeersIteratorCls *cls;
1705 int ret;
1706
1707 cls = GNUNET_new (struct PeersIteratorCls);
1708 cls->iterator = iterator;
1709 cls->cls = it_cls;
1710 ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers,
1711 valid_peer_iterator,
1712 cls);
1713 GNUNET_free (cls);
1714 return ret;
1715}
1716
1717
1718/**
1719 * @brief Add peer to known peers.
1720 *
1721 * This function is called on new peer_ids from 'external' sources
1722 * (client seed, cadet get_peers(), ...)
1723 *
1724 * @param sub Sub with the peer map that the @a peer will be added to
1725 * @param peer the new #GNUNET_PeerIdentity
1726 *
1727 * @return #GNUNET_YES if peer was inserted
1728 * #GNUNET_NO otherwise
1729 */
1730static int
1731insert_peer (struct Sub *sub,
1732 const struct GNUNET_PeerIdentity *peer)
1733{
1734 if (GNUNET_YES == check_peer_known (sub->peer_map, peer))
1735 {
1736 return GNUNET_NO; /* We already know this peer - nothing to do */
1737 }
1738 (void) create_peer_ctx (sub, peer);
1739 return GNUNET_YES;
1740}
1741
1742
1743/**
1744 * @brief Check whether flags on a peer are set.
1745 *
1746 * @param peer_map Peer map that is expected to contain the @a peer
1747 * @param peer the peer to check the flag of
1748 * @param flags the flags to check
1749 *
1750 * @return #GNUNET_SYSERR if peer is not known
1751 * #GNUNET_YES if all given flags are set
1752 * #GNUNET_NO otherwise
1753 */
1754static int
1755check_peer_flag (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
1756 const struct GNUNET_PeerIdentity *peer,
1757 enum Peers_PeerFlags flags)
1758{
1759 struct PeerContext *peer_ctx;
1760
1761 if (GNUNET_NO == check_peer_known (peer_map, peer))
1762 {
1763 return GNUNET_SYSERR;
1764 }
1765 peer_ctx = get_peer_ctx (peer_map, peer);
1766 return check_peer_flag_set (peer_ctx, flags);
1767}
1768
1769
1770/**
1771 * @brief Try connecting to a peer to see whether it is online
1772 *
1773 * If not known yet, insert into known peers
1774 *
1775 * @param sub Sub which would contain the @a peer
1776 * @param peer the peer whose online is to be checked
1777 * @return #GNUNET_YES if the check was issued
1778 * #GNUNET_NO otherwise
1779 */
1780static int
1781issue_peer_online_check (struct Sub *sub,
1782 const struct GNUNET_PeerIdentity *peer)
1783{
1784 struct PeerContext *peer_ctx;
1785
1786 (void) insert_peer (sub, peer); // TODO even needed?
1787 peer_ctx = get_peer_ctx (sub->peer_map, peer);
1788 if ((GNUNET_NO == check_peer_flag (sub->peer_map, peer, Peers_ONLINE)) &&
1789 (NULL == peer_ctx->online_check_pending))
1790 {
1791 check_peer_online (peer_ctx);
1792 return GNUNET_YES;
1793 }
1794 return GNUNET_NO;
1795}
1796
1797
1798/**
1799 * @brief Check if peer is removable.
1800 *
1801 * Check if
1802 * - a recv channel exists
1803 * - there are pending messages
1804 * - there is no pending pull reply
1805 *
1806 * @param peer_ctx Context of the peer in question
1807 * @return #GNUNET_YES if peer is removable
1808 * #GNUNET_NO if peer is NOT removable
1809 * #GNUNET_SYSERR if peer is not known
1810 */
1811static int
1812check_removable (const struct PeerContext *peer_ctx)
1813{
1814 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (
1815 peer_ctx->sub->peer_map,
1816 &peer_ctx->peer_id))
1817 {
1818 return GNUNET_SYSERR;
1819 }
1820
1821 if ((NULL != peer_ctx->recv_channel_ctx) ||
1822 (NULL != peer_ctx->pending_messages_head) ||
1823 (GNUNET_YES == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)))
1824 {
1825 return GNUNET_NO;
1826 }
1827 return GNUNET_YES;
1828}
1829
1830
1831/**
1832 * @brief Check whether @a peer is actually a peer.
1833 *
1834 * A valid peer is a peer that we know exists eg. we were connected to once.
1835 *
1836 * @param valid_peers Peer map that would contain the @a peer
1837 * @param peer peer in question
1838 *
1839 * @return #GNUNET_YES if peer is valid
1840 * #GNUNET_NO if peer is not valid
1841 */
1842static int
1843check_peer_valid (const struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1844 const struct GNUNET_PeerIdentity *peer)
1845{
1846 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer);
1847}
1848
1849
1850/**
1851 * @brief Indicate that we want to send to the other peer
1852 *
1853 * This establishes a sending channel
1854 *
1855 * @param peer_ctx Context of the target peer
1856 */
1857static void
1858indicate_sending_intention (struct PeerContext *peer_ctx)
1859{
1860 GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map,
1861 &peer_ctx->peer_id));
1862 (void) get_channel (peer_ctx);
1863}
1864
1865
1866/**
1867 * @brief Check whether other peer has the intention to send/opened channel
1868 * towars us
1869 *
1870 * @param peer_ctx Context of the peer in question
1871 *
1872 * @return #GNUNET_YES if peer has the intention to send
1873 * #GNUNET_NO otherwise
1874 */
1875static int
1876check_peer_send_intention (const struct PeerContext *peer_ctx)
1877{
1878 if (NULL != peer_ctx->recv_channel_ctx)
1879 {
1880 return GNUNET_YES;
1881 }
1882 return GNUNET_NO;
1883}
1884
1885
1886/**
1887 * Handle the channel a peer opens to us.
1888 *
1889 * @param cls The closure - Sub
1890 * @param channel The channel the peer wants to establish
1891 * @param initiator The peer's peer ID
1892 *
1893 * @return initial channel context for the channel
1894 * (can be NULL -- that's not an error)
1895 */
1896static void *
1897handle_inbound_channel (void *cls,
1898 struct GNUNET_CADET_Channel *channel,
1899 const struct GNUNET_PeerIdentity *initiator)
1900{
1901 struct PeerContext *peer_ctx;
1902 struct ChannelCtx *channel_ctx;
1903 struct Sub *sub = cls;
1904
1905 LOG (GNUNET_ERROR_TYPE_DEBUG,
1906 "New channel was established to us (Peer %s).\n",
1907 GNUNET_i2s (initiator));
1908 GNUNET_assert (NULL != channel); /* according to cadet API */
1909 /* Make sure we 'know' about this peer */
1910 peer_ctx = create_or_get_peer_ctx (sub, initiator);
1911 set_peer_online (peer_ctx);
1912 (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers);
1913 channel_ctx = add_channel_ctx (peer_ctx);
1914 channel_ctx->channel = channel;
1915 /* We only accept one incoming channel per peer */
1916 if (GNUNET_YES == check_peer_send_intention (get_peer_ctx (sub->peer_map,
1917 initiator)))
1918 {
1919 LOG (GNUNET_ERROR_TYPE_WARNING,
1920 "Already got one receive channel. Destroying old one.\n");
1921 GNUNET_break_op (0);
1922 destroy_channel (peer_ctx->recv_channel_ctx);
1923 peer_ctx->recv_channel_ctx = channel_ctx;
1924 /* return the channel context */
1925 return channel_ctx;
1926 }
1927 peer_ctx->recv_channel_ctx = channel_ctx;
1928 return channel_ctx;
1929}
1930
1931
1932/**
1933 * @brief Check whether a sending channel towards the given peer exists
1934 *
1935 * @param peer_ctx Context of the peer in question
1936 *
1937 * @return #GNUNET_YES if a sending channel towards that peer exists
1938 * #GNUNET_NO otherwise
1939 */
1940static int
1941check_sending_channel_exists (const struct PeerContext *peer_ctx)
1942{
1943 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
1944 &peer_ctx->peer_id))
1945 { /* If no such peer exists, there is no channel */
1946 return GNUNET_NO;
1947 }
1948 if (NULL == peer_ctx->send_channel_ctx)
1949 {
1950 return GNUNET_NO;
1951 }
1952 return GNUNET_YES;
1953}
1954
1955
1956/**
1957 * @brief Destroy the send channel of a peer e.g. stop indicating a sending
1958 * intention to another peer
1959 *
1960 * @param peer_ctx Context to the peer
1961 * @return #GNUNET_YES if channel was destroyed
1962 * #GNUNET_NO otherwise
1963 */
1964static int
1965destroy_sending_channel (struct PeerContext *peer_ctx)
1966{
1967 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
1968 &peer_ctx->peer_id))
1969 {
1970 return GNUNET_NO;
1971 }
1972 if (NULL != peer_ctx->send_channel_ctx)
1973 {
1974 destroy_channel (peer_ctx->send_channel_ctx);
1975 (void) check_connected (peer_ctx);
1976 return GNUNET_YES;
1977 }
1978 return GNUNET_NO;
1979}
1980
1981
1982/**
1983 * @brief Send a message to another peer.
1984 *
1985 * Keeps track about pending messages so they can be properly removed when the
1986 * peer is destroyed.
1987 *
1988 * @param peer_ctx Context of the peer to which the message is to be sent
1989 * @param ev envelope of the message
1990 * @param type type of the message
1991 */
1992static void
1993send_message (struct PeerContext *peer_ctx,
1994 struct GNUNET_MQ_Envelope *ev,
1995 const char *type)
1996{
1997 struct PendingMessage *pending_msg;
1998 struct GNUNET_MQ_Handle *mq;
1999
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "Sending message to %s of type %s\n",
2002 GNUNET_i2s (&peer_ctx->peer_id),
2003 type);
2004 pending_msg = insert_pending_message (peer_ctx, ev, type);
2005 mq = get_mq (peer_ctx);
2006 GNUNET_MQ_notify_sent (ev,
2007 mq_notify_sent_cb,
2008 pending_msg);
2009 GNUNET_MQ_send (mq, ev);
2010}
2011
2012
2013/**
2014 * @brief Schedule a operation on given peer
2015 *
2016 * Avoids scheduling an operation twice.
2017 *
2018 * @param peer_ctx Context of the peer for which to schedule the operation
2019 * @param peer_op the operation to schedule
2020 * @param cls Closure to @a peer_op
2021 *
2022 * @return #GNUNET_YES if the operation was scheduled
2023 * #GNUNET_NO otherwise
2024 */
2025static int
2026schedule_operation (struct PeerContext *peer_ctx,
2027 const PeerOp peer_op,
2028 void *cls)
2029{
2030 struct PeerPendingOp pending_op;
2031
2032 GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map,
2033 &peer_ctx->peer_id));
2034
2035 // TODO if ONLINE execute immediately
2036
2037 if (GNUNET_NO == check_operation_scheduled (peer_ctx, peer_op))
2038 {
2039 pending_op.op = peer_op;
2040 pending_op.op_cls = cls;
2041 GNUNET_array_append (peer_ctx->pending_ops,
2042 peer_ctx->num_pending_ops,
2043 pending_op);
2044 return GNUNET_YES;
2045 }
2046 return GNUNET_NO;
2047}
2048
2049
2050/***********************************************************************
2051* /Old gnunet-service-rps_peers.c
2052***********************************************************************/
2053
2054
2055/***********************************************************************
2056* Housekeeping with clients
2057***********************************************************************/
2058
2059/**
2060 * Closure used to pass the client and the id to the callback
2061 * that replies to a client's request
2062 */
2063struct ReplyCls
2064{
2065 /**
2066 * DLL
2067 */
2068 struct ReplyCls *next;
2069 struct ReplyCls *prev;
2070
2071 /**
2072 * The identifier of the request
2073 */
2074 uint32_t id;
2075
2076 /**
2077 * The handle to the request
2078 */
2079 struct RPS_SamplerRequestHandle *req_handle;
2080
2081 /**
2082 * The client handle to send the reply to
2083 */
2084 struct ClientContext *cli_ctx;
2085};
2086
2087
2088/**
2089 * Struct used to store the context of a connected client.
2090 */
2091struct ClientContext
2092{
2093 /**
2094 * DLL
2095 */
2096 struct ClientContext *next;
2097 struct ClientContext *prev;
2098
2099 /**
2100 * The message queue to communicate with the client.
2101 */
2102 struct GNUNET_MQ_Handle *mq;
2103
2104 /**
2105 * @brief How many updates this client expects to receive.
2106 */
2107 int64_t view_updates_left;
2108
2109 /**
2110 * @brief Whether this client wants to receive stream updates.
2111 * Either #GNUNET_YES or #GNUNET_NO
2112 */
2113 int8_t stream_update;
2114
2115 /**
2116 * The client handle to send the reply to
2117 */
2118 struct GNUNET_SERVICE_Client *client;
2119
2120 /**
2121 * The #Sub this context belongs to
2122 */
2123 struct Sub *sub;
2124};
2125
2126/**
2127 * DLL with all clients currently connected to us
2128 */
2129struct ClientContext *cli_ctx_head;
2130struct ClientContext *cli_ctx_tail;
2131
2132/***********************************************************************
2133* /Housekeeping with clients
2134***********************************************************************/
2135
2136
2137/***********************************************************************
2138* Util functions
2139***********************************************************************/
2140
2141
2142/**
2143 * Print peerlist to log.
2144 */
2145static void
2146print_peer_list (struct GNUNET_PeerIdentity *list,
2147 unsigned int len)
2148{
2149 unsigned int i;
2150
2151 LOG (GNUNET_ERROR_TYPE_DEBUG,
2152 "Printing peer list of length %u at %p:\n",
2153 len,
2154 list);
2155 for (i = 0; i < len; i++)
2156 {
2157 LOG (GNUNET_ERROR_TYPE_DEBUG,
2158 "%u. peer: %s\n",
2159 i, GNUNET_i2s (&list[i]));
2160 }
2161}
2162
2163
2164/**
2165 * Remove peer from list.
2166 */
2167static void
2168rem_from_list (struct GNUNET_PeerIdentity **peer_list,
2169 unsigned int *list_size,
2170 const struct GNUNET_PeerIdentity *peer)
2171{
2172 unsigned int i;
2173 struct GNUNET_PeerIdentity *tmp;
2174
2175 tmp = *peer_list;
2176
2177 LOG (GNUNET_ERROR_TYPE_DEBUG,
2178 "Removing peer %s from list at %p\n",
2179 GNUNET_i2s (peer),
2180 tmp);
2181
2182 for (i = 0; i < *list_size; i++)
2183 {
2184 if (0 == GNUNET_memcmp (&tmp[i], peer))
2185 {
2186 if (i < *list_size - 1)
2187 { /* Not at the last entry -- shift peers left */
2188 memmove (&tmp[i], &tmp[i + 1],
2189 ((*list_size) - i - 1) * sizeof(struct GNUNET_PeerIdentity));
2190 }
2191 /* Remove last entry (should be now useless PeerID) */
2192 GNUNET_array_grow (tmp, *list_size, (*list_size) - 1);
2193 }
2194 }
2195 *peer_list = tmp;
2196}
2197
2198
2199/**
2200 * Insert PeerID in #view
2201 *
2202 * Called once we know a peer is online.
2203 * Implements #PeerOp
2204 *
2205 * @return GNUNET_OK if peer was actually inserted
2206 * GNUNET_NO if peer was not inserted
2207 */
2208static void
2209insert_in_view_op (void *cls,
2210 const struct GNUNET_PeerIdentity *peer);
2211
2212/**
2213 * Insert PeerID in #view
2214 *
2215 * Called once we know a peer is online.
2216 *
2217 * @param sub Sub in with the view to insert in
2218 * @param peer the peer to insert
2219 *
2220 * @return GNUNET_OK if peer was actually inserted
2221 * GNUNET_NO if peer was not inserted
2222 */
2223static int
2224insert_in_view (struct Sub *sub,
2225 const struct GNUNET_PeerIdentity *peer)
2226{
2227 struct PeerContext *peer_ctx;
2228 int online;
2229 int ret;
2230
2231 online = check_peer_flag (sub->peer_map, peer, Peers_ONLINE);
2232 peer_ctx = get_peer_ctx (sub->peer_map, peer); // TODO indirection needed?
2233 if ((GNUNET_NO == online) ||
2234 (GNUNET_SYSERR == online)) /* peer is not even known */
2235 {
2236 (void) issue_peer_online_check (sub, peer);
2237 (void) schedule_operation (peer_ctx, insert_in_view_op, sub);
2238 return GNUNET_NO;
2239 }
2240 /* Open channel towards peer to keep connection open */
2241 indicate_sending_intention (peer_ctx);
2242 ret = View_put (sub->view, peer);
2243 if (peer_ctx->sub == msub)
2244 {
2245 GNUNET_STATISTICS_set (stats,
2246 "view size",
2247 View_size (peer_ctx->sub->view),
2248 GNUNET_NO);
2249 }
2250 return ret;
2251}
2252
2253
2254/**
2255 * @brief Send view to client
2256 *
2257 * @param cli_ctx the context of the client
2258 * @param view_array the peerids of the view as array (can be empty)
2259 * @param view_size the size of the view array (can be 0)
2260 */
2261static void
2262send_view (const struct ClientContext *cli_ctx,
2263 const struct GNUNET_PeerIdentity *view_array,
2264 uint64_t view_size)
2265{
2266 struct GNUNET_MQ_Envelope *ev;
2267 struct GNUNET_RPS_CS_DEBUG_ViewReply *out_msg;
2268 struct Sub *sub;
2269
2270 if (NULL == view_array)
2271 {
2272 if (NULL == cli_ctx->sub)
2273 sub = msub;
2274 else
2275 sub = cli_ctx->sub;
2276 view_size = View_size (sub->view);
2277 view_array = View_get_as_array (sub->view);
2278 }
2279
2280 ev = GNUNET_MQ_msg_extra (out_msg,
2281 view_size * sizeof(struct GNUNET_PeerIdentity),
2282 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY);
2283 out_msg->num_peers = htonl (view_size);
2284
2285 GNUNET_memcpy (&out_msg[1],
2286 view_array,
2287 view_size * sizeof(struct GNUNET_PeerIdentity));
2288 GNUNET_MQ_send (cli_ctx->mq, ev);
2289}
2290
2291
2292/**
2293 * @brief Send peer from biased stream to client.
2294 *
2295 * TODO merge with send_view, parameterise
2296 *
2297 * @param cli_ctx the context of the client
2298 * @param view_array the peerids of the view as array (can be empty)
2299 * @param view_size the size of the view array (can be 0)
2300 */
2301static void
2302send_stream_peers (const struct ClientContext *cli_ctx,
2303 uint64_t num_peers,
2304 const struct GNUNET_PeerIdentity *peers)
2305{
2306 struct GNUNET_MQ_Envelope *ev;
2307 struct GNUNET_RPS_CS_DEBUG_StreamReply *out_msg;
2308
2309 GNUNET_assert (NULL != peers);
2310
2311 ev = GNUNET_MQ_msg_extra (out_msg,
2312 num_peers * sizeof(struct GNUNET_PeerIdentity),
2313 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY);
2314 out_msg->num_peers = htonl (num_peers);
2315
2316 GNUNET_memcpy (&out_msg[1],
2317 peers,
2318 num_peers * sizeof(struct GNUNET_PeerIdentity));
2319 GNUNET_MQ_send (cli_ctx->mq, ev);
2320}
2321
2322
2323/**
2324 * @brief sends updates to clients that are interested
2325 *
2326 * @param sub Sub for which to notify clients
2327 */
2328static void
2329clients_notify_view_update (const struct Sub *sub)
2330{
2331 struct ClientContext *cli_ctx_iter;
2332 uint64_t num_peers;
2333 const struct GNUNET_PeerIdentity *view_array;
2334
2335 num_peers = View_size (sub->view);
2336 view_array = View_get_as_array (sub->view);
2337 /* check size of view is small enough */
2338 if (GNUNET_MAX_MESSAGE_SIZE < num_peers)
2339 {
2340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2341 "View is too big to send\n");
2342 return;
2343 }
2344
2345 for (cli_ctx_iter = cli_ctx_head;
2346 NULL != cli_ctx_iter;
2347 cli_ctx_iter = cli_ctx_iter->next)
2348 {
2349 if (1 < cli_ctx_iter->view_updates_left)
2350 {
2351 /* Client wants to receive limited amount of updates */
2352 cli_ctx_iter->view_updates_left -= 1;
2353 }
2354 else if (1 == cli_ctx_iter->view_updates_left)
2355 {
2356 /* Last update of view for client */
2357 cli_ctx_iter->view_updates_left = -1;
2358 }
2359 else if (0 > cli_ctx_iter->view_updates_left)
2360 {
2361 /* Client is not interested in updates */
2362 continue;
2363 }
2364 /* else _updates_left == 0 - infinite amount of updates */
2365
2366 /* send view */
2367 send_view (cli_ctx_iter, view_array, num_peers);
2368 }
2369}
2370
2371
2372/**
2373 * @brief sends updates to clients that are interested
2374 *
2375 * @param num_peers Number of peers to send
2376 * @param peers the array of peers to send
2377 */
2378static void
2379clients_notify_stream_peer (const struct Sub *sub,
2380 uint64_t num_peers,
2381 const struct GNUNET_PeerIdentity *peers)
2382// TODO enum StreamPeerSource)
2383{
2384 struct ClientContext *cli_ctx_iter;
2385
2386 LOG (GNUNET_ERROR_TYPE_DEBUG,
2387 "Got peer (%s) from biased stream - update all clients\n",
2388 GNUNET_i2s (peers));
2389
2390 for (cli_ctx_iter = cli_ctx_head;
2391 NULL != cli_ctx_iter;
2392 cli_ctx_iter = cli_ctx_iter->next)
2393 {
2394 if ((GNUNET_YES == cli_ctx_iter->stream_update) &&
2395 ((sub == cli_ctx_iter->sub) || (sub == msub) ))
2396 {
2397 send_stream_peers (cli_ctx_iter, num_peers, peers);
2398 }
2399 }
2400}
2401
2402
2403/**
2404 * Put random peer from sampler into the view as history update.
2405 *
2406 * @param ids Array of Peers to insert into view
2407 * @param num_peers Number of peers to insert
2408 * @param cls Closure - The Sub for which this is to be done
2409 */
2410static void
2411hist_update (const struct GNUNET_PeerIdentity *ids,
2412 uint32_t num_peers,
2413 void *cls)
2414{
2415 unsigned int i;
2416 struct Sub *sub = cls;
2417
2418 for (i = 0; i < num_peers; i++)
2419 {
2420 int inserted;
2421 if (GNUNET_YES != check_peer_known (sub->peer_map, &ids[i]))
2422 {
2423 LOG (GNUNET_ERROR_TYPE_WARNING,
2424 "Peer in history update not known!\n");
2425 continue;
2426 }
2427 inserted = insert_in_view (sub, &ids[i]);
2428 if (GNUNET_OK == inserted)
2429 {
2430 clients_notify_stream_peer (sub, 1, &ids[i]);
2431 }
2432#ifdef TO_FILE_FULL
2433 to_file (sub->file_name_view_log,
2434 "+%s\t(history)",
2435 GNUNET_i2s_full (ids));
2436#endif /* TO_FILE_FULL */
2437 }
2438 clients_notify_view_update (sub);
2439}
2440
2441
2442/**
2443 * Wrapper around #RPS_sampler_resize()
2444 *
2445 * If we do not have enough sampler elements, double current sampler size
2446 * If we have more than enough sampler elements, halv current sampler size
2447 *
2448 * @param sampler The sampler to resize
2449 * @param new_size New size to which to resize
2450 */
2451static void
2452resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
2453{
2454 unsigned int sampler_size;
2455
2456 // TODO statistics
2457 // TODO respect the min, max
2458 sampler_size = RPS_sampler_get_size (sampler);
2459 if (sampler_size > new_size * 4)
2460 { /* Shrinking */
2461 RPS_sampler_resize (sampler, sampler_size / 2);
2462 }
2463 else if (sampler_size < new_size)
2464 { /* Growing */
2465 RPS_sampler_resize (sampler, sampler_size * 2);
2466 }
2467 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
2468}
2469
2470
2471#if ENABLE_MALICIOUS
2472/**
2473 * Add all peers in @a peer_array to @a peer_map used as set.
2474 *
2475 * @param peer_array array containing the peers
2476 * @param num_peers number of peers in @peer_array
2477 * @param peer_map the peermap to use as set
2478 */
2479static void
2480add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
2481 unsigned int num_peers,
2482 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
2483{
2484 unsigned int i;
2485
2486 if (NULL == peer_map)
2487 {
2488 LOG (GNUNET_ERROR_TYPE_WARNING,
2489 "Trying to add peers to non-existing peermap.\n");
2490 return;
2491 }
2492
2493 for (i = 0; i < num_peers; i++)
2494 {
2495 GNUNET_CONTAINER_multipeermap_put (peer_map,
2496 &peer_array[i],
2497 NULL,
2498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2499 if (msub->peer_map == peer_map)
2500 {
2501 GNUNET_STATISTICS_set (stats,
2502 "# known peers",
2503 GNUNET_CONTAINER_multipeermap_size (peer_map),
2504 GNUNET_NO);
2505 }
2506 }
2507}
2508
2509
2510#endif /* ENABLE_MALICIOUS */
2511
2512
2513/**
2514 * Send a PULL REPLY to @a peer_id
2515 *
2516 * @param peer_ctx Context of the peer to send the reply to
2517 * @param peer_ids the peers to send to @a peer_id
2518 * @param num_peer_ids the number of peers to send to @a peer_id
2519 */
2520static void
2521send_pull_reply (struct PeerContext *peer_ctx,
2522 const struct GNUNET_PeerIdentity *peer_ids,
2523 unsigned int num_peer_ids)
2524{
2525 uint32_t send_size;
2526 struct GNUNET_MQ_Envelope *ev;
2527 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
2528
2529 /* Compute actual size */
2530 send_size = sizeof(struct GNUNET_RPS_P2P_PullReplyMessage)
2531 + num_peer_ids * sizeof(struct GNUNET_PeerIdentity);
2532
2533 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
2534 /* Compute number of peers to send
2535 * If too long, simply truncate */
2536 // TODO select random ones via permutation
2537 // or even better: do good protocol design
2538 send_size =
2539 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE
2540 - sizeof(struct GNUNET_RPS_P2P_PullReplyMessage))
2541 / sizeof(struct GNUNET_PeerIdentity);
2542 else
2543 send_size = num_peer_ids;
2544
2545 LOG (GNUNET_ERROR_TYPE_DEBUG,
2546 "Going to send PULL REPLY with %u peers to %s\n",
2547 send_size, GNUNET_i2s (&peer_ctx->peer_id));
2548
2549 ev = GNUNET_MQ_msg_extra (out_msg,
2550 send_size * sizeof(struct GNUNET_PeerIdentity),
2551 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
2552 out_msg->num_peers = htonl (send_size);
2553 GNUNET_memcpy (&out_msg[1], peer_ids,
2554 send_size * sizeof(struct GNUNET_PeerIdentity));
2555
2556 send_message (peer_ctx, ev, "PULL REPLY");
2557 if (peer_ctx->sub == msub)
2558 {
2559 GNUNET_STATISTICS_update (stats, "# pull reply send issued", 1, GNUNET_NO);
2560 }
2561 // TODO check with send intention: as send_channel is used/opened we indicate
2562 // a sending intention without intending it.
2563 // -> clean peer afterwards?
2564 // -> use recv_channel?
2565}
2566
2567
2568/**
2569 * Insert PeerID in #pull_map
2570 *
2571 * Called once we know a peer is online.
2572 *
2573 * @param cls Closure - Sub with the pull map to insert into
2574 * @param peer Peer to insert
2575 */
2576static void
2577insert_in_pull_map (void *cls,
2578 const struct GNUNET_PeerIdentity *peer)
2579{
2580 struct Sub *sub = cls;
2581
2582 CustomPeerMap_put (sub->pull_map, peer);
2583}
2584
2585
2586/**
2587 * Insert PeerID in #view
2588 *
2589 * Called once we know a peer is online.
2590 * Implements #PeerOp
2591 *
2592 * @param cls Closure - Sub with view to insert peer into
2593 * @param peer the peer to insert
2594 */
2595static void
2596insert_in_view_op (void *cls,
2597 const struct GNUNET_PeerIdentity *peer)
2598{
2599 struct Sub *sub = cls;
2600 int inserted;
2601
2602 inserted = insert_in_view (sub, peer);
2603 if (GNUNET_OK == inserted)
2604 {
2605 clients_notify_stream_peer (sub, 1, peer);
2606 }
2607}
2608
2609
2610/**
2611 * Update sampler with given PeerID.
2612 * Implements #PeerOp
2613 *
2614 * @param cls Closure - Sub containing the sampler to insert into
2615 * @param peer Peer to insert
2616 */
2617static void
2618insert_in_sampler (void *cls,
2619 const struct GNUNET_PeerIdentity *peer)
2620{
2621 struct Sub *sub = cls;
2622
2623 LOG (GNUNET_ERROR_TYPE_DEBUG,
2624 "Updating samplers with peer %s from insert_in_sampler()\n",
2625 GNUNET_i2s (peer));
2626 RPS_sampler_update (sub->sampler, peer);
2627 if (0 < RPS_sampler_count_id (sub->sampler, peer))
2628 {
2629 /* Make sure we 'know' about this peer */
2630 (void) issue_peer_online_check (sub, peer);
2631 /* Establish a channel towards that peer to indicate we are going to send
2632 * messages to it */
2633 // indicate_sending_intention (peer);
2634 }
2635 if (sub == msub)
2636 {
2637 GNUNET_STATISTICS_update (stats,
2638 "# observed peers in gossip",
2639 1,
2640 GNUNET_NO);
2641 }
2642#ifdef TO_FILE
2643 sub->num_observed_peers++;
2644 (void) GNUNET_CONTAINER_multipeermap_put
2645 (sub->observed_unique_peers,
2646 peer,
2647 NULL,
2648 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2649 uint32_t num_observed_unique_peers =
2650 GNUNET_CONTAINER_multipeermap_size (sub->observed_unique_peers);
2651 GNUNET_STATISTICS_set (stats,
2652 "# unique peers in gossip",
2653 num_observed_unique_peers,
2654 GNUNET_NO);
2655#ifdef TO_FILE_FULL
2656 to_file (sub->file_name_observed_log,
2657 "%" PRIu32 " %" PRIu32 " %f\n",
2658 sub->num_observed_peers,
2659 num_observed_unique_peers,
2660 1.0 * num_observed_unique_peers / sub->num_observed_peers)
2661#endif /* TO_FILE_FULL */
2662#endif /* TO_FILE */
2663}
2664
2665
2666/**
2667 * @brief This is called on peers from external sources (cadet, peerinfo, ...)
2668 * If the peer is not known, online check is issued and it is
2669 * scheduled to be inserted in sampler and view.
2670 *
2671 * "External sources" refer to every source except the gossip.
2672 *
2673 * @param sub Sub for which @a peer was received
2674 * @param peer peer to insert/peer received
2675 */
2676static void
2677got_peer (struct Sub *sub,
2678 const struct GNUNET_PeerIdentity *peer)
2679{
2680 /* If we did not know this peer already, insert it into sampler and view */
2681 if (GNUNET_YES == issue_peer_online_check (sub, peer))
2682 {
2683 schedule_operation (get_peer_ctx (sub->peer_map, peer),
2684 &insert_in_sampler, sub);
2685 schedule_operation (get_peer_ctx (sub->peer_map, peer),
2686 &insert_in_view_op, sub);
2687 }
2688 if (sub == msub)
2689 {
2690 GNUNET_STATISTICS_update (stats,
2691 "# learnd peers",
2692 1,
2693 GNUNET_NO);
2694 }
2695}
2696
2697
2698/**
2699 * @brief Checks if there is a sending channel and if it is needed
2700 *
2701 * @param peer_ctx Context of the peer to check
2702 * @return GNUNET_YES if sending channel exists and is still needed
2703 * GNUNET_NO otherwise
2704 */
2705static int
2706check_sending_channel_needed (const struct PeerContext *peer_ctx)
2707{
2708 /* struct GNUNET_CADET_Channel *channel; */
2709 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map,
2710 &peer_ctx->peer_id))
2711 {
2712 return GNUNET_NO;
2713 }
2714 if (GNUNET_YES == check_sending_channel_exists (peer_ctx))
2715 {
2716 if ((0 < RPS_sampler_count_id (peer_ctx->sub->sampler,
2717 &peer_ctx->peer_id)) ||
2718 (GNUNET_YES == View_contains_peer (peer_ctx->sub->view,
2719 &peer_ctx->peer_id)) ||
2720 (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->push_map,
2721 &peer_ctx->peer_id)) ||
2722 (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->pull_map,
2723 &peer_ctx->peer_id)) ||
2724 (GNUNET_YES == check_peer_flag (peer_ctx->sub->peer_map,
2725 &peer_ctx->peer_id,
2726 Peers_PULL_REPLY_PENDING)))
2727 { /* If we want to keep the connection to peer open */
2728 return GNUNET_YES;
2729 }
2730 return GNUNET_NO;
2731 }
2732 return GNUNET_NO;
2733}
2734
2735
2736/**
2737 * @brief remove peer from our knowledge, the view, push and pull maps and
2738 * samplers.
2739 *
2740 * @param sub Sub with the data structures the peer is to be removed from
2741 * @param peer the peer to remove
2742 */
2743static void
2744remove_peer (struct Sub *sub,
2745 const struct GNUNET_PeerIdentity *peer)
2746{
2747 (void) View_remove_peer (sub->view,
2748 peer);
2749 CustomPeerMap_remove_peer (sub->pull_map,
2750 peer);
2751 CustomPeerMap_remove_peer (sub->push_map,
2752 peer);
2753 RPS_sampler_reinitialise_by_value (sub->sampler,
2754 peer);
2755 /* We want to destroy the peer now.
2756 * Sometimes, it just seems that it's already been removed from the peer_map,
2757 * so check the peer_map first. */
2758 if (GNUNET_YES == check_peer_known (sub->peer_map,
2759 peer))
2760 {
2761 destroy_peer (get_peer_ctx (sub->peer_map,
2762 peer));
2763 }
2764}
2765
2766
2767/**
2768 * @brief Remove data that is not needed anymore.
2769 *
2770 * If the sending channel is no longer needed it is destroyed.
2771 *
2772 * @param sub Sub in which the current peer is to be cleaned
2773 * @param peer the peer whose data is about to be cleaned
2774 */
2775static void
2776clean_peer (struct Sub *sub,
2777 const struct GNUNET_PeerIdentity *peer)
2778{
2779 if (GNUNET_NO == check_sending_channel_needed (get_peer_ctx (sub->peer_map,
2780 peer)))
2781 {
2782 LOG (GNUNET_ERROR_TYPE_DEBUG,
2783 "Going to remove send channel to peer %s\n",
2784 GNUNET_i2s (peer));
2785 #if ENABLE_MALICIOUS
2786 if (0 != GNUNET_memcmp (&attacked_peer,
2787 peer))
2788 (void) destroy_sending_channel (get_peer_ctx (sub->peer_map,
2789 peer));
2790 #else /* ENABLE_MALICIOUS */
2791 (void) destroy_sending_channel (get_peer_ctx (sub->peer_map,
2792 peer));
2793 #endif /* ENABLE_MALICIOUS */
2794 }
2795
2796 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (sub->peer_map,
2797 peer))
2798 {
2799 /* Peer was already removed by callback on destroyed channel */
2800 LOG (GNUNET_ERROR_TYPE_WARNING,
2801 "Peer was removed from our knowledge during cleanup\n");
2802 return;
2803 }
2804
2805 if ((GNUNET_NO == check_peer_send_intention (get_peer_ctx (sub->peer_map,
2806 peer))) &&
2807 (GNUNET_NO == View_contains_peer (sub->view, peer)) &&
2808 (GNUNET_NO == CustomPeerMap_contains_peer (sub->push_map, peer)) &&
2809 (GNUNET_NO == CustomPeerMap_contains_peer (sub->pull_map, peer)) &&
2810 (0 == RPS_sampler_count_id (sub->sampler, peer)) &&
2811 (GNUNET_YES == check_removable (get_peer_ctx (sub->peer_map, peer))))
2812 { /* We can safely remove this peer */
2813 LOG (GNUNET_ERROR_TYPE_DEBUG,
2814 "Going to remove peer %s\n",
2815 GNUNET_i2s (peer));
2816 remove_peer (sub, peer);
2817 return;
2818 }
2819}
2820
2821
2822/**
2823 * @brief This is called when a channel is destroyed.
2824 *
2825 * Removes peer completely from our knowledge if the send_channel was destroyed
2826 * Otherwise simply delete the recv_channel
2827 * Also check if the knowledge about this peer is still needed.
2828 * If not, remove this peer from our knowledge.
2829 *
2830 * @param cls The closure - Context to the channel
2831 * @param channel The channel being closed
2832 */
2833static void
2834cleanup_destroyed_channel (void *cls,
2835 const struct GNUNET_CADET_Channel *channel)
2836{
2837 struct ChannelCtx *channel_ctx = cls;
2838 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
2839
2840 (void) channel;
2841
2842 channel_ctx->channel = NULL;
2843 if ((NULL != peer_ctx) &&
2844 (peer_ctx->send_channel_ctx == channel_ctx) &&
2845 (GNUNET_YES == check_sending_channel_needed (peer_ctx)) )
2846 {
2847 remove_channel_ctx (channel_ctx);
2848 remove_peer (peer_ctx->sub, &peer_ctx->peer_id);
2849 }
2850 else
2851 {
2852 /* We need this if-else construct because we need to make sure the channel
2853 * (context) is cleaned up before removing the peer, but still need to
2854 * compare it while checking the condition */
2855 remove_channel_ctx (channel_ctx);
2856 }
2857}
2858
2859
2860/***********************************************************************
2861* /Util functions
2862***********************************************************************/
2863
2864
2865/***********************************************************************
2866* Sub
2867***********************************************************************/
2868
2869/**
2870 * @brief Create a new Sub
2871 *
2872 * @param hash Hash of value shared among rps instances on other hosts that
2873 * defines a subgroup to sample from.
2874 * @param sampler_size Size of the sampler
2875 * @param round_interval Interval (in average) between two rounds
2876 *
2877 * @return Sub
2878 */
2879struct Sub *
2880new_sub (const struct GNUNET_HashCode *hash,
2881 uint32_t sampler_size,
2882 struct GNUNET_TIME_Relative round_interval)
2883{
2884 struct Sub *sub;
2885
2886 sub = GNUNET_new (struct Sub);
2887
2888 /* With the hash generated from the secret value this service only connects
2889 * to rps instances that share the value */
2890 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
2891 GNUNET_MQ_hd_fixed_size (peer_check,
2892 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2893 struct GNUNET_MessageHeader,
2894 NULL),
2895 GNUNET_MQ_hd_fixed_size (peer_push,
2896 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2897 struct GNUNET_MessageHeader,
2898 NULL),
2899 GNUNET_MQ_hd_fixed_size (peer_pull_request,
2900 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2901 struct GNUNET_MessageHeader,
2902 NULL),
2903 GNUNET_MQ_hd_var_size (peer_pull_reply,
2904 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
2905 struct GNUNET_RPS_P2P_PullReplyMessage,
2906 NULL),
2907 GNUNET_MQ_handler_end ()
2908 };
2909 sub->hash = *hash;
2910 sub->cadet_port =
2911 GNUNET_CADET_open_port (cadet_handle,
2912 &sub->hash,
2913 &handle_inbound_channel, /* Connect handler */
2914 sub, /* cls */
2915 NULL, /* WindowSize handler */
2916 &cleanup_destroyed_channel, /* Disconnect handler */
2917 cadet_handlers);
2918 if (NULL == sub->cadet_port)
2919 {
2920 LOG (GNUNET_ERROR_TYPE_ERROR,
2921 "Cadet port `%s' is already in use.\n",
2922 GNUNET_APPLICATION_PORT_RPS);
2923 GNUNET_assert (0);
2924 }
2925
2926 /* Set up general data structure to keep track about peers */
2927 sub->valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2928 if (GNUNET_OK !=
2929 GNUNET_CONFIGURATION_get_value_filename (cfg,
2930 "rps",
2931 "FILENAME_VALID_PEERS",
2932 &sub->filename_valid_peers))
2933 {
2934 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2935 "rps",
2936 "FILENAME_VALID_PEERS");
2937 }
2938 if (0 != strncmp ("DISABLE", sub->filename_valid_peers, 7))
2939 {
2940 char *tmp_filename_valid_peers;
2941 char str_hash[105];
2942
2943 GNUNET_snprintf (str_hash,
2944 sizeof(str_hash), "%s",
2945 GNUNET_h2s_full (hash));
2946 tmp_filename_valid_peers = sub->filename_valid_peers;
2947 GNUNET_asprintf (&sub->filename_valid_peers,
2948 "%s%s",
2949 tmp_filename_valid_peers,
2950 str_hash);
2951 GNUNET_free (tmp_filename_valid_peers);
2952 }
2953 sub->peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2954
2955 /* Set up the sampler */
2956 sub->sampler_size_est_min = sampler_size;
2957 sub->sampler_size_est_need = sampler_size;;
2958 LOG (GNUNET_ERROR_TYPE_DEBUG, "MINSIZE is %u\n", sub->sampler_size_est_min);
2959 GNUNET_assert (0 != round_interval.rel_value_us);
2960 sub->round_interval = round_interval;
2961 sub->sampler = RPS_sampler_init (sampler_size,
2962 round_interval);
2963
2964 /* Logging of internals */
2965#ifdef TO_FILE_FULL
2966 // FIXME: The service cannot know the index, which is required by this
2967 // function:
2968 // sub->file_name_view_log = store_prefix_file_name (&own_identity, "view");
2969#endif /* TO_FILE_FULL */
2970#ifdef TO_FILE
2971#ifdef TO_FILE_FULL
2972 // FIXME: The service cannot know the index, which is required by this
2973 // function:
2974 // sub->file_name_observed_log = store_prefix_file_name (&own_identity,
2975 // "observed");
2976#endif /* TO_FILE_FULL */
2977 sub->num_observed_peers = 0;
2978 sub->observed_unique_peers = GNUNET_CONTAINER_multipeermap_create (1,
2979 GNUNET_NO);
2980#endif /* TO_FILE */
2981
2982 /* Set up data structures for gossip */
2983 sub->push_map = CustomPeerMap_create (4);
2984 sub->pull_map = CustomPeerMap_create (4);
2985 sub->view_size_est_min = sampler_size;;
2986 sub->view = View_create (sub->view_size_est_min);
2987 if (sub == msub)
2988 {
2989 GNUNET_STATISTICS_set (stats,
2990 "view size aim",
2991 sub->view_size_est_min,
2992 GNUNET_NO);
2993 }
2994
2995 /* Start executing rounds */
2996 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub);
2997
2998 return sub;
2999}
3000
3001
3002#ifdef TO_FILE
3003// /**
3004// * @brief Write all numbers in the given array into the given file
3005// *
3006// * Single numbers divided by a newline
3007// *
3008// * FIXME: The call to store_prefix_file_name expects the index of the peer,
3009// * which cannot be known to the service.
3010// * Write a dedicated function that uses the peer id.
3011// *
3012// * @param hist_array[] the array to dump
3013// * @param file_name file to dump into
3014// */
3015// static void
3016// write_histogram_to_file (const uint32_t hist_array[],
3017// const char *file_name)
3018// {
3019// char collect_str[SIZE_DUMP_FILE + 1] = "";
3020// char *recv_str_iter;
3021// char *file_name_full;
3022//
3023// recv_str_iter = collect_str;
3024// file_name_full = store_prefix_file_name (&own_identity,
3025// file_name);
3026// for (uint32_t i = 0; i < HISTOGRAM_FILE_SLOTS; i++)
3027// {
3028// char collect_str_tmp[8];
3029//
3030// GNUNET_snprintf (collect_str_tmp,
3031// sizeof(collect_str_tmp),
3032// "%" PRIu32 "\n",
3033// hist_array[i]);
3034// recv_str_iter = stpncpy (recv_str_iter,
3035// collect_str_tmp,
3036// 6);
3037// }
3038// (void) stpcpy (recv_str_iter,
3039// "\n");
3040// LOG (GNUNET_ERROR_TYPE_DEBUG,
3041// "Writing push stats to disk\n");
3042// to_file_w_len (file_name_full,
3043// SIZE_DUMP_FILE, "%s",
3044// collect_str);
3045// GNUNET_free (file_name_full);
3046// }
3047
3048
3049#endif /* TO_FILE */
3050
3051
3052/**
3053 * @brief Destroy Sub.
3054 *
3055 * @param sub Sub to destroy
3056 */
3057static void
3058destroy_sub (struct Sub *sub)
3059{
3060 GNUNET_assert (NULL != sub);
3061 GNUNET_assert (NULL != sub->do_round_task);
3062 GNUNET_SCHEDULER_cancel (sub->do_round_task);
3063 sub->do_round_task = NULL;
3064
3065 /* Disconnect from cadet */
3066 GNUNET_CADET_close_port (sub->cadet_port);
3067 sub->cadet_port = NULL;
3068
3069 /* Clean up data structures for peers */
3070 RPS_sampler_destroy (sub->sampler);
3071 sub->sampler = NULL;
3072 View_destroy (sub->view);
3073 sub->view = NULL;
3074 CustomPeerMap_destroy (sub->push_map);
3075 sub->push_map = NULL;
3076 CustomPeerMap_destroy (sub->pull_map);
3077 sub->pull_map = NULL;
3078 peers_terminate (sub);
3079
3080 /* Free leftover data structures */
3081#ifdef TO_FILE_FULL
3082 GNUNET_free (sub->file_name_view_log);
3083 sub->file_name_view_log = NULL;
3084#endif /* TO_FILE_FULL */
3085#ifdef TO_FILE
3086#ifdef TO_FILE_FULL
3087 GNUNET_free (sub->file_name_observed_log);
3088 sub->file_name_observed_log = NULL;
3089#endif /* TO_FILE_FULL */
3090
3091 // FIXME: Currently this calls malfunctionning code
3092 // /* Write push frequencies to disk */
3093 // write_histogram_to_file (sub->push_recv,
3094 // "push_recv");
3095
3096 // /* Write push deltas to disk */
3097 // write_histogram_to_file (sub->push_delta,
3098 // "push_delta");
3099
3100 // /* Write pull delays to disk */
3101 // write_histogram_to_file (sub->pull_delays,
3102 // "pull_delays");
3103
3104 GNUNET_CONTAINER_multipeermap_destroy (sub->observed_unique_peers);
3105 sub->observed_unique_peers = NULL;
3106#endif /* TO_FILE */
3107
3108 GNUNET_free (sub);
3109}
3110
3111
3112/***********************************************************************
3113* /Sub
3114***********************************************************************/
3115
3116
3117/***********************************************************************
3118* Core handlers
3119***********************************************************************/
3120
3121/**
3122 * @brief Callback on initialisation of Core.
3123 *
3124 * @param cls - unused
3125 * @param my_identity - unused
3126 */
3127void
3128core_init (void *cls,
3129 const struct GNUNET_PeerIdentity *my_identity)
3130{
3131 (void) cls;
3132 (void) my_identity;
3133
3134 map_single_hop = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
3135}
3136
3137
3138/**
3139 * @brief Callback for core.
3140 * Method called whenever a given peer connects.
3141 *
3142 * @param cls closure - unused
3143 * @param peer peer identity this notification is about
3144 * @return closure given to #core_disconnects as peer_cls
3145 */
3146void *
3147core_connects (void *cls,
3148 const struct GNUNET_PeerIdentity *peer,
3149 struct GNUNET_MQ_Handle *mq)
3150{
3151 (void) cls;
3152 (void) mq;
3153
3154 GNUNET_assert (GNUNET_YES ==
3155 GNUNET_CONTAINER_multipeermap_put (map_single_hop,
3156 peer,
3157 NULL,
3158 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3159 return NULL;
3160}
3161
3162
3163/**
3164 * @brief Callback for core.
3165 * Method called whenever a peer disconnects.
3166 *
3167 * @param cls closure - unused
3168 * @param peer peer identity this notification is about
3169 * @param peer_cls closure given in #core_connects - unused
3170 */
3171void
3172core_disconnects (void *cls,
3173 const struct GNUNET_PeerIdentity *peer,
3174 void *peer_cls)
3175{
3176 (void) cls;
3177 (void) peer_cls;
3178
3179 GNUNET_CONTAINER_multipeermap_remove_all (map_single_hop, peer);
3180}
3181
3182
3183/***********************************************************************
3184* /Core handlers
3185***********************************************************************/
3186
3187
3188/**
3189 * @brief Destroy the context for a (connected) client
3190 *
3191 * @param cli_ctx Context to destroy
3192 */
3193static void
3194destroy_cli_ctx (struct ClientContext *cli_ctx)
3195{
3196 GNUNET_assert (NULL != cli_ctx);
3197 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
3198 cli_ctx_tail,
3199 cli_ctx);
3200 if (NULL != cli_ctx->sub)
3201 {
3202 destroy_sub (cli_ctx->sub);
3203 cli_ctx->sub = NULL;
3204 }
3205 GNUNET_free (cli_ctx);
3206}
3207
3208
3209/**
3210 * @brief Update sizes in sampler and view on estimate update from nse service
3211 *
3212 * @param sub Sub
3213 * @param logestimate the log(Base 2) value of the current network size estimate
3214 * @param std_dev standard deviation for the estimate
3215 */
3216static void
3217adapt_sizes (struct Sub *sub, double logestimate, double std_dev)
3218{
3219 double estimate;
3220
3221 // double scale; // TODO this might go global/config
3222
3223 LOG (GNUNET_ERROR_TYPE_DEBUG,
3224 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
3225 logestimate, std_dev, RPS_sampler_get_size (sub->sampler));
3226 // scale = .01;
3227 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
3228 // GNUNET_NSE_log_estimate_to_n (logestimate);
3229 estimate = pow (estimate, 1.0 / 3);
3230 // TODO add if std_dev is a number
3231 // estimate += (std_dev * scale);
3232 if (sub->view_size_est_min < ceil (estimate))
3233 {
3234 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
3235 sub->sampler_size_est_need = estimate;
3236 sub->view_size_est_need = estimate;
3237 }
3238 else
3239 {
3240 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
3241 // sub->sampler_size_est_need = sub->view_size_est_min;
3242 sub->view_size_est_need = sub->view_size_est_min;
3243 }
3244 if (sub == msub)
3245 {
3246 GNUNET_STATISTICS_set (stats,
3247 "view size aim",
3248 sub->view_size_est_need,
3249 GNUNET_NO);
3250 }
3251
3252 /* If the NSE has changed adapt the lists accordingly */
3253 resize_wrapper (sub->sampler, sub->sampler_size_est_need);
3254 View_change_len (sub->view, sub->view_size_est_need);
3255}
3256
3257
3258/**
3259 * Function called by NSE.
3260 *
3261 * Updates sizes of sampler list and view and adapt those lists
3262 * accordingly.
3263 *
3264 * implements #GNUNET_NSE_Callback
3265 *
3266 * @param cls Closure - unused
3267 * @param timestamp time when the estimate was received from the server (or created by the server)
3268 * @param logestimate the log(Base 2) value of the current network size estimate
3269 * @param std_dev standard deviation for the estimate
3270 */
3271static void
3272nse_callback (void *cls,
3273 struct GNUNET_TIME_Absolute timestamp,
3274 double logestimate, double std_dev)
3275{
3276 (void) cls;
3277 (void) timestamp;
3278 struct ClientContext *cli_ctx_iter;
3279
3280 adapt_sizes (msub, logestimate, std_dev);
3281 for (cli_ctx_iter = cli_ctx_head;
3282 NULL != cli_ctx_iter;
3283 cli_ctx_iter = cli_ctx_iter->next)
3284 {
3285 if (NULL != cli_ctx_iter->sub)
3286 {
3287 adapt_sizes (cli_ctx_iter->sub, logestimate, std_dev);
3288 }
3289 }
3290}
3291
3292
3293/**
3294 * @brief This function is called, when the client seeds peers.
3295 * It verifies that @a msg is well-formed.
3296 *
3297 * @param cls the closure (#ClientContext)
3298 * @param msg the message
3299 * @return #GNUNET_OK if @a msg is well-formed
3300 * #GNUNET_SYSERR otherwise
3301 */
3302static int
3303check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
3304{
3305 struct ClientContext *cli_ctx = cls;
3306 uint16_t msize = ntohs (msg->header.size);
3307 uint32_t num_peers = ntohl (msg->num_peers);
3308
3309 msize -= sizeof(struct GNUNET_RPS_CS_SeedMessage);
3310 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
3311 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
3312 {
3313 LOG (GNUNET_ERROR_TYPE_ERROR,
3314 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3315 ntohl (msg->num_peers),
3316 (msize / sizeof(struct GNUNET_PeerIdentity)));
3317 GNUNET_break (0);
3318 GNUNET_SERVICE_client_drop (cli_ctx->client);
3319 return GNUNET_SYSERR;
3320 }
3321 return GNUNET_OK;
3322}
3323
3324
3325/**
3326 * Handle seed from the client.
3327 *
3328 * @param cls closure
3329 * @param msg the actual message
3330 */
3331static void
3332handle_client_seed (void *cls,
3333 const struct GNUNET_RPS_CS_SeedMessage *msg)
3334{
3335 struct ClientContext *cli_ctx = cls;
3336 struct GNUNET_PeerIdentity *peers;
3337 uint32_t num_peers;
3338 uint32_t i;
3339
3340 num_peers = ntohl (msg->num_peers);
3341 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3342
3343 LOG (GNUNET_ERROR_TYPE_DEBUG,
3344 "Client seeded peers:\n");
3345 print_peer_list (peers, num_peers);
3346
3347 for (i = 0; i < num_peers; i++)
3348 {
3349 LOG (GNUNET_ERROR_TYPE_DEBUG,
3350 "Updating samplers with seed %" PRIu32 ": %s\n",
3351 i,
3352 GNUNET_i2s (&peers[i]));
3353
3354 if (NULL != msub)
3355 got_peer (msub, &peers[i]); /* Condition needed? */
3356 if (NULL != cli_ctx->sub)
3357 got_peer (cli_ctx->sub, &peers[i]);
3358 }
3359 GNUNET_SERVICE_client_continue (cli_ctx->client);
3360}
3361
3362
3363/**
3364 * Handle RPS request from the client.
3365 *
3366 * @param cls Client context
3367 * @param msg Message containing the number of updates the client wants to
3368 * receive
3369 */
3370static void
3371handle_client_view_request (void *cls,
3372 const struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg)
3373{
3374 struct ClientContext *cli_ctx = cls;
3375 uint64_t num_updates;
3376
3377 num_updates = ntohl (msg->num_updates);
3378
3379 LOG (GNUNET_ERROR_TYPE_DEBUG,
3380 "Client requested %" PRIu64 " updates of view.\n",
3381 num_updates);
3382
3383 GNUNET_assert (NULL != cli_ctx);
3384 cli_ctx->view_updates_left = num_updates;
3385 send_view (cli_ctx, NULL, 0);
3386 GNUNET_SERVICE_client_continue (cli_ctx->client);
3387}
3388
3389
3390/**
3391 * @brief Handle the cancellation of the view updates.
3392 *
3393 * @param cls The client context
3394 * @param msg Unused
3395 */
3396static void
3397handle_client_view_cancel (void *cls,
3398 const struct GNUNET_MessageHeader *msg)
3399{
3400 struct ClientContext *cli_ctx = cls;
3401
3402 (void) msg;
3403
3404 LOG (GNUNET_ERROR_TYPE_DEBUG,
3405 "Client does not want to receive updates of view any more.\n");
3406
3407 GNUNET_assert (NULL != cli_ctx);
3408 cli_ctx->view_updates_left = 0;
3409 GNUNET_SERVICE_client_continue (cli_ctx->client);
3410 if (GNUNET_YES == cli_ctx->stream_update)
3411 {
3412 destroy_cli_ctx (cli_ctx);
3413 }
3414}
3415
3416
3417/**
3418 * Handle RPS request for biased stream from the client.
3419 *
3420 * @param cls Client context
3421 * @param msg unused
3422 */
3423static void
3424handle_client_stream_request (void *cls,
3425 const struct
3426 GNUNET_RPS_CS_DEBUG_StreamRequest *msg)
3427{
3428 struct ClientContext *cli_ctx = cls;
3429
3430 (void) msg;
3431
3432 LOG (GNUNET_ERROR_TYPE_DEBUG,
3433 "Client requested peers from biased stream.\n");
3434 cli_ctx->stream_update = GNUNET_YES;
3435
3436 GNUNET_assert (NULL != cli_ctx);
3437 GNUNET_SERVICE_client_continue (cli_ctx->client);
3438}
3439
3440
3441/**
3442 * @brief Handles the cancellation of the stream of biased peer ids
3443 *
3444 * @param cls The client context
3445 * @param msg unused
3446 */
3447static void
3448handle_client_stream_cancel (void *cls,
3449 const struct GNUNET_MessageHeader *msg)
3450{
3451 struct ClientContext *cli_ctx = cls;
3452
3453 (void) msg;
3454
3455 LOG (GNUNET_ERROR_TYPE_DEBUG,
3456 "Client canceled receiving peers from biased stream.\n");
3457 cli_ctx->stream_update = GNUNET_NO;
3458
3459 GNUNET_assert (NULL != cli_ctx);
3460 GNUNET_SERVICE_client_continue (cli_ctx->client);
3461}
3462
3463
3464/**
3465 * @brief Create and start a Sub.
3466 *
3467 * @param cls Closure - unused
3468 * @param msg Message containing the necessary information
3469 */
3470static void
3471handle_client_start_sub (void *cls,
3472 const struct GNUNET_RPS_CS_SubStartMessage *msg)
3473{
3474 struct ClientContext *cli_ctx = cls;
3475
3476 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested start of a new sub.\n");
3477 if ((NULL != cli_ctx->sub) &&
3478 (0 != memcmp (&cli_ctx->sub->hash,
3479 &msg->hash,
3480 sizeof(struct GNUNET_HashCode))) )
3481 {
3482 LOG (GNUNET_ERROR_TYPE_WARNING,
3483 "Already have a Sub with different share for this client. Remove old one, add new.\n");
3484 destroy_sub (cli_ctx->sub);
3485 cli_ctx->sub = NULL;
3486 }
3487 cli_ctx->sub = new_sub (&msg->hash,
3488 msub->sampler_size_est_min, // TODO make api input?
3489 GNUNET_TIME_relative_ntoh (msg->round_interval));
3490 GNUNET_SERVICE_client_continue (cli_ctx->client);
3491}
3492
3493
3494/**
3495 * @brief Destroy the Sub
3496 *
3497 * @param cls Closure - unused
3498 * @param msg Message containing the hash that identifies the Sub
3499 */
3500static void
3501handle_client_stop_sub (void *cls,
3502 const struct GNUNET_RPS_CS_SubStopMessage *msg)
3503{
3504 struct ClientContext *cli_ctx = cls;
3505
3506 GNUNET_assert (NULL != cli_ctx->sub);
3507 if (0 != memcmp (&cli_ctx->sub->hash, &msg->hash, sizeof(struct
3508 GNUNET_HashCode)))
3509 {
3510 LOG (GNUNET_ERROR_TYPE_WARNING,
3511 "Share of current sub and request differ!\n");
3512 }
3513 destroy_sub (cli_ctx->sub);
3514 cli_ctx->sub = NULL;
3515 GNUNET_SERVICE_client_continue (cli_ctx->client);
3516}
3517
3518
3519/**
3520 * Handle a CHECK_LIVE message from another peer.
3521 *
3522 * This does nothing. But without calling #GNUNET_CADET_receive_done()
3523 * the channel is blocked for all other communication.
3524 *
3525 * @param cls Closure - Context of channel
3526 * @param msg Message - unused
3527 */
3528static void
3529handle_peer_check (void *cls,
3530 const struct GNUNET_MessageHeader *msg)
3531{
3532 const struct ChannelCtx *channel_ctx = cls;
3533 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3534
3535 (void) msg;
3536
3537 LOG (GNUNET_ERROR_TYPE_DEBUG,
3538 "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer));
3539 if (channel_ctx->peer_ctx->sub == msub)
3540 {
3541 GNUNET_STATISTICS_update (stats,
3542 "# pending online checks",
3543 -1,
3544 GNUNET_NO);
3545 }
3546
3547 GNUNET_CADET_receive_done (channel_ctx->channel);
3548}
3549
3550
3551/**
3552 * Handle a PUSH message from another peer.
3553 *
3554 * Check the proof of work and store the PeerID
3555 * in the temporary list for pushed PeerIDs.
3556 *
3557 * @param cls Closure - Context of channel
3558 * @param msg Message - unused
3559 */
3560static void
3561handle_peer_push (void *cls,
3562 const struct GNUNET_MessageHeader *msg)
3563{
3564 const struct ChannelCtx *channel_ctx = cls;
3565 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3566
3567 (void) msg;
3568
3569 // (check the proof of work (?))
3570
3571 LOG (GNUNET_ERROR_TYPE_DEBUG,
3572 "Received PUSH (%s)\n",
3573 GNUNET_i2s (peer));
3574 if (channel_ctx->peer_ctx->sub == msub)
3575 {
3576 GNUNET_STATISTICS_update (stats, "# push message received", 1, GNUNET_NO);
3577 if ((NULL != map_single_hop) &&
3578 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3579 peer)))
3580 {
3581 GNUNET_STATISTICS_update (stats,
3582 "# push message received (multi-hop peer)",
3583 1,
3584 GNUNET_NO);
3585 }
3586 }
3587
3588 #if ENABLE_MALICIOUS
3589 struct AttackedPeer *tmp_att_peer;
3590
3591 if ((1 == mal_type) ||
3592 (3 == mal_type))
3593 { /* Try to maximise representation */
3594 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3595 tmp_att_peer->peer_id = *peer;
3596 if (NULL == att_peer_set)
3597 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
3598 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3599 peer))
3600 {
3601 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3602 att_peers_tail,
3603 tmp_att_peer);
3604 add_peer_array_to_set (peer, 1, att_peer_set);
3605 }
3606 else
3607 {
3608 GNUNET_free (tmp_att_peer);
3609 }
3610 }
3611
3612
3613 else if (2 == mal_type)
3614 {
3615 /* We attack one single well-known peer - simply ignore */
3616 }
3617 #endif /* ENABLE_MALICIOUS */
3618
3619 /* Add the sending peer to the push_map */
3620 CustomPeerMap_put (channel_ctx->peer_ctx->sub->push_map, peer);
3621
3622 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map,
3623 &channel_ctx->peer_ctx->peer_id));
3624 GNUNET_CADET_receive_done (channel_ctx->channel);
3625}
3626
3627
3628/**
3629 * Handle PULL REQUEST request message from another peer.
3630 *
3631 * Reply with the view of PeerIDs.
3632 *
3633 * @param cls Closure - Context of channel
3634 * @param msg Message - unused
3635 */
3636static void
3637handle_peer_pull_request (void *cls,
3638 const struct GNUNET_MessageHeader *msg)
3639{
3640 const struct ChannelCtx *channel_ctx = cls;
3641 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
3642 const struct GNUNET_PeerIdentity *peer = &peer_ctx->peer_id;
3643 const struct GNUNET_PeerIdentity *view_array;
3644
3645 (void) msg;
3646
3647 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (
3648 peer));
3649 if (peer_ctx->sub == msub)
3650 {
3651 GNUNET_STATISTICS_update (stats,
3652 "# pull request message received",
3653 1,
3654 GNUNET_NO);
3655 if ((NULL != map_single_hop) &&
3656 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3657 &peer_ctx->peer_id)))
3658 {
3659 GNUNET_STATISTICS_update (stats,
3660 "# pull request message received (multi-hop peer)",
3661 1,
3662 GNUNET_NO);
3663 }
3664 }
3665
3666 #if ENABLE_MALICIOUS
3667 if ((1 == mal_type)
3668 || (3 == mal_type))
3669 { /* Try to maximise representation */
3670 send_pull_reply (peer_ctx, mal_peers, num_mal_peers);
3671 }
3672
3673 else if (2 == mal_type)
3674 { /* Try to partition network */
3675 if (0 == GNUNET_memcmp (&attacked_peer, peer))
3676 {
3677 send_pull_reply (peer_ctx, mal_peers, num_mal_peers);
3678 }
3679 }
3680 #endif /* ENABLE_MALICIOUS */
3681
3682 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map,
3683 &channel_ctx->peer_ctx->peer_id));
3684 GNUNET_CADET_receive_done (channel_ctx->channel);
3685 view_array = View_get_as_array (channel_ctx->peer_ctx->sub->view);
3686 send_pull_reply (peer_ctx,
3687 view_array,
3688 View_size (channel_ctx->peer_ctx->sub->view));
3689}
3690
3691
3692/**
3693 * Check whether we sent a corresponding request and
3694 * whether this reply is the first one.
3695 *
3696 * @param cls Closure - Context of channel
3697 * @param msg Message containing the replied peers
3698 */
3699static int
3700check_peer_pull_reply (void *cls,
3701 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3702{
3703 struct ChannelCtx *channel_ctx = cls;
3704 struct PeerContext *sender_ctx = channel_ctx->peer_ctx;
3705
3706 if (sizeof(struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
3707 {
3708 GNUNET_break_op (0);
3709 return GNUNET_SYSERR;
3710 }
3711
3712 if ((ntohs (msg->header.size) - sizeof(struct
3713 GNUNET_RPS_P2P_PullReplyMessage))
3714 / sizeof(struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
3715 {
3716 LOG (GNUNET_ERROR_TYPE_ERROR,
3717 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3718 ntohl (msg->num_peers),
3719 (ntohs (msg->header.size) - sizeof(struct
3720 GNUNET_RPS_P2P_PullReplyMessage))
3721 / sizeof(struct GNUNET_PeerIdentity));
3722 GNUNET_break_op (0);
3723 return GNUNET_SYSERR;
3724 }
3725
3726 if (GNUNET_YES != check_peer_flag (sender_ctx->sub->peer_map,
3727 &sender_ctx->peer_id,
3728 Peers_PULL_REPLY_PENDING))
3729 {
3730 LOG (GNUNET_ERROR_TYPE_WARNING,
3731 "Received a pull reply from a peer (%s) we didn't request one from!\n",
3732 GNUNET_i2s (&sender_ctx->peer_id));
3733 if (sender_ctx->sub == msub)
3734 {
3735 GNUNET_STATISTICS_update (stats,
3736 "# unrequested pull replies",
3737 1,
3738 GNUNET_NO);
3739 }
3740 }
3741 return GNUNET_OK;
3742}
3743
3744
3745/**
3746 * Handle PULL REPLY message from another peer.
3747 *
3748 * @param cls Closure
3749 * @param msg The message header
3750 */
3751static void
3752handle_peer_pull_reply (void *cls,
3753 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3754{
3755 const struct ChannelCtx *channel_ctx = cls;
3756 const struct GNUNET_PeerIdentity *sender = &channel_ctx->peer_ctx->peer_id;
3757 const struct GNUNET_PeerIdentity *peers;
3758 struct Sub *sub = channel_ctx->peer_ctx->sub;
3759 uint32_t i;
3760
3761#if ENABLE_MALICIOUS
3762 struct AttackedPeer *tmp_att_peer;
3763#endif /* ENABLE_MALICIOUS */
3764
3765 sub->pull_delays[sub->num_rounds - channel_ctx->peer_ctx->round_pull_req]++;
3766 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (
3767 sender));
3768 if (channel_ctx->peer_ctx->sub == msub)
3769 {
3770 GNUNET_STATISTICS_update (stats,
3771 "# pull reply messages received",
3772 1,
3773 GNUNET_NO);
3774 if ((NULL != map_single_hop) &&
3775 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3776 &channel_ctx->
3777 peer_ctx->peer_id)) )
3778 {
3779 GNUNET_STATISTICS_update (stats,
3780 "# pull reply messages received (multi-hop peer)",
3781 1,
3782 GNUNET_NO);
3783 }
3784 }
3785
3786 #if ENABLE_MALICIOUS
3787 // We shouldn't even receive pull replies as we're not sending
3788 if (2 == mal_type)
3789 {
3790 }
3791 #endif /* ENABLE_MALICIOUS */
3792
3793 /* Do actual logic */
3794 peers = (const struct GNUNET_PeerIdentity *) &msg[1];
3795
3796 LOG (GNUNET_ERROR_TYPE_DEBUG,
3797 "PULL REPLY received, got following %u peers:\n",
3798 ntohl (msg->num_peers));
3799
3800 for (i = 0; i < ntohl (msg->num_peers); i++)
3801 {
3802 LOG (GNUNET_ERROR_TYPE_DEBUG,
3803 "%u. %s\n",
3804 i,
3805 GNUNET_i2s (&peers[i]));
3806
3807 #if ENABLE_MALICIOUS
3808 if ((NULL != att_peer_set) &&
3809 ((1 == mal_type) || (3 == mal_type) ))
3810 { /* Add attacked peer to local list */
3811 // TODO check if we sent a request and this was the first reply
3812 if ((GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
3813 &peers[i]))
3814 && (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
3815 &peers[i])) )
3816 {
3817 tmp_att_peer = GNUNET_new (struct AttackedPeer);
3818 tmp_att_peer->peer_id = peers[i];
3819 GNUNET_CONTAINER_DLL_insert (att_peers_head,
3820 att_peers_tail,
3821 tmp_att_peer);
3822 add_peer_array_to_set (&peers[i], 1, att_peer_set);
3823 }
3824 continue;
3825 }
3826 #endif /* ENABLE_MALICIOUS */
3827 /* Make sure we 'know' about this peer */
3828 (void) insert_peer (channel_ctx->peer_ctx->sub,
3829 &peers[i]);
3830
3831 if (GNUNET_YES == check_peer_valid (channel_ctx->peer_ctx->sub->valid_peers,
3832 &peers[i]))
3833 {
3834 CustomPeerMap_put (channel_ctx->peer_ctx->sub->pull_map,
3835 &peers[i]);
3836 }
3837 else
3838 {
3839 schedule_operation (channel_ctx->peer_ctx,
3840 insert_in_pull_map,
3841 channel_ctx->peer_ctx->sub); /* cls */
3842 (void) issue_peer_online_check (channel_ctx->peer_ctx->sub,
3843 &peers[i]);
3844 }
3845 }
3846
3847 UNSET_PEER_FLAG (get_peer_ctx (channel_ctx->peer_ctx->sub->peer_map,
3848 sender),
3849 Peers_PULL_REPLY_PENDING);
3850 clean_peer (channel_ctx->peer_ctx->sub,
3851 sender);
3852
3853 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map,
3854 sender));
3855 GNUNET_CADET_receive_done (channel_ctx->channel);
3856}
3857
3858
3859/**
3860 * Compute a random delay.
3861 * A uniformly distributed value between mean + spread and mean - spread.
3862 *
3863 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
3864 * It would return a random value between 2 and 6 min.
3865 *
3866 * @param mean the mean time until the next round
3867 * @param spread the inverse amount of deviation from the mean
3868 */
3869static struct GNUNET_TIME_Relative
3870compute_rand_delay (struct GNUNET_TIME_Relative mean,
3871 unsigned int spread)
3872{
3873 struct GNUNET_TIME_Relative half_interval;
3874 struct GNUNET_TIME_Relative ret;
3875 unsigned int rand_delay;
3876 unsigned int max_rand_delay;
3877
3878 if (0 == spread)
3879 {
3880 LOG (GNUNET_ERROR_TYPE_WARNING,
3881 "Not accepting spread of 0\n");
3882 GNUNET_break (0);
3883 GNUNET_assert (0);
3884 }
3885 GNUNET_assert (0 != mean.rel_value_us);
3886
3887 /* Compute random time value between spread * mean and spread * mean */
3888 half_interval = GNUNET_TIME_relative_divide (mean, spread);
3889
3890 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us
3891 / mean.rel_value_us * (2 / spread);
3892 /**
3893 * Compute random value between (0 and 1) * round_interval
3894 * via multiplying round_interval with a 'fraction' (0 to value)/value
3895 */
3896 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3897 max_rand_delay);
3898 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay);
3899 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
3900 ret = GNUNET_TIME_relative_add (ret, half_interval);
3901
3902 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
3903 LOG (GNUNET_ERROR_TYPE_WARNING,
3904 "Returning FOREVER_REL\n");
3905
3906 return ret;
3907}
3908
3909
3910/**
3911 * Send single pull request
3912 *
3913 * @param peer_ctx Context to the peer to send request to
3914 */
3915static void
3916send_pull_request (struct PeerContext *peer_ctx)
3917{
3918 struct GNUNET_MQ_Envelope *ev;
3919
3920 GNUNET_assert (GNUNET_NO == check_peer_flag (peer_ctx->sub->peer_map,
3921 &peer_ctx->peer_id,
3922 Peers_PULL_REPLY_PENDING));
3923 SET_PEER_FLAG (peer_ctx,
3924 Peers_PULL_REPLY_PENDING);
3925 peer_ctx->round_pull_req = peer_ctx->sub->num_rounds;
3926
3927 LOG (GNUNET_ERROR_TYPE_DEBUG,
3928 "Going to send PULL REQUEST to peer %s.\n",
3929 GNUNET_i2s (&peer_ctx->peer_id));
3930
3931 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
3932 send_message (peer_ctx,
3933 ev,
3934 "PULL REQUEST");
3935 if (peer_ctx->sub)
3936 {
3937 GNUNET_STATISTICS_update (stats,
3938 "# pull request send issued",
3939 1,
3940 GNUNET_NO);
3941 if ((NULL != map_single_hop) &&
3942 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3943 &peer_ctx->peer_id)))
3944 {
3945 GNUNET_STATISTICS_update (stats,
3946 "# pull request send issued (multi-hop peer)",
3947 1,
3948 GNUNET_NO);
3949 }
3950 }
3951}
3952
3953
3954/**
3955 * Send single push
3956 *
3957 * @param peer_ctx Context of peer to send push to
3958 */
3959static void
3960send_push (struct PeerContext *peer_ctx)
3961{
3962 struct GNUNET_MQ_Envelope *ev;
3963
3964 LOG (GNUNET_ERROR_TYPE_DEBUG,
3965 "Going to send PUSH to peer %s.\n",
3966 GNUNET_i2s (&peer_ctx->peer_id));
3967
3968 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
3969 send_message (peer_ctx, ev, "PUSH");
3970 if (peer_ctx->sub)
3971 {
3972 GNUNET_STATISTICS_update (stats,
3973 "# push send issued",
3974 1,
3975 GNUNET_NO);
3976 if ((NULL != map_single_hop) &&
3977 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3978 &peer_ctx->peer_id)))
3979 {
3980 GNUNET_STATISTICS_update (stats,
3981 "# push send issued (multi-hop peer)",
3982 1,
3983 GNUNET_NO);
3984 }
3985 }
3986}
3987
3988
3989#if ENABLE_MALICIOUS
3990
3991
3992/**
3993 * @brief This function is called, when the client tells us to act malicious.
3994 * It verifies that @a msg is well-formed.
3995 *
3996 * @param cls the closure (#ClientContext)
3997 * @param msg the message
3998 * @return #GNUNET_OK if @a msg is well-formed
3999 */
4000static int
4001check_client_act_malicious (void *cls,
4002 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
4003{
4004 struct ClientContext *cli_ctx = cls;
4005 uint16_t msize = ntohs (msg->header.size);
4006 uint32_t num_peers = ntohl (msg->num_peers);
4007
4008 msize -= sizeof(struct GNUNET_RPS_CS_ActMaliciousMessage);
4009 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
4010 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
4011 {
4012 LOG (GNUNET_ERROR_TYPE_ERROR,
4013 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
4014 ntohl (msg->num_peers),
4015 (msize / sizeof(struct GNUNET_PeerIdentity)));
4016 GNUNET_break (0);
4017 GNUNET_SERVICE_client_drop (cli_ctx->client);
4018 return GNUNET_SYSERR;
4019 }
4020 return GNUNET_OK;
4021}
4022
4023
4024/**
4025 * Turn RPS service to act malicious.
4026 *
4027 * @param cls Closure
4028 * @param client The client that sent the message
4029 * @param msg The message header
4030 */
4031static void
4032handle_client_act_malicious (void *cls,
4033 const struct
4034 GNUNET_RPS_CS_ActMaliciousMessage *msg)
4035{
4036 struct ClientContext *cli_ctx = cls;
4037 struct GNUNET_PeerIdentity *peers;
4038 uint32_t num_mal_peers_sent;
4039 uint32_t num_mal_peers_old;
4040 struct Sub *sub = cli_ctx->sub;
4041
4042 if (NULL == sub)
4043 sub = msub;
4044 /* Do actual logic */
4045 peers = (struct GNUNET_PeerIdentity *) &msg[1];
4046 mal_type = ntohl (msg->type);
4047 if (NULL == mal_peer_set)
4048 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
4049
4050 LOG (GNUNET_ERROR_TYPE_DEBUG,
4051 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
4052 mal_type,
4053 ntohl (msg->num_peers));
4054
4055 if (1 == mal_type)
4056 { /* Try to maximise representation */
4057 /* Add other malicious peers to those we already know */
4058
4059 num_mal_peers_sent = ntohl (msg->num_peers);
4060 num_mal_peers_old = num_mal_peers;
4061 GNUNET_array_grow (mal_peers,
4062 num_mal_peers,
4063 num_mal_peers + num_mal_peers_sent);
4064 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
4065 peers,
4066 num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity));
4067
4068 /* Add all mal peers to mal_peer_set */
4069 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
4070 num_mal_peers_sent,
4071 mal_peer_set);
4072
4073 /* Substitute do_round () with do_mal_round () */
4074 GNUNET_assert (NULL != sub->do_round_task);
4075 GNUNET_SCHEDULER_cancel (sub->do_round_task);
4076 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub);
4077 }
4078
4079 else if ((2 == mal_type) ||
4080 (3 == mal_type))
4081 { /* Try to partition the network */
4082 /* Add other malicious peers to those we already know */
4083
4084 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
4085 num_mal_peers_old = num_mal_peers;
4086 GNUNET_assert (GNUNET_MAX_MALLOC_CHECKED > num_mal_peers_sent);
4087 GNUNET_array_grow (mal_peers,
4088 num_mal_peers,
4089 num_mal_peers + num_mal_peers_sent);
4090 if ((NULL != mal_peers) &&
4091 (0 != num_mal_peers) )
4092 {
4093 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
4094 peers,
4095 num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity));
4096
4097 /* Add all mal peers to mal_peer_set */
4098 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
4099 num_mal_peers_sent,
4100 mal_peer_set);
4101 }
4102
4103 /* Store the one attacked peer */
4104 GNUNET_memcpy (&attacked_peer,
4105 &msg->attacked_peer,
4106 sizeof(struct GNUNET_PeerIdentity));
4107 /* Set the flag of the attacked peer to valid to avoid problems */
4108 if (GNUNET_NO == check_peer_known (sub->peer_map, &attacked_peer))
4109 {
4110 (void) issue_peer_online_check (sub, &attacked_peer);
4111 }
4112
4113 LOG (GNUNET_ERROR_TYPE_DEBUG,
4114 "Attacked peer is %s\n",
4115 GNUNET_i2s (&attacked_peer));
4116
4117 /* Substitute do_round () with do_mal_round () */
4118 if (NULL != sub->do_round_task)
4119 {
4120 /* Probably in shutdown */
4121 GNUNET_SCHEDULER_cancel (sub->do_round_task);
4122 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub);
4123 }
4124 }
4125 else if (0 == mal_type)
4126 { /* Stop acting malicious */
4127 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
4128
4129 /* Substitute do_mal_round () with do_round () */
4130 GNUNET_SCHEDULER_cancel (sub->do_round_task);
4131 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub);
4132 }
4133 else
4134 {
4135 GNUNET_break (0);
4136 GNUNET_SERVICE_client_continue (cli_ctx->client);
4137 }
4138 GNUNET_SERVICE_client_continue (cli_ctx->client);
4139}
4140
4141
4142/**
4143 * Send out PUSHes and PULLs maliciously.
4144 *
4145 * This is executed regylary.
4146 *
4147 * @param cls Closure - Sub
4148 */
4149static void
4150do_mal_round (void *cls)
4151{
4152 uint32_t num_pushes;
4153 uint32_t i;
4154 struct GNUNET_TIME_Relative time_next_round;
4155 struct AttackedPeer *tmp_att_peer;
4156 struct Sub *sub = cls;
4157
4158 LOG (GNUNET_ERROR_TYPE_DEBUG,
4159 "Going to execute next round maliciously type %" PRIu32 ".\n",
4160 mal_type);
4161 sub->do_round_task = NULL;
4162 GNUNET_assert (mal_type <= 3);
4163 /* Do malicious actions */
4164 if (1 == mal_type)
4165 { /* Try to maximise representation */
4166 /* The maximum of pushes we're going to send this round */
4167 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
4168 num_attacked_peers),
4169 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
4170
4171 LOG (GNUNET_ERROR_TYPE_DEBUG,
4172 "Going to send %" PRIu32 " pushes\n",
4173 num_pushes);
4174
4175 /* Send PUSHes to attacked peers */
4176 for (i = 0; i < num_pushes; i++)
4177 {
4178 if (att_peers_tail == att_peer_index)
4179 att_peer_index = att_peers_head;
4180 else
4181 att_peer_index = att_peer_index->next;
4182
4183 send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id));
4184 }
4185
4186 /* Send PULLs to some peers to learn about additional peers to attack */
4187 tmp_att_peer = att_peer_index;
4188 for (i = 0; i < num_pushes * alpha; i++)
4189 {
4190 if (att_peers_tail == tmp_att_peer)
4191 tmp_att_peer = att_peers_head;
4192 else
4193 att_peer_index = tmp_att_peer->next;
4194
4195 send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id));
4196 }
4197 }
4198
4199
4200 else if (2 == mal_type)
4201 { /**
4202 * Try to partition the network
4203 * Send as many pushes to the attacked peer as possible
4204 * That is one push per round as it will ignore more.
4205 */
4206 (void) issue_peer_online_check (sub, &attacked_peer);
4207 if (GNUNET_YES == check_peer_flag (sub->peer_map,
4208 &attacked_peer,
4209 Peers_ONLINE))
4210 send_push (get_peer_ctx (sub->peer_map, &attacked_peer));
4211 }
4212
4213
4214 if (3 == mal_type)
4215 { /* Combined attack */
4216 /* Send PUSH to attacked peers */
4217 if (GNUNET_YES == check_peer_known (sub->peer_map, &attacked_peer))
4218 {
4219 (void) issue_peer_online_check (sub, &attacked_peer);
4220 if (GNUNET_YES == check_peer_flag (sub->peer_map,
4221 &attacked_peer,
4222 Peers_ONLINE))
4223 {
4224 LOG (GNUNET_ERROR_TYPE_DEBUG,
4225 "Goding to send push to attacked peer (%s)\n",
4226 GNUNET_i2s (&attacked_peer));
4227 send_push (get_peer_ctx (sub->peer_map, &attacked_peer));
4228 }
4229 }
4230 (void) issue_peer_online_check (sub, &attacked_peer);
4231
4232 /* The maximum of pushes we're going to send this round */
4233 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
4234 num_attacked_peers),
4235 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
4236
4237 LOG (GNUNET_ERROR_TYPE_DEBUG,
4238 "Going to send %" PRIu32 " pushes\n",
4239 num_pushes);
4240
4241 for (i = 0; i < num_pushes; i++)
4242 {
4243 if (att_peers_tail == att_peer_index)
4244 att_peer_index = att_peers_head;
4245 else
4246 att_peer_index = att_peer_index->next;
4247
4248 send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id));
4249 }
4250
4251 /* Send PULLs to some peers to learn about additional peers to attack */
4252 tmp_att_peer = att_peer_index;
4253 for (i = 0; i < num_pushes * alpha; i++)
4254 {
4255 if (att_peers_tail == tmp_att_peer)
4256 tmp_att_peer = att_peers_head;
4257 else
4258 att_peer_index = tmp_att_peer->next;
4259
4260 send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id));
4261 }
4262 }
4263
4264 /* Schedule next round */
4265 time_next_round = compute_rand_delay (sub->round_interval, 2);
4266
4267 GNUNET_assert (NULL == sub->do_round_task);
4268 sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
4269 &do_mal_round, sub);
4270 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
4271}
4272
4273
4274#endif /* ENABLE_MALICIOUS */
4275
4276
4277/**
4278 * Send out PUSHes and PULLs, possibly update #view, samplers.
4279 *
4280 * This is executed regylary.
4281 *
4282 * @param cls Closure - Sub
4283 */
4284static void
4285do_round (void *cls)
4286{
4287 unsigned int i;
4288 const struct GNUNET_PeerIdentity *view_array;
4289 unsigned int *permut;
4290 unsigned int a_peers; /* Number of peers we send pushes to */
4291 unsigned int b_peers; /* Number of peers we send pull requests to */
4292 uint32_t first_border;
4293 uint32_t second_border;
4294 struct GNUNET_PeerIdentity peer;
4295 struct GNUNET_PeerIdentity *update_peer;
4296 struct Sub *sub = cls;
4297
4298 sub->num_rounds++;
4299 LOG (GNUNET_ERROR_TYPE_DEBUG,
4300 "Going to execute next round.\n");
4301 if (sub == msub)
4302 {
4303 GNUNET_STATISTICS_update (stats, "# rounds", 1, GNUNET_NO);
4304 }
4305 sub->do_round_task = NULL;
4306#ifdef TO_FILE_FULL
4307 to_file (sub->file_name_view_log,
4308 "___ new round ___");
4309#endif /* TO_FILE_FULL */
4310 view_array = View_get_as_array (sub->view);
4311 for (i = 0; i < View_size (sub->view); i++)
4312 {
4313 LOG (GNUNET_ERROR_TYPE_DEBUG,
4314 "\t%s\n", GNUNET_i2s (&view_array[i]));
4315#ifdef TO_FILE_FULL
4316 to_file (sub->file_name_view_log,
4317 "=%s\t(do round)",
4318 GNUNET_i2s_full (&view_array[i]));
4319#endif /* TO_FILE_FULL */
4320 }
4321
4322
4323 /* Send pushes and pull requests */
4324 if (0 < View_size (sub->view))
4325 {
4326 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
4327 View_size (sub->view));
4328
4329 /* Send PUSHes */
4330 a_peers = ceil (alpha * View_size (sub->view));
4331
4332 LOG (GNUNET_ERROR_TYPE_DEBUG,
4333 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
4334 a_peers, alpha, View_size (sub->view));
4335 for (i = 0; i < a_peers; i++)
4336 {
4337 peer = view_array[permut[i]];
4338 // FIXME if this fails schedule/loop this for later
4339 send_push (get_peer_ctx (sub->peer_map, &peer));
4340 }
4341
4342 /* Send PULL requests */
4343 b_peers = ceil (beta * View_size (sub->view));
4344 first_border = a_peers;
4345 second_border = a_peers + b_peers;
4346 if (second_border > View_size (sub->view))
4347 {
4348 first_border = View_size (sub->view) - b_peers;
4349 second_border = View_size (sub->view);
4350 }
4351 LOG (GNUNET_ERROR_TYPE_DEBUG,
4352 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
4353 b_peers, beta, View_size (sub->view));
4354 for (i = first_border; i < second_border; i++)
4355 {
4356 peer = view_array[permut[i]];
4357 if (GNUNET_NO == check_peer_flag (sub->peer_map,
4358 &peer,
4359 Peers_PULL_REPLY_PENDING))
4360 { // FIXME if this fails schedule/loop this for later
4361 send_pull_request (get_peer_ctx (sub->peer_map, &peer));
4362 }
4363 }
4364
4365 GNUNET_free (permut);
4366 permut = NULL;
4367 }
4368
4369
4370 /* Update view */
4371 /* TODO see how many peers are in push-/pull- list! */
4372
4373 if ((CustomPeerMap_size (sub->push_map) <= alpha * sub->view_size_est_need) &&
4374 (0 < CustomPeerMap_size (sub->push_map)) &&
4375 (0 < CustomPeerMap_size (sub->pull_map)))
4376 { /* If conditions for update are fulfilled, update */
4377 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
4378
4379 uint32_t final_size;
4380 uint32_t peers_to_clean_size;
4381 struct GNUNET_PeerIdentity *peers_to_clean;
4382
4383 peers_to_clean = NULL;
4384 peers_to_clean_size = 0;
4385 GNUNET_array_grow (peers_to_clean,
4386 peers_to_clean_size,
4387 View_size (sub->view));
4388 GNUNET_memcpy (peers_to_clean,
4389 view_array,
4390 View_size (sub->view) * sizeof(struct GNUNET_PeerIdentity));
4391
4392 /* Seems like recreating is the easiest way of emptying the peermap */
4393 View_clear (sub->view);
4394#ifdef TO_FILE_FULL
4395 to_file (sub->file_name_view_log,
4396 "--- emptied ---");
4397#endif /* TO_FILE_FULL */
4398
4399 first_border = GNUNET_MIN (ceil (alpha * sub->view_size_est_need),
4400 CustomPeerMap_size (sub->push_map));
4401 second_border = first_border
4402 + GNUNET_MIN (floor (beta * sub->view_size_est_need),
4403 CustomPeerMap_size (sub->pull_map));
4404 final_size = second_border
4405 + ceil ((1 - (alpha + beta)) * sub->view_size_est_need);
4406 LOG (GNUNET_ERROR_TYPE_DEBUG,
4407 "first border: %" PRIu32 ", second border: %" PRIu32 ", final size: %"
4408 PRIu32 "\n",
4409 first_border,
4410 second_border,
4411 final_size);
4412
4413 /* Update view with peers received through PUSHes */
4414 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
4415 CustomPeerMap_size (sub->push_map));
4416 for (i = 0; i < first_border; i++)
4417 {
4418 int inserted;
4419 inserted = insert_in_view (sub,
4420 CustomPeerMap_get_peer_by_index (sub->push_map,
4421 permut[i]));
4422 if (GNUNET_OK == inserted)
4423 {
4424 clients_notify_stream_peer (sub,
4425 1,
4426 CustomPeerMap_get_peer_by_index (
4427 sub->push_map, permut[i]));
4428 }
4429#ifdef TO_FILE_FULL
4430 to_file (sub->file_name_view_log,
4431 "+%s\t(push list)",
4432 GNUNET_i2s_full (&view_array[i]));
4433#endif /* TO_FILE_FULL */
4434 // TODO change the peer_flags accordingly
4435 }
4436 GNUNET_free (permut);
4437 permut = NULL;
4438
4439 /* Update view with peers received through PULLs */
4440 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
4441 CustomPeerMap_size (sub->pull_map));
4442 for (i = first_border; i < second_border; i++)
4443 {
4444 int inserted;
4445 inserted = insert_in_view (sub,
4446 CustomPeerMap_get_peer_by_index (sub->pull_map,
4447 permut[i
4448 -
4449 first_border
4450 ]));
4451 if (GNUNET_OK == inserted)
4452 {
4453 clients_notify_stream_peer (sub,
4454 1,
4455 CustomPeerMap_get_peer_by_index (
4456 sub->pull_map,
4457 permut[i
4458 - first_border]));
4459 }
4460#ifdef TO_FILE_FULL
4461 to_file (sub->file_name_view_log,
4462 "+%s\t(pull list)",
4463 GNUNET_i2s_full (&view_array[i]));
4464#endif /* TO_FILE_FULL */
4465 // TODO change the peer_flags accordingly
4466 }
4467 GNUNET_free (permut);
4468 permut = NULL;
4469
4470 /* Update view with peers from history */
4471 RPS_sampler_get_n_rand_peers (sub->sampler,
4472 final_size - second_border,
4473 hist_update,
4474 sub);
4475 // TODO change the peer_flags accordingly
4476
4477 for (i = 0; i < View_size (sub->view); i++)
4478 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
4479
4480 /* Clean peers that were removed from the view */
4481 for (i = 0; i < peers_to_clean_size; i++)
4482 {
4483#ifdef TO_FILE_FULL
4484 to_file (sub->file_name_view_log,
4485 "-%s",
4486 GNUNET_i2s_full (&peers_to_clean[i]));
4487#endif /* TO_FILE_FULL */
4488 clean_peer (sub, &peers_to_clean[i]);
4489 }
4490
4491 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
4492 clients_notify_view_update (sub);
4493 }
4494 else
4495 {
4496 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
4497 if (sub == msub)
4498 {
4499 GNUNET_STATISTICS_update (stats, "# rounds blocked", 1, GNUNET_NO);
4500 if ((CustomPeerMap_size (sub->push_map) > alpha
4501 * sub->view_size_est_need) &&
4502 ! (0 >= CustomPeerMap_size (sub->pull_map)))
4503 GNUNET_STATISTICS_update (stats, "# rounds blocked - too many pushes",
4504 1, GNUNET_NO);
4505 if ((CustomPeerMap_size (sub->push_map) > alpha
4506 * sub->view_size_est_need) &&
4507 (0 >= CustomPeerMap_size (sub->pull_map)))
4508 GNUNET_STATISTICS_update (stats,
4509 "# rounds blocked - too many pushes, no pull replies",
4510 1, GNUNET_NO);
4511 if ((0 >= CustomPeerMap_size (sub->push_map)) &&
4512 ! (0 >= CustomPeerMap_size (sub->pull_map)))
4513 GNUNET_STATISTICS_update (stats, "# rounds blocked - no pushes", 1,
4514 GNUNET_NO);
4515 if ((0 >= CustomPeerMap_size (sub->push_map)) &&
4516 (0 >= CustomPeerMap_size (sub->pull_map)))
4517 GNUNET_STATISTICS_update (stats,
4518 "# rounds blocked - no pushes, no pull replies",
4519 1, GNUNET_NO);
4520 if ((0 >= CustomPeerMap_size (sub->pull_map)) &&
4521 (CustomPeerMap_size (sub->push_map) > alpha
4522 * sub->view_size_est_need) &&
4523 (0 >= CustomPeerMap_size (sub->push_map)) )
4524 GNUNET_STATISTICS_update (stats, "# rounds blocked - no pull replies",
4525 1, GNUNET_NO);
4526 }
4527 }
4528 // TODO independent of that also get some peers from CADET_get_peers()?
4529 if (CustomPeerMap_size (sub->push_map) < HISTOGRAM_FILE_SLOTS)
4530 {
4531 sub->push_recv[CustomPeerMap_size (sub->push_map)]++;
4532 }
4533 else
4534 {
4535 LOG (GNUNET_ERROR_TYPE_WARNING,
4536 "Push map size too big for histogram (%u, %u)\n",
4537 CustomPeerMap_size (sub->push_map),
4538 HISTOGRAM_FILE_SLOTS);
4539 }
4540 // FIXME check bounds of histogram
4541 sub->push_delta[(int32_t) (CustomPeerMap_size (sub->push_map)
4542 - (alpha * sub->view_size_est_need))
4543 + (HISTOGRAM_FILE_SLOTS / 2)]++;
4544 if (sub == msub)
4545 {
4546 GNUNET_STATISTICS_set (stats,
4547 "# peers in push map at end of round",
4548 CustomPeerMap_size (sub->push_map),
4549 GNUNET_NO);
4550 GNUNET_STATISTICS_set (stats,
4551 "# peers in pull map at end of round",
4552 CustomPeerMap_size (sub->pull_map),
4553 GNUNET_NO);
4554 GNUNET_STATISTICS_set (stats,
4555 "# peers in view at end of round",
4556 View_size (sub->view),
4557 GNUNET_NO);
4558 GNUNET_STATISTICS_set (stats,
4559 "# expected pushes",
4560 alpha * sub->view_size_est_need,
4561 GNUNET_NO);
4562 GNUNET_STATISTICS_set (stats,
4563 "delta expected - received pushes",
4564 CustomPeerMap_size (sub->push_map) - (alpha
4565 * sub->
4566 view_size_est_need),
4567 GNUNET_NO);
4568 }
4569
4570 LOG (GNUNET_ERROR_TYPE_DEBUG,
4571 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (sub->view%u) = %.2f)\n",
4572 CustomPeerMap_size (sub->push_map),
4573 CustomPeerMap_size (sub->pull_map),
4574 alpha,
4575 View_size (sub->view),
4576 alpha * View_size (sub->view));
4577
4578 /* Update samplers */
4579 for (i = 0; i < CustomPeerMap_size (sub->push_map); i++)
4580 {
4581 update_peer = CustomPeerMap_get_peer_by_index (sub->push_map, i);
4582 LOG (GNUNET_ERROR_TYPE_DEBUG,
4583 "Updating with peer %s from push list\n",
4584 GNUNET_i2s (update_peer));
4585 insert_in_sampler (sub, update_peer);
4586 clean_peer (sub, update_peer); /* This cleans only if it is not in the view */
4587 }
4588
4589 for (i = 0; i < CustomPeerMap_size (sub->pull_map); i++)
4590 {
4591 LOG (GNUNET_ERROR_TYPE_DEBUG,
4592 "Updating with peer %s from pull list\n",
4593 GNUNET_i2s (CustomPeerMap_get_peer_by_index (sub->pull_map, i)));
4594 insert_in_sampler (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i));
4595 /* This cleans only if it is not in the view */
4596 clean_peer (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i));
4597 }
4598
4599
4600 /* Empty push/pull lists */
4601 CustomPeerMap_clear (sub->push_map);
4602 CustomPeerMap_clear (sub->pull_map);
4603
4604 if (sub == msub)
4605 {
4606 GNUNET_STATISTICS_set (stats,
4607 "view size",
4608 View_size (sub->view),
4609 GNUNET_NO);
4610 }
4611
4612 struct GNUNET_TIME_Relative time_next_round;
4613
4614 time_next_round = compute_rand_delay (sub->round_interval, 2);
4615
4616 /* Schedule next round */
4617 sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
4618 &do_round, sub);
4619 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
4620}
4621
4622
4623/**
4624 * This is called from GNUNET_CADET_get_peers().
4625 *
4626 * It is called on every peer(ID) that cadet somehow has contact with.
4627 * We use those to initialise the sampler.
4628 *
4629 * implements #GNUNET_CADET_PeersCB
4630 *
4631 * @param cls Closure - Sub
4632 * @param peer Peer, or NULL on "EOF".
4633 * @param tunnel Do we have a tunnel towards this peer?
4634 * @param n_paths Number of known paths towards this peer.
4635 * @param best_path How long is the best path?
4636 * (0 = unknown, 1 = ourselves, 2 = neighbor)
4637 */
4638void
4639init_peer_cb (void *cls,
4640 const struct GNUNET_PeerIdentity *peer,
4641 int tunnel, /* "Do we have a tunnel towards this peer?" */
4642 unsigned int n_paths, /* "Number of known paths towards this peer" */
4643 unsigned int best_path) /* "How long is the best path?
4644 * (0 = unknown, 1 = ourselves, 2 = neighbor)" */
4645{
4646 struct Sub *sub = cls;
4647
4648 (void) tunnel;
4649 (void) n_paths;
4650 (void) best_path;
4651
4652 if (NULL != peer)
4653 {
4654 LOG (GNUNET_ERROR_TYPE_DEBUG,
4655 "Got peer_id %s from cadet\n",
4656 GNUNET_i2s (peer));
4657 got_peer (sub, peer);
4658 }
4659}
4660
4661
4662/**
4663 * @brief Iterator function over stored, valid peers.
4664 *
4665 * We initialise the sampler with those.
4666 *
4667 * @param cls Closure - Sub
4668 * @param peer the peer id
4669 * @return #GNUNET_YES if we should continue to
4670 * iterate,
4671 * #GNUNET_NO if not.
4672 */
4673static int
4674valid_peers_iterator (void *cls,
4675 const struct GNUNET_PeerIdentity *peer)
4676{
4677 struct Sub *sub = cls;
4678
4679 if (NULL != peer)
4680 {
4681 LOG (GNUNET_ERROR_TYPE_DEBUG,
4682 "Got stored, valid peer %s\n",
4683 GNUNET_i2s (peer));
4684 got_peer (sub, peer);
4685 }
4686 return GNUNET_YES;
4687}
4688
4689
4690/**
4691 * Iterator over peers from peerinfo.
4692 *
4693 * @param cls Closure - Sub
4694 * @param peer id of the peer, NULL for last call
4695 * @param hello hello message for the peer (can be NULL)
4696 * @param err_msg error message
4697 */
4698void
4699process_peerinfo_peers (void *cls,
4700 const struct GNUNET_PeerIdentity *peer,
4701 const struct GNUNET_MessageHeader *hello,
4702 const char *emsg)
4703{
4704 struct Sub *sub = cls;
4705
4706 (void) hello;
4707 (void) emsg;
4708
4709 if (NULL != peer)
4710 {
4711 LOG (GNUNET_ERROR_TYPE_DEBUG,
4712 "Got peer_id %s from peerinfo\n",
4713 GNUNET_i2s (peer));
4714 got_peer (sub, peer);
4715 }
4716}
4717
4718
4719/**
4720 * Task run during shutdown.
4721 *
4722 * @param cls Closure - unused
4723 */
4724static void
4725shutdown_task (void *cls)
4726{
4727 (void) cls;
4728 struct ClientContext *client_ctx;
4729
4730 LOG (GNUNET_ERROR_TYPE_DEBUG,
4731 "RPS service is going down\n");
4732
4733 /* Clean all clients */
4734 for (client_ctx = cli_ctx_head;
4735 NULL != cli_ctx_head;
4736 client_ctx = cli_ctx_head)
4737 {
4738 destroy_cli_ctx (client_ctx);
4739 }
4740 if (NULL != msub)
4741 {
4742 destroy_sub (msub);
4743 msub = NULL;
4744 }
4745
4746 /* Disconnect from other services */
4747 GNUNET_PEERSTORE_hello_changed_notify_cancel (peerstore_notify);
4748 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
4749 peerstore = NULL;
4750 GNUNET_NSE_disconnect (nse);
4751 if (NULL != map_single_hop)
4752 {
4753 /* core_init was called - core was initialised */
4754 /* disconnect first, so no callback tries to access missing peermap */
4755 GNUNET_CORE_disconnect (core_handle);
4756 core_handle = NULL;
4757 GNUNET_CONTAINER_multipeermap_destroy (map_single_hop);
4758 map_single_hop = NULL;
4759 }
4760
4761 if (NULL != stats)
4762 {
4763 GNUNET_STATISTICS_destroy (stats,
4764 GNUNET_NO);
4765 stats = NULL;
4766 }
4767 GNUNET_CADET_disconnect (cadet_handle);
4768 cadet_handle = NULL;
4769#if ENABLE_MALICIOUS
4770 struct AttackedPeer *tmp_att_peer;
4771 GNUNET_array_grow (mal_peers,
4772 num_mal_peers,
4773 0);
4774 if (NULL != mal_peer_set)
4775 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
4776 if (NULL != att_peer_set)
4777 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
4778 while (NULL != att_peers_head)
4779 {
4780 tmp_att_peer = att_peers_head;
4781 GNUNET_CONTAINER_DLL_remove (att_peers_head,
4782 att_peers_tail,
4783 tmp_att_peer);
4784 GNUNET_free (tmp_att_peer);
4785 }
4786#endif /* ENABLE_MALICIOUS */
4787 close_all_files ();
4788}
4789
4790
4791/**
4792 * Handle client connecting to the service.
4793 *
4794 * @param cls unused
4795 * @param client the new client
4796 * @param mq the message queue of @a client
4797 * @return @a client
4798 */
4799static void *
4800client_connect_cb (void *cls,
4801 struct GNUNET_SERVICE_Client *client,
4802 struct GNUNET_MQ_Handle *mq)
4803{
4804 struct ClientContext *cli_ctx;
4805
4806 (void) cls;
4807
4808 LOG (GNUNET_ERROR_TYPE_DEBUG,
4809 "Client connected\n");
4810 if (NULL == client)
4811 return client; /* Server was destroyed before a client connected. Shutting down */
4812 cli_ctx = GNUNET_new (struct ClientContext);
4813 cli_ctx->mq = mq;
4814 cli_ctx->view_updates_left = -1;
4815 cli_ctx->stream_update = GNUNET_NO;
4816 cli_ctx->client = client;
4817 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
4818 cli_ctx_tail,
4819 cli_ctx);
4820 return cli_ctx;
4821}
4822
4823
4824/**
4825 * Callback called when a client disconnected from the service
4826 *
4827 * @param cls closure for the service
4828 * @param client the client that disconnected
4829 * @param internal_cls should be equal to @a c
4830 */
4831static void
4832client_disconnect_cb (void *cls,
4833 struct GNUNET_SERVICE_Client *client,
4834 void *internal_cls)
4835{
4836 struct ClientContext *cli_ctx = internal_cls;
4837
4838 (void) cls;
4839 GNUNET_assert (client == cli_ctx->client);
4840 if (NULL == client)
4841 { /* shutdown task - destroy all clients */
4842 while (NULL != cli_ctx_head)
4843 destroy_cli_ctx (cli_ctx_head);
4844 }
4845 else
4846 { /* destroy this client */
4847 LOG (GNUNET_ERROR_TYPE_DEBUG,
4848 "Client disconnected. Destroy its context.\n");
4849 destroy_cli_ctx (cli_ctx);
4850 }
4851}
4852
4853
4854/**
4855 * Handle random peer sampling clients.
4856 *
4857 * @param cls closure
4858 * @param c configuration to use
4859 * @param service the initialized service
4860 */
4861static void
4862run (void *cls,
4863 const struct GNUNET_CONFIGURATION_Handle *c,
4864 struct GNUNET_SERVICE_Handle *service)
4865{
4866 struct GNUNET_TIME_Relative round_interval;
4867 long long unsigned int sampler_size;
4868 char hash_port_string[] = GNUNET_APPLICATION_PORT_RPS;
4869 struct GNUNET_HashCode hash;
4870
4871 (void) cls;
4872 (void) service;
4873
4874 GNUNET_log_setup ("rps",
4875 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG),
4876 NULL);
4877 cfg = c;
4878 /* Get own ID */
4879 GNUNET_CRYPTO_get_peer_identity (cfg,
4880 &own_identity); // TODO check return value
4881 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4882 "STARTING SERVICE (rps) for peer [%s]\n",
4883 GNUNET_i2s (&own_identity));
4884#if ENABLE_MALICIOUS
4885 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4886 "Malicious execution compiled in.\n");
4887#endif /* ENABLE_MALICIOUS */
4888
4889 /* Get time interval from the configuration */
4890 if (GNUNET_OK !=
4891 GNUNET_CONFIGURATION_get_value_time (cfg,
4892 "RPS",
4893 "ROUNDINTERVAL",
4894 &round_interval))
4895 {
4896 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4897 "RPS", "ROUNDINTERVAL");
4898 GNUNET_SCHEDULER_shutdown ();
4899 return;
4900 }
4901
4902 /* Get initial size of sampler/view from the configuration */
4903 if (GNUNET_OK !=
4904 GNUNET_CONFIGURATION_get_value_number (cfg,
4905 "RPS",
4906 "MINSIZE",
4907 &sampler_size))
4908 {
4909 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
4910 "RPS", "MINSIZE");
4911 GNUNET_SCHEDULER_shutdown ();
4912 return;
4913 }
4914
4915 cadet_handle = GNUNET_CADET_connect (cfg);
4916 GNUNET_assert (NULL != cadet_handle);
4917 core_handle = GNUNET_CORE_connect (cfg,
4918 NULL, /* cls */
4919 core_init, /* init */
4920 core_connects, /* connects */
4921 core_disconnects, /* disconnects */
4922 NULL); /* handlers */
4923 GNUNET_assert (NULL != core_handle);
4924
4925
4926 alpha = 0.45;
4927 beta = 0.45;
4928
4929
4930 /* Set up main Sub */
4931 GNUNET_CRYPTO_hash (hash_port_string,
4932 strlen (hash_port_string),
4933 &hash);
4934 msub = new_sub (&hash,
4935 sampler_size, /* Will be overwritten by config */
4936 round_interval);
4937
4938
4939 peerstore = GNUNET_PEERSTORE_connect (cfg);
4940
4941 /* connect to NSE */
4942 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
4943
4944 // LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
4945 // GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, msub);
4946 // TODO send push/pull to each of those peers?
4947 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
4948 restore_valid_peers (msub);
4949 get_valid_peers (msub->valid_peers, valid_peers_iterator, msub);
4950
4951 peerstore_notify = GNUNET_PEERSTORE_hello_changed_notify (peerstore,
4952 GNUNET_NO,
4953 process_peerinfo_peers,
4954 msub);
4955
4956 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
4957
4958 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
4959 stats = GNUNET_STATISTICS_create ("rps", cfg);
4960}
4961
4962
4963/**
4964 * Define "main" method using service macro.
4965 */
4966GNUNET_SERVICE_MAIN
4967 ("rps",
4968 GNUNET_SERVICE_OPTION_NONE,
4969 &run,
4970 &client_connect_cb,
4971 &client_disconnect_cb,
4972 NULL,
4973 GNUNET_MQ_hd_var_size (client_seed,
4974 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
4975 struct GNUNET_RPS_CS_SeedMessage,
4976 NULL),
4977#if ENABLE_MALICIOUS
4978 GNUNET_MQ_hd_var_size (client_act_malicious,
4979 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
4980 struct GNUNET_RPS_CS_ActMaliciousMessage,
4981 NULL),
4982#endif /* ENABLE_MALICIOUS */
4983 GNUNET_MQ_hd_fixed_size (client_view_request,
4984 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST,
4985 struct GNUNET_RPS_CS_DEBUG_ViewRequest,
4986 NULL),
4987 GNUNET_MQ_hd_fixed_size (client_view_cancel,
4988 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL,
4989 struct GNUNET_MessageHeader,
4990 NULL),
4991 GNUNET_MQ_hd_fixed_size (client_stream_request,
4992 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST,
4993 struct GNUNET_RPS_CS_DEBUG_StreamRequest,
4994 NULL),
4995 GNUNET_MQ_hd_fixed_size (client_stream_cancel,
4996 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL,
4997 struct GNUNET_MessageHeader,
4998 NULL),
4999 GNUNET_MQ_hd_fixed_size (client_start_sub,
5000 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START,
5001 struct GNUNET_RPS_CS_SubStartMessage,
5002 NULL),
5003 GNUNET_MQ_hd_fixed_size (client_stop_sub,
5004 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP,
5005 struct GNUNET_RPS_CS_SubStopMessage,
5006 NULL),
5007 GNUNET_MQ_handler_end ());
5008
5009/* end of gnunet-service-rps.c */
diff --git a/src/contrib/service/rps/gnunet-service-rps_custommap.c b/src/contrib/service/rps/gnunet-service-rps_custommap.c
new file mode 100644
index 000000000..b1e73be17
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_custommap.c
@@ -0,0 +1,356 @@
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/contrib/service/rps/gnunet-service-rps_custommap.h b/src/contrib/service/rps/gnunet-service-rps_custommap.h
new file mode 100644
index 000000000..e7f669c63
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_custommap.h
@@ -0,0 +1,159 @@
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/contrib/service/rps/gnunet-service-rps_sampler.c b/src/contrib/service/rps/gnunet-service-rps_sampler.c
new file mode 100644
index 000000000..598cc887e
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_sampler.c
@@ -0,0 +1,268 @@
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/contrib/service/rps/gnunet-service-rps_sampler.h b/src/contrib/service/rps/gnunet-service-rps_sampler.h
new file mode 100644
index 000000000..410823bea
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_sampler.h
@@ -0,0 +1,157 @@
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/contrib/service/rps/gnunet-service-rps_sampler_elem.c b/src/contrib/service/rps/gnunet-service-rps_sampler_elem.c
new file mode 100644
index 000000000..a6dc755a3
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_sampler_elem.c
@@ -0,0 +1,170 @@
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_elem 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/contrib/service/rps/gnunet-service-rps_sampler_elem.h b/src/contrib/service/rps/gnunet-service-rps_sampler_elem.h
new file mode 100644
index 000000000..98959a88f
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_sampler_elem.h
@@ -0,0 +1,153 @@
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/contrib/service/rps/gnunet-service-rps_view.c b/src/contrib/service/rps/gnunet-service-rps_view.c
new file mode 100644
index 000000000..d58b995a3
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_view.c
@@ -0,0 +1,294 @@
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/contrib/service/rps/gnunet-service-rps_view.h b/src/contrib/service/rps/gnunet-service-rps_view.h
new file mode 100644
index 000000000..4d42272c1
--- /dev/null
+++ b/src/contrib/service/rps/gnunet-service-rps_view.h
@@ -0,0 +1,145 @@
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/contrib/service/rps/profiler_rps.conf b/src/contrib/service/rps/profiler_rps.conf
new file mode 100644
index 000000000..89e5e35f2
--- /dev/null
+++ b/src/contrib/service/rps/profiler_rps.conf
@@ -0,0 +1,139 @@
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/contrib/service/rps/rps-sampler_client.c b/src/contrib/service/rps/rps-sampler_client.c
new file mode 100644
index 000000000..f6e98ce29
--- /dev/null
+++ b/src/contrib/service/rps/rps-sampler_client.c
@@ -0,0 +1,439 @@
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/contrib/service/rps/rps-sampler_client.h b/src/contrib/service/rps/rps-sampler_client.h
new file mode 100644
index 000000000..7a674371d
--- /dev/null
+++ b/src/contrib/service/rps/rps-sampler_client.h
@@ -0,0 +1,156 @@
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/contrib/service/rps/rps-sampler_common.c b/src/contrib/service/rps/rps-sampler_common.c
new file mode 100644
index 000000000..e3fb79501
--- /dev/null
+++ b/src/contrib/service/rps/rps-sampler_common.c
@@ -0,0 +1,742 @@
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
194void
195RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
196 double deficiency_factor)
197{
198 sampler->deficiency_factor = deficiency_factor;
199}
200
201
202/**
203 * @brief Add a callback that will be called when the next peer is inserted
204 * into the sampler
205 *
206 * @param sampler The sampler on which update it will be called
207 * @param notify_cb The callback
208 * @param cls Closure given to the callback
209 *
210 * @return The context containing callback and closure
211 */
212struct SamplerNotifyUpdateCTX *
213sampler_notify_on_update (struct RPS_Sampler *sampler,
214 SamplerNotifyUpdateCB notify_cb,
215 void *cls)
216{
217 struct SamplerNotifyUpdateCTX *notify_ctx;
218
219 LOG (GNUNET_ERROR_TYPE_DEBUG,
220 "Inserting new context for notification\n");
221 notify_ctx = GNUNET_new (struct SamplerNotifyUpdateCTX);
222 notify_ctx->notify_cb = notify_cb;
223 notify_ctx->cls = cls;
224 GNUNET_CONTAINER_DLL_insert (sampler->notify_ctx_head,
225 sampler->notify_ctx_tail,
226 notify_ctx);
227 return notify_ctx;
228}
229
230
231/**
232 * Get the size of the sampler.
233 *
234 * @param sampler the sampler to return the size of.
235 * @return the size of the sampler
236 */
237unsigned int
238RPS_sampler_get_size (struct RPS_Sampler *sampler)
239{
240 return sampler->sampler_size;
241}
242
243
244/**
245 * @brief Notify about update of the sampler.
246 *
247 * Call the callbacks that are waiting for notification on updates to the
248 * sampler.
249 *
250 * @param sampler The sampler the updates are waiting for
251 */
252static void
253notify_update (struct RPS_Sampler *sampler)
254{
255 struct SamplerNotifyUpdateCTX *tmp_notify_head;
256 struct SamplerNotifyUpdateCTX *tmp_notify_tail;
257
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "Calling callbacks waiting for update notification.\n");
260 tmp_notify_head = sampler->notify_ctx_head;
261 tmp_notify_tail = sampler->notify_ctx_tail;
262 sampler->notify_ctx_head = NULL;
263 sampler->notify_ctx_tail = NULL;
264 for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head;
265 NULL != tmp_notify_head;
266 notify_iter = tmp_notify_head)
267 {
268 GNUNET_assert (NULL != notify_iter->notify_cb);
269 GNUNET_CONTAINER_DLL_remove (tmp_notify_head,
270 tmp_notify_tail,
271 notify_iter);
272 notify_iter->notify_cb (notify_iter->cls);
273 GNUNET_free (notify_iter);
274 }
275}
276
277
278/**
279 * Update every sampler element of this sampler with given peer
280 *
281 * @param sampler the sampler to update.
282 * @param id the PeerID that is put in the sampler
283 */
284void
285RPS_sampler_update (struct RPS_Sampler *sampler,
286 const struct GNUNET_PeerIdentity *id)
287{
288 for (uint32_t i = 0; i < sampler->sampler_size; i++)
289 {
290 RPS_sampler_elem_next (sampler->sampler_elements[i],
291 id);
292 }
293 notify_update (sampler);
294}
295
296
297/**
298 * Reinitialise all previously initialised sampler elements with the given value.
299 *
300 * Used to get rid of a PeerID.
301 *
302 * FIXME: This should also consider currently pending requests
303 * (Pending requests already collect peerids. As long as not all
304 * requested IDs have been collected, they are kept.
305 * Ideally, the @p id should be removed from all pending requests. This
306 * seems quite complicated.)
307 *
308 * @param sampler the sampler to reinitialise a sampler element in.
309 * @param id the id of the sampler elements to update.
310 */
311void
312RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
313 const struct GNUNET_PeerIdentity *id)
314{
315 uint32_t i;
316
317 for (i = 0; i < sampler->sampler_size; i++)
318 {
319 if (0 == GNUNET_memcmp (id,
320 &(sampler->sampler_elements[i]->peer_id)))
321 {
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
323 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
324 }
325 }
326}
327
328
329/**
330 * Counts how many Samplers currently hold a given PeerID.
331 *
332 * @param sampler the sampler to count ids in.
333 * @param id the PeerID to count.
334 *
335 * @return the number of occurrences of id.
336 */
337uint32_t
338RPS_sampler_count_id (struct RPS_Sampler *sampler,
339 const struct GNUNET_PeerIdentity *id)
340{
341 uint32_t count;
342 uint32_t i;
343
344 count = 0;
345 for (i = 0; i < sampler->sampler_size; i++)
346 {
347 if ((0 == GNUNET_memcmp (&sampler->sampler_elements[i]->peer_id, id))
348 && (EMPTY != sampler->sampler_elements[i]->is_empty) )
349 count++;
350 }
351 return count;
352}
353
354
355/**
356 * Grow or shrink the size of the sampler.
357 *
358 * @param sampler the sampler to resize.
359 * @param new_size the new size of the sampler
360 */
361static void
362sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
363{
364 unsigned int old_size;
365 uint32_t i;
366
367 // TODO check min and max size
368
369 old_size = sampler->sampler_size;
370
371 if (old_size > new_size)
372 { /* Shrinking */
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "Shrinking sampler %d -> %d\n",
375 old_size,
376 new_size);
377
378 for (i = new_size; i < old_size; i++)
379 {
380 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
381 }
382
383 GNUNET_array_grow (sampler->sampler_elements,
384 sampler->sampler_size,
385 new_size);
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "sampler->sampler_elements now points to %p\n",
388 sampler->sampler_elements);
389 }
390 else if (old_size < new_size)
391 { /* Growing */
392 LOG (GNUNET_ERROR_TYPE_DEBUG,
393 "Growing sampler %d -> %d\n",
394 old_size,
395 new_size);
396
397 GNUNET_array_grow (sampler->sampler_elements,
398 sampler->sampler_size,
399 new_size);
400
401 for (i = old_size; i < new_size; i++)
402 { /* Add new sampler elements */
403 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
404 }
405 }
406 else
407 {
408 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
409 return;
410 }
411
412 GNUNET_assert (sampler->sampler_size == new_size);
413}
414
415
416/**
417 * Grow or shrink the size of the sampler.
418 *
419 * @param sampler the sampler to resize.
420 * @param new_size the new size of the sampler
421 */
422void
423RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
424{
425 GNUNET_assert (0 < new_size);
426 sampler_resize (sampler, new_size);
427}
428
429
430/**
431 * Empty the sampler.
432 *
433 * @param sampler the sampler to empty.
434 */
435static void
436sampler_empty (struct RPS_Sampler *sampler)
437{
438 sampler_resize (sampler, 0);
439}
440
441
442/**
443 * Callback to _get_rand_peer() used by _get_n_rand_peers().
444 *
445 * Implements #RPS_sampler_rand_peer_ready_cont
446 *
447 * Checks whether all n peers are available. If they are,
448 * give those back.
449 * @param cls Closure
450 * @param id Peer ID
451 * @param probability The probability with which this sampler has seen all ids
452 * @param num_observed How many ids this sampler has observed
453 */
454static void
455check_n_peers_ready (void *cls,
456 const struct GNUNET_PeerIdentity *id,
457 double probability,
458 uint32_t num_observed)
459{
460 struct RPS_SamplerRequestHandle *req_handle = cls;
461
462 (void) id;
463 RPS_sampler_n_rand_peers_ready_cb tmp_cb;
464 struct GNUNET_PeerIdentity *peers;
465 uint32_t num_peers;
466 void *cb_cls;
467 (void) probability;
468 (void) num_observed;
469
470 req_handle->cur_num_peers++;
471 LOG (GNUNET_ERROR_TYPE_DEBUG,
472 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
473 req_handle->cur_num_peers, req_handle->num_peers);
474
475 if (req_handle->num_peers == req_handle->cur_num_peers)
476 { /* All peers are ready -- return those to the client */
477 GNUNET_assert (NULL != req_handle->callback);
478
479 LOG (GNUNET_ERROR_TYPE_DEBUG,
480 "returning %" PRIX32 " peers to the client\n",
481 req_handle->num_peers);
482
483 /* Copy pointers and peers temporarily as they
484 * might be deleted from within the callback */
485 tmp_cb = req_handle->callback;
486 num_peers = req_handle->num_peers;
487 peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
488 GNUNET_memcpy (peers,
489 req_handle->ids,
490 num_peers * sizeof(struct GNUNET_PeerIdentity));
491 cb_cls = req_handle->cls;
492 RPS_sampler_request_cancel (req_handle);
493 req_handle = NULL;
494 tmp_cb (peers, num_peers, cb_cls);
495 GNUNET_free (peers);
496 }
497}
498
499
500/**
501 * Callback to _get_rand_peer() used by _get_rand_peer_info().
502 *
503 * Implements #RPS_sampler_rand_peer_ready_cont
504 *
505 * @param cls Closure
506 * @param id Peer ID
507 * @param probability The probability with which this sampler has seen all ids
508 * @param num_observed How many ids this sampler has observed
509 */
510static void
511check_peer_info_ready (void *cls,
512 const struct GNUNET_PeerIdentity *id,
513 double probability,
514 uint32_t num_observed)
515{
516 struct RPS_SamplerRequestHandleSingleInfo *req_handle = cls;
517
518 (void) id;
519 RPS_sampler_sinlge_info_ready_cb tmp_cb;
520 struct GNUNET_PeerIdentity *peer;
521 void *cb_cls;
522 (void) probability;
523 (void) num_observed;
524
525 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 "Got single peer with additional info\n");
527
528 GNUNET_assert (NULL != req_handle->callback);
529
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "returning single peer with info to the client\n");
532
533 /* Copy pointers and peers temporarily as they
534 * might be deleted from within the callback */
535 tmp_cb = req_handle->callback;
536 peer = GNUNET_new (struct GNUNET_PeerIdentity);
537 GNUNET_memcpy (peer,
538 req_handle->id,
539 sizeof(struct GNUNET_PeerIdentity));
540 cb_cls = req_handle->cls;
541 RPS_sampler_request_single_info_cancel (req_handle);
542 req_handle = NULL;
543 tmp_cb (peer, cb_cls, probability, num_observed);
544 GNUNET_free (peer);
545}
546
547
548struct RPS_SamplerRequestHandle *
549RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
550 uint32_t num_peers,
551 RPS_sampler_n_rand_peers_ready_cb cb,
552 void *cls)
553{
554 uint32_t i;
555 struct RPS_SamplerRequestHandle *req_handle;
556 struct GetPeerCls *gpc;
557
558 GNUNET_assert (0 != sampler->sampler_size);
559 if (0 == num_peers)
560 return NULL;
561
562 // TODO check if we have too much (distinct) sampled peers
563 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
564 req_handle->num_peers = num_peers;
565 req_handle->cur_num_peers = 0;
566 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
567 req_handle->sampler = sampler;
568 req_handle->callback = cb;
569 req_handle->cls = cls;
570 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
571 sampler->req_handle_tail,
572 req_handle);
573
574 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
576
577 for (i = 0; i < num_peers; i++)
578 {
579 gpc = GNUNET_new (struct GetPeerCls);
580 gpc->req_handle = req_handle;
581 gpc->req_single_info_handle = NULL;
582 gpc->cont = check_n_peers_ready;
583 gpc->cont_cls = req_handle;
584 gpc->id = &req_handle->ids[i];
585
586 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
587 req_handle->gpc_tail,
588 gpc);
589 // maybe add a little delay
590 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
591 gpc);
592 }
593 return req_handle;
594}
595
596
597/**
598 * Get one random peer with additional information.
599 *
600 * @param sampler the sampler to get peers from.
601 * @param cb callback that will be called once the ids are ready.
602 * @param cls closure given to @a cb
603 */
604struct RPS_SamplerRequestHandleSingleInfo *
605RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler,
606 RPS_sampler_sinlge_info_ready_cb cb,
607 void *cls)
608{
609 struct RPS_SamplerRequestHandleSingleInfo *req_handle;
610 struct GetPeerCls *gpc;
611
612 GNUNET_assert (0 != sampler->sampler_size);
613
614 // TODO check if we have too much (distinct) sampled peers
615 req_handle = GNUNET_new (struct RPS_SamplerRequestHandleSingleInfo);
616 req_handle->id = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity));
617 req_handle->sampler = sampler;
618 req_handle->callback = cb;
619 req_handle->cls = cls;
620 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_single_head,
621 sampler->req_handle_single_tail,
622 req_handle);
623
624 gpc = GNUNET_new (struct GetPeerCls);
625 gpc->req_handle = NULL;
626 gpc->req_single_info_handle = req_handle;
627 gpc->cont = check_peer_info_ready;
628 gpc->cont_cls = req_handle;
629 gpc->id = req_handle->id;
630
631 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
632 req_handle->gpc_tail,
633 gpc);
634 // maybe add a little delay
635 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
636 gpc);
637 return req_handle;
638}
639
640
641/**
642 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
643 *
644 * @param req_handle the handle to the request
645 */
646void
647RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
648{
649 struct GetPeerCls *i;
650
651 while (NULL != (i = req_handle->gpc_head))
652 {
653 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
654 req_handle->gpc_tail,
655 i);
656 if (NULL != i->get_peer_task)
657 {
658 GNUNET_SCHEDULER_cancel (i->get_peer_task);
659 }
660 if (NULL != i->notify_ctx)
661 {
662 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->notify_ctx_head,
663 req_handle->sampler->notify_ctx_tail,
664 i->notify_ctx);
665 GNUNET_free (i->notify_ctx);
666 i->notify_ctx = NULL;
667 }
668 GNUNET_free (i);
669 }
670 GNUNET_free (req_handle->ids);
671 req_handle->ids = NULL;
672 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
673 req_handle->sampler->req_handle_tail,
674 req_handle);
675 GNUNET_free (req_handle);
676}
677
678
679/**
680 * Cancel a request issued through #RPS_sampler_sinlge_info_ready_cb.
681 *
682 * @param req_handle the handle to the request
683 */
684void
685RPS_sampler_request_single_info_cancel (
686 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle)
687{
688 struct GetPeerCls *i;
689
690 while (NULL != (i = req_single_info_handle->gpc_head))
691 {
692 GNUNET_CONTAINER_DLL_remove (req_single_info_handle->gpc_head,
693 req_single_info_handle->gpc_tail,
694 i);
695 if (NULL != i->get_peer_task)
696 {
697 GNUNET_SCHEDULER_cancel (i->get_peer_task);
698 }
699 if (NULL != i->notify_ctx)
700 {
701 GNUNET_CONTAINER_DLL_remove (
702 req_single_info_handle->sampler->notify_ctx_head,
703 req_single_info_handle->sampler->
704 notify_ctx_tail,
705 i->notify_ctx);
706 GNUNET_free (i->notify_ctx);
707 i->notify_ctx = NULL;
708 }
709 GNUNET_free (i);
710 }
711 GNUNET_free (req_single_info_handle->id);
712 req_single_info_handle->id = NULL;
713 GNUNET_CONTAINER_DLL_remove (
714 req_single_info_handle->sampler->req_handle_single_head,
715 req_single_info_handle->sampler->
716 req_handle_single_tail,
717 req_single_info_handle);
718 GNUNET_free (req_single_info_handle);
719}
720
721
722/**
723 * Cleans the sampler.
724 */
725void
726RPS_sampler_destroy (struct RPS_Sampler *sampler)
727{
728 if (NULL != sampler->req_handle_head)
729 {
730 LOG (GNUNET_ERROR_TYPE_WARNING,
731 "There are still pending requests. Going to remove them.\n");
732 while (NULL != sampler->req_handle_head)
733 {
734 RPS_sampler_request_cancel (sampler->req_handle_head);
735 }
736 }
737 sampler_empty (sampler);
738 GNUNET_free (sampler);
739}
740
741
742/* end of rps-sampler_common.c */
diff --git a/src/contrib/service/rps/rps-sampler_common.h b/src/contrib/service/rps/rps-sampler_common.h
new file mode 100644
index 000000000..94727028a
--- /dev/null
+++ b/src/contrib/service/rps/rps-sampler_common.h
@@ -0,0 +1,389 @@
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 num_peers the number of peers requested
329 */
330struct RPS_SamplerRequestHandle *
331RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
332 uint32_t num_peers,
333 RPS_sampler_n_rand_peers_ready_cb cb,
334 void *cls);
335
336
337/**
338 * Get one random peer with additional information.
339 *
340 * @param sampler the sampler to get peers from.
341 * @param cb callback that will be called once the ids are ready.
342 * @param cls closure given to @a cb
343 */
344struct RPS_SamplerRequestHandleSingleInfo *
345RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler,
346 RPS_sampler_sinlge_info_ready_cb cb,
347 void *cls);
348
349
350/**
351 * Counts how many Samplers currently hold a given PeerID.
352 *
353 * @param sampler the sampler to count ids in.
354 * @param id the PeerID to count.
355 *
356 * @return the number of occurrences of id.
357 */
358uint32_t
359RPS_sampler_count_id (struct RPS_Sampler *sampler,
360 const struct GNUNET_PeerIdentity *id);
361
362
363/**
364 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
365 *
366 * @param req_handle the handle to the request
367 */
368void
369RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
370
371
372/**
373 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
374 *
375 * @param req_handle the handle to the request
376 */
377void
378RPS_sampler_request_single_info_cancel (
379 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle);
380
381
382/**
383 * Cleans the sampler.
384 */
385void
386RPS_sampler_destroy (struct RPS_Sampler *sampler);
387
388#endif /* RPS_SAMPLER_COMMON_H */
389/* end of rps-sampler_common.h */
diff --git a/src/contrib/service/rps/rps-test_util.c b/src/contrib/service/rps/rps-test_util.c
new file mode 100644
index 000000000..8fd82f7b3
--- /dev/null
+++ b/src/contrib/service/rps/rps-test_util.c
@@ -0,0 +1,550 @@
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/contrib/service/rps/rps-test_util.h b/src/contrib/service/rps/rps-test_util.h
new file mode 100644
index 000000000..9ff13bdcf
--- /dev/null
+++ b/src/contrib/service/rps/rps-test_util.h
@@ -0,0 +1,144 @@
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/contrib/service/rps/rps.conf.in b/src/contrib/service/rps/rps.conf.in
new file mode 100644
index 000000000..8c7098747
--- /dev/null
+++ b/src/contrib/service/rps/rps.conf.in
@@ -0,0 +1,38 @@
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/contrib/service/rps/rps.h b/src/contrib/service/rps/rps.h
new file mode 100644
index 000000000..202f881c9
--- /dev/null
+++ b/src/contrib/service/rps/rps.h
@@ -0,0 +1,336 @@
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/contrib/service/rps/rps_api.c b/src/contrib/service/rps/rps_api.c
new file mode 100644
index 000000000..c4166745a
--- /dev/null
+++ b/src/contrib/service/rps/rps_api.c
@@ -0,0 +1,1320 @@
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[out] hash 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/contrib/service/rps/test_rps.c b/src/contrib/service/rps/test_rps.c
new file mode 100644
index 000000000..ab132ec8a
--- /dev/null
+++ b/src/contrib/service/rps/test_rps.c
@@ -0,0 +1,3183 @@
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/contrib/service/rps/test_rps.conf b/src/contrib/service/rps/test_rps.conf
new file mode 100644
index 000000000..2b1101786
--- /dev/null
+++ b/src/contrib/service/rps/test_rps.conf
@@ -0,0 +1,135 @@
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/contrib/service/rps/test_rps_api.c b/src/contrib/service/rps/test_rps_api.c
new file mode 100644
index 000000000..06dad2887
--- /dev/null
+++ b/src/contrib/service/rps/test_rps_api.c
@@ -0,0 +1,87 @@
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/contrib/service/rps/test_service_rps_custommap.c b/src/contrib/service/rps/test_service_rps_custommap.c
new file mode 100644
index 000000000..82d75b7e0
--- /dev/null
+++ b/src/contrib/service/rps/test_service_rps_custommap.c
@@ -0,0 +1,128 @@
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 <platform.h>
26#include "gnunet-service-rps_custommap.h"
27
28#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
29 if (NULL != c_m) CustomPeerMap_destroy (c_m); return 1; }
30#define CHECK(c) { if (! (c)) ABORT (); }
31
32
33static int
34check ()
35{
36 struct CustomPeerMap *c_m;
37 struct GNUNET_PeerIdentity k1;
38 struct GNUNET_PeerIdentity k2;
39 unsigned int j;
40
41 CHECK (NULL != (c_m = CustomPeerMap_create (4)));
42 memset (&k1, 0, sizeof(k1));
43 memset (&k2, 1, sizeof(k2));
44 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k1));
45 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k2));
46 CHECK (GNUNET_NO == CustomPeerMap_remove_peer (c_m, &k1));
47 CHECK (GNUNET_NO == CustomPeerMap_remove_peer (c_m, &k2));
48 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 0));
49 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 0));
50 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 1));
51 CHECK (GNUNET_NO == CustomPeerMap_remove_peer_by_index (c_m, 1));
52 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 0));
53 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 0));
54 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 1));
55 CHECK (NULL == CustomPeerMap_get_peer_by_index (c_m, 1));
56 CustomPeerMap_clear (c_m); /* See if assertions trigger */
57 CHECK (0 == CustomPeerMap_size (c_m));
58
59 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k1));
60 CHECK (1 == CustomPeerMap_size (c_m));
61 CHECK (GNUNET_NO == CustomPeerMap_put (c_m, &k1));
62 CHECK (1 == CustomPeerMap_size (c_m));
63 CHECK (GNUNET_YES == CustomPeerMap_contains_peer (c_m, &k1));
64 CHECK (GNUNET_OK == CustomPeerMap_remove_peer (c_m, &k1));
65 CHECK (0 == CustomPeerMap_size (c_m));
66 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k1));
67 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k2));
68
69 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k1));
70 CHECK (1 == CustomPeerMap_size (c_m));
71 for (j = 0; j < 16; j++)
72 {
73 CHECK (GNUNET_NO == CustomPeerMap_put (c_m, &k1));
74 }
75 CHECK (1 == CustomPeerMap_size (c_m));
76 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k2));
77 CHECK (2 == CustomPeerMap_size (c_m));
78 for (j = 0; j < 16; j++)
79 {
80 CHECK (GNUNET_NO == CustomPeerMap_put (c_m, &k2));
81 }
82 CHECK (2 == CustomPeerMap_size (c_m));
83
84 /* iterate */
85 for (j = 0; j < CustomPeerMap_size (c_m); j++)
86 {
87 CHECK (NULL != CustomPeerMap_get_peer_by_index (c_m, j));
88 }
89 CHECK ((0 == memcmp (CustomPeerMap_get_peer_by_index (c_m, 0),
90 &k1, sizeof(k1))));
91 CHECK ((0 == memcmp (CustomPeerMap_get_peer_by_index (c_m, 1),
92 &k2, sizeof(k2))));
93 CHECK (GNUNET_OK == CustomPeerMap_remove_peer (c_m, &k1));
94 CHECK (1 == CustomPeerMap_size (c_m));
95 CHECK (GNUNET_NO == CustomPeerMap_contains_peer (c_m, &k1));
96 CHECK (GNUNET_YES == CustomPeerMap_contains_peer (c_m, &k2));
97 CHECK (NULL != CustomPeerMap_get_peer_by_index (c_m, 0));
98
99 CustomPeerMap_clear (c_m);
100 CHECK (0 == CustomPeerMap_size (c_m));
101
102 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k1));
103 CHECK (1 == CustomPeerMap_size (c_m));
104 CHECK (GNUNET_OK == CustomPeerMap_put (c_m, &k2));
105 CHECK (2 == CustomPeerMap_size (c_m));
106 CustomPeerMap_clear (c_m);
107 CHECK (0 == CustomPeerMap_size (c_m));
108
109 CustomPeerMap_destroy (c_m);
110
111 return 0;
112}
113
114
115int
116main (int argc, char *argv[])
117{
118 (void) argc;
119 (void) argv;
120
121 GNUNET_log_setup ("test_service_rps_peers",
122 "WARNING",
123 NULL);
124 return check ();
125}
126
127
128/* end of test_service_rps_custommap.c */
diff --git a/src/contrib/service/rps/test_service_rps_sampler_elem.c b/src/contrib/service/rps/test_service_rps_sampler_elem.c
new file mode 100644
index 000000000..afa17611c
--- /dev/null
+++ b/src/contrib/service/rps/test_service_rps_sampler_elem.c
@@ -0,0 +1,214 @@
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 <platform.h>
26#include "gnunet_util_lib.h"
27#include "gnunet-service-rps_sampler_elem.h"
28
29#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
30 return 1; }
31#define CHECK(c) { if (! (c)) ABORT (); }
32
33
34static int
35check ()
36{
37 struct GNUNET_PeerIdentity pid0;
38 struct GNUNET_PeerIdentity pid1;
39 struct RPS_SamplerElement *s_elem;
40 struct GNUNET_CRYPTO_AuthKey auth_key;
41 struct GNUNET_CRYPTO_AuthKey auth_key2;
42 struct GNUNET_HashCode hash_code;
43 struct GNUNET_HashCode hash_code2;
44
45 memset (&pid0, 1, sizeof(pid0));
46 memset (&pid1, 0, sizeof(pid1));
47
48 /* Check if creation and destruction of an
49 * (empty) sampler element works */
50 s_elem = RPS_sampler_elem_create ();
51 CHECK (NULL != s_elem);
52 CHECK (EMPTY == s_elem->is_empty);
53 CHECK (NULL != &s_elem->auth_key);
54 auth_key = s_elem->auth_key;
55 RPS_sampler_elem_destroy (s_elem);
56
57
58 /* Check creation of another sampler element
59 * yields another (random) key */
60 s_elem = RPS_sampler_elem_create ();
61 CHECK (NULL != s_elem);
62 CHECK (EMPTY == s_elem->is_empty);
63 CHECK (NULL != &s_elem->auth_key);
64 CHECK (auth_key.key != s_elem->auth_key.key);
65 CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
66 GNUNET_CRYPTO_HASH_LENGTH));
67 auth_key = s_elem->auth_key;
68
69 /* Check that reinitialisation
70 * yields another (random) key */
71 RPS_sampler_elem_reinit (s_elem);
72 CHECK (NULL != s_elem);
73 CHECK (EMPTY == s_elem->is_empty);
74 CHECK (NULL != &s_elem->auth_key);
75 CHECK (auth_key.key != s_elem->auth_key.key);
76 CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
77 GNUNET_CRYPTO_HASH_LENGTH));
78 RPS_sampler_elem_destroy (s_elem);
79
80
81 /* Check that input of single peer id
82 * sets valid values */
83 s_elem = RPS_sampler_elem_create ();
84 CHECK (EMPTY == s_elem->is_empty);
85 CHECK (NULL != &s_elem->auth_key);
86 CHECK (auth_key.key != s_elem->auth_key.key);
87 /* This fails only with minimal chance */
88 CHECK (0 != memcmp (auth_key.key, s_elem->auth_key.key,
89 GNUNET_CRYPTO_HASH_LENGTH));
90 auth_key = s_elem->auth_key;
91
92 /* Check also that the hash of the peer id changed
93 * Also fails with minimal probability */
94 hash_code = s_elem->peer_id_hash;
95 RPS_sampler_elem_next (s_elem, &pid0);
96 CHECK (0 == memcmp (&pid0,
97 &s_elem->peer_id,
98 sizeof(struct GNUNET_PeerIdentity)));
99 CHECK (0 != memcmp (&hash_code,
100 &s_elem->peer_id_hash,
101 sizeof(struct GNUNET_HashCode)));
102 hash_code = s_elem->peer_id_hash;
103
104 /* We can only check that the peer id is one of both inputs */
105 RPS_sampler_elem_next (s_elem, &pid1);
106 CHECK ((0 == memcmp (&pid0,
107 &s_elem->peer_id,
108 sizeof(struct GNUNET_PeerIdentity))) ||
109 (0 == memcmp (&pid1,
110 &s_elem->peer_id,
111 sizeof(struct GNUNET_PeerIdentity))));
112
113 /* Check that hash stayed the same when peer id did not change */
114 if (0 == memcmp (&pid0,
115 &s_elem->peer_id,
116 sizeof(struct GNUNET_PeerIdentity)))
117 {
118 CHECK (0 == memcmp (&hash_code,
119 &s_elem->peer_id_hash,
120 sizeof(struct GNUNET_HashCode)));
121 }
122 else /* Check that hash changed */
123 {
124 CHECK (0 != memcmp (&hash_code,
125 &s_elem->peer_id_hash,
126 sizeof(struct GNUNET_HashCode)));
127 }
128
129 /* Check multiple inputs of same id
130 * hash should not change anymore */
131 hash_code2 = s_elem->peer_id_hash;
132 RPS_sampler_elem_next (s_elem, &pid0);
133 CHECK (0 == memcmp (&hash_code2,
134 &s_elem->peer_id_hash,
135 sizeof(struct GNUNET_HashCode)));
136 RPS_sampler_elem_next (s_elem, &pid1);
137 CHECK (0 == memcmp (&hash_code2,
138 &s_elem->peer_id_hash,
139 sizeof(struct GNUNET_HashCode)));
140 RPS_sampler_elem_next (s_elem, &pid0);
141 CHECK (0 == memcmp (&hash_code2,
142 &s_elem->peer_id_hash,
143 sizeof(struct GNUNET_HashCode)));
144 RPS_sampler_elem_next (s_elem, &pid0);
145 CHECK (0 == memcmp (&hash_code2,
146 &s_elem->peer_id_hash,
147 sizeof(struct GNUNET_HashCode)));
148 RPS_sampler_elem_next (s_elem, &pid0);
149 CHECK (0 == memcmp (&hash_code2,
150 &s_elem->peer_id_hash,
151 sizeof(struct GNUNET_HashCode)));
152 RPS_sampler_elem_next (s_elem, &pid1);
153 CHECK (0 == memcmp (&hash_code2,
154 &s_elem->peer_id_hash,
155 sizeof(struct GNUNET_HashCode)));
156 RPS_sampler_elem_next (s_elem, &pid1);
157 CHECK (0 == memcmp (&hash_code2,
158 &s_elem->peer_id_hash,
159 sizeof(struct GNUNET_HashCode)));
160 RPS_sampler_elem_next (s_elem, &pid1);
161 CHECK (0 == memcmp (&hash_code2,
162 &s_elem->peer_id_hash,
163 sizeof(struct GNUNET_HashCode)));
164
165 /* Check whether pid stayed the same all the time */
166 if (0 == memcmp (&hash_code,
167 &hash_code2,
168 sizeof(struct GNUNET_HashCode)))
169 {
170 CHECK (0 == memcmp (&pid0,
171 &s_elem->peer_id,
172 sizeof(struct GNUNET_PeerIdentity)));
173 }
174 else
175 {
176 CHECK (0 == memcmp (&pid1,
177 &s_elem->peer_id,
178 sizeof(struct GNUNET_PeerIdentity)));
179 }
180 RPS_sampler_elem_destroy (s_elem);
181
182 /* Check _set() */
183 s_elem = RPS_sampler_elem_create ();
184 CHECK (NULL != s_elem);
185 CHECK (EMPTY == s_elem->is_empty);
186 CHECK (NULL != &s_elem->auth_key);
187 auth_key = s_elem->auth_key;
188 memset (&auth_key2, 0, sizeof(auth_key2));
189 RPS_sampler_elem_set (s_elem, auth_key2);
190 CHECK (0 == memcmp (auth_key2.key,
191 s_elem->auth_key.key,
192 GNUNET_CRYPTO_HASH_LENGTH));
193 RPS_sampler_elem_destroy (s_elem);
194
195
196 /* TODO: deterministic tests (use _set() to set auth_key) */
197 return 0;
198}
199
200
201int
202main (int argc, char *argv[])
203{
204 (void) argc;
205 (void) argv;
206
207 GNUNET_log_setup ("test_service_rps_peers",
208 "WARNING",
209 NULL);
210 return check ();
211}
212
213
214/* end of test_service_rps_peers.c */
diff --git a/src/contrib/service/rps/test_service_rps_view.c b/src/contrib/service/rps/test_service_rps_view.c
new file mode 100644
index 000000000..bb1413168
--- /dev/null
+++ b/src/contrib/service/rps/test_service_rps_view.c
@@ -0,0 +1,146 @@
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 <platform.h>
26#include "gnunet-service-rps_view.h"
27
28#define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
29 View_destroy (view); return 1; }
30#define CHECK(c) { if (! (c)) ABORT (); }
31
32
33static int
34check ()
35{
36 struct View *view;
37 struct GNUNET_PeerIdentity k1;
38 struct GNUNET_PeerIdentity k2;
39 const struct GNUNET_PeerIdentity *array;
40 unsigned int j;
41
42 view = View_create (3);
43 memset (&k1, 0, sizeof(k1));
44 memset (&k2, 1, sizeof(k2));
45 CHECK (GNUNET_NO == View_contains_peer (view, &k1));
46 CHECK (GNUNET_NO == View_contains_peer (view, &k2));
47 CHECK (GNUNET_NO == View_remove_peer (view, &k1));
48 CHECK (GNUNET_NO == View_remove_peer (view, &k2));
49 CHECK (NULL == View_get_peer_by_index (view, 0));
50 CHECK (NULL == View_get_peer_by_index (view, 1));
51 View_clear (view); /* See if assertions trigger */
52 CHECK (0 == View_size (view));
53
54 CHECK (GNUNET_OK == View_put (view, &k1));
55 CHECK (1 == View_size (view));
56 CHECK (GNUNET_NO == View_put (view, &k1));
57 CHECK (1 == View_size (view));
58 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
59 CHECK (GNUNET_OK == View_remove_peer (view, &k1));
60 CHECK (0 == View_size (view));
61 CHECK (GNUNET_NO == View_contains_peer (view, &k1));
62 CHECK (GNUNET_NO == View_contains_peer (view, &k2));
63
64 CHECK (GNUNET_OK == View_put (view, &k1));
65 CHECK (1 == View_size (view));
66 for (j = 0; j < 16; j++)
67 {
68 CHECK (GNUNET_NO == View_put (view, &k1));
69 }
70 CHECK (1 == View_size (view));
71 CHECK (GNUNET_OK == View_put (view, &k2));
72 CHECK (2 == View_size (view));
73 for (j = 0; j < 16; j++)
74 {
75 CHECK (GNUNET_NO == View_put (view, &k2));
76 }
77 CHECK (2 == View_size (view));
78
79 /* iterate */
80 for (j = 0; j < View_size (view); j++)
81 {
82 CHECK (NULL != View_get_peer_by_index (view, j));
83 }
84 CHECK ((0 == memcmp (View_get_peer_by_index (view, 0),
85 &k1, sizeof(k1))));
86 CHECK ((0 == memcmp (View_get_peer_by_index (view, 1),
87 &k2, sizeof(k2))));
88 CHECK (GNUNET_OK == View_remove_peer (view, &k1));
89 CHECK (1 == View_size (view));
90 CHECK (GNUNET_NO == View_contains_peer (view, &k1));
91 CHECK (GNUNET_YES == View_contains_peer (view, &k2));
92 CHECK (NULL != View_get_peer_by_index (view, 0));
93 CHECK (NULL == View_get_peer_by_index (view, 1));
94
95 View_clear (view);
96 CHECK (0 == View_size (view));
97
98 CHECK (GNUNET_OK == View_put (view, &k1));
99 CHECK (1 == View_size (view));
100 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
101 CHECK (GNUNET_OK == View_put (view, &k2));
102 CHECK (2 == View_size (view));
103 CHECK (GNUNET_YES == View_contains_peer (view, &k2));
104 array = View_get_as_array (view);
105 CHECK (0 == memcmp (&array[0], &k1, sizeof(k1)));
106 CHECK (0 == memcmp (&array[1], &k2, sizeof(k2)));
107 View_clear (view);
108 CHECK (0 == View_size (view));
109
110 /*View_change_len () */
111 CHECK (GNUNET_OK == View_put (view, &k1));
112 CHECK (GNUNET_OK == View_put (view, &k2));
113 CHECK (2 == View_size (view));
114 View_change_len (view, 4);
115 CHECK (2 == View_size (view));
116 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
117 CHECK (GNUNET_YES == View_contains_peer (view, &k2));
118 array = View_get_as_array (view);
119 CHECK (0 == memcmp (&array[0], &k1, sizeof(k1)));
120 CHECK (0 == memcmp (&array[1], &k2, sizeof(k2)));
121 View_change_len (view, 1);
122 CHECK (1 == View_size (view));
123 CHECK (GNUNET_YES == View_contains_peer (view, &k1));
124 CHECK (GNUNET_NO == View_contains_peer (view, &k2));
125 array = View_get_as_array (view);
126 CHECK (0 == memcmp (&array[0], &k1, sizeof(k1)));
127 View_clear (view);
128 CHECK (0 == View_size (view));
129
130 View_destroy (view);
131
132 return 0;
133}
134
135
136int
137main ()
138{
139 GNUNET_log_setup ("test_service_rps_peers",
140 "WARNING",
141 NULL);
142 return check ();
143}
144
145
146/* end of test_service_rps_view.c */