remaing remaining mandatory functions for basic operation
[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_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        0x80000000
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_TunnelCreateMessage
139 {
140   /**
141    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_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 channel_id;
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_TunnelDestroyMessage
173 {
174   /**
175    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_DESTROY
176    */
177   struct GNUNET_MessageHeader header;
178
179   /**
180    * ID of a channel controlled by this client.
181    */
182   struct GNUNET_CADET_ClientChannelNumber channel_id;
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 channel_id;
200
201   /**
202    * Payload follows
203    */
204 };
205
206
207 /**
208  * Message to allow the client send more data to the service
209  * (always service -> client).
210  */
211 struct GNUNET_CADET_LocalAck
212 {
213   /**
214    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK
215    */
216   struct GNUNET_MessageHeader header;
217
218   /**
219    * ID of the channel allowed to send more data.
220    */
221   struct GNUNET_CADET_ClientChannelNumber channel_id;
222
223 };
224
225
226 /**
227  * Message to inform the client about channels in the service.
228  */
229 struct GNUNET_CADET_LocalInfo
230 {
231   /**
232    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL or
233    * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER
234    */
235   struct GNUNET_MessageHeader header;
236
237   /**
238    * ID of the channel allowed to send more data.
239    */
240   struct GNUNET_CADET_ClientChannelNumber channel_id;
241
242   /**
243    * ID of the owner of the channel (can be local peer).
244    */
245 //   struct GNUNET_PeerIdentity owner;
246
247   /**
248    * ID of the destination of the channel (can be local peer).
249    */
250   struct GNUNET_PeerIdentity peer;
251 };
252
253
254 /**
255  * Message to inform the client about one of the peers in the service.
256  */
257 struct GNUNET_CADET_LocalInfoPeer
258 {
259   /**
260    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER or
261    * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS
262    */
263   struct GNUNET_MessageHeader header;
264
265   /**
266    * Number of paths.
267    */
268   uint16_t paths GNUNET_PACKED;
269
270   /**
271    * Do we have a tunnel toward this peer?
272    */
273   int16_t tunnel GNUNET_PACKED;
274
275   /**
276    * ID of the peer (can be local peer).
277    */
278   struct GNUNET_PeerIdentity destination;
279
280   /* If type == PEER (no 'S'): GNUNET_PeerIdentity paths[]
281    * (each path ends in destination) */
282 };
283
284
285 /**
286  * Message to inform the client about one of the tunnels in the service.
287  */
288 struct GNUNET_CADET_LocalInfoTunnel
289 {
290   /**
291    * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL
292    * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS
293    */
294   struct GNUNET_MessageHeader header;
295
296   /**
297    * Number of channels.
298    */
299   uint32_t channels GNUNET_PACKED;
300
301   /**
302    * ID of the destination of the tunnel (can be local peer).
303    */
304   struct GNUNET_PeerIdentity destination;
305
306   /**
307    * Number of connections.
308    */
309   uint32_t connections GNUNET_PACKED;
310
311   /**
312    * Encryption state.
313    */
314   uint16_t estate GNUNET_PACKED;
315
316   /**
317    * Connection state.
318    */
319   uint16_t cstate GNUNET_PACKED;
320
321   /* If TUNNEL (no 'S'): struct GNUNET_CADET_ConnectionTunnelIdentifier connection_ids[connections] */
322   /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
323 };
324
325
326 GNUNET_NETWORK_STRUCT_END
327
328
329 /**
330  * @brief Translate a fwd variable into a string representation, for logging.
331  *
332  * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
333  *
334  * @return String representing FWD or BCK.
335  */
336 char *
337 GC_f2s (int fwd);
338
339
340 /**
341  * Check if one pid is bigger than other, accounting for overflow.
342  *
343  * @param bigger Argument that should be bigger.
344  * @param smaller Argument that should be smaller.
345  *
346  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
347  */
348 int
349 GC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
350
351
352 /**
353  * Get the higher ACK value out of two values, taking in account overflow.
354  *
355  * @param a First ACK value.
356  * @param b Second ACK value.
357  *
358  * @return Highest ACK value from the two.
359  */
360 uint32_t
361 GC_max_pid (uint32_t a, uint32_t b);
362
363
364 /**
365  * Get the lower ACK value out of two values, taking in account overflow.
366  *
367  * @param a First ACK value.
368  * @param b Second ACK value.
369  *
370  * @return Lowest ACK value from the two.
371  */
372 uint32_t
373 GC_min_pid (uint32_t a, uint32_t b);
374
375
376 /**
377  * Allocate a string with a hexdump of any binary data.
378  *
379  * @param bin Arbitrary binary data.
380  * @param len Length of @a bin in bytes.
381  * @param output Where to write the output (if *output be NULL it's allocated).
382  *
383  * @return The size of the output.
384  */
385 size_t
386 GC_bin2s (void *bin,
387           unsigned int len,
388           char **output);
389
390
391 /**
392  * Convert a message type into a string to help debug
393  * Generated with:
394  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
395  * REPLACE:     "    case \2: return "\1"; break;"
396  *
397  * @param m Message type.
398  *
399  * @return Human readable string description.
400  */
401 const char *
402 GC_m2s (uint16_t m);
403
404 #if 0                           /* keep Emacsens' auto-indent happy */
405 {
406 #endif
407 #ifdef __cplusplus
408 }
409 #endif
410
411 #endif