error handling
[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      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Julius Bünger
23  *
24  * @file
25  * API to the rps service
26  *
27  * @defgroup rps  RPS service
28  * Random Peer Sampling
29  * @{
30  */
31 #ifndef GNUNET_RPS_SERVICE_H
32 #define GNUNET_RPS_SERVICE_H
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42 /**
43  * Version of the rps API.
44  */
45 #define GNUNET_RPS_VERSION 0x00000000
46
47 /**
48  * Handle for the random peer sampling service
49  */
50 struct GNUNET_RPS_Handle;
51
52 /**
53  * Handle for one request to the rps service
54  */
55 struct GNUNET_RPS_Request_Handle;
56
57 /**
58  * Callback called when requested random peers are available.
59  *
60  * @param cls the closure given with the request
61  * @param num_peers the number of peers returned
62  * @param peers array with num_peers PeerIDs
63  */
64 typedef void (*GNUNET_RPS_NotifyReadyCB) (void *cls,
65                                           uint64_t num_peers,
66                                           const struct
67                                           GNUNET_PeerIdentity *peers);
68
69
70 /**
71  * Callback called when requested random peer with additional information is
72  * available.
73  *
74  * @param cls the closure given with the request
75  * @param peer The Peer ID
76  * @param probability The probability with which all elements have been observed
77  * @param num_observed Number of IDs this sampler has observed
78  */
79 typedef void (*GNUNET_RPS_NotifyReadySingleInfoCB) (void *cls,
80                                                     const struct
81                                                     GNUNET_PeerIdentity *peer,
82                                                     double probability,
83                                                     uint32_t num_observed);
84
85
86 /**
87  * Connect to the rps service
88  *
89  * @param cfg configuration to use
90  * @return handle to the rps service
91  */
92 struct GNUNET_RPS_Handle *
93 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
94
95
96 /**
97  * @brief Start a sub with the given shared value
98  *
99  * @param h Handle to rps
100  * @param shared_value The shared value that defines the members of the sub (-group)
101  */
102 void
103 GNUNET_RPS_sub_start (struct GNUNET_RPS_Handle *h,
104                       const char *shared_value);
105
106
107 /**
108  * @brief Stop a sub with the given shared value
109  *
110  * @param h Handle to rps
111  * @param shared_value The shared value that defines the members of the sub (-group)
112  */
113 void
114 GNUNET_RPS_sub_stop (struct GNUNET_RPS_Handle *h,
115                      const char *shared_value);
116
117
118 /**
119  * Request n random peers.
120  *
121  * This does exacly the same as GNUNET_RPS_request_peers_single_call
122  * but needs a GNUNET_RPS_Handle.
123  * This exists only for other parts of GNUnet that expect having to
124  * (dis)connect from/to a service.
125  *
126  * @param h handle to the rps service
127  * @param n number of random peers to return
128  * @param ready_cb the callback to be called when the peers are available
129  * @param cls a closure that will be given to the callback
130  * @return handle to this request
131  */
132 struct GNUNET_RPS_Request_Handle *
133 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
134                           GNUNET_RPS_NotifyReadyCB ready_cb,
135                           void *cls);
136
137
138 /**
139  * Request one random peer, getting additional information.
140  *
141  * @param rps_handle handle to the rps service
142  * @param ready_cb the callback called when the peers are available
143  * @param cls closure given to the callback
144  * @return a handle to cancel this request
145  */
146 struct GNUNET_RPS_Request_Handle_Single_Info *
147 GNUNET_RPS_request_peer_info (struct GNUNET_RPS_Handle *rps_handle,
148                               GNUNET_RPS_NotifyReadySingleInfoCB ready_cb,
149                               void *cls);
150
151
152 /**
153  * Seed rps service with peerIDs.
154  *
155  * @param h handle to the rps service
156  * @param n number of peers to seed
157  * @param ids the ids of the peers seeded
158  */
159 void
160 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint32_t n,
161                      const struct GNUNET_PeerIdentity *ids);
162
163 /**
164  * Cancle an issued request.
165  *
166  * @param rh handle of the pending request to be canceled
167  */
168 void
169 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh);
170
171
172 /**
173  * Cancle an issued single info request.
174  *
175  * @param rhs request handle of request to cancle
176  */
177 void
178 GNUNET_RPS_request_single_info_cancel (
179   struct GNUNET_RPS_Request_Handle_Single_Info *rhs);
180
181
182 #if ENABLE_MALICIOUS
183 /**
184  * Turn RPS service to act malicious.
185  *
186  * @param h handle to the rps service
187  * @param type which type of malicious peer to turn to.
188  *             0 Don't act malicious at all
189  *             1 Try to maximise representation
190  *             2 Try to partition the network
191  *               (isolate one peer from the rest)
192  * @param n number of @a ids
193  * @param ids the ids of the malicious peers
194  *            if @type is 2 the last id is the id of the
195  *            peer to be isolated from the rest
196  */
197 void
198 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
199                           uint32_t type,
200                           uint32_t num_peers,
201                           const struct GNUNET_PeerIdentity *ids,
202                           const struct GNUNET_PeerIdentity *target_peer);
203
204 #endif /* ENABLE_MALICIOUS */
205
206 /* Get internals for debugging/profiling purposes */
207
208 /**
209  * Request updates of view
210  *
211  * @param rps_handle handle to the rps service
212  * @param num_req_peers number of peers we want to receive
213  *        (0 for infinite updates)
214  * @param cls a closure that will be given to the callback
215  * @param ready_cb the callback called when the peers are available
216  */
217 void
218 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
219                          uint32_t num_updates,
220                          GNUNET_RPS_NotifyReadyCB view_update_cb,
221                          void *cls);
222
223
224 /**
225  * Request biased stream of peers that are being put into the sampler
226  *
227  * @param rps_handle handle to the rps service
228  * @param cls a closure that will be given to the callback
229  * @param ready_cb the callback called when the peers are available
230  */
231 struct GNUNET_RPS_StreamRequestHandle *
232 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
233                            GNUNET_RPS_NotifyReadyCB stream_input_cb,
234                            void *cls);
235
236
237 /**
238  * @brief Cancel a specific request for updates from the biased peer stream
239  *
240  * @param srh The request handle to cancel
241  */
242 void
243 GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh);
244
245
246 /**
247  * Disconnect from the rps service
248  *
249  * @param h the handle to the rps service
250  */
251 void
252 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h);
253
254
255 #if 0                           /* keep Emacsens' auto-indent happy */
256 {
257 #endif
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif
263
264 /** @} */  /* end of group */