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