avoid passing both PeerIdentity and Address (which contains PeerIdentity) if address...
[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  * @param bytes_payload how much payload was transmitted
90  * @param bytes_on_wire how many bytes were used on the wire
91  */
92 typedef void
93 (*GST_NeighbourSendContinuation) (void *cls,
94                                   int success,
95                                   size_t bytes_payload,
96                                   size_t bytes_on_wire);
97
98
99 /**
100  * Transmit a message to the given target using the active connection.
101  *
102  * @param target destination
103  * @param msg message to send
104  * @param msg_size number of bytes in @a msg
105  * @param timeout when to fail with timeout
106  * @param cont function to call when done
107  * @param cont_cls closure for @a cont
108  */
109 void
110 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
111                      const void *msg,
112                      size_t msg_size,
113                      struct GNUNET_TIME_Relative timeout,
114                      GST_NeighbourSendContinuation cont, void *cont_cls);
115
116
117
118 /**
119  * FIXME
120  */
121 void
122 GST_neighbours_register_quota_notification (void *cls,
123                                            const struct GNUNET_PeerIdentity *peer,
124                                            const char *plugin,
125                                            struct Session *session);
126
127
128 /**
129  * FIXME
130  */
131 void
132 GST_neighbours_unregister_quota_notification (void *cls,
133                                               const struct GNUNET_PeerIdentity *peer,
134                                               const char *plugin,
135                                               struct Session *session);
136
137
138 /**
139  * We have received a message from the given sender.
140  * How long should we delay before receiving more?
141  * (Also used to keep the peer marked as live).
142  *
143  * @param sender sender of the message
144  * @param size size of the message
145  * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
146  *                   #GNUNET_NO if the neighbour is not connected or violates the quota
147  * @return how long to wait before reading more from this sender
148  */
149 struct GNUNET_TIME_Relative
150 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
151                                         ssize_t size,
152                                         int *do_forward);
153
154
155 /**
156  * Keep the connection to the given neighbour alive longer,
157  * we received a KEEPALIVE (or equivalent); send a response.
158  *
159  * @param neighbour neighbour to keep alive (by sending keep alive response)
160  * @param m the keep alive message containing the nonce to respond to
161  */
162 void
163 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
164                           const struct GNUNET_MessageHeader *m);
165
166
167 /**
168  * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
169  * latency to this peer.  Pass the updated information (existing ats
170  * plus calculated latency) to ATS.
171  *
172  * @param neighbour neighbour to keep alive
173  * @param m the message containing the keep alive response
174  */
175 void
176 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
177                                    const struct GNUNET_MessageHeader *m);
178
179
180 /**
181  * Change the incoming quota for the given peer.
182  *
183  * @param neighbour identity of peer to change qutoa for
184  * @param quota new quota
185  */
186 void
187 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
188                                    struct GNUNET_BANDWIDTH_Value32NBO quota);
189
190
191 /**
192  * If we have an active connection to the given target, it must be shutdown.
193  *
194  * @param target peer to disconnect from
195  */
196 void
197 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target);
198
199
200 /**
201  * Function called for each neighbour.
202  *
203  * @param cls closure
204  * @param peer identity of the neighbour
205  * @param address the address of the neighbour
206  * @param state current state the peer is in
207  * @param state_timeout timeout for this state
208  * @param bandwidth_in inbound quota in NBO
209  * @param bandwidth_out outbound quota in NBO
210  */
211 typedef void
212 (*GST_NeighbourIterator) (void *cls,
213                           const struct GNUNET_PeerIdentity *peer,
214                           const struct GNUNET_HELLO_Address *address,
215                           enum GNUNET_TRANSPORT_PeerState state,
216                           struct GNUNET_TIME_Absolute state_timeout,
217                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
218                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
219
220
221 /**
222  * Iterate over all connected neighbours.
223  *
224  * @param cb function to call
225  * @param cb_cls closure for @a cb
226  */
227 void
228 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls);
229
230
231 /**
232  * A session was terminated. Take note.
233  *
234  * @param peer identity of the peer where the session died
235  * @param session session that is gone
236  * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
237  *        this session was not in use
238  */
239 int
240 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
241                                    struct Session *session);
242
243
244 /**
245  * FIXME
246  */
247 void
248 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
249                                  struct Session *session,
250                                  const struct GNUNET_MessageHeader *message);
251
252
253 /**
254  * FIXME
255  */
256 void
257 GST_neighbours_notify_payload_recv (const struct GNUNET_HELLO_Address *address,
258                                     struct Session *session,
259                                     const struct GNUNET_MessageHeader *message);
260
261
262 /**
263  * FIXME
264  */
265 void
266 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
267                                     size_t size);
268
269
270 /**
271  * FIXME
272  */
273 void
274 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
275                                  struct Session *session,
276                                  size_t size);
277
278
279 /**
280  * For an existing neighbour record, set the active connection to
281  * use the given address.
282  *
283  * @param address address of the other peer to start using
284  * @param session session to use (or NULL)
285  * @param bandwidth_in inbound quota to be used when connection is up
286  * @param bandwidth_out outbound quota to be used when connection is up
287  */
288 void
289 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
290                                   struct Session *session,
291                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
292                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
293
294
295 /**
296  * We received a 'SESSION_CONNECT' message from the other peer.
297  * Consider switching to it.
298  *
299  * @param message possibly a 'struct SessionConnectMessage' (check format)
300  * @param peer identity of the peer to switch the address for
301  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
302  */
303 int
304 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
305                                    const struct GNUNET_PeerIdentity *peer);
306
307
308 /**
309  * We received a 'SESSION_CONNECT_ACK' message from the other peer.
310  * Consider switching to it.
311  *
312  * @param message possibly a `struct SessionConnectMessage` (check format)
313  * @param address address of the other peer
314  * @param session session to use (or NULL)
315  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
316  */
317 int
318 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
319                                        const struct GNUNET_HELLO_Address *address,
320                                        struct Session *session);
321
322
323 /**
324  * We received a 'SESSION_ACK' message from the other peer.
325  * If we sent a 'CONNECT_ACK' last, this means we are now
326  * connected.  Otherwise, do nothing.
327  *
328  * @param message possibly a 'struct SessionConnectMessage' (check format)
329  * @param address address of the other peer
330  * @param session session to use (or NULL)
331  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
332  */
333 int
334 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
335                                    const struct GNUNET_HELLO_Address *address,
336                                    struct Session *session);
337
338
339 /**
340  * Obtain current latency information for the given neighbour.
341  *
342  * @param peer
343  * @return observed latency of the address, FOREVER if the address was
344  *         never successfully validated
345  */
346 struct GNUNET_TIME_Relative
347 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer);
348
349
350 /**
351  * Obtain current address information for the given neighbour.
352  *
353  * @param peer
354  * @return address currently used
355  */
356 struct GNUNET_HELLO_Address *
357 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer);
358
359
360 /**
361  * We received a disconnect message from the given peer,
362  * validate and process.
363  *
364  * @param peer sender of the message
365  * @param msg the disconnect message
366  */
367 void
368 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
369                                           const struct GNUNET_MessageHeader *msg);
370
371
372 #endif
373 /* end of file gnunet-service-transport_neighbours.h */