- start test only after warmup
[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_peer_lib.h"
44 #include "gnunet_core_service.h"
45 #include "gnunet_protocols.h"
46 #include <gnunet_mesh_service.h>
47
48 /******************************************************************************/
49 /**************************       CONSTANTS      ******************************/
50 /******************************************************************************/
51
52 #define GNUNET_MESH_LOCAL_CHANNEL_ID_CLI        0x80000000
53 #define GNUNET_MESH_LOCAL_CHANNEL_ID_SERV       0xB0000000
54
55 #define HIGH_PID                                0xFFFF0000
56 #define LOW_PID                                 0x0000FFFF
57
58 #define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
59
60 /******************************************************************************/
61 /**************************        MESSAGES      ******************************/
62 /******************************************************************************/
63
64 GNUNET_NETWORK_STRUCT_BEGIN
65
66 /**
67  * Message for a client to register to the service
68  */
69 struct GNUNET_MESH_ClientConnect
70 {
71     /**
72      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
73      *
74      * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
75      *       sizeof(MESH_ApplicationType) * applications +
76      *       sizeof(uint16_t) * types
77      */
78   struct GNUNET_MessageHeader header;
79   /* uint32_t                 list_ports[]           */
80 };
81
82
83 /**
84  * Type for channel numbering.
85  * - Local channel numbers given by the service (incoming) are >= 0xB0000000
86  * - Local channel numbers given by the client (created) are >= 0x80000000
87  * - Global channel numbers are < 0x80000000
88  */
89 typedef uint32_t MESH_ChannelNumber;
90
91
92 /**
93  * Hash to be used in Mesh communication. Only 256 bits needed,
94  * instead of the 512 from @c GNUNET_HashCode.
95  *
96  */
97 struct GNUNET_MeshHash
98 {
99   unsigned char bits[256 / 8];
100 };
101
102
103 /**
104  * Message for a client to create and destroy channels.
105  */
106 struct GNUNET_MESH_ChannelMessage
107 {
108     /**
109      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
110      *
111      * Size: sizeof(struct GNUNET_MESH_ChannelMessage)
112      */
113   struct GNUNET_MessageHeader header;
114
115     /**
116      * ID of a channel controlled by this client.
117      */
118   MESH_ChannelNumber channel_id GNUNET_PACKED;
119
120     /**
121      * Channel's peer
122      */
123   struct GNUNET_PeerIdentity peer;
124
125     /**
126      * Port of the channel.
127      */
128   uint32_t port GNUNET_PACKED;
129
130     /**
131      * Options.
132      */
133   uint32_t opt GNUNET_PACKED;
134 };
135
136
137 /**
138  * Message for mesh data traffic.
139  */
140 struct GNUNET_MESH_LocalData
141 {
142     /**
143      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
144      */
145   struct GNUNET_MessageHeader header;
146
147     /**
148      * ID of the channel
149      */
150   uint32_t id GNUNET_PACKED;
151
152     /**
153      * Payload follows
154      */
155 };
156
157
158 /**
159  * Message to allow the client send more data to the service
160  * (always service -> client).
161  */
162 struct GNUNET_MESH_LocalAck
163 {
164     /**
165      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
166      */
167   struct GNUNET_MessageHeader header;
168
169     /**
170      * ID of the channel allowed to send more data.
171      */
172   MESH_ChannelNumber channel_id GNUNET_PACKED;
173
174 };
175
176
177 /**
178  * Message to inform the client about channels in the service.
179  */
180 struct GNUNET_MESH_LocalInfo
181 {
182   /**
183      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO[_TUNNEL,_PEER]
184    */
185   struct GNUNET_MessageHeader header;
186
187   /**
188    * ID of the channel allowed to send more data.
189    */
190   MESH_ChannelNumber channel_id GNUNET_PACKED;
191
192   /**
193    * ID of the owner of the channel (can be local peer).
194    */
195 //   struct GNUNET_PeerIdentity owner;
196
197   /**
198    * ID of the destination of the channel (can be local peer).
199    */
200   struct GNUNET_PeerIdentity peer;
201 };
202
203
204 /**
205  * Message to inform the client about one of the peers in the service.
206  */
207 struct GNUNET_MESH_LocalInfoPeer
208 {
209   /**
210    * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEER[S]
211    */
212   struct GNUNET_MessageHeader header;
213
214   /**
215    * Number of paths.
216    */
217   uint16_t paths GNUNET_PACKED;
218
219   /**
220    * Do we have a tunnel toward this peer?
221    */
222   int16_t tunnel GNUNET_PACKED;
223
224   /**
225    * ID of the destination of the tunnel (can be local peer).
226    */
227   struct GNUNET_PeerIdentity destination;
228
229   /* If type == PEER (no 'S'): GNUNET_PeerIdentity paths[]
230    * (each path ends in destination) */
231 };
232
233 /**
234  * Message to inform the client about one of the tunnels in the service.
235  */
236 struct GNUNET_MESH_LocalInfoTunnel
237 {
238   /**
239    * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL[S]
240    */
241   struct GNUNET_MessageHeader header;
242
243   /**
244    * Number of channels.
245    */
246   uint32_t channels GNUNET_PACKED;
247
248   /**
249    * ID of the destination of the tunnel (can be local peer).
250    */
251   struct GNUNET_PeerIdentity destination;
252
253   /**
254    * Number of connections.
255    */
256   uint32_t connections GNUNET_PACKED;
257
258   /**
259    * Encryption state.
260    */
261   uint16_t estate GNUNET_PACKED;
262
263   /**
264    * Connection state.
265    */
266   uint16_t cstate GNUNET_PACKED;
267
268   /* If TUNNEL (no 'S'): GNUNET_PeerIdentity connection_ids[connections] */
269   /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
270 };
271
272
273 GNUNET_NETWORK_STRUCT_END
274
275
276
277 /**
278  * @brief Translate a fwd variable into a string representation, for logging.
279  *
280  * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
281  *
282  * @return String representing FWD or BCK.
283  */
284 char *
285 GM_f2s (int fwd);
286
287
288 /**
289  * Check if one pid is bigger than other, accounting for overflow.
290  *
291  * @param bigger Argument that should be bigger.
292  * @param smaller Argument that should be smaller.
293  *
294  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
295  */
296 int
297 GM_is_pid_bigger (uint32_t bigger, uint32_t smaller);
298
299
300 /**
301  * Get the higher ACK value out of two values, taking in account overflow.
302  *
303  * @param a First ACK value.
304  * @param b Second ACK value.
305  *
306  * @return Highest ACK value from the two.
307  */
308 uint32_t
309 GM_max_pid (uint32_t a, uint32_t b);
310
311
312 /**
313  * Get the lower ACK value out of two values, taking in account overflow.
314  *
315  * @param a First ACK value.
316  * @param b Second ACK value.
317  *
318  * @return Lowest ACK value from the two.
319  */
320 uint32_t
321 GM_min_pid (uint32_t a, uint32_t b);
322
323
324 /**
325  * Convert a 256 bit MeshHash into a 512 HashCode to use in GNUNET_h2s,
326  * multihashmap, and other HashCode-based functions.
327  *
328  * @param id A 256 bit hash to expand.
329  *
330  * @return A HashCode containing the original 256 bit hash right-padded with 0.
331  */
332 const struct GNUNET_HashCode *
333 GM_h2hc (const struct GNUNET_MeshHash *id);
334
335 /**
336  * Convert a message type into a string to help debug
337  * Generated with:
338  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
339  * REPLACE:     "    case \2: return "\1"; break;"
340  *
341  * @param m Message type.
342  *
343  * @return Human readable string description.
344  */
345 const char *
346 GM_m2s (uint16_t m);
347
348 #if 0                           /* keep Emacsens' auto-indent happy */
349 {
350 #endif
351 #ifdef __cplusplus
352 }
353 #endif
354
355 #endif