eda012076d190d45a99209da864c31b27309119c
[oweals/gnunet.git] / src / include / gnunet_rps_service.h
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
19 /**
20  * @author Julius Bünger
21  *
22  * @file
23  * API to the rps service
24  *
25  * @defgroup rps  RPS service
26  * Random Peer Sampling
27  * @{
28  */
29 #ifndef GNUNET_RPS_SERVICE_H
30 #define GNUNET_RPS_SERVICE_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 /**
41  * Version of the rps API.
42  */
43 #define GNUNET_RPS_VERSION 0x00000000
44
45 /**
46  * Handle for the random peer sampling service
47  */
48 struct GNUNET_RPS_Handle;
49
50 /**
51  * Handle for one request to the rps service
52  */
53 struct GNUNET_RPS_Request_Handle;
54
55 /**
56  * Callback called when requested random peers are available.
57  *
58  * @param cls the closure given with the request
59  * @param num_peers the number of peers returned
60  * @param peers array with num_peers PeerIDs
61  */
62 typedef void (* GNUNET_RPS_NotifyReadyCB) (void *cls,
63     uint64_t num_peers,
64     const struct GNUNET_PeerIdentity *peers);
65
66 /**
67  * Callback called when view was updated
68  *
69  * @param num_peers the number of peers returned
70  * @param peers array with num_peers PeerIDs
71  */
72 typedef void (* GNUNET_RPS_ViewUpdateCB) (void *cls,
73     uint64_t num_peers,
74     const struct GNUNET_PeerIdentity *peers);
75
76 /**
77  * Callback called when a peer from the biased stream was received
78  *
79  * @param peer The received peer
80  */
81 typedef void (* GNUNET_RPS_StreamInputCB) (void *cls,
82     uint64_t num_peers,
83     const struct GNUNET_PeerIdentity *peer);
84
85 /**
86  * Connect to the rps service
87  *
88  * @param cfg configuration to use
89  * @return handle to the rps service
90  */
91   struct GNUNET_RPS_Handle *
92 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
93
94 /**
95  * Request n random peers.
96  *
97  * This does exacly the same as GNUNET_RPS_request_peers_single_call
98  * but needs a GNUNET_RPS_Handle.
99  * This exists only for other parts of GNUnet that expect having to
100  * (dis)connect from/to a service.
101  *
102  * @param h handle to the rps service
103  * @param n number of random peers to return
104  * @param ready_cb the callback to be called when the peers are available
105  * @param cls a closure that will be given to the callback
106  * @return handle to this request
107  */
108   struct GNUNET_RPS_Request_Handle *
109 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
110                           GNUNET_RPS_NotifyReadyCB ready_cb,
111                           void *cls);
112
113 /**
114  * Seed rps service with peerIDs.
115  *
116  * @param h handle to the rps service
117  * @param n number of peers to seed
118  * @param ids the ids of the peers seeded
119  */
120   void
121 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint32_t n,
122                      const struct GNUNET_PeerIdentity * ids);
123
124 /**
125  * Cancle an issued request.
126  *
127  * @param rh handle of the pending request to be canceled
128  */
129   void
130 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh);
131
132
133 #ifdef ENABLE_MALICIOUS
134 /**
135  * Turn RPS service to act malicious.
136  *
137  * @param h handle to the rps service
138  * @param type which type of malicious peer to turn to.
139  *             0 Don't act malicious at all
140  *             1 Try to maximise representation
141  *             2 Try to partition the network
142  *               (isolate one peer from the rest)
143  * @param n number of @a ids
144  * @param ids the ids of the malicious peers
145  *            if @type is 2 the last id is the id of the
146  *            peer to be isolated from the rest
147  */
148   void
149 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
150                           uint32_t type,
151                           uint32_t num_peers,
152                           const struct GNUNET_PeerIdentity *ids,
153                           const struct GNUNET_PeerIdentity *target_peer);
154 #endif /* ENABLE_MALICIOUS */
155
156 /* Get internals for debugging/profiling purposes */
157
158 /**
159  * Request updates of view
160  *
161  * @param rps_handle handle to the rps service
162  * @param num_req_peers number of peers we want to receive
163  *        (0 for infinite updates)
164  * @param cls a closure that will be given to the callback
165  * @param ready_cb the callback called when the peers are available
166  */
167 void
168 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
169                          uint32_t num_updates,
170                          GNUNET_RPS_ViewUpdateCB view_update_cb,
171                          void *cls);
172
173
174 /**
175  * Request biased stream of peers that are being put into the sampler
176  *
177  * @param rps_handle handle to the rps service
178  * @param num_req_peers number of peers we want to receive
179  *        (0 for infinite updates)
180  * @param cls a closure that will be given to the callback
181  * @param ready_cb the callback called when the peers are available
182  */
183 void
184 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
185                            uint32_t num_updates,
186                            GNUNET_RPS_StreamInputCB stream_input_cb,
187                            void *cls);
188
189
190 /**
191  * Disconnect from the rps service
192  *
193  * @param h the handle to the rps service
194  */
195   void
196 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h);
197
198
199 #if 0                           /* keep Emacsens' auto-indent happy */
200 {
201 #endif
202 #ifdef __cplusplus
203 }
204 #endif
205
206 #endif
207
208 /** @} */  /* end of group */