- doxygen
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_connection.h
1 /*
2      This file is part of GNUnet.
3      (C) 2013 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 mesh/gnunet-service-mesh_connection.h
23  * @brief mesh service; dealing with connections
24  * @author Bartlomiej Polot
25  *
26  * All functions in this file should use the prefix GMC (Gnunet Mesh Connection)
27  */
28
29 #ifndef GNUNET_SERVICE_MESH_CONNECTION_H
30 #define GNUNET_SERVICE_MESH_CONNECTION_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "gnunet_util_lib.h"
41
42
43 /**
44  * All the states a connection can be in.
45  */
46 enum MeshConnectionState
47 {
48   /**
49    * Uninitialized status, should never appear in operation.
50    */
51   MESH_CONNECTION_NEW,
52
53   /**
54    * Connection create message sent, waiting for ACK.
55    */
56   MESH_CONNECTION_SENT,
57
58   /**
59    * Connection ACK sent, waiting for ACK.
60    */
61   MESH_CONNECTION_ACK,
62
63   /**
64    * Connection confirmed, ready to carry traffic.
65    */
66   MESH_CONNECTION_READY,
67 };
68
69
70 /**
71  * Struct containing all information regarding a connection to a peer.
72  */
73 struct MeshConnection;
74
75 #include "mesh_path.h"
76 #include "gnunet-service-mesh_channel.h"
77 #include "gnunet-service-mesh_peer.h"
78
79
80
81 /**
82  * Core handler for connection creation.
83  *
84  * @param cls Closure (unused).
85  * @param peer Sender (neighbor).
86  * @param message Message.
87  *
88  * @return GNUNET_OK to keep the connection open,
89  *         GNUNET_SYSERR to close it (signal serious error)
90  */
91 int
92 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
93                    const struct GNUNET_MessageHeader *message);
94
95 /**
96  * Core handler for path confirmations.
97  *
98  * @param cls closure
99  * @param message message
100  * @param peer peer identity this notification is about
101  *
102  * @return GNUNET_OK to keep the connection open,
103  *         GNUNET_SYSERR to close it (signal serious error)
104  */
105 int
106 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
107                     const struct GNUNET_MessageHeader *message);
108
109 /**
110  * Core handler for notifications of broken paths
111  *
112  * @param cls Closure (unused).
113  * @param id Peer identity of sending neighbor.
114  * @param message Message.
115  *
116  * @return GNUNET_OK to keep the connection open,
117  *         GNUNET_SYSERR to close it (signal serious error)
118  */
119 int
120 GMC_handle_broken (void* cls,
121                    const struct GNUNET_PeerIdentity* id,
122                    const struct GNUNET_MessageHeader* message);
123
124 /**
125  * Core handler for tunnel destruction
126  *
127  * @param cls Closure (unused).
128  * @param peer Peer identity of sending neighbor.
129  * @param message Message.
130  *
131  * @return GNUNET_OK to keep the connection open,
132  *         GNUNET_SYSERR to close it (signal serious error)
133  */
134 int
135 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
136                     const struct GNUNET_MessageHeader *message);
137
138 /**
139  * Core handler for encrypted mesh network traffic (channel mgmt, data).
140  *
141  * @param cls Closure (unused).
142  * @param message Message received.
143  * @param peer Peer who sent the message.
144  *
145  * @return GNUNET_OK to keep the connection open,
146  *         GNUNET_SYSERR to close it (signal serious error)
147  */
148 int
149 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
150                       const struct GNUNET_MessageHeader *message);
151
152 /**
153  * Core handler for mesh network traffic point-to-point acks.
154  *
155  * @param cls closure
156  * @param message message
157  * @param peer peer identity this notification is about
158  *
159  * @return GNUNET_OK to keep the connection open,
160  *         GNUNET_SYSERR to close it (signal serious error)
161  */
162 int
163 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
164                 const struct GNUNET_MessageHeader *message);
165
166 /**
167  * Core handler for mesh network traffic point-to-point ack polls.
168  *
169  * @param cls closure
170  * @param message message
171  * @param peer peer identity this notification is about
172  *
173  * @return GNUNET_OK to keep the connection open,
174  *         GNUNET_SYSERR to close it (signal serious error)
175  */
176 int
177 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
178                  const struct GNUNET_MessageHeader *message);
179
180 /**
181  * Core handler for mesh keepalives.
182  *
183  * @param cls closure
184  * @param message message
185  * @param peer peer identity this notification is about
186  * @return GNUNET_OK to keep the connection open,
187  *         GNUNET_SYSERR to close it (signal serious error)
188  *
189  * TODO: Check who we got this from, to validate route.
190  */
191 int
192 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
193                       const struct GNUNET_MessageHeader *message);
194
195 /**
196  * Send an ACK on the appropriate connection/channel, depending on
197  * the direction and the position of the peer.
198  *
199  * @param c Which connection to send the hop-by-hop ACK.
200  * @param fwd Is this a fwd ACK? (will go dest->root)
201  */
202 void
203 GMC_send_ack (struct MeshConnection *c, int fwd);
204
205 /**
206  * Initialize the connections subsystem
207  *
208  * @param c Configuration handle.
209  */
210 void
211 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c);
212
213 /**
214  * Shut down the connections subsystem.
215  */
216 void
217 GMC_shutdown (void);
218
219 /**
220  * Create a connection.
221  *
222  * @param cid Connection ID (either created locally or imposed remotely).
223  * @param t Tunnel this connection belongs to (or NULL);
224  * @param p Path this connection has to use.
225  * @param own_pos Own position in the @c p path.
226  */
227 struct MeshConnection *
228 GMC_new (const struct GNUNET_HashCode *cid,
229          struct MeshTunnel3 *t,
230          struct MeshPeerPath *p,
231          unsigned int own_pos);
232
233 /**
234  * Connection is no longer needed: destroy it and remove from tunnel.
235  *
236  * @param c Connection to destroy.
237  */
238 void
239 GMC_destroy (struct MeshConnection *c);
240
241 /**
242  * Get the connection ID.
243  *
244  * @param c Connection to get the ID from.
245  *
246  * @return ID of the connection.
247  */
248 const struct GNUNET_HashCode *
249 GMC_get_id (const struct MeshConnection *c);
250
251 /**
252  * Get the connection path.
253  *
254  * @param c Connection to get the path from.
255  *
256  * @return path used by the connection.
257  */
258 const struct MeshPeerPath *
259 GMC_get_path (const struct MeshConnection *c);
260
261 /**
262  * Get the connection state.
263  *
264  * @param c Connection to get the state from.
265  *
266  * @return state of the connection.
267  */
268 enum MeshConnectionState
269 GMC_get_state (const struct MeshConnection *c);
270
271 /**
272  * Get the connection tunnel.
273  *
274  * @param c Connection to get the tunnel from.
275  *
276  * @return tunnel of the connection.
277  */
278 struct MeshTunnel3 *
279 GMC_get_tunnel (const struct MeshConnection *c);
280
281 /**
282  * Get free buffer space in a connection.
283  *
284  * @param c Connection.
285  * @param fwd Is query about FWD traffic?
286  *
287  * @return Free buffer space [0 - max_msgs_queue/max_connections]
288  */
289 unsigned int
290 GMC_get_buffer (struct MeshConnection *c, int fwd);
291
292 /**
293  * Get how many messages have we allowed to send to us from a direction.
294  *
295  * @param c Connection.
296  * @param fwd Are we asking about traffic from FWD (BCK messages)?
297  *
298  * @return last_ack_sent - last_pid_recv
299  */
300 unsigned int
301 GMC_get_allowed (struct MeshConnection *c, int fwd);
302
303 /**
304  * Get messages queued in a connection.
305  *
306  * @param c Connection.
307  * @param fwd Is query about FWD traffic?
308  *
309  * @return Number of messages queued.
310  */
311 unsigned int
312 GMC_get_qn (struct MeshConnection *c, int fwd);
313
314 /**
315  * Allow the connection to advertise a buffer of the given size.
316  *
317  * The connection will send an @c fwd ACK message (so: in direction !fwd)
318  * allowing up to last_pid_recv + buffer.
319  *
320  * @param c Connection.
321  * @param buffer How many more messages the connection can accept.
322  * @param fwd Is this about FWD traffic? (The ack will go dest->root).
323  */
324 void
325 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd);
326
327 /**
328  * Send FWD keepalive packets for a connection.
329  *
330  * @param cls Closure (connection for which to send the keepalive).
331  * @param tc Notification context.
332  */
333 void
334 GMC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
335
336 /**
337  * Send BCK keepalive packets for a connection.
338  *
339  * @param cls Closure (connection for which to send the keepalive).
340  * @param tc Notification context.
341  */
342 void
343 GMC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
344
345
346 /**
347  * Notify other peers on a connection of a broken link. Mark connections
348  * to destroy after all traffic has been sent.
349  *
350  * @param c Connection on which there has been a disconnection.
351  * @param peer Peer that disconnected.
352  */
353 void
354 GMC_notify_broken (struct MeshConnection *c,
355                    struct MeshPeer *peer);
356
357 /**
358  * Is this peer the first one on the connection?
359  *
360  * @param c Connection.
361  * @param fwd Is this about fwd traffic?
362  *
363  * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
364  */
365 int
366 GMC_is_origin (struct MeshConnection *c, int fwd);
367
368 /**
369  * Is this peer the last one on the connection?
370  *
371  * @param c Connection.
372  * @param fwd Is this about fwd traffic?
373  *            Note that the ROOT is the terminal for BCK traffic!
374  *
375  * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
376  */
377 int
378 GMC_is_terminal (struct MeshConnection *c, int fwd);
379
380 /**
381  * See if we are allowed to send by the next hop in the given direction.
382  *
383  * @param c Connection.
384  * @param fwd Is this about fwd traffic?
385  *
386  * @return GNUNET_YES in case it's OK.
387  */
388 int
389 GMC_is_sendable (struct MeshConnection *c, int fwd);
390
391 /**
392  * Sends an already built message on a connection, properly registering
393  * all used resources.
394  *
395  * @param message Message to send. Function makes a copy of it.
396  *                If message is not hop-by-hop, decrements TTL of copy.
397  * @param c Connection on which this message is transmitted.
398  * @param ch Channel on which this message is transmitted, or NULL.
399  * @param fwd Is this a fwd message?
400  */
401 void
402 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
403                            struct MeshConnection *c,
404                            struct MeshChannel *ch,
405                            int fwd);
406
407 /**
408  * Sends a CREATE CONNECTION message for a path to a peer.
409  * Changes the connection and tunnel states if necessary.
410  *
411  * @param connection Connection to create.
412  */
413 void
414 GMC_send_create (struct MeshConnection *connection);
415
416 /**
417  * Send a message to all peers in this connection that the connection
418  * is no longer valid.
419  *
420  * If some peer should not receive the message, it should be zero'ed out
421  * before calling this function.
422  *
423  * @param c The connection whose peers to notify.
424  */
425 void
426 GMC_send_destroy (struct MeshConnection *c);
427
428 /**
429  * @brief Start a polling timer for the connection.
430  *
431  * When a neighbor does not accept more traffic on the connection it could be
432  * caused by a simple congestion or by a lost ACK. Polling enables to check
433  * for the lastest ACK status for a connection.
434  *
435  * @param c Connection.
436  * @param fwd Should we poll in the FWD direction?
437  */
438 void
439 GMC_start_poll (struct MeshConnection *c, int fwd);
440
441
442 /**
443  * @brief Stop polling a connection for ACKs.
444  *
445  * Once we have enough ACKs for future traffic, polls are no longer necessary.
446  *
447  * @param c Connection.
448  * @param fwd Should we stop the poll in the FWD direction?
449  */
450 void
451 GMC_stop_poll (struct MeshConnection *c, int fwd);
452
453 /**
454  * Get a (static) string for a connection.
455  *
456  * @param c Connection.
457  */
458 const char *
459 GMC_2s (struct MeshConnection *c);
460
461 #if 0                           /* keep Emacsens' auto-indent happy */
462 {
463 #endif
464 #ifdef __cplusplus
465 }
466 #endif
467
468 /* ifndef GNUNET_SERVICE_MESH_CONNECTION_H */
469 #endif
470 /* end of gnunet-service-mesh_connection.h */