e562846f0dc82f5550d03568622714ba53ea4fc6
[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 #include <stdint.h>
29
30 #define MESH_DEBUG              GNUNET_YES
31
32 #define INITIAL_WINDOW_SIZE     8
33 #define ACK_THRESHOLD           INITIAL_WINDOW_SIZE / 2
34
35 #include "platform.h"
36 #include "gnunet_common.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_peer_lib.h"
39 #include "gnunet_core_service.h"
40 #include "gnunet_protocols.h"
41 #include <gnunet_mesh_service.h>
42
43 /******************************************************************************/
44 /********************        MESH LOCAL MESSAGES      *************************/
45 /******************************************************************************/
46 /*  Any API call should be documented in the folowing table under API CALL.
47  *  Also, any message type should be documented in the following table, with the
48  * associated event.
49  *
50  * API CALL (GNUNET_MESH_*)             MESSAGE USED
51  * ------------------------             ------------
52  * connect                              GNUNET_MESH_ClientConnect
53  * disconnect                           None (network level disconnect)
54  *
55  * tunnel_create                        GNUNET_MESH_TunnelMessage
56  * tunnel_destroy                       GNUNET_MESH_TunnelMessage
57  * tunnel_speed_max                     GNUNET_MESH_TunnelMessage
58  * tunnel_speed_min                     GNUNET_MESH_TunnelMessage
59  * tunnel_buffer                        GNUNET_MESH_TunnelMessage
60  *
61  * peer_request_connect_add             GNUNET_MESH_PeerControl
62  * peer_request_connect_del             GNUNET_MESH_PeerControl
63  * peer_request_connect_by_type         GNUNET_MESH_ConnectPeerByType
64  * peer_request_connect_by_string       GNUNET_MESH_ConnectPeerByString
65  * 
66  * peer_blacklist                       GNUNET_MESH_PeerControl
67  * peer_unblacklist                     GNUNET_MESH_PeerControl
68  *
69  * notify_transmit_ready                None (queue / GNUNET_CLIENT_ntf_tmt_rdy)
70  * notify_transmit_ready_cancel         None (clear of internal data structures)
71  *
72  * 
73  * EVENT                                MESSAGE USED
74  * -----                                ------------
75  * data                                 GNUNET_MESH_Unicast OR
76  *                                      GNUNET_MESH_Multicast OR
77  *                                      GNUNET_MESH_ToOrigin
78  * data ack                             GNUNET_MESH_LocalAck
79  * 
80  * new incoming tunnel                  GNUNET_MESH_PeerControl
81  * peer connects to a tunnel            GNUNET_MESH_PeerControl
82  * peer disconnects from a tunnel       GNUNET_MESH_PeerControl
83  */
84
85 /******************************************************************************/
86 /**************************       CONSTANTS      ******************************/
87 /******************************************************************************/
88
89 #define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI         0x80000000
90 #define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV        0xB0000000
91
92 #define HIGH_PID                                0xFFFF0000
93 #define LOW_PID                                 0x0000FFFF
94
95 #define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
96
97 /******************************************************************************/
98 /**************************        MESSAGES      ******************************/
99 /******************************************************************************/
100
101 GNUNET_NETWORK_STRUCT_BEGIN
102
103 /**
104  * Message for a client to register to the service
105  */
106 struct GNUNET_MESH_ClientConnect
107 {
108     /**
109      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
110      *
111      * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
112      *       sizeof(MESH_ApplicationType) * applications +
113      *       sizeof(uint16_t) * types
114      */
115   struct GNUNET_MessageHeader header;
116   uint16_t applications GNUNET_PACKED;
117   uint16_t types GNUNET_PACKED;
118   /* uint32_t                 list_apps[applications]     */
119   /* uint16_t                 list_types[types]           */
120 };
121
122
123 /**
124  * Type for tunnel numbering.
125  * - Local tunnel numbers given by the service (incoming) are >= 0xB0000000
126  * - Local tunnel numbers given by the client (created) are >= 0x80000000
127  * - Global tunnel numbers are < 0x80000000
128  */
129 typedef uint32_t MESH_TunnelNumber;
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      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[MAX|MIN]
139      *
140      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
141      */
142   struct GNUNET_MessageHeader header;
143
144     /**
145      * ID of a tunnel controlled by this client.
146      */
147   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
148 };
149
150
151 /**
152  * Message for the service to let a client know about created tunnels.
153  */
154 struct GNUNET_MESH_TunnelNotification
155 {
156     /**
157      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
158      *
159      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
160      */
161   struct GNUNET_MessageHeader header;
162
163     /**
164      * ID of a tunnel controlled by this client.
165      */
166   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
167
168     /**
169      * Peer at the other end, if any
170      */
171   struct GNUNET_PeerIdentity peer;
172
173     /**
174      * Tunnel options (speed, buffering)
175      */
176   uint32_t opt;
177 };
178
179 /**
180  * Message for announce of regular expressions.
181  */
182 struct GNUNET_MESH_RegexAnnounce
183 {
184     /**
185      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX
186      *
187      * Size: sizeof(struct GNUNET_MESH_RegexAnnounce) + strlen (regex)
188      */
189   struct GNUNET_MessageHeader header;
190
191     /**
192      * How many characters do we want to put in an edge label.
193      */
194   uint16_t compression_characters;
195
196     /**
197      * Is this the last message for this regex? (for regex > 65k)
198      */
199   int16_t last;
200
201   /* regex payload  */
202 };
203
204
205 /**
206  * Message for:
207  * - request adding and deleting peers from a tunnel
208  * - notify the client that peers have connected:
209  *   -- requested
210  *   -- unrequested (new incoming tunnels)
211  * - notify the client that peers have disconnected
212  */
213 struct GNUNET_MESH_PeerControl
214 {
215
216     /**
217      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL|[UN]BLACKLIST]
218      *       (client to service, client created tunnel)
219      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
220      *       (service to client)
221      *
222      * Size: sizeof(struct GNUNET_MESH_PeerControl)
223      */
224   struct GNUNET_MessageHeader header;
225
226     /**
227      * ID of a tunnel controlled by this client.
228      */
229   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
230
231     /**
232      * Peer to connect/disconnect.
233      */
234   struct GNUNET_PeerIdentity peer;
235 };
236
237
238 /**
239  * Message for connecting to peers offering a service, by service number.
240  */
241 struct GNUNET_MESH_ConnectPeerByType
242 {
243     /**
244      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
245      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
246      * 
247      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByType)
248      */
249   struct GNUNET_MessageHeader header;
250
251     /**
252      * ID of a tunnel controlled by this client.
253      */
254   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
255
256     /**
257      * Type specification
258      */
259   GNUNET_MESH_ApplicationType type GNUNET_PACKED;
260 };
261
262
263 /**
264  * Message for connecting to peers offering a service, by service string.
265  */
266 struct GNUNET_MESH_ConnectPeerByString
267 {
268     /**
269      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING
270      * 
271      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByString) + strlen (string)
272      */
273   struct GNUNET_MessageHeader header;
274
275     /**
276      * ID of a tunnel controlled by this client.
277      */
278   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
279
280   /* String describing the service */
281 };
282
283
284 /**
285  * Message to allow the client send more data to the service
286  * (always service -> client).
287  */
288 struct GNUNET_MESH_LocalAck
289 {
290     /**
291      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
292      */
293   struct GNUNET_MessageHeader header;
294
295     /**
296      * ID of the tunnel allowed to send more data.
297      */
298   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
299
300     /**
301      * ID of the last packet allowed.
302      */
303   uint32_t max_pid GNUNET_PACKED;
304 };
305
306
307 /**
308  * Message to inform the client about tunnels in the service.
309  */
310 struct GNUNET_MESH_LocalMonitor
311 {
312   /**
313      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR[_TUNNEL]
314    */
315   struct GNUNET_MessageHeader header;
316
317   /**
318    * ID of the tunnel allowed to send more data.
319    */
320   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
321
322   /**
323    * Number of peers in the tunnel.
324    */
325   uint32_t npeers GNUNET_PACKED;
326
327   /**
328    * Alignment.
329    */
330   uint32_t reserved GNUNET_PACKED;
331
332   /**
333    * ID of the owner of the tunnel (can be local peer).
334    */
335   struct GNUNET_PeerIdentity owner;
336
337   /* struct GNUNET_PeerIdentity peers[npeers] */
338 };
339
340
341 GNUNET_NETWORK_STRUCT_END
342
343 /******************************************************************************/
344 /************************        ENUMERATIONS      ****************************/
345 /******************************************************************************/
346
347 /**
348  * All the states a peer participating in a tunnel can be in.
349  */
350 enum MeshPeerState
351 {
352     /**
353      * Uninitialized status, should never appear in operation.
354      */
355   MESH_PEER_INVALID,
356
357     /**
358      * Peer is the root and owner of the tree
359      */
360   MESH_PEER_ROOT,
361
362     /**
363      * Peer only retransmits traffic, is not a final destination
364      */
365   MESH_PEER_RELAY,
366
367     /**
368      * Path to the peer not known yet
369      */
370   MESH_PEER_SEARCHING,
371
372     /**
373      * Request sent, not yet answered.
374      */
375   MESH_PEER_WAITING,
376
377     /**
378      * Peer connected and ready to accept data
379      */
380   MESH_PEER_READY,
381
382     /**
383      * Peer connected previosly but not responding
384      */
385   MESH_PEER_RECONNECTING
386 };
387
388
389 /**
390  * Check if one pid is bigger than other, accounting for overflow.
391  *
392  * @param bigger Argument that should be bigger.
393  * @param smaller Argument that should be smaller.
394  *
395  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
396  */
397 int
398 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
399
400
401 /**
402  * Get the higher ACK value out of two values, taking in account overflow.
403  *
404  * @param a First ACK value.
405  * @param b Second ACK value.
406  *
407  * @return Highest ACK value from the two.
408  */
409 uint32_t
410 GMC_max_pid (uint32_t a, uint32_t b);
411
412
413 /**
414  * Get the lower ACK value out of two values, taking in account overflow.
415  *
416  * @param a First ACK value.
417  * @param b Second ACK value.
418  *
419  * @return Lowest ACK value from the two.
420  */
421 uint32_t
422 GMC_min_pid (uint32_t a, uint32_t b);
423
424
425 /**
426  * Convert a message type into a string to help debug
427  * Generated with:
428  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
429  * REPLACE:     "    case \2: return "\1"; break;"
430  * 
431  * @param m Message type.
432  * 
433  * @return Human readable string description.
434  */
435 const char *
436 GNUNET_MESH_DEBUG_M2S (uint16_t m);
437
438 #endif