- simplified MESH client<->service API as per suggestions in bug #2934
[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_common.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet_peer_lib.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_protocols.h"
47 #include <gnunet_mesh_service.h>
48
49 /******************************************************************************/
50 /********************        MESH LOCAL MESSAGES      *************************/
51 /******************************************************************************/
52 /*  Any API call should be documented in the folowing table under API CALL.
53  *  Also, any message type should be documented in the following table, with the
54  * associated event.
55  *
56  * API CALL (GNUNET_MESH_*)             MESSAGE USED
57  * ------------------------             ------------
58  * connect                              GNUNET_MESH_ClientConnect
59  * disconnect                           None (network level disconnect)
60  *
61  * tunnel_create                        GNUNET_MESH_TunnelMessage
62  * tunnel_destroy                       GNUNET_MESH_TunnelMessage
63  * tunnel_buffer                        GNUNET_MESH_TunnelMessage
64  *
65  * notify_transmit_ready                None (queue / GNUNET_CLIENT_ntf_tmt_rdy)
66  * notify_transmit_ready_cancel         None (clear of internal data structures)
67  *
68  * 
69  * EVENT                                MESSAGE USED
70  * -----                                ------------
71  * data                                 GNUNET_MESH_Unicast OR
72  *                                      GNUNET_MESH_Multicast OR
73  *                                      GNUNET_MESH_ToOrigin
74  * data ack                             GNUNET_MESH_LocalAck
75  * 
76  * new incoming tunnel                  GNUNET_MESH_PeerControl
77  * peer connects to a tunnel            FIXME
78  * peer disconnects from a tunnel       FIXME
79  */
80
81 /******************************************************************************/
82 /**************************       CONSTANTS      ******************************/
83 /******************************************************************************/
84
85 #define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI         0x80000000
86 #define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV        0xB0000000
87
88 #define HIGH_PID                                0xFFFF0000
89 #define LOW_PID                                 0x0000FFFF
90
91 /**
92  * Value in tunnel info: *int (GNUNET_YES/GNUNET_NO)
93  */
94 #define GNUNET_MESH_OPTION_NOBUFFER             0x1
95 #define GNUNET_MESH_OPTION_RELIABLE             0x2
96
97 #define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
98
99 /******************************************************************************/
100 /**************************        MESSAGES      ******************************/
101 /******************************************************************************/
102
103 GNUNET_NETWORK_STRUCT_BEGIN
104
105 /**
106  * Message for a client to register to the service
107  */
108 struct GNUNET_MESH_ClientConnect
109 {
110     /**
111      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
112      *
113      * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
114      *       sizeof(MESH_ApplicationType) * applications +
115      *       sizeof(uint16_t) * types
116      */
117   struct GNUNET_MessageHeader header;
118   /* uint32_t                 list_ports[]           */
119 };
120
121
122 /**
123  * Type for tunnel numbering.
124  * - Local tunnel numbers given by the service (incoming) are >= 0xB0000000
125  * - Local tunnel numbers given by the client (created) are >= 0x80000000
126  * - Global tunnel numbers are < 0x80000000
127  */
128 typedef uint32_t MESH_TunnelNumber;
129
130
131 /**
132  * Message for a client to create and destroy tunnels.
133  */
134 struct GNUNET_MESH_TunnelMessage
135 {
136     /**
137      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
138      *
139      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
140      */
141   struct GNUNET_MessageHeader header;
142
143     /**
144      * ID of a tunnel controlled by this client.
145      */
146   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
147
148     /**
149      * Tunnel's peer
150      */
151   struct GNUNET_PeerIdentity peer;
152
153     /**
154      * Port of the tunnel.
155      */
156   uint32_t port GNUNET_PACKED;
157
158     /**
159      * Options.
160      */
161   int32_t options GNUNET_PACKED;
162 };
163
164
165 /**
166  * Message for the service to let a client know about created tunnels.
167  */
168 struct GNUNET_MESH_TunnelNotification
169 {
170     /**
171      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
172      *
173      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
174      */
175   struct GNUNET_MessageHeader header;
176
177     /**
178      * ID of a tunnel controlled by this client.
179      */
180   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
181
182     /**
183      * Peer at the other end.
184      */
185   struct GNUNET_PeerIdentity peer;
186
187     /**
188      * Port for this tunnel
189      */
190   uint32_t port GNUNET_PACKED;
191
192     /**
193      * Tunnel options (speed, buffering)
194      */
195   uint32_t opt GNUNET_PACKED;
196 };
197
198
199 /**
200  * Message for mesh data traffic.
201  */
202 struct GNUNET_MESH_LocalData
203 {
204     /**
205      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
206      */
207   struct GNUNET_MessageHeader header;
208
209     /**
210      * TID of the tunnel
211      */
212   uint32_t tid GNUNET_PACKED;
213
214     /**
215      * Payload follows
216      */
217 };
218
219
220 /**
221  * Message to allow the client send more data to the service
222  * (always service -> client).
223  */
224 struct GNUNET_MESH_LocalAck
225 {
226     /**
227      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
228      */
229   struct GNUNET_MessageHeader header;
230
231     /**
232      * ID of the tunnel allowed to send more data.
233      */
234   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
235
236 };
237
238
239 /**
240  * Message to inform the client about tunnels in the service.
241  */
242 struct GNUNET_MESH_LocalMonitor
243 {
244   /**
245      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR[_TUNNEL]
246    */
247   struct GNUNET_MessageHeader header;
248
249   /**
250    * ID of the tunnel allowed to send more data.
251    */
252   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
253
254   /**
255    * Alignment.
256    */
257   uint32_t reserved GNUNET_PACKED;
258
259   /**
260    * ID of the owner of the tunnel (can be local peer).
261    */
262   struct GNUNET_PeerIdentity owner;
263
264   /**
265    * ID of the destination of the tunnel (can be local peer).
266    */
267   struct GNUNET_PeerIdentity destination;
268 };
269
270
271 GNUNET_NETWORK_STRUCT_END
272
273 /******************************************************************************/
274 /************************        ENUMERATIONS      ****************************/
275 /******************************************************************************/
276
277 /**
278  * All the states a tunnel can be in.
279  */
280 enum MeshTunnelState
281 {
282     /**
283      * Uninitialized status, should never appear in operation.
284      */
285   MESH_TUNNEL_NEW,
286
287     /**
288      * Path to the peer not known yet
289      */
290   MESH_TUNNEL_SEARCHING,
291
292     /**
293      * Request sent, not yet answered.
294      */
295   MESH_TUNNEL_WAITING,
296
297     /**
298      * Peer connected and ready to accept data
299      */
300   MESH_TUNNEL_READY,
301
302     /**
303      * Peer connected previosly but not responding
304      */
305   MESH_TUNNEL_RECONNECTING
306 };
307
308
309 /**
310  * Check if one pid is bigger than other, accounting for overflow.
311  *
312  * @param bigger Argument that should be bigger.
313  * @param smaller Argument that should be smaller.
314  *
315  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
316  */
317 int
318 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
319
320
321 /**
322  * Get the higher ACK value out of two values, taking in account overflow.
323  *
324  * @param a First ACK value.
325  * @param b Second ACK value.
326  *
327  * @return Highest ACK value from the two.
328  */
329 uint32_t
330 GMC_max_pid (uint32_t a, uint32_t b);
331
332
333 /**
334  * Get the lower ACK value out of two values, taking in account overflow.
335  *
336  * @param a First ACK value.
337  * @param b Second ACK value.
338  *
339  * @return Lowest ACK value from the two.
340  */
341 uint32_t
342 GMC_min_pid (uint32_t a, uint32_t b);
343
344
345 /**
346  * Convert a message type into a string to help debug
347  * Generated with:
348  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
349  * REPLACE:     "    case \2: return "\1"; break;"
350  * 
351  * @param m Message type.
352  * 
353  * @return Human readable string description.
354  */
355 const char *
356 GNUNET_MESH_DEBUG_M2S (uint16_t m);
357
358 #if 0                           /* keep Emacsens' auto-indent happy */
359 {
360 #endif
361 #ifdef __cplusplus
362 }
363 #endif
364
365 #endif