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