- use variables to cound ch, conns
[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    * Connection to be destroyed, just waiting to empty queues.
70    */
71   MESH_CONNECTION_DESTROYED,
72 };
73
74
75 /**
76  * Struct containing all information regarding a connection to a peer.
77  */
78 struct MeshConnection;
79
80 /**
81  * Handle for messages queued but not yet sent.
82  */
83 struct MeshConnectionQueue;
84
85 #include "mesh_path.h"
86 #include "gnunet-service-mesh_channel.h"
87 #include "gnunet-service-mesh_peer.h"
88
89
90
91 /**
92  * Callback called when a queued message is sent.
93  *
94  * @param cls Closure.
95  * @param c Connection this message was on.
96  * @param type Type of message sent.
97  * @param fwd Was this a FWD going message?
98  * @param size Size of the message.
99  */
100 typedef void (*GMC_sent) (void *cls,
101                           struct MeshConnection *c,
102                           struct MeshConnectionQueue *q,
103                           uint16_t type, int fwd, size_t size);
104
105 /**
106  * Core handler for connection creation.
107  *
108  * @param cls Closure (unused).
109  * @param peer Sender (neighbor).
110  * @param message Message.
111  *
112  * @return GNUNET_OK to keep the connection open,
113  *         GNUNET_SYSERR to close it (signal serious error)
114  */
115 int
116 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
117                    const struct GNUNET_MessageHeader *message);
118
119 /**
120  * Core handler for path confirmations.
121  *
122  * @param cls closure
123  * @param message message
124  * @param peer peer identity this notification is about
125  *
126  * @return GNUNET_OK to keep the connection open,
127  *         GNUNET_SYSERR to close it (signal serious error)
128  */
129 int
130 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
131                     const struct GNUNET_MessageHeader *message);
132
133 /**
134  * Core handler for notifications of broken paths
135  *
136  * @param cls Closure (unused).
137  * @param id Peer identity of sending neighbor.
138  * @param message Message.
139  *
140  * @return GNUNET_OK to keep the connection open,
141  *         GNUNET_SYSERR to close it (signal serious error)
142  */
143 int
144 GMC_handle_broken (void* cls,
145                    const struct GNUNET_PeerIdentity* id,
146                    const struct GNUNET_MessageHeader* message);
147
148 /**
149  * Core handler for tunnel destruction
150  *
151  * @param cls Closure (unused).
152  * @param peer Peer identity of sending neighbor.
153  * @param message Message.
154  *
155  * @return GNUNET_OK to keep the connection open,
156  *         GNUNET_SYSERR to close it (signal serious error)
157  */
158 int
159 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
160                     const struct GNUNET_MessageHeader *message);
161
162 /**
163  * Core handler for encrypted mesh network traffic (channel mgmt, data).
164  *
165  * @param cls Closure (unused).
166  * @param message Message received.
167  * @param peer Peer who sent the message.
168  *
169  * @return GNUNET_OK to keep the connection open,
170  *         GNUNET_SYSERR to close it (signal serious error)
171  */
172 int
173 GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
174                       const struct GNUNET_MessageHeader *message);
175
176 /**
177  * Core handler for key exchange traffic (ephemeral key, ping, pong).
178  *
179  * @param cls Closure (unused).
180  * @param message Message received.
181  * @param peer Peer who sent the message.
182  *
183  * @return GNUNET_OK to keep the connection open,
184  *         GNUNET_SYSERR to close it (signal serious error)
185  */
186 int
187 GMC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
188                const struct GNUNET_MessageHeader *message);
189
190 /**
191  * Core handler for mesh network traffic point-to-point acks.
192  *
193  * @param cls closure
194  * @param message message
195  * @param peer peer identity this notification is about
196  *
197  * @return GNUNET_OK to keep the connection open,
198  *         GNUNET_SYSERR to close it (signal serious error)
199  */
200 int
201 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
202                 const struct GNUNET_MessageHeader *message);
203
204 /**
205  * Core handler for mesh network traffic point-to-point ack polls.
206  *
207  * @param cls closure
208  * @param message message
209  * @param peer peer identity this notification is about
210  *
211  * @return GNUNET_OK to keep the connection open,
212  *         GNUNET_SYSERR to close it (signal serious error)
213  */
214 int
215 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
216                  const struct GNUNET_MessageHeader *message);
217
218 /**
219  * Core handler for mesh keepalives.
220  *
221  * @param cls closure
222  * @param message message
223  * @param peer peer identity this notification is about
224  * @return GNUNET_OK to keep the connection open,
225  *         GNUNET_SYSERR to close it (signal serious error)
226  *
227  * TODO: Check who we got this from, to validate route.
228  */
229 int
230 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
231                       const struct GNUNET_MessageHeader *message);
232
233 /**
234  * Send an ACK on the appropriate connection/channel, depending on
235  * the direction and the position of the peer.
236  *
237  * @param c Which connection to send the hop-by-hop ACK.
238  * @param fwd Is this a fwd ACK? (will go dest->root).
239  * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
240  */
241 void
242 GMC_send_ack (struct MeshConnection *c, int fwd, int force);
243
244 /**
245  * Initialize the connections subsystem
246  *
247  * @param c Configuration handle.
248  */
249 void
250 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c);
251
252 /**
253  * Shut down the connections subsystem.
254  */
255 void
256 GMC_shutdown (void);
257
258 /**
259  * Create a connection.
260  *
261  * @param cid Connection ID (either created locally or imposed remotely).
262  * @param t Tunnel this connection belongs to (or NULL);
263  * @param p Path this connection has to use.
264  * @param own_pos Own position in the @c p path.
265  *
266  * @return Newly created connection, NULL in case of error (own id not in path).
267  */
268 struct MeshConnection *
269 GMC_new (const struct GNUNET_HashCode *cid,
270          struct MeshTunnel3 *t,
271          struct MeshPeerPath *p,
272          unsigned int own_pos);
273
274 /**
275  * Connection is no longer needed: destroy it.
276  *
277  * Cancels all pending traffic (including possible DESTROY messages), all
278  * maintenance tasks and removes the connection from neighbor peers and tunnel.
279  *
280  * @param c Connection to destroy.
281  */
282 void
283 GMC_destroy (struct MeshConnection *c);
284
285 /**
286  * Get the connection ID.
287  *
288  * @param c Connection to get the ID from.
289  *
290  * @return ID of the connection.
291  */
292 const struct GNUNET_HashCode *
293 GMC_get_id (const struct MeshConnection *c);
294
295 /**
296  * Get the connection path.
297  *
298  * @param c Connection to get the path from.
299  *
300  * @return path used by the connection.
301  */
302 const struct MeshPeerPath *
303 GMC_get_path (const struct MeshConnection *c);
304
305 /**
306  * Get the connection state.
307  *
308  * @param c Connection to get the state from.
309  *
310  * @return state of the connection.
311  */
312 enum MeshConnectionState
313 GMC_get_state (const struct MeshConnection *c);
314
315 /**
316  * Get the connection tunnel.
317  *
318  * @param c Connection to get the tunnel from.
319  *
320  * @return tunnel of the connection.
321  */
322 struct MeshTunnel3 *
323 GMC_get_tunnel (const struct MeshConnection *c);
324
325 /**
326  * Get free buffer space in a connection.
327  *
328  * @param c Connection.
329  * @param fwd Is query about FWD traffic?
330  *
331  * @return Free buffer space [0 - max_msgs_queue/max_connections]
332  */
333 unsigned int
334 GMC_get_buffer (struct MeshConnection *c, int fwd);
335
336 /**
337  * Get how many messages have we allowed to send to us from a direction.
338  *
339  * @param c Connection.
340  * @param fwd Are we asking about traffic from FWD (BCK messages)?
341  *
342  * @return last_ack_sent - last_pid_recv
343  */
344 unsigned int
345 GMC_get_allowed (struct MeshConnection *c, int fwd);
346
347 /**
348  * Get messages queued in a connection.
349  *
350  * @param c Connection.
351  * @param fwd Is query about FWD traffic?
352  *
353  * @return Number of messages queued.
354  */
355 unsigned int
356 GMC_get_qn (struct MeshConnection *c, int fwd);
357
358 /**
359  * Allow the connection to advertise a buffer of the given size.
360  *
361  * The connection will send an @c fwd ACK message (so: in direction !fwd)
362  * allowing up to last_pid_recv + buffer.
363  *
364  * @param c Connection.
365  * @param buffer How many more messages the connection can accept.
366  * @param fwd Is this about FWD traffic? (The ack will go dest->root).
367  */
368 void
369 GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd);
370
371 /**
372  * Send FWD keepalive packets for a connection.
373  *
374  * @param cls Closure (connection for which to send the keepalive).
375  * @param tc Notification context.
376  */
377 void
378 GMC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
379
380 /**
381  * Send BCK keepalive packets for a connection.
382  *
383  * @param cls Closure (connection for which to send the keepalive).
384  * @param tc Notification context.
385  */
386 void
387 GMC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
388
389
390 /**
391  * Notify other peers on a connection of a broken link. Mark connections
392  * to destroy after all traffic has been sent.
393  *
394  * @param c Connection on which there has been a disconnection.
395  * @param peer Peer that disconnected.
396  */
397 void
398 GMC_notify_broken (struct MeshConnection *c,
399                    struct MeshPeer *peer);
400
401 /**
402  * Is this peer the first one on the connection?
403  *
404  * @param c Connection.
405  * @param fwd Is this about fwd traffic?
406  *
407  * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
408  */
409 int
410 GMC_is_origin (struct MeshConnection *c, int fwd);
411
412 /**
413  * Is this peer the last one on the connection?
414  *
415  * @param c Connection.
416  * @param fwd Is this about fwd traffic?
417  *            Note that the ROOT is the terminal for BCK traffic!
418  *
419  * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
420  */
421 int
422 GMC_is_terminal (struct MeshConnection *c, int fwd);
423
424 /**
425  * See if we are allowed to send by the next hop in the given direction.
426  *
427  * @param c Connection.
428  * @param fwd Is this about fwd traffic?
429  *
430  * @return #GNUNET_YES in case it's OK to send.
431  */
432 int
433 GMC_is_sendable (struct MeshConnection *c, int fwd);
434
435 /**
436  * Cancel a previously sent message while it's in the queue.
437  *
438  * ONLY can be called before the continuation given to the send function
439  * is called. Once the continuation is called, the message is no longer in the
440  * queue.
441  *
442  * @param q Handle to the queue.
443  */
444 void
445 GMC_cancel (struct MeshConnectionQueue *q);
446
447 /**
448  * Sends an already built message on a connection, properly registering
449  * all used resources.
450  *
451  * @param message Message to send. Function makes a copy of it.
452  *                If message is not hop-by-hop, decrements TTL of copy.
453  * @param c Connection on which this message is transmitted.
454  * @param fwd Is this a fwd message?
455  * @param force Force the connection to accept the message (buffer overfill).
456  * @param cont Continuation called once message is sent. Can be NULL.
457  * @param cont_cls Closure for @c cont.
458  *
459  * @return Handle to cancel the message before it's sent.
460  *         NULL on error or if @c cont is NULL.
461  *         Invalid on @c cont call.
462  */
463 struct MeshConnectionQueue *
464 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
465                            struct MeshConnection *c, int fwd, int force,
466                            GMC_sent cont, void *cont_cls);
467
468 /**
469  * Sends a CREATE CONNECTION message for a path to a peer.
470  * Changes the connection and tunnel states if necessary.
471  *
472  * @param connection Connection to create.
473  */
474 void
475 GMC_send_create (struct MeshConnection *connection);
476
477 /**
478  * Send a message to all peers in this connection that the connection
479  * is no longer valid.
480  *
481  * If some peer should not receive the message, it should be zero'ed out
482  * before calling this function.
483  *
484  * @param c The connection whose peers to notify.
485  */
486 void
487 GMC_send_destroy (struct MeshConnection *c);
488
489 /**
490  * @brief Start a polling timer for the connection.
491  *
492  * When a neighbor does not accept more traffic on the connection it could be
493  * caused by a simple congestion or by a lost ACK. Polling enables to check
494  * for the lastest ACK status for a connection.
495  *
496  * @param c Connection.
497  * @param fwd Should we poll in the FWD direction?
498  */
499 void
500 GMC_start_poll (struct MeshConnection *c, int fwd);
501
502
503 /**
504  * @brief Stop polling a connection for ACKs.
505  *
506  * Once we have enough ACKs for future traffic, polls are no longer necessary.
507  *
508  * @param c Connection.
509  * @param fwd Should we stop the poll in the FWD direction?
510  */
511 void
512 GMC_stop_poll (struct MeshConnection *c, int fwd);
513
514 /**
515  * Get a (static) string for a connection.
516  *
517  * @param c Connection.
518  */
519 const char *
520 GMC_2s (const struct MeshConnection *c);
521
522 #if 0                           /* keep Emacsens' auto-indent happy */
523 {
524 #endif
525 #ifdef __cplusplus
526 }
527 #endif
528
529 /* ifndef GNUNET_SERVICE_MESH_CONNECTION_H */
530 #endif
531 /* end of gnunet-service-mesh_connection.h */