Added testcase code, fixed minor bugs
[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  * - speed requirement specification (change?) in mesh API -- API call
42  * - add ping message
43  * - add connection confirmation message
44  * - handle trnsmt_rdy return values
45  */
46
47 #include "platform.h"
48 #include "mesh.h"
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
52
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME       GNUNET_TIME_relative_multiply(\
55                                     GNUNET_TIME_UNIT_SECONDS,\
56                                     300)
57 #define APP_ANNOUNCE_TIME       GNUNET_TIME_relative_multiply(\
58                                     GNUNET_TIME_UNIT_SECONDS,\
59                                     5)
60
61 #define ID_ANNOUNCE_TIME        GNUNET_TIME_relative_multiply(\
62                                     GNUNET_TIME_UNIT_SECONDS,\
63                                     5)
64
65
66 /******************************************************************************/
67 /************************      DATA STRUCTURES     ****************************/
68 /******************************************************************************/
69
70 /** FWD declaration */
71 struct MeshPeerInfo;
72
73 /**
74  * Struct containing all info possibly needed to build a package when called
75  * back by core.
76  */
77 struct MeshDataDescriptor
78 {
79     /** ID of the tunnel this packet travels in */
80   struct MESH_TunnelID *origin;
81
82     /** Ultimate destination of the packet */
83   GNUNET_PEER_Id destination;
84
85     /** Number of identical messages sent to different hops (multicast) */
86   unsigned int copies;
87
88     /** Size of the data */
89   size_t size;
90
91     /** Client that asked for the transmission, if any */
92   struct GNUNET_SERVER_Client *client;
93
94     /** Who was is message being sent to */
95   struct MeshPeerInfo *peer;
96
97     /** Which handler was used to request the transmission */
98   unsigned int handler_n;
99
100   /* Data at the end */
101 };
102
103
104 /**
105  * Struct containing all information regarding a given peer
106  */
107 struct MeshPeerInfo
108 {
109     /**
110      * ID of the peer
111      */
112   GNUNET_PEER_Id id;
113
114     /**
115      * Last time we heard from this peer
116      */
117   struct GNUNET_TIME_Absolute last_contact;
118
119     /**
120      * Number of attempts to reconnect so far
121      */
122   int n_reconnect_attempts;
123
124     /**
125      * Paths to reach the peer, ordered by ascending hop count
126      */
127   struct MeshPeerPath *path_head;
128
129     /**
130      * Paths to reach the peer, ordered by ascending hop count
131      */
132   struct MeshPeerPath *path_tail;
133
134     /**
135      * Handle to stop the DHT search for a path to this peer
136      */
137   struct GNUNET_DHT_GetHandle *dhtget;
138
139     /**
140      * Handles to stop queued transmissions for this peer
141      */
142   struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
143
144     /**
145      * Pointer to info stuctures used as cls for queued transmissions
146      */
147   struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
148
149     /**
150      * Array of tunnels this peer participates in
151      * (most probably a small amount, therefore not a hashmap)
152      * When the path to the peer changes, notify these tunnels to let them
153      * re-adjust their path trees.
154      */
155   struct MeshTunnel **tunnels;
156
157     /**
158      * Number of tunnels above
159      */
160   unsigned int ntunnels;
161 };
162
163
164 /**
165  * Data scheduled to transmit (to local client or remote peer)
166  */
167 struct MeshQueue
168 {
169     /**
170      * Double linked list
171      */
172   struct MeshQueue *next;
173   struct MeshQueue *prev;
174
175     /**
176      * Target of the data (NULL if target is client)
177      */
178   struct MeshPeerInfo *peer;
179
180     /**
181      * Client to send the data to (NULL if target is peer)
182      */
183   struct MeshClient *client;
184
185     /**
186      * Size of the message to transmit
187      */
188   unsigned int size;
189
190     /**
191      * How old is the data?
192      */
193   struct GNUNET_TIME_Absolute timestamp;
194
195     /**
196      * Data itself
197      */
198   struct GNUNET_MessageHeader *data;
199 };
200
201 /**
202  * Globally unique tunnel identification (owner + number)
203  * DO NOT USE OVER THE NETWORK
204  */
205 struct MESH_TunnelID
206 {
207     /**
208      * Node that owns the tunnel
209      */
210   GNUNET_PEER_Id oid;
211
212     /**
213      * Tunnel number to differentiate all the tunnels owned by the node oid
214      * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
215      */
216   MESH_TunnelNumber tid;
217 };
218
219
220 struct MeshClient;              /* FWD declaration */
221
222 /**
223  * Struct containing all information regarding a tunnel
224  * For an intermediate node the improtant info used will be:
225  * - id        Tunnel unique identification
226  * - paths[0]  To know where to send it next
227  * - metainfo: ready, speeds, accounting
228  */
229 struct MeshTunnel
230 {
231     /**
232      * Tunnel ID
233      */
234   struct MESH_TunnelID id;
235
236     /**
237      * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
238      */
239   MESH_TunnelNumber local_tid;
240
241     /**
242      * Last time the tunnel was used
243      */
244   struct GNUNET_TIME_Absolute timestamp;
245
246     /**
247      * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
248      */
249   struct GNUNET_CONTAINER_MultiHashMap *peers;
250
251     /**
252      * Number of peers that are connected and potentially ready to receive data
253      */
254   unsigned int peers_ready;
255
256     /**
257      * Number of peers that have been added to the tunnel
258      */
259   unsigned int peers_total;
260
261     /**
262      * Client owner of the tunnel, if any
263      */
264   struct MeshClient *client;
265
266     /**
267      * Messages ready to transmit
268      */
269   struct MeshQueue *queue_head;
270   struct MeshQueue *queue_tail;
271
272   /**
273    * Tunnel paths
274    */
275   struct MeshTunnelTree *tree;
276
277   /**
278    * Task to keep the used paths alive
279    */
280   GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
281 };
282
283
284 /**
285  * Info needed to work with tunnel paths and peers
286  */
287 struct MeshPathInfo
288 {
289   /**
290    * Tunnel
291    */
292   struct MeshTunnel *t;
293
294   /**
295    * Destination peer
296    */
297   struct MeshPeerInfo *peer;
298
299   /**
300    * Path itself
301    */
302   struct MeshPeerPath *path;
303 };
304
305
306 /**
307  * Struct containing information about a client of the service
308  */
309 struct MeshClient
310 {
311     /**
312      * Linked list
313      */
314   struct MeshClient *next;
315   struct MeshClient *prev;
316
317     /**
318      * Tunnels that belong to this client, indexed by local id
319      */
320   struct GNUNET_CONTAINER_MultiHashMap *tunnels;
321
322     /**
323      * Handle to communicate with the client
324      */
325   struct GNUNET_SERVER_Client *handle;
326
327     /**
328      * Applications that this client has claimed to provide
329      */
330   struct GNUNET_CONTAINER_MultiHashMap *apps;
331
332     /**
333      * Messages that this client has declared interest in
334      */
335   struct GNUNET_CONTAINER_MultiHashMap *types;
336
337     /**
338      * Used to search peers offering a service
339      */
340   struct GNUNET_DHT_GetHandle *dht_get_type;
341
342 #if MESH_DEBUG
343     /**
344      * ID of the client, for debug messages
345      */
346   unsigned int id;
347 #endif
348
349 };
350
351
352
353 /******************************************************************************/
354 /************************      DEBUG FUNCTIONS     ****************************/
355 /******************************************************************************/
356
357 #if MESH_DEBUG
358 /**
359  * GNUNET_SCHEDULER_Task for printing a message after some operation is done
360  * @param cls string to print
361  * @param tc task context
362  */
363 static void
364 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
365 {
366   char *s = cls;
367
368   if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
369   {
370     return;
371   }
372   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
373 }
374 #endif
375
376
377 /******************************************************************************/
378 /***********************      GLOBAL VARIABLES     ****************************/
379 /******************************************************************************/
380
381 /**
382  * All the clients
383  */
384 static struct MeshClient *clients;
385 static struct MeshClient *clients_tail;
386
387 /**
388  * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
389  */
390 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
391
392 /**
393  * Peers known, indexed by PeerIdentity (MeshPeerInfo)
394  */
395 static struct GNUNET_CONTAINER_MultiHashMap *peers;
396
397 /**
398  * Handle to communicate with core
399  */
400 static struct GNUNET_CORE_Handle *core_handle;
401
402 /**
403  * Handle to use DHT
404  */
405 static struct GNUNET_DHT_Handle *dht_handle;
406
407 /**
408  * Handle to server
409  */
410 static struct GNUNET_SERVER_Handle *server_handle;
411
412 /**
413  * Notification context, to send messages to local clients
414  */
415 static struct GNUNET_SERVER_NotificationContext *nc;
416
417 /**
418  * Local peer own ID (memory efficient handle)
419  */
420 static GNUNET_PEER_Id myid;
421
422 /**
423  * Local peer own ID (full value)
424  */
425 static struct GNUNET_PeerIdentity my_full_id;
426
427 /**
428  * Own private key
429  */
430 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
431
432 /**
433  * Own public key.
434  */
435 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
436
437 /**
438  * Tunnel ID for the next created tunnel (global tunnel number)
439  */
440 static MESH_TunnelNumber next_tid;
441
442 /**
443  * All application types provided by this peer
444  */
445 static struct GNUNET_CONTAINER_MultiHashMap *applications;
446
447 /**
448  * All message types clients of this peer are interested in
449  */
450 static struct GNUNET_CONTAINER_MultiHashMap *types;
451
452 /**
453  * Task to periodically announce provided applications
454  */
455 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
456
457 /**
458  * Task to periodically announce itself in the network
459  */
460 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
461
462 #if MESH_DEBUG
463 unsigned int next_client_id;
464 #endif
465
466
467 /******************************************************************************/
468 /************************         ITERATORS        ****************************/
469 /******************************************************************************/
470
471
472 /**
473  * Iterator over hash map peer entries collect all neighbors who to resend the
474  * data to.
475  *
476  * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
477  * @param key current key code (peer id hash)
478  * @param value value in the hash map (peer_info)
479  * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
480  */
481 static int
482 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
483 {
484   struct MeshPeerInfo *peer_info = value;
485   struct MeshPathInfo *neighbors = cls;
486   struct GNUNET_PeerIdentity *id;
487   GNUNET_PEER_Id peer_id;
488   unsigned int i;
489
490   if (peer_info->id == myid)
491   {
492     return GNUNET_YES;
493   }
494   id = path_get_first_hop (neighbors->t->tree, peer_info->id);
495   peer_id = GNUNET_PEER_search(id);
496   for (i = 0; i < neighbors->path->length; i++)
497   {
498     if (neighbors->path->peers[i] == peer_id)
499       return GNUNET_YES;
500   }
501   GNUNET_array_append (neighbors->path->peers, neighbors->path->length,
502                        peer_id);
503
504   return GNUNET_YES;
505 }
506
507
508 /**
509  * Iterator over hash map peer entries and frees all data in it.
510  * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
511  *
512  * @param cls closure
513  * @param key current key code (will no longer contain valid data!!)
514  * @param value value in the hash map (treated as void *)
515  * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
516  */
517 static int
518 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
519 {
520   GNUNET_free(value);
521   return GNUNET_YES;
522 }
523
524
525 /******************************************************************************/
526 /************************    PERIODIC FUNCTIONS    ****************************/
527 /******************************************************************************/
528
529 /**
530  * Announce iterator over for each application provided by the peer
531  *
532  * @param cls closure
533  * @param key current key code
534  * @param value value in the hash map
535  * @return GNUNET_YES if we should continue to
536  *         iterate,
537  *         GNUNET_NO if not.
538  */
539 static int
540 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
541 {
542   /* FIXME are hashes in multihash map equal on all aquitectures? */
543   GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
544                   GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
545                   (const char *) &my_full_id,
546 #if MESH_DEBUG
547                   GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
548                   &mesh_debug, "DHT_put for app completed");
549 #else
550                   GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
551                                             APP_ANNOUNCE_TIME),
552                   APP_ANNOUNCE_TIME, NULL, NULL);
553 #endif
554   return GNUNET_OK;
555 }
556
557
558 /**
559  * Periodically announce what applications are provided by local clients
560  *
561  * @param cls closure
562  * @param tc task context
563  */
564 static void
565 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 {
567   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
568   {
569     announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
570     return;
571   }
572   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
573   GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
574                                          NULL);
575   announce_applications_task =
576       GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
577                                     cls);
578   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
579   return;
580 }
581
582
583 /**
584  * Periodically announce self id in the DHT
585  *
586  * @param cls closure
587  * @param tc task context
588  */
589 static void
590 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
591 {
592   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
593   {
594     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
595     return;
596   }
597   /* TODO
598    * - Set data expiration in function of X
599    * - Adapt X to churn
600    */
601   GNUNET_DHT_put (dht_handle,   /* DHT handle */
602                   &my_full_id.hashPubKey,       /* Key to use */
603                   10U,          /* Replication level */
604                   GNUNET_DHT_RO_RECORD_ROUTE,   /* DHT options */
605                   GNUNET_BLOCK_TYPE_TEST,       /* Block type */
606                   0,            /* Size of the data */
607                   NULL,         /* Data itself */
608                   GNUNET_TIME_absolute_get_forever (),  /* Data expiration */
609                   GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
610 #if MESH_DEBUG
611                   &mesh_debug, "DHT_put for id completed");
612 #else
613                   NULL,         /* Continuation */
614                   NULL);        /* Continuation closure */
615 #endif
616   announce_id_task =
617       GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
618 }
619
620
621 /******************************************************************************/
622 /******************      GENERAL HELPER FUNCTIONS      ************************/
623 /******************************************************************************/
624
625 /**
626  * Retrieve the MeshPeerInfo stucture associated with the peer, create one
627  * and insert it in the appropiate structures if the peer is not known yet.
628  *
629  * @param peer Identity of the peer
630  *
631  * @return Existing or newly created peer info
632  */
633 static struct MeshPeerInfo *
634 peer_info_get (const struct GNUNET_PeerIdentity *peer)
635 {
636   struct MeshPeerInfo *peer_info;
637
638   peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
639   if (NULL == peer_info)
640   {
641     peer_info =
642         (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
643     GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
644                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
645     peer_info->id = GNUNET_PEER_intern (peer);
646   }
647
648   return peer_info;
649 }
650
651
652 #if LATER
653 /**
654  * Destroy the peer_info and free any allocated resources linked to it
655  * @param t tunnel the path belongs to
656  * @param pi the peer_info to destroy
657  * @return GNUNET_OK on success
658  */
659 static int
660 peer_info_destroy (struct MeshPeerInfo *pi)
661 {
662   GNUNET_HashCode hash;
663   struct GNUNET_PeerIdentity id;
664
665   GNUNET_PEER_resolve (pi->id, &id);
666   GNUNET_PEER_change_rc (pi->id, -1);
667   GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
668
669   GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
670   GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
671   GNUNET_free (pi);
672   return GNUNET_OK;
673 }
674 #endif
675
676
677 /**
678  * Notify a tunnel that a connection has broken that affects at least
679  * some of its peers.
680  *
681  * @param t Tunnel affected
682  * @param peer Peer that (at least) has been affected by the disconnection
683  * @param p1 Peer that got disconnected from p2
684  * @param p2 Peer that got disconnected from p1
685  */
686 static void
687 tunnel_notify_connection_broken (struct MeshTunnel *t,
688                                  struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
689                                  GNUNET_PEER_Id p2);
690
691
692 /**
693  * Remove all paths that rely on a direct connection between p1 and p2
694  * from the peer itself and notify all tunnels about it.
695  *
696  * @param pi PeerInfo of affected peer
697  * @param p1 GNUNET_PEER_Id of one peer.
698  * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
699  *           no longer is.
700  */
701 static void
702 path_remove_from_peer (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
703                        GNUNET_PEER_Id p2)
704 {
705   struct MeshPeerPath *p;
706   struct MeshPeerPath *aux;
707   unsigned int destroyed;
708   unsigned int i;
709
710   destroyed = 0;
711   p = peer->path_head;
712   while (NULL != p)
713   {
714     aux = p->next;
715     for (i = 0; i < (p->length - 1); i++)
716     {
717       if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
718           (p->peers[i] == p2 && p->peers[i + 1] == p1))
719       {
720         path_destroy (p);
721         destroyed++;
722         break;
723       }
724     }
725     p = aux;
726   }
727   if (0 == destroyed)
728     return;
729
730   for (i = 0; i < peer->ntunnels; i++)
731   {
732     tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
733   }
734 }
735
736
737 /**
738  * Add the path to the peer and update the path used to reach it in case this
739  * is the shortest.
740  *
741  * @param peer_info Destination peer to add the path to.
742  * @param path New path to add. Last peer must be the peer in arg 1.
743  *             Path will be either used of freed if already known.
744  *
745  * TODO: trim the part from origin to us? Add it as path to origin?
746  */
747 void
748 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
749 {
750   struct MeshPeerPath *aux;
751   unsigned int l;
752   unsigned int l2;
753
754   if (NULL == peer_info || NULL == path)
755   {
756     GNUNET_break (0);
757     return;
758   }
759
760   l = path_get_length (path);
761
762   for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
763   {
764     l2 = path_get_length (aux);
765     if (l2 > l)
766     {
767       GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
768                                           peer_info->path_tail, aux, path);
769     }
770     else
771     {
772       if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
773       {
774         path_destroy(path);
775         return;
776       }
777     }
778   }
779   GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
780                                     path);
781   return;
782 }
783
784
785 /**
786  * Add the path to the origin peer and update the path used to reach it in case
787  * this is the shortest.
788  * The path is given in peer_info -> destination, therefore we turn the path
789  * upside down first.
790  *
791  * @param peer_info Peer to add the path to, being the origin of the path.
792  * @param path New path to add after being inversed.
793  */
794 static void
795 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
796 {
797   path_invert(path);
798   path_add_to_peer (peer_info, path);
799 }
800
801
802 /**
803  * Build a PeerPath from the paths returned from the DHT, reversing the paths
804  * to obtain a local peer -> destination path and interning the peer ids.
805  *
806  * @param get_path NULL-terminated array of pointers
807  *                 to the peers on reverse GET path (or NULL if not recorded)
808  * @param put_path NULL-terminated array of pointers
809  *                 to the peers on the PUT path (or NULL if not recorded)
810  *
811  * @return Newly allocated and created path
812  */
813 static struct MeshPeerPath *
814 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
815                      const struct GNUNET_PeerIdentity *const *put_path)
816 {
817   struct MeshPeerPath *p;
818   GNUNET_PEER_Id id;
819   int i;
820
821   p = GNUNET_malloc (sizeof (struct MeshPeerPath));
822   for (i = 0; get_path[i] != NULL; i++) ;
823   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH:    GET has %d hops.\n", i);
824   for (i--; i >= 0; i--)
825   {
826     id = GNUNET_PEER_intern (get_path[i]);
827     if (p->length > 0 && id == p->peers[p->length - 1])
828     {
829       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH:    Optimizing 1 hop out.\n");
830       GNUNET_PEER_change_rc(id, -1);
831     }
832     else
833     {
834       p->peers =
835         GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
836       p->peers[p->length] = id;
837       p->length++;
838     }
839   }
840   for (i = 0; put_path[i] != NULL; i++) ;
841   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH:    PUT has %d hops.\n", i);
842   for (i--; i >= 0; i--)
843   {
844     id = GNUNET_PEER_intern (put_path[i]);
845     if (p->length > 0 && id == p->peers[p->length - 1])
846     {
847       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH:    Optimizing 1 hop out.\n");
848       GNUNET_PEER_change_rc(id, -1);
849     }
850     else
851     {
852       p->peers =
853         GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
854       p->peers[p->length] = id;
855       p->length++;
856     }
857   }
858 #if MESH_DEBUG
859   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860               "MESH:    (first of GET: %s)\n",
861               GNUNET_h2s_full(&get_path[0]->hashPubKey));
862   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863               "MESH:    (first of PUT: %s)\n",
864               GNUNET_h2s_full(&put_path[0]->hashPubKey));
865   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866               "MESH:    In total: %d hops\n",
867               p->length);
868 #endif
869   return p;
870 }
871
872
873 /**
874  * Send keepalive packets for a peer
875  *
876  * @param cls unused
877  * @param tc unused
878  *
879  * FIXME path
880  */
881 void
882 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
883 {
884   struct MeshTunnel *t = cls;
885
886 //   struct GNUNET_PeerIdentity id;
887
888   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
889   {
890     return;
891   }
892   /* FIXME implement multicast keepalive. Just an empty multicast packet? */
893 //   GNUNET_PEER_resolve (path_get_first_hop (path->t, path->peer)->id, &id);
894 //   GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
895 //                                      GNUNET_TIME_UNIT_FOREVER_REL, &id,
896 //                                      sizeof (struct GNUNET_MESH_ManipulatePath)
897 //                                      +
898 //                                      (path->path->length *
899 //                                       sizeof (struct GNUNET_PeerIdentity)),
900 //                                      &send_core_create_path,
901 //                                      t);
902   t->path_refresh_task =
903       GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
904   return;
905 }
906
907
908 /**
909  * Check if client has registered with the service and has not disconnected
910  *
911  * @param client the client to check
912  *
913  * @return non-NULL if client exists in the global DLL
914  */
915 static struct MeshClient *
916 client_get (struct GNUNET_SERVER_Client *client)
917 {
918   struct MeshClient *c;
919
920   c = clients;
921   while (NULL != c)
922   {
923     if (c->handle == client)
924       return c;
925     c = c->next;
926   }
927   return NULL;
928 }
929
930
931 /**
932  * Checks if a given client has subscribed to certain message type
933  *
934  * @param message_type Type of message to check
935  * @param c Client to check
936  *
937  * @return GNUNET_YES or GNUNET_NO, depending on subscription status
938  *
939  * TODO inline?
940  */
941 static int
942 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
943 {
944   GNUNET_HashCode hc;
945
946   GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
947   return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
948 }
949
950
951 /**
952  * Search for a tunnel among the tunnels for a client
953  *
954  * @param c the client whose tunnels to search in
955  * @param tid the local id of the tunnel
956  *
957  * @return tunnel handler, NULL if doesn't exist
958  */
959 static struct MeshTunnel *
960 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
961 {
962   GNUNET_HashCode hash;
963
964   GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
965   return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
966 }
967
968
969 /**
970  * Search for a tunnel by global ID using PEER_ID
971  *
972  * @param pi owner of the tunnel
973  * @param tid global tunnel number
974  *
975  * @return tunnel handler, NULL if doesn't exist
976  */
977 static struct MeshTunnel *
978 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
979 {
980   struct MESH_TunnelID id;
981   GNUNET_HashCode hash;
982
983   id.oid = pi;
984   id.tid = tid;
985
986   GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
987   return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
988 }
989
990
991 /**
992  * Search for a tunnel by global ID using full PeerIdentities
993  *
994  * @param oid owner of the tunnel
995  * @param tid global tunnel number
996  *
997  * @return tunnel handler, NULL if doesn't exist
998  */
999 static struct MeshTunnel *
1000 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1001 {
1002   return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1003 }
1004
1005
1006 void
1007 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1008 {
1009   
1010 }
1011
1012
1013 /**
1014  * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1015  * needed rescources.
1016  *
1017  * @param t Tunnel we want to add a new peer to
1018  * @param peer PeerInfo of the peer being added
1019  *
1020  */
1021 void
1022 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1023 {
1024   struct MeshPeerPath *p;
1025   struct MeshPeerPath *best_p;
1026   unsigned int best_cost;
1027   unsigned int cost;
1028
1029   GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1030   if (NULL == (p = peer->path_head))
1031     return;
1032
1033   best_p = p;
1034   best_cost = UINT_MAX;
1035   while (NULL != p)
1036   {
1037     if ((cost = path_get_cost (t->tree, p)) < best_cost)
1038     {
1039       best_cost = cost;
1040       best_p = p;
1041     }
1042     p = p->next;
1043   }
1044   tunnel_add_path (t->tree, best_p, &notify_peer_disconnected);
1045   if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1046     t->path_refresh_task =
1047         GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1048 }
1049
1050
1051 /**
1052  * Notify a tunnel that a connection has broken that affects at least
1053  * some of its peers.
1054  *
1055  * @param t Tunnel affected
1056  * @param peer Peer that (at least) has been affected by the disconnection
1057  * @param p1 Peer that got disconnected from p2
1058  * @param p2 Peer that got disconnected from p1
1059  * 
1060  * FIXME path
1061  */
1062 static void
1063 tunnel_notify_connection_broken (struct MeshTunnel *t,
1064                                  struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1065                                  GNUNET_PEER_Id p2)
1066 {
1067 }
1068
1069
1070 /**
1071  * Recursively destory the path tree of a tunnel.
1072  * Note: it does not liberate memory for itself, parent must do it!
1073  *
1074  * @param n The node to destroy, along with children.
1075  *
1076  * @return GNUNET_OK on success
1077  */
1078 static void
1079 tunnel_destroy_tree_node (struct MeshTunnelTreeNode *n)
1080 {
1081   unsigned int i;
1082
1083   for (i = 0; i < n->nchildren; i++)
1084   {
1085     tunnel_destroy_tree_node(&n->children[i]);
1086   }
1087   if (NULL != n->children)
1088     GNUNET_free (n->children);
1089 }
1090
1091
1092 /**
1093  * Destroy the tunnel and free any allocated resources linked to it
1094  *
1095  * @param t the tunnel to destroy
1096  *
1097  * @return GNUNET_OK on success
1098  */
1099 static int
1100 tunnel_destroy (struct MeshTunnel *t)
1101 {
1102   struct MeshClient *c;
1103   struct MeshQueue *q;
1104   struct MeshQueue *qn;
1105   GNUNET_HashCode hash;
1106   int r;
1107
1108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1109   if (NULL == t)
1110     return GNUNET_OK;
1111
1112   c = t->client;
1113 #if MESH_DEBUG
1114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:   by client %u\n", c->id);
1115 #endif
1116
1117   GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1118   if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1119   {
1120     r = GNUNET_SYSERR;
1121   }
1122
1123   GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1124   if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1125   {
1126     r = GNUNET_SYSERR;
1127   }
1128   GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1129   q = t->queue_head;
1130   while (NULL != q)
1131   {
1132     if (NULL != q->data)
1133       GNUNET_free (q->data);
1134     qn = q->next;
1135     GNUNET_free (q);
1136     q = qn;
1137     /* TODO cancel core transmit ready in case it was active */
1138   }
1139
1140   GNUNET_CONTAINER_multihashmap_iterate(t->tree->first_hops, &iterate_free, t);
1141   GNUNET_CONTAINER_multihashmap_destroy(t->tree->first_hops);
1142   tunnel_destroy_tree_node(t->tree->root);
1143   GNUNET_free(t->tree->root);
1144   GNUNET_free (t->tree);
1145   GNUNET_free (t);
1146   return r;
1147 }
1148
1149
1150 /**
1151  * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1152  * client when the client disconnects.
1153  * 
1154  * @param cls closure (client that is disconnecting)
1155  * @param key the hash of the local tunnel id (used to access the hashmap)
1156  * @param value the value stored at the key (tunnel to destroy)
1157  * 
1158  * @return GNUNET_OK on success
1159  */
1160 static int
1161 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1162 {
1163   int r;
1164
1165   r = tunnel_destroy ((struct MeshTunnel *) value);
1166   return r;
1167 }
1168
1169
1170 /******************************************************************************/
1171 /****************      MESH NETWORK HANDLER HELPERS     ***********************/
1172 /******************************************************************************/
1173
1174
1175 /**
1176  * Function called to notify a client about the socket
1177  * being ready to queue more data.  "buf" will be
1178  * NULL and "size" zero if the socket was closed for
1179  * writing in the meantime.
1180  *
1181  * @param cls closure
1182  * @param size number of bytes available in buf
1183  * @param buf where the callee should write the message
1184  * @return number of bytes written to buf
1185  */
1186 static size_t
1187 send_core_create_path (void *cls, size_t size, void *buf)
1188 {
1189   struct MeshPathInfo *info = cls;
1190   struct GNUNET_MESH_ManipulatePath *msg;
1191   struct GNUNET_PeerIdentity *peer_ptr;
1192   struct MeshPeerInfo *peer = info->peer;
1193   struct MeshTunnel *t = info->t;
1194   struct MeshPeerPath *p = info->path;
1195   size_t size_needed;
1196   int i;
1197
1198   size_needed =
1199       sizeof (struct GNUNET_MESH_ManipulatePath) +
1200       p->length * sizeof (struct GNUNET_PeerIdentity);
1201
1202   if (size < size_needed || NULL == buf)
1203   {
1204     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1205     GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1206                                        GNUNET_TIME_UNIT_FOREVER_REL,
1207                                        path_get_first_hop (t->tree, peer->id),
1208                                        size_needed, &send_core_create_path,
1209                                        info);
1210     return 0;
1211   }
1212
1213   msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1214   msg->header.size = htons (size_needed);
1215   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1216   msg->tid = ntohl (t->id.tid);
1217
1218   peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1219   for (i = 0; i < p->length; i++)
1220   {
1221     GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1222   }
1223
1224   path_destroy (p);
1225   GNUNET_free (info);
1226
1227   return size_needed;
1228 }
1229
1230
1231 #if LATER
1232 /**
1233  * Function called to notify a client about the socket
1234  * being ready to queue more data.  "buf" will be
1235  * NULL and "size" zero if the socket was closed for
1236  * writing in the meantime.
1237  *
1238  * @param cls closure (MeshDataDescriptor with all info to build packet)
1239  * @param size number of bytes available in buf
1240  * @param buf where the callee should write the message
1241  * @return number of bytes written to buf
1242  */
1243 static size_t
1244 send_core_data_to_origin (void *cls, size_t size, void *buf)
1245 {
1246   struct MeshDataDescriptor *info = cls;
1247   struct GNUNET_MESH_ToOrigin *msg = buf;
1248   size_t total_size;
1249
1250   GNUNET_assert (NULL != info);
1251   total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1252   GNUNET_assert (total_size < 65536);   /* UNIT16_MAX */
1253
1254   if (total_size > size)
1255   {
1256     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1257                 "not enough buffer to send data to origin\n");
1258     return 0;
1259   }
1260   msg->header.size = htons (total_size);
1261   msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1262   GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1263   msg->tid = htonl (info->origin->tid);
1264   if (0 != info->size)
1265   {
1266     memcpy (&msg[1], &info[1], info->size);
1267   }
1268   if (NULL != info->client)
1269   {
1270     GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1271   }
1272   GNUNET_free (info);
1273   return total_size;
1274 }
1275 #endif
1276
1277 /**
1278  * Function called to notify a client about the socket
1279  * being ready to queue more data.  "buf" will be
1280  * NULL and "size" zero if the socket was closed for
1281  * writing in the meantime.
1282  *
1283  * @param cls closure (data itself)
1284  * @param size number of bytes available in buf
1285  * @param buf where the callee should write the message
1286  * @return number of bytes written to buf
1287  */
1288 static size_t
1289 send_core_data_unicast (void *cls, size_t size, void *buf)
1290 {
1291   struct MeshDataDescriptor *info = cls;
1292   struct GNUNET_MESH_Unicast *msg = buf;
1293   size_t total_size;
1294
1295   GNUNET_assert (NULL != info);
1296   total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1297   GNUNET_assert (total_size < 65536);   /* UNIT16_MAX */
1298
1299   if (total_size > size)
1300   {
1301     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1302                 "not enough buffer to send data to peer\n");
1303     return 0;
1304   }
1305   msg->header.size = htons (total_size);
1306   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1307   GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1308   GNUNET_PEER_resolve (info->destination, &msg->destination);
1309   msg->tid = htonl (info->origin->tid);
1310   if (0 != info->size)
1311   {
1312     memcpy (&msg[1], &info[1], info->size);
1313   }
1314   if (NULL != info->client)
1315   {
1316     GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1317   }
1318   GNUNET_free (info);
1319   return total_size;
1320 }
1321
1322
1323 /**
1324  * Function called to notify a client about the socket
1325  * being ready to queue more data.  "buf" will be
1326  * NULL and "size" zero if the socket was closed for
1327  * writing in the meantime.
1328  *
1329  * @param cls closure (data itself)
1330  * @param size number of bytes available in buf
1331  * @param buf where the callee should write the message
1332  * @return number of bytes written to buf
1333  */
1334 static size_t
1335 send_core_data_multicast (void *cls, size_t size, void *buf)
1336 {
1337   struct MeshDataDescriptor *info = cls;
1338   struct GNUNET_MESH_Multicast *msg = buf;
1339   size_t total_size;
1340
1341   GNUNET_assert (NULL != info);
1342   total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1343   GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1344
1345   if (info->peer)
1346   {
1347     info->peer->core_transmit[info->handler_n] = NULL;
1348   }
1349   if (total_size > size)
1350   {
1351     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1352                 "not enough buffer to send data futher\n");
1353     return 0;
1354   }
1355   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1356   msg->header.size = htons (total_size);
1357   GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1358   msg->tid = htonl (info->origin->tid);
1359   memcpy (&msg[1], &info[1], total_size);
1360   if (0 == --info->copies)
1361   {
1362     if (NULL != info->client)
1363     {
1364       GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1365     }
1366     GNUNET_free (info);
1367   }
1368   return total_size;
1369 }
1370
1371
1372 /**
1373  * Function called to notify a client about the socket
1374  * being ready to queue more data.  "buf" will be
1375  * NULL and "size" zero if the socket was closed for
1376  * writing in the meantime.
1377  *
1378  * @param cls closure (MeshDataDescriptor)
1379  * @param size number of bytes available in buf
1380  * @param buf where the callee should write the message
1381  * @return number of bytes written to buf
1382  */
1383 static size_t
1384 send_core_path_ack (void *cls, size_t size, void *buf)
1385 {
1386   struct MeshDataDescriptor *info = cls;
1387   struct GNUNET_MESH_PathACK *msg = buf;
1388
1389   GNUNET_assert (NULL != info);
1390   if (info->peer)
1391   {
1392     info->peer->core_transmit[info->handler_n] = NULL;
1393   }
1394   if (sizeof (struct GNUNET_MESH_PathACK) > size)
1395   {
1396     GNUNET_break (0);
1397     return 0;
1398   }
1399   msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1400   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1401   GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1402   msg->tid = htonl (info->origin->tid);
1403   msg->peer_id = my_full_id;
1404   GNUNET_free (info);
1405   /* TODO add signature */
1406
1407   return sizeof (struct GNUNET_MESH_PathACK);
1408 }
1409
1410
1411 /**
1412  * Function called to notify a client about the socket
1413  * being ready to queue more data.  "buf" will be
1414  * NULL and "size" zero if the socket was closed for
1415  * writing in the meantime.
1416  *
1417  * @param cls closure (data itself)
1418  * @param size number of bytes available in buf
1419  * @param buf where the callee should write the message
1420  * @return number of bytes written to buf
1421  */
1422 static size_t
1423 send_core_data_raw (void *cls, size_t size, void *buf)
1424 {
1425   struct GNUNET_MessageHeader *msg = cls;
1426   size_t total_size;
1427
1428   GNUNET_assert (NULL != msg);
1429   total_size = ntohs (msg->size);
1430
1431   if (total_size > size)
1432   {
1433     GNUNET_break (0);
1434     return 0;
1435   }
1436   memcpy (buf, msg, total_size);
1437   GNUNET_free (cls);
1438   return total_size;
1439 }
1440
1441
1442 #if LATER
1443 /**
1444  * Send another peer a notification to destroy a tunnel
1445  * @param cls The tunnel to destroy
1446  * @param size Size in the buffer
1447  * @param buf Memory where to put the data to transmit
1448  * @return Size of data put in buffer
1449  */
1450 static size_t
1451 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1452 {
1453   struct MeshTunnel *t = cls;
1454   struct MeshClient *c;
1455   struct GNUNET_MESH_TunnelMessage *msg;
1456
1457   c = t->client;
1458   msg = buf;
1459   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1460    /*FIXME*/ msg->header.size =
1461       htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1462   msg->tunnel_id = htonl (t->id.tid);
1463
1464   tunnel_destroy (c, t);
1465   return sizeof (struct GNUNET_MESH_TunnelMessage);
1466 }
1467 #endif
1468
1469
1470 /**
1471  * Send the message to all clients that have subscribed to its type
1472  *
1473  * @param msg Pointer to the message itself
1474  * @return number of clients this message was sent to
1475  */
1476 static unsigned int
1477 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1478 {
1479   struct MeshClient *c;
1480   unsigned int count;
1481   uint16_t type;
1482
1483   type = ntohs (msg->type);
1484   for (count = 0, c = clients; c != NULL; c = c->next)
1485   {
1486     if (client_is_subscribed (type, c))
1487     {
1488       count++;
1489       GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1490                                                   GNUNET_YES);
1491     }
1492   }
1493   return count;
1494 }
1495
1496
1497
1498 /**
1499  * Notify the client that owns the tunnel that a peer has connected to it
1500  * 
1501  * @param t Tunnel whose owner to notify
1502  * @param id Short id of the peer that has connected
1503  */
1504 static void
1505 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1506 {
1507   struct GNUNET_MESH_PeerControl pc;
1508
1509   pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1510   pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1511   pc.tunnel_id = htonl (t->local_tid);
1512   GNUNET_PEER_resolve (id, &pc.peer);
1513   GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1514                                               &pc.header, GNUNET_NO);
1515 }
1516
1517
1518 /******************************************************************************/
1519 /********************      MESH NETWORK HANDLERS     **************************/
1520 /******************************************************************************/
1521
1522
1523 /**
1524  * Core handler for path creation
1525  * struct GNUNET_CORE_MessageHandler
1526  *
1527  * @param cls closure
1528  * @param message message
1529  * @param peer peer identity this notification is about
1530  * @param atsi performance data
1531  * @return GNUNET_OK to keep the connection open,
1532  *         GNUNET_SYSERR to close it (signal serious error)
1533  *
1534  */
1535 static int
1536 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1537                          const struct GNUNET_MessageHeader *message,
1538                          const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1539 {
1540   unsigned int own_pos;
1541   uint16_t size;
1542   uint16_t i;
1543   MESH_TunnelNumber tid;
1544   struct GNUNET_MESH_ManipulatePath *msg;
1545   struct GNUNET_PeerIdentity *pi;
1546   struct GNUNET_PeerIdentity id;
1547   GNUNET_HashCode hash;
1548   struct MeshPeerPath *path;
1549   struct MeshPeerInfo *dest_peer_info;
1550   struct MeshPeerInfo *orig_peer_info;
1551   struct MeshTunnel *t;
1552
1553   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554               "MESH: Received a MESH path create msg\n");
1555   size = ntohs (message->size);
1556   if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1557   {
1558     GNUNET_break_op (0);
1559     return GNUNET_OK;
1560   }
1561
1562   size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1563   if (size % sizeof (struct GNUNET_PeerIdentity))
1564   {
1565     GNUNET_break_op (0);
1566     return GNUNET_OK;
1567   }
1568   size /= sizeof (struct GNUNET_PeerIdentity);
1569   if (size < 2)
1570   {
1571     GNUNET_break_op (0);
1572     return GNUNET_OK;
1573   }
1574   msg = (struct GNUNET_MESH_ManipulatePath *) message;
1575
1576   tid = ntohl (msg->tid);
1577   pi = (struct GNUNET_PeerIdentity *) &msg[1];
1578   t = tunnel_get (pi, tid);
1579
1580   if (NULL == t)
1581   {
1582     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1583     t = GNUNET_malloc (sizeof (struct MeshTunnel));
1584     t->id.oid = GNUNET_PEER_intern (pi);
1585     t->id.tid = tid;
1586     t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1587
1588     GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1589     if (GNUNET_OK !=
1590         GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1591                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1592     {
1593       GNUNET_break (0);
1594       return GNUNET_OK;
1595     }
1596
1597   }
1598   dest_peer_info =
1599       GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1600   if (NULL == dest_peer_info)
1601   {
1602     dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1603     dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1604     GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1605                                        dest_peer_info,
1606                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1607   }
1608   orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1609   if (NULL == orig_peer_info)
1610   {
1611     orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1612     orig_peer_info->id = GNUNET_PEER_intern (pi);
1613     GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1614                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1615   }
1616
1617   path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1618   path->length = size;
1619   path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1620   own_pos = 0;
1621   for (i = 0; i < size; i++)
1622   {
1623     path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1624     if (path->peers[i] == myid)
1625       own_pos = i;
1626   }
1627   if (own_pos == 0)
1628   {                             /* cannot be self, must be 'not found' */
1629     /* create path: self not found in path through self */
1630     GNUNET_break_op (0);
1631     path_destroy (path);
1632     /* FIXME error. destroy tunnel? leave for timeout? */
1633     return 0;
1634   }
1635   if (own_pos == size - 1)
1636   {
1637     /* It is for us! Send ack. */
1638     struct MeshDataDescriptor *info;
1639     unsigned int j;
1640
1641     path_add_to_origin (orig_peer_info, path);  /* inverts path!  */
1642     info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1643     info->origin = &t->id;
1644     info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1645     GNUNET_assert (info->peer);
1646     for (j = 0; info->peer->core_transmit[j]; j++)
1647     {
1648       if (j == 9)
1649       {
1650         GNUNET_break (0);
1651         return GNUNET_OK;
1652       }
1653     }
1654     info->handler_n = j;
1655     info->peer->core_transmit[j] =
1656         GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1657                                            GNUNET_TIME_UNIT_FOREVER_REL, peer,
1658                                            sizeof (struct GNUNET_MessageHeader),
1659                                            &send_core_path_ack, info);
1660   }
1661   else
1662   {
1663     /* It's for somebody else! Retransmit. */
1664     struct MeshPathInfo *path_info;
1665
1666     path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1667     path_info->t = t;
1668     path_info->path = path;
1669     path_info->peer = dest_peer_info;
1670
1671     path_add_to_peer (dest_peer_info, path);
1672     GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1673     GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1674                                        GNUNET_TIME_UNIT_FOREVER_REL, &id,
1675                                        sizeof (struct GNUNET_MessageHeader),
1676                                        &send_core_create_path, path_info);
1677   }
1678   return GNUNET_OK;
1679 }
1680
1681
1682 /**
1683  * Core handler for mesh network traffic going from the origin to a peer
1684  *
1685  * @param cls closure
1686  * @param peer peer identity this notification is about
1687  * @param message message
1688  * @param atsi performance data
1689  * @return GNUNET_OK to keep the connection open,
1690  *         GNUNET_SYSERR to close it (signal serious error)
1691  */
1692 static int
1693 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1694                           const struct GNUNET_MessageHeader *message,
1695                           const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1696 {
1697   struct GNUNET_MESH_Unicast *msg;
1698   struct MeshTunnel *t;
1699   struct MeshPeerInfo *pi;
1700   size_t size;
1701
1702   size = ntohs (message->size);
1703   if (size <
1704       sizeof (struct GNUNET_MESH_Unicast) +
1705       sizeof (struct GNUNET_MessageHeader))
1706   {
1707     GNUNET_break (0);
1708     return GNUNET_OK;
1709   }
1710   msg = (struct GNUNET_MESH_Unicast *) message;
1711   t = tunnel_get (&msg->oid, ntohl (msg->tid));
1712   if (NULL == t)
1713   {
1714     /* TODO notify back: we don't know this tunnel */
1715     return GNUNET_OK;
1716   }
1717   pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1718                                           &msg->destination.hashPubKey);
1719   if (NULL == pi)
1720   {
1721     /* TODO maybe feedback, log to statistics */
1722     return GNUNET_OK;
1723   }
1724   if (pi->id == myid)
1725   {
1726     send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1727     return GNUNET_OK;
1728   }
1729   msg = GNUNET_malloc (size);
1730   memcpy (msg, message, size);
1731   GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1732                                      GNUNET_TIME_UNIT_FOREVER_REL,
1733                                      path_get_first_hop (t->tree, pi->id),
1734                                      size,
1735                                      &send_core_data_raw, msg);
1736   return GNUNET_OK;
1737 }
1738
1739
1740 /**
1741  * Core handler for mesh network traffic going from the origin to all peers
1742  *
1743  * @param cls closure
1744  * @param message message
1745  * @param peer peer identity this notification is about
1746  * @param atsi performance data
1747  * @return GNUNET_OK to keep the connection open,
1748  *         GNUNET_SYSERR to close it (signal serious error)
1749  */
1750 static int
1751 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1752                             const struct GNUNET_MessageHeader *message,
1753                             const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1754 {
1755   struct GNUNET_MESH_Multicast *msg;
1756   struct GNUNET_PeerIdentity id;
1757   struct MeshDataDescriptor *info;
1758   struct MeshPathInfo neighbors;
1759   struct MeshTunnel *t;
1760   size_t size;
1761   uint16_t i;
1762   uint16_t j;
1763
1764
1765   size = ntohs (message->size);
1766   if (size <
1767       sizeof (struct GNUNET_MESH_Multicast) +
1768       sizeof (struct GNUNET_MessageHeader))
1769   {
1770     GNUNET_break_op (0);
1771     return GNUNET_OK;
1772   }
1773   msg = (struct GNUNET_MESH_Multicast *) message;
1774   t = tunnel_get (&msg->oid, ntohl (msg->tid));
1775
1776   if (NULL == t)
1777   {
1778     /* TODO notify that we dont know that tunnel */
1779     return GNUNET_OK;
1780   }
1781
1782   /* Transmit to locally interested clients */
1783   if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1784   {
1785     send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1786   }
1787
1788   /* Retransmit to other peers.
1789    * Using path here as just a collection of peers, not a path per se.
1790    */
1791   neighbors.t = t;
1792   neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1793   GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1794                                          &neighbors);
1795   if (0 == neighbors.path->length)
1796   {
1797     GNUNET_free (neighbors.path);
1798     return GNUNET_OK;
1799   }
1800   size -= sizeof (struct GNUNET_MESH_Multicast);
1801   info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1802   info->origin = &t->id;
1803   info->copies = neighbors.path->length;
1804   for (i = 0; i < info->copies; i++)
1805   {
1806     GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
1807     info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1808     GNUNET_assert (NULL != info->peer);
1809     for (j = 0; 0 != info->peer->core_transmit[j]; j++)
1810     {
1811       if (j == (CORE_QUEUE_SIZE - 1))
1812       {
1813         GNUNET_break (0);
1814         return GNUNET_OK;
1815       }
1816     }
1817     info->handler_n = j;
1818     info->peer->infos[j] = info;
1819     info->peer->core_transmit[j] =
1820         GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1821                                            GNUNET_TIME_UNIT_FOREVER_REL, &id,
1822                                            ntohs (msg->header.size),
1823                                            &send_core_data_multicast, info);
1824   }
1825   GNUNET_free (neighbors.path->peers);
1826   GNUNET_free (neighbors.path);
1827   return GNUNET_OK;
1828 }
1829
1830
1831 /**
1832  * Core handler for mesh network traffic
1833  *
1834  * @param cls closure
1835  * @param message message
1836  * @param peer peer identity this notification is about
1837  * @param atsi performance data
1838  *
1839  * @return GNUNET_OK to keep the connection open,
1840  *         GNUNET_SYSERR to close it (signal serious error)
1841  */
1842 static int
1843 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1844                           const struct GNUNET_MessageHeader *message,
1845                           const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1846 {
1847   struct GNUNET_MESH_ToOrigin *msg;
1848   struct GNUNET_PeerIdentity id;
1849   struct MeshPeerInfo *peer_info;
1850   struct MeshTunnel *t;
1851   size_t size;
1852
1853   size = ntohs (message->size);
1854   if (size < sizeof (struct GNUNET_MESH_ToOrigin) +     /* Payload must be */
1855       sizeof (struct GNUNET_MessageHeader))     /* at least a header */
1856   {
1857     GNUNET_break_op (0);
1858     return GNUNET_OK;
1859   }
1860   msg = (struct GNUNET_MESH_ToOrigin *) message;
1861   t = tunnel_get (&msg->oid, ntohl (msg->tid));
1862
1863   if (NULL == t)
1864   {
1865     /* TODO notify that we dont know this tunnel (whom)? */
1866     return GNUNET_OK;
1867   }
1868
1869   if (t->id.oid == myid)
1870   {
1871     if (NULL == t->client)
1872     {
1873       /* got data packet for ownerless tunnel */
1874       GNUNET_break_op (0);
1875       return GNUNET_OK;
1876     }
1877     /* TODO signature verification */
1878     GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1879                                                 GNUNET_YES);
1880     return GNUNET_OK;
1881   }
1882   peer_info = peer_info_get (&msg->oid);
1883   if (NULL == peer_info)
1884   {
1885     /* unknown origin of tunnel */
1886     GNUNET_break (0);
1887     return GNUNET_OK;
1888   }
1889   GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1890   msg = GNUNET_malloc (size);
1891   memcpy (msg, message, size);
1892   GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1893                                      GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1894                                      &send_core_data_raw, msg);
1895
1896   return GNUNET_OK;
1897 }
1898
1899
1900 /**
1901  * Core handler for path ACKs
1902  *
1903  * @param cls closure
1904  * @param message message
1905  * @param peer peer identity this notification is about
1906  * @param atsi performance data
1907  *
1908  * @return GNUNET_OK to keep the connection open,
1909  *         GNUNET_SYSERR to close it (signal serious error)
1910  *
1911  * FIXME path change state
1912  */
1913 static int
1914 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1915                       const struct GNUNET_MessageHeader *message,
1916                       const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1917 {
1918   struct GNUNET_MESH_PathACK *msg;
1919   struct MeshTunnel *t;
1920   struct MeshPeerInfo *peer_info;
1921
1922   msg = (struct GNUNET_MESH_PathACK *) message;
1923   t = tunnel_get (&msg->oid, msg->tid);
1924   if (NULL == t)
1925   {
1926     /* TODO notify that we don't know the tunnel */
1927     return GNUNET_OK;
1928   }
1929
1930   /* Message for us? */
1931   if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1932   {
1933
1934     if (NULL == t->client)
1935     {
1936       GNUNET_break (0);
1937       return GNUNET_OK;
1938     }
1939     peer_info = peer_info_get (&msg->peer_id);
1940     if (NULL == peer_info)
1941     {
1942       GNUNET_break_op (0);
1943       return GNUNET_OK;
1944     }
1945     /* FIXME change state of peer */
1946     send_client_peer_connected(t, peer_info->id);
1947     return GNUNET_OK;
1948   }
1949
1950   peer_info = peer_info_get (&msg->oid);
1951   if (NULL == peer_info)
1952   {
1953     /* If we know the tunnel, we should DEFINITELY know the peer */
1954     GNUNET_break (0);
1955     return GNUNET_OK;
1956   }
1957   msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1958   memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1959   GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1960                                      GNUNET_TIME_UNIT_FOREVER_REL,
1961                                      path_get_first_hop (t->tree,
1962                                                          peer_info->id),
1963                                      sizeof (struct GNUNET_MESH_PathACK),
1964                                      &send_core_data_raw, msg);
1965   return GNUNET_OK;
1966 }
1967
1968
1969 /**
1970  * Functions to handle messages from core
1971  */
1972 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1973   {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1974   {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1975   {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1976   {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1977   {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1978    sizeof (struct GNUNET_MESH_PathACK)},
1979   {NULL, 0, 0}
1980 };
1981
1982
1983
1984 /******************************************************************************/
1985 /****************       MESH LOCAL HANDLER HELPERS      ***********************/
1986 /******************************************************************************/
1987
1988 /**
1989  * deregister_app: iterator for removing each application registered by a client
1990  * 
1991  * @param cls closure
1992  * @param key the hash of the application id (used to access the hashmap)
1993  * @param value the value stored at the key (client)
1994  * 
1995  * @return GNUNET_OK on success
1996  */
1997 static int
1998 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1999 {
2000   GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2001   return GNUNET_OK;
2002 }
2003
2004 #if LATER
2005 /**
2006  * notify_client_connection_failure: notify a client that the connection to the
2007  * requested remote peer is not possible (for instance, no route found)
2008  * Function called when the socket is ready to queue more data. "buf" will be
2009  * NULL and "size" zero if the socket was closed for writing in the meantime.
2010  *
2011  * @param cls closure
2012  * @param size number of bytes available in buf
2013  * @param buf where the callee should write the message
2014  * @return number of bytes written to buf
2015  */
2016 static size_t
2017 notify_client_connection_failure (void *cls, size_t size, void *buf)
2018 {
2019   int size_needed;
2020   struct MeshPeerInfo *peer_info;
2021   struct GNUNET_MESH_PeerControl *msg;
2022   struct GNUNET_PeerIdentity id;
2023
2024   if (0 == size && NULL == buf)
2025   {
2026     // TODO retry? cancel?
2027     return 0;
2028   }
2029
2030   size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2031   peer_info = (struct MeshPeerInfo *) cls;
2032   msg = (struct GNUNET_MESH_PeerControl *) buf;
2033   msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2034   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2035 //     msg->tunnel_id = htonl(peer_info->t->tid);
2036   GNUNET_PEER_resolve (peer_info->id, &id);
2037   memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2038
2039   return size_needed;
2040 }
2041 #endif
2042
2043
2044 /**
2045  * Function to process paths received for a new peer addition. The recorded
2046  * paths form the initial tunnel, which can be optimized later.
2047  * Called on each result obtained for the DHT search.
2048  *
2049  * @param cls closure
2050  * @param exp when will this value expire
2051  * @param key key of the result
2052  * @param get_path NULL-terminated array of pointers
2053  *                 to the peers on reverse GET path (or NULL if not recorded)
2054  * @param put_path NULL-terminated array of pointers
2055  *                 to the peers on the PUT path (or NULL if not recorded)
2056  * @param type type of the result
2057  * @param size number of bytes in data
2058  * @param data pointer to the result data
2059  *
2060  * FIXME path
2061  */
2062 static void
2063 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2064                     const GNUNET_HashCode * key,
2065                     const struct GNUNET_PeerIdentity *const *get_path,
2066                     const struct GNUNET_PeerIdentity *const *put_path,
2067                     enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2068 {
2069   struct MeshPathInfo *path_info = cls;
2070   struct MeshPeerPath *p;
2071   struct GNUNET_PeerIdentity pi;
2072   int i;
2073
2074   if (NULL == get_path || NULL == put_path)
2075   {
2076     if (NULL == path_info->peer->path_head)
2077     {
2078       // Find ourselves some alternate initial path to the destination: retry
2079       GNUNET_DHT_get_stop (path_info->peer->dhtget);
2080       GNUNET_PEER_resolve (path_info->peer->id, &pi);
2081       path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle,       /* handle */
2082                                                       GNUNET_TIME_UNIT_FOREVER_REL,     /* timeout */
2083                                                       GNUNET_BLOCK_TYPE_TEST,   /* type */
2084                                                       &pi.hashPubKey,   /*key to search */
2085                                                       4,        /* replication level */
2086                                                       GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2087                                                       0,        /* mutator */
2088                                                       NULL,     /* xquery */
2089                                                       0,        /* xquery bits */
2090                                                       dht_get_id_handler,
2091                                                       (void *) path_info);
2092       return;
2093     }
2094   }
2095
2096   p = path_build_from_dht (get_path, put_path);
2097   path_add_to_peer (path_info->peer, p);
2098   for (i = 0; i < path_info->peer->ntunnels; i++)
2099   {
2100     tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2101   }
2102   GNUNET_free (path_info);
2103
2104   return;
2105 }
2106
2107
2108 /**
2109  * Function to process paths received for a new peer addition. The recorded
2110  * paths form the initial tunnel, which can be optimized later.
2111  * Called on each result obtained for the DHT search.
2112  *
2113  * @param cls closure
2114  * @param exp when will this value expire
2115  * @param key key of the result
2116  * @param get_path NULL-terminated array of pointers
2117  *                 to the peers on reverse GET path (or NULL if not recorded)
2118  * @param put_path NULL-terminated array of pointers
2119  *                 to the peers on the PUT path (or NULL if not recorded)
2120  * @param type type of the result
2121  * @param size number of bytes in data
2122  * @param data pointer to the result data
2123  */
2124 static void
2125 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2126                       const GNUNET_HashCode * key,
2127                       const struct GNUNET_PeerIdentity *const *get_path,
2128                       const struct GNUNET_PeerIdentity *const *put_path,
2129                       enum GNUNET_BLOCK_Type type, size_t size,
2130                       const void *data)
2131 {
2132   const struct GNUNET_PeerIdentity *pi = data;
2133   struct GNUNET_PeerIdentity id;
2134   struct MeshTunnel *t = cls;
2135   struct MeshPeerInfo *peer_info;
2136   struct MeshPathInfo *path_info;
2137   struct MeshPeerPath *p;
2138   int i;
2139
2140   if (size != sizeof (struct GNUNET_PeerIdentity))
2141   {
2142     GNUNET_break_op (0);
2143     return;
2144   }
2145   GNUNET_assert (NULL != t->client);
2146   GNUNET_DHT_get_stop (t->client->dht_get_type);
2147   t->client->dht_get_type = NULL;
2148   peer_info = peer_info_get (pi);
2149   GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2150                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2151
2152   if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2153       NULL == peer_info->dhtget)
2154   {
2155     /* we don't have a route to the peer, let's try a direct lookup */
2156     peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2157                                               /* handle */
2158                                               GNUNET_TIME_UNIT_FOREVER_REL,
2159                                               /* timeout */
2160                                               GNUNET_BLOCK_TYPE_TEST,
2161                                               /* block type */
2162                                               &pi->hashPubKey,
2163                                               /* key to look up */
2164                                               10U,
2165                                               /* replication level */
2166                                               GNUNET_DHT_RO_RECORD_ROUTE,
2167                                               /* option to dht: record route */
2168                                               NULL,     /* bloom filter */
2169                                               0,        /* mutator */
2170                                               NULL,     /* xquery */
2171                                               0,        /* xquery bits */
2172                                               dht_get_id_handler,
2173                                               /* callback */
2174                                               peer_info);       /* closure */
2175   }
2176
2177   p = path_build_from_dht (get_path, put_path);
2178   path_add_to_peer (peer_info, p);
2179   tunnel_add_peer(t, peer_info);
2180   p = tunnel_get_path_to_peer(t->tree, peer_info->id);
2181 #if MESH_DEBUG
2182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183               "MESH: new route for tunnel 0x%x found, has %u hops\n",
2184               t->local_tid, p->length);
2185   for (i = 0; i < p->length; i++)
2186   {
2187     GNUNET_PEER_resolve (p->peers[0], &id);
2188     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2189                 GNUNET_h2s_full (&id.hashPubKey));
2190   }
2191 #endif
2192
2193   if (p->length > 1)
2194   {
2195     path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2196     path_info->t = t;
2197     path_info->peer = peer_info;
2198     path_info->path = p;
2199     GNUNET_PEER_resolve (p->peers[1], &id);
2200     GNUNET_CORE_notify_transmit_ready (core_handle,
2201                                      /* handle */
2202                                      0,
2203                                      /* cork */
2204                                      0,
2205                                      /* priority */
2206                                      GNUNET_TIME_UNIT_FOREVER_REL,
2207                                      /* timeout */
2208                                      &id,
2209                                      /* target */
2210                                      sizeof (struct GNUNET_MESH_ManipulatePath)
2211                                      +
2212                                      (p->length *
2213                                       sizeof (struct GNUNET_PeerIdentity)),
2214                                      /*size */
2215                                      &send_core_create_path,
2216                                      /* callback */
2217                                      path_info);        /* cls */
2218     return;
2219   }
2220   path_destroy(p);
2221   send_client_peer_connected(t, myid);
2222 }
2223
2224 /******************************************************************************/
2225 /*********************       MESH LOCAL HANDLES      **************************/
2226 /******************************************************************************/
2227
2228
2229 /**
2230  * Handler for client disconnection
2231  *
2232  * @param cls closure
2233  * @param client identification of the client; NULL
2234  *        for the last call when the server is destroyed
2235  */
2236 static void
2237 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2238 {
2239   struct MeshClient *c;
2240   struct MeshClient *next;
2241
2242   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2243   if (client == NULL)
2244      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:    (SERVER DOWN)\n");
2245   c = clients;
2246   while (NULL != c)
2247   {
2248     if (c->handle != client && NULL != client)
2249     {
2250       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:    ... searching\n");
2251       c = c->next;
2252       continue;
2253     }
2254     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2255     if (NULL != c->tunnels)
2256     {
2257       GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2258                                              &tunnel_destroy_iterator,
2259                                              c);
2260       GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2261     }
2262
2263     /* deregister clients applications */
2264     if (NULL != c->apps)
2265     {
2266       GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2267       GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2268     }
2269     if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2270         GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2271     {
2272       GNUNET_SCHEDULER_cancel (announce_applications_task);
2273       announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2274     }
2275     if (NULL != c->types)
2276       GNUNET_CONTAINER_multihashmap_destroy (c->types);
2277     if (NULL != c->dht_get_type)
2278       GNUNET_DHT_get_stop (c->dht_get_type);
2279     GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2280     next = c->next;
2281     GNUNET_free (c);
2282     c = next;
2283   }
2284
2285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:    done!\n");
2286   return;
2287 }
2288
2289
2290 /**
2291  * Handler for new clients
2292  *
2293  * @param cls closure
2294  * @param client identification of the client
2295  * @param message the actual message, which includes messages the client wants
2296  */
2297 static void
2298 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2299                          const struct GNUNET_MessageHeader *message)
2300 {
2301   struct GNUNET_MESH_ClientConnect *cc_msg;
2302   struct MeshClient *c;
2303   GNUNET_MESH_ApplicationType *a;
2304   unsigned int size;
2305   uint16_t ntypes;
2306   uint16_t *t;
2307   uint16_t napps;
2308   uint16_t i;
2309
2310   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2311   /* Check data sanity */
2312   size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2313   cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2314   ntypes = ntohs (cc_msg->types);
2315   napps = ntohs (cc_msg->applications);
2316   if (size !=
2317       ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2318   {
2319     GNUNET_break (0);
2320     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2321     return;
2322   }
2323
2324   /* Create new client structure */
2325   c = GNUNET_malloc (sizeof (struct MeshClient));
2326 #if MESH_DEBUG
2327   c->id = next_client_id++;
2328 #endif
2329   c->handle = client;
2330   a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2331   if (napps > 0)
2332   {
2333     GNUNET_MESH_ApplicationType at;
2334     GNUNET_HashCode hc;
2335
2336     c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2337     for (i = 0; i < napps; i++)
2338     {
2339       at = ntohl (a[i]);
2340       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:   app type: %u\n", at);
2341       GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2342       /* store in clients hashmap */
2343       GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2344                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2345       /* store in global hashmap, for announcements */
2346       GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2347                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2348     }
2349     if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2350       announce_applications_task =
2351           GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2352
2353   }
2354   if (ntypes > 0)
2355   {
2356     uint16_t u16;
2357     GNUNET_HashCode hc;
2358
2359     t = (uint16_t *) & a[napps];
2360     c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2361     for (i = 0; i < ntypes; i++)
2362     {
2363       u16 = ntohs (t[i]);
2364       GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2365
2366       /* store in clients hashmap */
2367       GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2368                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2369       /* store in global hashmap */
2370       GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2371                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2372     }
2373   }
2374   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375               "MESH:  client has %u+%u subscriptions\n", napps, ntypes);
2376
2377   GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2378   c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2379   GNUNET_SERVER_notification_context_add (nc, client);
2380
2381   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2382 #if MESH_DEBUG
2383   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2384 #endif
2385 }
2386
2387
2388 /**
2389  * Handler for requests of new tunnels
2390  *
2391  * @param cls closure
2392  * @param client identification of the client
2393  * @param message the actual message
2394  */
2395 static void
2396 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2397                             const struct GNUNET_MessageHeader *message)
2398 {
2399   struct GNUNET_MESH_TunnelMessage *t_msg;
2400   struct MeshTunnel *t;
2401   struct MeshClient *c;
2402   GNUNET_HashCode hash;
2403
2404   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2405
2406   /* Sanity check for client registration */
2407   if (NULL == (c = client_get (client)))
2408   {
2409     GNUNET_break (0);
2410     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2411     return;
2412   }
2413 #if MESH_DEBUG
2414   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:   by client %u\n", c->id);
2415 #endif
2416
2417   /* Message sanity check */
2418   if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2419   {
2420     GNUNET_break (0);
2421     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2422     return;
2423   }
2424
2425   t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2426   /* Sanity check for tunnel numbering */
2427   if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2428   {
2429     GNUNET_break (0);
2430     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2431     return;
2432   }
2433   /* Sanity check for duplicate tunnel IDs */
2434   if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2435   {
2436     GNUNET_break (0);
2437     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2438     return;
2439   }
2440
2441   t = GNUNET_malloc (sizeof (struct MeshTunnel));
2442   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2443   while (NULL != tunnel_get_by_pi (myid, next_tid))
2444     next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2445   t->id.tid = next_tid++;
2446   t->id.oid = myid;
2447   t->local_tid = ntohl (t_msg->tunnel_id);
2448   t->client = c;
2449   t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2450
2451   GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2452   if (GNUNET_OK !=
2453       GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2454                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2455   {
2456     GNUNET_break (0);
2457     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2458     return;
2459   }
2460
2461   GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2462   if (GNUNET_OK !=
2463       GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2464                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2465   {
2466     GNUNET_break (0);
2467     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2468     return;
2469   }
2470   t->tree = GNUNET_malloc (sizeof(struct MeshTunnelTree));
2471   t->tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
2472   t->tree->t = t;
2473   t->tree->refresh = REFRESH_PATH_TIME;
2474   t->tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
2475   t->tree->root->status = MESH_PEER_READY;
2476   t->tree->root->t = t;
2477   t->tree->root->peer = myid;
2478   t->tree->me = t->tree->root;
2479
2480   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2481   return;
2482 }
2483
2484
2485 /**
2486  * Handler for requests of deleting tunnels
2487  *
2488  * @param cls closure
2489  * @param client identification of the client
2490  * @param message the actual message
2491  */
2492 static void
2493 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2494                              const struct GNUNET_MessageHeader *message)
2495 {
2496   struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2497   struct MeshClient *c;
2498   struct MeshTunnel *t;
2499   MESH_TunnelNumber tid;
2500   GNUNET_HashCode hash;
2501
2502   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2503
2504   /* Sanity check for client registration */
2505   if (NULL == (c = client_get (client)))
2506   {
2507     GNUNET_break (0);
2508     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2509     return;
2510   }
2511   /* Message sanity check */
2512   if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2513   {
2514     GNUNET_break (0);
2515     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2516     return;
2517   }
2518 #if MESH_DEBUG
2519   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:   by client %u\n", c->id);
2520 #endif
2521   tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2522
2523   /* Retrieve tunnel */
2524   tid = ntohl (tunnel_msg->tunnel_id);
2525
2526   /* Remove from local id hashmap */
2527   GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2528   t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2529   GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2530
2531   /* Remove from global id hashmap */
2532   GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2533   GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2534
2535 //     notify_tunnel_destroy(t); FIXME
2536   tunnel_destroy(t);
2537   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2538   return;
2539 }
2540
2541
2542 /**
2543  * Handler for connection requests to new peers
2544  *
2545  * @param cls closure
2546  * @param client identification of the client
2547  * @param message the actual message (PeerControl)
2548  *
2549  * FIXME path
2550  */
2551 static void
2552 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2553                           const struct GNUNET_MessageHeader *message)
2554 {
2555   struct GNUNET_MESH_PeerControl *peer_msg;
2556   struct MeshClient *c;
2557   struct MeshTunnel *t;
2558   MESH_TunnelNumber tid;
2559   struct MeshPeerInfo *peer_info;
2560
2561
2562   /* Sanity check for client registration */
2563   if (NULL == (c = client_get (client)))
2564   {
2565     GNUNET_break (0);
2566     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2567     return;
2568   }
2569
2570   peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2571   /* Sanity check for message size */
2572   if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2573   {
2574     GNUNET_break (0);
2575     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2576     return;
2577   }
2578
2579   /* Tunnel exists? */
2580   tid = ntohl (peer_msg->tunnel_id);
2581   t = tunnel_get_by_local_id (c, tid);
2582   if (NULL == t)
2583   {
2584     GNUNET_break (0);
2585     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2586     return;
2587   }
2588
2589   /* Does client own tunnel? */
2590   if (t->client->handle != client)
2591   {
2592     GNUNET_break (0);
2593     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2594     return;
2595   }
2596
2597   t->peers_total++;
2598   peer_info = peer_info_get (&peer_msg->peer);
2599
2600   /* Start DHT search if needed FIXME: if not already connected */
2601   if (NULL == peer_info->dhtget)
2602   {
2603     peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_TEST, &peer_msg->peer.hashPubKey, 4,  /* replication level */
2604                                               GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2605                                               0,        /* mutator */
2606                                               NULL,     /* xquery */
2607                                               0,        /* xquery bits */
2608                                               dht_get_id_handler,
2609                                               (void *) peer_info);
2610   }
2611
2612   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2613   return;
2614 }
2615
2616
2617 /**
2618  * Handler for disconnection requests of peers in a tunnel
2619  *
2620  * @param cls closure
2621  * @param client identification of the client
2622  * @param message the actual message (PeerControl)
2623  */
2624 static void
2625 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2626                           const struct GNUNET_MessageHeader *message)
2627 {
2628   struct GNUNET_MESH_PeerControl *peer_msg;
2629   struct MeshClient *c;
2630   struct MeshTunnel *t;
2631   MESH_TunnelNumber tid;
2632
2633   /* Sanity check for client registration */
2634   if (NULL == (c = client_get (client)))
2635   {
2636     GNUNET_break (0);
2637     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2638     return;
2639   }
2640   peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2641   /* Sanity check for message size */
2642   if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2643   {
2644     GNUNET_break (0);
2645     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2646     return;
2647   }
2648
2649   /* Tunnel exists? */
2650   tid = ntohl (peer_msg->tunnel_id);
2651   t = tunnel_get_by_local_id (c, tid);
2652   if (NULL == t)
2653   {
2654     GNUNET_break (0);
2655     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2656     return;
2657   }
2658
2659   /* Does client own tunnel? */
2660   if (t->client->handle != client)
2661   {
2662     GNUNET_break (0);
2663     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2664     return;
2665   }
2666
2667   /* Ok, delete peer from tunnel */
2668   GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2669                                             &peer_msg->peer.hashPubKey);
2670
2671   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2672   return;
2673 }
2674
2675
2676 /**
2677  * Handler for connection requests to new peers by type
2678  *
2679  * @param cls closure
2680  * @param client identification of the client
2681  * @param message the actual message (ConnectPeerByType)
2682  */
2683 static void
2684 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2685                               const struct GNUNET_MessageHeader *message)
2686 {
2687   struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2688   struct MeshClient *c;
2689   struct MeshTunnel *t;
2690   GNUNET_HashCode hash;
2691   GNUNET_MESH_ApplicationType type;
2692   MESH_TunnelNumber tid;
2693
2694   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2695   /* Sanity check for client registration */
2696   if (NULL == (c = client_get (client)))
2697   {
2698     GNUNET_break (0);
2699     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2700     return;
2701   }
2702
2703   connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2704   /* Sanity check for message size */
2705   if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2706       ntohs (connect_msg->header.size))
2707   {
2708     GNUNET_break (0);
2709     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2710     return;
2711   }
2712
2713   /* Tunnel exists? */
2714   tid = ntohl (connect_msg->tunnel_id);
2715   t = tunnel_get_by_local_id (c, tid);
2716   if (NULL == t)
2717   {
2718     GNUNET_break (0);
2719     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2720     return;
2721   }
2722
2723   /* Does client own tunnel? */
2724   if (t->client->handle != client)
2725   {
2726     GNUNET_break (0);
2727     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2728     return;
2729   }
2730
2731   /* Do WE have the service? */
2732   type = ntohl (connect_msg->type);
2733   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:  type requested: %u\n", type);
2734   GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2735   if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2736       GNUNET_YES)
2737   {
2738     /* Yes! Fast forward, add ourselves to the tunnel and send the
2739      * good news to the client
2740      */
2741     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:  available locally\n");
2742     GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2743                                        peer_info_get (&my_full_id),
2744                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2745
2746     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:  notifying client\n");
2747     send_client_peer_connected(t, myid);
2748     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:  Done\n");
2749     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2750     return;
2751   }
2752   /* Ok, lets find a peer offering the service */
2753   if (c->dht_get_type)
2754   {
2755     GNUNET_DHT_get_stop (c->dht_get_type);
2756   }
2757   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:  looking in DHT for %s\n",
2758               GNUNET_h2s_full (&hash));
2759   c->dht_get_type =
2760       GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2761                             GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2762                             GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2763                             &dht_get_type_handler, t);
2764
2765   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2766   return;
2767 }
2768
2769
2770 /**
2771  * Handler for client traffic directed to one peer
2772  *
2773  * @param cls closure
2774  * @param client identification of the client
2775  * @param message the actual message
2776  */
2777 static void
2778 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2779                       const struct GNUNET_MessageHeader *message)
2780 {
2781   struct MeshClient *c;
2782   struct MeshTunnel *t;
2783   struct MeshPeerInfo *pi;
2784   struct GNUNET_MESH_Unicast *data_msg;
2785   struct MeshDataDescriptor *info;
2786   MESH_TunnelNumber tid;
2787   size_t data_size;
2788
2789   /* Sanity check for client registration */
2790   if (NULL == (c = client_get (client)))
2791   {
2792     GNUNET_break (0);
2793     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2794     return;
2795   }
2796   data_msg = (struct GNUNET_MESH_Unicast *) message;
2797   /* Sanity check for message size */
2798   if (sizeof (struct GNUNET_MESH_Unicast) +
2799       sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2800   {
2801     GNUNET_break (0);
2802     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2803     return;
2804   }
2805
2806   /* Tunnel exists? */
2807   tid = ntohl (data_msg->tid);
2808   t = tunnel_get_by_local_id (c, tid);
2809   if (NULL == t)
2810   {
2811     GNUNET_break (0);
2812     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2813     return;
2814   }
2815
2816   /*  Is it a local tunnel? Then, does client own the tunnel? */
2817   if (t->client->handle != NULL && t->client->handle != client)
2818   {
2819     GNUNET_break (0);
2820     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2821     return;
2822   }
2823
2824   pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2825                                           &data_msg->destination.hashPubKey);
2826   /* Is the selected peer in the tunnel? */
2827   if (NULL == pi)
2828   {
2829     GNUNET_break (0);
2830     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2831     return;
2832   }
2833   if (pi->id == myid)
2834   {
2835     struct GNUNET_MESH_Unicast copy;
2836
2837     /* Work around const limitation */
2838     memcpy (&copy, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2839     copy.oid = my_full_id;
2840     copy.tid = htonl (t->id.tid);
2841     handle_mesh_data_unicast (NULL, &my_full_id, &copy.header, NULL);
2842     return;
2843   }
2844   data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2845   info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2846   memcpy (&info[1], &data_msg[1], data_size);
2847   info->destination = pi->id;
2848   info->origin = &t->id;
2849   info->size = data_size;
2850   info->client = client;
2851   GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2852                                      GNUNET_TIME_UNIT_FOREVER_REL,
2853                                      path_get_first_hop (t->tree, pi->id),
2854                                      /* FIXME re-check types */
2855                                      data_size +
2856                                      sizeof (struct GNUNET_MESH_Unicast),
2857                                      &send_core_data_unicast, info);
2858   return;
2859 }
2860
2861 /**
2862  * Handler for client traffic directed to all peers in a tunnel
2863  *
2864  * @param cls closure
2865  * @param client identification of the client
2866  * @param message the actual message
2867  */
2868 static void
2869 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2870                         const struct GNUNET_MessageHeader *message)
2871 {
2872   struct MeshClient *c;
2873   struct MeshTunnel *t;
2874   struct GNUNET_MESH_Multicast *data_msg;
2875   MESH_TunnelNumber tid;
2876
2877   /* Sanity check for client registration */
2878   if (NULL == (c = client_get (client)))
2879   {
2880     GNUNET_break (0);
2881     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2882     return;
2883   }
2884   data_msg = (struct GNUNET_MESH_Multicast *) message;
2885   /* Sanity check for message size */
2886   if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2887   {
2888     GNUNET_break (0);
2889     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2890     return;
2891   }
2892
2893   /* Tunnel exists? */
2894   tid = ntohl (data_msg->tid);
2895   t = tunnel_get_by_local_id (c, tid);
2896   if (NULL == t)
2897   {
2898     GNUNET_break (0);
2899     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2900     return;
2901   }
2902
2903   /* Does client own tunnel? */
2904   if (t->client->handle != client)
2905   {
2906     GNUNET_break (0);
2907     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2908     return;
2909   }
2910
2911   /*  TODO */
2912
2913   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2914   return;
2915 }
2916
2917 /**
2918  * Functions to handle messages from clients
2919  */
2920 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2921   {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2922   {&handle_local_tunnel_create, NULL,
2923    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2924    sizeof (struct GNUNET_MESH_TunnelMessage)},
2925   {&handle_local_tunnel_destroy, NULL,
2926    GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2927    sizeof (struct GNUNET_MESH_TunnelMessage)},
2928   {&handle_local_connect_add, NULL,
2929    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2930    sizeof (struct GNUNET_MESH_PeerControl)},
2931   {&handle_local_connect_del, NULL,
2932    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2933    sizeof (struct GNUNET_MESH_PeerControl)},
2934   {&handle_local_connect_by_type, NULL,
2935    GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2936    sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2937   {&handle_local_unicast, NULL,
2938    GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2939   {&handle_local_unicast, NULL,
2940    GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2941   {&handle_local_multicast, NULL,
2942    GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2943   {NULL, NULL, 0, 0}
2944 };
2945
2946
2947 /**
2948  * To be called on core init/fail.
2949  *
2950  * @param cls service closure
2951  * @param server handle to the server for this service
2952  * @param identity the public identity of this peer
2953  * @param publicKey the public key of this peer
2954  */
2955 static void
2956 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2957            const struct GNUNET_PeerIdentity *identity,
2958            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2959 {
2960   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2961   core_handle = server;
2962   if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2963   {
2964     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2965     GNUNET_SCHEDULER_shutdown();   
2966   }
2967   return;
2968 }
2969
2970 /**
2971  * Method called whenever a given peer connects.
2972  *
2973  * @param cls closure
2974  * @param peer peer identity this notification is about
2975  * @param atsi performance data for the connection
2976  */
2977 static void
2978 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2979               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2980 {
2981 //     GNUNET_PEER_Id              pid;
2982   struct MeshPeerInfo *peer_info;
2983   struct MeshPeerPath *path;
2984
2985   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2986   peer_info = peer_info_get (peer);
2987   if (myid == peer_info->id)
2988   {
2989     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:      (self)\n");
2990     return;
2991   }
2992   path = GNUNET_malloc (sizeof (struct MeshPeerPath));
2993   path->length = 2;
2994   path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
2995   path->peers[0] = myid;
2996   path->peers[1] = peer_info->id;
2997   path_add_to_peer (peer_info, path);
2998   return;
2999 }
3000
3001 /**
3002  * Method called whenever a peer disconnects.
3003  *
3004  * @param cls closure
3005  * @param peer peer identity this notification is about
3006  */
3007 static void
3008 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3009 {
3010   struct MeshPeerInfo *pi;
3011   unsigned int i;
3012
3013   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3014   pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3015   if (!pi)
3016   {
3017     GNUNET_break (0);
3018     return;
3019   }
3020   for (i = 0; i < CORE_QUEUE_SIZE; i++)
3021   {
3022     if (pi->core_transmit[i])
3023     {
3024       GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3025       /* TODO: notify that tranmission has failed */
3026       GNUNET_free (pi->infos[i]);
3027     }
3028   }
3029   path_remove_from_peer (pi, pi->id, myid);
3030   if (myid == pi->id)
3031   {
3032     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:      (self)\n");
3033   }
3034   return;
3035 }
3036
3037
3038 /******************************************************************************/
3039 /************************      MAIN FUNCTIONS      ****************************/
3040 /******************************************************************************/
3041
3042 /**
3043  * Task run during shutdown.
3044  *
3045  * @param cls unused
3046  * @param tc unused
3047  */
3048 static void
3049 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3050 {
3051   struct MeshClient *c;
3052
3053   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3054   if (core_handle != NULL)
3055   {
3056     GNUNET_CORE_disconnect (core_handle);
3057     core_handle = NULL;
3058   }
3059   if (dht_handle != NULL)
3060   {
3061     for (c = clients; NULL != c; c = c->next)
3062       if (NULL != c->dht_get_type)
3063         GNUNET_DHT_get_stop (c->dht_get_type);
3064     GNUNET_DHT_disconnect (dht_handle);
3065     dht_handle = NULL;
3066   }
3067   if (nc != NULL)
3068   {
3069     GNUNET_SERVER_notification_context_destroy (nc);
3070     nc = NULL;
3071   }
3072   if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3073   {
3074     GNUNET_SCHEDULER_cancel (announce_id_task);
3075     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3076   }
3077   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3078 }
3079
3080 /**
3081  * Process mesh requests.
3082  *
3083  * @param cls closure
3084  * @param server the initialized server
3085  * @param c configuration to use
3086  */
3087 static void
3088 run (void *cls, struct GNUNET_SERVER_Handle *server,
3089      const struct GNUNET_CONFIGURATION_Handle *c)
3090 {
3091   struct MeshPeerInfo *peer;
3092   struct MeshPeerPath *p;
3093   char *keyfile;
3094
3095   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3096   server_handle = server;
3097   core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3098                                      CORE_QUEUE_SIZE,   /* queue size */
3099                                      NULL,      /* Closure passed to MESH functions */
3100                                      &core_init,        /* Call core_init once connected */
3101                                      &core_connect,     /* Handle connects */
3102                                      &core_disconnect,  /* remove peers on disconnects */
3103                                      NULL,      /* Do we care about "status" updates? */
3104                                      NULL,      /* Don't notify about all incoming messages */
3105                                      GNUNET_NO, /* For header only in notification */
3106                                      NULL,      /* Don't notify about all outbound messages */
3107                                      GNUNET_NO, /* For header-only out notification */
3108                                      core_handlers);    /* Register these handlers */
3109   if (core_handle == NULL)
3110   {
3111     GNUNET_break (0);
3112     GNUNET_SCHEDULER_shutdown ();
3113     return;
3114   }
3115
3116   if (GNUNET_OK !=
3117        GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3118                                                 &keyfile))
3119   {
3120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3121                 _
3122                 ("Mesh service is lacking key configuration settings.  Exiting.\n"));
3123     GNUNET_SCHEDULER_shutdown ();
3124     return;
3125   }
3126   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3127   GNUNET_free (keyfile);
3128   if (my_private_key == NULL)
3129   {
3130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3131                 _("Mesh service could not access hostkey.  Exiting.\n"));
3132     GNUNET_SCHEDULER_shutdown ();
3133     return;
3134   }
3135   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3136   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3137                       &my_full_id.hashPubKey);
3138   myid = GNUNET_PEER_intern (&my_full_id);
3139
3140   dht_handle = GNUNET_DHT_connect (c, 64);
3141   if (dht_handle == NULL)
3142   {
3143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3144                    Running without DHT has a severe\
3145                    impact in MESH capabilities.\n\
3146                    Plase check your configuretion and enable DHT.\n");
3147     GNUNET_break (0);
3148   }
3149
3150   next_tid = 0;
3151
3152   tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3153   peers = GNUNET_CONTAINER_multihashmap_create (32);
3154   applications = GNUNET_CONTAINER_multihashmap_create (32);
3155   types = GNUNET_CONTAINER_multihashmap_create (32);
3156
3157   GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3158   nc = GNUNET_SERVER_notification_context_create (server_handle,
3159                                                   LOCAL_QUEUE_SIZE);
3160   GNUNET_SERVER_disconnect_notify (server_handle,
3161                                    &handle_local_client_disconnect,
3162                                    NULL);
3163
3164
3165   clients = NULL;
3166   clients_tail = NULL;
3167 #if MESH_DEBUG
3168   next_client_id = 0;
3169 #endif
3170
3171   announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3172   announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3173
3174   /* Create a peer_info for the local peer */
3175   peer = peer_info_get(&my_full_id);
3176   p = GNUNET_malloc (sizeof (struct MeshPeerPath));
3177   p->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
3178   p->length = 1;
3179   p->peers[0] = myid;
3180   path_add_to_peer(peer, p);
3181
3182   /* Scheduled the task to clean up when shutdown is called */
3183   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3184                                 NULL);
3185
3186   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3187 }
3188
3189 /**
3190  * The main function for the mesh service.
3191  *
3192  * @param argc number of arguments from the command line
3193  * @param argv command line arguments
3194  * @return 0 ok, 1 on error
3195  */
3196 int
3197 main (int argc, char *const *argv)
3198 {
3199   int ret;
3200
3201 #if MESH_DEBUG
3202   fprintf (stderr, "main ()\n");
3203 #endif
3204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3205   ret =
3206       (GNUNET_OK ==
3207        GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3208                            NULL)) ? 0 : 1;
3209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3210 #if MESH_DEBUG
3211   fprintf (stderr, "main () END\n");
3212 #endif
3213   return ret;
3214 }