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