Added path testcase
[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
33 #include "platform.h"
34 #include "gnunet_common.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_peer_lib.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_protocols.h"
39 #include <gnunet_mesh_service_new.h>
40
41 /******************************************************************************/
42 /********************        MESH LOCAL MESSAGES      *************************/
43 /******************************************************************************/
44 /*  Any API call should be documented in the folowing table under API CALL.
45  *  Also, any message type should be documented in the following table, with the
46  * associated event.
47  *
48  * API CALL (GNUNET_MESH_*)             MESSAGE USED
49  * ------------------------             ------------
50  * connect                              GNUNET_MESH_ClientConnect
51  * disconnect                           None (network level disconnect)
52  *
53  * tunnel_create                        GNUNET_MESH_TunnelMessage
54  * tunnel_destroy                       GNUNET_MESH_TunnelMessage
55  *
56  * peer_request_connect_add             GNUNET_MESH_PeerControl
57  * peer_request_connect_del             GNUNET_MESH_PeerControl
58  * peer_request_connect_by_type         GNUNET_MESH_ConnectPeerByType
59  *
60  * notify_transmit_ready                *GNUNET_MESH_TransmitReady?*
61  * notify_transmit_ready_cancel         None (clear of internal data structures)
62  *
63  *
64  *
65  * EVENT                                MESSAGE USED
66  * -----                                ------------
67  * data                                 GNUNET_MESH_Data OR
68  *                                      GNUNET_MESH_DataBroadcast
69  * new incoming tunnel                  GNUNET_MESH_PeerControl
70  * peer connects to a tunnel            GNUNET_MESH_PeerControl
71  * peer disconnects from a tunnel       GNUNET_MESH_PeerControl
72  */
73
74 /******************************************************************************/
75 /**************************       CONSTANTS      ******************************/
76 /******************************************************************************/
77
78 #define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI 0x80000000
79 #define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV 0xB0000000
80
81 #define CORE_QUEUE_SIZE         10
82 #define LOCAL_QUEUE_SIZE        100
83
84 /******************************************************************************/
85 /**************************        MESSAGES      ******************************/
86 /******************************************************************************/
87
88 /**
89  * Message for a client to register to the service
90  */
91 struct GNUNET_MESH_ClientConnect
92 {
93     /**
94      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
95      *
96      * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
97      *       sizeof(MESH_ApplicationType) * applications +
98      *       sizeof(uint16_t) * types
99      */
100   struct GNUNET_MessageHeader header;
101   uint16_t applications GNUNET_PACKED;
102   uint16_t types GNUNET_PACKED;
103   /* uint16_t                 list_apps[applications]     */
104   /* uint16_t                 list_types[types]           */
105 };
106
107
108 /**
109  * Type for tunnel numbering.
110  * - Local tunnel numbers are >= 0x80000000
111  * - Global tunnel numbers are < 0x80000000
112  */
113 typedef uint32_t MESH_TunnelNumber;
114
115 /**
116  * Message for a client to create and destroy tunnels.
117  */
118 struct GNUNET_MESH_TunnelMessage
119 {
120     /**
121      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
122      *
123      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
124      */
125   struct GNUNET_MessageHeader header;
126
127     /**
128      * ID of a tunnel controlled by this client.
129      */
130   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
131 };
132
133
134 /**
135  * Message for the service to let a client know about created tunnels.
136  */
137 struct GNUNET_MESH_TunnelNotification
138 {
139     /**
140      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
141      *
142      * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
143      */
144   struct GNUNET_MessageHeader header;
145
146     /**
147      * ID of a tunnel controlled by this client.
148      */
149   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
150
151     /**
152      * Peer at the other end, if any
153      */
154   struct GNUNET_PeerIdentity peer;
155 };
156
157 /**
158  * Message for:
159  * - request adding and deleting peers from a tunnel
160  * - notify the client that peers have connected:
161  *   -- requested
162  *   -- unrequested (new incoming tunnels)
163  * - notify the client that peers have disconnected
164  */
165 struct GNUNET_MESH_PeerControl
166 {
167
168   /**
169    * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL]
170    *       (client to service, client created tunnel)
171    *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
172    *       (service to client)
173    *
174    * Size: sizeof(struct GNUNET_MESH_PeerControl)
175    */
176   struct GNUNET_MessageHeader header;
177
178   /**
179    * ID of a tunnel controlled by this client.
180    */
181   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
182
183   /**
184    * Peer to connect/disconnect.
185    */
186   struct GNUNET_PeerIdentity peer;
187 };
188
189
190 /**
191  * Message for connecting to peers offering a certain service.
192  */
193 struct GNUNET_MESH_ConnectPeerByType
194 {
195     /**
196      * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
197      *       GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
198      */
199   struct GNUNET_MessageHeader header;
200
201   /**
202    * ID of a tunnel controlled by this client.
203    */
204   MESH_TunnelNumber tunnel_id GNUNET_PACKED;
205
206   /**
207    * Type specification
208    */
209   GNUNET_MESH_ApplicationType type GNUNET_PACKED;
210 };
211
212
213 /******************************************************************************/
214 /************************        ENUMERATIONS      ****************************/
215 /******************************************************************************/
216
217 /**
218  * All the states a peer participating in a tunnel can be in.
219  */
220 enum MeshPeerState
221 {
222     /**
223      * Peer only retransmits traffic, is not a final destination
224      */
225   MESH_PEER_RELAY,
226
227     /**
228      * Path to the peer not known yet
229      */
230   MESH_PEER_SEARCHING,
231
232     /**
233      * Request sent, not yet answered.
234      */
235   MESH_PEER_WAITING,
236
237     /**
238      * Peer connected and ready to accept data
239      */
240   MESH_PEER_READY,
241
242     /**
243      * Peer connected previosly but not responding
244      */
245   MESH_PEER_RECONNECTING
246 };
247
248
249 /******************************************************************************/
250 /************************      DATA STRUCTURES     ****************************/
251 /******************************************************************************/
252
253 /**
254  * Information regarding a possible path to reach a single peer
255  */
256 struct MeshPeerPath
257 {
258
259     /**
260      * Linked list
261      */
262   struct MeshPeerPath *next;
263   struct MeshPeerPath *prev;
264
265     /**
266      * List of all the peers that form the path from origin to target.
267      */
268   GNUNET_PEER_Id *peers;
269
270     /**
271      * Number of peers (hops) in the path
272      */
273   unsigned int length;
274
275 };
276
277
278 /**
279  * Node of path tree for a tunnel
280  */
281 struct MeshTunnelPathNode
282 {
283   /**
284    * Tunnel this node belongs to (and therefore tree)
285    */
286   struct MeshTunnel *t;
287
288   /**
289    * Peer this node describes
290    */
291   GNUNET_PEER_Id peer;
292
293   /**
294    * Parent node in the tree
295    */
296   struct MeshTunnelPathNode *parent;
297
298   /**
299    * Array of children
300    */
301   struct MeshTunnelPathNode *children;
302
303   /**
304    * Number of children
305    */
306   unsigned int nchildren;
307
308     /**
309      * Status of the peer in the tunnel
310      */
311   enum MeshPeerState status;
312 };
313
314
315 /**
316  * Tree to reach all peers in the tunnel
317  */
318 struct MeshTunnelPath
319 {
320   /**
321    * How often to refresh the path
322    */
323   struct GNUNET_TIME_Relative refresh;
324
325   /**
326    * Tunnel this path belongs to
327    */
328   struct MeshTunnel *t;
329
330   /**
331    * Root node of peer tree
332    */
333   struct MeshTunnelPathNode *root;
334
335   /**
336    * Node that represents our position in the tree (for non local tunnels)
337    */
338   struct MeshTunnelPathNode *me;
339
340   /**
341    * Cache of all peers and the first hop to them.
342    * Indexed by Peer_Identity, contains a pointer to the PeerIdentity
343    * of 1st hop.
344    */
345   struct GNUNET_CONTAINER_MultiHashMap *first_hops;
346
347 };
348
349
350 /** FWD declaration */
351 struct MeshPeerInfo;
352
353 /**
354  * Struct containing all info possibly needed to build a package when called
355  * back by core.
356  */
357 struct MeshDataDescriptor
358 {
359     /** ID of the tunnel this packet travels in */
360   struct MESH_TunnelID *origin;
361
362     /** Ultimate destination of the packet */
363   GNUNET_PEER_Id destination;
364
365     /** Number of identical messages sent to different hops (multicast) */
366   unsigned int copies;
367
368     /** Size of the data */
369   size_t size;
370
371     /** Client that asked for the transmission, if any */
372   struct GNUNET_SERVER_Client *client;
373
374     /** Who was is message being sent to */
375   struct MeshPeerInfo *peer;
376
377     /** Which handler was used to request the transmission */
378   unsigned int handler_n;
379
380   /* Data at the end */
381 };
382
383
384 /**
385  * Struct containing all information regarding a given peer
386  */
387 struct MeshPeerInfo
388 {
389     /**
390      * ID of the peer
391      */
392   GNUNET_PEER_Id id;
393
394     /**
395      * Last time we heard from this peer
396      */
397   struct GNUNET_TIME_Absolute last_contact;
398
399     /**
400      * Number of attempts to reconnect so far
401      */
402   int n_reconnect_attempts;
403
404     /**
405      * Paths to reach the peer, ordered by ascending hop count
406      */
407   struct MeshPeerPath *path_head;
408
409     /**
410      * Paths to reach the peer, ordered by ascending hop count
411      */
412   struct MeshPeerPath *path_tail;
413
414     /**
415      * Handle to stop the DHT search for a path to this peer
416      */
417   struct GNUNET_DHT_GetHandle *dhtget;
418
419     /**
420      * Handles to stop queued transmissions for this peer
421      */
422   struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
423
424     /**
425      * Pointer to info stuctures used as cls for queued transmissions
426      */
427   struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
428
429     /**
430      * Array of tunnels this peer participates in
431      * (most probably a small amount, therefore not a hashmap)
432      * When the path to the peer changes, notify these tunnels to let them
433      * re-adjust their path trees.
434      */
435   struct MeshTunnel **tunnels;
436
437     /**
438      * Number of tunnels above
439      */
440   unsigned int ntunnels;
441 };
442
443
444 /**
445  * Data scheduled to transmit (to local client or remote peer)
446  */
447 struct MeshQueue
448 {
449     /**
450      * Double linked list
451      */
452   struct MeshQueue *next;
453   struct MeshQueue *prev;
454
455     /**
456      * Target of the data (NULL if target is client)
457      */
458   struct MeshPeerInfo *peer;
459
460     /**
461      * Client to send the data to (NULL if target is peer)
462      */
463   struct MeshClient *client;
464
465     /**
466      * Size of the message to transmit
467      */
468   unsigned int size;
469
470     /**
471      * How old is the data?
472      */
473   struct GNUNET_TIME_Absolute timestamp;
474
475     /**
476      * Data itself
477      */
478   struct GNUNET_MessageHeader *data;
479 };
480
481 /**
482  * Globally unique tunnel identification (owner + number)
483  * DO NOT USE OVER THE NETWORK
484  */
485 struct MESH_TunnelID
486 {
487     /**
488      * Node that owns the tunnel
489      */
490   GNUNET_PEER_Id oid;
491
492     /**
493      * Tunnel number to differentiate all the tunnels owned by the node oid
494      * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
495      */
496   MESH_TunnelNumber tid;
497 };
498
499
500 struct MeshClient;              /* FWD declaration */
501
502 /**
503  * Struct containing all information regarding a tunnel
504  * For an intermediate node the improtant info used will be:
505  * - id        Tunnel unique identification
506  * - paths[0]  To know where to send it next
507  * - metainfo: ready, speeds, accounting
508  */
509 struct MeshTunnel
510 {
511     /**
512      * Tunnel ID
513      */
514   struct MESH_TunnelID id;
515
516     /**
517      * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
518      */
519   MESH_TunnelNumber local_tid;
520
521     /**
522      * Last time the tunnel was used
523      */
524   struct GNUNET_TIME_Absolute timestamp;
525
526     /**
527      * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
528      */
529   struct GNUNET_CONTAINER_MultiHashMap *peers;
530
531     /**
532      * Number of peers that are connected and potentially ready to receive data
533      */
534   unsigned int peers_ready;
535
536     /**
537      * Number of peers that have been added to the tunnel
538      */
539   unsigned int peers_total;
540
541     /**
542      * Client owner of the tunnel, if any
543      */
544   struct MeshClient *client;
545
546     /**
547      * Messages ready to transmit
548      */
549   struct MeshQueue *queue_head;
550   struct MeshQueue *queue_tail;
551
552   /**
553    * Tunnel paths
554    */
555   struct MeshTunnelPath *tree;
556
557   /**
558    * Task to keep the used paths alive
559    */
560   GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
561 };
562
563
564 /**
565  * Info needed to work with tunnel paths and peers
566  */
567 struct MeshPathInfo
568 {
569   /**
570    * Tunnel
571    */
572   struct MeshTunnel *t;
573
574   /**
575    * Destination peer
576    */
577   struct MeshPeerInfo *peer;
578
579   /**
580    * Path itself
581    */
582   struct MeshPeerPath *path;
583 };
584
585
586 /**
587  * Struct containing information about a client of the service
588  */
589 struct MeshClient
590 {
591     /**
592      * Linked list
593      */
594   struct MeshClient *next;
595   struct MeshClient *prev;
596
597     /**
598      * Tunnels that belong to this client, indexed by local id
599      */
600   struct GNUNET_CONTAINER_MultiHashMap *tunnels;
601
602     /**
603      * Handle to communicate with the client
604      */
605   struct GNUNET_SERVER_Client *handle;
606
607     /**
608      * Applications that this client has claimed to provide
609      */
610   struct GNUNET_CONTAINER_MultiHashMap *apps;
611
612     /**
613      * Messages that this client has declared interest in
614      */
615   struct GNUNET_CONTAINER_MultiHashMap *types;
616
617     /**
618      * Used to search peers offering a service
619      */
620   struct GNUNET_DHT_GetHandle *dht_get_type;
621
622 #if MESH_DEBUG
623     /**
624      * ID of the client, for debug messages
625      */
626   unsigned int id;
627 #endif
628
629 };
630
631 #endif