aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2014-12-05 14:07:11 +0000
committerBart Polot <bart@net.in.tum.de>2014-12-05 14:07:11 +0000
commit6cd1fc3aa29926ce0326d07ba684e1b65a1a0db7 (patch)
treeb201a08dda53a39a1572a18d8e08d0359896ea74
parentf78293457b6e5d922d199a0c1f28a544ba943e7a (diff)
downloadgnunet-6cd1fc3aa29926ce0326d07ba684e1b65a1a0db7.tar.gz
gnunet-6cd1fc3aa29926ce0326d07ba684e1b65a1a0db7.zip
Import RPS
-rw-r--r--.gitignore12
-rw-r--r--configure.ac3
-rw-r--r--pkgconfig/Makefile.am2
-rw-r--r--pkgconfig/gnunetrps.pc.in12
-rw-r--r--src/Makefile.am1
-rw-r--r--src/include/gnunet_protocols.h39
-rw-r--r--src/include/gnunet_rps_service.h83
-rw-r--r--src/rps/Makefile.am76
-rw-r--r--src/rps/gnunet-rps.c71
-rw-r--r--src/rps/gnunet-service-rps.c1382
-rw-r--r--src/rps/rps.conf.in7
-rw-r--r--src/rps/rps.h139
-rw-r--r--src/rps/rps_api.c279
-rw-r--r--src/rps/test_rps.conf39
-rw-r--r--src/rps/test_rps_api.c84
-rw-r--r--src/rps/test_rps_multipeer.c244
16 files changed, 2472 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 1cef035c4..e80a99663 100644
--- a/.gitignore
+++ b/.gitignore
@@ -132,6 +132,7 @@
132/pkgconfig/gnunetats.pc 132/pkgconfig/gnunetats.pc
133/pkgconfig/gnunetregex.pc 133/pkgconfig/gnunetregex.pc
134/pkgconfig/gnunetnse.pc 134/pkgconfig/gnunetnse.pc
135/pkgconfig/gnunetrps.pc
135/pkgconfig/gnunetnat.pc 136/pkgconfig/gnunetnat.pc
136/pkgconfig/Makefile.in 137/pkgconfig/Makefile.in
137/pkgconfig/Makefile 138/pkgconfig/Makefile
@@ -1000,6 +1001,17 @@
1000/src/revocation/Makefile 1001/src/revocation/Makefile
1001/src/revocation/.deps 1002/src/revocation/.deps
1002 1003
1004# /src/rps/
1005/src/rps/rps.conf
1006/src/rps/test_rps_multipeer
1007/src/rps/Makefile.in
1008/src/rps/Makefile
1009/src/rps/.deps
1010/src/rps/gnunet-service-rps
1011/src/rps/gnunet-rps
1012/src/rps/*log
1013/src/rps/*.trs
1014
1003# /src/scalarproduct/ 1015# /src/scalarproduct/
1004/src/scalarproduct/gnunet-service-scalarproduct 1016/src/scalarproduct/gnunet-service-scalarproduct
1005/src/scalarproduct/scalarproduct.conf 1017/src/scalarproduct/scalarproduct.conf
diff --git a/configure.ac b/configure.ac
index ae7e04394..9c0bdc1b1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1483,6 +1483,8 @@ src/regex/Makefile
1483src/regex/regex.conf 1483src/regex/regex.conf
1484src/revocation/Makefile 1484src/revocation/Makefile
1485src/revocation/revocation.conf 1485src/revocation/revocation.conf
1486src/rps/Makefile
1487src/rps/rps.conf
1486src/secretsharing/Makefile 1488src/secretsharing/Makefile
1487src/secretsharing/secretsharing.conf 1489src/secretsharing/secretsharing.conf
1488src/sensor/Makefile 1490src/sensor/Makefile
@@ -1542,6 +1544,7 @@ pkgconfig/gnunetpsyc.pc
1542pkgconfig/gnunetpsycstore.pc 1544pkgconfig/gnunetpsycstore.pc
1543pkgconfig/gnunetregex.pc 1545pkgconfig/gnunetregex.pc
1544pkgconfig/gnunetrevocation.pc 1546pkgconfig/gnunetrevocation.pc
1547pkgconfig/gnunetrps.pc
1545pkgconfig/gnunetscalarproduct.pc 1548pkgconfig/gnunetscalarproduct.pc
1546pkgconfig/gnunetset.pc 1549pkgconfig/gnunetset.pc
1547pkgconfig/gnunetsocial.pc 1550pkgconfig/gnunetsocial.pc
diff --git a/pkgconfig/Makefile.am b/pkgconfig/Makefile.am
index ac40906cd..547755c12 100644
--- a/pkgconfig/Makefile.am
+++ b/pkgconfig/Makefile.am
@@ -31,6 +31,7 @@ pcfiles = \
31 gnunetpsycstore.pc \ 31 gnunetpsycstore.pc \
32 gnunetregex.pc \ 32 gnunetregex.pc \
33 gnunetrevocation.pc \ 33 gnunetrevocation.pc \
34 gnunetrps.pc \
34 gnunetscalarproduct.pc \ 35 gnunetscalarproduct.pc \
35 gnunetset.pc \ 36 gnunetset.pc \
36 gnunetspeaker.pc \ 37 gnunetspeaker.pc \
@@ -87,6 +88,7 @@ EXTRA_DIST = \
87 gnunetpsycstore.pc.in \ 88 gnunetpsycstore.pc.in \
88 gnunetregex.pc.in \ 89 gnunetregex.pc.in \
89 gnunetrevocation.pc.in \ 90 gnunetrevocation.pc.in \
91 gnunetrps.pc.in \
90 gnunetscalarproduct.pc.in \ 92 gnunetscalarproduct.pc.in \
91 gnunetset.pc.in \ 93 gnunetset.pc.in \
92 gnunetspeaker.pc.in \ 94 gnunetspeaker.pc.in \
diff --git a/pkgconfig/gnunetrps.pc.in b/pkgconfig/gnunetrps.pc.in
new file mode 100644
index 000000000..94042e8eb
--- /dev/null
+++ b/pkgconfig/gnunetrps.pc.in
@@ -0,0 +1,12 @@
1prefix=@prefix@
2exec_prefix=@exec_prefix@
3libdir=@libdir@
4includedir=@includedir@
5
6Name: GNUnet RPS
7Description: random peer sampling based on the BRAHMS protocol
8URL: https://gnunet.org
9Version: @VERSION@
10Requires:
11Libs: -L${libdir} -lgnunetrps
12Cflags: -I${includedir}
diff --git a/src/Makefile.am b/src/Makefile.am
index 18f8aeef2..8efb7afe6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -20,6 +20,7 @@ if HAVE_EXPERIMENTAL
20 env \ 20 env \
21 psycstore \ 21 psycstore \
22 psyc \ 22 psyc \
23 rps \
23 social \ 24 social \
24 $(CONSENSUS) \ 25 $(CONSENSUS) \
25 $(SECRETSHARING) \ 26 $(SECRETSHARING) \
diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h
index 5a2b644be..747a252ff 100644
--- a/src/include/gnunet_protocols.h
+++ b/src/include/gnunet_protocols.h
@@ -2630,10 +2630,47 @@ extern "C"
2630#define GNUNET_MESSAGE_TYPE_DHT_CLIENT_ACT_MALICIOUS_OK 894 2630#define GNUNET_MESSAGE_TYPE_DHT_CLIENT_ACT_MALICIOUS_OK 894
2631 2631
2632#endif 2632#endif
2633
2634/*******************************************************************************
2635 * RPS messages
2636 ******************************************************************************/
2637
2638/* P2P Messages */
2639
2640/**
2641 * RPS PUSH message to push own ID to another peer
2642 */
2643#define GNUNET_MESSAGE_TYPE_RPS_PP_PUSH 950
2644
2645/**
2646 * RPS PULL REQUEST message to request the local view of another peer
2647 */
2648#define GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST 951
2649
2650/**
2651 * RPS PULL REPLY message which contains the view of the other peer
2652 */
2653#define GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY 952
2654
2655
2656
2657/* Client-Service Messages */
2658
2659/**
2660 * RPS CS REQUEST Message for the Client to request (a) random peer(s)
2661 */
2662#define GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST 953
2663
2664/**
2665 * RPS CS REPLY Message for the Server to send (a) random peer(s)
2666 */
2667#define GNUNET_MESSAGE_TYPE_RPS_CS_REPLY 954
2668
2669
2633/*******************************************************************************/ 2670/*******************************************************************************/
2634 2671
2635/** 2672/**
2636 * Next available: 904 2673 * Next available: 960
2637 */ 2674 */
2638 2675
2639/** 2676/**
diff --git a/src/include/gnunet_rps_service.h b/src/include/gnunet_rps_service.h
new file mode 100644
index 000000000..dc822b8f1
--- /dev/null
+++ b/src/include/gnunet_rps_service.h
@@ -0,0 +1,83 @@
1/*
2 This file is part of GNUnet
3 (C)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21/**
22 * @file include/gnunet_rps_service.h
23 * @brief API to the rps service
24 * @author Julius Bünger
25 */
26#ifndef GNUNET_RPS_SERVICE_H
27#define GNUNET_RPS_SERVICE_H
28
29#ifdef __cplusplus
30extern "C"
31{
32#if 0 /* keep Emacsens' auto-indent happy */
33}
34#endif
35#endif
36
37/**
38 * Version of the rps API.
39 */
40#define GNUNET_RPS_VERSION 0x00000000
41
42typedef void (* GNUNET_RPS_NotifyReadyCB) (void *cls, uint64_t num_peers, struct GNUNET_PeerIdentity *peers);
43
44 struct GNUNET_RPS_Request_Handle *
45GNUNET_RPS_request_peers_single_call (const struct GNUNET_CONFIGURATION_Handle *cfg,
46 uint64_t n,
47 GNUNET_RPS_NotifyReadyCB ready_cb,
48 void *cls);
49
50/**
51 * Connect to the rps service
52 */
53 struct GNUNET_RPS_Handle *
54GNUNET_RPS_connect( const struct GNUNET_CONFIGURATION_Handle *cfg );
55
56/**
57 * Request n random peers.
58 */
59 struct GNUNET_RPS_Request_Handle *
60GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint64_t n,
61 GNUNET_RPS_NotifyReadyCB ready_cb,
62 void *cls);
63
64/**
65 * Cancle an issued request.
66 */
67 void
68GNUNET_RPS_request_cancel ( struct GNUNET_RPS_Request_Handle *rh );
69
70/**
71 * Disconnect from the rps service
72 */
73 void
74GNUNET_RPS_disconnect ( struct GNUNET_RPS_Handle *h );
75
76#if 0 /* keep Emacsens' auto-indent happy */
77{
78#endif
79#ifdef __cplusplus
80}
81#endif
82
83#endif
diff --git a/src/rps/Makefile.am b/src/rps/Makefile.am
new file mode 100644
index 000000000..f6c31b107
--- /dev/null
+++ b/src/rps/Makefile.am
@@ -0,0 +1,76 @@
1AM_CPPFLAGS = -I$(top_srcdir)/src/include
2
3if MINGW
4 WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols
5endif
6
7if USE_COVERAGE
8 AM_CFLAGS = --coverage -O0
9 XLIB = -lgcov
10endif
11
12pkgcfgdir= $(pkgdatadir)/config.d/
13
14libexecdir= $(pkglibdir)/libexec/
15
16pkgcfg_DATA = \
17 rps.conf
18
19bin_PROGRAMS = gnunet-rps
20
21gnunet_rps_SOURCES = gnunet-rps.c
22gnunet_rps_LDADD = \
23 libgnunetrps.la \
24 $(top_builddir)/src/util/libgnunetutil.la \
25 $(XLIB) $(GN_LIBINTL)
26
27lib_LTLIBRARIES = libgnunetrps.la
28
29libgnunetrps_la_SOURCES = \
30 rps_api.c rps.h
31libgnunetrps_la_LIBADD = \
32 $(top_builddir)/src/util/libgnunetutil.la \
33 $(GN_LIBINTL) $(XLIB)
34libgnunetrps_la_LDFLAGS = \
35 $(GN_LIB_LDFLAGS) $(WINFLAGS) \
36 -version-info 0:0:0
37
38
39libexec_PROGRAMS = \
40 gnunet-service-rps
41
42
43gnunet_service_rps_SOURCES = \
44 gnunet-service-rps.c
45gnunet_service_rps_LDADD = \
46 libgnunetrps.la \
47 $(top_builddir)/src/util/libgnunetutil.la \
48 $(top_builddir)/src/cadet/libgnunetcadet.la \
49 $(top_builddir)/src/nse/libgnunetnse.la \
50 $(top_builddir)/src/statistics/libgnunetstatistics.la \
51 $(LIBGCRYPT_LIBS) \
52 -lm -lgcrypt \
53 $(GN_LIBINTL)
54
55if HAVE_TESTING
56check_PROGRAMS = \
57 test_rps_multipeer
58endif
59
60if ENABLE_TEST_RUN
61AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;
62TESTS = $(check_PROGRAMS)
63endif
64
65test_rps_multipeer_SOURCES = \
66 test_rps_multipeer.c
67test_rps_multipeer_LDADD = \
68 libgnunetrps.la \
69 $(top_builddir)/src/util/libgnunetutil.la \
70 $(top_builddir)/src/testbed/libgnunettestbed.la \
71 -lm
72
73
74EXTRA_DIST = \
75 test_rps.conf
76
diff --git a/src/rps/gnunet-rps.c b/src/rps/gnunet-rps.c
new file mode 100644
index 000000000..7a4f75d6f
--- /dev/null
+++ b/src/rps/gnunet-rps.c
@@ -0,0 +1,71 @@
1/*
2 This file is part of GNUnet.
3 (C)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file rps/gnunet-rps.c
23 * @brief rps tool
24 * @author Julius Bünger
25 */
26#include <gnunet/platform.h>
27#include <gnunet/gnunet_util_lib.h>
28#include "gnunet_rps_service.h"
29
30static int ret;
31
32/**
33 * Main function that will be run by the scheduler.
34 *
35 * @param cls closure
36 * @param args remaining command-line arguments
37 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
38 * @param cfg configuration
39 */
40static void
41run (void *cls,
42 char *const *args,
43 const char *cfgfile,
44 const struct GNUNET_CONFIGURATION_Handle *cfg)
45{
46 ret = 0;
47}
48
49/**
50 * The main function to rps.
51 *
52 * @param argc number of arguments from the command line
53 * @param argv command line arguments
54 * @return 0 ok, 1 on error
55 */
56int
57main (int argc, char *const *argv)
58{
59 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
60 GNUNET_GETOPT_OPTION_END
61 };
62 return (GNUNET_OK ==
63 GNUNET_PROGRAM_run (argc,
64 argv,
65 "gnunet-rps [options [value]]",
66 gettext_noop
67 ("rps"),
68 options, &run, NULL)) ? ret : 1;
69}
70
71/* end of gnunet-rps.c */
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
new file mode 100644
index 000000000..6d96f5486
--- /dev/null
+++ b/src/rps/gnunet-service-rps.c
@@ -0,0 +1,1382 @@
1/*
2 This file is part of GNUnet.
3 (C)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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_util_lib.h"
28#include "gnunet_cadet_service.h"
29#include "gnunet_nse_service.h"
30#include "rps.h"
31
32#include <math.h>
33#include <inttypes.h>
34
35#define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
36
37// TODO modify @brief in every file
38
39// TODO take care that messages are not longer than 64k
40
41// TODO check for overflows
42
43// TODO align message structs
44
45// TODO multipeerlist indep of gossiped list
46
47// TODO maybe wait during initialisation some time to get some peers
48// - initialise peers before proceeding
49// - Use the magic 0000 peer GNUNET_CADET_get_peers() returns
50
51// (TODO api -- possibility of getting weak random peer immideately)
52
53// TODO malicious peer
54
55// TODO switch Slist -> DLL
56
57/**
58 * Our configuration.
59 */
60static const struct GNUNET_CONFIGURATION_Handle *cfg;
61
62/**
63 * Our own identity.
64 */
65struct GNUNET_PeerIdentity *own_identity;
66
67/**
68 * Compare two peer identities. Taken from secretsharing.
69 *
70 * @param p1 Some peer identity.
71 * @param p2 Some peer identity.
72 * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
73 */
74static int
75peer_id_cmp (const void *p1, const void *p2)
76{
77 return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
78}
79
80/***********************************************************************
81 * Sampler
82 *
83 * WARNING: This section needs to be reviewed regarding the use of
84 * functions providing (pseudo)randomness!
85***********************************************************************/
86
87// TODO init list
88// TODO grow/shrink list
89
90/**
91 * A sampler sampling PeerIDs.
92 */
93struct Sampler
94{
95 /**
96 * Min-wise linear permutation used by this sampler.
97 *
98 * This is an key later used by a hmac.
99 */
100 struct GNUNET_CRYPTO_AuthKey auth_key;
101
102 /**
103 * The PeerID this sampler currently samples.
104 */
105 struct GNUNET_PeerIdentity *peer_id;
106
107 /**
108 * The according hash value of this PeerID.
109 */
110 struct GNUNET_HashCode peer_id_hash;
111
112};
113
114typedef void (* SAMPLER_deleteCB) (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash);
115
116/**
117 * (Re)Initialise given Sampler with random min-wise independent function.
118 *
119 * In this implementation this means choosing an auth_key for later use in
120 * a hmac at random.
121 */
122 struct Sampler *
123SAMPLER_init()
124{
125 struct Sampler *s;
126
127 s = GNUNET_new(struct Sampler);
128
129 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
130 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
131 &(s->auth_key.key),
132 GNUNET_CRYPTO_HASH_LENGTH);
133
134 s->peer_id = own_identity; // Maybe set to own PeerID. So we always have
135 // a valid PeerID in the sampler.
136 // Maybe take a PeerID as second argument.
137
138 GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
139 sizeof(struct GNUNET_PeerIdentity),
140 &s->peer_id_hash);
141
142 return s;
143}
144
145/**
146 * Compare two hashes.
147 *
148 * Returns if the first one is smaller then the second.
149 * Used by SAMPLER_next() to compare hashes.
150 */
151 int
152hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2)
153{
154 return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode)) < 0;
155}
156
157/**
158 * Input an PeerID into the given sampler.
159 */
160 static void
161SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls)
162{
163 struct GNUNET_HashCode other_hash;
164
165 GNUNET_CRYPTO_hmac(&s->auth_key,
166 id,
167 sizeof(struct GNUNET_PeerIdentity),
168 &other_hash);
169
170 if ( NULL == s->peer_id ) { // Or whatever is a valid way to say
171 // "we have no PeerID at the moment"
172 *s->peer_id = *id;
173 s->peer_id_hash = other_hash;
174
175 } else {
176
177 if ( hash_cmp(other_hash, s->peer_id_hash) ) {
178 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Discarding old PeerID %s\n",
179 GNUNET_i2s(id), GNUNET_i2s(s->peer_id));
180
181 if ( NULL != del_cb ) {
182 del_cb(cb_cls, s->peer_id, s->peer_id_hash);
183 }
184 *s->peer_id = *id;
185 s->peer_id_hash = other_hash;
186
187 } else {
188 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Keeping old PeerID %s\n",
189 GNUNET_i2s(id), GNUNET_i2s(s->peer_id));
190 }
191
192 }
193}
194
195
196
197/**
198 * A fuction to update every sampler in the given list
199 */
200 static void
201SAMPLER_update_list(struct GNUNET_CONTAINER_SList *lst, const struct GNUNET_PeerIdentity *id,
202 SAMPLER_deleteCB del_cb, void *cb_cls)
203{
204 struct GNUNET_CONTAINER_SList_Iterator *iter;
205 struct Sampler *sampler;
206 size_t s;
207
208 iter = GNUNET_malloc(sizeof(struct GNUNET_CONTAINER_SList_Iterator));
209 *iter = GNUNET_CONTAINER_slist_begin(lst);
210 s = sizeof(struct Sampler);
211 do {
212 sampler = (struct Sampler *) GNUNET_CONTAINER_slist_get(iter, &s);
213 SAMPLER_next(sampler, id, del_cb, cb_cls);
214 } while ( GNUNET_NO != GNUNET_CONTAINER_slist_next(iter) );
215
216 GNUNET_CONTAINER_slist_iter_destroy(iter);
217}
218
219/**
220 * Get one random peer out of the sampled peers.
221 *
222 * We might want to reinitialise this sampler after giving the
223 * corrsponding peer to the client.
224 */
225 struct GNUNET_PeerIdentity*
226SAMPLER_get_rand_peer (struct GNUNET_CONTAINER_SList *lst)
227{
228 uint64_t list_size;
229
230 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
231
232 list_size = (uint64_t) GNUNET_CONTAINER_slist_count(lst);
233
234 if ( 0 == list_size ) {
235 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
236 return own_identity;
237 } else {
238 uint64_t index;
239 struct GNUNET_CONTAINER_SList_Iterator *iter;
240 uint64_t i;
241 size_t s;
242 struct GNUNET_PeerIdentity *peer;
243
244 /**
245 * Choose the index of the peer we want to give back
246 * at random from the interval of the sampler list
247 */
248 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
249 list_size);
250 // TODO check that it does not overflow
251 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Length of Slist: %" PRIu64 ", index: %" PRIu64 "\n", list_size, index);
252
253 s = sizeof( struct Sampler );
254 iter = GNUNET_malloc(sizeof(struct GNUNET_CONTAINER_SList_Iterator));
255 *iter = GNUNET_CONTAINER_slist_begin(lst);
256 for ( i = 0 ; i < index ; i++ ) {
257 if (GNUNET_NO == GNUNET_CONTAINER_slist_next(iter) ) { // Maybe unneeded
258 *iter = GNUNET_CONTAINER_slist_begin(lst);
259 }
260 }
261
262 // TODO something missing?
263
264 // FIXME this looks wrong:
265 peer = ((struct Sampler *) GNUNET_CONTAINER_slist_get(iter, &s))->peer_id;
266 GNUNET_CONTAINER_slist_iter_destroy(iter);
267 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s (own ID: %s)\n", GNUNET_i2s(peer), GNUNET_i2s(own_identity));
268
269 return peer;
270 }
271}
272
273/**
274 * Get n random peers out of the sampled peers.
275 *
276 * We might want to reinitialise this sampler after giving the
277 * corrsponding peer to the client.
278 * Random with or without consumption?
279 */
280 struct GNUNET_PeerIdentity** // TODO give back simple array
281SAMPLER_get_n_rand_peers (struct GNUNET_CONTAINER_SList *lst, uint64_t n)
282{
283 // TODO check if we have too much (distinct) sampled peers
284 // If we are not ready yet maybe schedule for later
285 struct GNUNET_PeerIdentity **peers;
286 uint64_t i;
287
288 peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity *));
289
290 for ( i = 0 ; i < n ; i++ ) {
291 peers[i] = SAMPLER_get_rand_peer(lst);
292 }
293
294 // TODO something else missing?
295 return peers;
296}
297
298/**
299 * Counts how many Samplers currently hold a given PeerID.
300 */
301 uint64_t
302SAMPLER_count_id ( struct GNUNET_CONTAINER_SList *lst, struct GNUNET_PeerIdentity *id ) {
303 size_t s;
304 struct GNUNET_CONTAINER_SList_Iterator *iter;
305 uint64_t count;
306
307 s = sizeof( struct Sampler );
308 iter = GNUNET_new(struct GNUNET_CONTAINER_SList_Iterator);
309 *iter = GNUNET_CONTAINER_slist_begin(lst);
310 count = 0;
311 while ( GNUNET_YES == GNUNET_CONTAINER_slist_next(iter) ) {
312 if ( peer_id_cmp( ((struct Sampler *) GNUNET_CONTAINER_slist_get(iter, &s))->peer_id, id) ) {
313 count++;
314 }
315 }
316 GNUNET_CONTAINER_slist_iter_destroy(iter);
317 return count;
318}
319
320
321/***********************************************************************
322 * /Sampler
323***********************************************************************/
324
325
326
327/***********************************************************************
328 * Gossip list
329***********************************************************************/
330
331///**
332// * Get one random peer out of the gossiped peer list.
333// */
334// struct GNUNET_PeerIdentity *
335//get_random_peer(struct GNUNET_CONTAINER_MultiPeerMap * lst)
336//{
337// size_t n;
338// struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
339// uint64_t index;
340// uint64_t i;
341// struct GNUNET_PeerIdentity *peer;
342//
343// n = (size_t) GNUNET_CONTAINER_multipeermap_size(lst);
344// index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
345// (uint64_t) n);
346// iter = GNUNET_CONTAINER_multipeermap_iterator_create(lst);
347//
348// for ( i = 0 ; i < index ; i++ ) {
349// GNUNET_CONTAINER_multipeermap_iterator_next(iter, NULL, NULL);
350// }
351//
352// peer = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
353// GNUNET_CONTAINER_multipeermap_iterator_next(iter, peer, NULL);
354//
355// return peer;
356//}
357
358
359/***********************************************************************
360 * /Gossip list
361***********************************************************************/
362
363
364
365/***********************************************************************
366 * Housekeeping with peers
367***********************************************************************/
368
369/**
370 * Struct used to store the context of a connected client.
371 */
372struct client_ctx
373{
374 /**
375 * The message queue to communicate with the client.
376 */
377 struct GNUNET_MQ_Handle *mq;
378};
379
380/**
381 * Used to keep track in what lists single peerIDs are.
382 */
383enum in_list_flag // probably unneeded
384{
385 in_other_sampler_list = 0x1,
386 in_other_gossip_list = 0x2, // unneeded?
387 in_own_sampler_list = 0x4,
388 in_own_gossip_list = 0x8 // unneeded?
389};
390
391/**
392 * Struct used to keep track of other peer's status
393 *
394 * This is stored in a multipeermap.
395 */
396struct peer_context
397{
398 /**
399 * In own gossip/sampler list, in other's gossip/sampler list
400 */
401 uint32_t in_flags; // unneeded?
402
403 /**
404 * Message queue open to client
405 */
406 struct GNUNET_MQ_Handle *mq;
407
408 /**
409 * Channel open to client.
410 */
411 struct GNUNET_CADET_Channel *to_channel;
412
413 /**
414 * Channel open from client.
415 */
416 struct GNUNET_CADET_Channel *from_channel; // unneeded
417
418 /**
419 * This is pobably followed by 'statistical' data (when we first saw
420 * him, how did we get his ID, how many pushes (in a timeinterval),
421 * ...)
422 */
423};
424
425/***********************************************************************
426 * /Housekeeping with peers
427***********************************************************************/
428
429/**
430 * Set of all peers to keep track of them.
431 */
432static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
433
434
435// -- gossip list length --
436// Depends on the (estimated) size of the
437// network. - Initial size might be the
438// number of peers cadet provides.
439// TODO other events to grow/shrink size?
440
441/**
442 * List of samplers
443 */
444struct GNUNET_CONTAINER_SList *sampler_list;
445
446/**
447 * Sampler list size
448 *
449 * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3).
450 */
451size_t sampler_list_size;
452
453
454/**
455 * The gossiped list of peers.
456 */
457struct GNUNET_PeerIdentity *gossip_list;
458
459/**
460 * Size of the gossiped list
461 */
462unsigned int gossip_list_size;
463
464/**
465 * Min size of the gossip list
466 */
467uint64_t gossip_list_min_size;
468
469///**
470// * Max size of the gossip list
471// *
472// * This will probably be left to be set by the client.
473// */
474//uint64_t gossip_list_max_size;
475
476
477/**
478 * The estimated size of the network.
479 *
480 * Influenced by the stdev.
481 */
482size_t est_size;
483
484
485
486/**
487 * Percentage of total peer number in the gossip list
488 * to send random PUSHes to
489 */
490float alpha;
491
492/**
493 * Percentage of total peer number in the gossip list
494 * to send random PULLs to
495 */
496float beta;
497
498/**
499 * The percentage gamma of history updates.
500 * Simply 1 - alpha - beta
501 */
502
503
504
505
506/**
507 * Identifier for the main task that runs periodically.
508 */
509GNUNET_SCHEDULER_TaskIdentifier do_round_task;
510
511/**
512 * Time inverval the do_round task runs in.
513 */
514struct GNUNET_TIME_Relative round_interval;
515
516
517
518/**
519 * List to store peers received through pushes temporary.
520 */
521struct GNUNET_CONTAINER_SList *push_list;
522
523/**
524 * List to store peers received through pulls temporary.
525 */
526struct GNUNET_CONTAINER_SList *pull_list;
527
528
529/**
530 * Handler to NSE.
531 */
532struct GNUNET_NSE_Handle *nse;
533
534/**
535 * Handler to CADET.
536 */
537struct GNUNET_CADET_Handle *cadet_handle;
538
539
540/***********************************************************************
541 * Util functions
542***********************************************************************/
543
544/**
545 * Get random peer from the gossip list.
546 */
547 struct GNUNET_PeerIdentity *
548get_rand_gossip_peer()
549{
550 uint64_t index;
551 struct GNUNET_PeerIdentity *peer;
552
553 // TODO find a better solution.
554 // FIXME if we have only own ID in gossip list this will block
555 // but then we might have a problem nevertheless ?
556
557 do {
558
559 /**;
560 * Choose the index of the peer we want to return
561 * at random from the interval of the gossip list
562 */
563 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
564 gossip_list_size);
565
566 peer = &(gossip_list[index]);
567 } while ( own_identity == peer || NULL == peer );
568
569 return peer;
570}
571
572/**
573 * Get the message queue of a specific peer.
574 *
575 * If we already have a message queue open to this client,
576 * simply return it, otherways create one.
577 */
578 struct GNUNET_MQ_Handle *
579get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id)
580{
581 struct peer_context *ctx;
582 struct GNUNET_MQ_Handle * mq;
583 struct GNUNET_CADET_Channel *channel;
584
585 if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) {
586
587 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
588 GNUNET_RPS_CADET_PORT,
589 GNUNET_CADET_OPTION_RELIABLE);
590 mq = GNUNET_CADET_mq_create(channel);
591
592 ctx = GNUNET_malloc(sizeof(struct peer_context));
593 ctx->in_flags = 0;
594 ctx->to_channel = channel;
595 ctx->mq = mq;
596
597 GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
599 } else {
600 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer_id);
601 if ( NULL == ctx->mq ) {
602 if ( NULL == ctx->to_channel ) {
603 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
604 GNUNET_RPS_CADET_PORT,
605 GNUNET_CADET_OPTION_RELIABLE);
606 ctx->to_channel = channel;
607 }
608
609 mq = GNUNET_CADET_mq_create(ctx->to_channel);
610 ctx->mq = mq;
611 }
612 }
613
614 return ctx->mq;
615}
616
617
618/***********************************************************************
619 * /Util functions
620***********************************************************************/
621
622/**
623 * Function called by NSE.
624 *
625 * Updates sizes of sampler list and gossip list and adapt those lists
626 * accordingly.
627 */
628 void
629nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
630{
631 double estimate;
632 //double scale; // TODO this might go gloabal/config
633
634 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
635 //scale = .01;
636 estimate = 1 << (uint64_t) round(logestimate);
637 // GNUNET_NSE_log_estimate_to_n (logestimate);
638 estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
639 if ( 0 < estimate ) {
640 LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
641 est_size = estimate;
642 } else {
643 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
644 }
645}
646
647/**
648 * Handle RPS request from the client.
649 *
650 * @param cls closure
651 * @param client identification of the client
652 * @param message the actual message
653 */
654static void
655// TODO rename
656handle_cs_request (void *cls,
657 struct GNUNET_SERVER_Client *client,
658 const struct GNUNET_MessageHeader *message)
659{
660 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
661
662 struct GNUNET_RPS_CS_RequestMessage *msg;
663 //unsigned int n_arr[sampler_list_size];// =
664 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
665 //struct GNUNET_MQ_Handle *mq;
666 struct client_ctx *cli_ctx;
667 struct GNUNET_MQ_Envelope *ev;
668 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
669 uint64_t num_peers;
670 uint64_t i;
671
672 // TODO
673 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
674 // Does not work because the compiler seems not to find it.
675 cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
676 if ( NULL == cli_ctx ) {
677 cli_ctx = GNUNET_new(struct client_ctx);
678 cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
679 GNUNET_SERVER_client_set_user_context(client, cli_ctx);
680 }
681
682 //mq = GNUNET_MQ_queue_for_server_client(client);
683
684 // TODO How many peers do we give back?
685 // Wait until we have enough random peers?
686
687 ev = GNUNET_MQ_msg_extra(out_msg,
688 GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
689 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
690 out_msg->num_peers = GNUNET_ntohll(msg->num_peers);
691
692 num_peers = GNUNET_ntohll(msg->num_peers);
693 //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
694 for ( i = 0 ; i < num_peers ; i++ ) {
695 memcpy(&out_msg[1] + i * sizeof(struct GNUNET_PeerIdentity),
696 SAMPLER_get_rand_peer(sampler_list),
697 sizeof(struct GNUNET_PeerIdentity));
698 }
699
700 GNUNET_MQ_send(cli_ctx->mq, ev);
701 //GNUNET_MQ_send(mq, ev);
702 //GNUNET_MQ_destroy(mq);
703
704 GNUNET_SERVER_receive_done (client,
705 GNUNET_OK);
706}
707
708/**
709 * Handle a PUSH message from another peer.
710 *
711 * Check the proof of work and store the PeerID
712 * in the temporary list for pushed PeerIDs.
713 *
714 * @param cls Closure
715 * @param channel The channel the PUSH was received over
716 * @param channel_ctx The context associated with this channel
717 * @param msg The message header
718 */
719static int
720handle_peer_push (void *cls,
721 struct GNUNET_CADET_Channel *channel,
722 void **channel_ctx,
723 const struct GNUNET_MessageHeader *msg)
724{
725 LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
726
727 struct GNUNET_PeerIdentity *peer;
728
729 // TODO check the proof of work
730 // and check limit for PUSHes
731 // IF we count per peer PUSHes
732 // maybe remove from gossip/sampler list
733
734 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
735
736 /* Add the sending peer to the push_list */
737 GNUNET_CONTAINER_slist_add(push_list,
738 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
739 peer, sizeof(struct GNUNET_PeerIdentity));
740
741 return GNUNET_OK;
742}
743
744/**
745 * Handle PULL REQUEST request message from another peer.
746 *
747 * Reply with the gossip list of PeerIDs.
748 *
749 * @param cls Closure
750 * @param channel The channel the PUSH was received over
751 * @param channel_ctx The context associated with this channel
752 * @param msg The message header
753 */
754static int
755handle_peer_pull_request (void *cls,
756 struct GNUNET_CADET_Channel *channel,
757 void **channel_ctx,
758 const struct GNUNET_MessageHeader *msg)
759{
760
761 struct GNUNET_PeerIdentity *peer;
762 struct GNUNET_MQ_Handle *mq;
763 //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
764 struct GNUNET_MQ_Envelope *ev;
765 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
766
767 // TODO find some way to keep one peer from spamming with pull requests
768 // allow only one request per time interval ?
769 // otherwise remove from peerlist?
770
771 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
772 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
773
774 mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
775 //mq = get_mq(peer_map, peer);
776
777 //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
778 // TODO how many peers do we actually send?
779 // GNUNET_ntohll(in_msg->num_peers)
780 ev = GNUNET_MQ_msg_extra(out_msg,
781 gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
782 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
783 out_msg->num_peers = GNUNET_htonll(gossip_list_size);
784 memcpy(&out_msg[1], gossip_list,
785 gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
786
787 GNUNET_MQ_send(mq, ev);
788
789 GNUNET_MQ_destroy(mq);
790
791
792 return GNUNET_OK;
793}
794
795/**
796 * Handle PULL REPLY message from another peer.
797 *
798 * Check whether we sent a corresponding request and
799 * whether this reply is the first one.
800 *
801 * @param cls Closure
802 * @param channel The channel the PUSH was received over
803 * @param channel_ctx The context associated with this channel
804 * @param msg The message header
805 */
806static int
807handle_peer_pull_reply (void *cls,
808 struct GNUNET_CADET_Channel *channel,
809 void **channel_ctx,
810 const struct GNUNET_MessageHeader *msg)
811{
812 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
813
814 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
815 uint64_t i;
816
817 // TODO check that we sent a request and that it is the first reply
818
819 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
820 for ( i = 0 ; i < in_msg->num_peers ; i++ ) {
821 GNUNET_CONTAINER_slist_add(pull_list,
822 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
823 &in_msg[1] + i * sizeof(struct GNUNET_PeerIdentity),
824 sizeof(struct GNUNET_PeerIdentity));
825 }
826
827 // TODO maybe a disconnect happens here
828
829 return GNUNET_OK;
830}
831
832
833/**
834 * Callback called when a Sampler is updated.
835 */
836 void
837delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
838{
839 size_t s;
840
841 s = SAMPLER_count_id(sampler_list, id);
842 if ( 1 >= s ) {
843 // TODO cleanup peer
844 GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id);
845 }
846}
847
848
849/**
850 * Send out PUSHes and PULLs.
851 *
852 * This is executed regylary.
853 */
854static void
855do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
856{
857 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
858
859 uint64_t i;
860 struct Sampler *s;
861 struct GNUNET_CONTAINER_SList_Iterator *iter;
862 //unsigned int *n_arr;
863 struct GNUNET_RPS_P2P_PushMessage *push_msg;
864 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
865 struct GNUNET_MQ_Envelope *ev;
866 struct GNUNET_PeerIdentity *peer;
867
868 // TODO print lists, ...
869 // TODO cleanup peer_map
870
871 iter = GNUNET_new(struct GNUNET_CONTAINER_SList_Iterator);
872
873
874 /* If the NSE has changed adapt the lists accordingly */
875 // TODO check nse == 0!
876 LOG(GNUNET_ERROR_TYPE_DEBUG, "Checking size estimate.\n");
877 if ( sampler_list_size < est_size ) {
878 LOG(GNUNET_ERROR_TYPE_DEBUG, "Growing size.\n");
879 /* Grow the lists. */
880 for ( i = 0 ; i < est_size - sampler_list_size ; i++ ) {
881 s = SAMPLER_init();
882 GNUNET_CONTAINER_slist_add_end(sampler_list,
883 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, // DEPRECATED
884 s,
885 sizeof(struct Sampler));
886
887 // TODO add peers to gossiped ones?
888 }
889 } else if ( sampler_list_size > est_size ) {
890 LOG(GNUNET_ERROR_TYPE_DEBUG, "Shrinking size.\n");
891 /* Shrink the lists. */
892 for ( i = 0 ; i < sampler_list_size - est_size ; i++ ) {
893 *iter = GNUNET_CONTAINER_slist_begin(sampler_list);
894 GNUNET_CONTAINER_slist_erase(iter);
895 GNUNET_CONTAINER_slist_iter_destroy(iter); // Maybe unneeded but I don't know whether _erase() also deletes the iter
896 }
897 }
898
899 GNUNET_array_grow(gossip_list, gossip_list_size, est_size); // FIXME Do conversion correct or change type
900
901 gossip_list_size = sampler_list_size = est_size;
902
903
904
905
906 /* Would it make sense to have one shuffeled gossip list and then
907 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
908 * use the rest to update sampler? */
909
910 /* Send PUSHes */
911 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
912 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %" PRIu64 ") peers.\n",
913 alpha * gossip_list_size, alpha, gossip_list_size);
914 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length
915 peer = get_rand_gossip_peer();
916 // TODO check NULL == peer
917 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer));
918
919 ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
920 //ev = GNUNET_MQ_msg_extra();
921 /* TODO Compute proof of work here
922 push_msg; */
923 push_msg->placeholder = 0;
924 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
925
926 // TODO modify in_flags of respective peer?
927 }
928
929
930 /* Send PULL requests */
931 // TODO
932 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
933 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %" PRIu64 ") peers.\n",
934 beta * gossip_list_size, beta, gossip_list_size);
935 for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length
936 peer = get_rand_gossip_peer();
937 // TODO check NULL == peer
938 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer));
939
940 ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
941 //ev = GNUNET_MQ_msg_extra();
942 pull_msg->placeholder = 0;
943 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
944 // TODO modify in_flags of respective peer?
945 }
946
947
948
949
950 /* Update gossip list */
951 uint64_t tmp_index;
952
953 if ( GNUNET_CONTAINER_slist_count(push_list) <= alpha * gossip_list_size &&
954 GNUNET_CONTAINER_slist_count(push_list) != 0 &&
955 GNUNET_CONTAINER_slist_count(pull_list) != 0 ) {
956 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
957
958 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO use SAMPLER_get_n_rand_peers
959 /* Update gossip list with peers received through PUSHes */
960 gossip_list[i] = *SAMPLER_get_rand_peer(push_list);
961 // TODO change the in_flags accordingly
962 }
963
964 for ( i = 0 ; i < beta * gossip_list_size ; i++ ) {
965 /* Update gossip list with peers received through PULLs */
966 tmp_index = i + round(alpha * gossip_list_size);
967 gossip_list[tmp_index] = *SAMPLER_get_rand_peer(pull_list);
968 // TODO change the in_flags accordingly
969 }
970
971 for ( i = 0 ; i < (1 - (alpha + beta)) * gossip_list_size ; i++ ) {
972 /* Update gossip list with peers from history */
973 tmp_index = i + round((alpha + beta) * gossip_list_size);
974 gossip_list[tmp_index] = *SAMPLER_get_rand_peer(sampler_list);
975 // TODO change the in_flags accordingly
976 }
977
978 } else {
979 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
980 }
981 // TODO independent of that also get some peers from CADET_get_peers()?
982
983
984
985 /* Update samplers */
986 size_t size;
987
988 if ( 0 < GNUNET_CONTAINER_slist_count(push_list) ) {
989 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the sampler list from pushes.\n");
990
991 *iter = GNUNET_CONTAINER_slist_begin(push_list);
992 size = sizeof(struct GNUNET_PeerIdentity);
993
994 while ( GNUNET_NO != GNUNET_CONTAINER_slist_next(iter) ) {
995 peer = (struct GNUNET_PeerIdentity *) GNUNET_CONTAINER_slist_get(iter, &size);
996 SAMPLER_update_list(sampler_list, peer, NULL, NULL);
997 // TODO set in_flag
998 }
999 GNUNET_CONTAINER_slist_iter_destroy(iter);
1000
1001 } else {
1002 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the sampler list - received no pushes.\n");
1003 }
1004
1005 if ( 0 < GNUNET_CONTAINER_slist_count(pull_list) ) {
1006 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the sampler list - received no pushes.\n");
1007
1008 *iter = GNUNET_CONTAINER_slist_begin(pull_list);
1009
1010 while ( GNUNET_NO != GNUNET_CONTAINER_slist_next(iter) ) {
1011 peer = (struct GNUNET_PeerIdentity *) GNUNET_CONTAINER_slist_get(iter, &size);
1012 SAMPLER_update_list(sampler_list, peer, NULL, NULL);
1013 // TODO set in_flag
1014 }
1015 GNUNET_CONTAINER_slist_iter_destroy(iter);
1016 } else {
1017 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the sampler list - received no pulls.\n");
1018 }
1019
1020
1021 GNUNET_free(iter);
1022
1023
1024 // TODO go over whole peer_map and do cleanups
1025 // delete unneeded peers, set in_flags, check channel/mq
1026
1027
1028
1029 /* Empty push/pull lists */
1030 if ( 0 != GNUNET_CONTAINER_slist_count(push_list) ) {
1031 GNUNET_CONTAINER_slist_clear(push_list);
1032 }
1033
1034 if ( 0 != GNUNET_CONTAINER_slist_count(push_list) ) {
1035 GNUNET_CONTAINER_slist_clear(push_list);
1036 }
1037
1038
1039 /* Schedule next round */
1040 // TODO
1041 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1042 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1043}
1044
1045static void
1046rps_start (struct GNUNET_SERVER_Handle *server);
1047
1048/**
1049 * This is called from GNUNET_CADET_get_peers().
1050 *
1051 * It is called on every peer(ID) that cadet somehow has contact with.
1052 * We use those to initialise the sampler.
1053 */
1054void
1055init_peer_cb (void *cls,
1056 const struct GNUNET_PeerIdentity *peer,
1057 int tunnel, // "Do we have a tunnel towards this peer?"
1058 unsigned int n_paths, // "Number of known paths towards this peer"
1059 unsigned int best_path) // "How long is the best path?
1060 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1061{
1062 // FIXME use the magic 0000 PeerID
1063 if ( NULL != peer ) {
1064 LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s from CADET\n", GNUNET_i2s(peer));
1065 SAMPLER_update_list(sampler_list, peer, NULL, NULL);
1066 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) ) {
1067 } else {
1068 struct peer_context *ctx;
1069
1070 ctx = GNUNET_malloc(sizeof(struct peer_context));
1071 ctx->in_flags = 0;
1072 ctx->mq = NULL;
1073 ctx->to_channel = NULL;
1074 ctx->from_channel = NULL;
1075 GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1076 }
1077
1078 uint64_t i;
1079 i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1080 gossip_list[i] = *peer;
1081 // TODO send push/pull to each of those peers?
1082 } else {
1083 rps_start( (struct GNUNET_SERVER_Handle *) cls);
1084 }
1085}
1086
1087
1088
1089
1090/**
1091 * Task run during shutdown.
1092 *
1093 * @param cls unused
1094 * @param tc unused
1095 */
1096static void
1097shutdown_task (void *cls,
1098 const struct GNUNET_SCHEDULER_TaskContext *tc)
1099{
1100 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1101
1102 GNUNET_NSE_disconnect(nse);
1103 GNUNET_CADET_disconnect(cadet_handle);
1104 // TODO delete global data
1105}
1106
1107
1108/**
1109 * A client disconnected. Remove all of its data structure entries.
1110 *
1111 * @param cls closure, NULL
1112 * @param client identification of the client
1113 */
1114static void
1115handle_client_disconnect (void *cls,
1116 struct GNUNET_SERVER_Client * client)
1117{
1118 // TODO reinitialise that sampler
1119}
1120
1121/**
1122 * Handle the channel a peer opens to us.
1123 *
1124 * @param cls The closure
1125 * @param channel The channel the peer wants to establish
1126 * @param initiator The peer's peer ID
1127 * @param port The port the channel is being established over
1128 * @param options Further options
1129 */
1130 static void *
1131handle_inbound_channel (void *cls,
1132 struct GNUNET_CADET_Channel *channel,
1133 const struct GNUNET_PeerIdentity *initiator,
1134 uint32_t port,
1135 enum GNUNET_CADET_ChannelOption options)
1136{
1137 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1138
1139 GNUNET_assert( NULL != channel );
1140
1141 // TODO we might even not store the from_channel
1142
1143 if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1144 ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1145 // FIXME there might already be an established channel
1146 } else {
1147 struct peer_context *ctx;
1148
1149 ctx = GNUNET_malloc( sizeof(struct peer_context));
1150 ctx->in_flags = in_other_gossip_list;
1151 ctx->mq = NULL; // TODO create mq?
1152 ctx->from_channel = channel;
1153
1154 GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1155 }
1156 return NULL; // TODO
1157}
1158
1159/**
1160 * This is called when a remote peer destroys a channel.
1161 *
1162 * @param cls The closure
1163 * @param channel The channel being closed
1164 * @param channel_ctx The context associated with this channel
1165 */
1166static void
1167cleanup_channel(void *cls,
1168 const struct GNUNET_CADET_Channel *channel,
1169 void *channel_ctx)
1170{
1171 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1172}
1173
1174/**
1175 * Actually start the service.
1176 */
1177static void
1178rps_start (struct GNUNET_SERVER_Handle *server)
1179{
1180 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1181 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1182 {NULL, NULL, 0, 0}
1183 };
1184
1185 GNUNET_SERVER_add_handlers (server, handlers);
1186 GNUNET_SERVER_disconnect_notify (server,
1187 &handle_client_disconnect,
1188 NULL);
1189 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1190
1191
1192
1193 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1194 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1195
1196 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1197 &shutdown_task,
1198 NULL);
1199}
1200
1201
1202
1203/**
1204 * Process statistics requests.
1205 *
1206 * @param cls closure
1207 * @param server the initialized server
1208 * @param c configuration to use
1209 */
1210static void
1211run (void *cls,
1212 struct GNUNET_SERVER_Handle *server,
1213 const struct GNUNET_CONFIGURATION_Handle *c)
1214{
1215 // TODO check what this does -- copied from gnunet-boss
1216 // - seems to work as expected
1217 GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1218
1219 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1220
1221 uint32_t i;
1222
1223 cfg = c;
1224
1225
1226 own_identity = GNUNET_new(struct GNUNET_PeerIdentity);
1227
1228 GNUNET_CRYPTO_get_peer_identity(cfg, own_identity);
1229
1230
1231
1232 /* Get time interval from the configuration */
1233 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1234 "ROUNDINTERVAL",
1235 &round_interval))
1236 {
1237 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1238 GNUNET_SCHEDULER_shutdown();
1239 return;
1240 }
1241
1242 /* Get initial size of sampler/gossip list from the configuration */
1243 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1244 "INITSIZE",
1245 (long long unsigned int *) &est_size)) // FIXME convert
1246 {
1247 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1248 GNUNET_SCHEDULER_shutdown();
1249 return;
1250 }
1251 LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1252
1253 gossip_list_size = sampler_list_size = est_size; // TODO rename est_size
1254
1255
1256 gossip_list = NULL;
1257
1258 static unsigned int tmp = 0;
1259
1260 GNUNET_array_grow(gossip_list, tmp, gossip_list_size);
1261
1262
1263
1264 /* connect to NSE */
1265 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1266 // TODO check whether that was successful
1267 // TODO disconnect on shutdown
1268 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1269
1270
1271 alpha = 0.45;
1272 beta = 0.45;
1273 // TODO initialise thresholds - ?
1274
1275 ///* Get alpha from the configuration */
1276 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1277 // "ALPHA",
1278 // &alpha))
1279 //{
1280 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1281 //}
1282 //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1283
1284 ///* Get beta from the configuration */
1285 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1286 // "BETA",
1287 // &beta))
1288 //{
1289 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1290 //}
1291 //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1292
1293
1294
1295
1296 peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1297
1298
1299 /* Initialise sampler and gossip list */
1300 struct Sampler *s;
1301
1302 sampler_list = GNUNET_CONTAINER_slist_create();
1303
1304 //if ( gossip_list_size == sampler_list_size ) {
1305 for ( i = 0 ; i < sampler_list_size ; i++ ) {
1306 /* Init sampler list */
1307 s = SAMPLER_init();
1308 GNUNET_CONTAINER_slist_add(sampler_list,
1309 GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC, // TODO DEPRECATED
1310 s,
1311 sizeof(struct Sampler));
1312 /* Init gossip list */
1313 // TODO init gossip list
1314 // What do we need to do here?
1315 }
1316 //} else {
1317 // for ( i = 0 ; i < gossip_list_size ; i++ ) {
1318 // // TODO init gossip list
1319 // }
1320 // for ( i = 0 ; i < sampler_list_size ; i++ ) {
1321 // // TODO init RPF func
1322 // // TODO init Sample list
1323 // // TODO init Sampled list
1324 // }
1325 //}
1326 uint64_t tmp_s = (uint64_t) GNUNET_CONTAINER_slist_count(sampler_list);
1327 LOG(GNUNET_ERROR_TYPE_DEBUG, "Initialised sampler list %" PRIu64 "\n", tmp_s);
1328
1329
1330
1331 push_list = GNUNET_CONTAINER_slist_create();
1332 pull_list = GNUNET_CONTAINER_slist_create();
1333
1334
1335
1336 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1337 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1338 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1339 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1340 {NULL, 0, 0}
1341 };
1342
1343 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1344 cadet_handle = GNUNET_CADET_connect(cfg,
1345 cls,
1346 &handle_inbound_channel,
1347 &cleanup_channel,
1348 cadet_handlers,
1349 ports);
1350 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1351
1352
1353 LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1354 GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1355 // FIXME use magic 0000 PeerID to _start_ the service
1356
1357 // TODO send push/pull to each of those peers?
1358
1359
1360
1361}
1362
1363
1364/**
1365 * The main function for the rps service.
1366 *
1367 * @param argc number of arguments from the command line
1368 * @param argv command line arguments
1369 * @return 0 ok, 1 on error
1370 */
1371int
1372main (int argc, char *const *argv)
1373{
1374 return (GNUNET_OK ==
1375 GNUNET_SERVICE_run (argc,
1376 argv,
1377 "rps",
1378 GNUNET_SERVICE_OPTION_NONE,
1379 &run, NULL)) ? 0 : 1;
1380}
1381
1382/* end of gnunet-service-rps.c */
diff --git a/src/rps/rps.conf.in b/src/rps/rps.conf.in
new file mode 100644
index 000000000..bd64791be
--- /dev/null
+++ b/src/rps/rps.conf.in
@@ -0,0 +1,7 @@
1[rps]
2BINARY = gnunet-service-rps
3UNIXPATH = /tmp/gnunet-service-rps.sock
4HOME = $SERVICEHOME
5# PORT = 2106
6@UNIXONLY@ PORT = 2087
7
diff --git a/src/rps/rps.h b/src/rps/rps.h
new file mode 100644
index 000000000..46c8ab0bb
--- /dev/null
+++ b/src/rps/rps.h
@@ -0,0 +1,139 @@
1/*
2 This file is part of GNUnet
3 (C) 2012-2013 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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 push own ID to other peer.
42 */
43struct GNUNET_RPS_P2P_PushMessage
44{
45 /**
46 * Header including size and type in NBO
47 */
48 struct GNUNET_MessageHeader header;
49
50 /**
51 * TODO Proof of work
52 */
53 uint64_t placeholder;
54};
55
56/**
57 * P2P Message to request PeerIDs from other peer.
58 */
59struct GNUNET_RPS_P2P_PullRequestMessage
60{
61 /**
62 * Header including size and type in NBO
63 */
64 struct GNUNET_MessageHeader header;
65
66 /* This probably stays empty as we just
67 * infrom the peer of our existence */
68 uint64_t placeholder;
69};
70
71/**
72 * P2P Message to send PeerIDs to other peer.
73 */
74struct GNUNET_RPS_P2P_PullReplyMessage
75{
76 /**
77 * Header including size and type in NBO
78 */
79 struct GNUNET_MessageHeader header;
80
81 /**
82 * Number of PeerIDs sent
83 */
84 uint64_t num_peers GNUNET_PACKED;
85
86 /* Followed by num_peers * GNUNET_PeerIdentity */
87};
88
89
90
91/***********************************************************************
92 * Client-Service Messages
93***********************************************************************/
94
95/**
96 * Message from client to RPS service to request random peer(s).
97 */
98struct GNUNET_RPS_CS_RequestMessage
99{
100 /**
101 * Header including size and type in NBO
102 */
103 struct GNUNET_MessageHeader header;
104
105 /**
106 * Identifyer of the message.
107 */
108 uint64_t n;
109
110 /**
111 * Number of random peer requested
112 */
113 uint64_t num_peers GNUNET_PACKED;
114};
115
116/**
117 * Message from RPS service to client to reply with random peer(s).
118 */
119struct GNUNET_RPS_CS_ReplyMessage
120{
121 /**
122 * Header including size and type in NBO
123 */
124 struct GNUNET_MessageHeader header;
125
126 /**
127 * Identifyer of the message.
128 */
129 uint64_t n;
130
131 /**
132 * Number of random peer replied
133 */
134 uint64_t num_peers GNUNET_PACKED;
135
136 /* Followed by num_peers * GNUNET_PeerIdentity */
137};
138
139GNUNET_NETWORK_STRUCT_END
diff --git a/src/rps/rps_api.c b/src/rps/rps_api.c
new file mode 100644
index 000000000..ba8a594b7
--- /dev/null
+++ b/src/rps/rps_api.c
@@ -0,0 +1,279 @@
1/*
2 This file is part of GNUnet.
3 (C)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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
31/**
32 * Handler to handle requests from a client.
33 */
34struct GNUNET_RPS_Handle
35{
36 /**
37 * The handle to the client configuration.
38 */
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 /**
42 * The connection to the client.
43 */
44 struct GNUNET_CLIENT_Connection *conn;
45
46 /**
47 * The message queue to the client.
48 */
49 struct GNUNET_MQ_Handle *mq;
50};
51
52/**
53 * Handler to single requests from the client.
54 */
55struct GNUNET_RPS_Request_Handle
56{
57 /**
58 * The client issuing the request.
59 */
60 struct GNUNET_RPS_Handle *h;
61
62 /**
63 * The nuber of the request.
64 */
65 uint64_t n;
66
67 /**
68 * The callback to be called when we receive an answer.
69 */
70 GNUNET_RPS_NotifyReadyCB ready_cb;
71
72 /**
73 * The closure for the callback.
74 */
75 void *ready_cb_cls;
76};
77
78/**
79 * Array of Request_Handles.
80 */
81struct GNUNET_RPS_Request_Handle *req_handlers = NULL;
82
83/**
84 * Current length of req_handlers.
85 */
86unsigned int req_handlers_size = 0;
87
88/**
89 * Struct used to pack the callback, its closure (provided by the caller)
90 * and the connection handler to the service to pass it to a callback function.
91 */
92struct cb_cls_pack
93{
94 /**
95 * Callback provided by the client
96 */
97 GNUNET_RPS_NotifyReadyCB cb;
98
99 /**
100 * Closure provided by the client
101 */
102 void *cls;
103
104 /**
105 * Handle to the service connection
106 */
107 struct GNUNET_CLIENT_Connection *service_conn;
108};
109
110
111
112
113/**
114 * This function is called, when the service replies to our request.
115 * It calls the callback the caller gave us with the provided closure
116 * and disconnects afterwards.
117 *
118 * @param cls the closure
119 * @param message the message
120 */
121 static void
122handle_reply (void *cls,
123 const struct GNUNET_MessageHeader *message)
124{
125 struct GNUNET_RPS_CS_ReplyMessage *msg;
126 //struct cb_cls_pack *pack;
127 //struct GNUNET_RPS_Handle *h;
128 struct GNUNET_PeerIdentity *peers;
129 struct GNUNET_RPS_Request_Handle *rh;
130
131 /* Give the peers back */
132 msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
133 //pack = (struct cb_cls_pack *) cls;
134 //h = (struct GNUNET_RPS_Handle *) cls;
135 peers = (struct GNUNET_PeerIdentity *) &msg[1];
136 rh = &req_handlers[msg->n];
137 rh->ready_cb((rh)->ready_cb_cls, msg->num_peers, peers);
138
139 /* Disconnect */
140 //GNUNET_CLIENT_disconnect(pack->service_conn);
141}
142
143/**
144 */
145 static void
146mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
147{
148 //TODO LOG
149}
150
151/**
152 * Request n random peers.
153 *
154 * @param cfg the configuration to use.
155 * @param n number of peers requesting.
156 * @param cb a callback function called when the peers are ready
157 * @param cls a closure given to the callback function
158 */
159 struct GNUNET_RPS_Request_Handle *
160GNUNET_RPS_request_peers_single_call (const struct GNUNET_CONFIGURATION_Handle *cfg,
161 uint64_t n,
162 GNUNET_RPS_NotifyReadyCB ready_cb,
163 void *cls)
164{
165 //struct GNUNET_CLIENT_Connection *service_conn;
166 //static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
167 // {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
168 // GNUNET_MQ_HANDLERS_END
169 //};
170 //struct cb_cls_pack *pack;
171 //struct GNUNET_MQ_Handle *mq;
172 //struct GNUNET_MQ_Envelope *ev;
173 //struct GNUNET_RPS_CS_RequestMessage *msg;
174 struct GNUNET_RPS_Handle *h;
175 struct GNUNET_RPS_Request_Handle *rh;
176
177 /* Connect to the service */
178 h = GNUNET_RPS_connect(cfg);
179 //h = GNUNET_new(struct GNUNET_RPS_Handle);
180 //h->conn = GNUNET_CLIENT_connect("rps", cfg);
181 //rh = GNUNET_new(struct GNUNET_RPS_Request_Handle);
182 ////pack = GNUNET_malloc(sizeof(struct cb_cls_pack));
183 ////pack->cb = ready_cb;
184 ////pack->cls = cls;
185 ////pack->service_conn = service_conn;
186 //mq = GNUNET_MQ_queue_for_connection_client(service_conn,
187 // mq_handlers,
188 // mq_error_handler, // TODO implement
189 // h);
190
191 /* Send the request to the service */
192 rh = GNUNET_RPS_request_peers(h, n, ready_cb, cls);
193 //ev = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
194 //msg->num_peers = GNUNET_htonll(n);
195 //GNUNET_MQ_send(mq, ev);
196 //GNUNET_CLIENT_disconnect(service_conn);
197 //rh = GNUNET_new(struct GNUNET_RPS_Request_Handle);
198 GNUNET_RPS_disconnect(h);
199 return rh;
200}
201
202/**
203 * Connect to the rps service
204 */
205 struct GNUNET_RPS_Handle *
206GNUNET_RPS_connect( const struct GNUNET_CONFIGURATION_Handle *cfg )
207{
208 struct GNUNET_RPS_Handle *h;
209 //struct GNUNET_RPS_Request_Handle *rh;
210 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
211 {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
212 GNUNET_MQ_HANDLERS_END
213 };
214
215 h = GNUNET_new(struct GNUNET_RPS_Handle);
216 //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
217 //*h->cfg = *cfg;
218 h->cfg = cfg; // FIXME |^
219 h->conn = GNUNET_CLIENT_connect("rps", cfg);
220 h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
221 mq_handlers,
222 mq_error_handler, // TODO implement
223 h);
224
225
226 return h;
227}
228
229/**
230 * Request n random peers.
231 */
232 struct GNUNET_RPS_Request_Handle *
233GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint64_t n,
234 GNUNET_RPS_NotifyReadyCB ready_cb,
235 void *cls)
236{
237 struct GNUNET_RPS_Request_Handle *rh;
238 struct GNUNET_MQ_Envelope *ev;
239 struct GNUNET_RPS_CS_RequestMessage *msg;
240
241 // assert func != NULL
242 rh = GNUNET_new(struct GNUNET_RPS_Request_Handle);
243 rh->h = h;
244 rh->n = req_handlers_size; // TODO ntoh
245 rh->ready_cb = ready_cb;
246 rh->ready_cb_cls = cls;
247
248 GNUNET_array_append(req_handlers, req_handlers_size, *rh);
249 //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
250
251 ev = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
252 msg->num_peers = GNUNET_htonll(n);
253 msg->n = rh->n;
254 GNUNET_MQ_send(h->mq, ev);
255 return rh;
256}
257
258/**
259 * Cancle an issued request.
260 */
261 void
262GNUNET_RPS_request_cancel ( struct GNUNET_RPS_Request_Handle *rh )
263{
264 // TODO
265}
266
267/**
268 * Disconnect to the rps service
269 */
270 void
271GNUNET_RPS_disconnect ( struct GNUNET_RPS_Handle *h )
272{
273 if ( NULL != h->conn ) {
274 GNUNET_CLIENT_disconnect(h->conn);
275 }
276}
277
278
279/* end of rps_api.c */
diff --git a/src/rps/test_rps.conf b/src/rps/test_rps.conf
new file mode 100644
index 000000000..f27bf0717
--- /dev/null
+++ b/src/rps/test_rps.conf
@@ -0,0 +1,39 @@
1[rps]
2AUTOSTART = YES
3PREFIX = valgrind --log-file=/tmp/rps/valgrind!gnunet-service-rps!%p
4BINARY = gnunet-service-rps
5UNIXPATH = /tmp/gnunet-service-rps.sock
6HOME = $SERVICEHOME
7# PORT = 2106
8@UNIXONLY@ PORT = 2087
9
10# This is the timeinterval between the rounds
11ROUNDINTERVAL = 10 s
12
13# This is the 'estimate' in the beginning.
14# This determines the size of the peers we keep in memory
15# until we receive the first estimate from NSE.
16# Keep in mind, that (networksize)^(1/3) should be enough.
17# So, 50 is enough for a network of size 50^3 = 125000
18INITSIZE = 4
19
20ALPHA = 0.45
21
22
23[arm]
24DEFAULTSERVICES = core ats transport cadet nse rps
25
26[testbed]
27OPERATION_TIMEOUT = 60 s
28
29MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS = 1
30OVERLAY_TOPOLOGY = CLIQUE
31#SCALE_FREE_TOPOLOGY_CAP =
32
33OVERLAY_RANDOM_LINKS = 5
34
35SETUP_TIMEOUT = 2 m
36
37
38[nse]
39WORKBITS = 0 \ No newline at end of file
diff --git a/src/rps/test_rps_api.c b/src/rps/test_rps_api.c
new file mode 100644
index 000000000..1772594ac
--- /dev/null
+++ b/src/rps/test_rps_api.c
@@ -0,0 +1,84 @@
1/*
2 This file is part of GNUnet.
3 (C)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file rps/test_rps_api.c
22 * @brief testcase for rps_api.c
23 */
24#include <gnunet/platform.h>
25#include <gnunet/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 if (NULL == path)
52 {
53 fprintf (stderr, "Service executable not found `%s'\n", "gnunet-service-rps");
54 return;
55 }
56
57 proc = GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
58 NULL, NULL, path, "gnunet-service-rps", NULL);
59
60 GNUNET_free (path);
61 GNUNET_assert (NULL != proc);
62 GNUNET_PROGRAM_run (1, argv, "test-rps-api", "nohelp",
63 options, &run, &ok);
64 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
65 {
66 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
67 ok = 1;
68 }
69 GNUNET_OS_process_wait (proc);
70 GNUNET_OS_process_destroy (proc);
71 return ok;
72}
73
74
75int
76main (int argc, char *argv[])
77{
78 GNUNET_log_setup ("test_statistics_api",
79 "WARNING",
80 NULL);
81 return check ();
82}
83
84/* end of test_rps_api.c */
diff --git a/src/rps/test_rps_multipeer.c b/src/rps/test_rps_multipeer.c
new file mode 100644
index 000000000..5c90e393d
--- /dev/null
+++ b/src/rps/test_rps_multipeer.c
@@ -0,0 +1,244 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file rps/test_rps_multipeer.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_testbed_service.h"
29#include "gnunet_rps_service.h"
30#include <time.h>
31
32
33/**
34 * How many peers do we start?
35 */
36#define NUM_PEERS 10
37
38/**
39 * How long do we run the test?
40 */
41#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
42
43
44/**
45 * Information we track for each peer.
46 */
47struct RPSPeer
48{
49 /**
50 * Handle for RPS connect operation.
51 */
52 struct GNUNET_TESTBED_Operation *op;
53
54 /**
55 * Handle to RPS service.
56 */
57 struct GNUNET_RPS_Handle *rps_handle;
58};
59
60
61/**
62 * Information for all the peers.
63 */
64static struct RPSPeer rps_peers[NUM_PEERS];
65
66/**
67 * Return value from 'main'.
68 */
69static int ok;
70
71
72/**
73 * Task run on timeout to shut everything down.
74 */
75static void
76shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77{
78 unsigned int i;
79
80 for (i=0;i<NUM_PEERS;i++)
81 GNUNET_TESTBED_operation_done (rps_peers[i].op);
82 GNUNET_SCHEDULER_shutdown ();
83}
84
85
86/**
87 * Callback to call when network size estimate is updated.
88 *
89 * @param cls closure
90 * @param timestamp server timestamp
91 * @param estimate the value of the current network size estimate
92 * @param std_dev standard deviation (rounded down to nearest integer)
93 * of the size estimation values seen
94 *
95 */
96static void
97handle_reply (void *cls, uint64_t n, struct GNUNET_PeerIdentity *peers)
98{
99 //struct RPSPeer *peer = cls;
100
101 //FPRINTF (stderr,
102 // "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
103 // (unsigned int) (peer - rps_peers),
104 // estimate, std_dev,
105 // GNUNET_NSE_log_estimate_to_n (estimate),
106 // NUM_PEERS);
107
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got peer %s\n", GNUNET_i2s(peers));
109
110 ok = 0;
111}
112
113
114/**
115 * Callback to be called when RPS service connect operation is completed
116 *
117 * @param cls the callback closure from functions generating an operation
118 * @param op the operation that has been finished
119 * @param ca_result the RPS service handle returned from rps_connect_adapter
120 * @param emsg error message in case the operation has failed; will be NULL if
121 * operation has executed successfully.
122 */
123static void
124rps_connect_complete_cb (void *cls,
125 struct GNUNET_TESTBED_Operation *op,
126 void *ca_result,
127 const char *emsg)
128{
129 struct RPSPeer *peer = cls;
130 struct GNUNET_RPS_Handle *rps = ca_result;
131
132 GNUNET_assert (op == peer->op);
133 if (NULL != emsg)
134 {
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136 "Failed to connect to RPS service: %s\n",
137 emsg);
138 ok = 1;
139 GNUNET_SCHEDULER_shutdown ();
140 return;
141 }
142 peer->rps_handle = rps;
143 //sleep(50);
144 GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
145 GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
146 //sleep(10000);
147 //GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
148 //sleep(10000);
149 //GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
150 //sleep(10000);
151 //GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
152}
153
154
155/**
156 * Adapter function called to establish a connection to
157 * the RPS service.
158 *
159 * @param cls closure
160 * @param cfg configuration of the peer to connect to; will be available until
161 * GNUNET_TESTBED_operation_done() is called on the operation returned
162 * from GNUNET_TESTBED_service_connect()
163 * @return service handle to return in 'op_result', NULL on error
164 */
165static void *
166rps_connect_adapter (void *cls,
167 const struct GNUNET_CONFIGURATION_Handle *cfg)
168{
169 return GNUNET_RPS_connect (cfg);
170}
171
172
173/**
174 * Adapter function called to destroy connection to
175 * RPS service.
176 *
177 * @param cls closure
178 * @param op_result service handle returned from the connect adapter
179 */
180static void
181rps_disconnect_adapter (void *cls,
182 void *op_result)
183{
184 struct GNUNET_RPS_Handle *h = op_result;
185 GNUNET_RPS_disconnect (h);
186}
187
188
189/**
190 * Actual "main" function for the testcase.
191 *
192 * @param cls closure
193 * @param h the run handle
194 * @param num_peers number of peers in 'peers'
195 * @param peers handle to peers run in the testbed
196 * @param links_succeeded the number of overlay link connection attempts that
197 * succeeded
198 * @param links_failed the number of overlay link connection attempts that
199 * failed
200 */
201static void
202run (void *cls,
203 struct GNUNET_TESTBED_RunHandle *h,
204 unsigned int num_peers,
205 struct GNUNET_TESTBED_Peer **peers,
206 unsigned int links_succeeded,
207 unsigned int links_failed)
208{
209 unsigned int i;
210
211 GNUNET_assert (NUM_PEERS == num_peers);
212 for (i=0;i<num_peers;i++)
213 rps_peers[i].op = GNUNET_TESTBED_service_connect (&rps_peers[i],
214 peers[i],
215 "rps",
216 &rps_connect_complete_cb,
217 &rps_peers[i],
218 &rps_connect_adapter,
219 &rps_disconnect_adapter,
220 &rps_peers[i]);
221 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
222}
223
224
225/**
226 * Entry point for the testcase, sets up the testbed.
227 *
228 * @param argc unused
229 * @param argv unused
230 * @return 0 on success
231 */
232int
233main (int argc, char *argv[])
234{
235 ok = 1;
236 (void) GNUNET_TESTBED_test_run ("test-rps-multipeer",
237 "test_rps.conf",
238 NUM_PEERS,
239 0, NULL, NULL,
240 &run, NULL);
241 return ok;
242}
243
244/* end of test_rps_multipeer.c */