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