e74206e25593fa9f9fa3b88a4e747d994867a3cb
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh.c
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  * @file mesh/gnunet-service-mesh.c
23  * @brief GNUnet MESH service
24  * @author Bartlomiej Polot
25  *
26  * STRUCTURE:
27  * - DATA STRUCTURES
28  * - GLOBAL VARIABLES
29  * - GENERAL HELPERS
30  * - PERIODIC FUNCTIONS
31  * - MESH NETWORK HANDLER HELPERS
32  * - MESH NETWORK HANDLES
33  * - MESH LOCAL HANDLER HELPERS
34  * - MESH LOCAL HANDLES
35  * - MAIN FUNCTIONS (main & run)
36  *
37  * TODO:
38  * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39  * - partial disconnect reporting -- same as error reporting?
40  * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41  * - add ping message
42  * - relay corking down to core
43  * - set ttl relative to tree depth
44  * - Add data ACK count in path ACK
45  * TODO END
46  */
47
48 #include "platform.h"
49 #include "mesh.h"
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
57
58 #define MESH_BLOOM_SIZE         128
59
60 #define MESH_DEBUG_DHT          GNUNET_YES
61 #define MESH_DEBUG_CONNECTION   GNUNET_NO
62
63 #define INITIAL_WINDOW_SIZE     2
64 #define ACK_THRESHOLD           INITIAL_WINDOW_SIZE / 2
65
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #else
69 #define DEBUG_CONN(...)
70 #endif
71
72 #if MESH_DEBUG_DHT
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
74 #else
75 #define DEBUG_DHT(...)
76 #endif
77
78 /******************************************************************************/
79 /************************      DATA STRUCTURES     ****************************/
80 /******************************************************************************/
81
82 /** FWD declaration */
83 struct MeshPeerInfo;
84 struct MeshClient;
85
86
87 /**
88  * Struct representing a piece of data being sent to other peers
89  */
90 struct MeshData
91 {
92   /** Tunnel it belongs to. */
93   struct MeshTunnel *t;
94
95   /** In case of a multicast, task to allow a client to send more data if
96    * some neighbor is too slow. */
97   GNUNET_SCHEDULER_TaskIdentifier *task;
98
99   /** How many remaining neighbors we need to send this to. */
100   unsigned int *reference_counter;
101
102   /** Size of the data. */
103   size_t data_len;
104
105   /** Data itself */
106   void *data;
107 };
108
109
110 /**
111  * Struct containing info about a queued transmission to this peer
112  */
113 struct MeshPeerQueue
114 {
115     /**
116       * DLL next
117       */
118   struct MeshPeerQueue *next;
119
120     /**
121       * DLL previous
122       */
123   struct MeshPeerQueue *prev;
124
125     /**
126      * Peer this transmission is directed to.
127      */
128   struct MeshPeerInfo *peer;
129
130     /**
131      * Tunnel this message belongs to.
132      */
133   struct MeshTunnel *tunnel;
134
135     /**
136      * Pointer to info stucture used as cls.
137      */
138   void *cls;
139
140     /**
141      * Type of message
142      */
143   uint16_t type;
144
145     /**
146      * Size of the message
147      */
148   size_t size;
149 };
150
151
152 /**
153  * Struct containing all info possibly needed to build a package when called
154  * back by core.
155  */
156 struct MeshTransmissionDescriptor
157 {
158     /** ID of the tunnel this packet travels in */
159   struct MESH_TunnelID *origin;
160
161     /** Who was this message being sent to */
162   struct MeshPeerInfo *peer;
163
164     /** Ultimate destination of the packet */
165   GNUNET_PEER_Id destination;
166
167     /** Data descriptor */
168   struct MeshData* mesh_data;
169 };
170
171
172 /**
173  * Struct containing all information regarding a given peer
174  */
175 struct MeshPeerInfo
176 {
177     /**
178      * ID of the peer
179      */
180   GNUNET_PEER_Id id;
181
182     /**
183      * Last time we heard from this peer
184      */
185   struct GNUNET_TIME_Absolute last_contact;
186
187     /**
188      * Number of attempts to reconnect so far
189      */
190   int n_reconnect_attempts;
191
192     /**
193      * Paths to reach the peer, ordered by ascending hop count
194      */
195   struct MeshPeerPath *path_head;
196
197     /**
198      * Paths to reach the peer, ordered by ascending hop count
199      */
200   struct MeshPeerPath *path_tail;
201
202     /**
203      * Handle to stop the DHT search for a path to this peer
204      */
205   struct GNUNET_DHT_GetHandle *dhtget;
206
207     /**
208      * Closure given to the DHT GET
209      */
210   struct MeshPathInfo *dhtgetcls;
211
212     /**
213      * Array of tunnels this peer participates in
214      * (most probably a small amount, therefore not a hashmap)
215      * When the path to the peer changes, notify these tunnels to let them
216      * re-adjust their path trees.
217      */
218   struct MeshTunnel **tunnels;
219
220     /**
221      * Number of tunnels this peers participates in
222      */
223   unsigned int ntunnels;
224
225    /**
226     * Transmission queue to core DLL head
227     */
228   struct MeshPeerQueue *queue_head;
229
230    /**
231     * Transmission queue to core DLL tail
232     */
233    struct MeshPeerQueue *queue_tail;
234
235    /**
236     * How many messages are in the queue to this peer.
237     */
238    unsigned int queue_n;
239
240    /**
241     * Handle to for queued transmissions
242     */
243   struct GNUNET_CORE_TransmitHandle *core_transmit;
244 };
245
246
247 /**
248  * Globally unique tunnel identification (owner + number)
249  * DO NOT USE OVER THE NETWORK
250  */
251 struct MESH_TunnelID
252 {
253     /**
254      * Node that owns the tunnel
255      */
256   GNUNET_PEER_Id oid;
257
258     /**
259      * Tunnel number to differentiate all the tunnels owned by the node oid
260      * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
261      */
262   MESH_TunnelNumber tid;
263 };
264
265
266 /**
267  * Struct containing all information regarding a tunnel
268  * For an intermediate node the improtant info used will be:
269  * - id        Tunnel unique identification
270  * - paths[0]  To know where to send it next
271  * - metainfo: ready, speeds, accounting
272  */
273 struct MeshTunnel
274 {
275     /**
276      * Tunnel ID
277      */
278   struct MESH_TunnelID id;
279
280     /**
281      * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
282      */
283   MESH_TunnelNumber local_tid;
284
285     /**
286      * Local tunnel number for local destination clients (incoming number)
287      * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
288      * number.
289      */
290   MESH_TunnelNumber local_tid_dest;
291
292     /**
293      * Is the speed on the tunnel limited to the slowest peer?
294      */
295   int speed_min;
296
297     /**
298      * Is the tunnel bufferless (minimum latency)?
299      */
300   int nobuffer;
301
302     /**
303      * Packet ID of the last fwd packet seen (sent/retransmitted/received).
304      */
305   uint32_t fwd_pid;
306
307     /**
308      * Packet ID of the last bck packet sent (unique counter per hop).
309      */
310   uint32_t bck_pid;
311
312   /**
313      * SKIP value for this tunnel.
314      */
315   uint32_t skip;
316
317     /**
318      * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
319      * Contains the Flow Control info: FWD ACK value received,
320      * last BCK ACK sent, PID and SKIP values.
321      */
322   struct GNUNET_CONTAINER_MultiHashMap *children_fc;
323
324     /**
325      * Last ACK sent towards the origin (for traffic towards leaf node).
326      */
327   uint32_t last_fwd_ack;
328
329   /**
330    * BCK ACK value received from the hop towards the owner of the tunnel,
331    * (previous node / owner): up to what message PID can we sent back to him.
332    */
333   uint32_t bck_ack;
334
335     /**
336      * How many messages are in the forward queue (towards leaves).
337      */
338   unsigned int fwd_queue_n;
339
340     /**
341      * How many messages do we accept in the forward queue.
342      */
343   unsigned int fwd_queue_max;
344
345     /**
346      * How many messages are in the backward queue (towards origin).
347      */
348   unsigned int bck_queue_n;
349
350     /**
351      * How many messages do we accept in the backward queue.
352     */
353    unsigned int bck_queue_max;
354
355     /**
356      * Last time the tunnel was used
357      */
358   struct GNUNET_TIME_Absolute timestamp;
359
360     /**
361      * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
362      * containing peers added by id or by type, not intermediate peers.
363      */
364   struct GNUNET_CONTAINER_MultiHashMap *peers;
365
366     /**
367      * Number of peers that are connected and potentially ready to receive data
368      */
369   unsigned int peers_ready;
370
371     /**
372      * Number of peers that have been added to the tunnel
373      */
374   unsigned int peers_total;
375
376     /**
377      * Client owner of the tunnel, if any
378      */
379   struct MeshClient *owner;
380
381     /**
382      * Clients that have been informed about and want to stay in the tunnel.
383      */
384   struct MeshClient **clients;
385
386     /**
387      * Flow control info for each client.
388      */
389   struct MeshTunnelClientInfo *clients_fc;
390
391   /**
392      * Number of elements in clients/clients_fc
393      */
394   unsigned int nclients;
395
396     /**
397      * Clients that have been informed but requested to leave the tunnel.
398      */
399   struct MeshClient **ignore;
400
401     /**
402      * Number of elements in clients
403      */
404   unsigned int nignore;
405
406     /**
407      * Blacklisted peers
408      */
409   GNUNET_PEER_Id *blacklisted;
410
411     /**
412      * Number of elements in blacklisted
413      */
414   unsigned int nblacklisted;
415
416   /**
417    * Bloomfilter (for peer identities) to stop circular routes
418    */
419   char bloomfilter[MESH_BLOOM_SIZE];
420
421   /**
422    * Tunnel paths
423    */
424   struct MeshTunnelTree *tree;
425
426   /**
427    * Application type we are looking for in this tunnel
428    */
429   GNUNET_MESH_ApplicationType type;
430
431     /**
432      * Used to search peers offering a service
433      */
434   struct GNUNET_DHT_GetHandle *dht_get_type;
435
436     /**
437      * Initial context of the regex search for a connect_by_string
438      */
439   struct MeshRegexSearchContext *regex_ctx;
440
441     /**
442      * Task to keep the used paths alive
443      */
444   GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
445
446     /**
447      * Task to destroy the tunnel after timeout
448      *
449      * FIXME: merge the two? a tunnel will have either
450      * a path refresh OR a timeout, never both!
451      */
452   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
453
454     /**
455      * Flag to signal the destruction of the tunnel.
456      * If this is set GNUNET_YES the tunnel will be destroyed
457      * when the queue is empty.
458      */
459   int destroy;
460 };
461
462
463 /**
464  * Info about a child node in a tunnel, needed to perform flow control.
465  */
466 struct MeshTunnelChildInfo
467 {
468     /**
469      * ID of the child node.
470      */
471   GNUNET_PEER_Id id;
472
473     /**
474      * SKIP value.
475      */
476   uint32_t skip;
477
478     /**
479      * Last sent PID.
480      */
481   uint32_t pid;
482
483     /**
484      * Maximum PID allowed (FWD ACK received).
485      */
486   uint32_t fwd_ack;
487
488     /**
489      * Last ACK sent to that child (BCK ACK).
490      */
491   uint32_t bck_ack;
492 };
493
494
495 /**
496  * Info about a leaf client of a tunnel, needed to perform flow control.
497  */
498 struct MeshTunnelClientInfo
499 {
500   /**
501    * PID of the last packet sent to the client (FWD).
502    */
503   uint32_t fwd_pid;
504
505   /**
506    * PID of the last packet received from the client (BCK).
507    */
508   uint32_t bck_pid;
509
510   /**
511    * Maximum PID allowed (FWD ACK received).
512    */
513   uint32_t fwd_ack;
514   
515   /**
516    * Last ACK sent to that child (BCK ACK).
517    */
518   uint32_t bck_ack;
519 };
520
521
522
523 /**
524  * Info collected during iteration of child nodes in order to get the ACK value
525  * for a tunnel.
526  */
527 struct MeshTunnelChildIteratorContext
528 {
529     /**
530      * Tunnel whose info is being collected.
531      */
532   struct MeshTunnel *t;
533
534     /**
535      * Is this context initialized? Is the value in max_child_ack valid?
536      */
537   int init;
538
539     /**
540      * Maximum child ACK so far.
541      */
542   uint32_t max_child_ack;
543
544     /**
545      * Number of children nodes
546      */
547   unsigned int nchildren;
548 };
549
550
551 /**
552  * Info needed to work with tunnel paths and peers
553  */
554 struct MeshPathInfo
555 {
556   /**
557    * Tunnel
558    */
559   struct MeshTunnel *t;
560
561   /**
562    * Neighbouring peer to whom we send the packet to
563    */
564   struct MeshPeerInfo *peer;
565
566   /**
567    * Path itself
568    */
569   struct MeshPeerPath *path;
570 };
571
572
573 /**
574  * Struct containing information about a client of the service
575  */
576 struct MeshClient
577 {
578     /**
579      * Linked list next
580      */
581   struct MeshClient *next;
582
583     /**
584      * Linked list prev
585      */
586   struct MeshClient *prev;
587
588     /**
589      * Tunnels that belong to this client, indexed by local id
590      */
591   struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
592
593    /**
594      * Tunnels this client has accepted, indexed by incoming local id
595      */
596   struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
597
598    /**
599      * Tunnels this client has rejected, indexed by incoming local id
600      */
601   struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
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      * Whether the client is active or shutting down (don't send confirmations
619      * to a client that is shutting down.
620      */
621   int shutting_down;
622
623     /**
624      * ID of the client, mainly for debug messages
625      */
626   unsigned int id;
627   
628     /**
629      * Regular expressions describing the services offered by this client.
630      */
631   char **regexes; // FIXME add timeout? API to remove a regex?
632
633     /**
634      * Number of regular expressions in regexes.
635      */
636   unsigned int n_regex;
637
638     /**
639      * Task to refresh all regular expresions in the DHT.
640      */
641   GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
642
643 };
644
645
646 /**
647  * Struct to keep information of searches of services described by a regex
648  * using a user-provided string service description.
649  */
650 struct MeshRegexSearchInfo
651 {
652     /**
653      * Which tunnel is this for
654      */
655   struct MeshTunnel *t;
656
657     /**
658      * User provided description of the searched service.
659      */
660   char *description;
661
662     /**
663      * Part of the description already consumed by the search.
664      */
665   size_t position;
666
667     /**
668      * Running DHT GETs.
669      */
670   struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
671
672     /**
673      * Results from running DHT GETs.
674      */
675   struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
676
677     /**
678      * Contexts, for each running DHT GET. Free all on end of search.
679      */
680   struct MeshRegexSearchContext **contexts;
681
682     /**
683      * Number of contexts (branches/steps in search).
684      */
685   unsigned int n_contexts;
686
687     /**
688      * Peer that is connecting via connect_by_string. When connected, free ctx.
689      */
690   GNUNET_PEER_Id peer;
691
692     /**
693      * Other peers that are found but not yet being connected to.
694      */
695   GNUNET_PEER_Id *peers;
696
697     /**
698      * Number of elements in peers.
699      */
700   unsigned int n_peers;
701
702     /**
703      * Next peer to try to connect to.
704      */
705   unsigned int i_peer;
706
707     /**
708      * Timeout for a connect attempt.
709      * When reached, try to connect to a different peer, if any. If not,
710      * try the same peer again.
711      */
712   GNUNET_SCHEDULER_TaskIdentifier timeout;
713
714 };
715
716 /**
717  * Struct to keep state of running searches that have consumed a part of
718  * the inital string.
719  */
720 struct MeshRegexSearchContext
721 {
722     /**
723      * Part of the description already consumed by
724      * this particular search branch.
725      */
726   size_t position;
727
728     /**
729      * Information about the search.
730      */
731   struct MeshRegexSearchInfo *info;
732
733 };
734
735 /******************************************************************************/
736 /************************      DEBUG FUNCTIONS     ****************************/
737 /******************************************************************************/
738
739 #if MESH_DEBUG
740 /**
741  * GNUNET_SCHEDULER_Task for printing a message after some operation is done
742  * @param cls string to print
743  * @param success  GNUNET_OK if the PUT was transmitted,
744  *                GNUNET_NO on timeout,
745  *                GNUNET_SYSERR on disconnect from service
746  *                after the PUT message was transmitted
747  *                (so we don't know if it was received or not)
748  */
749
750 #if 0
751 static void
752 mesh_debug (void *cls, int success)
753 {
754   char *s = cls;
755
756   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
757 }
758 #endif
759
760 #endif
761
762 /******************************************************************************/
763 /***********************      GLOBAL VARIABLES     ****************************/
764 /******************************************************************************/
765
766
767 /**
768  * Configuration parameters
769  */
770 static struct GNUNET_TIME_Relative refresh_path_time;
771 static struct GNUNET_TIME_Relative app_announce_time;
772 static struct GNUNET_TIME_Relative id_announce_time;
773 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
774 static struct GNUNET_TIME_Relative connect_timeout;
775 static long long unsigned int default_ttl;
776 static long long unsigned int dht_replication_level;
777 static long long unsigned int max_tunnels;
778 static long long unsigned int max_msgs_queue;
779
780 /**
781  * DLL with all the clients, head.
782  */
783 static struct MeshClient *clients;
784
785 /**
786  * DLL with all the clients, tail.
787  */
788 static struct MeshClient *clients_tail;
789
790 /**
791  * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
792  */
793 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
794
795 /**
796  * Number of tunnels known.
797  */
798 static unsigned long long n_tunnels;
799
800 /**
801  * Tunnels incoming, indexed by MESH_TunnelNumber
802  * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
803  */
804 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
805
806 /**
807  * Peers known, indexed by PeerIdentity (MeshPeerInfo).
808  */
809 static struct GNUNET_CONTAINER_MultiHashMap *peers;
810
811 /*
812  * Handle to communicate with transport
813  */
814 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
815
816 /**
817  * Handle to communicate with core.
818  */
819 static struct GNUNET_CORE_Handle *core_handle;
820
821 /**
822  * Handle to use DHT.
823  */
824 static struct GNUNET_DHT_Handle *dht_handle;
825
826 /**
827  * Handle to server.
828  */
829 static struct GNUNET_SERVER_Handle *server_handle;
830
831 /**
832  * Handle to the statistics service.
833  */
834 static struct GNUNET_STATISTICS_Handle *stats;
835
836 /**
837  * Notification context, to send messages to local clients.
838  */
839 static struct GNUNET_SERVER_NotificationContext *nc;
840
841 /**
842  * Local peer own ID (memory efficient handle).
843  */
844 static GNUNET_PEER_Id myid;
845
846 /**
847  * Local peer own ID (full value).
848  */
849 static struct GNUNET_PeerIdentity my_full_id;
850
851 /**
852  * Own private key.
853  */
854 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
855
856 /**
857  * Own public key.
858  */
859 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
860
861 /**
862  * Tunnel ID for the next created tunnel (global tunnel number).
863  */
864 static MESH_TunnelNumber next_tid;
865
866 /**
867  * Tunnel ID for the next incoming tunnel (local tunnel number).
868  */
869 static MESH_TunnelNumber next_local_tid;
870
871 /**
872  * All application types provided by this peer.
873  */
874 static struct GNUNET_CONTAINER_MultiHashMap *applications;
875
876 /**
877  * All message types clients of this peer are interested in.
878  */
879 static struct GNUNET_CONTAINER_MultiHashMap *types;
880
881 /**
882  * Task to periodically announce provided applications.
883  */
884 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
885
886 /**
887  * Task to periodically announce itself in the network.
888  */
889 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
890
891 /**
892  * Next ID to assign to a client.
893  */
894 unsigned int next_client_id;
895
896
897 /******************************************************************************/
898 /***********************         DECLARATIONS        **************************/
899 /******************************************************************************/
900
901 /* FIXME move declarations here */
902
903 /**
904  * Function to process paths received for a new peer addition. The recorded
905  * paths form the initial tunnel, which can be optimized later.
906  * Called on each result obtained for the DHT search.
907  *
908  * @param cls closure
909  * @param exp when will this value expire
910  * @param key key of the result
911  * @param type type of the result
912  * @param size number of bytes in data
913  * @param data pointer to the result data
914  */
915 static void
916 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
917                     const struct GNUNET_HashCode * key,
918                     const struct GNUNET_PeerIdentity *get_path,
919                     unsigned int get_path_length,
920                     const struct GNUNET_PeerIdentity *put_path,
921                     unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
922                     size_t size, const void *data);
923
924
925 /**
926  * Function to process DHT string to regex matching.
927  * Called on each result obtained for the DHT search.
928  *
929  * @param cls closure (search context)
930  * @param exp when will this value expire
931  * @param key key of the result
932  * @param get_path path of the get request (not used)
933  * @param get_path_length lenght of get_path (not used)
934  * @param put_path path of the put request (not used)
935  * @param put_path_length length of the put_path (not used)
936  * @param type type of the result
937  * @param size number of bytes in data
938  * @param data pointer to the result data
939  *
940  * TODO: re-issue the request after certain time? cancel after X results?
941  */
942 static void
943 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
944                         const struct GNUNET_HashCode * key,
945                         const struct GNUNET_PeerIdentity *get_path,
946                         unsigned int get_path_length,
947                         const struct GNUNET_PeerIdentity *put_path,
948                         unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
949                         size_t size, const void *data);
950
951
952 /**
953  * Function to process DHT string to regex matching.
954  * Called on each result obtained for the DHT search.
955  *
956  * @param cls closure (search context)
957  * @param exp when will this value expire
958  * @param key key of the result
959  * @param get_path path of the get request (not used)
960  * @param get_path_length lenght of get_path (not used)
961  * @param put_path path of the put request (not used)
962  * @param put_path_length length of the put_path (not used)
963  * @param type type of the result
964  * @param size number of bytes in data
965  * @param data pointer to the result data
966  */
967 static void
968 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
969                                const struct GNUNET_HashCode * key,
970                                const struct GNUNET_PeerIdentity *get_path,
971                                unsigned int get_path_length,
972                                const struct GNUNET_PeerIdentity *put_path,
973                                unsigned int put_path_length,
974                                enum GNUNET_BLOCK_Type type,
975                                size_t size, const void *data);
976
977
978 /**
979  * Retrieve the MeshPeerInfo stucture associated with the peer, create one
980  * and insert it in the appropiate structures if the peer is not known yet.
981  *
982  * @param peer Short identity of the peer.
983  *
984  * @return Existing or newly created peer info.
985  */
986 static struct MeshPeerInfo *
987 peer_info_get_short (const GNUNET_PEER_Id peer);
988
989
990 /**
991  * Try to establish a new connection to this peer.
992  * Use the best path for the given tunnel.
993  * If the peer doesn't have any path to it yet, try to get one.
994  * If the peer already has some path, send a CREATE PATH towards it.
995  *
996  * @param peer PeerInfo of the peer.
997  * @param t Tunnel for which to create the path, if possible.
998  */
999 static void
1000 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1001
1002
1003 /**
1004  * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1005  * needed rescources.
1006  * If peer already exists, reevaluate shortest path and change if different.
1007  *
1008  * @param t Tunnel we want to add a new peer to
1009  * @param peer PeerInfo of the peer being added
1010  *
1011  */
1012 static void
1013 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1014
1015
1016 /**
1017  * Removes an explicit path from a tunnel, freeing all intermediate nodes
1018  * that are no longer needed, as well as nodes of no longer reachable peers.
1019  * The tunnel itself is also destoyed if results in a remote empty tunnel.
1020  *
1021  * @param t Tunnel from which to remove the path.
1022  * @param peer Short id of the peer which should be removed.
1023  */
1024 static void
1025 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1026
1027
1028 /**
1029  * Search for a tunnel by global ID using full PeerIdentities.
1030  *
1031  * @param oid owner of the tunnel.
1032  * @param tid global tunnel number.
1033  *
1034  * @return tunnel handler, NULL if doesn't exist.
1035  */
1036 static struct MeshTunnel *
1037 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1038
1039
1040 /**
1041  * Delete an active client from the tunnel.
1042  *
1043  * @param t Tunnel.
1044  * @param c Client.
1045  */
1046 static void
1047 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1048
1049 /**
1050  * Notify a tunnel that a connection has broken that affects at least
1051  * some of its peers.
1052  *
1053  * @param t Tunnel affected.
1054  * @param p1 Peer that got disconnected from p2.
1055  * @param p2 Peer that got disconnected from p1.
1056  *
1057  * @return Short ID of the peer disconnected (either p1 or p2).
1058  *         0 if the tunnel remained unaffected.
1059  */
1060 static GNUNET_PEER_Id
1061 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1062                                  GNUNET_PEER_Id p2);
1063
1064
1065 /**
1066  * Get the current ack value for a tunnel, for data going from root to leaves,
1067  * taking in account the tunnel mode and the status of all children and clients.
1068  *
1069  * @param t Tunnel.
1070  *
1071  * @return Maximum PID allowed.
1072  */
1073 static uint32_t
1074 tunnel_get_fwd_ack (struct MeshTunnel *t);
1075
1076
1077 /**
1078  * Add a client to a tunnel, initializing all needed data structures.
1079  * 
1080  * @param t Tunnel to which add the client.
1081  * @param c Client which to add to the tunnel.
1082  */
1083 static void
1084 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1085
1086
1087 /**
1088  * Iterator over edges in a regex block retrieved from the DHT.
1089  *
1090  * @param cls Closure.
1091  * @param token Token that follows to next state.
1092  * @param len Lenght of token.
1093  * @param key Hash of next state.
1094  *
1095  * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1096  */
1097 static int
1098 regex_edge_iterator (void *cls,
1099                      const char *token,
1100                      size_t len,
1101                      const struct GNUNET_HashCode *key);
1102
1103
1104 /**
1105  * Find a path to a peer that offers a regex servcie compatible
1106  * with a given string.
1107  * 
1108  * @param key The key of the accepting state.
1109  * @param ctx Context containing info about the string, tunnel, etc.
1110  */
1111 static void
1112 regex_find_path (const struct GNUNET_HashCode *key,
1113                  struct MeshRegexSearchContext *ctx);
1114
1115
1116 /**
1117  * Queue and pass message to core when possible.
1118  *
1119  * @param cls Closure (type dependant).
1120  * @param type Type of the message, 0 for a raw message.
1121  * @param size Size of the message.
1122  * @param dst Neighbor to send message to.
1123  * @param t Tunnel this message belongs to.
1124  */
1125 static void
1126 queue_add (void *cls, uint16_t type, size_t size,
1127            struct MeshPeerInfo *dst, struct MeshTunnel *t);
1128
1129 /**
1130  * Free a transmission that was already queued with all resources
1131  * associated to the request.
1132  *
1133  * @param queue Queue handler to cancel.
1134  * @param clear_cls Is it necessary to free associated cls?
1135  */
1136 static void
1137 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1138
1139 /******************************************************************************/
1140 /************************         ITERATORS        ****************************/
1141 /******************************************************************************/
1142
1143 /**
1144  * Iterator over found existing mesh regex blocks that match an ongoing search.
1145  *
1146  * @param cls closure
1147  * @param key current key code
1148  * @param value value in the hash map
1149  * @return GNUNET_YES if we should continue to iterate,
1150  *         GNUNET_NO if not.
1151  */
1152 static int
1153 regex_result_iterator (void *cls,
1154                        const struct GNUNET_HashCode * key,
1155                        void *value)
1156 {
1157   struct MeshRegexBlock *block = value;
1158   struct MeshRegexSearchContext *ctx = cls;
1159
1160   if (GNUNET_YES == ntohl(block->accepting) &&
1161       ctx->position == strlen (ctx->info->description))
1162   {
1163     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1164     regex_find_path (key, ctx);
1165     return GNUNET_YES; // We found an accept state!
1166   }
1167   else
1168   {
1169     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1170                 ctx->position, strlen(ctx->info->description),
1171                 ntohl(block->accepting));
1172
1173   }
1174   (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1175                                           &regex_edge_iterator, ctx);
1176
1177   return GNUNET_YES;
1178 }
1179
1180
1181 /**
1182  * Iterator over edges in a regex block retrieved from the DHT.
1183  *
1184  * @param cls Closure (context of the search).
1185  * @param token Token that follows to next state.
1186  * @param len Lenght of token.
1187  * @param key Hash of next state.
1188  *
1189  * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1190  */
1191 static int
1192 regex_edge_iterator (void *cls,
1193                      const char *token,
1194                      size_t len,
1195                      const struct GNUNET_HashCode *key)
1196 {
1197   struct MeshRegexSearchContext *ctx = cls;
1198   struct MeshRegexSearchContext *new_ctx;
1199   struct MeshRegexSearchInfo *info = ctx->info;
1200   struct GNUNET_DHT_GetHandle *get_h;
1201   char *current;
1202   size_t current_len;
1203
1204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*    Start of regex edge iterator\n");
1205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     descr : %s\n", info->description);
1206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     posit : %u\n", ctx->position);
1207   current = &info->description[ctx->position];
1208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     currt : %s\n", current);
1209   current_len = strlen (info->description) - ctx->position;
1210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     ctlen : %u\n", current_len);
1211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     tklen : %u\n", len);
1212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     tk[0] : %c\n", token[0]);
1213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     nextk : %s\n", GNUNET_h2s(key));
1214   if (len > current_len)
1215   {
1216     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     Token too long, END\n");
1217     return GNUNET_YES; // Token too long, wont match
1218   }
1219   if (0 != strncmp (current, token, len))
1220   {
1221     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     Token doesn't match, END\n");
1222     return GNUNET_YES; // Token doesn't match
1223   }
1224   new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1225   new_ctx->info = info;
1226   new_ctx->position = ctx->position + len;
1227   GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1228   if (GNUNET_YES ==
1229       GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1230   {
1231     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     GET running, END\n");
1232     GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1233                                                 &regex_result_iterator,
1234                                                 new_ctx);
1235     return GNUNET_YES; // We are already looking for it
1236   }
1237   /* Start search in DHT */
1238   get_h = 
1239       GNUNET_DHT_get_start (dht_handle,    /* handle */
1240                             GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1241                             key,     /* key to search */
1242                             dht_replication_level, /* replication level */
1243                             GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1244                             NULL,       /* xquery */ // FIXME BLOOMFILTER
1245                             0,     /* xquery bits */ // FIXME BLOOMFILTER SIZE
1246                             &dht_get_string_handler, new_ctx);
1247   if (GNUNET_OK !=
1248       GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1249                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1250   {
1251     GNUNET_break (0);
1252     return GNUNET_YES;
1253   }
1254   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*    End of regex edge iterator\n");
1255   return GNUNET_YES;
1256 }
1257
1258 /**
1259  * Iterator over hash map entries to cancel DHT GET requests after a
1260  * successful connect_by_string.
1261  *
1262  * @param cls Closure (unused).
1263  * @param key Current key code (unused).
1264  * @param value Value in the hash map (get handle).
1265  * @return GNUNET_YES if we should continue to iterate,
1266  *         GNUNET_NO if not.
1267  */
1268 static int
1269 regex_cancel_dht_get (void *cls,
1270                       const struct GNUNET_HashCode * key,
1271                       void *value)
1272 {
1273   struct GNUNET_DHT_GetHandle *h = value;
1274
1275   GNUNET_DHT_get_stop (h);
1276   return GNUNET_YES;
1277 }
1278
1279
1280 /**
1281  * Iterator over hash map entries to free MeshRegexBlocks stored during the
1282  * search for connect_by_string.
1283  *
1284  * @param cls Closure (unused).
1285  * @param key Current key code (unused).
1286  * @param value MeshRegexBlock in the hash map.
1287  * @return GNUNET_YES if we should continue to iterate,
1288  *         GNUNET_NO if not.
1289  */
1290 static int
1291 regex_free_result (void *cls,
1292                    const struct GNUNET_HashCode * key,
1293                    void *value)
1294 {
1295
1296   GNUNET_free (value);
1297   return GNUNET_YES;
1298 }
1299
1300
1301 /**
1302  * Regex callback iterator to store own service description in the DHT.
1303  *
1304  * @param cls closure.
1305  * @param key hash for current state.
1306  * @param proof proof for current state.
1307  * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1308  * @param num_edges number of edges leaving current state.
1309  * @param edges edges leaving current state.
1310  */
1311 void
1312 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1313                 int accepting, unsigned int num_edges,
1314                 const struct GNUNET_REGEX_Edge *edges)
1315 {
1316     struct MeshRegexBlock *block;
1317     struct MeshRegexEdge *block_edge;
1318     enum GNUNET_DHT_RouteOption opt;
1319     size_t size;
1320     size_t len;
1321     unsigned int i;
1322     unsigned int offset;
1323     char *aux;
1324
1325     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326                 "  regex dht put for state %s\n",
1327                 GNUNET_h2s(key));
1328     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329                 "   proof: %s\n",
1330                 proof);
1331     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332                 "   num edges: %u\n",
1333                 num_edges);
1334
1335     opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1336     if (GNUNET_YES == accepting)
1337     {
1338         struct MeshRegexAccept block;
1339
1340         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341                     "   state %s is accepting, putting own id\n",
1342                     GNUNET_h2s(key));
1343         size = sizeof (block);
1344         block.key = *key;
1345         block.id = my_full_id;
1346         (void)
1347         GNUNET_DHT_put(dht_handle, key,
1348                        dht_replication_level,
1349                        opt | GNUNET_DHT_RO_RECORD_ROUTE,
1350                        GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1351                        size,
1352                        (char *) &block,
1353                        GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1354                                                  app_announce_time),
1355                        app_announce_time,
1356                        NULL, NULL);
1357     }
1358     len = strlen(proof);
1359     size = sizeof (struct MeshRegexBlock) + len;
1360     block = GNUNET_malloc (size);
1361
1362     block->key = *key;
1363     block->n_proof = htonl (len);
1364     block->n_edges = htonl (num_edges);
1365     block->accepting = htonl (accepting);
1366
1367     /* Store the proof at the end of the block. */
1368     aux = (char *) &block[1];
1369     memcpy (aux, proof, len);
1370     aux = &aux[len];
1371
1372     /* Store each edge in a variable length MeshEdge struct at the
1373      * very end of the MeshRegexBlock structure.
1374      */
1375     for (i = 0; i < num_edges; i++)
1376     {
1377         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378                     "    edge %s towards %s\n",
1379                     edges[i].label,
1380                     GNUNET_h2s(&edges[i].destination));
1381
1382         /* aux points at the end of the last block */
1383         len = strlen (edges[i].label);
1384         size += sizeof (struct MeshRegexEdge) + len;
1385         // Calculate offset FIXME is this ok? use size instead?
1386         offset = aux - (char *) block;
1387         block = GNUNET_realloc (block, size);
1388         aux = &((char *) block)[offset];
1389         block_edge = (struct MeshRegexEdge *) aux;
1390         block_edge->key = edges[i].destination;
1391         block_edge->n_token = htonl (len);
1392         aux = (char *) &block_edge[1];
1393         memcpy (aux, edges[i].label, len);
1394         aux = &aux[len];
1395     }
1396     (void)
1397     GNUNET_DHT_put(dht_handle, key,
1398                    dht_replication_level,
1399                    opt,
1400                    GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1401                    (char *) block,
1402                    GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1403                                             app_announce_time),
1404                    app_announce_time,
1405                    NULL, NULL);
1406     GNUNET_free (block);
1407 }
1408
1409
1410 /**
1411  * Store the regular expression describing a local service into the DHT.
1412  *
1413  * @param regex The regular expresion.
1414  */
1415 static void
1416 regex_put (const char *regex)
1417 {
1418   struct GNUNET_REGEX_Automaton *dfa;
1419
1420   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1421   dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1422   GNUNET_REGEX_iterate_all_edges (dfa, &regex_iterator, NULL);
1423   GNUNET_REGEX_automaton_destroy (dfa);
1424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1425
1426 }
1427
1428 /**
1429  * Find a path to a peer that offers a regex servcie compatible
1430  * with a given string.
1431  * 
1432  * @param key The key of the accepting state.
1433  * @param ctx Context containing info about the string, tunnel, etc.
1434  */
1435 static void
1436 regex_find_path (const struct GNUNET_HashCode *key,
1437                  struct MeshRegexSearchContext *ctx)
1438 {
1439   struct GNUNET_DHT_GetHandle *get_h;
1440
1441   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1442   get_h = GNUNET_DHT_get_start (dht_handle,    /* handle */
1443                                 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1444                                 key,     /* key to search */
1445                                 dht_replication_level, /* replication level */
1446                                 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1447                                 GNUNET_DHT_RO_RECORD_ROUTE,
1448                                 NULL,       /* xquery */ // FIXME BLOOMFILTER
1449                                 0,     /* xquery bits */ // FIXME BLOOMFILTER SIZE
1450                                 &dht_get_string_accept_handler, ctx);
1451   GNUNET_break (GNUNET_OK ==
1452                 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1453                                                   key,
1454                                                   get_h,
1455                                                   GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1456 }
1457
1458
1459 /**
1460  * Function called if the connect attempt to a peer found via
1461  * connect_by_string times out. Try to connect to another peer, if any.
1462  * Otherwise try to reconnect to the same peer.
1463  *
1464  * @param cls Closure (info about regex search).
1465  * @param tc TaskContext.
1466  */
1467 static void
1468 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1469 {
1470   struct MeshRegexSearchInfo *info = cls;
1471   struct MeshPeerInfo *peer_info;
1472   GNUNET_PEER_Id id;
1473   GNUNET_PEER_Id old;
1474
1475   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1476   info->timeout = GNUNET_SCHEDULER_NO_TASK;
1477   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1478   {
1479     return;
1480   }
1481
1482   old = info->peer;
1483   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  timed out: %u\n", old);
1484
1485   if (0 < info->n_peers)
1486   {
1487     // Select next peer, put current in that spot.
1488     id = info->peers[info->i_peer];
1489     info->peers[info->i_peer] = info->peer;
1490     info->i_peer = (info->i_peer + 1) % info->n_peers;
1491   }
1492   else
1493   {
1494     // Try to connect to same peer again.
1495     id = info->peer;
1496   }
1497   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  trying: %u\n", id);
1498
1499   peer_info = peer_info_get_short(id);
1500   tunnel_add_peer (info->t, peer_info);
1501   if (old != id)
1502     tunnel_delete_peer (info->t, old);
1503   peer_info_connect (peer_info, info->t);
1504   info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1505                                                 &regex_connect_timeout,
1506                                                 info);
1507   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1508 }
1509
1510
1511 /**
1512  * Cancel an ongoing regex search in the DHT and free all resources.
1513  *
1514  * @param ctx The search context.
1515  */
1516 static void
1517 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1518 {
1519   struct MeshRegexSearchInfo *info = ctx->info;
1520   int i;
1521
1522   GNUNET_free (info->description);
1523   GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1524                                              &regex_cancel_dht_get, NULL);
1525   GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1526                                          &regex_free_result, NULL);
1527   GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1528   GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1529   info->t->regex_ctx = NULL;
1530   for (i = 0; i < info->n_contexts; i++)
1531   {
1532     GNUNET_free (info->contexts[i]);
1533   }
1534   if (0 < info->n_contexts)
1535     GNUNET_free (info->contexts);
1536   if (0 < info->n_peers)
1537     GNUNET_free (info->peers);
1538   if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1539   {
1540     GNUNET_SCHEDULER_cancel(info->timeout);
1541   }
1542   GNUNET_free (info);
1543 }
1544
1545
1546 /******************************************************************************/
1547 /************************    PERIODIC FUNCTIONS    ****************************/
1548 /******************************************************************************/
1549
1550 /**
1551  * Announce iterator over for each application provided by the peer
1552  *
1553  * @param cls closure
1554  * @param key current key code
1555  * @param value value in the hash map
1556  * @return GNUNET_YES if we should continue to
1557  *         iterate,
1558  *         GNUNET_NO if not.
1559  */
1560 static int
1561 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1562 {
1563   struct PBlock block;
1564   struct MeshClient *c;
1565
1566   block.id = my_full_id;
1567   c =  GNUNET_CONTAINER_multihashmap_get (applications, key);
1568   block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1569   if (0 == block.type)
1570   {
1571     GNUNET_break(0);
1572     return GNUNET_YES;
1573   }
1574   block.type = htonl (block.type);
1575
1576   GNUNET_break (NULL != 
1577                 GNUNET_DHT_put (dht_handle, key,
1578                   dht_replication_level,
1579                   GNUNET_DHT_RO_RECORD_ROUTE |
1580                   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1581                   GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1582                   sizeof (block),
1583                   (const char *) &block,
1584                   GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1585                                             app_announce_time),
1586                   app_announce_time, NULL, NULL));
1587   return GNUNET_OK;
1588 }
1589
1590
1591 /**
1592  * Periodically announce what applications are provided by local clients
1593  * (by regex)
1594  *
1595  * @param cls closure
1596  * @param tc task context
1597  */
1598 static void
1599 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1600 {
1601   struct MeshClient *c = cls;
1602   unsigned int i;
1603
1604   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1605   {
1606     c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1607     return;
1608   }
1609
1610   DEBUG_DHT ("Starting PUT for regex\n");
1611
1612   for (i = 0; i < c->n_regex; i++)
1613   {
1614     regex_put (c->regexes[i]);
1615   }
1616   c->regex_announce_task =
1617       GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1618   DEBUG_DHT ("Finished PUT for regex\n");
1619
1620   return;
1621 }
1622
1623
1624 /**
1625  * Periodically announce what applications are provided by local clients
1626  * (by type)
1627  *
1628  * @param cls closure
1629  * @param tc task context
1630  */
1631 static void
1632 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1633 {
1634   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1635   {
1636     announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1637     return;
1638   }
1639  
1640   DEBUG_DHT ("Starting PUT for apps\n");
1641
1642   GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1643                                          NULL);
1644   announce_applications_task =
1645       GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1646                                     cls);
1647   DEBUG_DHT ("Finished PUT for apps\n");
1648
1649   return;
1650 }
1651
1652
1653 /**
1654  * Periodically announce self id in the DHT
1655  *
1656  * @param cls closure
1657  * @param tc task context
1658  */
1659 static void
1660 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1661 {
1662   struct PBlock block;
1663
1664   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1665   {
1666     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1667     return;
1668   }
1669   /* TODO
1670    * - Set data expiration in function of X
1671    * - Adapt X to churn
1672    */
1673   DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1674
1675   block.id = my_full_id;
1676   block.type = htonl (0);
1677   GNUNET_DHT_put (dht_handle,   /* DHT handle */
1678                   &my_full_id.hashPubKey,       /* Key to use */
1679                   dht_replication_level,     /* Replication level */
1680                   GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,    /* DHT options */
1681                   GNUNET_BLOCK_TYPE_MESH_PEER,       /* Block type */
1682                   sizeof (block),  /* Size of the data */
1683                   (const char *) &block, /* Data itself */
1684                   GNUNET_TIME_UNIT_FOREVER_ABS,  /* Data expiration */
1685                   GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1686                   NULL,         /* Continuation */
1687                   NULL);        /* Continuation closure */
1688   announce_id_task =
1689       GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1690 }
1691
1692
1693 /******************************************************************************/
1694 /******************      GENERAL HELPER FUNCTIONS      ************************/
1695 /******************************************************************************/
1696
1697 /**
1698  * Decrements the reference counter and frees all resources if needed
1699  *
1700  * @param mesh_data Data Descriptor used in a multicast message.
1701  *                  Freed no longer needed (last message).
1702  */
1703 static void
1704 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1705 {
1706   /* Make sure it's a multicast packet */
1707   GNUNET_assert (NULL != mesh_data->reference_counter);
1708
1709   if (0 == --(*(mesh_data->reference_counter)))
1710   {
1711     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1712     if (NULL != mesh_data->task)
1713     {
1714       if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1715       {
1716         GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1717         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1718         GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1719       }
1720       GNUNET_free (mesh_data->task);
1721     }
1722     GNUNET_free (mesh_data->reference_counter);
1723     GNUNET_free (mesh_data->data);
1724     GNUNET_free (mesh_data);
1725   }
1726 }
1727
1728
1729 /**
1730  * Check if client has registered with the service and has not disconnected
1731  *
1732  * @param client the client to check
1733  *
1734  * @return non-NULL if client exists in the global DLL
1735  */
1736 static struct MeshClient *
1737 client_get (struct GNUNET_SERVER_Client *client)
1738 {
1739   struct MeshClient *c;
1740
1741   c = clients;
1742   while (NULL != c)
1743   {
1744     if (c->handle == client)
1745       return c;
1746     c = c->next;
1747   }
1748   return NULL;
1749 }
1750
1751
1752 /**
1753  * Checks if a given client has subscribed to certain message type
1754  *
1755  * @param message_type Type of message to check
1756  * @param c Client to check
1757  *
1758  * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1759  */
1760 static int
1761 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1762 {
1763   struct GNUNET_HashCode hc;
1764
1765   if (NULL == c->types)
1766     return GNUNET_NO;
1767   GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1768   return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1769 }
1770
1771
1772 /**
1773  * Allow a client to send more data after transmitting a multicast message
1774  * which some neighbor has not yet accepted altough a reasonable time has
1775  * passed.
1776  *
1777  * @param cls Closure (DataDescriptor containing the task identifier)
1778  * @param tc Task Context
1779  * 
1780  * FIXME reference counter cshould be just int
1781  */
1782 static void
1783 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1784 {
1785   struct MeshData *mdata = cls;
1786
1787   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1788     return;
1789   GNUNET_assert (NULL != mdata->reference_counter);
1790   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791               "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1792               *(mdata->reference_counter));
1793   *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1794   GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1795 }
1796
1797
1798 /**
1799  * Check whether client wants traffic from a tunnel.
1800  *
1801  * @param c Client to check.
1802  * @param t Tunnel to be found.
1803  *
1804  * @return GNUNET_YES if client knows tunnel.
1805  * 
1806  * TODO look in client hashmap
1807  */
1808 static int
1809 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1810 {
1811   unsigned int i;
1812
1813   for (i = 0; i < t->nclients; i++)
1814     if (t->clients[i] == c)
1815       return GNUNET_YES;
1816   return GNUNET_NO;
1817 }
1818
1819
1820 /**
1821  * Check whether client has been informed about a tunnel.
1822  *
1823  * @param c Client to check.
1824  * @param t Tunnel to be found.
1825  *
1826  * @return GNUNET_YES if client knows tunnel.
1827  * 
1828  * TODO look in client hashmap
1829  */
1830 static int
1831 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1832 {
1833   unsigned int i;
1834
1835   for (i = 0; i < t->nignore; i++)
1836     if (t->ignore[i] == c)
1837       return GNUNET_YES;
1838   return client_wants_tunnel(c, t);
1839 }
1840
1841
1842 /**
1843  * Marks a client as uninterested in traffic from the tunnel, updating both
1844  * client and tunnel to reflect this.
1845  *
1846  * @param c Client that doesn't want traffic anymore.
1847  * @param t Tunnel which should be ignored.
1848  *
1849  * FIXME when to delete an incoming tunnel?
1850  */
1851 static void
1852 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1853 {
1854   struct GNUNET_HashCode hash;
1855
1856   GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1857   GNUNET_break (GNUNET_YES ==
1858                 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1859                                                       &hash, t));
1860   GNUNET_break (GNUNET_YES ==
1861                 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1862                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1863   tunnel_delete_active_client (t, c);
1864   GNUNET_array_append (t->ignore, t->nignore, c);
1865 }
1866
1867
1868 /**
1869  * Deletes a tunnel from a client (either owner or destination). To be used on
1870  * tunnel destroy, otherwise, use client_ignore_tunnel.
1871  *
1872  * @param c Client whose tunnel to delete.
1873  * @param t Tunnel which should be deleted.
1874  */
1875 static void
1876 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1877 {
1878   struct GNUNET_HashCode hash;
1879
1880   if (c == t->owner)
1881   {
1882     GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1883     GNUNET_assert (GNUNET_YES ==
1884                    GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1885                                                          &hash,
1886                                                          t));
1887   }
1888   else
1889   {
1890     GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1891     // FIXME XOR?
1892     GNUNET_assert (GNUNET_YES ==
1893                    GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1894                                                          &hash,
1895                                                          t) ||
1896                    GNUNET_YES ==
1897                    GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1898                                                          &hash,
1899                                                          t));
1900   }
1901 }
1902
1903
1904 /**
1905  * Send the message to all clients that have subscribed to its type
1906  *
1907  * @param msg Pointer to the message itself
1908  * @param payload Pointer to the payload of the message.
1909  * @return number of clients this message was sent to
1910  */
1911 static unsigned int
1912 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1913                          const struct GNUNET_MessageHeader *payload)
1914 {
1915   struct GNUNET_PeerIdentity *oid;
1916   struct MeshClient *c;
1917   struct MeshTunnel *t;
1918   MESH_TunnelNumber *tid;
1919   unsigned int count;
1920   uint16_t type;
1921   char cbuf[htons (msg->size)];
1922
1923   type = ntohs (payload->type);
1924   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1925   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1926               GNUNET_MESH_DEBUG_M2S (type));
1927
1928   memcpy (cbuf, msg, sizeof (cbuf));
1929   switch (htons (msg->type))
1930   {
1931     struct GNUNET_MESH_Unicast *uc;
1932     struct GNUNET_MESH_Multicast *mc;
1933     struct GNUNET_MESH_ToOrigin *to;
1934
1935   case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1936     uc = (struct GNUNET_MESH_Unicast *) cbuf;
1937     tid = &uc->tid;
1938     oid = &uc->oid;
1939     break;
1940   case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1941     mc = (struct GNUNET_MESH_Multicast *) cbuf;
1942     tid = &mc->tid;
1943     oid = &mc->oid;
1944     break;
1945   case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1946     to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1947     tid = &to->tid;
1948     oid = &to->oid;
1949     break;
1950   default:
1951     GNUNET_break (0);
1952     return 0;
1953   }
1954   t = tunnel_get (oid, ntohl (*tid));
1955   if (NULL == t)
1956   {
1957     GNUNET_break (0);
1958     return 0;
1959   }
1960
1961   for (count = 0, c = clients; c != NULL; c = c->next)
1962   {
1963     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   client %u\n", c->id);
1964     if (client_is_subscribed (type, c))
1965     {
1966       if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1967       {
1968         if (c != t->owner)
1969           continue;
1970         *tid = htonl (t->local_tid);
1971       }
1972       else
1973       {
1974         if (GNUNET_NO == client_knows_tunnel (c, t))
1975         {
1976           /* This client doesn't know the tunnel */
1977           struct GNUNET_MESH_TunnelNotification tmsg;
1978           struct GNUNET_HashCode hash;
1979
1980           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "     sending tunnel create\n");
1981           tmsg.header.size = htons (sizeof (tmsg));
1982           tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1983           GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1984           tmsg.tunnel_id = htonl (t->local_tid_dest);
1985           GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1986                                                       &tmsg.header, GNUNET_NO);
1987           tunnel_add_client (t, c);
1988           GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1989                               &hash);
1990           GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1991                                        c->incoming_tunnels, &hash, t,
1992                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1993         }
1994         *tid = htonl (t->local_tid_dest);
1995       }
1996
1997       /* Check if the client wants to get traffic from the tunnel */
1998       if (GNUNET_NO == client_wants_tunnel(c, t))
1999         continue;
2000       count++;
2001       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "     sending\n");
2002       GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2003                                                   (struct GNUNET_MessageHeader
2004                                                    *) cbuf, GNUNET_NO);
2005     }
2006   }
2007   return count;
2008 }
2009
2010
2011 /**
2012  * Notify the client that owns the tunnel that a peer has connected to it
2013  * (the requested path to it has been confirmed).
2014  *
2015  * @param t Tunnel whose owner to notify
2016  * @param id Short id of the peer that has connected
2017  */
2018 static void
2019 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2020 {
2021   struct GNUNET_MESH_PeerControl pc;
2022
2023   pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2024   pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2025   pc.tunnel_id = htonl (t->local_tid);
2026   GNUNET_PEER_resolve (id, &pc.peer);
2027   GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2028                                               GNUNET_NO);
2029 }
2030
2031
2032 /**
2033  * Notify all clients (not depending on registration status) that the incoming
2034  * tunnel is no longer valid.
2035  *
2036  * @param t Tunnel that was destroyed.
2037  */
2038 static void
2039 send_clients_tunnel_destroy (struct MeshTunnel *t)
2040 {
2041   struct GNUNET_MESH_TunnelMessage msg;
2042
2043   msg.header.size = htons (sizeof (msg));
2044   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2045   msg.tunnel_id = htonl (t->local_tid_dest);
2046   GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2047 }
2048
2049
2050 /**
2051  * Notify clients of tunnel disconnections, if needed.
2052  * In case the origin disconnects, the destination clients get a tunnel destroy
2053  * notification. If the last destination disconnects (only one remaining client
2054  * in tunnel), the origin gets a (local ID) peer disconnected.
2055  * Note that the function must be called BEFORE removing the client from
2056  * the tunnel.
2057  *
2058  * @param t Tunnel that was destroyed.
2059  * @param c Client that disconnected.
2060  */
2061 static void
2062 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2063 {
2064   unsigned int i;
2065
2066   if (c == t->owner)
2067   {
2068     struct GNUNET_MESH_TunnelMessage msg;
2069
2070     msg.header.size = htons (sizeof (msg));
2071     msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2072     msg.tunnel_id = htonl (t->local_tid_dest);
2073     for (i = 0; i < t->nclients; i++)
2074       GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2075                                                   &msg.header, GNUNET_NO);
2076   }
2077   // FIXME when to disconnect an incoming tunnel?
2078   else if (1 == t->nclients && NULL != t->owner)
2079   {
2080     struct GNUNET_MESH_PeerControl msg;
2081
2082     msg.header.size = htons (sizeof (msg));
2083     msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2084     msg.tunnel_id = htonl (t->local_tid);
2085     msg.peer = my_full_id;
2086     GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2087                                                 &msg.header, GNUNET_NO);
2088   }
2089 }
2090
2091
2092 /**
2093  * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2094  * and insert it in the appropiate structures if the peer is not known yet.
2095  *
2096  * @param peer Full identity of the peer.
2097  *
2098  * @return Existing or newly created peer info.
2099  */
2100 static struct MeshPeerInfo *
2101 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2102 {
2103   struct MeshPeerInfo *peer_info;
2104
2105   peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2106   if (NULL == peer_info)
2107   {
2108     peer_info =
2109         (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2110     GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2111                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2112     peer_info->id = GNUNET_PEER_intern (peer);
2113   }
2114
2115   return peer_info;
2116 }
2117
2118
2119 /**
2120  * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2121  * and insert it in the appropiate structures if the peer is not known yet.
2122  *
2123  * @param peer Short identity of the peer.
2124  *
2125  * @return Existing or newly created peer info.
2126  */
2127 static struct MeshPeerInfo *
2128 peer_info_get_short (const GNUNET_PEER_Id peer)
2129 {
2130   struct GNUNET_PeerIdentity id;
2131
2132   GNUNET_PEER_resolve (peer, &id);
2133   return peer_info_get (&id);
2134 }
2135
2136
2137 /**
2138  * Iterator to remove the tunnel from the list of tunnels a peer participates
2139  * in.
2140  *
2141  * @param cls Closure (tunnel info)
2142  * @param key GNUNET_PeerIdentity of the peer (unused)
2143  * @param value PeerInfo of the peer
2144  *
2145  * @return always GNUNET_YES, to keep iterating
2146  */
2147 static int
2148 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2149 {
2150   struct MeshTunnel *t = cls;
2151   struct MeshPeerInfo *peer = value;
2152   unsigned int i;
2153
2154   for (i = 0; i < peer->ntunnels; i++)
2155   {
2156     if (0 ==
2157         memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2158     {
2159       peer->ntunnels--;
2160       peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2161       peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2162       return GNUNET_YES;
2163     }
2164   }
2165   return GNUNET_YES;
2166 }
2167
2168
2169 /**
2170   * Core callback to write a pre-constructed data packet to core buffer
2171   *
2172   * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2173   * @param size Number of bytes available in buf.
2174   * @param buf Where the to write the message.
2175   *
2176   * @return number of bytes written to buf
2177   */
2178 static size_t
2179 send_core_data_raw (void *cls, size_t size, void *buf)
2180 {
2181   struct MeshTransmissionDescriptor *info = cls;
2182   struct GNUNET_MessageHeader *msg;
2183   size_t total_size;
2184
2185   GNUNET_assert (NULL != info);
2186   GNUNET_assert (NULL != info->mesh_data);
2187   msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2188   total_size = ntohs (msg->size);
2189
2190   if (total_size > size)
2191   {
2192     GNUNET_break (0);
2193     return 0;
2194   }
2195   memcpy (buf, msg, total_size);
2196   data_descriptor_decrement_rc (info->mesh_data);
2197   GNUNET_free (info);
2198   return total_size;
2199 }
2200
2201
2202 /**
2203  * Sends an already built non-multicast message to a peer,
2204  * properly registrating all used resources.
2205  *
2206  * @param message Message to send. Function makes a copy of it.
2207  * @param peer Short ID of the neighbor whom to send the message.
2208  * @param t Tunnel on which this message is transmitted.
2209  */
2210 static void
2211 send_message (const struct GNUNET_MessageHeader *message,
2212               const struct GNUNET_PeerIdentity *peer,
2213               struct MeshTunnel *t)
2214 {
2215   struct MeshTransmissionDescriptor *info;
2216   struct MeshPeerInfo *neighbor;
2217   struct MeshPeerPath *p;
2218   size_t size;
2219   uint16_t type;
2220
2221 //   GNUNET_TRANSPORT_try_connect(); FIXME use?
2222
2223   size = ntohs (message->size);
2224   info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2225   info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2226   info->mesh_data->data = GNUNET_malloc (size);
2227   memcpy (info->mesh_data->data, message, size);
2228   type = ntohs(message->type);
2229   if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2230   {
2231     struct GNUNET_MESH_Unicast *m;
2232
2233     m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2234     m->ttl = htonl (ntohl (m->ttl) - 1);
2235   }
2236   info->mesh_data->data_len = size;
2237   info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2238   *info->mesh_data->reference_counter = 1;
2239   neighbor = peer_info_get (peer);
2240   for (p = neighbor->path_head; NULL != p; p = p->next)
2241   {
2242     if (2 >= p->length)
2243     {
2244       break;
2245     }
2246   }
2247   if (NULL == p)
2248   {
2249 #if MESH_DEBUG
2250     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2251                 "  %s IS NOT DIRECTLY CONNECTED\n",
2252                 GNUNET_i2s(peer));
2253     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2254                 "  PATHS TO %s:\n",
2255                 GNUNET_i2s(peer));
2256     for (p = neighbor->path_head; NULL != p; p = p->next)
2257     {
2258       struct GNUNET_PeerIdentity debug_id;
2259       unsigned int i;
2260
2261       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262                   "    path with %u hops through:\n",
2263                   p->length);
2264       for (i = 0; i < p->length; i++)
2265       {
2266         GNUNET_PEER_resolve(p->peers[i], &debug_id);
2267         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268                     "      hop %u: %s\n",
2269                     i, GNUNET_i2s(&debug_id));
2270       }
2271     }
2272 #endif
2273     GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2274     GNUNET_free (info->mesh_data->data);
2275     GNUNET_free (info->mesh_data);
2276     GNUNET_free (info);
2277     return;
2278   }
2279   info->peer = neighbor;
2280   if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2281   {
2282     /*
2283      * TODO: in this case we only need the service to retransmit
2284      * the message down the path. If we pass the real type to queue_add,
2285      * queue_send will try to build the message from scratch. This can
2286      * probably be done by some other way instead of deleteing the type
2287      * info.
2288      */
2289     type = 0;
2290   }
2291   queue_add (info,
2292              type,
2293              size,
2294              neighbor,
2295              t);
2296 }
2297
2298
2299 /**
2300  * Sends a CREATE PATH message for a path to a peer, properly registrating
2301  * all used resources.
2302  *
2303  * @param peer PeerInfo of the final peer for whom this path is being created.
2304  * @param p Path itself.
2305  * @param t Tunnel for which the path is created.
2306  */
2307 static void
2308 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2309                   struct MeshTunnel *t)
2310 {
2311   struct GNUNET_PeerIdentity id;
2312   struct MeshPathInfo *path_info;
2313   struct MeshPeerInfo *neighbor;
2314
2315   unsigned int i;
2316
2317   if (NULL == p)
2318   {
2319     p = tree_get_path_to_peer (t->tree, peer->id);
2320     if (NULL == p)
2321     {
2322       GNUNET_break (0);
2323       return;
2324     }
2325   }
2326   for (i = 0; i < p->length; i++)
2327   {
2328     if (p->peers[i] == myid)
2329       break;
2330   }
2331   if (i >= p->length - 1)
2332   {
2333     path_destroy (p);
2334     GNUNET_break (0);
2335     return;
2336   }
2337   GNUNET_PEER_resolve (p->peers[i + 1], &id);
2338
2339   path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2340   path_info->path = p;
2341   path_info->t = t;
2342   neighbor = peer_info_get (&id);
2343   path_info->peer = neighbor;
2344   queue_add (path_info,
2345              GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2346              sizeof (struct GNUNET_MESH_ManipulatePath) +
2347                 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2348              neighbor,
2349              t);
2350 }
2351
2352
2353 /**
2354  * Sends a DESTROY PATH message to free resources for a path in a tunnel
2355  *
2356  * @param t Tunnel whose path to destroy.
2357  * @param destination Short ID of the peer to whom the path to destroy.
2358  */
2359 static void
2360 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2361 {
2362   struct MeshPeerPath *p;
2363   size_t size;
2364
2365   p = tree_get_path_to_peer (t->tree, destination);
2366   if (NULL == p)
2367   {
2368     GNUNET_break (0);
2369     return;
2370   }
2371   size = sizeof (struct GNUNET_MESH_ManipulatePath);
2372   size += p->length * sizeof (struct GNUNET_PeerIdentity);
2373   {
2374     struct GNUNET_MESH_ManipulatePath *msg;
2375     struct GNUNET_PeerIdentity *pi;
2376     char cbuf[size];
2377     unsigned int i;
2378
2379     msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2380     msg->header.size = htons (size);
2381     msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2382     msg->tid = htonl (t->id.tid);
2383     pi = (struct GNUNET_PeerIdentity *) &msg[1];
2384     for (i = 0; i < p->length; i++)
2385     {
2386       GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2387     }
2388     send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2389   }
2390   path_destroy (p);
2391 }
2392
2393
2394 /**
2395  * Try to establish a new connection to this peer.
2396  * Use the best path for the given tunnel.
2397  * If the peer doesn't have any path to it yet, try to get one.
2398  * If the peer already has some path, send a CREATE PATH towards it.
2399  *
2400  * @param peer PeerInfo of the peer.
2401  * @param t Tunnel for which to create the path, if possible.
2402  */
2403 static void
2404 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2405 {
2406   struct MeshPeerPath *p;
2407   struct MeshPathInfo *path_info;
2408
2409   if (NULL != peer->path_head)
2410   {
2411     p = tree_get_path_to_peer (t->tree, peer->id);
2412     if (NULL == p)
2413     {
2414       GNUNET_break (0);
2415       return;
2416     }
2417
2418     // FIXME always send create path to self
2419     if (p->length > 1)
2420     {
2421       send_create_path (peer, p, t);
2422     }
2423     else
2424     {
2425       struct GNUNET_HashCode hash;
2426
2427       path_destroy (p);
2428       send_client_peer_connected (t, myid);
2429       t->local_tid_dest = next_local_tid++;
2430       GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2431                           &hash);
2432       if (GNUNET_OK !=
2433           GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2434                                              GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2435       {
2436         GNUNET_break (0);
2437         return;
2438       }
2439     }
2440   }
2441   else if (NULL == peer->dhtget)
2442   {
2443     struct GNUNET_PeerIdentity id;
2444
2445     GNUNET_PEER_resolve (peer->id, &id);
2446     path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2447     path_info->peer = peer;
2448     path_info->t = t;
2449     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2450                 "  Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2451     peer->dhtgetcls = path_info;
2452     peer->dhtget = GNUNET_DHT_get_start (dht_handle,    /* handle */
2453                                          GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2454                                          &id.hashPubKey,     /* key to search */
2455                                          dht_replication_level, /* replication level */
2456                                          GNUNET_DHT_RO_RECORD_ROUTE |
2457                                          GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2458                                          NULL,       /* xquery */ // FIXME BLOOMFILTER
2459                                          0,     /* xquery bits */ // FIXME BLOOMFILTER SIZE
2460                                          &dht_get_id_handler, path_info);
2461   }
2462   /* Otherwise, there is no path but the DHT get is already started. */
2463 }
2464
2465
2466 /**
2467  * Task to delay the connection of a peer
2468  *
2469  * @param cls Closure (path info with tunnel and peer to connect).
2470  *            Will be free'd on exection.
2471  * @param tc TaskContext
2472  */
2473 static void
2474 peer_info_connect_task (void *cls,
2475                         const struct GNUNET_SCHEDULER_TaskContext *tc)
2476 {
2477   struct MeshPathInfo *path_info = cls;
2478
2479   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2480   {
2481     GNUNET_free (cls);
2482     return;
2483   }
2484   peer_info_connect (path_info->peer, path_info->t);
2485   GNUNET_free (cls);
2486 }
2487
2488
2489 /**
2490  * Destroy the peer_info and free any allocated resources linked to it
2491  *
2492  * @param pi The peer_info to destroy.
2493  *
2494  * @return GNUNET_OK on success
2495  */
2496 static int
2497 peer_info_destroy (struct MeshPeerInfo *pi)
2498 {
2499   struct GNUNET_PeerIdentity id;
2500   struct MeshPeerPath *p;
2501   struct MeshPeerPath *nextp;
2502
2503   GNUNET_PEER_resolve (pi->id, &id);
2504   GNUNET_PEER_change_rc (pi->id, -1);
2505
2506   if (GNUNET_YES !=
2507       GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2508   {
2509     GNUNET_break (0);
2510     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2511                 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2512   }
2513   if (NULL != pi->dhtget)
2514   {
2515     GNUNET_DHT_get_stop (pi->dhtget);
2516     GNUNET_free (pi->dhtgetcls);
2517   }
2518   p = pi->path_head;
2519   while (NULL != p)
2520   {
2521     nextp = p->next;
2522     GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2523     path_destroy (p);
2524     p = nextp;
2525   }
2526   GNUNET_free (pi);
2527   return GNUNET_OK;
2528 }
2529
2530
2531 /**
2532  * Remove all paths that rely on a direct connection between p1 and p2
2533  * from the peer itself and notify all tunnels about it.
2534  *
2535  * @param peer PeerInfo of affected peer.
2536  * @param p1 GNUNET_PEER_Id of one peer.
2537  * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2538  *           no longer is.
2539  *
2540  * TODO: optimize (see below)
2541  */
2542 static void
2543 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2544                        GNUNET_PEER_Id p2)
2545 {
2546   struct MeshPeerPath *p;
2547   struct MeshPeerPath *aux;
2548   struct MeshPeerInfo *peer_d;
2549   GNUNET_PEER_Id d;
2550   unsigned int destroyed;
2551   unsigned int best;
2552   unsigned int cost;
2553   unsigned int i;
2554
2555   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2556   destroyed = 0;
2557   p = peer->path_head;
2558   while (NULL != p)
2559   {
2560     aux = p->next;
2561     for (i = 0; i < (p->length - 1); i++)
2562     {
2563       if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2564           (p->peers[i] == p2 && p->peers[i + 1] == p1))
2565       {
2566         GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2567         path_destroy (p);
2568         destroyed++;
2569         break;
2570       }
2571     }
2572     p = aux;
2573   }
2574   if (0 == destroyed)
2575     return;
2576
2577   for (i = 0; i < peer->ntunnels; i++)
2578   {
2579     d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2580     if (0 == d)
2581       continue;
2582     /* TODO
2583      * Problem: one or more peers have been deleted from the tunnel tree.
2584      * We don't know who they are to try to add them again.
2585      * We need to try to find a new path for each of the disconnected peers.
2586      * Some of them might already have a path to reach them that does not
2587      * involve p1 and p2. Adding all anew might render in a better tree than
2588      * the trivial immediate fix.
2589      *
2590      * Trivial immiediate fix: try to reconnect to the disconnected node. All
2591      * its children will be reachable trough him.
2592      */
2593     peer_d = peer_info_get_short (d);
2594     best = UINT_MAX;
2595     aux = NULL;
2596     for (p = peer_d->path_head; NULL != p; p = p->next)
2597     {
2598       if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2599       {
2600         best = cost;
2601         aux = p;
2602       }
2603     }
2604     if (NULL != aux)
2605     {
2606       /* No callback, as peer will be already disconnected and a connection
2607        * scheduled by tunnel_notify_connection_broken.
2608        */
2609       tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2610     }
2611     else
2612     {
2613       peer_info_connect (peer_d, peer->tunnels[i]);
2614     }
2615   }
2616   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2617 }
2618
2619
2620 /**
2621  * Add the path to the peer and update the path used to reach it in case this
2622  * is the shortest.
2623  *
2624  * @param peer_info Destination peer to add the path to.
2625  * @param path New path to add. Last peer must be the peer in arg 1.
2626  *             Path will be either used of freed if already known.
2627  * @param trusted Do we trust that this path is real?
2628  */
2629 void
2630 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2631                     int trusted)
2632 {
2633   struct MeshPeerPath *aux;
2634   unsigned int l;
2635   unsigned int l2;
2636
2637   if ((NULL == peer_info) || (NULL == path))
2638   {
2639     GNUNET_break (0);
2640     path_destroy (path);
2641     return;
2642   }
2643   if (path->peers[path->length - 1] != peer_info->id)
2644   {
2645     GNUNET_break (0);
2646     path_destroy (path);
2647     return;
2648   }
2649   if (path->length <= 2 && GNUNET_NO == trusted)
2650   {
2651     /* Only allow CORE to tell us about direct paths */
2652     path_destroy (path);
2653     return;
2654   }
2655   GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2656   for (l = 1; l < path->length; l++)
2657   {
2658     if (path->peers[l] == myid)
2659     {
2660       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2661       for (l2 = 0; l2 < path->length - l; l2++)
2662       {
2663         path->peers[l2] = path->peers[l + l2];
2664       }
2665       path->length -= l;
2666       l = 1;
2667       path->peers =
2668           GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2669     }
2670   }
2671 #if MESH_DEBUG
2672   {
2673     struct GNUNET_PeerIdentity id;
2674
2675     GNUNET_PEER_resolve (peer_info->id, &id);
2676     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2677                 path->length, GNUNET_i2s (&id));
2678   }
2679 #endif
2680   l = path_get_length (path);
2681   if (0 == l)
2682   {
2683     GNUNET_free (path);
2684     return;
2685   }
2686
2687   GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2688   for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2689   {
2690     l2 = path_get_length (aux);
2691     if (l2 > l)
2692     {
2693       GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2694                                           peer_info->path_tail, aux, path);
2695       return;
2696     }
2697     else
2698     {
2699       if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2700       {
2701         path_destroy (path);
2702         return;
2703       }
2704     }
2705   }
2706   GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2707                                     path);
2708   return;
2709 }
2710
2711
2712 /**
2713  * Add the path to the origin peer and update the path used to reach it in case
2714  * this is the shortest.
2715  * The path is given in peer_info -> destination, therefore we turn the path
2716  * upside down first.
2717  *
2718  * @param peer_info Peer to add the path to, being the origin of the path.
2719  * @param path New path to add after being inversed.
2720  * @param trusted Do we trust that this path is real?
2721  */
2722 static void
2723 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2724                               struct MeshPeerPath *path, int trusted)
2725 {
2726   path_invert (path);
2727   peer_info_add_path (peer_info, path, trusted);
2728 }
2729
2730
2731 /**
2732  * Build a PeerPath from the paths returned from the DHT, reversing the paths
2733  * to obtain a local peer -> destination path and interning the peer ids.
2734  *
2735  * @return Newly allocated and created path
2736  */
2737 static struct MeshPeerPath *
2738 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2739                      unsigned int get_path_length,
2740                      const struct GNUNET_PeerIdentity *put_path,
2741                      unsigned int put_path_length)
2742 {
2743   struct MeshPeerPath *p;
2744   GNUNET_PEER_Id id;
2745   int i;
2746
2747   p = path_new (1);
2748   p->peers[0] = myid;
2749   GNUNET_PEER_change_rc (myid, 1);
2750   i = get_path_length;
2751   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   GET has %d hops.\n", i);
2752   for (i--; i >= 0; i--)
2753   {
2754     id = GNUNET_PEER_intern (&get_path[i]);
2755     if (p->length > 0 && id == p->peers[p->length - 1])
2756     {
2757       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
2758       GNUNET_PEER_change_rc (id, -1);
2759     }
2760     else
2761     {
2762       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Adding from GET: %s.\n",
2763                   GNUNET_i2s (&get_path[i]));
2764       p->length++;
2765       p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2766       p->peers[p->length - 1] = id;
2767     }
2768   }
2769   i = put_path_length;
2770   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   PUT has %d hops.\n", i);
2771   for (i--; i >= 0; i--)
2772   {
2773     id = GNUNET_PEER_intern (&put_path[i]);
2774     if (id == myid)
2775     {
2776       /* PUT path went through us, so discard the path up until now and start
2777        * from here to get a much shorter (and loop-free) path.
2778        */
2779       path_destroy (p);
2780       p = path_new (0);
2781     }
2782     if (p->length > 0 && id == p->peers[p->length - 1])
2783     {
2784       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
2785       GNUNET_PEER_change_rc (id, -1);
2786     }
2787     else
2788     {
2789       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Adding from PUT: %s.\n",
2790                   GNUNET_i2s (&put_path[i]));
2791       p->length++;
2792       p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2793       p->peers[p->length - 1] = id;
2794     }
2795   }
2796 #if MESH_DEBUG
2797   if (get_path_length > 0)
2798     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (first of GET: %s)\n",
2799                 GNUNET_i2s (&get_path[0]));
2800   if (put_path_length > 0)
2801     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (first of PUT: %s)\n",
2802                 GNUNET_i2s (&put_path[0]));
2803   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   In total: %d hops\n",
2804               p->length);
2805   for (i = 0; i < p->length; i++)
2806   {
2807     struct GNUNET_PeerIdentity peer_id;
2808
2809     GNUNET_PEER_resolve (p->peers[i], &peer_id);
2810     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "       %u: %s\n", p->peers[i],
2811                 GNUNET_i2s (&peer_id));
2812   }
2813 #endif
2814   return p;
2815 }
2816
2817
2818 /**
2819  * Adds a path to the peer_infos of all the peers in the path
2820  *
2821  * @param p Path to process.
2822  * @param confirmed Whether we know if the path works or not.
2823  */
2824 static void
2825 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2826 {
2827   unsigned int i;
2828
2829   /* TODO: invert and add */
2830   for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2831   for (i++; i < p->length; i++)
2832   {
2833     struct MeshPeerInfo *aux;
2834     struct MeshPeerPath *copy;
2835
2836     aux = peer_info_get_short (p->peers[i]);
2837     copy = path_duplicate (p);
2838     copy->length = i + 1;
2839     peer_info_add_path (aux, copy, GNUNET_NO);
2840   }
2841 }
2842
2843
2844 /**
2845  * Send keepalive packets for a peer
2846  *
2847  * @param cls Closure (tunnel for which to send the keepalive).
2848  * @param tc Notification context.
2849  *
2850  * TODO: implement explicit multicast keepalive?
2851  */
2852 static void
2853 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2854
2855
2856 /**
2857  * Search for a tunnel among the incoming tunnels
2858  *
2859  * @param tid the local id of the tunnel
2860  *
2861  * @return tunnel handler, NULL if doesn't exist
2862  */
2863 static struct MeshTunnel *
2864 tunnel_get_incoming (MESH_TunnelNumber tid)
2865 {
2866   struct GNUNET_HashCode hash;
2867
2868   GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2869   GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2870   return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2871 }
2872
2873
2874 /**
2875  * Search for a tunnel among the tunnels for a client
2876  *
2877  * @param c the client whose tunnels to search in
2878  * @param tid the local id of the tunnel
2879  *
2880  * @return tunnel handler, NULL if doesn't exist
2881  */
2882 static struct MeshTunnel *
2883 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2884 {
2885   if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2886   {
2887     return tunnel_get_incoming (tid);
2888   }
2889   else
2890   {
2891     struct GNUNET_HashCode hash;
2892
2893     GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2894     return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2895   }
2896 }
2897
2898
2899 /**
2900  * Search for a tunnel by global ID using PEER_ID
2901  *
2902  * @param pi owner of the tunnel
2903  * @param tid global tunnel number
2904  *
2905  * @return tunnel handler, NULL if doesn't exist
2906  */
2907 static struct MeshTunnel *
2908 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2909 {
2910   struct MESH_TunnelID id;
2911   struct GNUNET_HashCode hash;
2912
2913   id.oid = pi;
2914   id.tid = tid;
2915
2916   GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2917   return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2918 }
2919
2920
2921 /**
2922  * Search for a tunnel by global ID using full PeerIdentities
2923  *
2924  * @param oid owner of the tunnel
2925  * @param tid global tunnel number
2926  *
2927  * @return tunnel handler, NULL if doesn't exist
2928  */
2929 static struct MeshTunnel *
2930 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2931 {
2932   return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2933 }
2934
2935
2936 /**
2937  * Delete an active client from the tunnel.
2938  * 
2939  * @param t Tunnel.
2940  * @param c Client.
2941  */
2942 static void
2943 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2944 {
2945   unsigned int i;
2946
2947   for (i = 0; i < t->nclients; i++)
2948   {
2949     if (t->clients[i] == c)
2950     {
2951       t->clients[i] = t->clients[t->nclients - 1];
2952       t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2953       GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2954       t->nclients++;
2955       GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2956       break;
2957     }
2958   }
2959 }
2960
2961
2962 /**
2963  * Delete an ignored client from the tunnel.
2964  * 
2965  * @param t Tunnel.
2966  * @param c Client.
2967  */
2968 static void
2969 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2970 {
2971   unsigned int i;
2972
2973   for (i = 0; i < t->nignore; i++)
2974   {
2975     if (t->ignore[i] == c)
2976     {
2977       t->ignore[i] = t->ignore[t->nignore - 1];
2978       GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2979       break;
2980     }
2981   }
2982 }
2983
2984
2985 /**
2986  * Delete a client from the tunnel. It should be only done on
2987  * client disconnection, otherwise use client_ignore_tunnel.
2988  * 
2989  * @param t Tunnel.
2990  * @param c Client.
2991  */
2992 static void
2993 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2994 {
2995   tunnel_delete_ignored_client (t, c);
2996   tunnel_delete_active_client (t, c);
2997 }
2998
2999
3000 /**
3001  * Iterator to free MeshTunnelChildInfo of tunnel children.
3002  *
3003  * @param cls Closure (tunnel info).
3004  * @param key Hash of GNUNET_PEER_Id (unused).
3005  * @param value MeshTunnelChildInfo of the child.
3006  *
3007  * @return always GNUNET_YES, to keep iterating
3008  */
3009 static int
3010 tunnel_destroy_child (void *cls,
3011                       const struct GNUNET_HashCode * key,
3012                       void *value)
3013 {
3014   GNUNET_free (value);
3015   return GNUNET_YES;
3016 }
3017
3018
3019 /**
3020  * Callback used to notify a client owner of a tunnel that a peer has
3021  * disconnected, most likely because of a path change.
3022  *
3023  * @param cls Closure (tunnel this notification is about).
3024  * @param peer_id Short ID of disconnected peer.
3025  */
3026 void
3027 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3028 {
3029   struct MeshTunnel *t = cls;
3030   struct MeshPeerInfo *peer;
3031   struct MeshPathInfo *path_info;
3032
3033   if (NULL != t->owner && NULL != nc)
3034   {
3035     struct GNUNET_MESH_PeerControl msg;
3036
3037     msg.header.size = htons (sizeof (msg));
3038     msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3039     msg.tunnel_id = htonl (t->local_tid);
3040     GNUNET_PEER_resolve (peer_id, &msg.peer);
3041     GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3042                                                 &msg.header, GNUNET_NO);
3043   }
3044   peer = peer_info_get_short (peer_id);
3045   path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3046   path_info->peer = peer;
3047   path_info->t = t;
3048   GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3049 }
3050
3051
3052 /**
3053  * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3054  * needed rescources.
3055  * If peer already exists, reevaluate shortest path and change if different.
3056  *
3057  * @param t Tunnel we want to add a new peer to
3058  * @param peer PeerInfo of the peer being added
3059  *
3060  */
3061 static void
3062 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3063 {
3064   struct GNUNET_PeerIdentity id;
3065   struct MeshPeerPath *best_p;
3066   struct MeshPeerPath *p;
3067   unsigned int best_cost;
3068   unsigned int cost;
3069
3070   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3071   GNUNET_PEER_resolve (peer->id, &id);
3072   if (GNUNET_NO ==
3073       GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3074   {
3075     t->peers_total++;
3076     GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3077     GNUNET_assert (GNUNET_OK ==
3078                    GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3079                                                       peer,
3080                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3081   }
3082
3083   if (NULL != (p = peer->path_head))
3084   {
3085     best_p = p;
3086     best_cost = tree_get_path_cost (t->tree, p);
3087     while (NULL != p)
3088     {
3089       if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3090       {
3091         best_cost = cost;
3092         best_p = p;
3093       }
3094       p = p->next;
3095     }
3096     tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3097     if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3098       t->path_refresh_task =
3099           GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3100   }
3101   else
3102   {
3103     /* Start a DHT get */
3104     peer_info_connect (peer, t);
3105   }
3106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3107 }
3108
3109 /**
3110  * Add a path to a tunnel which we don't own, just to remember the next hop.
3111  * If destination node was already in the tunnel, the first hop information
3112  * will be replaced with the new path.
3113  *
3114  * @param t Tunnel we want to add a new peer to
3115  * @param p Path to add
3116  * @param own_pos Position of local node in path.
3117  *
3118  */
3119 static void
3120 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3121                  unsigned int own_pos)
3122 {
3123   struct GNUNET_PeerIdentity id;
3124
3125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3126   GNUNET_assert (0 != own_pos);
3127   tree_add_path (t->tree, p, NULL, NULL);
3128   if (own_pos < p->length - 1)
3129   {
3130     GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3131     tree_update_first_hops (t->tree, myid, &id);
3132   }
3133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3134 }
3135
3136 /**
3137  * Add a client to a tunnel, initializing all needed data structures.
3138  * 
3139  * @param t Tunnel to which add the client.
3140  * @param c Client which to add to the tunnel.
3141  */
3142 static void
3143 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3144 {
3145   struct MeshTunnelClientInfo clinfo;
3146
3147   GNUNET_array_append (t->clients, t->nclients, c);
3148   clinfo.fwd_ack = t->fwd_pid + 1;
3149   clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3150   clinfo.fwd_pid = t->fwd_pid;
3151   clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3152   t->nclients--;
3153   GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3154 }
3155
3156
3157 /**
3158  * Notifies a tunnel that a connection has broken that affects at least
3159  * some of its peers. Sends a notification towards the root of the tree.
3160  * In case the peer is the owner of the tree, notifies the client that owns
3161  * the tunnel and tries to reconnect.
3162  *
3163  * @param t Tunnel affected.
3164  * @param p1 Peer that got disconnected from p2.
3165  * @param p2 Peer that got disconnected from p1.
3166  *
3167  * @return Short ID of the peer disconnected (either p1 or p2).
3168  *         0 if the tunnel remained unaffected.
3169  */
3170 static GNUNET_PEER_Id
3171 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3172                                  GNUNET_PEER_Id p2)
3173 {
3174   GNUNET_PEER_Id pid;
3175
3176   pid =
3177       tree_notify_connection_broken (t->tree, p1, p2,
3178                                      &tunnel_notify_client_peer_disconnected,
3179                                      t);
3180   if (myid != p1 && myid != p2)
3181   {
3182     return pid;
3183   }
3184   if (pid != myid)
3185   {
3186     if (tree_get_predecessor (t->tree) != 0)
3187     {
3188       /* We are the peer still connected, notify owner of the disconnection. */
3189       struct GNUNET_MESH_PathBroken msg;
3190       struct GNUNET_PeerIdentity neighbor;
3191
3192       msg.header.size = htons (sizeof (msg));
3193       msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3194       GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3195       msg.tid = htonl (t->id.tid);
3196       msg.peer1 = my_full_id;
3197       GNUNET_PEER_resolve (pid, &msg.peer2);
3198       GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3199       send_message (&msg.header, &neighbor, t);
3200     }
3201   }
3202   return pid;
3203 }
3204
3205
3206 /**
3207  * Send a multicast packet to a neighbor.
3208  *
3209  * @param cls Closure (Info about the multicast packet)
3210  * @param neighbor_id Short ID of the neighbor to send the packet to.
3211  */
3212 static void
3213 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3214 {
3215   struct MeshData *mdata = cls;
3216   struct MeshTransmissionDescriptor *info;
3217   struct GNUNET_PeerIdentity neighbor;
3218
3219   info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3220
3221   info->mesh_data = mdata;
3222   (*(mdata->reference_counter)) ++;
3223   info->destination = neighbor_id;
3224   GNUNET_PEER_resolve (neighbor_id, &neighbor);
3225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   sending to %s...\n",
3226               GNUNET_i2s (&neighbor));
3227   info->peer = peer_info_get (&neighbor);
3228   GNUNET_assert (NULL != info->peer);
3229   queue_add(info,
3230             GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3231             info->mesh_data->data_len,
3232             info->peer,
3233             mdata->t);
3234 }
3235
3236
3237 /**
3238  * Send a message in a tunnel in multicast, sending a copy to each child node
3239  * down the local one in the tunnel tree.
3240  *
3241  * @param t Tunnel in which to send the data.
3242  * @param msg Message to be sent.
3243  * @param internal Has the service generated this message?
3244  */
3245 static void
3246 tunnel_send_multicast (struct MeshTunnel *t,
3247                        const struct GNUNET_MessageHeader *msg,
3248                        int internal)
3249 {
3250   struct MeshData *mdata;
3251
3252   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253               " sending a multicast packet...\n");
3254   mdata = GNUNET_malloc (sizeof (struct MeshData));
3255   mdata->data_len = ntohs (msg->size);
3256   mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3257   mdata->t = t;
3258   mdata->data = GNUNET_malloc (mdata->data_len);
3259   memcpy (mdata->data, msg, mdata->data_len);
3260   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3261   {
3262     struct GNUNET_MESH_Multicast *mcast;
3263
3264     mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3265     mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3266     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  data packet, ttl: %u\n",
3267                 ntohl (mcast->ttl));
3268   }
3269   else
3270   {
3271     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  not a data packet, no ttl\n");
3272   }
3273   if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3274       && GNUNET_NO == internal)
3275   {
3276     mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3277     (*(mdata->task)) =
3278         GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3279                                       mdata);
3280     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3281                 *(mdata->task));
3282   }
3283
3284   tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3285   if (*(mdata->reference_counter) == 0)
3286   {
3287     GNUNET_free (mdata->data);
3288     GNUNET_free (mdata->reference_counter);
3289     if (NULL != mdata->task)
3290     {
3291       GNUNET_SCHEDULER_cancel(*(mdata->task));
3292       GNUNET_free (mdata->task);
3293       GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3294     }
3295     // FIXME change order?
3296     GNUNET_free (mdata);
3297   }
3298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299               " sending a multicast packet done\n");
3300   return;
3301 }
3302
3303
3304 /**
3305  * Increase the SKIP value of all peers that
3306  * have not received a unicast message.
3307  *
3308  * @param cls Closure (ID of the peer that HAS received the message).
3309  * @param key ID of the neighbor.
3310  * @param value Information about the neighbor.
3311  *
3312  * @return GNUNET_YES to keep iterating.
3313  */
3314 static int
3315 tunnel_add_skip (void *cls,
3316                  const struct GNUNET_HashCode * key,
3317                  void *value)
3318 {
3319   struct GNUNET_PeerIdentity *neighbor = cls;
3320   struct MeshTunnelChildInfo *cinfo = value;
3321
3322   /* TODO compare only pointers? key == neighbor? */
3323   if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3324   {
3325     return GNUNET_YES;
3326   }
3327   cinfo->skip++;
3328   return GNUNET_YES;
3329 }
3330
3331
3332 /**
3333  * @brief Get neighbor's Flow Control information.
3334  *
3335  * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3336  * descendant of the local node in a certain tunnel.
3337  * If the info is not yet there (recently created path), creates the data struct
3338  * and inserts it into the tunnel info, initialized to the current tunnel ACK
3339  * values.
3340  *
3341  * @param t Tunnel related.
3342  * @param peer Neighbor whose Flow Control info is needed.
3343  *
3344  * @return Neighbor's Flow Control info.
3345  */
3346 static struct MeshTunnelChildInfo *
3347 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3348                         const struct GNUNET_PeerIdentity *peer)
3349 {
3350   struct MeshTunnelChildInfo *cinfo;
3351   cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3352                                              &peer->hashPubKey);
3353   if (NULL == cinfo)
3354   {
3355     uint32_t delta;
3356
3357     cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3358     cinfo->id = GNUNET_PEER_intern (peer);
3359     cinfo->skip = t->fwd_pid;
3360
3361     delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3362     cinfo->fwd_ack = t->fwd_pid + delta;
3363     cinfo->bck_ack = delta;
3364
3365     GNUNET_assert (GNUNET_OK ==
3366       GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3367                                          &peer->hashPubKey,
3368                                          cinfo,
3369                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3370   }
3371   return cinfo;
3372 }
3373
3374
3375 /**
3376  * Get the Flow Control info of a client.
3377  * 
3378  * @param t Tunnel on which to look.
3379  * @param c Client whose ACK to get.
3380  * 
3381  * @return ACK value.
3382  */
3383 static struct MeshTunnelClientInfo *
3384 tunnel_get_client_fc (struct MeshTunnel *t,
3385                       struct MeshClient *c)
3386 {
3387   unsigned int i;
3388
3389   for (i = 0; i < t->nclients; i++)
3390   {
3391     if (t->clients[i] != c)
3392       continue;
3393     return &t->clients_fc[i];
3394   }
3395   GNUNET_assert (0);
3396   return NULL; // avoid compiler / coverity complaints
3397 }
3398
3399
3400 /**
3401  * Iterator to get the appropiate ACK value from all children nodes.
3402  *
3403  * @param cls Closue (tunnel).
3404  * @param id Id of the child node.
3405  */
3406 static void
3407 tunnel_get_child_fwd_ack (void *cls,
3408                           GNUNET_PEER_Id id)
3409 {
3410   struct GNUNET_PeerIdentity peer_id;
3411   struct MeshTunnelChildInfo *cinfo;
3412   struct MeshTunnelChildIteratorContext *ctx = cls;
3413   struct MeshTunnel *t = ctx->t;
3414   uint32_t ack;
3415
3416   GNUNET_PEER_resolve (id, &peer_id);
3417   cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3418   ack = cinfo->fwd_ack;
3419
3420   if (GNUNET_NO == ctx->init)
3421   {
3422     ctx->max_child_ack = ack;
3423     ctx->init = GNUNET_YES;
3424   }
3425
3426   if (GNUNET_YES == t->speed_min)
3427   {
3428     ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3429   }
3430   else
3431   {
3432     ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3433   }
3434
3435 }
3436
3437
3438 /**
3439  * Get the maximum PID allowed to transmit to any
3440  * tunnel child of the local peer, depending on the tunnel
3441  * buffering/speed settings.
3442  *
3443  * @param t Tunnel.
3444  *
3445  * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3446  */
3447 static int64_t
3448 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3449 {
3450   struct MeshTunnelChildIteratorContext ctx;
3451   ctx.t = t;
3452   ctx.max_child_ack = 0;
3453   ctx.nchildren = 0;
3454   tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3455
3456   if (0 == ctx.nchildren)
3457     return -1LL;
3458
3459   if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3460     ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3461
3462   return (int64_t) ctx.max_child_ack;
3463 }
3464
3465
3466 /**
3467  * Set the FWD ACK value of a client in a particular tunnel.
3468  * 
3469  * @param t Tunnel affected.
3470  * @param c Client whose ACK to set.
3471  * @param ack ACK value.
3472  */
3473 static void
3474 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3475                            struct MeshClient *c, 
3476                            uint32_t ack)
3477 {
3478   unsigned int i;
3479
3480   for (i = 0; i < t->nclients; i++)
3481   {
3482     if (t->clients[i] != c)
3483       continue;
3484     t->clients_fc[i].fwd_ack = ack;
3485     return;
3486   }
3487   GNUNET_break (0);
3488 }
3489
3490
3491 /**
3492  * Get the highest ACK value of all clients in a particular tunnel,
3493  * according to the buffering/speed settings.
3494  * 
3495  * @param t Tunnel on which to look.
3496  * 
3497  * @return Corresponding ACK value (max uint32_t).
3498  *         If no clients are suscribed, -1.
3499  */
3500 static int64_t
3501 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3502 {
3503   unsigned int i;
3504   int64_t ack;
3505
3506   if (0 == t->nclients)
3507     return -1;
3508
3509   for (ack = -1, i = 0; i < t->nclients; i++)
3510   {
3511     if (-1 == ack ||
3512         (GNUNET_YES == t->speed_min &&
3513          GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3514         (GNUNET_NO == t->speed_min &&
3515          GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3516     {
3517       ack = t->clients_fc[i].fwd_ack;
3518     }
3519   }
3520
3521   if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3522     ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3523
3524   return (uint32_t) ack;
3525 }
3526
3527
3528 /**
3529  * Get the current fwd ack value for a tunnel, taking in account the tunnel
3530  * mode and the status of all children nodes.
3531  *
3532  * @param t Tunnel.
3533  *
3534  * @return Maximum PID allowed.
3535  */
3536 static uint32_t
3537 tunnel_get_fwd_ack (struct MeshTunnel *t)
3538 {
3539   uint32_t count;
3540   uint32_t buffer_free;
3541   int64_t child_ack;
3542   int64_t client_ack;
3543   uint32_t ack;
3544
3545   count = t->fwd_pid - t->skip;
3546   buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3547   ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3548   child_ack = tunnel_get_children_fwd_ack (t);
3549   client_ack = tunnel_get_clients_fwd_ack (t);
3550   if (-1 == child_ack)
3551   {
3552     // Node has no children, child_ack AND core buffer are irrelevant.
3553     GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3554     return (uint32_t) client_ack;
3555   }
3556
3557   if (GNUNET_YES == t->speed_min)
3558   {
3559     ack = GMC_min_pid ((uint32_t) child_ack, ack);
3560     ack = GMC_min_pid ((uint32_t) client_ack, ack);
3561   }
3562   else
3563   {
3564     ack = GMC_max_pid ((uint32_t) child_ack, ack);
3565     ack = GMC_max_pid ((uint32_t) client_ack, ack);
3566   }
3567   if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3568     ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3569   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3570               count, buffer_free, child_ack, client_ack, ack);
3571   return ack;
3572 }
3573
3574
3575 /**
3576  * Build a local ACK message and send it to a local client.
3577  * 
3578  * @param t Tunnel on which to send the ACK.
3579  * @param c Client to whom send the ACK.
3580  * @param ack Value of the ACK.
3581  */
3582 static void
3583 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3584 {
3585   struct GNUNET_MESH_LocalAck msg;
3586
3587   msg.header.size = htons (sizeof (msg));
3588   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3589   msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3590   msg.max_pid = htonl (ack); 
3591   GNUNET_SERVER_notification_context_unicast(nc,
3592                                               c->handle,
3593                                               &msg.header,
3594                                               GNUNET_NO);
3595 }
3596
3597 /**
3598  * Build an ACK message and queue it to send to the given peer.
3599  * 
3600  * @param t Tunnel on which to send the ACK.
3601  * @param peer Peer to whom send the ACK.
3602  * @param ack Value of the ACK.
3603  */
3604 static void
3605 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer,  uint32_t ack)
3606 {
3607   struct GNUNET_MESH_ACK msg;
3608
3609   GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3610   msg.header.size = htons (sizeof (msg));
3611   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3612   msg.pid = htonl (ack);
3613   msg.tid = htonl (t->id.tid);
3614
3615   send_message (&msg.header, peer, t);
3616 }
3617
3618
3619 /**
3620  * Notify a the owner of a tunnel about how many more
3621  * payload packages will we accept on a given tunnel.
3622  *
3623  * @param t Tunnel on which to send the ACK.
3624  */
3625 static void
3626 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3627 {
3628   uint32_t ack;
3629
3630   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3631               "Sending client FWD ACK on tunnel %X\n",
3632               t->local_tid);
3633
3634   ack = tunnel_get_fwd_ack (t);
3635
3636   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3637   if (t->last_fwd_ack == ack)
3638     return;
3639
3640   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3641   t->last_fwd_ack = ack;
3642   send_local_ack (t, t->owner, ack);
3643 }
3644
3645
3646 /**
3647  * Send an ACK informing the predecessor about the available buffer space.
3648  * In case there is no predecessor, inform the owning client.
3649  * If buffering is off, send only on behalf of children or self if endpoint.
3650  * If buffering is on, send when sent to children and buffer space is free.
3651  * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3652  * the ACK itself goes "back" (towards root).
3653  * 
3654  * @param t Tunnel on which to send the ACK.
3655  * @param type Type of message that triggered the ACK transmission.
3656  */
3657 static void
3658 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3659 {
3660   struct GNUNET_PeerIdentity id;
3661   uint32_t ack;
3662
3663   if (NULL != t->owner)
3664   {
3665     tunnel_send_client_fwd_ack (t);
3666     return;
3667   }
3668   /* Is it after unicast / multicast retransmission? */
3669   switch (type)
3670   {
3671     case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3672     case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3673       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3674                   "ACK due to FWD DATA retransmission\n");
3675       if (GNUNET_YES == t->nobuffer)
3676       {
3677         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3678         return;
3679       }
3680       if (t->fwd_queue_max > t->fwd_queue_n * 2)
3681       {
3682         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3683         return;
3684       }
3685       break;
3686     case GNUNET_MESSAGE_TYPE_MESH_ACK:
3687     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3688       break;
3689     default:
3690       GNUNET_break (0);
3691   }
3692
3693   /* Ok, ACK might be necessary, what PID to ACK? */
3694   ack = tunnel_get_fwd_ack (t);
3695
3696   /* If speed_min and not all children have ack'd, dont send yet */
3697   if (ack == t->last_fwd_ack)
3698   {
3699     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3700     return;
3701   }
3702
3703   t->last_fwd_ack = ack;
3704   GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3705   send_ack (t, &id, ack);
3706 }
3707
3708
3709 /**
3710  * Iterator to send a child node a BCK ACK to allow him to send more
3711  * to_origin data.
3712  *
3713  * @param cls Closure (tunnel).
3714  * @param id Id of the child node.
3715  */
3716 static void
3717 tunnel_send_child_bck_ack (void *cls,
3718                            GNUNET_PEER_Id id)
3719 {
3720   struct MeshTunnel *t = cls;
3721   struct MeshTunnelChildInfo *cinfo;
3722   struct GNUNET_PeerIdentity peer;
3723
3724   GNUNET_PEER_resolve (id, &peer);
3725   cinfo = tunnel_get_neighbor_fc (t, &peer);
3726
3727   if (cinfo->bck_ack != cinfo->pid &&
3728       GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3729     return;
3730
3731   cinfo->bck_ack++;
3732   send_ack (t, &peer, cinfo->bck_ack);
3733 }
3734
3735
3736 /**
3737  * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3738  * 
3739  * Iterates over all clients and sends BCK ACKs to the ones that need it.
3740  * 
3741  * @param t Tunnel on which to send the BCK ACKs.
3742  */
3743 static void
3744 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3745 {
3746   unsigned int i;
3747   unsigned int tunnel_delta;
3748
3749   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Sending BCK ACK to clients\n");
3750
3751   tunnel_delta = t->bck_ack - t->bck_pid;
3752   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   tunnel delta: %u\n", tunnel_delta);
3753
3754   /* Find client whom to allow to send to origin (with lowest buffer space) */
3755   for (i = 0; i < t->nclients; i++)
3756   {
3757     struct MeshTunnelClientInfo *clinfo;
3758     unsigned int delta;
3759
3760     clinfo = &t->clients_fc[i];
3761     delta = clinfo->bck_ack - clinfo->bck_pid;
3762     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "    client %u delta: %u\n",
3763          t->clients[i]->id, delta);
3764
3765     if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3766         (GNUNET_YES == t->nobuffer && 0 == delta))
3767     {
3768       uint32_t ack;
3769
3770       ack = clinfo->bck_pid;
3771       ack += t->nobuffer ? 1 : tunnel_delta;
3772       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3773                   "    sending ack to client %u: %u\n",
3774                   t->clients[i]->id, ack);
3775       send_local_ack (t, t->clients[i], ack);
3776       clinfo->bck_ack = ack;
3777     }
3778   }
3779 }
3780
3781
3782 /**
3783  * Send an ACK informing the children nodes and destination clients about
3784  * the available buffer space.
3785  * If buffering is off, send only on behalf of root (can be self).
3786  * If buffering is on, send when sent to predecessor and buffer space is free.
3787  * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3788  * the ACK itself goes "forward" (towards children/clients).
3789  * 
3790  * @param t Tunnel on which to send the ACK.
3791  * @param type Type of message that triggered the ACK transmission.
3792  */
3793 static void
3794 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3795 {
3796   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3797               "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3798               t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3799   /* Is it after data to_origin retransmission? */
3800   switch (type)
3801   {
3802     case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3803       if (GNUNET_YES == t->nobuffer)
3804       {
3805         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3806                     "    Not sending ACK, nobuffer\n");
3807         return;
3808       }
3809       break;
3810     case GNUNET_MESSAGE_TYPE_MESH_ACK:
3811     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3812       break;
3813     default:
3814       GNUNET_break (0);
3815   }
3816
3817   tunnel_send_clients_bck_ack (t);
3818   tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3819 }
3820
3821
3822 /**
3823  * Send a message to all peers in this tunnel that the tunnel is no longer
3824  * valid.
3825  *
3826  * @param t The tunnel whose peers to notify.
3827  */
3828 static void
3829 tunnel_send_destroy (struct MeshTunnel *t)
3830 {
3831   struct GNUNET_MESH_TunnelDestroy msg;
3832
3833   msg.header.size = htons (sizeof (msg));
3834   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3835   GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3836   msg.tid = htonl (t->id.tid);
3837   tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3838 }
3839
3840
3841 /**
3842  * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3843  *
3844  * @param cls Closure (Tunnel which to cancel).
3845  * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3846  */
3847 static void
3848 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3849 {
3850   struct MeshTunnel *t = cls;
3851   struct MeshPeerInfo *peer_info;
3852   struct MeshPeerQueue *pq;
3853   struct MeshPeerQueue *next;
3854
3855   peer_info = peer_info_get_short (neighbor_id);
3856   for (pq = peer_info->queue_head; NULL != pq; pq = next)
3857   {
3858     next = pq->next;
3859     if (pq->tunnel == t)
3860     {
3861       queue_destroy (pq, GNUNET_YES);
3862     }
3863   }
3864   if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3865   {
3866     GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3867     peer_info->core_transmit = NULL;
3868   }
3869 }
3870
3871 /**
3872  * Destroy the tunnel and free any allocated resources linked to it.
3873  *
3874  * @param t the tunnel to destroy
3875  *
3876  * @return GNUNET_OK on success
3877  */
3878 static int
3879 tunnel_destroy (struct MeshTunnel *t)
3880 {
3881   struct MeshClient *c;
3882   struct GNUNET_HashCode hash;
3883   unsigned int i;
3884   int r;
3885
3886   if (NULL == t)
3887     return GNUNET_OK;
3888
3889   tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3890
3891   r = GNUNET_OK;
3892   c = t->owner;
3893 #if MESH_DEBUG
3894   {
3895     struct GNUNET_PeerIdentity id;
3896
3897     GNUNET_PEER_resolve (t->id.oid, &id);
3898     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3899                 GNUNET_i2s (&id), t->id.tid);
3900     if (NULL != c)
3901       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
3902   }
3903 #endif
3904
3905   GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3906   if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3907   {
3908     r = GNUNET_SYSERR;
3909   }
3910
3911   GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3912   if (NULL != c &&
3913       GNUNET_YES !=
3914       GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3915   {
3916     r = GNUNET_SYSERR;
3917   }
3918   GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3919   for (i = 0; i < t->nclients; i++)
3920   {
3921     c = t->clients[i];
3922     if (GNUNET_YES !=
3923           GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3924     {
3925       r = GNUNET_SYSERR;
3926     }
3927   }
3928   for (i = 0; i < t->nignore; i++)
3929   {
3930     c = t->ignore[i];
3931     if (GNUNET_YES !=
3932           GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3933     {
3934       r = GNUNET_SYSERR;
3935     }
3936   }
3937   if (t->nclients > 0)
3938   {
3939     if (GNUNET_YES !=
3940         GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3941     {
3942       r = GNUNET_SYSERR;
3943     }
3944     GNUNET_free (t->clients);
3945   }
3946   if (NULL != t->peers)
3947   {
3948     GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3949                                            t);
3950     GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3951   }
3952
3953   GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3954                                          &tunnel_destroy_child,
3955                                          t);
3956   GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3957
3958   tree_destroy (t->tree);
3959
3960   if (NULL != t->regex_ctx)
3961     regex_cancel_search (t->regex_ctx);
3962   if (NULL != t->dht_get_type)
3963     GNUNET_DHT_get_stop (t->dht_get_type);
3964   if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3965     GNUNET_SCHEDULER_cancel (t->timeout_task);
3966   if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3967     GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3968
3969   n_tunnels--;
3970   GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3971   GNUNET_assert (0 <= n_tunnels);
3972   GNUNET_free (t);
3973   return r;
3974 }
3975
3976
3977 /**
3978  * Create a new tunnel
3979  * 
3980  * @param owner Who is the owner of the tunnel (short ID).
3981  * @param tid Tunnel Number of the tunnel.
3982  * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3983  * @param local Tunnel Number for the tunnel, for the client point of view.
3984  * 
3985  * @return A new initialized tunnel. NULL on error.
3986  */
3987 static struct MeshTunnel *
3988 tunnel_new (GNUNET_PEER_Id owner,
3989             MESH_TunnelNumber tid,
3990             struct MeshClient *client,
3991             MESH_TunnelNumber local)
3992 {
3993   struct MeshTunnel *t;
3994   struct GNUNET_HashCode hash;
3995
3996   if (n_tunnels >= max_tunnels && NULL == client)
3997     return NULL;
3998
3999   t = GNUNET_malloc (sizeof (struct MeshTunnel));
4000   t->id.oid = owner;
4001   t->id.tid = tid;
4002   t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4003   t->bck_queue_max = t->fwd_queue_max;
4004   t->tree = tree_new (owner);
4005   t->owner = client;
4006   t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4007   t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4008   t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4009   t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4010   t->local_tid = local;
4011   t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4012   n_tunnels++;
4013   GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4014
4015   GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4016   if (GNUNET_OK !=
4017       GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4018                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4019   {
4020     GNUNET_break (0);
4021     tunnel_destroy (t);
4022     if (NULL != client)
4023     {
4024       GNUNET_break (0);
4025       GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4026     }
4027     return NULL;
4028   }
4029
4030   if (NULL != client)
4031   {
4032     GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4033     if (GNUNET_OK !=
4034         GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4035                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4036     {
4037       tunnel_destroy (t);
4038       GNUNET_break (0);
4039       GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4040       return NULL;
4041     }
4042   }
4043
4044   return t;
4045 }
4046
4047
4048 /**
4049  * Removes an explicit path from a tunnel, freeing all intermediate nodes
4050  * that are no longer needed, as well as nodes of no longer reachable peers.
4051  * The tunnel itself is also destoyed if results in a remote empty tunnel.
4052  *
4053  * @param t Tunnel from which to remove the path.
4054  * @param peer Short id of the peer which should be removed.
4055  */
4056 static void
4057 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4058 {
4059   if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4060     tunnel_destroy (t);
4061 }
4062
4063
4064 /**
4065  * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4066  * client when the client disconnects. If the client is not the owner, the
4067  * owner will get notified if no more clients are in the tunnel and the client
4068  * get removed from the tunnel's list.
4069  *
4070  * @param cls closure (client that is disconnecting)
4071  * @param key the hash of the local tunnel id (used to access the hashmap)
4072  * @param value the value stored at the key (tunnel to destroy)
4073  *
4074  * @return GNUNET_OK on success
4075  */
4076 static int
4077 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4078 {
4079   struct MeshTunnel *t = value;
4080   struct MeshClient *c = cls;
4081   int r;
4082
4083   send_client_tunnel_disconnect(t, c);
4084   if (c != t->owner)
4085   {
4086     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4087                 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4088     tunnel_delete_client(t, c);
4089     client_delete_tunnel(c, t);
4090     return GNUNET_OK;
4091   }
4092   tunnel_send_destroy(t);
4093   r = tunnel_destroy (t);
4094   return r;
4095 }
4096
4097
4098 /**
4099  * Timeout function, destroys tunnel if called
4100  *
4101  * @param cls Closure (tunnel to destroy).
4102  * @param tc TaskContext
4103  */
4104 static void
4105 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4106 {
4107   struct MeshTunnel *t = cls;
4108
4109   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4110     return;
4111   t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4112   tunnel_destroy (t);
4113 }
4114
4115 /**
4116  * Resets the tunnel timeout. Starts it if no timeout was running.
4117  *
4118  * @param t Tunnel whose timeout to reset.
4119  *
4120  * TODO use heap to improve efficiency of scheduler.
4121  */
4122 static void
4123 tunnel_reset_timeout (struct MeshTunnel *t)
4124 {
4125   if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4126     GNUNET_SCHEDULER_cancel (t->timeout_task);
4127   t->timeout_task =
4128       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4129                                     (refresh_path_time, 4), &tunnel_timeout, t);
4130 }
4131
4132
4133 /******************************************************************************/
4134 /****************      MESH NETWORK HANDLER HELPERS     ***********************/
4135 /******************************************************************************/
4136
4137 /**
4138  * Function to send a create path packet to a peer.
4139  *
4140  * @param cls closure
4141  * @param size number of bytes available in buf
4142  * @param buf where the callee should write the message
4143  * @return number of bytes written to buf
4144  */
4145 static size_t
4146 send_core_path_create (void *cls, size_t size, void *buf)
4147 {
4148   struct MeshPathInfo *info = cls;
4149   struct GNUNET_MESH_ManipulatePath *msg;
4150   struct GNUNET_PeerIdentity *peer_ptr;
4151   struct MeshTunnel *t = info->t;
4152   struct MeshPeerPath *p = info->path;
4153   size_t size_needed;
4154   uint32_t opt;
4155   int i;
4156
4157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4158   size_needed =
4159       sizeof (struct GNUNET_MESH_ManipulatePath) +
4160       p->length * sizeof (struct GNUNET_PeerIdentity);
4161
4162   if (size < size_needed || NULL == buf)
4163   {
4164     GNUNET_break (0);
4165     return 0;
4166   }
4167   msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4168   msg->header.size = htons (size_needed);
4169   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4170   msg->tid = ntohl (t->id.tid);
4171
4172   if (GNUNET_YES == t->speed_min)
4173     opt = MESH_TUNNEL_OPT_SPEED_MIN;
4174   if (GNUNET_YES == t->nobuffer)
4175     opt |= MESH_TUNNEL_OPT_NOBUFFER;
4176   msg->opt = htonl(opt);
4177
4178   peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4179   for (i = 0; i < p->length; i++)
4180   {
4181     GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4182   }
4183
4184   path_destroy (p);
4185   GNUNET_free (info);
4186
4187   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4188               "CREATE PATH (%u bytes long) sent!\n", size_needed);
4189   return size_needed;
4190 }
4191
4192
4193 /**
4194  * Fill the core buffer 
4195  *
4196  * @param cls closure (data itself)
4197  * @param size number of bytes available in buf
4198  * @param buf where the callee should write the message
4199  *
4200  * @return number of bytes written to buf
4201  */
4202 static size_t
4203 send_core_data_multicast (void *cls, size_t size, void *buf)
4204 {
4205   struct MeshTransmissionDescriptor *info = cls;
4206   size_t total_size;
4207
4208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4209   GNUNET_assert (NULL != info);
4210   GNUNET_assert (NULL != info->peer);
4211   total_size = info->mesh_data->data_len;
4212   GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4213
4214   if (total_size > size)
4215   {
4216     GNUNET_break (0);
4217     return 0;
4218   }
4219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4220   memcpy (buf, info->mesh_data->data, total_size);
4221 #if MESH_DEBUG
4222   {
4223     struct GNUNET_MESH_Multicast *mc;
4224     struct GNUNET_MessageHeader *mh;
4225
4226     mh = buf;
4227     if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4228     {
4229       mc = (struct GNUNET_MESH_Multicast *) mh;
4230       mh = (struct GNUNET_MessageHeader *) &mc[1];
4231       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4232                   " multicast, payload type %s\n",
4233                   GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4234       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4235                   " multicast, payload size %u\n", ntohs (mh->size));
4236     }
4237     else
4238     {
4239       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4240                   GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4241     }
4242   }
4243 #endif
4244   data_descriptor_decrement_rc (info->mesh_data);
4245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4246   GNUNET_free (info);
4247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4248   return total_size;
4249 }
4250
4251
4252 /**
4253  * Creates a path ack message in buf and frees all unused resources.
4254  *
4255  * @param cls closure (MeshTransmissionDescriptor)
4256  * @param size number of bytes available in buf
4257  * @param buf where the callee should write the message
4258  * @return number of bytes written to buf
4259  */
4260 static size_t
4261 send_core_path_ack (void *cls, size_t size, void *buf)
4262 {
4263   struct MeshTransmissionDescriptor *info = cls;
4264   struct GNUNET_MESH_PathACK *msg = buf;
4265
4266   GNUNET_assert (NULL != info);
4267   if (sizeof (struct GNUNET_MESH_PathACK) > size)
4268   {
4269     GNUNET_break (0);
4270     return 0;
4271   }
4272   msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4273   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4274   GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4275   msg->tid = htonl (info->origin->tid);
4276   msg->peer_id = my_full_id;
4277
4278   GNUNET_free (info);
4279   /* TODO add signature */
4280
4281   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4282   return sizeof (struct GNUNET_MESH_PathACK);
4283 }
4284
4285
4286 /**
4287  * Free a transmission that was already queued with all resources
4288  * associated to the request.
4289  *
4290  * @param queue Queue handler to cancel.
4291  * @param clear_cls Is it necessary to free associated cls?
4292  */
4293 static void
4294 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4295 {
4296   struct MeshTransmissionDescriptor *dd;
4297   struct MeshPathInfo *path_info;
4298
4299   if (GNUNET_YES == clear_cls)
4300   {
4301     switch (queue->type)
4302     {
4303     case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4304     case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4305     case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4306         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   type payload\n");
4307         dd = queue->cls;
4308         data_descriptor_decrement_rc (dd->mesh_data);
4309         break;
4310     case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4311         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   type create path\n");
4312         path_info = queue->cls;
4313         path_destroy (path_info->path);
4314         break;
4315     default:
4316         GNUNET_break (0);
4317         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   type unknown!\n");
4318     }
4319     GNUNET_free_non_null (queue->cls);
4320   }
4321   GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4322                                queue->peer->queue_tail,
4323                                queue);
4324   GNUNET_free (queue);
4325 }
4326
4327
4328 /**
4329   * Core callback to write a queued packet to core buffer
4330   *
4331   * @param cls Closure (peer info).
4332   * @param size Number of bytes available in buf.
4333   * @param buf Where the to write the message.
4334   *
4335   * @return number of bytes written to buf
4336   */
4337 static size_t
4338 queue_send (void *cls, size_t size, void *buf)
4339 {
4340     struct MeshPeerInfo *peer = cls;
4341     struct GNUNET_MessageHeader *msg;
4342     struct MeshPeerQueue *queue;
4343     struct MeshTunnel *t;
4344     size_t data_size;
4345
4346     peer->core_transmit = NULL;
4347     queue = peer->queue_head;
4348
4349     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4350
4351     /* If queue is empty, send should have been cancelled */
4352     if (NULL == queue)
4353     {
4354         GNUNET_break(0);
4355         return 0;
4356     }
4357     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   not empty\n");
4358
4359     /* Check if buffer size is enough for the message */
4360     if (queue->size > size)
4361     {
4362         struct GNUNET_PeerIdentity id;
4363
4364         GNUNET_PEER_resolve (peer->id, &id);
4365         peer->core_transmit =
4366             GNUNET_CORE_notify_transmit_ready(core_handle,
4367                                               0,
4368                                               0,
4369                                               GNUNET_TIME_UNIT_FOREVER_REL,
4370                                               &id,
4371                                               queue->size,
4372                                               &queue_send,
4373                                               peer);
4374         return 0;
4375     }
4376     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   size ok\n");
4377
4378     t = queue->tunnel;
4379     if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4380     {
4381       t->fwd_queue_n--;
4382       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   unicast: %u\n");
4383     }
4384     else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4385     {
4386       t->bck_queue_n--;
4387       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   to origin\n");
4388     }
4389
4390     /* Fill buf */
4391     switch (queue->type)
4392     {
4393       case 0:
4394       case GNUNET_MESSAGE_TYPE_MESH_ACK:
4395       case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4396       case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4397         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4398                     "*********   raw: %u\n",
4399                     queue->type);
4400         /* Fall through */
4401       case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4402       case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4403         data_size = send_core_data_raw (queue->cls, size, buf);
4404         msg = (struct GNUNET_MessageHeader *) buf;
4405         switch (ntohs (msg->type)) // Type of preconstructed message
4406         {
4407           case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4408             tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4409             break;
4410           case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4411             tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4412             break;
4413           default:
4414               break;
4415         }
4416         break;
4417       case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4418         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   multicast\n");
4419         data_size = send_core_data_multicast(queue->cls, size, buf);
4420         tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4421         break;
4422       case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4423         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   path create\n");
4424         data_size = send_core_path_create(queue->cls, size, buf);
4425         break;
4426       case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4427         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   path ack\n");
4428         data_size = send_core_path_ack(queue->cls, size, buf);
4429         break;
4430       default:
4431         GNUNET_break (0);
4432         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4433                     "*********   type unknown: %u\n",
4434                     queue->type);
4435         data_size = 0;
4436     }
4437
4438     /* Free queue, but cls was freed by send_core_* */
4439     queue_destroy (queue, GNUNET_NO);
4440
4441     if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4442     {
4443       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********  destroying tunnel!\n");
4444       tunnel_destroy (t);
4445     }
4446
4447     /* If more data in queue, send next */
4448     if (NULL != peer->queue_head)
4449     {
4450         struct GNUNET_PeerIdentity id;
4451
4452         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   more data!\n");
4453         GNUNET_PEER_resolve (peer->id, &id);
4454         peer->core_transmit =
4455             GNUNET_CORE_notify_transmit_ready(core_handle,
4456                                               0,
4457                                               0,
4458                                               GNUNET_TIME_UNIT_FOREVER_REL,
4459                                               &id,
4460                                               peer->queue_head->size,
4461                                               &queue_send,
4462                                               peer);
4463     }
4464     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   return %d\n", data_size);
4465     return data_size;
4466 }
4467
4468
4469 /**
4470  * Queue and pass message to core when possible.
4471  *
4472  * @param cls Closure (type dependant).
4473  * @param type Type of the message, 0 for a raw message.
4474  * @param size Size of the message.
4475  * @param dst Neighbor to send message to.
4476  * @param t Tunnel this message belongs to.
4477  */
4478 static void
4479 queue_add (void *cls, uint16_t type, size_t size,
4480            struct MeshPeerInfo *dst, struct MeshTunnel *t)
4481 {
4482   struct MeshPeerQueue *queue;
4483   unsigned int *max;
4484   unsigned int *n;
4485
4486   n = NULL;
4487   if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4488       GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4489   {
4490     n = &t->fwd_queue_n;
4491     max = &t->fwd_queue_max;
4492   }
4493   else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4494   {
4495     n = &t->bck_queue_n;
4496     max = &t->bck_queue_max;
4497   }
4498   if (NULL != n) {
4499     if (*n >= *max)
4500     {
4501       if (NULL == t->owner)
4502         GNUNET_break_op(0);       // TODO: kill connection?
4503       else
4504         GNUNET_break(0);
4505       return;                       // Drop message
4506     }
4507     (*n)++;
4508   }
4509   queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4510   queue->cls = cls;
4511   queue->type = type;
4512   queue->size = size;
4513   queue->peer = dst;
4514   queue->tunnel = t;
4515   GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4516   if (NULL == dst->core_transmit)
4517   {
4518       struct GNUNET_PeerIdentity id;
4519
4520       GNUNET_PEER_resolve (dst->id, &id);
4521       dst->core_transmit =
4522           GNUNET_CORE_notify_transmit_ready(core_handle,
4523                                             0,
4524                                             0,
4525                                             GNUNET_TIME_UNIT_FOREVER_REL,
4526                                             &id,
4527                                             size,
4528                                             &queue_send,
4529                                             dst);
4530   }
4531 }
4532
4533
4534 /******************************************************************************/
4535 /********************      MESH NETWORK HANDLERS     **************************/
4536 /******************************************************************************/
4537
4538
4539 /**
4540  * Core handler for path creation
4541  *
4542  * @param cls closure
4543  * @param message message
4544  * @param peer peer identity this notification is about
4545  * @param atsi performance data
4546  * @param atsi_count number of records in 'atsi'
4547  *
4548  * @return GNUNET_OK to keep the connection open,
4549  *         GNUNET_SYSERR to close it (signal serious error)
4550  */
4551 static int
4552 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4553                          const struct GNUNET_MessageHeader *message,
4554                          const struct GNUNET_ATS_Information *atsi,
4555                          unsigned int atsi_count)
4556 {
4557   unsigned int own_pos;
4558   uint16_t size;
4559   uint16_t i;
4560   MESH_TunnelNumber tid;
4561   struct GNUNET_MESH_ManipulatePath *msg;
4562   struct GNUNET_PeerIdentity *pi;
4563   struct GNUNET_HashCode hash;
4564   struct MeshPeerPath *path;
4565   struct MeshPeerInfo *dest_peer_info;
4566   struct MeshPeerInfo *orig_peer_info;
4567   struct MeshTunnel *t;
4568
4569   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4570               "Received a path create msg [%s]\n",
4571               GNUNET_i2s (&my_full_id));
4572   size = ntohs (message->size);
4573   if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4574   {
4575     GNUNET_break_op (0);
4576     return GNUNET_OK;
4577   }
4578
4579   size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4580   if (size % sizeof (struct GNUNET_PeerIdentity))
4581   {
4582     GNUNET_break_op (0);
4583     return GNUNET_OK;
4584   }
4585   size /= sizeof (struct GNUNET_PeerIdentity);
4586   if (size < 2)
4587   {
4588     GNUNET_break_op (0);
4589     return GNUNET_OK;
4590   }
4591   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "    path has %u hops.\n", size);
4592   msg = (struct GNUNET_MESH_ManipulatePath *) message;
4593
4594   tid = ntohl (msg->tid);
4595   pi = (struct GNUNET_PeerIdentity *) &msg[1];
4596   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4597               "    path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4598   t = tunnel_get (pi, tid);
4599   if (NULL == t) // FIXME only for INCOMING tunnels?
4600   {
4601     uint32_t opt;
4602
4603     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Creating tunnel\n");
4604     t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4605     if (NULL == t)
4606     {
4607       // FIXME notify failure
4608       return GNUNET_OK;
4609     }
4610     opt = ntohl (msg->opt);
4611     t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4612                    GNUNET_YES : GNUNET_NO;
4613     t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4614                   GNUNET_YES : GNUNET_NO;
4615
4616     if (GNUNET_YES == t->nobuffer)
4617     {
4618       t->bck_queue_max = 1;
4619       t->fwd_queue_max = 1;
4620     }
4621
4622     // FIXME only assign a local tid if a local client is interested (on demand)
4623     while (NULL != tunnel_get_incoming (next_local_tid))
4624       next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4625     t->local_tid_dest = next_local_tid++;
4626     next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4627     // FIXME end
4628
4629     tunnel_reset_timeout (t);
4630     GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4631     if (GNUNET_OK !=
4632         GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4633                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4634     {
4635       tunnel_destroy (t);
4636       GNUNET_break (0);
4637       return GNUNET_OK;
4638     }
4639   }
4640   dest_peer_info =
4641       GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4642   if (NULL == dest_peer_info)
4643   {
4644     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4645                 "  Creating PeerInfo for destination.\n");
4646     dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4647     dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4648     GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4649                                        dest_peer_info,
4650                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4651   }
4652   orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4653   if (NULL == orig_peer_info)
4654   {
4655     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4656                 "  Creating PeerInfo for origin.\n");
4657     orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4658     orig_peer_info->id = GNUNET_PEER_intern (pi);
4659     GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4660                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4661   }
4662   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Creating path...\n");
4663   path = path_new (size);
4664   own_pos = 0;
4665   for (i = 0; i < size; i++)
4666   {
4667     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ... adding %s\n",
4668                 GNUNET_i2s (&pi[i]));
4669     path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4670     if (path->peers[i] == myid)
4671       own_pos = i;
4672   }
4673   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Own position: %u\n", own_pos);
4674   if (own_pos == 0)
4675   {
4676     /* cannot be self, must be 'not found' */
4677     /* create path: self not found in path through self */
4678     GNUNET_break_op (0);
4679     path_destroy (path);
4680     tunnel_destroy (t);
4681     return GNUNET_OK;
4682   }
4683   path_add_to_peers (path, GNUNET_NO);
4684   tunnel_add_path (t, path, own_pos);
4685   if (own_pos == size - 1)
4686   {
4687     /* It is for us! Send ack. */
4688     struct MeshTransmissionDescriptor *info;
4689
4690     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  It's for us!\n");
4691     peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4692     if (NULL == t->peers)
4693     {
4694       /* New tunnel! Notify clients on data. */
4695       t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4696     }
4697     GNUNET_break (GNUNET_SYSERR !=
4698                   GNUNET_CONTAINER_multihashmap_put (t->peers,
4699                                                      &my_full_id.hashPubKey,
4700                                                      peer_info_get
4701                                                      (&my_full_id),
4702                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4703     info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4704     info->origin = &t->id;
4705     info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4706     GNUNET_assert (NULL != info->peer);
4707     queue_add(info,
4708               GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4709               sizeof (struct GNUNET_MESH_PathACK),
4710               info->peer,
4711               t);
4712   }
4713   else
4714   {
4715     struct MeshPeerPath *path2;
4716
4717     /* It's for somebody else! Retransmit. */
4718     path2 = path_duplicate (path);
4719     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Retransmitting.\n");
4720     peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4721     path2 = path_duplicate (path);
4722     peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4723     send_create_path (dest_peer_info, path, t);
4724   }
4725   return GNUNET_OK;
4726 }
4727
4728
4729 /**
4730  * Core handler for path destruction
4731  *
4732  * @param cls closure
4733  * @param message message
4734  * @param peer peer identity this notification is about
4735  * @param atsi performance data
4736  * @param atsi_count number of records in 'atsi'
4737  *
4738  * @return GNUNET_OK to keep the connection open,
4739  *         GNUNET_SYSERR to close it (signal serious error)
4740  */
4741 static int
4742 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4743                           const struct GNUNET_MessageHeader *message,
4744                           const struct GNUNET_ATS_Information *atsi,
4745                           unsigned int atsi_count)
4746 {
4747   struct GNUNET_MESH_ManipulatePath *msg;
4748   struct GNUNET_PeerIdentity *pi;
4749   struct MeshPeerPath *path;
4750   struct MeshTunnel *t;
4751   unsigned int own_pos;
4752   unsigned int i;
4753   size_t size;
4754
4755   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4756               "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4757   size = ntohs (message->size);
4758   if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4759   {
4760     GNUNET_break_op (0);
4761     return GNUNET_OK;
4762   }
4763
4764   size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4765   if (size % sizeof (struct GNUNET_PeerIdentity))
4766   {
4767     GNUNET_break_op (0);
4768     return GNUNET_OK;
4769   }
4770   size /= sizeof (struct GNUNET_PeerIdentity);
4771   if (size < 2)
4772   {
4773     GNUNET_break_op (0);
4774     return GNUNET_OK;
4775   }
4776   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "    path has %u hops.\n", size);
4777
4778   msg = (struct GNUNET_MESH_ManipulatePath *) message;
4779   pi = (struct GNUNET_PeerIdentity *) &msg[1];
4780   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4781               "    path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4782               msg->tid);
4783   t = tunnel_get (pi, ntohl (msg->tid));
4784   if (NULL == t)
4785   {
4786     /* TODO notify back: we don't know this tunnel */
4787     GNUNET_break_op (0);
4788     return GNUNET_OK;
4789   }
4790   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Creating path...\n");
4791   path = path_new (size);
4792   own_pos = 0;
4793   for (i = 0; i < size; i++)
4794   {
4795     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ... adding %s\n",
4796                 GNUNET_i2s (&pi[i]));
4797     path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4798     if (path->peers[i] == myid)
4799       own_pos = i;
4800   }
4801   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Own position: %u\n", own_pos);
4802   if (own_pos < path->length - 1)
4803     send_message (message, &pi[own_pos + 1], t);
4804   else
4805     send_client_tunnel_disconnect(t, NULL);
4806
4807   tunnel_delete_peer (t, path->peers[path->length - 1]);
4808   path_destroy (path);
4809   return GNUNET_OK;
4810 }
4811
4812
4813 /**
4814  * Core handler for notifications of broken paths
4815  *
4816  * @param cls closure
4817  * @param message message
4818  * @param peer peer identity this notification is about
4819  * @param atsi performance data
4820  * @param atsi_count number of records in 'atsi'
4821  *
4822  * @return GNUNET_OK to keep the connection open,
4823  *         GNUNET_SYSERR to close it (signal serious error)
4824  */
4825 static int
4826 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4827                          const struct GNUNET_MessageHeader *message,
4828                          const struct GNUNET_ATS_Information *atsi,
4829                          unsigned int atsi_count)
4830 {
4831   struct GNUNET_MESH_PathBroken *msg;
4832   struct MeshTunnel *t;
4833
4834   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4835               "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4836   msg = (struct GNUNET_MESH_PathBroken *) message;
4837   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
4838               GNUNET_i2s (&msg->peer1));
4839   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
4840               GNUNET_i2s (&msg->peer2));
4841   t = tunnel_get (&msg->oid, ntohl (msg->tid));
4842   if (NULL == t)
4843   {
4844     GNUNET_break_op (0);
4845     return GNUNET_OK;
4846   }
4847   tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4848                                    GNUNET_PEER_search (&msg->peer2));
4849   return GNUNET_OK;
4850
4851 }
4852
4853
4854 /**
4855  * Core handler for tunnel destruction
4856  *
4857  * @param cls closure
4858  * @param message message
4859  * @param peer peer identity this notification is about
4860  * @param atsi performance data
4861  * @param atsi_count number of records in 'atsi'
4862  *
4863  * @return GNUNET_OK to keep the connection open,
4864  *         GNUNET_SYSERR to close it (signal serious error)
4865  */
4866 static int
4867 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4868                             const struct GNUNET_MessageHeader *message,
4869                             const struct GNUNET_ATS_Information *atsi,
4870                             unsigned int atsi_count)
4871 {
4872   struct GNUNET_MESH_TunnelDestroy *msg;
4873   struct MeshTunnel *t;
4874
4875   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4876               "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4877   msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4878   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for tunnel %s [%u]\n",
4879               GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4880   t = tunnel_get (&msg->oid, ntohl (msg->tid));
4881   if (NULL == t)
4882   {
4883     /* Probably already got the message from another path,
4884      * destroyed the tunnel and retransmitted to children.
4885      * Safe to ignore.
4886      */
4887     GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4888     return GNUNET_OK;
4889   }
4890   if (t->id.oid == myid)
4891   {
4892     GNUNET_break_op (0);
4893     return GNUNET_OK;
4894   }
4895   if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4896   {
4897     /* Tunnel was incoming, notify clients */
4898     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4899                 t->local_tid, t->local_tid_dest);
4900     send_clients_tunnel_destroy (t);
4901   }
4902   tunnel_send_destroy (t);
4903   t->destroy = GNUNET_YES;
4904   // TODO: add timeout to destroy the tunnel anyway
4905   return GNUNET_OK;
4906 }
4907
4908
4909 /**
4910  * Core handler for mesh network traffic going from the origin to a peer
4911  *
4912  * @param cls closure
4913  * @param peer peer identity this notification is about
4914  * @param message message
4915  * @param atsi performance data
4916  * @param atsi_count number of records in 'atsi'
4917  * @return GNUNET_OK to keep the connection open,
4918  *         GNUNET_SYSERR to close it (signal serious error)
4919  */
4920 static int
4921 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4922                           const struct GNUNET_MessageHeader *message,
4923                           const struct GNUNET_ATS_Information *atsi,
4924                           unsigned int atsi_count)
4925 {
4926   struct GNUNET_MESH_Unicast *msg;
4927   struct GNUNET_PeerIdentity *neighbor;
4928   struct MeshTunnelChildInfo *cinfo;
4929   struct MeshTunnel *t;
4930   GNUNET_PEER_Id dest_id;
4931   uint32_t pid;
4932   uint32_t ttl;
4933   size_t size;
4934
4935   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4936               GNUNET_i2s (peer));
4937   /* Check size */
4938   size = ntohs (message->size);
4939   if (size <
4940       sizeof (struct GNUNET_MESH_Unicast) +
4941       sizeof (struct GNUNET_MessageHeader))
4942   {
4943     GNUNET_break (0);
4944     return GNUNET_OK;
4945   }
4946   msg = (struct GNUNET_MESH_Unicast *) message;
4947   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4948               GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4949   /* Check tunnel */
4950   t = tunnel_get (&msg->oid, ntohl (msg->tid));
4951   if (NULL == t)
4952   {
4953     /* TODO notify back: we don't know this tunnel */
4954     GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4955     GNUNET_break_op (0);
4956     return GNUNET_OK;
4957   }
4958   pid = ntohl (msg->pid);
4959   if (t->fwd_pid == pid)
4960   {
4961     GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4962     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4963                 " Already seen pid %u, DROPPING!\n", pid);
4964     return GNUNET_OK;
4965   }
4966   else
4967   {
4968     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4969                 " pid %u not seen yet, forwarding\n", pid);
4970   }
4971   t->skip += (pid - t->fwd_pid) - 1;
4972   t->fwd_pid = pid;
4973   if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4974   {
4975     GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4976     GNUNET_break_op (0);
4977     return GNUNET_OK;
4978   }
4979   tunnel_reset_timeout (t);
4980   dest_id = GNUNET_PEER_search (&msg->destination);
4981   if (dest_id == myid)
4982   {
4983     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4984                 "  it's for us! sending to clients...\n");
4985     GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4986     send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4987     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4988     return GNUNET_OK;
4989   }
4990   ttl = ntohl (msg->ttl);
4991   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   ttl: %u\n", ttl);
4992   if (ttl == 0)
4993   {
4994     GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4995     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4996     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4997     return GNUNET_OK;
4998   }
4999   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5000               "  not for us, retransmitting...\n");
5001
5002   neighbor = tree_get_first_hop (t->tree, dest_id);
5003   cinfo = tunnel_get_neighbor_fc (t, neighbor);
5004   cinfo->pid = pid;
5005   GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5006                                          &tunnel_add_skip,
5007                                          &neighbor);
5008   if (GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5009   {
5010     GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5011     GNUNET_break_op (0);
5012     return GNUNET_OK;
5013   }
5014   send_message (message, neighbor, t);
5015   GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5016
5017   return GNUNET_OK;
5018 }
5019
5020
5021 /**
5022  * Core handler for mesh network traffic going from the origin to all peers
5023  *
5024  * @param cls closure
5025  * @param message message
5026  * @param peer peer identity this notification is about
5027  * @param atsi performance data
5028  * @param atsi_count number of records in 'atsi'
5029  * @return GNUNET_OK to keep the connection open,
5030  *         GNUNET_SYSERR to close it (signal serious error)
5031  *
5032  * TODO: Check who we got this from, to validate route.
5033  */
5034 static int
5035 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5036                             const struct GNUNET_MessageHeader *message,
5037                             const struct GNUNET_ATS_Information *atsi,
5038                             unsigned int atsi_count)
5039 {
5040   struct GNUNET_MESH_Multicast *msg;
5041   struct MeshTunnel *t;
5042   size_t size;
5043   uint32_t pid;
5044
5045   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5046               GNUNET_i2s (peer));
5047   size = ntohs (message->size);
5048   if (sizeof (struct GNUNET_MESH_Multicast) +
5049       sizeof (struct GNUNET_MessageHeader) > size)
5050   {
5051     GNUNET_break_op (0);
5052     return GNUNET_OK;
5053   }
5054   msg = (struct GNUNET_MESH_Multicast *) message;
5055   t = tunnel_get (&msg->oid, ntohl (msg->tid));
5056
5057   if (NULL == t)
5058   {
5059     /* TODO notify that we dont know that tunnel */
5060     GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5061     GNUNET_break_op (0);
5062     return GNUNET_OK;
5063   }
5064   pid = ntohl (msg->pid);
5065   if (t->fwd_pid == pid)
5066   {
5067     /* already seen this packet, drop */
5068     GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5069     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5070                 " Already seen pid %u, DROPPING!\n", pid);
5071     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5072     return GNUNET_OK;
5073   }
5074   else
5075   {
5076     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5077                 " pid %u not seen yet, forwarding\n", pid);
5078   }
5079   t->skip += (pid - t->fwd_pid) - 1;
5080   t->fwd_pid = pid;
5081   tunnel_reset_timeout (t);
5082
5083   /* Transmit to locally interested clients */
5084   if (NULL != t->peers &&
5085       GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5086   {
5087     GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5088     send_subscribed_clients (message, &msg[1].header);
5089     tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5090   }
5091   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   ttl: %u\n", ntohl (msg->ttl));
5092   if (ntohl (msg->ttl) == 0)
5093   {
5094     GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5095     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5096     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5097     return GNUNET_OK;
5098   }
5099   GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5100   tunnel_send_multicast (t, message, GNUNET_NO);
5101   return GNUNET_OK;
5102 }
5103
5104
5105 /**
5106  * Core handler for mesh network traffic toward the owner of a tunnel
5107  *
5108  * @param cls closure
5109  * @param message message
5110  * @param peer peer identity this notification is about
5111  * @param atsi performance data
5112  * @param atsi_count number of records in 'atsi'
5113  *
5114  * @return GNUNET_OK to keep the connection open,
5115  *         GNUNET_SYSERR to close it (signal serious error)
5116  */
5117 static int
5118 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5119                           const struct GNUNET_MessageHeader *message,
5120                           const struct GNUNET_ATS_Information *atsi,
5121                           unsigned int atsi_count)
5122 {
5123   struct GNUNET_MESH_ToOrigin *msg;
5124   struct GNUNET_PeerIdentity id;
5125   struct MeshPeerInfo *peer_info;
5126   struct MeshTunnel *t;
5127   size_t size;
5128
5129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5130               GNUNET_i2s (peer));
5131   size = ntohs (message->size);
5132   if (size < sizeof (struct GNUNET_MESH_ToOrigin) +     /* Payload must be */
5133       sizeof (struct GNUNET_MessageHeader))     /* at least a header */
5134   {
5135     GNUNET_break_op (0);
5136     return GNUNET_OK;
5137   }
5138   msg = (struct GNUNET_MESH_ToOrigin *) message;
5139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5140               GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5141   t = tunnel_get (&msg->oid, ntohl (msg->tid));
5142
5143   if (NULL == t)
5144   {
5145     /* TODO notify that we dont know this tunnel (whom)? */
5146     GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5147     GNUNET_break_op (0);
5148     return GNUNET_OK;
5149   }
5150
5151   if (NULL != t->owner)
5152   {
5153     char cbuf[size];
5154     struct GNUNET_MESH_ToOrigin *copy;
5155
5156     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5157                 "  it's for us! sending to clients...\n");
5158     /* TODO signature verification */
5159     memcpy (cbuf, message, size);
5160     copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5161     copy->tid = htonl (t->local_tid);
5162     GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5163     GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5164                                                 &copy->header, GNUNET_NO);
5165     tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5166     return GNUNET_OK;
5167   }
5168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5169               "  not for us, retransmitting...\n");
5170
5171   peer_info = peer_info_get (&msg->oid);
5172   if (NULL == peer_info)
5173   {
5174     /* unknown origin of tunnel */
5175     GNUNET_break (0);
5176     return GNUNET_OK;
5177   }
5178   GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5179   send_message (message, &id, t);
5180   GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5181
5182   return GNUNET_OK;
5183 }
5184
5185
5186 /**
5187  * Core handler for mesh network traffic point-to-point acks.
5188  *
5189  * @param cls closure
5190  * @param message message
5191  * @param peer peer identity this notification is about
5192  * @param atsi performance data
5193  * @param atsi_count number of records in 'atsi'
5194  *
5195  * @return GNUNET_OK to keep the connection open,
5196  *         GNUNET_SYSERR to close it (signal serious error)
5197  */
5198 static int
5199 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5200                  const struct GNUNET_MessageHeader *message,
5201                  const struct GNUNET_ATS_Information *atsi,
5202                  unsigned int atsi_count)
5203 {
5204   struct GNUNET_MESH_ACK *msg;
5205   struct MeshTunnel *t;
5206   uint32_t ack;
5207
5208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5209               GNUNET_i2s (peer));
5210   msg = (struct GNUNET_MESH_ACK *) message;
5211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5212               GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5213   t = tunnel_get (&msg->oid, ntohl (msg->tid));
5214
5215   if (NULL == t)
5216   {
5217     /* TODO notify that we dont know this tunnel (whom)? */
5218     GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5219     GNUNET_break_op (0);
5220     return GNUNET_OK;
5221   }
5222   ack = ntohl (msg->pid);
5223
5224   /* Is this a forward or backward ACK? */
5225   if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5226   {
5227     struct MeshTunnelChildInfo *cinfo;
5228
5229     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  FWD ACK\n");
5230     cinfo = tunnel_get_neighbor_fc (t, peer);
5231     cinfo->fwd_ack = ack;
5232     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5233   }
5234   else
5235   {
5236     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  BCK ACK\n");
5237     t->bck_ack = ack;
5238     tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5239   }
5240   // FIXME fc Unlock queues?
5241   return GNUNET_OK;
5242 }
5243
5244
5245 /**
5246  * Core handler for path ACKs
5247  *
5248  * @param cls closure
5249  * @param message message
5250  * @param peer peer identity this notification is about
5251  * @param atsi performance data
5252  * @param atsi_count number of records in 'atsi'
5253  *
5254  * @return GNUNET_OK to keep the connection open,
5255  *         GNUNET_SYSERR to close it (signal serious error)
5256  */
5257 static int
5258 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5259                       const struct GNUNET_MessageHeader *message,
5260                       const struct GNUNET_ATS_Information *atsi,
5261                       unsigned int atsi_count)
5262 {
5263   struct GNUNET_MESH_PathACK *msg;
5264   struct GNUNET_PeerIdentity id;
5265   struct MeshPeerInfo *peer_info;
5266   struct MeshPeerPath *p;
5267   struct MeshTunnel *t;
5268
5269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5270               GNUNET_i2s (&my_full_id));
5271   msg = (struct GNUNET_MESH_PathACK *) message;
5272   t = tunnel_get (&msg->oid, ntohl(msg->tid));
5273   if (NULL == t)
5274   {
5275     /* TODO notify that we don't know the tunnel */
5276     GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  don't know the tunnel %s [%X]!\n",
5278                 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5279     return GNUNET_OK;
5280   }
5281   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %s [%X]\n",
5282               GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5283
5284   peer_info = peer_info_get (&msg->peer_id);
5285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by peer %s\n",
5286               GNUNET_i2s (&msg->peer_id));
5287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  via peer %s\n",
5288               GNUNET_i2s (peer));
5289
5290   if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5291   {
5292     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5293                 "connect_by_string completed, stopping search\n");
5294     regex_cancel_search (t->regex_ctx);
5295     t->regex_ctx = NULL;
5296   }
5297
5298   /* Add paths to peers? */
5299   p = tree_get_path_to_peer (t->tree, peer_info->id);
5300   if (NULL != p)
5301   {
5302     path_add_to_peers (p, GNUNET_YES);
5303     path_destroy (p);
5304   }
5305   else
5306   {
5307     GNUNET_break (0);
5308   }
5309
5310   /* Message for us? */
5311   if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5312   {
5313     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  It's for us!\n");
5314     if (NULL == t->owner)
5315     {
5316       GNUNET_break_op (0);
5317       return GNUNET_OK;
5318     }
5319     if (NULL != t->dht_get_type)
5320     {
5321       GNUNET_DHT_get_stop (t->dht_get_type);
5322       t->dht_get_type = NULL;
5323     }
5324     if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5325     {
5326       tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5327       send_client_peer_connected (t, peer_info->id);
5328     }
5329     return GNUNET_OK;
5330   }
5331
5332   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5333               "  not for us, retransmitting...\n");
5334   GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5335   peer_info = peer_info_get (&msg->oid);
5336   if (NULL == peer_info)
5337   {
5338     /* If we know the tunnel, we should DEFINITELY know the peer */
5339     GNUNET_break (0);
5340     return GNUNET_OK;
5341   }
5342   send_message (message, &id, t);
5343   return GNUNET_OK;
5344 }
5345
5346
5347 /**
5348  * Functions to handle messages from core
5349  */
5350 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5351   {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5352   {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5353   {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5354    sizeof (struct GNUNET_MESH_PathBroken)},
5355   {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5356    sizeof (struct GNUNET_MESH_TunnelDestroy)},
5357   {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5358   {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5359   {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5360   {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5361     sizeof (struct GNUNET_MESH_ACK)},
5362   {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5363    sizeof (struct GNUNET_MESH_PathACK)},
5364   {NULL, 0, 0}
5365 };
5366
5367
5368
5369 /******************************************************************************/
5370 /****************       MESH LOCAL HANDLER HELPERS      ***********************/
5371 /******************************************************************************/
5372
5373 /**
5374  * deregister_app: iterator for removing each application registered by a client
5375  *
5376  * @param cls closure
5377  * @param key the hash of the application id (used to access the hashmap)
5378  * @param value the value stored at the key (client)
5379  *
5380  * @return GNUNET_OK on success
5381  */
5382 static int
5383 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5384 {
5385   struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5386   GNUNET_break (GNUNET_YES ==
5387                 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5388   return GNUNET_OK;
5389 }
5390
5391 #if LATER
5392 /**
5393  * notify_client_connection_failure: notify a client that the connection to the
5394  * requested remote peer is not possible (for instance, no route found)
5395  * Function called when the socket is ready to queue more data. "buf" will be
5396  * NULL and "size" zero if the socket was closed for writing in the meantime.
5397  *
5398  * @param cls closure
5399  * @param size number of bytes available in buf
5400  * @param buf where the callee should write the message
5401  * @return number of bytes written to buf
5402  */
5403 static size_t
5404 notify_client_connection_failure (void *cls, size_t size, void *buf)
5405 {
5406   int size_needed;
5407   struct MeshPeerInfo *peer_info;
5408   struct GNUNET_MESH_PeerControl *msg;
5409   struct GNUNET_PeerIdentity id;
5410
5411   if (0 == size && NULL == buf)
5412   {
5413     // TODO retry? cancel?
5414     return 0;
5415   }
5416
5417   size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5418   peer_info = (struct MeshPeerInfo *) cls;
5419   msg = (struct GNUNET_MESH_PeerControl *) buf;
5420   msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5421   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5422 //     msg->tunnel_id = htonl(peer_info->t->tid);
5423   GNUNET_PEER_resolve (peer_info->id, &id);
5424   memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5425
5426   return size_needed;
5427 }
5428 #endif
5429
5430
5431 /**
5432  * Send keepalive packets for a peer
5433  *
5434  * @param cls Closure (tunnel for which to send the keepalive).
5435  * @param tc Notification context.
5436  *
5437  * TODO: implement explicit multicast keepalive?
5438  */
5439 static void
5440 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5441 {
5442   struct MeshTunnel *t = cls;
5443   struct GNUNET_MessageHeader *payload;
5444   struct GNUNET_MESH_Multicast *msg;
5445   size_t size =
5446       sizeof (struct GNUNET_MESH_Multicast) +
5447       sizeof (struct GNUNET_MessageHeader);
5448   char cbuf[size];
5449
5450   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5451   {
5452     return;
5453   }
5454   t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5455
5456   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5457               "sending keepalive for tunnel %d\n", t->id.tid);
5458
5459   msg = (struct GNUNET_MESH_Multicast *) cbuf;
5460   msg->header.size = htons (size);
5461   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5462   msg->oid = my_full_id;
5463   msg->tid = htonl (t->id.tid);
5464   msg->ttl = htonl (default_ttl);
5465   msg->pid = htonl (t->fwd_pid + 1);
5466   t->fwd_pid++;
5467   payload = (struct GNUNET_MessageHeader *) &msg[1];
5468   payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5469   payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5470   tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5471
5472   t->path_refresh_task =
5473       GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5474   return;
5475 }
5476
5477
5478 /**
5479  * Function to process paths received for a new peer addition. The recorded
5480  * paths form the initial tunnel, which can be optimized later.
5481  * Called on each result obtained for the DHT search.
5482  *
5483  * @param cls closure
5484  * @param exp when will this value expire
5485  * @param key key of the result
5486  * @param get_path path of the get request
5487  * @param get_path_length lenght of get_path
5488  * @param put_path path of the put request
5489  * @param put_path_length length of the put_path
5490  * @param type type of the result
5491  * @param size number of bytes in data
5492  * @param data pointer to the result data
5493  *
5494  * TODO: re-issue the request after certain time? cancel after X results?
5495  */
5496 static void
5497 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5498                     const struct GNUNET_HashCode * key,
5499                     const struct GNUNET_PeerIdentity *get_path,
5500                     unsigned int get_path_length,
5501                     const struct GNUNET_PeerIdentity *put_path,
5502                     unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5503                     size_t size, const void *data)
5504 {
5505   struct MeshPathInfo *path_info = cls;
5506   struct MeshPeerPath *p;
5507   struct GNUNET_PeerIdentity pi;
5508   int i;
5509
5510   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5511   GNUNET_PEER_resolve (path_info->peer->id, &pi);
5512   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for %s\n", GNUNET_i2s (&pi));
5513
5514   p = path_build_from_dht (get_path, get_path_length, put_path,
5515                            put_path_length);
5516   path_add_to_peers (p, GNUNET_NO);
5517   path_destroy(p);
5518   for (i = 0; i < path_info->peer->ntunnels; i++)
5519   {
5520     tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5521     peer_info_connect (path_info->peer, path_info->t);
5522   }
5523
5524   return;
5525 }
5526
5527
5528 /**
5529  * Function to process paths received for a new peer addition. The recorded
5530  * paths form the initial tunnel, which can be optimized later.
5531  * Called on each result obtained for the DHT search.
5532  *
5533  * @param cls closure
5534  * @param exp when will this value expire
5535  * @param key key of the result
5536  * @param get_path path of the get request
5537  * @param get_path_length lenght of get_path
5538  * @param put_path path of the put request
5539  * @param put_path_length length of the put_path
5540  * @param type type of the result
5541  * @param size number of bytes in data
5542  * @param data pointer to the result data
5543  */
5544 static void
5545 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5546                       const struct GNUNET_HashCode * key,
5547                       const struct GNUNET_PeerIdentity *get_path,
5548                       unsigned int get_path_length,
5549                       const struct GNUNET_PeerIdentity *put_path,
5550                       unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5551                       size_t size, const void *data)
5552 {
5553   const struct PBlock *pb = data;
5554   const struct GNUNET_PeerIdentity *pi = &pb->id;
5555   struct MeshTunnel *t = cls;
5556   struct MeshPeerInfo *peer_info;
5557   struct MeshPeerPath *p;
5558
5559   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5560   if (size != sizeof (struct PBlock))
5561   {
5562     GNUNET_break_op (0);
5563     return;
5564   }
5565   if (ntohl(pb->type) != t->type)
5566   {
5567     GNUNET_break_op (0);
5568     return;
5569   }
5570   GNUNET_assert (NULL != t->owner);
5571   peer_info = peer_info_get (pi);
5572   (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5573                                             peer_info,
5574                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5575
5576   p = path_build_from_dht (get_path, get_path_length, put_path,
5577                            put_path_length);
5578   path_add_to_peers (p, GNUNET_NO);
5579   path_destroy(p);
5580   tunnel_add_peer (t, peer_info);
5581   peer_info_connect (peer_info, t);
5582 }
5583
5584
5585 /**
5586  * Function to process DHT string to regex matching.
5587  * Called on each result obtained for the DHT search.
5588  *
5589  * @param cls closure (search context)
5590  * @param exp when will this value expire
5591  * @param key key of the result
5592  * @param get_path path of the get request (not used)
5593  * @param get_path_length lenght of get_path (not used)
5594  * @param put_path path of the put request (not used)
5595  * @param put_path_length length of the put_path (not used)
5596  * @param type type of the result
5597  * @param size number of bytes in data
5598  * @param data pointer to the result data
5599  */
5600 static void
5601 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5602                                const struct GNUNET_HashCode * key,
5603                                const struct GNUNET_PeerIdentity *get_path,
5604                                unsigned int get_path_length,
5605                                const struct GNUNET_PeerIdentity *put_path,
5606                                unsigned int put_path_length,
5607                                enum GNUNET_BLOCK_Type type,
5608                                size_t size, const void *data)
5609 {
5610   const struct MeshRegexAccept *block = data;
5611   struct MeshRegexSearchContext *ctx = cls;
5612   struct MeshRegexSearchInfo *info = ctx->info;
5613   struct MeshPeerPath *p;
5614   struct MeshPeerInfo *peer_info;
5615
5616   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5617   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for %s\n", info->description);
5618
5619   peer_info = peer_info_get(&block->id);
5620   p = path_build_from_dht (get_path, get_path_length, put_path,
5621                            put_path_length);
5622   path_add_to_peers (p, GNUNET_NO);
5623   path_destroy(p);
5624
5625   tunnel_add_peer (info->t, peer_info);
5626   peer_info_connect (peer_info, info->t);
5627   if (0 == info->peer)
5628   {
5629     info->peer = peer_info->id;
5630   }
5631   else
5632   {
5633     GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5634   }
5635
5636   info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5637                                                 &regex_connect_timeout,
5638                                                 info);
5639
5640   return;
5641 }
5642
5643
5644 /**
5645  * Function to process DHT string to regex matching.
5646  * Called on each result obtained for the DHT search.
5647  *
5648  * @param cls closure (search context)
5649  * @param exp when will this value expire
5650  * @param key key of the result
5651  * @param get_path path of the get request (not used)
5652  * @param get_path_length lenght of get_path (not used)
5653  * @param put_path path of the put request (not used)
5654  * @param put_path_length length of the put_path (not used)
5655  * @param type type of the result
5656  * @param size number of bytes in data
5657  * @param data pointer to the result data
5658  *
5659  * TODO: re-issue the request after certain time? cancel after X results?
5660  */
5661 static void
5662 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5663                         const struct GNUNET_HashCode * key,
5664                         const struct GNUNET_PeerIdentity *get_path,
5665                         unsigned int get_path_length,
5666                         const struct GNUNET_PeerIdentity *put_path,
5667                         unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5668                         size_t size, const void *data)
5669 {
5670   const struct MeshRegexBlock *block = data;
5671   struct MeshRegexSearchContext *ctx = cls;
5672   struct MeshRegexSearchInfo *info = ctx->info;
5673   void *copy;
5674   size_t len;
5675
5676   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5677               "DHT GET STRING RETURNED RESULTS\n");
5678   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5679               "  key: %s\n", GNUNET_h2s (key));
5680
5681   copy = GNUNET_malloc (size);
5682   memcpy (copy, data, size);
5683   GNUNET_break (GNUNET_OK ==
5684                 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5685                                                   GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5686   len = ntohl (block->n_proof);
5687   {
5688     char proof[len + 1];
5689
5690     memcpy (proof, &block[1], len);
5691     proof[len] = '\0';
5692     if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5693     {
5694       GNUNET_break_op (0);
5695       return;
5696     }
5697   }
5698   len = strlen (info->description);
5699   if (len == ctx->position) // String processed
5700   {
5701     if (GNUNET_YES == ntohl (block->accepting))
5702     {
5703       regex_find_path(key, ctx);
5704     }
5705     else
5706     {
5707       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  block not accepting!\n");
5708       // FIXME REGEX this block not successful, wait for more? start timeout?
5709     }
5710     return;
5711   }
5712   GNUNET_break (GNUNET_OK ==
5713                 GNUNET_MESH_regex_block_iterate (block, size,
5714                                                  &regex_edge_iterator, ctx));
5715   return;
5716 }
5717
5718 /******************************************************************************/
5719 /*********************       MESH LOCAL HANDLES      **************************/
5720 /******************************************************************************/
5721
5722
5723 /**
5724  * Handler for client disconnection
5725  *
5726  * @param cls closure
5727  * @param client identification of the client; NULL
5728  *        for the last call when the server is destroyed
5729  */
5730 static void
5731 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5732 {
5733   struct MeshClient *c;
5734   struct MeshClient *next;
5735   unsigned int i;
5736
5737   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5738   if (client == NULL)
5739   {
5740     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (SERVER DOWN)\n");
5741     return;
5742   }
5743   c = clients;
5744   while (NULL != c)
5745   {
5746     if (c->handle != client)
5747     {
5748       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   ... searching\n");
5749       c = c->next;
5750       continue;
5751     }
5752     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5753                 c->id);
5754     GNUNET_SERVER_client_drop (c->handle);
5755     c->shutting_down = GNUNET_YES;
5756     GNUNET_assert (NULL != c->own_tunnels);
5757     GNUNET_assert (NULL != c->incoming_tunnels);
5758     GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5759                                            &tunnel_destroy_iterator, c);
5760     GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5761                                            &tunnel_destroy_iterator, c);
5762     GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5763                                            &tunnel_destroy_iterator, c);
5764     GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5765     GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5766     GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5767
5768     /* deregister clients applications */
5769     if (NULL != c->apps)
5770     {
5771       GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5772       GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5773     }
5774     if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5775         GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5776     {
5777       GNUNET_SCHEDULER_cancel (announce_applications_task);
5778       announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5779     }
5780     if (NULL != c->types)
5781       GNUNET_CONTAINER_multihashmap_destroy (c->types);
5782     for (i = 0; i < c->n_regex; i++)
5783     {
5784       GNUNET_free (c->regexes[i]);
5785     }
5786     GNUNET_free_non_null (c->regexes);
5787     if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5788       GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5789     next = c->next;
5790     GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5791     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  CLIENT FREE at %p\n", c);
5792     GNUNET_free (c);
5793     GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5794     c = next;
5795   }
5796   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   done!\n");
5797   return;
5798 }
5799
5800
5801 /**
5802  * Handler for new clients
5803  *
5804  * @param cls closure
5805  * @param client identification of the client
5806  * @param message the actual message, which includes messages the client wants
5807  */
5808 static void
5809 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5810                          const struct GNUNET_MessageHeader *message)
5811 {
5812   struct GNUNET_MESH_ClientConnect *cc_msg;
5813   struct MeshClient *c;
5814   GNUNET_MESH_ApplicationType *a;
5815   unsigned int size;
5816   uint16_t ntypes;
5817   uint16_t *t;
5818   uint16_t napps;
5819   uint16_t i;
5820
5821   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5822   /* Check data sanity */
5823   size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5824   cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5825   ntypes = ntohs (cc_msg->types);
5826   napps = ntohs (cc_msg->applications);
5827   if (size !=
5828       ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5829   {
5830     GNUNET_break (0);
5831     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5832     return;
5833   }
5834
5835   /* Create new client structure */
5836   c = GNUNET_malloc (sizeof (struct MeshClient));
5837   c->id = next_client_id++;
5838   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  CLIENT NEW %u\n", c->id);
5839   c->handle = client;
5840   GNUNET_SERVER_client_keep (client);
5841   a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5842   if (napps > 0)
5843   {
5844     GNUNET_MESH_ApplicationType at;
5845     struct GNUNET_HashCode hc;
5846
5847     c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5848     for (i = 0; i < napps; i++)
5849     {
5850       at = ntohl (a[i]);
5851       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  app type: %u\n", at);
5852       GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5853       /* store in clients hashmap */
5854       GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5855                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5856       /* store in global hashmap, for announcements */
5857       GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5858                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5859     }
5860     if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5861       announce_applications_task =
5862           GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5863
5864   }
5865   if (ntypes > 0)
5866   {
5867     uint16_t u16;
5868     struct GNUNET_HashCode hc;
5869
5870     t = (uint16_t *) & a[napps];
5871     c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5872     for (i = 0; i < ntypes; i++)
5873     {
5874       u16 = ntohs (t[i]);
5875       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  msg type: %u\n", u16);
5876       GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5877
5878       /* store in clients hashmap */
5879       GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5880                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5881       /* store in global hashmap */
5882       GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5883                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5884     }
5885   }
5886   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5887               " client has %u+%u subscriptions\n", napps, ntypes);
5888
5889   GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5890   c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5891   c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5892   c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5893   GNUNET_SERVER_notification_context_add (nc, client);
5894   GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5895
5896   GNUNET_SERVER_receive_done (client, GNUNET_OK);
5897   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5898 }
5899
5900
5901 /**
5902  * Handler for clients announcing available services by a regular expression.
5903  *
5904  * @param cls closure
5905  * @param client identification of the client
5906  * @param message the actual message, which includes messages the client wants
5907  */
5908 static void
5909 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5910                              const struct GNUNET_MessageHeader *message)
5911 {
5912   struct MeshClient *c;
5913   char *regex;
5914   size_t len;
5915
5916   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5917
5918   /* Sanity check for client registration */
5919   if (NULL == (c = client_get (client)))
5920   {
5921     GNUNET_break (0);
5922     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5923     return;
5924   }
5925   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
5926
5927   len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5928   regex = GNUNET_malloc (len + 1);
5929   memcpy (regex, &message[1], len);
5930   regex[len] = '\0';
5931   GNUNET_array_append (c->regexes, c->n_regex, regex);
5932   if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5933   {
5934     c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5935   }
5936   else
5937   {
5938     regex_put(regex);
5939   }
5940   GNUNET_SERVER_receive_done (client, GNUNET_OK);
5941   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5942 }
5943
5944
5945 /**
5946  * Handler for requests of new tunnels
5947  *
5948  * @param cls closure
5949  * @param client identification of the client
5950  * @param message the actual message
5951  */
5952 static void
5953 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5954                             const struct GNUNET_MessageHeader *message)
5955 {
5956   struct GNUNET_MESH_TunnelMessage *t_msg;
5957   struct MeshTunnel *t;
5958   struct MeshClient *c;
5959   MESH_TunnelNumber tid;
5960
5961   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5962
5963   /* Sanity check for client registration */
5964   if (NULL == (c = client_get (client)))
5965   {
5966     GNUNET_break (0);
5967     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5968     return;
5969   }
5970   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
5971
5972   /* Message sanity check */
5973   if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5974   {
5975     GNUNET_break (0);
5976     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5977     return;
5978   }
5979
5980   t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5981   /* Sanity check for tunnel numbering */
5982   tid = ntohl (t_msg->tunnel_id);
5983   if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5984   {
5985     GNUNET_break (0);
5986     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5987     return;
5988   }
5989   /* Sanity check for duplicate tunnel IDs */
5990   if (NULL != tunnel_get_by_local_id (c, tid))
5991   {
5992     GNUNET_break (0);
5993     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5994     return;
5995   }
5996
5997   while (NULL != tunnel_get_by_pi (myid, next_tid))
5998     next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5999   t = tunnel_new (myid, next_tid++, c, tid);
6000   if (NULL == t)
6001   {
6002     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6003     GNUNET_break (0);
6004     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6005     return;
6006   }
6007   next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6008   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6009               GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6010   t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6011
6012   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6013   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6014   return;
6015 }
6016
6017
6018 /**
6019  * Handler for requests of deleting tunnels
6020  *
6021  * @param cls closure
6022  * @param client identification of the client
6023  * @param message the actual message
6024  */
6025 static void
6026 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6027                              const struct GNUNET_MessageHeader *message)
6028 {
6029   struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6030   struct MeshClient *c;
6031   struct MeshTunnel *t;
6032   MESH_TunnelNumber tid;
6033
6034   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6035               "Got a DESTROY TUNNEL from client!\n");
6036
6037   /* Sanity check for client registration */
6038   if (NULL == (c = client_get (client)))
6039   {
6040     GNUNET_break (0);
6041     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6042     return;
6043   }
6044   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6045
6046   /* Message sanity check */
6047   if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6048   {
6049     GNUNET_break (0);
6050     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6051     return;
6052   }
6053
6054   tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6055
6056   /* Retrieve tunnel */
6057   tid = ntohl (tunnel_msg->tunnel_id);
6058   t = tunnel_get_by_local_id(c, tid);
6059   if (NULL == t)
6060   {
6061     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  tunnel %X not found\n", tid);
6062     GNUNET_break (0);
6063     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6064     return;
6065   }
6066   if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6067   {
6068     client_ignore_tunnel (c, t);
6069 #if 0
6070     // TODO: when to destroy incoming tunnel?
6071     if (t->nclients == 0)
6072     {
6073       GNUNET_assert (GNUNET_YES ==
6074                      GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6075                                                            &hash, t));
6076       GNUNET_assert (GNUNET_YES ==
6077                      GNUNET_CONTAINER_multihashmap_remove (t->peers,
6078                                                            &my_full_id.hashPubKey,
6079                                                            t));
6080     }
6081 #endif
6082     GNUNET_SERVER_receive_done (client, GNUNET_OK);
6083     return;
6084   }
6085   send_client_tunnel_disconnect(t, c);
6086   client_delete_tunnel(c, t);
6087
6088   /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6089   t->owner = NULL;
6090   tunnel_send_destroy (t);
6091   t->destroy = GNUNET_YES;
6092   // The tunnel will be destroyed when the last message is transmitted.
6093   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6094   return;
6095 }
6096
6097
6098 /**
6099  * Handler for requests of seeting tunnel's speed.
6100  *
6101  * @param cls Closure (unused).
6102  * @param client Identification of the client.
6103  * @param message The actual message.
6104  */
6105 static void
6106 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6107                            const struct GNUNET_MessageHeader *message)
6108 {
6109   struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6110   struct MeshClient *c;
6111   struct MeshTunnel *t;
6112   MESH_TunnelNumber tid;
6113
6114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6115               "Got a SPEED request from client!\n");
6116
6117   /* Sanity check for client registration */
6118   if (NULL == (c = client_get (client)))
6119   {
6120     GNUNET_break (0);
6121     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6122     return;
6123   }
6124
6125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6126
6127   tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6128
6129   /* Retrieve tunnel */
6130   tid = ntohl (tunnel_msg->tunnel_id);
6131   t = tunnel_get_by_local_id(c, tid);
6132   if (NULL == t)
6133   {
6134     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "  tunnel %X not found\n", tid);
6135     GNUNET_break (0);
6136     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6137     return;
6138   }
6139
6140   switch (ntohs(message->type))
6141   {
6142       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6143           t->speed_min = GNUNET_YES;
6144           break;
6145       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6146           t->speed_min = GNUNET_NO;
6147           break;
6148       default:
6149           GNUNET_break (0);
6150   }
6151   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6152 }
6153
6154
6155 /**
6156  * Handler for requests of seeting tunnel's buffering policy.
6157  *
6158  * @param cls Closure (unused).
6159  * @param client Identification of the client.
6160  * @param message The actual message.
6161  */
6162 static void
6163 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6164                             const struct GNUNET_MessageHeader *message)
6165 {
6166   struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6167   struct MeshClient *c;
6168   struct MeshTunnel *t;
6169   MESH_TunnelNumber tid;
6170
6171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6172               "Got a BUFFER request from client!\n");
6173
6174   /* Sanity check for client registration */
6175   if (NULL == (c = client_get (client)))
6176   {
6177     GNUNET_break (0);
6178     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6179     return;
6180   }
6181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6182
6183   tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6184
6185   /* Retrieve tunnel */
6186   tid = ntohl (tunnel_msg->tunnel_id);
6187   t = tunnel_get_by_local_id(c, tid);
6188   if (NULL == t)
6189   {
6190     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  tunnel %X not found\n", tid);
6191     GNUNET_break (0);
6192     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6193     return;
6194   }
6195
6196   switch (ntohs(message->type))
6197   {
6198       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6199           t->nobuffer = GNUNET_NO;
6200           break;
6201       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6202           t->nobuffer = GNUNET_YES;
6203           break;
6204       default:
6205           GNUNET_break (0);
6206   }
6207
6208   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6209 }
6210
6211
6212 /**
6213  * Handler for connection requests to new peers
6214  *
6215  * @param cls closure
6216  * @param client identification of the client
6217  * @param message the actual message (PeerControl)
6218  */
6219 static void
6220 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6221                           const struct GNUNET_MessageHeader *message)
6222 {
6223   struct GNUNET_MESH_PeerControl *peer_msg;
6224   struct MeshPeerInfo *peer_info;
6225   struct MeshClient *c;
6226   struct MeshTunnel *t;
6227   MESH_TunnelNumber tid;
6228
6229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6230   /* Sanity check for client registration */
6231   if (NULL == (c = client_get (client)))
6232   {
6233     GNUNET_break (0);
6234     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6235     return;
6236   }
6237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6238
6239   peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6240
6241   /* Sanity check for message size */
6242   if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6243   {
6244     GNUNET_break (0);
6245     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6246     return;
6247   }
6248
6249   /* Tunnel exists? */
6250   tid = ntohl (peer_msg->tunnel_id);
6251   t = tunnel_get_by_local_id (c, tid);
6252   if (NULL == t)
6253   {
6254     GNUNET_break (0);
6255     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6256     return;
6257   }
6258
6259   /* Does client own tunnel? */
6260   if (t->owner->handle != client)
6261   {
6262     GNUNET_break (0);
6263     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6264     return;
6265   }
6266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "     for %s\n",
6267               GNUNET_i2s (&peer_msg->peer));
6268   peer_info = peer_info_get (&peer_msg->peer);
6269
6270   tunnel_add_peer (t, peer_info);
6271   peer_info_connect (peer_info, t);
6272
6273   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6274   return;
6275 }
6276
6277
6278 /**
6279  * Handler for disconnection requests of peers in a tunnel
6280  *
6281  * @param cls closure
6282  * @param client identification of the client
6283  * @param message the actual message (PeerControl)
6284  */
6285 static void
6286 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6287                           const struct GNUNET_MessageHeader *message)
6288 {
6289   struct GNUNET_MESH_PeerControl *peer_msg;
6290   struct MeshPeerInfo *peer_info;
6291   struct MeshClient *c;
6292   struct MeshTunnel *t;
6293   MESH_TunnelNumber tid;
6294
6295   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6296   /* Sanity check for client registration */
6297   if (NULL == (c = client_get (client)))
6298   {
6299     GNUNET_break (0);
6300     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6301     return;
6302   }
6303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6304
6305   peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6306
6307   /* Sanity check for message size */
6308   if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6309   {
6310     GNUNET_break (0);
6311     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6312     return;
6313   }
6314
6315   /* Tunnel exists? */
6316   tid = ntohl (peer_msg->tunnel_id);
6317   t = tunnel_get_by_local_id (c, tid);
6318   if (NULL == t)
6319   {
6320     GNUNET_break (0);
6321     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6322     return;
6323   }
6324   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X\n", t->id.tid);
6325
6326   /* Does client own tunnel? */
6327   if (t->owner->handle != client)
6328   {
6329     GNUNET_break (0);
6330     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6331     return;
6332   }
6333
6334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for peer %s\n",
6335               GNUNET_i2s (&peer_msg->peer));
6336   /* Is the peer in the tunnel? */
6337   peer_info =
6338       GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6339   if (NULL == peer_info)
6340   {
6341     GNUNET_break (0);
6342     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6343     return;
6344   }
6345
6346   /* Ok, delete peer from tunnel */
6347   GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6348                                             &peer_msg->peer.hashPubKey);
6349
6350   send_destroy_path (t, peer_info->id);
6351   tunnel_delete_peer (t, peer_info->id);
6352   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6353   return;
6354 }
6355
6356 /**
6357  * Handler for blacklist requests of peers in a tunnel
6358  *
6359  * @param cls closure
6360  * @param client identification of the client
6361  * @param message the actual message (PeerControl)
6362  * 
6363  * FIXME implement DHT block bloomfilter
6364  */
6365 static void
6366 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6367                           const struct GNUNET_MessageHeader *message)
6368 {
6369   struct GNUNET_MESH_PeerControl *peer_msg;
6370   struct MeshClient *c;
6371   struct MeshTunnel *t;
6372   MESH_TunnelNumber tid;
6373
6374   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6375   /* Sanity check for client registration */
6376   if (NULL == (c = client_get (client)))
6377   {
6378     GNUNET_break (0);
6379     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6380     return;
6381   }
6382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6383
6384   peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6385
6386   /* Sanity check for message size */
6387   if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6388   {
6389     GNUNET_break (0);
6390     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6391     return;
6392   }
6393
6394   /* Tunnel exists? */
6395   tid = ntohl (peer_msg->tunnel_id);
6396   t = tunnel_get_by_local_id (c, tid);
6397   if (NULL == t)
6398   {
6399     GNUNET_break (0);
6400     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6401     return;
6402   }
6403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X\n", t->id.tid);
6404
6405   GNUNET_array_append(t->blacklisted, t->nblacklisted,
6406                       GNUNET_PEER_intern(&peer_msg->peer));
6407 }
6408
6409
6410 /**
6411  * Handler for unblacklist requests of peers in a tunnel
6412  *
6413  * @param cls closure
6414  * @param client identification of the client
6415  * @param message the actual message (PeerControl)
6416  */
6417 static void
6418 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6419                           const struct GNUNET_MessageHeader *message)
6420 {
6421   struct GNUNET_MESH_PeerControl *peer_msg;
6422   struct MeshClient *c;
6423   struct MeshTunnel *t;
6424   MESH_TunnelNumber tid;
6425   GNUNET_PEER_Id pid;
6426   unsigned int i;
6427
6428   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6429   /* Sanity check for client registration */
6430   if (NULL == (c = client_get (client)))
6431   {
6432     GNUNET_break (0);
6433     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6434     return;
6435   }
6436   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6437
6438   peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6439
6440   /* Sanity check for message size */
6441   if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6442   {
6443     GNUNET_break (0);
6444     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6445     return;
6446   }
6447
6448   /* Tunnel exists? */
6449   tid = ntohl (peer_msg->tunnel_id);
6450   t = tunnel_get_by_local_id (c, tid);
6451   if (NULL == t)
6452   {
6453     GNUNET_break (0);
6454     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6455     return;
6456   }
6457   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X\n", t->id.tid);
6458
6459   /* if peer is not known, complain */
6460   pid = GNUNET_PEER_search (&peer_msg->peer);
6461   if (0 == pid)
6462   {
6463     GNUNET_break (0);
6464     return;
6465   }
6466
6467   /* search and remove from list */
6468   for (i = 0; i < t->nblacklisted; i++)
6469   {
6470     if (t->blacklisted[i] == pid)
6471     {
6472       t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6473       GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6474       return;
6475     }
6476   }
6477
6478   /* if peer hasn't been blacklisted, complain */
6479   GNUNET_break (0);
6480 }
6481
6482
6483 /**
6484  * Handler for connection requests to new peers by type
6485  *
6486  * @param cls closure
6487  * @param client identification of the client
6488  * @param message the actual message (ConnectPeerByType)
6489  */
6490 static void
6491 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6492                               const struct GNUNET_MessageHeader *message)
6493 {
6494   struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6495   struct MeshClient *c;
6496   struct MeshTunnel *t;
6497   struct GNUNET_HashCode hash;
6498   MESH_TunnelNumber tid;
6499
6500   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6501   /* Sanity check for client registration */
6502   if (NULL == (c = client_get (client)))
6503   {
6504     GNUNET_break (0);
6505     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6506     return;
6507   }
6508   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6509
6510   connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6511
6512   /* Sanity check for message size */
6513   if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6514       ntohs (connect_msg->header.size))
6515   {
6516     GNUNET_break (0);
6517     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6518     return;
6519   }
6520
6521   /* Tunnel exists? */
6522   tid = ntohl (connect_msg->tunnel_id);
6523   t = tunnel_get_by_local_id (c, tid);
6524   if (NULL == t)
6525   {
6526     GNUNET_break (0);
6527     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6528     return;
6529   }
6530
6531   /* Does client own tunnel? */
6532   if (t->owner->handle != client)
6533   {
6534     GNUNET_break (0);
6535     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6536     return;
6537   }
6538
6539   /* Do WE have the service? */
6540   t->type = ntohl (connect_msg->type);
6541   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6542   GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6543   if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6544       GNUNET_YES)
6545   {
6546     /* Yes! Fast forward, add ourselves to the tunnel and send the
6547      * good news to the client, and alert the destination client of
6548      * an incoming tunnel.
6549      *
6550      * FIXME send a path create to self, avoid code duplication
6551      */
6552     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6553     GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6554                                        peer_info_get (&my_full_id),
6555                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6556
6557     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6558     send_client_peer_connected (t, myid);
6559     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6560     GNUNET_SERVER_receive_done (client, GNUNET_OK);
6561
6562     t->local_tid_dest = next_local_tid++;
6563     GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6564     GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6565                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6566
6567     return;
6568   }
6569   /* Ok, lets find a peer offering the service */
6570   if (NULL != t->dht_get_type)
6571   {
6572     GNUNET_DHT_get_stop (t->dht_get_type);
6573   }
6574   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6575               GNUNET_h2s (&hash));
6576   t->dht_get_type =
6577       GNUNET_DHT_get_start (dht_handle, 
6578                             GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6579                             &hash,
6580                             dht_replication_level,
6581                             GNUNET_DHT_RO_RECORD_ROUTE |
6582                             GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6583                             NULL, 0,
6584                             &dht_get_type_handler, t);
6585
6586   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6587   return;
6588 }
6589
6590
6591 /**
6592  * Handler for connection requests to new peers by a string service description.
6593  *
6594  * @param cls closure
6595  * @param client identification of the client
6596  * @param message the actual message, which includes messages the client wants
6597  */
6598 static void
6599 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6600                                 const struct GNUNET_MessageHeader *message)
6601 {
6602   struct GNUNET_MESH_ConnectPeerByString *msg;
6603   struct MeshRegexSearchContext *ctx;
6604   struct MeshRegexSearchInfo *info;
6605   struct GNUNET_DHT_GetHandle *get_h;
6606   struct GNUNET_HashCode key;
6607   struct MeshTunnel *t;
6608   struct MeshClient *c;
6609   MESH_TunnelNumber tid;
6610   const char *string;
6611   size_t size;
6612   size_t len;
6613   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6614               "Connect by string started\n");
6615   msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6616   size = htons (message->size);
6617
6618   /* Sanity check for client registration */
6619   if (NULL == (c = client_get (client)))
6620   {
6621     GNUNET_break (0);
6622     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6623     return;
6624   }
6625   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6626
6627   /* Message size sanity check */
6628   if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6629   {
6630     GNUNET_break (0);
6631     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6632     return;
6633   }
6634
6635   /* Tunnel exists? */
6636   tid = ntohl (msg->tunnel_id);
6637   t = tunnel_get_by_local_id (c, tid);
6638   if (NULL == t)
6639   {
6640     GNUNET_break (0);
6641     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6642     return;
6643   }
6644
6645   /* Does client own tunnel? */
6646   if (t->owner->handle != client)
6647   {
6648     GNUNET_break (0);
6649     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6650     return;
6651   }
6652
6653   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6654               "  on tunnel %s [%u]\n",
6655               GNUNET_i2s(&my_full_id),
6656               t->id.tid);
6657
6658   /* Only one connect_by_string allowed at the same time! */
6659   /* FIXME: allow more, return handle at api level to cancel, document */
6660   if (NULL != t->regex_ctx)
6661   {
6662     GNUNET_break (0);
6663     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6664     return;
6665   }
6666
6667   /* Find string itself */
6668   len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6669   string = (const char *) &msg[1];
6670
6671   /* Initialize context */
6672   size = GNUNET_REGEX_get_first_key(string, len, &key);
6673   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6674               "  consumed %u bits out of %u\n", size, len);
6675   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6676               "  looking for %s\n", GNUNET_h2s (&key));
6677
6678   info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6679   info->t = t;
6680   info->description = GNUNET_malloc (len + 1);
6681   memcpy (info->description, string, len);
6682   info->description[len] = '\0';
6683   info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6684   info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6685   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   string: %s\n", info->description);
6686
6687   ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6688   ctx->position = size;
6689   ctx->info = info;
6690   t->regex_ctx = ctx;
6691
6692   GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6693
6694   /* Start search in DHT */
6695   get_h = GNUNET_DHT_get_start (dht_handle,    /* handle */
6696                                 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6697                                 &key,     /* key to search */
6698                                 dht_replication_level, /* replication level */
6699                                 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6700                                 NULL,       /* xquery */ // FIXME BLOOMFILTER
6701                                 0,     /* xquery bits */ // FIXME BLOOMFILTER SIZE
6702                                 &dht_get_string_handler, ctx);
6703
6704   GNUNET_break (GNUNET_OK ==
6705                 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6706                                                   &key,
6707                                                   get_h,
6708                                                   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6709
6710   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6711   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6712 }
6713
6714
6715 /**
6716  * Handler for client traffic directed to one peer
6717  *
6718  * @param cls closure
6719  * @param client identification of the client
6720  * @param message the actual message
6721  */
6722 static void
6723 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6724                       const struct GNUNET_MessageHeader *message)
6725 {
6726   struct MeshClient *c;
6727   struct MeshTunnel *t;
6728   struct MeshPeerInfo *pi;
6729   struct GNUNET_MESH_Unicast *data_msg;
6730   MESH_TunnelNumber tid;
6731   size_t size;
6732
6733   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6734               "Got a unicast request from a client!\n");
6735
6736   /* Sanity check for client registration */
6737   if (NULL == (c = client_get (client)))
6738   {
6739     GNUNET_break (0);
6740     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6741     return;
6742   }
6743   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6744
6745   data_msg = (struct GNUNET_MESH_Unicast *) message;
6746
6747   /* Sanity check for message size */
6748   size = ntohs (message->size);
6749   if (sizeof (struct GNUNET_MESH_Unicast) +
6750       sizeof (struct GNUNET_MessageHeader) > size)
6751   {
6752     GNUNET_break (0);
6753     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6754     return;
6755   }
6756
6757   /* Tunnel exists? */
6758   tid = ntohl (data_msg->tid);
6759   t = tunnel_get_by_local_id (c, tid);
6760   if (NULL == t)
6761   {
6762     GNUNET_break (0);
6763     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6764     return;
6765   }
6766
6767   /*  Is it a local tunnel? Then, does client own the tunnel? */
6768   if (t->owner->handle != client)
6769   {
6770     GNUNET_break (0);
6771     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6772     return;
6773   }
6774
6775   pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6776                                           &data_msg->destination.hashPubKey);
6777   /* Is the selected peer in the tunnel? */
6778   if (NULL == pi)
6779   {
6780     GNUNET_break (0);
6781     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6782     return;
6783   }
6784
6785   /* PID should be as expected */
6786   if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6787   {
6788     GNUNET_break (0);
6789     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6790               "Unicast PID, expected %u, got %u\n",
6791               t->fwd_pid + 1, ntohl (data_msg->pid));
6792     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6793     return;
6794   }
6795
6796   /* Ok, everything is correct, send the message
6797    * (pretend we got it from a mesh peer)
6798    */
6799   {
6800     /* Work around const limitation */
6801     char buf[ntohs (message->size)] GNUNET_ALIGN;
6802     struct GNUNET_MESH_Unicast *copy;
6803
6804     copy = (struct GNUNET_MESH_Unicast *) buf;
6805     memcpy (buf, data_msg, size);
6806     copy->oid = my_full_id;
6807     copy->tid = htonl (t->id.tid);
6808     copy->ttl = htonl (default_ttl);
6809     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6810                 "  calling generic handler...\n");
6811     handle_mesh_data_unicast (NULL, &my_full_id, &copy->header, NULL, 0);
6812   }
6813   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6814   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6815   return;
6816 }
6817
6818
6819 /**
6820  * Handler for client traffic directed to the origin
6821  *
6822  * @param cls closure
6823  * @param client identification of the client
6824  * @param message the actual message
6825  */
6826 static void
6827 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6828                         const struct GNUNET_MessageHeader *message)
6829 {
6830   struct GNUNET_MESH_ToOrigin *data_msg;
6831   struct MeshTunnelClientInfo *clinfo;
6832   struct MeshClient *c;
6833   struct MeshTunnel *t;
6834   MESH_TunnelNumber tid;
6835   size_t size;
6836
6837   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6838               "Got a ToOrigin request from a client!\n");
6839   /* Sanity check for client registration */
6840   if (NULL == (c = client_get (client)))
6841   {
6842     GNUNET_break (0);
6843     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6844     return;
6845   }
6846   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6847
6848   data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6849
6850   /* Sanity check for message size */
6851   size = ntohs (message->size);
6852   if (sizeof (struct GNUNET_MESH_ToOrigin) +
6853       sizeof (struct GNUNET_MessageHeader) > size)
6854   {
6855     GNUNET_break (0);
6856     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6857     return;
6858   }
6859
6860   /* Tunnel exists? */
6861   tid = ntohl (data_msg->tid);
6862   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X\n", tid);
6863   if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6864   {
6865     GNUNET_break (0);
6866     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6867     return;
6868   }
6869   t = tunnel_get_by_local_id (c, tid);
6870   if (NULL == t)
6871   {
6872     GNUNET_break (0);
6873     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6874     return;
6875   }
6876
6877   /*  It should be sent by someone who has this as incoming tunnel. */
6878   if (GNUNET_NO == client_knows_tunnel (c, t))
6879   {
6880     GNUNET_break (0);
6881     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6882     return;
6883   }
6884
6885   /* PID should be as expected */
6886   clinfo = tunnel_get_client_fc (t, c);
6887   if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6888   {
6889     GNUNET_break (0);
6890     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6891                 "To Origin PID, expected %u, got %u\n",
6892                 clinfo->bck_pid + 1, ntohl (data_msg->pid));
6893     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6894     return;
6895   }
6896
6897   /* Ok, everything is correct, send the message
6898    * (pretend we got it from a mesh peer)
6899    */
6900   {
6901     char buf[ntohs (message->size)] GNUNET_ALIGN;
6902     struct GNUNET_MESH_ToOrigin *copy;
6903
6904     /* Work around const limitation */
6905     copy = (struct GNUNET_MESH_ToOrigin *) buf;
6906     memcpy (buf, data_msg, size);
6907     GNUNET_PEER_resolve (t->id.oid, &copy->oid);
6908     copy->tid = htonl (t->id.tid);
6909     copy->ttl = htonl (default_ttl);
6910     GNUNET_assert (ntohl (copy->pid) == (t->bck_pid + 1));
6911     copy->sender = my_full_id;
6912     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6913                 "  calling generic handler...\n");
6914     handle_mesh_data_to_orig (NULL, &my_full_id, &copy->header, NULL, 0);
6915   }
6916   GNUNET_SERVER_receive_done (client, GNUNET_OK);
6917   return;
6918 }
6919
6920
6921 /**
6922  * Handler for client traffic directed to all peers in a tunnel
6923  *
6924  * @param cls closure
6925  * @param client identification of the client
6926  * @param message the actual message
6927  */
6928 static void
6929 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6930                         const struct GNUNET_MessageHeader *message)
6931 {
6932   struct MeshClient *c;
6933   struct MeshTunnel *t;
6934   struct GNUNET_MESH_Multicast *data_msg;
6935   MESH_TunnelNumber tid;
6936
6937   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6938               "Got a multicast request from a client!\n");
6939
6940   /* Sanity check for client registration */
6941   if (NULL == (c = client_get (client)))
6942   {
6943     GNUNET_break (0);
6944     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6945     return;
6946   }
6947   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
6948
6949   data_msg = (struct GNUNET_MESH_Multicast *) message;
6950
6951   /* Sanity check for message size */
6952   if (sizeof (struct GNUNET_MESH_Multicast) +
6953       sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6954   {
6955     GNUNET_break (0);
6956     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6957     return;
6958   }
6959
6960   /* Tunnel exists? */
6961   tid = ntohl (data_msg->tid);
6962   t = tunnel_get_by_local_id (c, tid);
6963   if (NULL == t)
6964   {
6965     GNUNET_break (0);
6966     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6967     return;
6968   }
6969
6970   /* Does client own tunnel? */
6971   if (t->owner->handle != client)
6972   {
6973     GNUNET_break (0);
6974     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6975     return;
6976   }
6977
6978   /* PID should be as expected */
6979   if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6980   {
6981     GNUNET_break (0);
6982     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6983               "Multicast PID, expected %u, got %u\n",
6984               t->fwd_pid + 1, ntohl (data_msg->pid));
6985     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6986     return;
6987   }
6988
6989   {
6990     char buf[ntohs (message->size)] GNUNET_ALIGN;
6991     struct GNUNET_MESH_Multicast *copy;
6992
6993     copy = (struct GNUNET_MESH_Multicast *) buf;
6994     memcpy (buf, message, ntohs (message->size));
6995     copy->oid = my_full_id;
6996     copy->tid = htonl (t->id.tid);
6997     copy->ttl = htonl (default_ttl);
6998     GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
6999     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7000                 "  calling generic handler...\n");
7001     handle_mesh_data_multicast (client, &my_full_id, &copy->header, NULL, 0);
7002   }
7003
7004   /* receive done gets called when last copy is sent to a neighbor */
7005   return;
7006 }
7007
7008
7009 /**
7010  * Handler for client's ACKs for payload traffic.
7011  *
7012  * @param cls Closure (unused).
7013  * @param client Identification of the client.
7014  * @param message The actual message.
7015  */
7016 static void
7017 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7018                   const struct GNUNET_MessageHeader *message)
7019 {
7020   struct GNUNET_MESH_LocalAck *msg;
7021   struct MeshTunnel *t;
7022   struct MeshClient *c;
7023   MESH_TunnelNumber tid;
7024   uint32_t ack;
7025
7026   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7027   /* Sanity check for client registration */
7028   if (NULL == (c = client_get (client)))
7029   {
7030     GNUNET_break (0);
7031     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7032     return;
7033   }
7034   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
7035
7036   msg = (struct GNUNET_MESH_LocalAck *) message;
7037
7038   /* Tunnel exists? */
7039   tid = ntohl (msg->tunnel_id);
7040   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X\n", tid);
7041   t = tunnel_get_by_local_id (c, tid);
7042   if (NULL == t)
7043   {
7044     GNUNET_break (0); // FIXME fc
7045     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7046     return;
7047   }
7048
7049   ack = ntohl (msg->max_pid);
7050   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ack %u\n", ack);
7051
7052   /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7053   if (NULL != t->owner && t->owner->handle == client)
7054   {
7055     /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7056     t->bck_ack = ack;
7057     tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7058   }
7059   else
7060   {
7061     /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7062     tunnel_set_client_fwd_ack (t, c, ack);
7063     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7064   }
7065
7066   GNUNET_SERVER_receive_done (client, GNUNET_OK);
7067   return;
7068 }
7069
7070
7071 /**
7072  * Functions to handle messages from clients
7073  */
7074 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7075   {&handle_local_new_client, NULL,
7076    GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7077   {&handle_local_announce_regex, NULL,
7078    GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7079   {&handle_local_tunnel_create, NULL,
7080    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7081    sizeof (struct GNUNET_MESH_TunnelMessage)},
7082   {&handle_local_tunnel_destroy, NULL,
7083    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7084    sizeof (struct GNUNET_MESH_TunnelMessage)},
7085   {&handle_local_tunnel_speed, NULL,
7086    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7087    sizeof (struct GNUNET_MESH_TunnelMessage)},
7088   {&handle_local_tunnel_speed, NULL,
7089    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7090    sizeof (struct GNUNET_MESH_TunnelMessage)},
7091   {&handle_local_tunnel_buffer, NULL,
7092    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7093    sizeof (struct GNUNET_MESH_TunnelMessage)},
7094   {&handle_local_tunnel_buffer, NULL,
7095    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7096    sizeof (struct GNUNET_MESH_TunnelMessage)},
7097   {&handle_local_connect_add, NULL,
7098    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7099    sizeof (struct GNUNET_MESH_PeerControl)},
7100   {&handle_local_connect_del, NULL,
7101    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7102    sizeof (struct GNUNET_MESH_PeerControl)},
7103   {&handle_local_blacklist, NULL,
7104    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7105    sizeof (struct GNUNET_MESH_PeerControl)},
7106   {&handle_local_unblacklist, NULL,
7107    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7108    sizeof (struct GNUNET_MESH_PeerControl)},
7109   {&handle_local_connect_by_type, NULL,
7110    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7111    sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7112   {&handle_local_connect_by_string, NULL,
7113    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7114   {&handle_local_unicast, NULL,
7115    GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7116   {&handle_local_to_origin, NULL,
7117    GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7118   {&handle_local_multicast, NULL,
7119    GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7120   {&handle_local_ack, NULL,
7121    GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7122    sizeof (struct GNUNET_MESH_LocalAck)},
7123   {NULL, NULL, 0, 0}
7124 };
7125
7126
7127 /**
7128  * To be called on core init/fail.
7129  *
7130  * @param cls service closure
7131  * @param server handle to the server for this service
7132  * @param identity the public identity of this peer
7133  */
7134 static void
7135 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7136            const struct GNUNET_PeerIdentity *identity)
7137 {
7138   static int i = 0;
7139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7140   core_handle = server;
7141   if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7142       NULL == server)
7143   {
7144     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7145     GNUNET_SCHEDULER_shutdown (); // Try gracefully
7146     if (10 < i++)
7147       GNUNET_abort(); // Try harder
7148   }
7149   return;
7150 }
7151
7152
7153 /**
7154  * Method called whenever a given peer connects.
7155  *
7156  * @param cls closure
7157  * @param peer peer identity this notification is about
7158  * @param atsi performance data for the connection
7159  * @param atsi_count number of records in 'atsi'
7160  */
7161 static void
7162 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7163               const struct GNUNET_ATS_Information *atsi,
7164               unsigned int atsi_count)
7165 {
7166   struct MeshPeerInfo *peer_info;
7167   struct MeshPeerPath *path;
7168
7169   DEBUG_CONN ("Peer connected\n");
7170   DEBUG_CONN ("     %s\n", GNUNET_i2s (&my_full_id));
7171   peer_info = peer_info_get (peer);
7172   if (myid == peer_info->id)
7173   {
7174     DEBUG_CONN ("     (self)\n");
7175     return;
7176   }
7177   else
7178   {
7179     DEBUG_CONN ("     %s\n", GNUNET_i2s (peer));
7180   }
7181   path = path_new (2);
7182   path->peers[0] = myid;
7183   path->peers[1] = peer_info->id;
7184   GNUNET_PEER_change_rc (myid, 1);
7185   GNUNET_PEER_change_rc (peer_info->id, 1);
7186   peer_info_add_path (peer_info, path, GNUNET_YES);
7187   GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7188   return;
7189 }
7190
7191
7192 /**
7193  * Method called whenever a peer disconnects.
7194  *
7195  * @param cls closure
7196  * @param peer peer identity this notification is about
7197  */
7198 static void
7199 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7200 {
7201   struct MeshPeerInfo *pi;
7202   struct MeshPeerQueue *q;
7203   struct MeshPeerQueue *n;
7204
7205   DEBUG_CONN ("Peer disconnected\n");
7206   pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7207   if (NULL == pi)
7208   {
7209     GNUNET_break (0);
7210     return;
7211   }
7212   q = pi->queue_head;
7213   while (NULL != q)
7214   {
7215       n = q->next;
7216       if (q->peer == pi)
7217       {
7218         /* try to reroute this traffic instead */
7219         queue_destroy(q, GNUNET_YES);
7220       }
7221       q = n;
7222   }
7223   peer_info_remove_path (pi, pi->id, myid);
7224   if (myid == pi->id)
7225   {
7226     DEBUG_CONN ("     (self)\n");
7227   }
7228   GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7229   return;
7230 }
7231
7232
7233 /******************************************************************************/
7234 /************************      MAIN FUNCTIONS      ****************************/
7235 /******************************************************************************/
7236
7237 /**
7238  * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7239  *
7240  * @param cls closure
7241  * @param key current key code
7242  * @param value value in the hash map
7243  * @return GNUNET_YES if we should continue to iterate,
7244  *         GNUNET_NO if not.
7245  */
7246 static int
7247 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7248 {
7249   struct MeshTunnel *t = value;
7250
7251   tunnel_destroy (t);
7252   return GNUNET_YES;
7253 }
7254
7255 /**
7256  * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7257  *
7258  * @param cls closure
7259  * @param key current key code
7260  * @param value value in the hash map
7261  * @return GNUNET_YES if we should continue to iterate,
7262  *         GNUNET_NO if not.
7263  */
7264 static int
7265 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7266 {
7267   struct MeshPeerInfo *p = value;
7268   struct MeshPeerQueue *q;
7269   struct MeshPeerQueue *n;
7270
7271   q = p->queue_head;
7272   while (NULL != q)
7273   {
7274       n = q->next;
7275       if (q->peer == p)
7276       {
7277         queue_destroy(q, GNUNET_YES);
7278       }
7279       q = n;
7280   }
7281   peer_info_destroy (p);
7282   return GNUNET_YES;
7283 }
7284
7285 /**
7286  * Task run during shutdown.
7287  *
7288  * @param cls unused
7289  * @param tc unused
7290  */
7291 static void
7292 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7293 {
7294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7295
7296   if (core_handle != NULL)
7297   {
7298     GNUNET_CORE_disconnect (core_handle);
7299     core_handle = NULL;
7300   }
7301   GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7302   GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7303   if (dht_handle != NULL)
7304   {
7305     GNUNET_DHT_disconnect (dht_handle);
7306     dht_handle = NULL;
7307   }
7308   if (nc != NULL)
7309   {
7310     GNUNET_SERVER_notification_context_destroy (nc);
7311     nc = NULL;
7312   }
7313   if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7314   {
7315     GNUNET_SCHEDULER_cancel (announce_id_task);
7316     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7317   }
7318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7319 }
7320
7321 /**
7322  * Process mesh requests.
7323  *
7324  * @param cls closure
7325  * @param server the initialized server
7326  * @param c configuration to use
7327  */
7328 static void
7329 run (void *cls, struct GNUNET_SERVER_Handle *server,
7330      const struct GNUNET_CONFIGURATION_Handle *c)
7331 {
7332   struct MeshPeerInfo *peer;
7333   struct MeshPeerPath *p;
7334   char *keyfile;
7335
7336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7337   server_handle = server;
7338   core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7339                                      NULL,      /* Closure passed to MESH functions */
7340                                      &core_init,        /* Call core_init once connected */
7341                                      &core_connect,     /* Handle connects */
7342                                      &core_disconnect,  /* remove peers on disconnects */
7343                                      NULL,      /* Don't notify about all incoming messages */
7344                                      GNUNET_NO, /* For header only in notification */
7345                                      NULL,      /* Don't notify about all outbound messages */
7346                                      GNUNET_NO, /* For header-only out notification */
7347                                      core_handlers);    /* Register these handlers */
7348
7349   if (core_handle == NULL)
7350   {
7351     GNUNET_break (0);
7352     GNUNET_SCHEDULER_shutdown ();
7353     return;
7354   }
7355
7356   if (GNUNET_OK !=
7357       GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7358                                                &keyfile))
7359   {
7360     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7361                 _
7362                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7363                 "hostkey");
7364     GNUNET_SCHEDULER_shutdown ();
7365     return;
7366   }
7367
7368   if (GNUNET_OK !=
7369       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7370                                            &refresh_path_time))
7371   {
7372     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7373                 _
7374                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7375                 "refresh path time");
7376     GNUNET_SCHEDULER_shutdown ();
7377     return;
7378   }
7379
7380   if (GNUNET_OK !=
7381       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7382                                            &app_announce_time))
7383   {
7384     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7385                 _
7386                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7387                 "app announce time");
7388     GNUNET_SCHEDULER_shutdown ();
7389     return;
7390   }
7391
7392   if (GNUNET_OK !=
7393       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7394                                            &id_announce_time))
7395   {
7396     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7397                 _
7398                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7399                 "id announce time");
7400     GNUNET_SCHEDULER_shutdown ();
7401     return;
7402   }
7403
7404   if (GNUNET_OK !=
7405       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7406                                            &unacknowledged_wait_time))
7407   {
7408     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7409                 _
7410                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7411                 "unacknowledged wait time");
7412     GNUNET_SCHEDULER_shutdown ();
7413     return;
7414   }
7415
7416   if (GNUNET_OK !=
7417       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7418                                            &connect_timeout))
7419   {
7420     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7421                 _
7422                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7423                 "connect timeout");
7424     GNUNET_SCHEDULER_shutdown ();
7425     return;
7426   }
7427
7428   if (GNUNET_OK !=
7429       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7430                                              &max_msgs_queue))
7431   {
7432     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7433                 _
7434                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7435                 "max msgs queue");
7436     GNUNET_SCHEDULER_shutdown ();
7437     return;
7438   }
7439
7440   if (GNUNET_OK !=
7441       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7442                                              &max_tunnels))
7443   {
7444     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7445                 _
7446                 ("Mesh service is lacking key configuration settings (%s).  Exiting.\n"),
7447                 "max tunnels");
7448     GNUNET_SCHEDULER_shutdown ();
7449     return;
7450   }
7451
7452   if (GNUNET_OK !=
7453       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7454                                              &default_ttl))
7455   {
7456     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7457                 _
7458                 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7459                 "default ttl", 64);
7460     default_ttl = 64;
7461   }
7462
7463   if (GNUNET_OK !=
7464       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7465                                              &dht_replication_level))
7466   {
7467     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7468                 _
7469                 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7470                 "dht replication level", 10);
7471     dht_replication_level = 10;
7472   }
7473
7474   
7475   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7476   GNUNET_free (keyfile);
7477   if (my_private_key == NULL)
7478   {
7479     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7480                 _("Mesh service could not access hostkey.  Exiting.\n"));
7481     GNUNET_SCHEDULER_shutdown ();
7482     return;
7483   }
7484   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7485   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7486                       &my_full_id.hashPubKey);
7487   myid = GNUNET_PEER_intern (&my_full_id);
7488
7489 //   transport_handle = GNUNET_TRANSPORT_connect(c,
7490 //                                               &my_full_id,
7491 //                                               NULL,
7492 //                                               NULL,
7493 //                                               NULL,
7494 //                                               NULL);
7495
7496   dht_handle = GNUNET_DHT_connect (c, 64);
7497   if (dht_handle == NULL)
7498   {
7499     GNUNET_break (0);
7500   }
7501
7502   stats = GNUNET_STATISTICS_create ("mesh", c);
7503
7504
7505   next_tid = 0;
7506   next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7507
7508   tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7509   incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7510   peers = GNUNET_CONTAINER_multihashmap_create (32);
7511   applications = GNUNET_CONTAINER_multihashmap_create (32);
7512   types = GNUNET_CONTAINER_multihashmap_create (32);
7513
7514   GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7515   nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7516   GNUNET_SERVER_disconnect_notify (server_handle,
7517                                    &handle_local_client_disconnect, NULL);
7518
7519
7520   clients = NULL;
7521   clients_tail = NULL;
7522   next_client_id = 0;
7523
7524   announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7525   announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7526
7527   /* Create a peer_info for the local peer */
7528   peer = peer_info_get (&my_full_id);
7529   p = path_new (1);
7530   p->peers[0] = myid;
7531   GNUNET_PEER_change_rc (myid, 1);
7532   peer_info_add_path (peer, p, GNUNET_YES);
7533
7534   /* Scheduled the task to clean up when shutdown is called */
7535   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7536                                 NULL);
7537
7538   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7539 }
7540
7541 /**
7542  * The main function for the mesh service.
7543  *
7544  * @param argc number of arguments from the command line
7545  * @param argv command line arguments
7546  * @return 0 ok, 1 on error
7547  */
7548 int
7549 main (int argc, char *const *argv)
7550 {
7551   int ret;
7552
7553   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7554   ret =
7555       (GNUNET_OK ==
7556        GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7557                            NULL)) ? 0 : 1;
7558   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7559
7560   return ret;
7561 }