- cleanup
[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 #include "mesh_path.h"
43 #include "gnunet-service-mesh_channel.h"
44 #include "gnunet-service-mesh_peer.h"
45
46
47 /**
48  * All the states a connection can be in.
49  */
50 enum MeshConnectionState
51 {
52   /**
53    * Uninitialized status, should never appear in operation.
54    */
55   MESH_CONNECTION_NEW,
56
57   /**
58    * Connection create message sent, waiting for ACK.
59    */
60   MESH_CONNECTION_SENT,
61
62   /**
63    * Connection ACK sent, waiting for ACK.
64    */
65   MESH_CONNECTION_ACK,
66
67   /**
68    * Connection confirmed, ready to carry traffic.
69    */
70   MESH_CONNECTION_READY,
71 };
72
73
74 /**
75  * Struct containing all information regarding a connection to a peer.
76  */
77 struct MeshConnection;
78
79
80
81
82 /**
83  * Core handler for connection creation.
84  *
85  * @param cls Closure (unused).
86  * @param peer Sender (neighbor).
87  * @param message Message.
88  *
89  * @return GNUNET_OK to keep the connection open,
90  *         GNUNET_SYSERR to close it (signal serious error)
91  */
92 int
93 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
94                    const struct GNUNET_MessageHeader *message);
95
96 /**
97  * Core handler for path confirmations.
98  *
99  * @param cls closure
100  * @param message message
101  * @param peer peer identity this notification is about
102  *
103  * @return GNUNET_OK to keep the connection open,
104  *         GNUNET_SYSERR to close it (signal serious error)
105  */
106 int
107 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
108                     const struct GNUNET_MessageHeader *message);
109
110 /**
111  * Core handler for notifications of broken paths
112  *
113  * @param cls Closure (unused).
114  * @param peer Peer identity of sending neighbor.
115  * @param message Message.
116  *
117  * @return GNUNET_OK to keep the connection open,
118  *         GNUNET_SYSERR to close it (signal serious error)
119  */
120 int
121 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
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 mesh network traffic going orig->dest.
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_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
150                 const struct GNUNET_MessageHeader *message);
151
152
153 /**
154  * Core handler for mesh network traffic going dest->orig.
155  *
156  * @param cls Closure (unused).
157  * @param message Message received.
158  * @param peer Peer who sent the message.
159  *
160  * @return GNUNET_OK to keep the connection open,
161  *         GNUNET_SYSERR to close it (signal serious error)
162  */
163 int
164 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
165                 const struct GNUNET_MessageHeader *message);
166
167 /**
168  * Core handler for mesh network traffic point-to-point acks.
169  *
170  * @param cls closure
171  * @param message message
172  * @param peer peer identity this notification is about
173  *
174  * @return GNUNET_OK to keep the connection open,
175  *         GNUNET_SYSERR to close it (signal serious error)
176  */
177 int
178 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
179                 const struct GNUNET_MessageHeader *message);
180
181 /**
182  * Core handler for mesh network traffic point-to-point ack polls.
183  *
184  * @param cls closure
185  * @param message message
186  * @param peer peer identity this notification is about
187  *
188  * @return GNUNET_OK to keep the connection open,
189  *         GNUNET_SYSERR to close it (signal serious error)
190  */
191 int
192 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
193                  const struct GNUNET_MessageHeader *message);
194
195 /**
196  * Core handler for mesh keepalives.
197  *
198  * @param cls closure
199  * @param message message
200  * @param peer peer identity this notification is about
201  * @return GNUNET_OK to keep the connection open,
202  *         GNUNET_SYSERR to close it (signal serious error)
203  *
204  * TODO: Check who we got this from, to validate route.
205  */
206 int
207 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
208                       const struct GNUNET_MessageHeader *message);
209
210 /**
211  * Initialize the connections subsystem
212  *
213  * @param c Configuration handle.
214  */
215 void
216 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c);
217
218 /**
219  * Shut down the connections subsystem.
220  */
221 void
222 GMC_shutdown (void);
223
224 /**
225  * Create a connection.
226  *
227  * @param cid Connection ID (either created locally or imposed remotely).
228  * @param t Tunnel this connection belongs to (or NULL);
229  * @param p Path this connection has to use.
230  * @param own_pos Own position in the @c p path.
231  */
232 struct MeshConnection *
233 GMC_new (const struct GNUNET_HashCode *cid,
234          struct MeshTunnel3 *t,
235          struct MeshPeerPath *p,
236          unsigned int own_pos);
237
238 /**
239  * Connection is no longer needed: destroy it and remove from tunnel.
240  *
241  * @param c Connection to destroy.
242  */
243 void
244 GMC_destroy (struct MeshConnection *c);
245
246 /**
247  * Get the connection ID.
248  *
249  * @param c Connection to get the ID from.
250  *
251  * @return ID of the connection.
252  */
253 const struct GNUNET_HashCode *
254 GMC_get_id (const struct MeshConnection *c);
255
256 /**
257  * Get the connection state.
258  *
259  * @param c Connection to get the state from.
260  *
261  * @return state of the connection.
262  */
263 enum MeshConnectionState
264 GMC_get_state (const struct MeshConnection *c);
265
266 /**
267  * Get free buffer space in a connection.
268  *
269  * @param c Connection.
270  * @param fwd Is query about FWD traffic?
271  *
272  * @return Free buffer space [0 - max_msgs_queue/max_connections]
273  */
274 unsigned int
275 GMC_get_buffer (struct MeshConnection *c, int fwd);
276
277 /**
278  * Get messages queued in a connection.
279  *
280  * @param c Connection.
281  * @param fwd Is query about FWD traffic?
282  *
283  * @return Number of messages queued.
284  */
285 unsigned int
286 GMC_get_qn (struct MeshConnection *c, int fwd);
287
288 /**
289  * Send FWD keepalive packets for a connection.
290  *
291  * @param cls Closure (connection for which to send the keepalive).
292  * @param tc Notification context.
293  */
294 void
295 GMC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
296
297 /**
298  * Send BCK keepalive packets for a connection.
299  *
300  * @param cls Closure (connection for which to send the keepalive).
301  * @param tc Notification context.
302  */
303 void
304 GMC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
305
306
307 /**
308  * Change the tunnel state.
309  *
310  * @param c Connection whose state to change.
311  * @param state New state.
312  */
313 void
314 GMC_change_state (struct MeshConnection* c, enum MeshConnectionState state);
315
316 /**
317  * Notify other peers on a connection of a broken link. Mark connections
318  * to destroy after all traffic has been sent.
319  *
320  * @param c Connection on which there has been a disconnection.
321  * @param peer Peer that disconnected.
322  * @param my_full_id My ID (to send to other peers).
323  */
324 void
325 GMC_notify_broken (struct MeshConnection *c,
326                    struct MeshPeer *peer,
327                    struct GNUNET_PeerIdentity *my_full_id);
328
329 /**
330  * Is this peer the first one on the connection?
331  *
332  * @param c Connection.
333  * @param fwd Is this about fwd traffic?
334  *
335  * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
336  */
337 int
338 GMC_is_origin (struct MeshConnection *c, int fwd);
339
340 /**
341  * Is this peer the last one on the connection?
342  *
343  * @param c Connection.
344  * @param fwd Is this about fwd traffic?
345  *            Note that the ROOT is the terminal for BCK traffic!
346  *
347  * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
348  */
349 int
350 GMC_is_terminal (struct MeshConnection *c, int fwd);
351
352 /**
353  * Sends an already built message on a connection, properly registering
354  * all used resources.
355  *
356  * @param message Message to send. Function makes a copy of it.
357  *                If message is not hop-by-hop, decrements TTL of copy.
358  * @param c Connection on which this message is transmitted.
359  * @param ch Channel on which this message is transmitted, or NULL.
360  * @param fwd Is this a fwd message?
361  */
362 void
363 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
364                            struct MeshConnection *c,
365                            struct MeshChannel *ch,
366                            int fwd);
367
368 /**
369  * Send a message to all peers in this connection that the connection
370  * is no longer valid.
371  *
372  * If some peer should not receive the message, it should be zero'ed out
373  * before calling this function.
374  *
375  * @param c The connection whose peers to notify.
376  */
377 void
378 GMC_send_destroy (struct MeshConnection *c);
379
380 #if 0                           /* keep Emacsens' auto-indent happy */
381 {
382 #endif
383 #ifdef __cplusplus
384 }
385 #endif
386
387 /* ifndef GNUNET_SERVICE_MESH_CONNECTION_H */
388 #endif
389 /* end of gnunet-service-mesh_connection.h */