- use variables to cound ch, conns
[oweals/gnunet.git] / src / mesh / mesh.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @author Bartlomiej Polot
23  * @file mesh/mesh.h
24  */
25
26 #ifndef MESH_H_
27 #define MESH_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 MESH_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_mesh_service.h>
47
48 /******************************************************************************/
49 /**************************       CONSTANTS      ******************************/
50 /******************************************************************************/
51
52 #define GNUNET_MESH_LOCAL_CHANNEL_ID_CLI        0x80000000
53 #define GNUNET_MESH_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  * Message for a client to register to the service
68  */
69 struct GNUNET_MESH_ClientConnect
70 {
71     /**
72      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
73      *
74      * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
75      *       sizeof(MESH_ApplicationType) * applications +
76      *       sizeof(uint16_t) * types
77      */
78   struct GNUNET_MessageHeader header;
79   /* uint32_t                 list_ports[]           */
80 };
81
82
83 /**
84  * Type for channel numbering.
85  * - Local channel numbers given by the service (incoming) are >= 0xB0000000
86  * - Local channel numbers given by the client (created) are >= 0x80000000
87  * - Global channel numbers are < 0x80000000
88  */
89 typedef uint32_t MESH_ChannelNumber;
90
91
92 /**
93  * Message for a client to create and destroy channels.
94  */
95 struct GNUNET_MESH_ChannelMessage
96 {
97     /**
98      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
99      *
100      * Size: sizeof(struct GNUNET_MESH_ChannelMessage)
101      */
102   struct GNUNET_MessageHeader header;
103
104     /**
105      * ID of a channel controlled by this client.
106      */
107   MESH_ChannelNumber channel_id GNUNET_PACKED;
108
109     /**
110      * Channel's peer
111      */
112   struct GNUNET_PeerIdentity peer;
113
114     /**
115      * Port of the channel.
116      */
117   uint32_t port GNUNET_PACKED;
118
119     /**
120      * Options.
121      */
122   uint32_t opt GNUNET_PACKED;
123 };
124
125
126 /**
127  * Message for mesh data traffic.
128  */
129 struct GNUNET_MESH_LocalData
130 {
131     /**
132      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
133      */
134   struct GNUNET_MessageHeader header;
135
136     /**
137      * ID of the channel
138      */
139   uint32_t id GNUNET_PACKED;
140
141     /**
142      * Payload follows
143      */
144 };
145
146
147 /**
148  * Message to allow the client send more data to the service
149  * (always service -> client).
150  */
151 struct GNUNET_MESH_LocalAck
152 {
153     /**
154      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
155      */
156   struct GNUNET_MessageHeader header;
157
158     /**
159      * ID of the channel allowed to send more data.
160      */
161   MESH_ChannelNumber channel_id GNUNET_PACKED;
162
163 };
164
165
166 /**
167  * Message to inform the client about channels in the service.
168  */
169 struct GNUNET_MESH_LocalInfo
170 {
171   /**
172      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO[_TUNNEL]
173    */
174   struct GNUNET_MessageHeader header;
175
176   /**
177    * ID of the channel allowed to send more data.
178    */
179   MESH_ChannelNumber channel_id GNUNET_PACKED;
180
181   /**
182    * ID of the owner of the channel (can be local peer).
183    */
184 //   struct GNUNET_PeerIdentity owner;
185
186   /**
187    * ID of the destination of the channel (can be local peer).
188    */
189   struct GNUNET_PeerIdentity peer;
190 };
191
192 /**
193  * Message to inform the client about one of the tunnels in the service.
194  */
195 struct GNUNET_MESH_LocalInfoTunnel
196 {
197   /**
198    * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL[S]
199    */
200   struct GNUNET_MessageHeader header;
201
202   /**
203    * Number of channels.
204    */
205   uint32_t channels GNUNET_PACKED;
206
207   /**
208    * ID of the destination of the tunnel (can be local peer).
209    */
210   struct GNUNET_PeerIdentity destination;
211
212   /**
213    * Number of connections.
214    */
215   uint32_t connections GNUNET_PACKED;
216
217   /**
218    * Encryption state.
219    */
220   uint16_t estate GNUNET_PACKED;
221
222   /**
223    * Connection state.
224    */
225   uint16_t cstate GNUNET_PACKED;
226
227   /* If TUNNEL (no 'S'): GNUNET_PeerIdentity connection_ids[connections] */
228   /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
229 };
230
231
232 GNUNET_NETWORK_STRUCT_END
233
234
235
236 /**
237  * @brief Translate a fwd variable into a string representation, for logging.
238  *
239  * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
240  *
241  * @return String representing FWD or BCK.
242  */
243 char *
244 GM_f2s (int fwd);
245
246
247 /**
248  * Check if one pid is bigger than other, accounting for overflow.
249  *
250  * @param bigger Argument that should be bigger.
251  * @param smaller Argument that should be smaller.
252  *
253  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
254  */
255 int
256 GM_is_pid_bigger (uint32_t bigger, uint32_t smaller);
257
258
259 /**
260  * Get the higher ACK value out of two values, taking in account overflow.
261  *
262  * @param a First ACK value.
263  * @param b Second ACK value.
264  *
265  * @return Highest ACK value from the two.
266  */
267 uint32_t
268 GM_max_pid (uint32_t a, uint32_t b);
269
270
271 /**
272  * Get the lower ACK value out of two values, taking in account overflow.
273  *
274  * @param a First ACK value.
275  * @param b Second ACK value.
276  *
277  * @return Lowest ACK value from the two.
278  */
279 uint32_t
280 GM_min_pid (uint32_t a, uint32_t b);
281
282
283 /**
284  * Convert a message type into a string to help debug
285  * Generated with:
286  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
287  * REPLACE:     "    case \2: return "\1"; break;"
288  *
289  * @param m Message type.
290  *
291  * @return Human readable string description.
292  */
293 const char *
294 GM_m2s (uint16_t m);
295
296 #if 0                           /* keep Emacsens' auto-indent happy */
297 {
298 #endif
299 #ifdef __cplusplus
300 }
301 #endif
302
303 #endif