44601754a65f8aa52e4304b3170f6f724d823350
[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
29 #ifdef __cplusplus
30 extern "C"
31 {
32 #if 0                           /* keep Emacsens' auto-indent happy */
33 }
34 #endif
35 #endif
36
37 #include <stdint.h>
38
39 #define MESH_DEBUG              GNUNET_YES
40
41 #include "platform.h"
42 #include "gnunet_common.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet_peer_lib.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_protocols.h"
47 #include <gnunet_mesh_service.h>
48
49 /******************************************************************************/
50 /********************        MESH LOCAL MESSAGES      *************************/
51 /******************************************************************************/
52 /*  Any API call should be documented in the folowing table under API CALL.
53  *  Also, any message type should be documented in the following table, with the
54  * associated event.
55  *
56  * API CALL (GNUNET_MESH_*)             MESSAGE USED
57  * ------------------------             ------------
58  * connect                              GNUNET_MESH_ClientConnect
59  * disconnect                           None (network level disconnect)
60  *
61  * tunnel_create                        GNUNET_MESH_TunnelMessage
62  * tunnel_destroy                       GNUNET_MESH_TunnelMessage
63  * tunnel_buffer                        GNUNET_MESH_TunnelMessage
64  *
65  * notify_transmit_ready                None (queue / GNUNET_CLIENT_ntf_tmt_rdy)
66  * notify_transmit_ready_cancel         None (clear of internal data structures)
67  *
68  * 
69  * EVENT                                MESSAGE USED
70  * -----                                ------------
71  * data                                 GNUNET_MESH_Unicast OR
72  *                                      GNUNET_MESH_Multicast OR
73  *                                      GNUNET_MESH_ToOrigin
74  * data ack                             GNUNET_MESH_LocalAck
75  * 
76  * new incoming tunnel                  GNUNET_MESH_PeerControl
77  * peer connects to a tunnel            FIXME
78  * peer disconnects from a tunnel       FIXME
79  */
80
81 /******************************************************************************/
82 /**************************       CONSTANTS      ******************************/
83 /******************************************************************************/
84
85 #define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI         0x80000000
86 #define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV        0xB0000000
87
88 #define HIGH_PID                                0xFFFF0000
89 #define LOW_PID                                 0x0000FFFF
90
91 /**
92  * Value in tunnel info: *int (GNUNET_YES/GNUNET_NO)
93  */
94 typedef enum
95 {
96   /**
97    * Disable buffering on intermediate nodes (for minimum latency).
98    */
99   GNUNET_MESH_OPTION_NOBUFFER   = 0x1,
100
101   /**
102    * Enable tunnel reliability, lost messages will be retransmitted.
103    */
104   GNUNET_MESH_OPTION_RELIABLE   = 0x2,
105
106   /**
107    * Enable out of order delivery of messages.
108    */
109   GNUNET_MESH_OPTION_OOORDER    = 0x4
110
111 } MeshTunnelOption;
112
113 #define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
114
115 /******************************************************************************/
116 /**************************        MESSAGES      ******************************/
117 /******************************************************************************/
118
119 GNUNET_NETWORK_STRUCT_BEGIN
120
121 /**
122  * Message for a client to register to the service
123  */
124 struct GNUNET_MESH_ClientConnect
125 {
126     /**
127      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
128      *
129      * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
130      *       sizeof(MESH_ApplicationType) * applications +
131      *       sizeof(uint16_t) * types
132      */
133   struct GNUNET_MessageHeader header;
134   /* uint32_t                 list_ports[]           */
135 };
136
137
138 /**
139  * Type for tunnel numbering.
140  * - Local tunnel numbers given by the service (incoming) are >= 0xB0000000
141  * - Local tunnel numbers given by the client (created) are >= 0x80000000
142  * - Global tunnel numbers are < 0x80000000
143  */
144 typedef uint32_t MESH_TunnelNumber;
145
146
147 /**
148  * Message for a client to create and destroy tunnels.
149  */
150 struct GNUNET_MESH_TunnelMessage
151 {
152     /**
153      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
154      *
155      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
156      */
157   struct GNUNET_MessageHeader header;
158
159     /**
160      * ID of a tunnel controlled by this client.
161      */
162   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
163
164     /**
165      * Tunnel's peer
166      */
167   struct GNUNET_PeerIdentity peer;
168
169     /**
170      * Port of the tunnel.
171      */
172   uint32_t port GNUNET_PACKED;
173
174     /**
175      * Options.
176      */
177   int32_t options GNUNET_PACKED;
178 };
179
180
181 /**
182  * Message for the service to let a client know about created tunnels.
183  */
184 struct GNUNET_MESH_TunnelNotification
185 {
186     /**
187      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
188      *
189      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
190      */
191   struct GNUNET_MessageHeader header;
192
193     /**
194      * ID of a tunnel controlled by this client.
195      */
196   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
197
198     /**
199      * Peer at the other end.
200      */
201   struct GNUNET_PeerIdentity peer;
202
203     /**
204      * Port for this tunnel
205      */
206   uint32_t port GNUNET_PACKED;
207
208     /**
209      * Tunnel options (speed, buffering)
210      */
211   uint32_t opt GNUNET_PACKED;
212 };
213
214
215 /**
216  * Message for mesh data traffic.
217  */
218 struct GNUNET_MESH_LocalData
219 {
220     /**
221      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
222      */
223   struct GNUNET_MessageHeader header;
224
225     /**
226      * TID of the tunnel
227      */
228   uint32_t tid GNUNET_PACKED;
229
230     /**
231      * Payload follows
232      */
233 };
234
235
236 /**
237  * Message to allow the client send more data to the service
238  * (always service -> client).
239  */
240 struct GNUNET_MESH_LocalAck
241 {
242     /**
243      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
244      */
245   struct GNUNET_MessageHeader header;
246
247     /**
248      * ID of the tunnel allowed to send more data.
249      */
250   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
251
252 };
253
254
255 /**
256  * Message to inform the client about tunnels in the service.
257  */
258 struct GNUNET_MESH_LocalMonitor
259 {
260   /**
261      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR[_TUNNEL]
262    */
263   struct GNUNET_MessageHeader header;
264
265   /**
266    * ID of the tunnel allowed to send more data.
267    */
268   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
269
270   /**
271    * Alignment.
272    */
273   uint32_t reserved GNUNET_PACKED;
274
275   /**
276    * ID of the owner of the tunnel (can be local peer).
277    */
278   struct GNUNET_PeerIdentity owner;
279
280   /**
281    * ID of the destination of the tunnel (can be local peer).
282    */
283   struct GNUNET_PeerIdentity destination;
284 };
285
286
287 GNUNET_NETWORK_STRUCT_END
288
289 /******************************************************************************/
290 /************************        ENUMERATIONS      ****************************/
291 /******************************************************************************/
292
293 /**
294  * All the states a tunnel can be in.
295  */
296 enum MeshTunnelState
297 {
298     /**
299      * Uninitialized status, should never appear in operation.
300      */
301   MESH_TUNNEL_NEW,
302
303     /**
304      * Path to the peer not known yet
305      */
306   MESH_TUNNEL_SEARCHING,
307
308     /**
309      * Request sent, not yet answered.
310      */
311   MESH_TUNNEL_WAITING,
312
313     /**
314      * Peer connected and ready to accept data
315      */
316   MESH_TUNNEL_READY,
317
318     /**
319      * Peer connected previosly but not responding
320      */
321   MESH_TUNNEL_RECONNECTING
322 };
323
324
325 /**
326  * Check if one pid is bigger than other, accounting for overflow.
327  *
328  * @param bigger Argument that should be bigger.
329  * @param smaller Argument that should be smaller.
330  *
331  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
332  */
333 int
334 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
335
336
337 /**
338  * Get the higher ACK value out of two values, taking in account overflow.
339  *
340  * @param a First ACK value.
341  * @param b Second ACK value.
342  *
343  * @return Highest ACK value from the two.
344  */
345 uint32_t
346 GMC_max_pid (uint32_t a, uint32_t b);
347
348
349 /**
350  * Get the lower ACK value out of two values, taking in account overflow.
351  *
352  * @param a First ACK value.
353  * @param b Second ACK value.
354  *
355  * @return Lowest ACK value from the two.
356  */
357 uint32_t
358 GMC_min_pid (uint32_t a, uint32_t b);
359
360
361 /**
362  * Convert a message type into a string to help debug
363  * Generated with:
364  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
365  * REPLACE:     "    case \2: return "\1"; break;"
366  * 
367  * @param m Message type.
368  * 
369  * @return Human readable string description.
370  */
371 const char *
372 GNUNET_MESH_DEBUG_M2S (uint16_t m);
373
374 #if 0                           /* keep Emacsens' auto-indent happy */
375 {
376 #endif
377 #ifdef __cplusplus
378 }
379 #endif
380
381 #endif