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