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