- move all core/PeerIdentity interacting code to _peer, remove GNUNET_PeerIdentity...
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_peer.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 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 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #include "gnunet_core_service.h"
26
27 #include "gnunet-service-mesh_peer.h"
28 #include "gnunet-service-mesh_dht.h"
29 #include "gnunet-service-mesh_connection.h"
30 #include "gnunet-service-mesh_local.h"
31 #include "mesh_path.h"
32
33 /******************************************************************************/
34 /********************************   STRUCTS  **********************************/
35 /******************************************************************************/
36
37 /**
38  * Struct containing all information regarding a given peer
39  */
40 struct MeshPeer
41 {
42     /**
43      * ID of the peer
44      */
45   GNUNET_PEER_Id id;
46
47     /**
48      * Last time we heard from this peer
49      */
50   struct GNUNET_TIME_Absolute last_contact;
51
52     /**
53      * Paths to reach the peer, ordered by ascending hop count
54      */
55   struct MeshPeerPath *path_head;
56
57     /**
58      * Paths to reach the peer, ordered by ascending hop count
59      */
60   struct MeshPeerPath *path_tail;
61
62     /**
63      * Handle to stop the DHT search for paths to this peer
64      */
65   struct GMD_search_handle *search_h;
66
67     /**
68      * Tunnel to this peer, if any.
69      */
70   struct MeshTunnel2 *tunnel;
71
72     /**
73      * Connections that go through this peer, indexed by tid;
74      */
75   struct GNUNET_CONTAINER_MultiHashMap *connections;
76
77     /**
78      * Handle for queued transmissions
79      */
80   struct GNUNET_CORE_TransmitHandle *core_transmit;
81
82   /**
83    * Transmission queue to core DLL head
84    */
85   struct MeshPeerQueue *queue_head;
86
87   /**
88    * Transmission queue to core DLL tail
89    */
90   struct MeshPeerQueue *queue_tail;
91
92   /**
93    * How many messages are in the queue to this peer.
94    */
95   unsigned int queue_n;
96 };
97
98
99 /******************************************************************************/
100 /*******************************   GLOBALS  ***********************************/
101 /******************************************************************************/
102
103 /**
104  * Peers known, indexed by PeerIdentity (MeshPeer).
105  */
106 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
107
108 /**
109  * How many peers do we want to remember?
110  */
111 static unsigned long long max_peers;
112
113 /**
114  * Percentage of messages that will be dropped (for test purposes only).
115  */
116 static unsigned long long drop_percent;
117
118 /**
119  * Handle to communicate with core.
120  */
121 static struct GNUNET_CORE_Handle *core_handle;
122
123 /**
124  * Local peer own ID (full value).
125  */
126 const static struct GNUNET_PeerIdentity *my_full_id;
127
128 /******************************************************************************/
129 /***************************** CORE CALLBACKS *********************************/
130 /******************************************************************************/
131
132
133 /**
134  * Iterator to notify all connections of a broken link. Mark connections
135  * to destroy after all traffic has been sent.
136  *
137  * @param cls Closure (peer disconnected).
138  * @param key Current key code (peer id).
139  * @param value Value in the hash map (connection).
140  *
141  * @return GNUNET_YES if we should continue to iterate,
142  *         GNUNET_NO if not.
143  */
144 static int
145 notify_broken (void *cls,
146                const struct GNUNET_HashCode *key,
147                void *value)
148 {
149   struct MeshPeer *peer = cls;
150   struct MeshConnection *c = value;
151
152   GMC_notify_broken (c, peer, my_full_id);
153
154   return GNUNET_YES;
155 }
156
157 /**
158  * Method called whenever a given peer connects.
159  *
160  * @param cls closure
161  * @param peer peer identity this notification is about
162  */
163 static void
164 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
165 {
166   struct MeshPeer *pi;
167   struct MeshPeerPath *path;
168
169   LOG ("Peer connected\n");
170   LOG ("     %s\n", GNUNET_i2s (&my_full_id));
171   pi = peer_get (peer);
172   if (myid == pi->id)
173   {
174     DEBUG_CONN ("     (self)\n");
175     path = path_new (1);
176   }
177   else
178   {
179     DEBUG_CONN ("     %s\n", GNUNET_i2s (peer));
180     path = path_new (2);
181     path->peers[1] = pi->id;
182     GNUNET_PEER_change_rc (pi->id, 1);
183     GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
184   }
185   path->peers[0] = myid;
186   GNUNET_PEER_change_rc (myid, 1);
187   peer_add_path (pi, path, GNUNET_YES);
188
189   pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
190   return;
191 }
192
193
194 /**
195  * Method called whenever a peer disconnects.
196  *
197  * @param cls closure
198  * @param peer peer identity this notification is about
199  */
200 static void
201 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
202 {
203   struct MeshPeer *pi;
204
205   DEBUG_CONN ("Peer disconnected\n");
206   pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
207   if (NULL == pi)
208   {
209     GNUNET_break (0);
210     return;
211   }
212
213   GNUNET_CONTAINER_multihashmap_iterate (pi->connections, &notify_broken, pi);
214   GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
215   pi->connections = NULL;
216   if (NULL != pi->core_transmit)
217     {
218       GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit);
219       pi->core_transmit = NULL;
220     }
221   if (myid == pi->id)
222   {
223     DEBUG_CONN ("     (self)\n");
224   }
225   GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
226
227   return;
228 }
229
230
231
232 /**
233  * To be called on core init/fail.
234  *
235  * @param cls Closure (config)
236  * @param identity the public identity of this peer
237  */
238 static void
239 core_init (void *cls,
240            const struct GNUNET_PeerIdentity *identity)
241 {
242   const struct GNUNET_CONFIGURATION_Handle *c = cls;
243   static int i = 0;
244
245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
246   if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
247   {
248     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250                 " core id %s\n",
251                 GNUNET_i2s (identity));
252     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253                 " my id %s\n",
254                 GNUNET_i2s (&my_full_id));
255     GNUNET_CORE_disconnect (core_handle);
256     core_handle = GNUNET_CORE_connect (c, /* Main configuration */
257                                        NULL,      /* Closure passed to MESH functions */
258                                        &core_init,        /* Call core_init once connected */
259                                        &core_connect,     /* Handle connects */
260                                        &core_disconnect,  /* remove peers on disconnects */
261                                        NULL,      /* Don't notify about all incoming messages */
262                                        GNUNET_NO, /* For header only in notification */
263                                        NULL,      /* Don't notify about all outbound messages */
264                                        GNUNET_NO, /* For header-only out notification */
265                                        core_handlers);    /* Register these handlers */
266     if (10 < i++)
267       GNUNET_abort();
268   }
269   GML_start ();
270   return;
271 }
272
273 /**
274   * Core callback to write a pre-constructed data packet to core buffer
275   *
276   * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
277   * @param size Number of bytes available in buf.
278   * @param buf Where the to write the message.
279   *
280   * @return number of bytes written to buf
281   */
282 static size_t
283 send_core_data_raw (void *cls, size_t size, void *buf)
284 {
285   struct GNUNET_MessageHeader *msg = cls;
286   size_t total_size;
287
288   GNUNET_assert (NULL != msg);
289   total_size = ntohs (msg->size);
290
291   if (total_size > size)
292   {
293     GNUNET_break (0);
294     return 0;
295   }
296   memcpy (buf, msg, total_size);
297   GNUNET_free (cls);
298   return total_size;
299 }
300
301
302 /**
303  * Function to send a create connection message to a peer.
304  *
305  * @param c Connection to create.
306  * @param size number of bytes available in buf
307  * @param buf where the callee should write the message
308  * @return number of bytes written to buf
309  */
310 static size_t
311 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
312 {
313   struct GNUNET_MESH_ConnectionCreate *msg;
314   struct GNUNET_PeerIdentity *peer_ptr;
315   struct MeshPeerPath *p = c->path;
316   size_t size_needed;
317   int i;
318
319   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
320   size_needed =
321       sizeof (struct GNUNET_MESH_ConnectionCreate) +
322       p->length * sizeof (struct GNUNET_PeerIdentity);
323
324   if (size < size_needed || NULL == buf)
325   {
326     GNUNET_break (0);
327     return 0;
328   }
329   msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
330   msg->header.size = htons (size_needed);
331   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
332   msg->cid = c->id;
333
334   peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
335   for (i = 0; i < p->length; i++)
336   {
337     GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
338   }
339
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341               "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
342   return size_needed;
343 }
344
345
346 /**
347  * Creates a path ack message in buf and frees all unused resources.
348  *
349  * @param c Connection to send an ACK on.
350  * @param size number of bytes available in buf
351  * @param buf where the callee should write the message
352  *
353  * @return number of bytes written to buf
354  */
355 static size_t
356 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
357 {
358   struct GNUNET_MESH_ConnectionACK *msg = buf;
359   struct MeshTunnel2 *t = c->t;
360
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
362   GNUNET_assert (NULL != t);
363   if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
364   {
365     GNUNET_break (0);
366     return 0;
367   }
368   msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
369   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
370   msg->cid = c->id;
371   msg->reserved = 0;
372
373   /* TODO add signature */
374
375   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
376   return sizeof (struct GNUNET_MESH_ConnectionACK);
377 }
378
379
380 /******************************************************************************/
381 /********************************   STATIC  ***********************************/
382 /******************************************************************************/
383
384 /**
385  * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
386  *
387  * @param cls closure
388  * @param key current key code
389  * @param value value in the hash map
390  * @return #GNUNET_YES if we should continue to iterate,
391  *         #GNUNET_NO if not.
392  */
393 static int
394 shutdown_tunnel (void *cls,
395                  const struct GNUNET_PeerIdentity *key,
396                  void *value)
397 {
398   struct MeshPeer *p = value;
399   struct MeshTunnel2 *t = p->tunnel;
400
401   if (NULL != t)
402     GMT_destroy (t);
403   return GNUNET_YES;
404 }
405
406
407
408 /**
409  * Destroy the peer_info and free any allocated resources linked to it
410  *
411  * @param peer The peer_info to destroy.
412  *
413  * @return GNUNET_OK on success
414  */
415 static int
416 peer_destroy (struct MeshPeer *peer)
417 {
418   struct GNUNET_PeerIdentity id;
419   struct MeshPeerPath *p;
420   struct MeshPeerPath *nextp;
421
422   GNUNET_PEER_resolve (peer->id, &id);
423   GNUNET_PEER_change_rc (peer->id, -1);
424
425   if (GNUNET_YES !=
426     GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
427   {
428     GNUNET_break (0);
429     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
430                 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
431   }
432     if (NULL != peer->search_h)
433     {
434       GMD_search_stop (peer->search_h);
435     }
436       p = peer->path_head;
437       while (NULL != p)
438       {
439         nextp = p->next;
440         GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
441         path_destroy (p);
442         p = nextp;
443       }
444         tunnel_destroy_empty (peer->tunnel);
445         GNUNET_free (peer);
446         return GNUNET_OK;
447 }
448
449
450 /**
451  * Returns if peer is used (has a tunnel, is neighbor).
452  *
453  * @peer Peer to check.
454  *
455  * @return GNUNET_YES if peer is in use.
456  */
457 static int
458 peer_is_used (struct MeshPeer *peer)
459 {
460   struct MeshPeerPath *p;
461
462   if (NULL != peer->tunnel)
463     return GNUNET_YES;
464
465   for (p = peer->path_head; NULL != p; p = p->next)
466   {
467     if (p->length < 3)
468       return GNUNET_YES;
469   }
470     return GNUNET_NO;
471 }
472
473
474 /**
475  * Iterator over all the peers to get the oldest timestamp.
476  *
477  * @param cls Closure (unsued).
478  * @param key ID of the peer.
479  * @param value Peer_Info of the peer.
480  */
481 static int
482 peer_get_oldest (void *cls,
483                  const struct GNUNET_PeerIdentity *key,
484                  void *value)
485 {
486   struct MeshPeer *p = value;
487   struct GNUNET_TIME_Absolute *abs = cls;
488
489   /* Don't count active peers */
490   if (GNUNET_YES == peer_is_used (p))
491     return GNUNET_YES;
492
493   if (abs->abs_value_us < p->last_contact.abs_value_us)
494     abs->abs_value_us = p->last_contact.abs_value_us;
495
496   return GNUNET_YES;
497 }
498
499
500 /**
501  * Iterator over all the peers to remove the oldest entry.
502  *
503  * @param cls Closure (unsued).
504  * @param key ID of the peer.
505  * @param value Peer_Info of the peer.
506  */
507 static int
508 peer_timeout (void *cls,
509               const struct GNUNET_PeerIdentity *key,
510               void *value)
511 {
512   struct MeshPeer *p = value;
513   struct GNUNET_TIME_Absolute *abs = cls;
514
515   if (p->last_contact.abs_value_us == abs->abs_value_us &&
516     GNUNET_NO == peer_is_used (p))
517   {
518     peer_destroy (p);
519     return GNUNET_NO;
520   }
521     return GNUNET_YES;
522 }
523
524
525 /**
526  * Delete oldest unused peer.
527  */
528 static void
529 peer_delete_oldest (void)
530 {
531   struct GNUNET_TIME_Absolute abs;
532
533   abs = GNUNET_TIME_UNIT_FOREVER_ABS;
534
535   GNUNET_CONTAINER_multipeermap_iterate (peers,
536                                          &peer_get_oldest,
537                                          &abs);
538   GNUNET_CONTAINER_multipeermap_iterate (peers,
539                                          &peer_timeout,
540                                          &abs);
541 }
542
543
544 /**
545  * Retrieve the MeshPeer stucture associated with the peer, create one
546  * and insert it in the appropriate structures if the peer is not known yet.
547  *
548  * @param peer Full identity of the peer.
549  *
550  * @return Existing or newly created peer info.
551  */
552 static struct MeshPeer *
553 peer_get (const struct GNUNET_PeerIdentity *peer_id)
554 {
555   struct MeshPeer *peer;
556
557   peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
558   if (NULL == peer)
559   {
560     peer = GNUNET_new (struct MeshPeer);
561     if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
562     {
563       peer_delete_oldest ();
564     }
565         GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
566                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
567         peer->id = GNUNET_PEER_intern (peer_id);
568   }
569     peer->last_contact = GNUNET_TIME_absolute_get();
570
571     return peer;
572 }
573
574
575 /**
576  * Retrieve the MeshPeer stucture associated with the peer, create one
577  * and insert it in the appropriate structures if the peer is not known yet.
578  *
579  * @param peer Short identity of the peer.
580  *
581  * @return Existing or newly created peer info.
582  */
583 static struct MeshPeer *
584 peer_get_short (const GNUNET_PEER_Id peer)
585 {
586   return peer_get (GNUNET_PEER_resolve2 (peer));
587 }
588
589
590 /**
591  * Get a cost of a path for a peer considering existing tunnel connections.
592  *
593  * @param peer Peer towards which the path is considered.
594  * @param path Candidate path.
595  *
596  * @return Cost of the path (path length + number of overlapping nodes)
597  */
598 static unsigned int
599 peer_get_path_cost (const struct MeshPeer *peer,
600                     const struct MeshPeerPath *path)
601 {
602   struct MeshConnection *c;
603   unsigned int overlap;
604   unsigned int i;
605   unsigned int j;
606
607   if (NULL == path)
608     return 0;
609
610   overlap = 0;
611   GNUNET_assert (NULL != peer->tunnel);
612
613   for (i = 0; i < path->length; i++)
614   {
615     for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
616     {
617       for (j = 0; j < c->path->length; j++)
618       {
619         if (path->peers[i] == c->path->peers[j])
620         {
621           overlap++;
622           break;
623         }
624       }
625     }
626   }
627   return (path->length + overlap) * (path->score * -1);
628 }
629
630
631 /**
632  * Choose the best path towards a peer considering the tunnel properties.
633  *
634  * @param peer The destination peer.
635  *
636  * @return Best current known path towards the peer, if any.
637  */
638 static struct MeshPeerPath *
639 peer_get_best_path (const struct MeshPeer *peer)
640 {
641   struct MeshPeerPath *best_p;
642   struct MeshPeerPath *p;
643   struct MeshConnection *c;
644   unsigned int best_cost;
645   unsigned int cost;
646
647   best_cost = UINT_MAX;
648   best_p = NULL;
649   for (p = peer->path_head; NULL != p; p = p->next)
650   {
651     for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
652       if (c->path == p)
653         break;
654       if (NULL != c)
655         continue; /* If path is in use in a connection, skip it. */
656
657             if ((cost = peer_get_path_cost (peer, p)) < best_cost)
658             {
659               best_cost = cost;
660               best_p = p;
661             }
662   }
663     return best_p;
664 }
665
666
667 /**
668  * Add the path to the peer and update the path used to reach it in case this
669  * is the shortest.
670  *
671  * @param peer_info Destination peer to add the path to.
672  * @param path New path to add. Last peer must be the peer in arg 1.
673  *             Path will be either used of freed if already known.
674  * @param trusted Do we trust that this path is real?
675  */
676 void
677 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
678                int trusted)
679 {
680   struct MeshPeerPath *aux;
681   unsigned int l;
682   unsigned int l2;
683
684   if ((NULL == peer_info) || (NULL == path))
685   {
686     GNUNET_break (0);
687     path_destroy (path);
688     return;
689   }
690     if (path->peers[path->length - 1] != peer_info->id)
691     {
692       GNUNET_break (0);
693       path_destroy (path);
694       return;
695     }
696       if (2 >= path->length && GNUNET_NO == trusted)
697       {
698         /* Only allow CORE to tell us about direct paths */
699         path_destroy (path);
700         return;
701       }
702         for (l = 1; l < path->length; l++)
703         {
704           if (path->peers[l] == myid)
705           {
706             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
707             for (l2 = 0; l2 < path->length - l; l2++)
708             {
709               path->peers[l2] = path->peers[l + l2];
710             }
711                   path->length -= l;
712                   l = 1;
713                   path->peers =
714                             GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
715           }
716         }
717
718           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
719                       path->length, peer2s (peer_info));
720
721           l = path_get_length (path);
722           if (0 == l)
723           {
724             path_destroy (path);
725             return;
726           }
727
728             GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
729             for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
730             {
731               l2 = path_get_length (aux);
732               if (l2 > l)
733               {
734                 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
735                                                     peer_info->path_tail, aux, path);
736                 return;
737               }
738                   else
739                   {
740                     if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
741                     {
742                       path_destroy (path);
743                       return;
744                     }
745                   }
746             }
747               GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
748                                                 path);
749               return;
750 }
751
752
753 /**
754  * Add the path to the origin peer and update the path used to reach it in case
755  * this is the shortest.
756  * The path is given in peer_info -> destination, therefore we turn the path
757  * upside down first.
758  *
759  * @param peer_info Peer to add the path to, being the origin of the path.
760  * @param path New path to add after being inversed.
761  *             Path will be either used or freed.
762  * @param trusted Do we trust that this path is real?
763  */
764 static void
765 peer_add_path_to_origin (struct MeshPeer *peer_info,
766                          struct MeshPeerPath *path, int trusted)
767 {
768   if (NULL == path)
769     return;
770   path_invert (path);
771   peer_add_path (peer_info, path, trusted);
772 }
773
774
775 /**
776  * Adds a path to the peer_infos of all the peers in the path
777  *
778  * @param p Path to process.
779  * @param confirmed Whether we know if the path works or not.
780  */
781 static void
782 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
783 {
784   unsigned int i;
785
786   /* TODO: invert and add */
787   for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
788   for (i++; i < p->length; i++)
789   {
790     struct MeshPeer *aux;
791     struct MeshPeerPath *copy;
792     
793     aux = peer_get_short (p->peers[i]);
794     copy = path_duplicate (p);
795     copy->length = i + 1;
796     peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
797   }
798 }
799
800
801 /**
802  * Function to process paths received for a new peer addition. The recorded
803  * paths form the initial tunnel, which can be optimized later.
804  * Called on each result obtained for the DHT search.
805  *
806  * @param cls closure
807  * @param path
808  */
809 static void
810 search_handler (void *cls, struct MeshPeerPath *path)
811 {
812   struct MeshPeer *peer = cls;
813   unsigned int connection_count;
814
815   path_add_to_peers (path, GNUNET_NO);
816
817   /* Count connections */
818   connection_count = GMC_count (peer->tunnel->connection_head);
819
820   /* If we already have 3 (or more (?!)) connections, it's enough */
821   if (3 <= connection_count)
822     return;
823
824   if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
825   {
826     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
827     GMP_connect (peer);
828   }
829   return;
830 }
831
832
833 /**
834  * Core callback to write a queued packet to core buffer
835  *
836  * @param cls Closure (peer info).
837  * @param size Number of bytes available in buf.
838  * @param buf Where the to write the message.
839  *
840  * @return number of bytes written to buf
841  */
842 static size_t
843 queue_send (void *cls, size_t size, void *buf)
844 {
845   struct MeshPeer *peer = cls;
846   struct MeshFlowControl *fc;
847   struct MeshConnection *c;
848   struct GNUNET_MessageHeader *msg;
849   struct MeshPeerQueue *queue;
850   struct MeshTunnel2 *t;
851   struct MeshChannel *ch;
852   const struct GNUNET_PeerIdentity *dst_id;
853   size_t data_size;
854   uint32_t pid;
855   uint16_t type;
856   int fwd;
857
858   peer->core_transmit = NULL;
859   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
860
861   if (NULL == buf || 0 == size)
862   {
863     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
864     return 0;
865   }
866
867   /* Initialize */
868   queue = peer_get_first_message (peer);
869   if (NULL == queue)
870   {
871     GNUNET_break (0); /* Core tmt_rdy should've been canceled */
872     return 0;
873   }
874   c = queue->c;
875   fwd = queue->fwd;
876   fc = fwd ? &c->fwd_fc : &c->bck_fc;
877
878   dst_id = GNUNET_PEER_resolve2 (peer->id);
879   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   towards %s\n", GNUNET_i2s (dst_id));
880   /* Check if buffer size is enough for the message */
881   if (queue->size > size)
882   {
883       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   not enough room, reissue\n");
884       peer->core_transmit =
885           GNUNET_CORE_notify_transmit_ready (core_handle,
886                                              GNUNET_NO,
887                                              0,
888                                              GNUNET_TIME_UNIT_FOREVER_REL,
889                                              dst_id,
890                                              queue->size,
891                                              &queue_send,
892                                              peer);
893       return 0;
894   }
895   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   size %u ok\n", queue->size);
896
897   t = (NULL != c) ? c->t : NULL;
898   type = 0;
899
900   /* Fill buf */
901   switch (queue->type)
902   {
903     case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
904     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
905     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
906     case GNUNET_MESSAGE_TYPE_MESH_FWD:
907     case GNUNET_MESSAGE_TYPE_MESH_BCK:
908     case GNUNET_MESSAGE_TYPE_MESH_ACK:
909     case GNUNET_MESSAGE_TYPE_MESH_POLL:
910       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911                   "*   raw: %s\n",
912                   GNUNET_MESH_DEBUG_M2S (queue->type));
913       data_size = send_core_data_raw (queue->cls, size, buf);
914       msg = (struct GNUNET_MessageHeader *) buf;
915       type = ntohs (msg->type);
916       break;
917     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
918       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   path create\n");
919       if (GMC_is_origin (c, GNUNET_YES))
920         data_size = send_core_connection_create (queue->c, size, buf);
921       else
922         data_size = send_core_data_raw (queue->cls, size, buf);
923       break;
924     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
925       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   path ack\n");
926       if (GMC_is_origin (c, GNUNET_NO) ||
927           GMC_is_origin (c, GNUNET_YES))
928         data_size = send_core_connection_ack (queue->c, size, buf);
929       else
930         data_size = send_core_data_raw (queue->cls, size, buf);
931       break;
932     case GNUNET_MESSAGE_TYPE_MESH_DATA:
933     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
934     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
935       /* This should be encapsulted */
936       GNUNET_break (0);
937       data_size = 0;
938       break;
939     default:
940       GNUNET_break (0);
941       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "*   type unknown: %u\n",
942                   queue->type);
943       data_size = 0;
944   }
945
946   if (0 < drop_percent &&
947       GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
948   {
949     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
950                 "Dropping message of type %s\n",
951                 GNUNET_MESH_DEBUG_M2S (queue->type));
952     data_size = 0;
953   }
954
955   /* Free queue, but cls was freed by send_core_* */
956   ch = queue->ch;
957   queue_destroy (queue, GNUNET_NO);
958
959   /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
960   switch (type)
961   {
962     case GNUNET_MESSAGE_TYPE_MESH_FWD:
963     case GNUNET_MESSAGE_TYPE_MESH_BCK:
964       pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
965       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   accounting pid %u\n", pid);
966       fc->last_pid_sent = pid;
967       send_ack (c, ch, fwd);
968       break;
969     default:
970       break;
971   }
972
973   /* If more data in queue, send next */
974   queue = peer_get_first_message (peer);
975   if (NULL != queue)
976   {
977     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   more data!\n");
978     if (NULL == peer->core_transmit) {
979       peer->core_transmit =
980           GNUNET_CORE_notify_transmit_ready(core_handle,
981                                             0,
982                                             0,
983                                             GNUNET_TIME_UNIT_FOREVER_REL,
984                                             dst_id,
985                                             queue->size,
986                                             &queue_send,
987                                             peer);
988     }
989     else
990     {
991       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992                   "*   tmt rdy called somewhere else\n");
993     }
994     if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
995     {
996       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   starting poll timeout\n");
997       fc->poll_task =
998           GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
999     }
1000   }
1001   else
1002   {
1003     if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1004     {
1005       GNUNET_SCHEDULER_cancel (fc->poll_task);
1006       fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1007     }
1008   }
1009   if (NULL != c)
1010   {
1011     c->pending_messages--;
1012     if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
1013     {
1014       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*  destroying connection!\n");
1015       GMC_destroy (c);
1016     }
1017   }
1018
1019   if (NULL != t)
1020   {
1021     t->pending_messages--;
1022     if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
1023     {
1024 //       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*  destroying tunnel!\n");
1025       tunnel_destroy (t);
1026     }
1027   }
1028   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*  Return %d\n", data_size);
1029   return data_size;
1030 }
1031
1032
1033
1034 /**
1035  * Get first sendable message.
1036  *
1037  * @param peer The destination peer.
1038  *
1039  * @return Best current known path towards the peer, if any.
1040  */
1041 static struct MeshPeerQueue *
1042 peer_get_first_message (const struct MeshPeer *peer)
1043 {
1044   struct MeshPeerQueue *q;
1045
1046   for (q = peer->queue_head; NULL != q; q = q->next)
1047   {
1048     if (queue_is_sendable (q))
1049       return q;
1050   }
1051
1052   return NULL;
1053 }
1054
1055
1056 static int
1057 queue_is_sendable (struct MeshPeerQueue *q)
1058 {
1059   struct MeshFlowControl *fc;
1060
1061   /* Is PID-independent? */
1062   switch (q->type)
1063   {
1064     case GNUNET_MESSAGE_TYPE_MESH_ACK:
1065     case GNUNET_MESSAGE_TYPE_MESH_POLL:
1066       return GNUNET_YES;
1067   }
1068
1069   /* Is PID allowed? */
1070   fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
1071   if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1072     return GNUNET_YES;
1073
1074   return GNUNET_NO;
1075 }
1076
1077
1078 /******************************************************************************/
1079 /********************************    API    ***********************************/
1080 /******************************************************************************/
1081
1082
1083 /**
1084  * Free a transmission that was already queued with all resources
1085  * associated to the request.
1086  *
1087  * @param queue Queue handler to cancel.
1088  * @param clear_cls Is it necessary to free associated cls?
1089  */
1090 void
1091 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
1092 {
1093   struct MeshPeer *peer;
1094   struct MeshFlowControl *fc;
1095   int fwd;
1096
1097   fwd = queue->fwd;
1098   peer = queue->peer;
1099   GNUNET_assert (NULL != queue->c);
1100   fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
1101
1102   if (GNUNET_YES == clear_cls)
1103   {
1104     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   queue destroy type %s\n",
1105                 GNUNET_MESH_DEBUG_M2S (queue->type));
1106     switch (queue->type)
1107     {
1108       case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1109       case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1110         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1111         GNUNET_break (GNUNET_YES == queue->c->destroy);
1112         /* fall through */
1113       case GNUNET_MESSAGE_TYPE_MESH_FWD:
1114       case GNUNET_MESSAGE_TYPE_MESH_BCK:
1115       case GNUNET_MESSAGE_TYPE_MESH_ACK:
1116       case GNUNET_MESSAGE_TYPE_MESH_POLL:
1117       case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1118       case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1119       case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1120         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   prebuilt message\n");;
1121         GNUNET_free_non_null (queue->cls);
1122         break;
1123
1124       default:
1125         GNUNET_break (0);
1126         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "   type %s unknown!\n",
1127                     GNUNET_MESH_DEBUG_M2S (queue->type));
1128     }
1129
1130   }
1131   GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1132
1133   if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
1134       queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
1135   {
1136     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Q_N- %p %u\n", fc, fc->queue_n);
1137     fc->queue_n--;
1138     peer->queue_n--;
1139   }
1140   if (NULL != queue->c)
1141   {
1142     queue->c->pending_messages--;
1143     if (NULL != queue->c->t)
1144     {
1145       queue->c->t->pending_messages--;
1146     }
1147   }
1148
1149   GNUNET_free (queue);
1150 }
1151
1152
1153 /**
1154  * @brief Queue and pass message to core when possible.
1155  *
1156  * @param cls Closure (@c type dependant). It will be used by queue_send to
1157  *            build the message to be sent if not already prebuilt.
1158  * @param type Type of the message, 0 for a raw message.
1159  * @param size Size of the message.
1160  * @param c Connection this message belongs to (cannot be NULL).
1161  * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1162  * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1163  */
1164 void
1165 GMP_queue_add (void *cls, uint16_t type, size_t size, 
1166                struct MeshConnection *c,
1167                struct MeshChannel *ch,
1168                int fwd)
1169 {
1170   struct MeshPeerQueue *queue;
1171   struct MeshFlowControl *fc;
1172   struct MeshPeer *peer;
1173   int priority;
1174   int call_core;
1175
1176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177               "queue add %s %s (%u) on c %p, ch %p\n",
1178               fwd ? "FWD" : "BCK",  GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
1179   GNUNET_assert (NULL != c);
1180
1181   fc   = fwd ? &c->fwd_fc : &c->bck_fc;
1182   peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1183
1184   if (NULL == fc)
1185   {
1186     GNUNET_break (0);
1187     return;
1188   }
1189
1190   if (NULL == peer->connections)
1191   {
1192     /* We are not connected to this peer, ignore request. */
1193     GNUNET_break_op (0);
1194     return;
1195   }
1196
1197   priority = 0;
1198
1199   if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1200       GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1201   {
1202     priority = 100;
1203   }
1204
1205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
1207   if (fc->queue_n >= fc->queue_max && 0 == priority)
1208   {
1209     GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
1210                               1, GNUNET_NO);
1211     GNUNET_break (0);
1212     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213                 "queue full: %u/%u\n",
1214                 fc->queue_n, fc->queue_max);
1215     return; /* Drop this message */
1216   }
1217
1218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
1219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "     ack %u\n", fc->last_ack_recv);
1220   if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
1221   {
1222     call_core = GNUNET_NO;
1223     if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
1224         GNUNET_MESSAGE_TYPE_MESH_POLL != type)
1225     {
1226       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227                   "no buffer space (%u > %u): starting poll\n",
1228                   fc->last_pid_sent + 1, fc->last_ack_recv);
1229       fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1230                                                     &connection_poll,
1231                                                     fc);
1232     }
1233   }
1234   else
1235     call_core = GNUNET_YES;
1236   queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1237   queue->cls = cls;
1238   queue->type = type;
1239   queue->size = size;
1240   queue->peer = peer;
1241   queue->c = c;
1242   queue->ch = ch;
1243   queue->fwd = fwd;
1244   if (100 <= priority)
1245   {
1246     struct MeshPeerQueue *copy;
1247     struct MeshPeerQueue *next;
1248
1249     for (copy = peer->queue_head; NULL != copy; copy = next)
1250     {
1251       next = copy->next;
1252       if (copy->type == type && copy->c == c && copy->fwd == fwd)
1253       {
1254         /* Example: also a FWD ACK for connection XYZ */
1255         queue_destroy (copy, GNUNET_YES);
1256       }
1257     }
1258     GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1259   }
1260   else
1261   {
1262     GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1263     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Q_N+ %p %u\n", fc, fc->queue_n);
1264     fc->queue_n++;
1265     peer->queue_n++;
1266   }
1267
1268   if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1269   {
1270     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271                 "calling core tmt rdy towards %s for %u bytes\n",
1272                 peer2s (peer), size);
1273     peer->core_transmit =
1274         GNUNET_CORE_notify_transmit_ready (core_handle,
1275                                            0,
1276                                            0,
1277                                            GNUNET_TIME_UNIT_FOREVER_REL,
1278                                            GNUNET_PEER_resolve2 (peer->id),
1279                                            size,
1280                                            &queue_send,
1281                                            peer);
1282   }
1283   else
1284   {
1285     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286                 "core tmt rdy towards %s already called\n",
1287                 peer2s (peer));
1288
1289   }
1290   c->pending_messages++;
1291   if (NULL != c->t)
1292     c->t->pending_messages++;
1293 }
1294
1295
1296
1297 /**
1298  * Initialize the peer subsystem.
1299  *
1300  * @param c Configuration.
1301  * @param id Peer identity
1302  */
1303 void
1304 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c,
1305           const struct GNUNET_PeerIdentity *id)
1306 {
1307   my_full_id = id;
1308   peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1309   if (GNUNET_OK !=
1310       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1311                                              &max_peers))
1312   {
1313     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1314                                "MESH", "MAX_PEERS", "USING DEFAULT");
1315     max_peers = 1000;
1316   }
1317
1318   if (GNUNET_OK !=
1319       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1320                                              &drop_percent))
1321   {
1322     drop_percent = 0;
1323   }
1324   else
1325   {
1326     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1327                 "\n***************************************\n"
1328                 "Mesh is running with drop mode enabled.\n"
1329                 "This is NOT a good idea!\n"
1330                 "Remove the DROP_PERCENT option from your configuration.\n"
1331                 "***************************************\n");
1332   }
1333
1334   core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1335                                      NULL,      /* Closure passed to MESH functions */
1336                                      &core_init,        /* Call core_init once connected */
1337                                      &core_connect,     /* Handle connects */
1338                                      &core_disconnect,  /* remove peers on disconnects */
1339                                      NULL,      /* Don't notify about all incoming messages */
1340                                      GNUNET_NO, /* For header only in notification */
1341                                      NULL,      /* Don't notify about all outbound messages */
1342                                      GNUNET_NO, /* For header-only out notification */
1343                                      core_handlers);    /* Register these handlers */
1344   if (NULL == core_handle)
1345   {
1346     GNUNET_break (0);
1347     GNUNET_SCHEDULER_shutdown ();
1348     return;
1349   }
1350 }
1351
1352 /**
1353  * Shut down the peer subsystem.
1354  */
1355 void
1356 GMP_shutdown (void)
1357 {
1358   GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1359 }
1360
1361
1362 /**
1363  * Try to establish a new connection to this peer in the given tunnel.
1364  * If the peer doesn't have any path to it yet, try to get one.
1365  * If the peer already has some path, send a CREATE CONNECTION towards it.
1366  *
1367  * @param peer PeerInfo of the peer.
1368  */
1369 void
1370 GMP_connect (struct MeshPeer *peer)
1371 {
1372   struct MeshTunnel2 *t;
1373   struct MeshPeerPath *p;
1374   struct MeshConnection *c;
1375   int rerun_search;
1376
1377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378               "peer_connect towards %s\n",
1379               peer2s (peer));
1380   t = peer->tunnel;
1381   c = NULL;
1382   rerun_search = GNUNET_NO;
1383
1384   if (NULL != peer->path_head)
1385   {
1386     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1387     p = peer_get_best_path (peer);
1388     if (NULL != p)
1389     {
1390       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %u hops\n", p->length);
1391       c = tunnel_use_path (t, p);
1392       if (NULL == c)
1393       {
1394         /* This case can happen when the path includes a first hop that is
1395          * not yet known to be connected.
1396          *
1397          * This happens quite often during testing when running mesh
1398          * under valgrind: core connect notifications come very late and the
1399          * DHT result has already come and created a valid path.
1400          * In this case, the peer->connections hashmap will be NULL and
1401          * tunnel_use_path will not be able to create a connection from that
1402          * path.
1403          *
1404          * Re-running the DHT GET should give core time to callback.
1405          */
1406         GNUNET_break(0);
1407                 rerun_search = GNUNET_YES;
1408       }
1409       else
1410       {
1411         send_connection_create (c);
1412         return;
1413       }
1414     }
1415   }
1416
1417   if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1418   {
1419     GMD_search_stop (peer->search_h);
1420     peer->search_h = NULL;
1421     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422                 "  Stopping DHT GET for peer %s\n", peer2s (peer));
1423   }
1424
1425   if (NULL == peer->search_h)
1426   {
1427     const struct GNUNET_PeerIdentity *id;
1428
1429     id = GNUNET_PEER_resolve2 (peer->id);
1430     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431                 "  Starting DHT GET for peer %s\n", peer2s (peer));
1432     peer->search_h = GMD_search (id, &search_handler, peer);
1433     if (MESH_TUNNEL_NEW == t->state)
1434       tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
1435   }
1436 }
1437
1438 /**
1439  * Get the static string for a peer ID.
1440  *
1441  * @param peer Peer.
1442  *
1443  * @return Static string for it's ID.
1444  */
1445 const char *
1446 GMP_2s (const struct MeshPeer *peer)
1447 {
1448   if (NULL == peer)
1449     return "(NULL)";
1450   return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1451 }