- implementation of configurable regex compression
[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   /* regex  */
197 };
198
199
200 /**
201  * Message for:
202  * - request adding and deleting peers from a tunnel
203  * - notify the client that peers have connected:
204  *   -- requested
205  *   -- unrequested (new incoming tunnels)
206  * - notify the client that peers have disconnected
207  */
208 struct GNUNET_MESH_PeerControl
209 {
210
211     /**
212      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL|[UN]BLACKLIST]
213      *       (client to service, client created tunnel)
214      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
215      *       (service to client)
216      *
217      * Size: sizeof(struct GNUNET_MESH_PeerControl)
218      */
219   struct GNUNET_MessageHeader header;
220
221     /**
222      * ID of a tunnel controlled by this client.
223      */
224   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
225
226     /**
227      * Peer to connect/disconnect.
228      */
229   struct GNUNET_PeerIdentity peer;
230 };
231
232
233 /**
234  * Message for connecting to peers offering a service, by service number.
235  */
236 struct GNUNET_MESH_ConnectPeerByType
237 {
238     /**
239      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
240      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
241      * 
242      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByType)
243      */
244   struct GNUNET_MessageHeader header;
245
246     /**
247      * ID of a tunnel controlled by this client.
248      */
249   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
250
251     /**
252      * Type specification
253      */
254   GNUNET_MESH_ApplicationType type GNUNET_PACKED;
255 };
256
257
258 /**
259  * Message for connecting to peers offering a service, by service string.
260  */
261 struct GNUNET_MESH_ConnectPeerByString
262 {
263     /**
264      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING
265      * 
266      * Size: sizeof(struct GNUNET_MESH_ConnectPeerByString) + strlen (string)
267      */
268   struct GNUNET_MessageHeader header;
269
270     /**
271      * ID of a tunnel controlled by this client.
272      */
273   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
274
275   /* String describing the service */
276 };
277
278
279 /**
280  * Message to allow the client send more data to the service
281  * (always service -> client).
282  */
283 struct GNUNET_MESH_LocalAck
284 {
285     /**
286      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
287      */
288   struct GNUNET_MessageHeader header;
289
290     /**
291      * ID of the tunnel allowed to send more data.
292      */
293   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
294
295     /**
296      * ID of the last packet allowed.
297      */
298   uint32_t max_pid GNUNET_PACKED;
299 };
300
301
302 GNUNET_NETWORK_STRUCT_END
303
304 /******************************************************************************/
305 /************************        ENUMERATIONS      ****************************/
306 /******************************************************************************/
307
308 /**
309  * All the states a peer participating in a tunnel can be in.
310  */
311 enum MeshPeerState
312 {
313     /**
314      * Uninitialized status, should never appear in operation.
315      */
316   MESH_PEER_INVALID,
317
318     /**
319      * Peer is the root and owner of the tree
320      */
321   MESH_PEER_ROOT,
322
323     /**
324      * Peer only retransmits traffic, is not a final destination
325      */
326   MESH_PEER_RELAY,
327
328     /**
329      * Path to the peer not known yet
330      */
331   MESH_PEER_SEARCHING,
332
333     /**
334      * Request sent, not yet answered.
335      */
336   MESH_PEER_WAITING,
337
338     /**
339      * Peer connected and ready to accept data
340      */
341   MESH_PEER_READY,
342
343     /**
344      * Peer connected previosly but not responding
345      */
346   MESH_PEER_RECONNECTING
347 };
348
349
350 /**
351  * Check if one pid is bigger than other, accounting for overflow.
352  *
353  * @param bigger Argument that should be bigger.
354  * @param smaller Argument that should be smaller.
355  *
356  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
357  */
358 int
359 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
360
361
362 /**
363  * Get the higher ACK value out of two values, taking in account overflow.
364  *
365  * @param a First ACK value.
366  * @param b Second ACK value.
367  *
368  * @return Highest ACK value from the two.
369  */
370 uint32_t
371 GMC_max_pid (uint32_t a, uint32_t b);
372
373
374 /**
375  * Get the lower ACK value out of two values, taking in account overflow.
376  *
377  * @param a First ACK value.
378  * @param b Second ACK value.
379  *
380  * @return Lowest ACK value from the two.
381  */
382 uint32_t
383 GMC_min_pid (uint32_t a, uint32_t b);
384
385
386 /**
387  * Convert a message type into a string to help debug
388  * Generated with:
389  * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
390  * REPLACE:     "    case \2: return "\1"; break;"
391  * 
392  * @param m Message type.
393  * 
394  * @return Human readable string description.
395  */
396 const char *
397 GNUNET_MESH_DEBUG_M2S (uint16_t m);
398
399 #endif