Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / cadet / cadet.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001 - 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @author Bartlomiej Polot
21  * @file cadet/cadet.h
22  */
23
24 #ifndef CADET_H_
25 #define CADET_H_
26
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #if 0                           /* keep Emacsens' auto-indent happy */
31 }
32 #endif
33 #endif
34
35 #include <stdint.h>
36
37 #if !defined(GNUNET_CULL_LOGGING)
38   #define CADET_TIMING_START \
39           struct GNUNET_TIME_Absolute __timestamp;\
40           __timestamp = GNUNET_TIME_absolute_get()
41
42   #define CADET_TIMING_END   \
43           struct GNUNET_TIME_Relative __duration;\
44           __duration = GNUNET_TIME_absolute_get_duration (__timestamp);\
45           LOG (GNUNET_ERROR_TYPE_INFO, " %s duration %s\n",\
46               __FUNCTION__,\
47               GNUNET_STRINGS_relative_time_to_string (__duration, GNUNET_YES));
48 #else
49   #define CADET_TIMING_START
50   #define CADET_TIMING_END
51 #endif
52
53
54 #include "platform.h"
55 #include "gnunet_util_lib.h"
56 #include "gnunet_peer_lib.h"
57 #include "gnunet_core_service.h"
58 #include "gnunet_cadet_service.h"
59 #include "gnunet_protocols.h"
60 #include "gnunet_cadet_service.h"
61
62 /******************************************************************************/
63 /**************************       CONSTANTS      ******************************/
64 /******************************************************************************/
65
66 /**
67  * Minimum value for channel IDs of local clients.
68  */
69 #define GNUNET_CADET_LOCAL_CHANNEL_ID_CLI        0x80000000U
70
71 /**
72  * FIXME.
73  */
74 #define HIGH_PID                                0xFF000000
75
76 /**
77  * FIXME.
78  */
79 #define LOW_PID                                 0x00FFFFFF
80
81
82 /**
83  * Test if the two PIDs (of type `uint32_t`) are in the range where we
84  * have to worry about overflows.  This is the case when @a pid is
85  * large and @a max is small, useful when comparing @a pid smaller
86  * than @a max.
87  */
88 #define PID_OVERFLOW(pid, max) (((pid) > HIGH_PID) && ((max) < LOW_PID))
89
90 /******************************************************************************/
91 /**************************        MESSAGES      ******************************/
92 /******************************************************************************/
93
94 GNUNET_NETWORK_STRUCT_BEGIN
95
96
97 /**
98  * Number uniquely identifying a channel of a client.
99  */
100 struct GNUNET_CADET_ClientChannelNumber
101 {
102   /**
103    * Values for channel numbering.
104    * Local channel numbers given by the service (incoming) are
105    * smaller than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
106    * Local channel numbers given by the client (created) are
107    * larger than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
108    */
109   uint32_t channel_of_client GNUNET_PACKED;
110 };
111
112
113 /**
114  * Message for a client to create and destroy channels.
115  */
116 struct GNUNET_CADET_PortMessage
117 {
118   /**
119    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN
120    * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE
121    *
122    * Size: sizeof(struct GNUNET_CADET_ChannelMessage)
123    */
124   struct GNUNET_MessageHeader header;
125
126   /**
127    * Port to open/close.
128    */
129   struct GNUNET_HashCode port GNUNET_PACKED;
130 };
131
132
133 /**
134  * Message for a client to create channels.
135  */
136 struct GNUNET_CADET_LocalChannelCreateMessage
137 {
138   /**
139    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE
140    *
141    * Size: sizeof(struct GNUNET_CADET_ChannelOpenMessageMessage)
142    */
143   struct GNUNET_MessageHeader header;
144
145   /**
146    * ID of a channel controlled by this client.
147    */
148   struct GNUNET_CADET_ClientChannelNumber ccn;
149
150   /**
151    * Channel's peer
152    */
153   struct GNUNET_PeerIdentity peer;
154
155   /**
156    * Port of the channel.
157    */
158   struct GNUNET_HashCode port;
159
160   /**
161    * Options.
162    */
163   uint32_t opt GNUNET_PACKED;
164 };
165
166
167 /**
168  * Message for or to a client to destroy tunnel.
169  */
170 struct GNUNET_CADET_LocalChannelDestroyMessage
171 {
172   /**
173    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY
174    */
175   struct GNUNET_MessageHeader header;
176
177   /**
178    * ID of a channel controlled by this client.
179    */
180   struct GNUNET_CADET_ClientChannelNumber ccn;
181 };
182
183
184 /**
185  * Message for cadet data traffic.
186  */
187 struct GNUNET_CADET_LocalData
188 {
189   /**
190    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA
191    */
192   struct GNUNET_MessageHeader header;
193
194   /**
195    * ID of the channel
196    */
197   struct GNUNET_CADET_ClientChannelNumber ccn;
198
199   /**
200    * Payload follows
201    */
202 };
203
204
205 /**
206  * Message to allow the client send more data to the service
207  * (always service -> client).
208  */
209 struct GNUNET_CADET_LocalAck
210 {
211   /**
212    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK
213    */
214   struct GNUNET_MessageHeader header;
215
216   /**
217    * ID of the channel allowed to send more data.
218    */
219   struct GNUNET_CADET_ClientChannelNumber ccn;
220
221 };
222
223
224 /**
225  * Message to inform the client about channels in the service.
226  *
227  * TODO: split into two messages!
228  */
229 struct GNUNET_CADET_LocalInfo
230 {
231   /**
232    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL or
233    * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER
234    */
235   struct GNUNET_MessageHeader header;
236
237   /**
238    * ID of the channel allowed to send more data.
239    */
240   struct GNUNET_CADET_ClientChannelNumber ccn;
241
242   /**
243    * ID of the destination of the channel (can be local peer).
244    */
245   struct GNUNET_PeerIdentity peer;
246 };
247
248
249 /**
250  * Message to inform the client about one of the peers in the service.
251  *
252  * TODO: split into two messages!
253  */
254 struct GNUNET_CADET_LocalInfoPeer
255 {
256   /**
257    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER or
258    * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS
259    */
260   struct GNUNET_MessageHeader header;
261
262   /**
263    * Number of paths.
264    */
265   uint16_t paths GNUNET_PACKED;
266
267   /**
268    * Do we have a tunnel toward this peer?
269    */
270   int16_t tunnel GNUNET_PACKED;
271
272   /**
273    * ID of the peer (can be local peer).
274    */
275   struct GNUNET_PeerIdentity destination;
276
277   /* If type == PEER (no 'S'): GNUNET_PeerIdentity paths[]
278    * (each path ends in destination) */
279 };
280
281
282 /**
283  * Message to inform the client about one of the tunnels in the service.
284  *
285  * TODO: split into two messages!
286  */
287 struct GNUNET_CADET_LocalInfoTunnel
288 {
289   /**
290    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL
291    * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS
292    */
293   struct GNUNET_MessageHeader header;
294
295   /**
296    * Number of channels.
297    */
298   uint32_t channels GNUNET_PACKED;
299
300   /**
301    * ID of the destination of the tunnel (can be local peer).
302    */
303   struct GNUNET_PeerIdentity destination;
304
305   /**
306    * Number of connections.
307    */
308   uint32_t connections GNUNET_PACKED;
309
310   /**
311    * Encryption state.
312    */
313   uint16_t estate GNUNET_PACKED;
314
315   /**
316    * Connection state.
317    */
318   uint16_t cstate GNUNET_PACKED;
319
320   /* If TUNNEL (no 'S'): struct GNUNET_CADET_ConnectionTunnelIdentifier connection_ids[connections] */
321   /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
322 };
323
324
325 GNUNET_NETWORK_STRUCT_END
326
327
328 /**
329  * @brief Translate a fwd variable into a string representation, for logging.
330  *
331  * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
332  *
333  * @return String representing FWD or BCK.
334  */
335 char *
336 GC_f2s (int fwd);
337
338
339 /**
340  * Check if one pid is bigger than other, accounting for overflow.
341  *
342  * @param bigger Argument that should be bigger.
343  * @param smaller Argument that should be smaller.
344  *
345  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
346  */
347 int
348 GC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
349
350
351 /**
352  * Get the higher ACK value out of two values, taking in account overflow.
353  *
354  * @param a First ACK value.
355  * @param b Second ACK value.
356  *
357  * @return Highest ACK value from the two.
358  */
359 uint32_t
360 GC_max_pid (uint32_t a, uint32_t b);
361
362
363 /**
364  * Get the lower ACK value out of two values, taking in account overflow.
365  *
366  * @param a First ACK value.
367  * @param b Second ACK value.
368  *
369  * @return Lowest ACK value from the two.
370  */
371 uint32_t
372 GC_min_pid (uint32_t a, uint32_t b);
373
374
375 /**
376  * Allocate a string with a hexdump of any binary data.
377  *
378  * @param bin Arbitrary binary data.
379  * @param len Length of @a bin in bytes.
380  * @param output Where to write the output (if *output be NULL it's allocated).
381  *
382  * @return The size of the output.
383  */
384 size_t
385 GC_bin2s (void *bin,
386           unsigned int len,
387           char **output);
388
389
390 /**
391  * Convert a message type into a string to help debug
392  * Generated with:
393  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
394  * REPLACE:     "    case \2: return "\1"; break;"
395  *
396  * @param m Message type.
397  *
398  * @return Human readable string description.
399  */
400 const char *
401 GC_m2s (uint16_t m);
402
403 #if 0                           /* keep Emacsens' auto-indent happy */
404 {
405 #endif
406 #ifdef __cplusplus
407 }
408 #endif
409
410 #endif