29400e39f953fa25d24361b8799f47969c685a15
[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      SPDX-License-Identifier: AGPL3.0-or-later
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 #if 0 /* keep Emacsens' auto-indent happy */
32 }
33 #endif
34 #endif
35
36 #include <stdint.h>
37
38 #if ! defined(GNUNET_CULL_LOGGING)
39 #define CADET_TIMING_START                 \
40   struct GNUNET_TIME_Absolute __timestamp; \
41   __timestamp = GNUNET_TIME_absolute_get ()
42
43 #define CADET_TIMING_END                                        \
44   struct GNUNET_TIME_Relative __duration;                       \
45   __duration = GNUNET_TIME_absolute_get_duration (__timestamp); \
46   LOG (GNUNET_ERROR_TYPE_INFO,                                  \
47        " %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_cadet_service.h"
61 #include "gnunet_protocols.h"
62 #include "gnunet_cadet_service.h"
63
64 /******************************************************************************/
65 /**************************       CONSTANTS      ******************************/
66 /******************************************************************************/
67
68 /**
69  * Minimum value for channel IDs of local clients.
70  */
71 #define GNUNET_CADET_LOCAL_CHANNEL_ID_CLI 0x80000000U
72
73 /**
74  * FIXME.
75  */
76 #define HIGH_PID 0xFF000000
77
78 /**
79  * FIXME.
80  */
81 #define LOW_PID 0x00FFFFFF
82
83
84 /**
85  * Test if the two PIDs (of type `uint32_t`) are in the range where we
86  * have to worry about overflows.  This is the case when @a pid is
87  * large and @a max is small, useful when comparing @a pid smaller
88  * than @a max.
89  */
90 #define PID_OVERFLOW(pid, max) (((pid) > HIGH_PID) && ((max) < LOW_PID))
91
92 /******************************************************************************/
93 /**************************        MESSAGES      ******************************/
94 /******************************************************************************/
95
96 GNUNET_NETWORK_STRUCT_BEGIN
97
98
99 /**
100  * Number uniquely identifying a channel of a client.
101  */
102 struct GNUNET_CADET_ClientChannelNumber
103 {
104   /**
105    * Values for channel numbering.
106    * Local channel numbers given by the service (incoming) are
107    * smaller than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
108    * Local channel numbers given by the client (created) are
109    * larger than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
110    */
111   uint32_t channel_of_client GNUNET_PACKED;
112 };
113
114
115 /**
116  * Message for a client to create and destroy channels.
117  */
118 struct GNUNET_CADET_PortMessage
119 {
120   /**
121    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN
122    * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE
123    *
124    * Size: sizeof(struct GNUNET_CADET_ChannelMessage)
125    */
126   struct GNUNET_MessageHeader header;
127
128   /**
129    * Port to open/close.
130    */
131   struct GNUNET_HashCode port GNUNET_PACKED;
132 };
133
134
135 /**
136  * Message for a client to create channels.
137  */
138 struct GNUNET_CADET_LocalChannelCreateMessage
139 {
140   /**
141    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE
142    *
143    * Size: sizeof(struct GNUNET_CADET_ChannelOpenMessageMessage)
144    */
145   struct GNUNET_MessageHeader header;
146
147   /**
148    * ID of a channel controlled by this client.
149    */
150   struct GNUNET_CADET_ClientChannelNumber ccn;
151
152   /**
153    * Channel's peer
154    */
155   struct GNUNET_PeerIdentity peer;
156
157   /**
158    * Port of the channel.
159    */
160   struct GNUNET_HashCode port;
161
162   /**
163    * Options.
164    */
165   uint32_t opt GNUNET_PACKED;
166 };
167
168
169 /**
170  * Message for or to a client to destroy tunnel.
171  */
172 struct GNUNET_CADET_LocalChannelDestroyMessage
173 {
174   /**
175    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY
176    */
177   struct GNUNET_MessageHeader header;
178
179   /**
180    * ID of a channel controlled by this client.
181    */
182   struct GNUNET_CADET_ClientChannelNumber ccn;
183 };
184
185
186 /**
187  * Message for cadet data traffic.
188  */
189 struct GNUNET_CADET_LocalData
190 {
191   /**
192    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA
193    */
194   struct GNUNET_MessageHeader header;
195
196   /**
197    * ID of the channel
198    */
199   struct GNUNET_CADET_ClientChannelNumber ccn;
200
201   /**
202    * Priority and preferences (an enum GNUNET_MQ_PriorityPreferences)
203    * of the message in NBO.
204    */
205   uint32_t pp GNUNET_PACKED;
206
207   /**
208    * Payload follows
209    */
210 };
211
212
213 /**
214  * Message to allow the client send more data to the service
215  * (always service -> client).
216  */
217 struct GNUNET_CADET_LocalAck
218 {
219   /**
220    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK
221    */
222   struct GNUNET_MessageHeader header;
223
224   /**
225    * ID of the channel allowed to send more data.
226    */
227   struct GNUNET_CADET_ClientChannelNumber ccn;
228 };
229
230
231 /**
232  * Message to inform the client about channels in the service.
233  *
234  * TODO: split into two messages!
235  */
236 struct GNUNET_CADET_LocalInfo
237 {
238   /**
239    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL or
240    * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER
241    */
242   struct GNUNET_MessageHeader header;
243
244   /**
245    * ID of the channel allowed to send more data.
246    */
247   struct GNUNET_CADET_ClientChannelNumber ccn;
248
249   /**
250    * ID of the destination of the channel (can be local peer).
251    */
252   struct GNUNET_PeerIdentity peer;
253 };
254
255
256 /**
257  * Message to inform the client about channels in the service.
258  */
259 struct GNUNET_CADET_RequestPathInfoMessage
260 {
261   /**
262    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH
263    */
264   struct GNUNET_MessageHeader header;
265
266   /**
267    * Always zero.
268    */
269   uint32_t resered GNUNET_PACKED;
270
271   /**
272    * ID of the destination of the channel (can be local peer).
273    */
274   struct GNUNET_PeerIdentity peer;
275 };
276
277
278 /**
279  * Message to inform the client about channels in the service.
280  */
281 struct GNUNET_CADET_ChannelInfoMessage
282 {
283   /**
284    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL.
285    */
286   struct GNUNET_MessageHeader header;
287
288   /**
289    * Root of the channel
290    */
291   struct GNUNET_PeerIdentity root;
292
293   /**
294    * Destination of the channel
295    */
296   struct GNUNET_PeerIdentity dest;
297
298   /* FIXME: expand! */
299 };
300
301
302 /**
303  * Message to as the service about information on a channel.
304  */
305 struct GNUNET_CADET_RequestChannelInfoMessage
306 {
307   /**
308    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL.
309    */
310   struct GNUNET_MessageHeader header;
311
312   /**
313    * Target of the channel.
314    */
315   struct GNUNET_PeerIdentity target;
316 };
317
318
319 /**
320  * Message to inform the client about one of the paths known to the service.
321  */
322 struct GNUNET_CADET_LocalInfoPath
323 {
324   /**
325    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH.
326    */
327   struct GNUNET_MessageHeader header;
328
329   /**
330    * Offset of the peer that was requested.
331    */
332   uint32_t off GNUNET_PACKED;
333 };
334
335
336 /**
337  * Message to inform the client about one of the peers in the service.
338  */
339 struct GNUNET_CADET_LocalInfoPeers
340 {
341   /**
342    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS
343    */
344   struct GNUNET_MessageHeader header;
345
346   /**
347    * Number of paths.
348    */
349   uint16_t paths GNUNET_PACKED;
350
351   /**
352    * Do we have a tunnel toward this peer?
353    */
354   int16_t tunnel GNUNET_PACKED;
355
356   /**
357    * Shortest known path.
358    */
359   uint32_t best_path_length GNUNET_PACKED;
360
361   /**
362    * ID of the peer (can be local peer).
363    */
364   struct GNUNET_PeerIdentity destination;
365 };
366
367
368 /**
369  * Message to inform the client about one of the tunnels in the service.
370  *
371  * TODO: split into two messages!
372  */
373 struct GNUNET_CADET_LocalInfoTunnel
374 {
375   /**
376    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL
377    * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS
378    */
379   struct GNUNET_MessageHeader header;
380
381   /**
382    * Number of channels.
383    */
384   uint32_t channels GNUNET_PACKED;
385
386   /**
387    * ID of the destination of the tunnel (can be local peer).
388    */
389   struct GNUNET_PeerIdentity destination;
390
391   /**
392    * Number of connections.
393    */
394   uint32_t connections GNUNET_PACKED;
395
396   /**
397    * Encryption state.
398    */
399   uint16_t estate GNUNET_PACKED;
400
401   /**
402    * Connection state.
403    */
404   uint16_t cstate GNUNET_PACKED;
405
406   /* If TUNNEL (no 'S'): struct GNUNET_CADET_ConnectionTunnelIdentifier connection_ids[connections] */
407   /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
408 };
409
410
411 GNUNET_NETWORK_STRUCT_END
412
413
414 /**
415  * @brief Translate a fwd variable into a string representation, for logging.
416  *
417  * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
418  *
419  * @return String representing FWD or BCK.
420  */
421 char *
422 GC_f2s (int fwd);
423
424
425 /**
426  * Check if one pid is bigger than other, accounting for overflow.
427  *
428  * @param bigger Argument that should be bigger.
429  * @param smaller Argument that should be smaller.
430  *
431  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
432  */
433 int
434 GC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
435
436
437 /**
438  * Get the higher ACK value out of two values, taking in account overflow.
439  *
440  * @param a First ACK value.
441  * @param b Second ACK value.
442  *
443  * @return Highest ACK value from the two.
444  */
445 uint32_t
446 GC_max_pid (uint32_t a, uint32_t b);
447
448
449 /**
450  * Get the lower ACK value out of two values, taking in account overflow.
451  *
452  * @param a First ACK value.
453  * @param b Second ACK value.
454  *
455  * @return Lowest ACK value from the two.
456  */
457 uint32_t
458 GC_min_pid (uint32_t a, uint32_t b);
459
460
461 /**
462  * Allocate a string with a hexdump of any binary data.
463  *
464  * @param bin Arbitrary binary data.
465  * @param len Length of @a bin in bytes.
466  * @param output Where to write the output (if *output be NULL it's allocated).
467  *
468  * @return The size of the output.
469  */
470 size_t
471 GC_bin2s (void *bin, unsigned int len, char **output);
472
473
474 /**
475  * Convert a message type into a string to help debug
476  * Generated with:
477  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
478  * REPLACE:     "    case \2: return "\1"; break;"
479  *
480  * @param m Message type.
481  *
482  * @return Human readable string description.
483  */
484 const char *
485 GC_m2s (uint16_t m);
486
487 #if 0 /* keep Emacsens' auto-indent happy */
488 {
489 #endif
490 #ifdef __cplusplus
491 }
492 #endif
493
494 #endif