274ca94a28e767a14cb7bce95695af39fa75c7af
[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 /**
68  * Connect to the rps service
69  *
70  * @param cfg configuration to use
71  * @return handle to the rps service
72  */
73   struct GNUNET_RPS_Handle *
74 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
75
76
77 /**
78  * @brief Start a sub with the given shared value
79  *
80  * @param h Handle to rps
81  * @param shared_value The shared value that defines the members of the sub (-gorup)
82  */
83 void
84 GNUNET_RPS_sub_start (struct GNUNET_RPS_Handle *h,
85                       const char *shared_value);
86
87
88 /**
89  * @brief Stop a sub with the given shared value
90  *
91  * @param h Handle to rps
92  * @param shared_value The shared value that defines the members of the sub (-gorup)
93  */
94 void
95 GNUNET_RPS_sub_stop (struct GNUNET_RPS_Handle *h,
96                      const char *shared_value);
97
98
99 /**
100  * Request n random peers.
101  *
102  * This does exacly the same as GNUNET_RPS_request_peers_single_call
103  * but needs a GNUNET_RPS_Handle.
104  * This exists only for other parts of GNUnet that expect having to
105  * (dis)connect from/to a service.
106  *
107  * @param h handle to the rps service
108  * @param n number of random peers to return
109  * @param ready_cb the callback to be called when the peers are available
110  * @param cls a closure that will be given to the callback
111  * @return handle to this request
112  */
113   struct GNUNET_RPS_Request_Handle *
114 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
115                           GNUNET_RPS_NotifyReadyCB ready_cb,
116                           void *cls);
117
118 /**
119  * Seed rps service with peerIDs.
120  *
121  * @param h handle to the rps service
122  * @param n number of peers to seed
123  * @param ids the ids of the peers seeded
124  */
125   void
126 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint32_t n,
127                      const struct GNUNET_PeerIdentity * ids);
128
129 /**
130  * Cancle an issued request.
131  *
132  * @param rh handle of the pending request to be canceled
133  */
134   void
135 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh);
136
137
138 #ifdef ENABLE_MALICIOUS
139 /**
140  * Turn RPS service to act malicious.
141  *
142  * @param h handle to the rps service
143  * @param type which type of malicious peer to turn to.
144  *             0 Don't act malicious at all
145  *             1 Try to maximise representation
146  *             2 Try to partition the network
147  *               (isolate one peer from the rest)
148  * @param n number of @a ids
149  * @param ids the ids of the malicious peers
150  *            if @type is 2 the last id is the id of the
151  *            peer to be isolated from the rest
152  */
153   void
154 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
155                           uint32_t type,
156                           uint32_t num_peers,
157                           const struct GNUNET_PeerIdentity *ids,
158                           const struct GNUNET_PeerIdentity *target_peer);
159 #endif /* ENABLE_MALICIOUS */
160
161 /* Get internals for debugging/profiling purposes */
162
163 /**
164  * Request updates of view
165  *
166  * @param rps_handle handle to the rps service
167  * @param num_req_peers number of peers we want to receive
168  *        (0 for infinite updates)
169  * @param cls a closure that will be given to the callback
170  * @param ready_cb the callback called when the peers are available
171  */
172 void
173 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
174                          uint32_t num_updates,
175                          GNUNET_RPS_NotifyReadyCB view_update_cb,
176                          void *cls);
177
178
179 /**
180  * Request biased stream of peers that are being put into the sampler
181  *
182  * @param rps_handle handle to the rps service
183  * @param cls a closure that will be given to the callback
184  * @param ready_cb the callback called when the peers are available
185  */
186 struct GNUNET_RPS_StreamRequestHandle *
187 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
188                            GNUNET_RPS_NotifyReadyCB stream_input_cb,
189                            void *cls);
190
191
192 /**
193  * @brief Cancel a specific request for updates from the biased peer stream
194  *
195  * @param srh The request handle to cancel
196  */
197 void
198 GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh);
199
200
201 /**
202  * Disconnect from the rps service
203  *
204  * @param h the handle to the rps service
205  */
206   void
207 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h);
208
209
210 #if 0                           /* keep Emacsens' auto-indent happy */
211 {
212 #endif
213 #ifdef __cplusplus
214 }
215 #endif
216
217 #endif
218
219 /** @} */  /* end of group */