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