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