- Update Mesh Client <-> Service API documentation
[oweals/gnunet.git] / src / mesh / mesh.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001 - 2011 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  * @author Bartlomiej Polot
23  * @file mesh/mesh.h
24  */
25
26 #ifndef MESH_H_
27 #define MESH_H_
28 #include <stdint.h>
29
30 #define MESH_DEBUG              GNUNET_YES
31
32 #define INITIAL_WINDOW_SIZE     8
33 #define ACK_THRESHOLD           INITIAL_WINDOW_SIZE / 2
34
35 #include "platform.h"
36 #include "gnunet_common.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_peer_lib.h"
39 #include "gnunet_core_service.h"
40 #include "gnunet_protocols.h"
41 #include <gnunet_mesh_service.h>
42
43 /******************************************************************************/
44 /********************        MESH LOCAL MESSAGES      *************************/
45 /******************************************************************************/
46 /*  Any API call should be documented in the folowing table under API CALL.
47  *  Also, any message type should be documented in the following table, with the
48  * associated event.
49  *
50  * API CALL (GNUNET_MESH_*)             MESSAGE USED
51  * ------------------------             ------------
52  * connect                              GNUNET_MESH_ClientConnect
53  * disconnect                           None (network level disconnect)
54  *
55  * tunnel_create                        GNUNET_MESH_TunnelMessage
56  * tunnel_destroy                       GNUNET_MESH_TunnelMessage
57  * tunnel_speed_max                     GNUNET_MESH_TunnelMessage
58  * tunnel_speed_min                     GNUNET_MESH_TunnelMessage
59  * tunnel_buffer                        GNUNET_MESH_TunnelMessage
60  *
61  * peer_request_connect_add             GNUNET_MESH_PeerControl
62  * peer_request_connect_del             GNUNET_MESH_PeerControl
63  * peer_request_connect_by_type         GNUNET_MESH_ConnectPeerByType
64  * peer_request_connect_by_string       GNUNET_MESH_ConnectPeerByString
65  * 
66  * peer_blacklist                       GNUNET_MESH_PeerControl
67  * peer_unblacklist                     GNUNET_MESH_PeerControl
68  *
69  * notify_transmit_ready                None (queue / GNUNET_CLIENT_ntf_tmt_rdy)
70  * notify_transmit_ready_cancel         None (clear of internal data structures)
71  *
72  * 
73  * EVENT                                MESSAGE USED
74  * -----                                ------------
75  * data                                 GNUNET_MESH_Unicast OR
76  *                                      GNUNET_MESH_Multicast OR
77  *                                      GNUNET_MESH_ToOrigin
78  * data ack                             GNUNET_MESH_LocalAck
79  * 
80  * new incoming tunnel                  GNUNET_MESH_PeerControl
81  * peer connects to a tunnel            GNUNET_MESH_PeerControl
82  * peer disconnects from a tunnel       GNUNET_MESH_PeerControl
83  */
84
85 /******************************************************************************/
86 /**************************       CONSTANTS      ******************************/
87 /******************************************************************************/
88
89 #define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI         0x80000000
90 #define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV        0xB0000000
91
92 #define HIGH_PID                                0xFFFF0000
93 #define LOW_PID                                 0x0000FFFF
94
95 #define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
96
97 /******************************************************************************/
98 /**************************        MESSAGES      ******************************/
99 /******************************************************************************/
100
101 GNUNET_NETWORK_STRUCT_BEGIN
102
103 /**
104  * Message for a client to register to the service
105  */
106 struct GNUNET_MESH_ClientConnect
107 {
108     /**
109      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
110      *
111      * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
112      *       sizeof(MESH_ApplicationType) * applications +
113      *       sizeof(uint16_t) * types
114      */
115   struct GNUNET_MessageHeader header;
116   uint16_t applications GNUNET_PACKED;
117   uint16_t types GNUNET_PACKED;
118   /* uint32_t                 list_apps[applications]     */
119   /* uint16_t                 list_types[types]           */
120 };
121
122
123 /**
124  * Type for tunnel numbering.
125  * - Local tunnel numbers given by the service (incoming) are >= 0xB0000000
126  * - Local tunnel numbers given by the client (created) are >= 0x80000000
127  * - Global tunnel numbers are < 0x80000000
128  */
129 typedef uint32_t MESH_TunnelNumber;
130
131 /**
132  * Message for a client to create and destroy tunnels.
133  */
134 struct GNUNET_MESH_TunnelMessage
135 {
136     /**
137      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
138      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[MAX|MIN]
139      *
140      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
141      */
142   struct GNUNET_MessageHeader header;
143
144     /**
145      * ID of a tunnel controlled by this client.
146      */
147   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
148 };
149
150
151 /**
152  * Message for the service to let a client know about created tunnels.
153  */
154 struct GNUNET_MESH_TunnelNotification
155 {
156     /**
157      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
158      *
159      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
160      */
161   struct GNUNET_MessageHeader header;
162
163     /**
164      * ID of a tunnel controlled by this client.
165      */
166   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
167
168     /**
169      * Peer at the other end, if any
170      */
171   struct GNUNET_PeerIdentity peer;
172 };
173
174 /**
175  * Message for:
176  * - request adding and deleting peers from a tunnel
177  * - notify the client that peers have connected:
178  *   -- requested
179  *   -- unrequested (new incoming tunnels)
180  * - notify the client that peers have disconnected
181  */
182 struct GNUNET_MESH_PeerControl
183 {
184
185     /**
186      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL|[UN]BLACKLIST]
187      *       (client to service, client created tunnel)
188      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
189      *       (service to client)
190      *
191      * Size: sizeof(struct GNUNET_MESH_PeerControl)
192      */
193   struct GNUNET_MessageHeader header;
194
195     /**
196      * ID of a tunnel controlled by this client.
197      */
198   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
199
200     /**
201      * Peer to connect/disconnect.
202      */
203   struct GNUNET_PeerIdentity peer;
204 };
205
206
207 /**
208  * Message for connecting to peers offering a service, by service number.
209  */
210 struct GNUNET_MESH_ConnectPeerByType
211 {
212     /**
213      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
214      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
215      * 
216      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByType)
217      */
218   struct GNUNET_MessageHeader header;
219
220     /**
221      * ID of a tunnel controlled by this client.
222      */
223   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
224
225     /**
226      * Type specification
227      */
228   GNUNET_MESH_ApplicationType type GNUNET_PACKED;
229 };
230
231
232 /**
233  * Message for connecting to peers offering a service, by service string.
234  */
235 struct GNUNET_MESH_ConnectPeerByString
236 {
237     /**
238      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING
239      * 
240      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByString) + strlen (string)
241      */
242   struct GNUNET_MessageHeader header;
243
244     /**
245      * ID of a tunnel controlled by this client.
246      */
247   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
248
249   /* String describing the service */
250 };
251
252
253 /**
254  * Message to allow the client send more data to the service
255  * (always service -> client).
256  */
257 struct GNUNET_MESH_LocalAck
258 {
259     /**
260      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
261      */
262   struct GNUNET_MessageHeader header;
263
264     /**
265      * ID of the tunnel allowed to send more data.
266      */
267   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
268
269     /**
270      * ID of the last packet allowed.
271      */
272   uint32_t max_pid GNUNET_PACKED;
273 };
274
275
276 GNUNET_NETWORK_STRUCT_END
277
278 /******************************************************************************/
279 /************************        ENUMERATIONS      ****************************/
280 /******************************************************************************/
281
282 /**
283  * All the states a peer participating in a tunnel can be in.
284  */
285 enum MeshPeerState
286 {
287     /**
288      * Uninitialized status, should never appear in operation.
289      */
290   MESH_PEER_INVALID,
291
292     /**
293      * Peer is the root and owner of the tree
294      */
295   MESH_PEER_ROOT,
296
297     /**
298      * Peer only retransmits traffic, is not a final destination
299      */
300   MESH_PEER_RELAY,
301
302     /**
303      * Path to the peer not known yet
304      */
305   MESH_PEER_SEARCHING,
306
307     /**
308      * Request sent, not yet answered.
309      */
310   MESH_PEER_WAITING,
311
312     /**
313      * Peer connected and ready to accept data
314      */
315   MESH_PEER_READY,
316
317     /**
318      * Peer connected previosly but not responding
319      */
320   MESH_PEER_RECONNECTING
321 };
322
323
324 /**
325  * Check if one pid is bigger than other, accounting for overflow.
326  *
327  * @param bigger Argument that should be bigger.
328  * @param smaller Argument that should be smaller.
329  *
330  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
331  */
332 int
333 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
334
335
336 /**
337  * Get the higher ACK value out of two values, taking in account overflow.
338  *
339  * @param a First ACK value.
340  * @param b Second ACK value.
341  *
342  * @return Highest ACK value from the two.
343  */
344 uint32_t
345 GMC_max_pid (uint32_t a, uint32_t b);
346
347
348 /**
349  * Get the lower ACK value out of two values, taking in account overflow.
350  *
351  * @param a First ACK value.
352  * @param b Second ACK value.
353  *
354  * @return Lowest ACK value from the two.
355  */
356 uint32_t
357 GMC_min_pid (uint32_t a, uint32_t b);
358
359
360 /**
361  * Convert a message type into a string to help debug
362  * Generated with:
363  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
364  * REPLACE:     "    case \2: return "\1"; break;"
365  * 
366  * @param m Message type.
367  * 
368  * @return Human readable string description.
369  */
370 const char *
371 GNUNET_MESH_DEBUG_M2S (uint16_t m);
372
373 #endif