-rps doxygen
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @author Bartlomiej Polot
23  * @file cadet/cadet.h
24  */
25
26 #ifndef CADET_H_
27 #define CADET_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 CADET_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_cadet_service.h>
47
48 /******************************************************************************/
49 /**************************       CONSTANTS      ******************************/
50 /******************************************************************************/
51
52 #define GNUNET_CADET_LOCAL_CHANNEL_ID_CLI        0x80000000
53 #define GNUNET_CADET_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 /**
68  * Message for a client to create and destroy channels.
69  */
70 struct GNUNET_CADET_PortMessage
71 {
72     /**
73      * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_[OPEN|CLOSE]
74      *
75      * Size: sizeof(struct GNUNET_CADET_ChannelMessage)
76      */
77   struct GNUNET_MessageHeader header;
78
79     /**
80      * Port to open/close.
81      */
82   struct GNUNET_HashCode port GNUNET_PACKED;
83 };
84
85 /**
86  * Type for channel numbering.
87  * - Local channel numbers given by the service (incoming) are >= 0xB0000000
88  * - Local channel numbers given by the client (created) are >= 0x80000000
89  * - Global channel numbers are < 0x80000000
90  */
91 typedef uint32_t CADET_ChannelNumber;
92
93
94 /**
95  * Message for a client to create channels.
96  */
97 struct GNUNET_CADET_ChannelCreateMessage
98 {
99     /**
100      * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE
101      *
102      * Size: sizeof(struct GNUNET_CADET_ChannelCreateMessage)
103      */
104   struct GNUNET_MessageHeader header;
105
106     /**
107      * ID of a channel controlled by this client.
108      */
109   CADET_ChannelNumber channel_id GNUNET_PACKED;
110
111     /**
112      * Channel's peer
113      */
114   struct GNUNET_PeerIdentity peer;
115
116     /**
117      * Port of the channel.
118      */
119   struct GNUNET_HashCode port;
120
121     /**
122      * Options.
123      */
124   uint32_t opt GNUNET_PACKED;
125 };
126
127
128 /**
129  * Message for a client to destroy channels.
130  */
131 struct GNUNET_CADET_ChannelDestroyMessage
132 {
133     /**
134      * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_DESTROY
135      *
136      * Size: sizeof(struct GNUNET_CADET_ChannelDestroyMessage)
137      */
138   struct GNUNET_MessageHeader header;
139
140     /**
141      * ID of a channel controlled by this client.
142      */
143   CADET_ChannelNumber channel_id GNUNET_PACKED;
144 };
145
146
147 /**
148  * Message for cadet data traffic.
149  */
150 struct GNUNET_CADET_LocalData
151 {
152     /**
153      * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA
154      */
155   struct GNUNET_MessageHeader header;
156
157     /**
158      * ID of the channel
159      */
160   uint32_t id GNUNET_PACKED;
161
162     /**
163      * Payload follows
164      */
165 };
166
167
168 /**
169  * Message to allow the client send more data to the service
170  * (always service -> client).
171  */
172 struct GNUNET_CADET_LocalAck
173 {
174     /**
175      * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK
176      */
177   struct GNUNET_MessageHeader header;
178
179     /**
180      * ID of the channel allowed to send more data.
181      */
182   CADET_ChannelNumber channel_id GNUNET_PACKED;
183
184 };
185
186
187 /**
188  * Message to inform the client about channels in the service.
189  */
190 struct GNUNET_CADET_LocalInfo
191 {
192   /**
193      * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO[_TUNNEL,_PEER]
194    */
195   struct GNUNET_MessageHeader header;
196
197   /**
198    * ID of the channel allowed to send more data.
199    */
200   CADET_ChannelNumber channel_id GNUNET_PACKED;
201
202   /**
203    * ID of the owner of the channel (can be local peer).
204    */
205 //   struct GNUNET_PeerIdentity owner;
206
207   /**
208    * ID of the destination of the channel (can be local peer).
209    */
210   struct GNUNET_PeerIdentity peer;
211 };
212
213
214 /**
215  * Message to inform the client about one of the peers in the service.
216  */
217 struct GNUNET_CADET_LocalInfoPeer
218 {
219   /**
220    * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER[S]
221    */
222   struct GNUNET_MessageHeader header;
223
224   /**
225    * Number of paths.
226    */
227   uint16_t paths GNUNET_PACKED;
228
229   /**
230    * Do we have a tunnel toward this peer?
231    */
232   int16_t tunnel GNUNET_PACKED;
233
234   /**
235    * ID of the peer (can be local peer).
236    */
237   struct GNUNET_PeerIdentity destination;
238
239   /* If type == PEER (no 'S'): GNUNET_PeerIdentity paths[]
240    * (each path ends in destination) */
241 };
242
243 /**
244  * Message to inform the client about one of the tunnels in the service.
245  */
246 struct GNUNET_CADET_LocalInfoTunnel
247 {
248   /**
249    * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL[S]
250    */
251   struct GNUNET_MessageHeader header;
252
253   /**
254    * Number of channels.
255    */
256   uint32_t channels GNUNET_PACKED;
257
258   /**
259    * ID of the destination of the tunnel (can be local peer).
260    */
261   struct GNUNET_PeerIdentity destination;
262
263   /**
264    * Number of connections.
265    */
266   uint32_t connections GNUNET_PACKED;
267
268   /**
269    * Encryption state.
270    */
271   uint16_t estate GNUNET_PACKED;
272
273   /**
274    * Connection state.
275    */
276   uint16_t cstate GNUNET_PACKED;
277
278   /* If TUNNEL (no 'S'): GNUNET_PeerIdentity connection_ids[connections] */
279   /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
280 };
281
282
283 GNUNET_NETWORK_STRUCT_END
284
285
286 /**
287  * @brief Translate a fwd variable into a string representation, for logging.
288  *
289  * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
290  *
291  * @return String representing FWD or BCK.
292  */
293 char *
294 GC_f2s (int fwd);
295
296
297 /**
298  * Check if one pid is bigger than other, accounting for overflow.
299  *
300  * @param bigger Argument that should be bigger.
301  * @param smaller Argument that should be smaller.
302  *
303  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
304  */
305 int
306 GC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
307
308
309 /**
310  * Get the higher 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 Highest ACK value from the two.
316  */
317 uint32_t
318 GC_max_pid (uint32_t a, uint32_t b);
319
320
321 /**
322  * Get the lower ACK value out of two values, taking in account overflow.
323  *
324  * @param a First ACK value.
325  * @param b Second ACK value.
326  *
327  * @return Lowest ACK value from the two.
328  */
329 uint32_t
330 GC_min_pid (uint32_t a, uint32_t b);
331
332
333 /**
334  * Convert a 256 bit CadetHash into a 512 HashCode to use in GNUNET_h2s,
335  * multihashmap, and other HashCode-based functions.
336  *
337  * @param id A 256 bit hash to expand.
338  *
339  * @return A HashCode containing the original 256 bit hash right-padded with 0.
340  */
341 const struct GNUNET_HashCode *
342 GC_h2hc (const struct GNUNET_CADET_Hash *id);
343
344 /**
345  * Get a string from a Cadet Hash (256 bits).
346  * WARNING: Not reentrant (based on GNUNET_h2s).
347  */
348 const char *
349 GC_h2s (const struct GNUNET_CADET_Hash *id);
350
351
352 /**
353  * Allocate a string with a hexdump of any binary data.
354  *
355  * @param bin Arbitrary binary data.
356  * @param len Length of @a bin in bytes.
357  * @param output Where to write the output (if *output be NULL it's allocated).
358  *
359  * @return The size of the output.
360  */
361 size_t
362 GC_bin2s (void *bin, unsigned int len, char **output);
363
364 /**
365  * Convert a message type into a string to help debug
366  * Generated with:
367  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
368  * REPLACE:     "    case \2: return "\1"; break;"
369  *
370  * @param m Message type.
371  *
372  * @return Human readable string description.
373  */
374 const char *
375 GC_m2s (uint16_t m);
376
377 #if 0                           /* keep Emacsens' auto-indent happy */
378 {
379 #endif
380 #ifdef __cplusplus
381 }
382 #endif
383
384 #endif