hunting bugs
[oweals/gnunet.git] / src / transport / gnunet-service-transport_neighbours.h
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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 /**
22  * @file transport/gnunet-service-transport_neighbours.h
23  * @brief neighbour management API
24  * @author Christian Grothoff
25  */
26 #ifndef GNUNET_SERVICE_TRANSPORT_NEIGHBOURS_H
27 #define GNUNET_SERVICE_TRANSPORT_NEIGHBOURS_H
28
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_transport_plugin.h"
32 #include "transport.h"
33 #include "gnunet_util_lib.h"
34
35 // TODO:
36 // - ATS and similar info is a bit lacking in the API right now...
37
38
39
40 /**
41  * Initialize the neighbours subsystem.
42  *
43  * @param cls closure for callbacks
44  * @param connect_cb function to call if we connect to a peer
45  * @param disconnect_cb function to call if we disconnect from a peer
46  * @param peer_address_cb function to call if a neighbour's active address changes
47  */
48 void
49 GST_neighbours_start (void *cls,
50                       NotifyConnect connect_cb,
51                       GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
52                       GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb);
53
54
55 /**
56  * Cleanup the neighbours subsystem.
57  */
58 void
59 GST_neighbours_stop (void);
60
61
62 /**
63  * Try to create a connection to the given target (eventually).
64  *
65  * @param target peer to try to connect to
66  */
67 void
68 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target);
69
70
71 /**
72  * Test if we're connected to the given peer.
73  *
74  * @param target peer to test
75  * @return GNUNET_YES if we are connected, GNUNET_NO if not
76  */
77 int
78 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target);
79
80
81 /**
82  * Function called after the transmission is done.
83  *
84  * @param cls closure
85  * @param success GNUNET_OK on success, GNUNET_NO on failure, GNUNET_SYSERR if we're not connected
86  */
87 typedef void (*GST_NeighbourSendContinuation) (void *cls, int success);
88
89
90 /**
91  * Transmit a message to the given target using the active connection.
92  *
93  * @param target destination
94  * @param msg message to send
95  * @param msg_size number of bytes in msg
96  * @param timeout when to fail with timeout
97  * @param cont function to call when done
98  * @param cont_cls closure for 'cont'
99  */
100 void
101 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
102                      size_t msg_size, struct GNUNET_TIME_Relative timeout,
103                      GST_NeighbourSendContinuation cont, void *cont_cls);
104
105
106 /**
107  * We have received a message from the given sender.
108  * How long should we delay before receiving more?
109  * (Also used to keep the peer marked as live).
110  *
111  * @param sender sender of the message
112  * @param size size of the message
113  * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
114  *                   GNUNET_NO if the neighbour is not connected or violates the quota
115  * @return how long to wait before reading more from this sender
116  */
117 struct GNUNET_TIME_Relative
118 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
119                                         *sender, ssize_t size, int *do_forward);
120
121
122 /**
123  * Keep the connection to the given neighbour alive longer,
124  * we received a KEEPALIVE (or equivalent).
125  *
126  * @param neighbour neighbour to keep alive
127  */
128 void
129 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour);
130
131
132 /**
133  * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
134  * to this peer
135  *
136  * @param neighbour neighbour to keep alive
137  * @param ats performance data
138  * @param ats_count number of entries in ats
139  */
140 void
141 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
142                                    const struct GNUNET_ATS_Information *ats,
143                                    uint32_t ats_count);
144
145
146 /**
147  * Change the incoming quota for the given peer.
148  *
149  * @param neighbour identity of peer to change qutoa for
150  * @param quota new quota
151  */
152 void
153 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
154                                    struct GNUNET_BANDWIDTH_Value32NBO quota);
155
156
157 /**
158  * If we have an active connection to the given target, it must be shutdown.
159  *
160  * @param target peer to disconnect from
161  */
162 void
163 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target);
164
165
166 /**
167  * Function called for each connected neighbour.
168  *
169  * @param cls closure
170  * @param neighbour identity of the neighbour
171  * @param ats performance data
172  * @param ats_count number of entries in ats (including 0-termination)
173  * @param address the address (or NULL)
174  * @param bandwidth_in inbound quota in NBO
175  * @param bandwidth_out outbound quota in NBO
176  */
177 typedef void (*GST_NeighbourIterator) (void *cls,
178                                        const struct GNUNET_PeerIdentity *
179                                        neighbour,
180                                        const struct GNUNET_ATS_Information *
181                                        ats, uint32_t ats_count,
182                                        const struct GNUNET_HELLO_Address *
183                                        address,
184                                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
185                                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
186
187
188 /**
189  * Iterate over all connected neighbours.
190  *
191  * @param cb function to call
192  * @param cb_cls closure for cb
193  */
194 void
195 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls);
196
197
198 /**
199  * A session was terminated. Take note.
200  *
201  * @param peer identity of the peer where the session died
202  * @param session session that is gone
203  * @return GNUNET_YES if this was a session used, GNUNET_NO if
204  *        this session was not in use
205  */
206 int
207 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
208                                    struct Session *session);
209
210
211 /**
212  * For an existing neighbour record, set the active connection to
213  * use the given address.
214  *
215  * @param peer identity of the peer to switch the address for
216  * @param address address of the other peer, NULL if other peer
217  *                       connected to us
218  * @param session session to use (or NULL)
219  * @param ats performance data
220  * @param ats_count number of entries in ats
221  * @param bandwidth_in inbound quota to be used when connection is up
222  * @param bandwidth_out outbound quota to be used when connection is up
223  */
224 void
225 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
226                                        const struct GNUNET_HELLO_Address
227                                        *address, struct Session *session,
228                                        const struct GNUNET_ATS_Information *ats,
229                                        uint32_t ats_count,
230                                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
231                                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
232
233
234 /**
235  * We received a 'SESSION_CONNECT' message from the other peer.
236  * Consider switching to it.
237  *
238  * @param message possibly a 'struct SessionConnectMessage' (check format)
239  * @param peer identity of the peer to switch the address for
240  * @param address address of the other peer, NULL if other peer
241  *                       connected to us
242  * @param session session to use (or NULL)
243  * @param ats performance data
244  * @param ats_count number of entries in ats (excluding 0-termination)
245   */
246 void
247 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
248                                const struct GNUNET_PeerIdentity *peer,
249                                const struct GNUNET_HELLO_Address *address,
250                                struct Session *session,
251                                const struct GNUNET_ATS_Information *ats,
252                                uint32_t ats_count);
253
254
255 /**
256  * We received a 'SESSION_CONNECT_ACK' message from the other peer.
257  * Consider switching to it.
258  *
259  * @param message possibly a 'struct SessionConnectMessage' (check format)
260  * @param peer identity of the peer to switch the address for
261  * @param address address of the other peer, NULL if other peer
262  *                       connected to us
263  * @param session session to use (or NULL)
264  * @param ats performance data
265  * @param ats_count number of entries in ats
266  */
267 void
268 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
269                                    const struct GNUNET_PeerIdentity *peer,
270                                    const struct GNUNET_HELLO_Address *address,
271                                    struct Session *session,
272                                    const struct GNUNET_ATS_Information *ats,
273                                    uint32_t ats_count);
274
275
276 /**
277  * We received a 'SESSION_ACK' message from the other peer.
278  * FIXME: describe what this means!
279  *
280  * @param message possibly a 'struct SessionConnectMessage' (check format)
281  * @param peer identity of the peer to switch the address for
282  * @param address address of the other peer, NULL if other peer
283  *                       connected to us
284  * @param session session to use (or NULL)
285  * @param ats performance data
286  * @param ats_count number of entries in ats
287  */
288 void
289 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
290                                    const struct GNUNET_PeerIdentity *peer,
291                                    const struct GNUNET_HELLO_Address *address,
292                                    struct Session *session,
293                                    const struct GNUNET_ATS_Information *ats,
294                                    uint32_t ats_count);
295
296
297 /**
298  * Obtain current latency information for the given neighbour.
299  *
300  * @param peer
301  * @return observed latency of the address, FOREVER if the address was
302  *         never successfully validated
303  */
304 struct GNUNET_TIME_Relative
305 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer);
306
307
308 /**
309  * Obtain current address information for the given neighbour.
310  *
311  * @param peer
312  * @return address currently used
313  */
314 struct GNUNET_HELLO_Address *
315 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer);
316
317
318 /**
319  * We received a disconnect message from the given peer,
320  * validate and process.
321  *
322  * @param peer sender of the message
323  * @param msg the disconnect message
324  */
325 void
326 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
327                                           *peer,
328                                           const struct GNUNET_MessageHeader
329                                           *msg);
330
331
332 #endif
333 /* end of file gnunet-service-transport_neighbours.h */