2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @author Bartlomiej Polot
30 #define MESH_DEBUG GNUNET_YES
32 #define INITIAL_WINDOW_SIZE 8
33 #define ACK_THRESHOLD INITIAL_WINDOW_SIZE / 2
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>
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
50 * API CALL (GNUNET_MESH_*) MESSAGE USED
51 * ------------------------ ------------
52 * connect GNUNET_MESH_ClientConnect
53 * disconnect None (network level disconnect)
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
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
66 * peer_blacklist GNUNET_MESH_PeerControl
67 * peer_unblacklist GNUNET_MESH_PeerControl
69 * notify_transmit_ready None (queue / GNUNET_CLIENT_ntf_tmt_rdy)
70 * notify_transmit_ready_cancel None (clear of internal data structures)
75 * data GNUNET_MESH_Unicast OR
76 * GNUNET_MESH_Multicast OR
77 * GNUNET_MESH_ToOrigin
78 * data ack GNUNET_MESH_LocalAck
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
85 /******************************************************************************/
86 /************************** CONSTANTS ******************************/
87 /******************************************************************************/
89 #define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI 0x80000000
90 #define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV 0xB0000000
92 #define HIGH_PID 0xFFFF0000
93 #define LOW_PID 0x0000FFFF
95 #define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
97 /******************************************************************************/
98 /************************** MESSAGES ******************************/
99 /******************************************************************************/
101 GNUNET_NETWORK_STRUCT_BEGIN
104 * Message for a client to register to the service
106 struct GNUNET_MESH_ClientConnect
109 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
111 * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
112 * sizeof(MESH_ApplicationType) * applications +
113 * sizeof(uint16_t) * types
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] */
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
129 typedef uint32_t MESH_TunnelNumber;
132 * Message for a client to create and destroy tunnels.
134 struct GNUNET_MESH_TunnelMessage
137 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
138 * GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[MAX|MIN]
140 * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
142 struct GNUNET_MessageHeader header;
145 * ID of a tunnel controlled by this client.
147 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
152 * Message for the service to let a client know about created tunnels.
154 struct GNUNET_MESH_TunnelNotification
157 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
159 * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
161 struct GNUNET_MessageHeader header;
164 * ID of a tunnel controlled by this client.
166 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
169 * Peer at the other end, if any
171 struct GNUNET_PeerIdentity peer;
176 * - request adding and deleting peers from a tunnel
177 * - notify the client that peers have connected:
179 * -- unrequested (new incoming tunnels)
180 * - notify the client that peers have disconnected
182 struct GNUNET_MESH_PeerControl
186 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL|[UN]BLACKLIST]
187 * (client to service, client created tunnel)
188 * GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
189 * (service to client)
191 * Size: sizeof(struct GNUNET_MESH_PeerControl)
193 struct GNUNET_MessageHeader header;
196 * ID of a tunnel controlled by this client.
198 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
201 * Peer to connect/disconnect.
203 struct GNUNET_PeerIdentity peer;
208 * Message for connecting to peers offering a service, by service number.
210 struct GNUNET_MESH_ConnectPeerByType
213 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
214 * GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
216 * Size: sizeof(struct GNUNET_MESH_ConnectPeerByType)
218 struct GNUNET_MessageHeader header;
221 * ID of a tunnel controlled by this client.
223 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
228 GNUNET_MESH_ApplicationType type GNUNET_PACKED;
233 * Message for connecting to peers offering a service, by service string.
235 struct GNUNET_MESH_ConnectPeerByString
238 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING
240 * Size: sizeof(struct GNUNET_MESH_ConnectPeerByString) + strlen (string)
242 struct GNUNET_MessageHeader header;
245 * ID of a tunnel controlled by this client.
247 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
249 /* String describing the service */
254 * Message to allow the client send more data to the service
255 * (always service -> client).
257 struct GNUNET_MESH_LocalAck
260 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
262 struct GNUNET_MessageHeader header;
265 * ID of the tunnel allowed to send more data.
267 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
270 * ID of the last packet allowed.
272 uint32_t max_pid GNUNET_PACKED;
276 GNUNET_NETWORK_STRUCT_END
278 /******************************************************************************/
279 /************************ ENUMERATIONS ****************************/
280 /******************************************************************************/
283 * All the states a peer participating in a tunnel can be in.
288 * Uninitialized status, should never appear in operation.
293 * Peer is the root and owner of the tree
298 * Peer only retransmits traffic, is not a final destination
303 * Path to the peer not known yet
308 * Request sent, not yet answered.
313 * Peer connected and ready to accept data
318 * Peer connected previosly but not responding
320 MESH_PEER_RECONNECTING
325 * Check if one pid is bigger than other, accounting for overflow.
327 * @param bigger Argument that should be bigger.
328 * @param smaller Argument that should be smaller.
330 * @return True if bigger (arg1) has a higher value than smaller (arg 2).
333 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
337 * Get the higher ACK value out of two values, taking in account overflow.
339 * @param a First ACK value.
340 * @param b Second ACK value.
342 * @return Highest ACK value from the two.
345 GMC_max_pid (uint32_t a, uint32_t b);
349 * Get the lower ACK value out of two values, taking in account overflow.
351 * @param a First ACK value.
352 * @param b Second ACK value.
354 * @return Lowest ACK value from the two.
357 GMC_min_pid (uint32_t a, uint32_t b);
361 * Convert a message type into a string to help debug
363 * FIND: "#define ([^ ]+)[ ]*([0-9]+)"
364 * REPLACE: " case \2: return "\1"; break;"
366 * @param m Message type.
368 * @return Human readable string description.
371 GNUNET_MESH_DEBUG_M2S (uint16_t m);