-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / transport / gnunet-service-transport_neighbours.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 GNUnet e.V.
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  * @file transport/gnunet-service-transport_neighbours.h
21  * @brief neighbour management API
22  * @author Christian Grothoff
23  */
24 #ifndef GNUNET_SERVICE_TRANSPORT_NEIGHBOURS_H
25 #define GNUNET_SERVICE_TRANSPORT_NEIGHBOURS_H
26
27 #include "gnunet_statistics_service.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_transport_plugin.h"
30 #include "gnunet-service-transport.h"
31 #include "transport.h"
32 #include "gnunet_util_lib.h"
33
34
35 /**
36  * Initialize the neighbours subsystem.
37  *
38  * @param max_fds maximum number of fds to use
39  */
40 void
41 GST_neighbours_start (unsigned int max_fds);
42
43
44 /**
45  * Cleanup the neighbours subsystem.
46  */
47 void
48 GST_neighbours_stop (void);
49
50
51 /**
52  * Test if we're connected to the given peer.
53  *
54  * @param target peer to test
55  * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
56  */
57 int
58 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target);
59
60
61 /**
62  * Function called after the transmission is done.
63  *
64  * @param cls closure
65  * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
66  * @param bytes_payload how much payload was transmitted
67  * @param bytes_on_wire how many bytes were used on the wire
68  */
69 typedef void
70 (*GST_NeighbourSendContinuation) (void *cls,
71                                   int success,
72                                   size_t bytes_payload,
73                                   size_t bytes_on_wire);
74
75
76 /**
77  * Transmit a message to the given target using the active connection.
78  *
79  * @param target destination
80  * @param msg message to send
81  * @param msg_size number of bytes in @a msg
82  * @param timeout when to fail with timeout
83  * @param cont function to call when done
84  * @param cont_cls closure for @a cont
85  */
86 void
87 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
88                      const void *msg,
89                      size_t msg_size,
90                      struct GNUNET_TIME_Relative timeout,
91                      GST_NeighbourSendContinuation cont, void *cont_cls);
92
93
94 /**
95  * We have received a message from the given sender.
96  * How long should we delay before receiving more?
97  * (Also used to keep the peer marked as live).
98  *
99  * @param sender sender of the message
100  * @param size size of the message
101  * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
102  *                   #GNUNET_NO if the neighbour is not connected or violates the quota
103  * @return how long to wait before reading more from this sender
104  */
105 struct GNUNET_TIME_Relative
106 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
107                                         ssize_t size,
108                                         int *do_forward);
109
110
111 /**
112  * Keep the connection to the given neighbour alive longer,
113  * we received a KEEPALIVE (or equivalent); send a response.
114  *
115  * @param neighbour neighbour to keep alive (by sending keep alive response)
116  * @param m the keep alive message containing the nonce to respond to
117  */
118 void
119 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
120                           const struct GNUNET_MessageHeader *m);
121
122
123 /**
124  * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
125  * latency to this peer.  Pass the updated information (existing ats
126  * plus calculated latency) to ATS.
127  *
128  * @param neighbour neighbour to keep alive
129  * @param m the message containing the keep alive response
130  */
131 void
132 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
133                                    const struct GNUNET_MessageHeader *m);
134
135
136 /**
137  * If we have an active connection to the given target, it must be shutdown.
138  *
139  * @param target peer to disconnect from
140  */
141 void
142 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target);
143
144
145 /**
146  * Function called for each neighbour.
147  *
148  * @param cls closure
149  * @param peer identity of the neighbour
150  * @param address the address of the neighbour
151  * @param state current state the peer is in
152  * @param state_timeout timeout for this state
153  * @param bandwidth_in inbound quota in NBO
154  * @param bandwidth_out outbound quota in NBO
155  */
156 typedef void
157 (*GST_NeighbourIterator) (void *cls,
158                           const struct GNUNET_PeerIdentity *peer,
159                           const struct GNUNET_HELLO_Address *address,
160                           enum GNUNET_TRANSPORT_PeerState state,
161                           struct GNUNET_TIME_Absolute state_timeout,
162                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
163                           struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
164
165
166 /**
167  * Iterate over all connected neighbours.
168  *
169  * @param cb function to call
170  * @param cb_cls closure for @a cb
171  */
172 void
173 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls);
174
175
176 /**
177  * A session was terminated. Take note.
178  *
179  * @param peer identity of the peer where the session died
180  * @param session session that is gone
181  * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
182  *        this session was not in use
183  */
184 int
185 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
186                                    struct GNUNET_ATS_Session *session);
187
188
189 /**
190  * Track information about data we received from the
191  * given address (used to notify ATS about our utilization
192  * of allocated resources).
193  *
194  * @param address the address we got data from
195  * @param message the message we received (really only the size is used)
196  */
197 void
198 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
199                                  const struct GNUNET_MessageHeader *message);
200
201
202 /**
203  * Track information about data we transmitted using the given @a
204  * address and @a session (used to notify ATS about our utilization of
205  * allocated resources).
206  *
207  * @param address the address we transmitted data to
208  * @param session session we used to transmit data
209  * @param message the message we sent (really only the size is used)
210  */
211 void
212 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
213                                  struct GNUNET_ATS_Session *session,
214                                  size_t size);
215
216
217 /**
218  * For an existing neighbour record, set the active connection to
219  * use the given address.
220  *
221  * @param address address of the other peer to start using
222  * @param session session to use (or NULL)
223  * @param bandwidth_in inbound quota to be used when connection is up
224  * @param bandwidth_out outbound quota to be used when connection is up
225  */
226 void
227 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
228                                   struct GNUNET_ATS_Session *session,
229                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
230                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
231
232
233 /**
234  * We received a 'SESSION_CONNECT' message from the other peer.
235  * Consider switching to it.
236  *
237  * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
238  * @param peer identity of the peer to switch the address for
239  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
240  */
241 int
242 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
243                                    const struct GNUNET_PeerIdentity *peer);
244
245
246 /**
247  * We received a 'SESSION_CONNECT_ACK' message from the other peer.
248  * Consider switching to it.
249  *
250  * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
251  * @param address address of the other peer
252  * @param session session to use (or NULL)
253  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
254  */
255 int
256 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
257                                        const struct GNUNET_HELLO_Address *address,
258                                        struct GNUNET_ATS_Session *session);
259
260
261 /**
262  * We received a 'SESSION_ACK' message from the other peer.
263  * If we sent a 'CONNECT_ACK' last, this means we are now
264  * connected.  Otherwise, do nothing.
265  *
266  * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
267  * @param address address of the other peer
268  * @param session session to use (or NULL)
269  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
270  */
271 int
272 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
273                                    const struct GNUNET_HELLO_Address *address,
274                                    struct GNUNET_ATS_Session *session);
275
276
277 /**
278  * Obtain current address information for the given neighbour.
279  *
280  * @param peer
281  * @return address currently used
282  */
283 const struct GNUNET_HELLO_Address *
284 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer);
285
286
287 /**
288  * We received a quoat message from the given peer,
289  * validate and process.
290  *
291  * @param peer sender of the message
292  * @param msg the quota message
293  */
294 void
295 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
296                                      const struct GNUNET_MessageHeader *msg);
297
298
299 /**
300  * We received a disconnect message from the given peer,
301  * validate and process.
302  *
303  * @param peer sender of the message
304  * @param msg the disconnect message
305  */
306 void
307 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
308                                           const struct GNUNET_MessageHeader *msg);
309
310
311 #endif
312 /* end of file gnunet-service-transport_neighbours.h */