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