- arm doesn't have time to finish in 30s
[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      * Tunnel options (speed, buffering)
175      */
176   uint32_t opt;
177 };
178
179 /**
180  * Message for:
181  * - request adding and deleting peers from a tunnel
182  * - notify the client that peers have connected:
183  *   -- requested
184  *   -- unrequested (new incoming tunnels)
185  * - notify the client that peers have disconnected
186  */
187 struct GNUNET_MESH_PeerControl
188 {
189
190     /**
191      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL|[UN]BLACKLIST]
192      *       (client to service, client created tunnel)
193      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
194      *       (service to client)
195      *
196      * Size: sizeof(struct GNUNET_MESH_PeerControl)
197      */
198   struct GNUNET_MessageHeader header;
199
200     /**
201      * ID of a tunnel controlled by this client.
202      */
203   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
204
205     /**
206      * Peer to connect/disconnect.
207      */
208   struct GNUNET_PeerIdentity peer;
209 };
210
211
212 /**
213  * Message for connecting to peers offering a service, by service number.
214  */
215 struct GNUNET_MESH_ConnectPeerByType
216 {
217     /**
218      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
219      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
220      * 
221      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByType)
222      */
223   struct GNUNET_MessageHeader header;
224
225     /**
226      * ID of a tunnel controlled by this client.
227      */
228   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
229
230     /**
231      * Type specification
232      */
233   GNUNET_MESH_ApplicationType type GNUNET_PACKED;
234 };
235
236
237 /**
238  * Message for connecting to peers offering a service, by service string.
239  */
240 struct GNUNET_MESH_ConnectPeerByString
241 {
242     /**
243      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING
244      * 
245      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByString) + strlen (string)
246      */
247   struct GNUNET_MessageHeader header;
248
249     /**
250      * ID of a tunnel controlled by this client.
251      */
252   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
253
254   /* String describing the service */
255 };
256
257
258 /**
259  * Message to allow the client send more data to the service
260  * (always service -> client).
261  */
262 struct GNUNET_MESH_LocalAck
263 {
264     /**
265      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
266      */
267   struct GNUNET_MessageHeader header;
268
269     /**
270      * ID of the tunnel allowed to send more data.
271      */
272   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
273
274     /**
275      * ID of the last packet allowed.
276      */
277   uint32_t max_pid GNUNET_PACKED;
278 };
279
280
281 GNUNET_NETWORK_STRUCT_END
282
283 /******************************************************************************/
284 /************************        ENUMERATIONS      ****************************/
285 /******************************************************************************/
286
287 /**
288  * All the states a peer participating in a tunnel can be in.
289  */
290 enum MeshPeerState
291 {
292     /**
293      * Uninitialized status, should never appear in operation.
294      */
295   MESH_PEER_INVALID,
296
297     /**
298      * Peer is the root and owner of the tree
299      */
300   MESH_PEER_ROOT,
301
302     /**
303      * Peer only retransmits traffic, is not a final destination
304      */
305   MESH_PEER_RELAY,
306
307     /**
308      * Path to the peer not known yet
309      */
310   MESH_PEER_SEARCHING,
311
312     /**
313      * Request sent, not yet answered.
314      */
315   MESH_PEER_WAITING,
316
317     /**
318      * Peer connected and ready to accept data
319      */
320   MESH_PEER_READY,
321
322     /**
323      * Peer connected previosly but not responding
324      */
325   MESH_PEER_RECONNECTING
326 };
327
328
329 /**
330  * Check if one pid is bigger than other, accounting for overflow.
331  *
332  * @param bigger Argument that should be bigger.
333  * @param smaller Argument that should be smaller.
334  *
335  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
336  */
337 int
338 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
339
340
341 /**
342  * Get the higher ACK value out of two values, taking in account overflow.
343  *
344  * @param a First ACK value.
345  * @param b Second ACK value.
346  *
347  * @return Highest ACK value from the two.
348  */
349 uint32_t
350 GMC_max_pid (uint32_t a, uint32_t b);
351
352
353 /**
354  * Get the lower ACK value out of two values, taking in account overflow.
355  *
356  * @param a First ACK value.
357  * @param b Second ACK value.
358  *
359  * @return Lowest ACK value from the two.
360  */
361 uint32_t
362 GMC_min_pid (uint32_t a, uint32_t b);
363
364
365 /**
366  * Convert a message type into a string to help debug
367  * Generated with:
368  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
369  * REPLACE:     "    case \2: return "\1"; break;"
370  * 
371  * @param m Message type.
372  * 
373  * @return Human readable string description.
374  */
375 const char *
376 GNUNET_MESH_DEBUG_M2S (uint16_t m);
377
378 #endif