7bb3c6aa1a203d8bfe62a8f0f66f7ef899104352
[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 key exchange traffic (ephemeral key, ping, pong).
154  *
155  * @param cls Closure (unused).
156  * @param message Message received.
157  * @param peer Peer who sent the message.
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_kx (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 acks.
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_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
178                 const struct GNUNET_MessageHeader *message);
179
180 /**
181  * Core handler for mesh network traffic point-to-point ack polls.
182  *
183  * @param cls closure
184  * @param message message
185  * @param peer peer identity this notification is about
186  *
187  * @return GNUNET_OK to keep the connection open,
188  *         GNUNET_SYSERR to close it (signal serious error)
189  */
190 int
191 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
192                  const struct GNUNET_MessageHeader *message);
193
194 /**
195  * Core handler for mesh keepalives.
196  *
197  * @param cls closure
198  * @param message message
199  * @param peer peer identity this notification is about
200  * @return GNUNET_OK to keep the connection open,
201  *         GNUNET_SYSERR to close it (signal serious error)
202  *
203  * TODO: Check who we got this from, to validate route.
204  */
205 int
206 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
207                       const struct GNUNET_MessageHeader *message);
208
209 /**
210  * Send an ACK on the appropriate connection/channel, depending on
211  * the direction and the position of the peer.
212  *
213  * @param c Which connection to send the hop-by-hop ACK.
214  * @param fwd Is this a fwd ACK? (will go dest->root)
215  */
216 void
217 GMC_send_ack (struct MeshConnection *c, int fwd);
218
219 /**
220  * Initialize the connections subsystem
221  *
222  * @param c Configuration handle.
223  */
224 void
225 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c);
226
227 /**
228  * Shut down the connections subsystem.
229  */
230 void
231 GMC_shutdown (void);
232
233 /**
234  * Create a connection.
235  *
236  * @param cid Connection ID (either created locally or imposed remotely).
237  * @param t Tunnel this connection belongs to (or NULL);
238  * @param p Path this connection has to use.
239  * @param own_pos Own position in the @c p path.
240  *
241  * @return Newly created connection, NULL in case of error (own id not in path).
242  */
243 struct MeshConnection *
244 GMC_new (const struct GNUNET_HashCode *cid,
245          struct MeshTunnel3 *t,
246          struct MeshPeerPath *p,
247          unsigned int own_pos);
248
249 /**
250  * Connection is no longer needed: destroy it.
251  *
252  * Cancels all pending traffic (including possible DESTROY messages), all
253  * maintenance tasks and removes the connection from neighbor peers and tunnel.
254  *
255  * @param c Connection to destroy.
256  */
257 void
258 GMC_destroy (struct MeshConnection *c);
259
260 /**
261  * Get the connection ID.
262  *
263  * @param c Connection to get the ID from.
264  *
265  * @return ID of the connection.
266  */
267 const struct GNUNET_HashCode *
268 GMC_get_id (const struct MeshConnection *c);
269
270 /**
271  * Get the connection path.
272  *
273  * @param c Connection to get the path from.
274  *
275  * @return path used by the connection.
276  */
277 const struct MeshPeerPath *
278 GMC_get_path (const struct MeshConnection *c);
279
280 /**
281  * Get the connection state.
282  *
283  * @param c Connection to get the state from.
284  *
285  * @return state of the connection.
286  */
287 enum MeshConnectionState
288 GMC_get_state (const struct MeshConnection *c);
289
290 /**
291  * Get the connection tunnel.
292  *
293  * @param c Connection to get the tunnel from.
294  *
295  * @return tunnel of the connection.
296  */
297 struct MeshTunnel3 *
298 GMC_get_tunnel (const struct MeshConnection *c);
299
300 /**
301  * Get free buffer space in a connection.
302  *
303  * @param c Connection.
304  * @param fwd Is query about FWD traffic?
305  *
306  * @return Free buffer space [0 - max_msgs_queue/max_connections]
307  */
308 unsigned int
309 GMC_get_buffer (struct MeshConnection *c, int fwd);
310
311 /**
312  * Get how many messages have we allowed to send to us from a direction.
313  *
314  * @param c Connection.
315  * @param fwd Are we asking about traffic from FWD (BCK messages)?
316  *
317  * @return last_ack_sent - last_pid_recv
318  */
319 unsigned int
320 GMC_get_allowed (struct MeshConnection *c, int fwd);
321
322 /**
323  * Get messages queued in a connection.
324  *
325  * @param c Connection.
326  * @param fwd Is query about FWD traffic?
327  *
328  * @return Number of messages queued.
329  */
330 unsigned int
331 GMC_get_qn (struct MeshConnection *c, int fwd);
332
333 /**
334  * Allow the connection to advertise a buffer of the given size.
335  *
336  * The connection will send an @c fwd ACK message (so: in direction !fwd)
337  * allowing up to last_pid_recv + buffer.
338  *
339  * @param c Connection.
340  * @param buffer How many more messages the connection can accept.
341  * @param fwd Is this about FWD traffic? (The ack will go dest->root).
342  */
343 void
344 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd);
345
346 /**
347  * Send FWD keepalive packets for a connection.
348  *
349  * @param cls Closure (connection for which to send the keepalive).
350  * @param tc Notification context.
351  */
352 void
353 GMC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
354
355 /**
356  * Send BCK keepalive packets for a connection.
357  *
358  * @param cls Closure (connection for which to send the keepalive).
359  * @param tc Notification context.
360  */
361 void
362 GMC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
363
364
365 /**
366  * Notify other peers on a connection of a broken link. Mark connections
367  * to destroy after all traffic has been sent.
368  *
369  * @param c Connection on which there has been a disconnection.
370  * @param peer Peer that disconnected.
371  */
372 void
373 GMC_notify_broken (struct MeshConnection *c,
374                    struct MeshPeer *peer);
375
376 /**
377  * Is this peer the first one on the connection?
378  *
379  * @param c Connection.
380  * @param fwd Is this about fwd traffic?
381  *
382  * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
383  */
384 int
385 GMC_is_origin (struct MeshConnection *c, int fwd);
386
387 /**
388  * Is this peer the last one on the connection?
389  *
390  * @param c Connection.
391  * @param fwd Is this about fwd traffic?
392  *            Note that the ROOT is the terminal for BCK traffic!
393  *
394  * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
395  */
396 int
397 GMC_is_terminal (struct MeshConnection *c, int fwd);
398
399 /**
400  * See if we are allowed to send by the next hop in the given direction.
401  *
402  * @param c Connection.
403  * @param fwd Is this about fwd traffic?
404  *
405  * @return #GNUNET_YES in case it's OK to send.
406  */
407 int
408 GMC_is_sendable (struct MeshConnection *c, int fwd);
409
410 /**
411  * Sends an already built message on a connection, properly registering
412  * all used resources.
413  *
414  * @param message Message to send. Function makes a copy of it.
415  *                If message is not hop-by-hop, decrements TTL of copy.
416  * @param c Connection on which this message is transmitted.
417  * @param fwd Is this a fwd message?
418  */
419 void
420 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
421                            struct MeshConnection *c,
422                            int fwd);
423
424 /**
425  * Sends a CREATE CONNECTION message for a path to a peer.
426  * Changes the connection and tunnel states if necessary.
427  *
428  * @param connection Connection to create.
429  */
430 void
431 GMC_send_create (struct MeshConnection *connection);
432
433 /**
434  * Send a message to all peers in this connection that the connection
435  * is no longer valid.
436  *
437  * If some peer should not receive the message, it should be zero'ed out
438  * before calling this function.
439  *
440  * @param c The connection whose peers to notify.
441  */
442 void
443 GMC_send_destroy (struct MeshConnection *c);
444
445 /**
446  * @brief Start a polling timer for the connection.
447  *
448  * When a neighbor does not accept more traffic on the connection it could be
449  * caused by a simple congestion or by a lost ACK. Polling enables to check
450  * for the lastest ACK status for a connection.
451  *
452  * @param c Connection.
453  * @param fwd Should we poll in the FWD direction?
454  */
455 void
456 GMC_start_poll (struct MeshConnection *c, int fwd);
457
458
459 /**
460  * @brief Stop polling a connection for ACKs.
461  *
462  * Once we have enough ACKs for future traffic, polls are no longer necessary.
463  *
464  * @param c Connection.
465  * @param fwd Should we stop the poll in the FWD direction?
466  */
467 void
468 GMC_stop_poll (struct MeshConnection *c, int fwd);
469
470 /**
471  * Get a (static) string for a connection.
472  *
473  * @param c Connection.
474  */
475 const char *
476 GMC_2s (struct MeshConnection *c);
477
478 #if 0                           /* keep Emacsens' auto-indent happy */
479 {
480 #endif
481 #ifdef __cplusplus
482 }
483 #endif
484
485 /* ifndef GNUNET_SERVICE_MESH_CONNECTION_H */
486 #endif
487 /* end of gnunet-service-mesh_connection.h */