tell transport to connect, not core
[oweals/gnunet.git] / src / mesh / mesh_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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/mesh_api.c
23  * @brief mesh service; API for the Mesh. This is used to talk to arbitrary peers
24  *        as of 2011-01Jan-06 this is a mockup.
25  * @author Philipp Tölke
26  */
27 #include <platform.h>
28 #include <gnunet_constants.h>
29 #include <gnunet_mesh_service.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_transport_service.h>
32 #include <gnunet_container_lib.h>
33 #include <gnunet_applications.h>
34
35 struct tunnel_id
36 {
37   uint32_t id GNUNET_PACKED;
38   struct GNUNET_PeerIdentity initiator;
39   struct GNUNET_PeerIdentity target;
40 };
41
42 static uint32_t current_id = 0;
43
44 struct tunnel_message
45 {
46   struct GNUNET_MessageHeader hdr;
47   struct tunnel_id id;
48   /* followed by another GNUNET_MessageHeader */
49 };
50
51 struct notify_cls
52 {
53   void *notify_cls;
54   GNUNET_CONNECTION_TransmitReadyNotify notify;
55   struct GNUNET_MESH_Tunnel *tunnel;
56 };
57
58 struct GNUNET_MESH_Tunnel
59 {
60   /* The other peer this tunnel leads to; just unicast for the moment! */
61   struct GNUNET_PeerIdentity peer;
62
63   struct tunnel_id id;
64
65   /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
66   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
67   GNUNET_MESH_TunnelConnectHandler connect_handler;
68   void *handler_cls;
69
70   struct GNUNET_MESH_Handle *handle;
71
72   /* The application-type requested for this tunnel. Is only needed for pending
73    * by_tupe-tunnels
74    */
75   uint16_t application_type;
76
77   struct GNUNET_MESH_TransmitHandle *notify_handle;
78
79   /* The context of the receive-function. */
80   void *ctx;
81
82   /* A list, usable by application-code (for queues) */
83   void *app_head;
84   void *app_tail;
85
86   /* A pointer, usable by application-code */
87   void *app_data;
88 };
89
90 struct tunnel_list_element
91 {
92   struct GNUNET_MESH_Tunnel tunnel;
93   struct tunnel_list_element *next, *prev;
94 };
95
96 struct tunnel_list
97 {
98   struct tunnel_list_element *head, *tail;
99 };
100
101 struct type_list_element
102 {
103   GNUNET_MESH_ApplicationType type;
104   struct type_list_element *next, *prev;
105 };
106
107 struct peer_list_element
108 {
109   struct GNUNET_PeerIdentity peer;
110
111   /* list of application-types */
112   struct type_list_element *type_head, *type_tail;
113
114   struct GNUNET_TRANSPORT_ATS_Information atsi;
115   struct peer_list_element *next, *prev;
116
117   /* The handle that sends the hellos to this peer */
118   struct GNUNET_CORE_TransmitHandle *hello;
119
120   GNUNET_SCHEDULER_TaskIdentifier sched;
121
122   struct GNUNET_MESH_Handle *handle;
123 };
124
125 struct peer_list
126 {
127   struct peer_list_element *head, *tail;
128 };
129
130 struct GNUNET_MESH_Handle
131 {
132   struct GNUNET_CORE_Handle *core;
133   struct GNUNET_TRANSPORT_Handle *transport;
134   struct GNUNET_MESH_MessageHandler *handlers;
135   struct GNUNET_PeerIdentity myself;
136   unsigned int connected_to_core;
137   struct peer_list connected_peers;
138   struct tunnel_list established_tunnels;
139   struct tunnel_list pending_tunnels;
140   struct tunnel_list pending_by_type_tunnels;
141   void *cls;
142   GNUNET_MESH_TunnelEndHandler *cleaner;
143   size_t hello_message_size;
144   uint16_t *hello_message;
145 };
146
147 static void
148 send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 {
150   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
151     return;
152
153   struct GNUNET_MESH_Tunnel *tunnel = cls;
154
155   tunnel->connect_handler (tunnel->handler_cls, NULL, NULL);
156 }
157
158 static void
159 send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
162     return;
163
164   struct GNUNET_MESH_Tunnel *tunnel = cls;
165
166   tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL);
167   GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
168 }
169
170 static void
171 call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 {
173   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
174     return;
175
176   struct GNUNET_MESH_Tunnel *tunnel = cls;
177
178   tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL);
179   GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
180 }
181
182 static void
183 core_startup (void *cls, struct GNUNET_CORE_Handle *core
184               __attribute__ ((unused)),
185               const struct GNUNET_PeerIdentity *my_identity,
186               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey
187               __attribute__ ((unused)))
188 {
189   struct GNUNET_MESH_Handle *handle = cls;
190
191   memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
192   handle->connected_to_core = GNUNET_YES;
193 }
194
195 static size_t
196 send_hello_message (void *cls, size_t size, void *buf)
197 {
198   if (cls == NULL)
199     return 0;
200
201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
202
203   struct peer_list_element *element = cls;
204   struct GNUNET_MESH_Handle *handle = element->handle;
205
206   element->hello = NULL;
207   struct GNUNET_MessageHeader *hdr = buf;
208
209   size_t sent =
210       sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size;
211
212   if (sent > size)
213     return 0;
214
215   hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO);
216   hdr->size = htons (size);
217
218   memcpy (hdr + 1, handle->hello_message, handle->hello_message_size);
219   return sent;
220 }
221
222 void
223 schedule_hello_message (void *cls,
224                         const struct GNUNET_SCHEDULER_TaskContext *tctx)
225 {
226   struct peer_list_element *element = cls;
227
228   element->sched = GNUNET_SCHEDULER_NO_TASK;
229
230   if ((tctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
231     return;
232
233   if (element->hello == NULL)
234     element->hello =
235         GNUNET_CORE_notify_transmit_ready (element->handle->core, GNUNET_NO, 42,
236                                            GNUNET_TIME_UNIT_SECONDS,
237                                            &element->peer,
238                                            sizeof (struct GNUNET_MessageHeader)
239                                            +
240                                            element->handle->hello_message_size,
241                                            &send_hello_message, element);
242
243   element->sched =
244       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
245                                     schedule_hello_message, cls);
246 }
247
248
249 /**
250  * Core calls this if we are connected to a new peer.
251  *
252  * The peer is added to the connected_peers-list.
253  *
254  */
255 static void
256 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
257               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
258 {
259   struct GNUNET_MESH_Handle *handle = cls;
260
261   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262               "Core tells us we are connected to peer %s\n", GNUNET_i2s (peer));
263
264   /* put the new peer into the list of connected peers */
265   struct peer_list_element *element =
266       GNUNET_malloc (sizeof (struct peer_list_element));
267   memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
268   element->handle = handle;
269
270   /* Send a hello to this peer */
271   element->sched = GNUNET_SCHEDULER_add_now (schedule_hello_message, element);
272
273   if (NULL != atsi)
274     memcpy (&element->atsi, atsi,
275             sizeof (struct GNUNET_TRANSPORT_ATS_Information));
276
277   GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
278                                      handle->connected_peers.tail,
279                                      handle->connected_peers.tail, element);
280
281   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
282
283   while (tunnel != NULL)
284   {
285     if (0 ==
286         memcmp (&tunnel->tunnel.peer, peer,
287                 sizeof (struct GNUNET_PeerIdentity)))
288     {
289       struct tunnel_list_element *next = tunnel->next;
290
291       GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
292                                    handle->pending_tunnels.tail, tunnel);
293       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
294                                          handle->established_tunnels.tail,
295                                          handle->established_tunnels.tail,
296                                          tunnel);
297       tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi);
298       GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
299       tunnel = next;
300     }
301     else
302       tunnel = tunnel->next;
303   }
304 }
305
306 /**
307  * Core calls this if we disconnect a peer
308  *
309  * Remove this peer from the list of connected peers
310  * Close all tunnels this peer belongs to
311  */
312 static void
313 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
314 {
315   struct GNUNET_MESH_Handle *handle = cls;
316
317   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318               "Core tells us we are no longer connected to peer %s\n",
319               GNUNET_i2s (peer));
320
321   struct peer_list_element *element = handle->connected_peers.head;
322
323   while (element != NULL)
324   {
325     if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
326       break;
327     element = element->next;
328   }
329   if (element != NULL)
330   {
331     GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
332                                  handle->connected_peers.tail, element);
333     while (element->type_head != NULL)
334     {
335       struct type_list_element *tail = element->type_tail;
336
337       GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
338                                    tail);
339       GNUNET_free (tail);
340     }
341     if (element->hello != NULL)
342       GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
343     GNUNET_SCHEDULER_cancel (element->sched);
344     GNUNET_free (element);
345   }
346
347   struct tunnel_list_element *telement = handle->established_tunnels.head;
348
349   while (telement != NULL)
350   {
351     if (0 ==
352         memcmp (&telement->tunnel.peer, peer,
353                 sizeof (struct GNUNET_PeerIdentity)))
354     {
355       /* disconnect tunnels */
356       /* outbound tunnels */
357       if (telement->tunnel.connect_handler != NULL &&
358           NULL != telement->tunnel.disconnect_handler)
359         telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
360                                              peer);
361       /* inbound tunnels */
362       else if (NULL != handle->cleaner)
363         handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx);
364
365       struct tunnel_list_element *next = telement->next;
366
367       GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
368                                    handle->established_tunnels.tail, telement);
369       GNUNET_free (telement);
370       telement = next;
371     }
372     else
373     {
374       telement = telement->next;
375     }
376   }
377 }
378
379 /**
380  * Receive a message from core.
381  * This is a hello-message, containing the application-types the other peer can receive
382  */
383 static int
384 receive_hello (void *cls, const struct GNUNET_PeerIdentity *other,
385                const struct GNUNET_MessageHeader *message,
386                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
387 {
388   struct GNUNET_MESH_Handle *handle = cls;
389   uint16_t *num = (uint16_t *) (message + 1);
390   GNUNET_MESH_ApplicationType *ports =
391       (GNUNET_MESH_ApplicationType *) (num + 1);
392   unsigned int i;
393
394   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395               "The peer %s tells us he supports %d application-types.\n",
396               GNUNET_i2s (other), ntohs (*num));
397
398   struct peer_list_element *element = handle->connected_peers.head;
399
400   while (element != NULL)
401   {
402     if (0 ==
403         memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
404       break;
405     element = element->next;
406   }
407
408   GNUNET_assert (NULL != element);
409
410   for (i = 0; i < ntohs (*num); i++)
411   {
412     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413                 "The peer %s newly supports the application-type %d\n",
414                 GNUNET_i2s (other), ntohs (ports[i]));
415     if (GNUNET_APPLICATION_TYPE_END == ntohs (ports[i]))
416       continue;
417     struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type);
418
419     new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]);
420     GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail,
421                                  new_type);
422   }
423
424   struct type_list_element *type;
425
426   for (type = element->type_head; type != NULL; type = type->next)
427   {
428     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429                 "The peer %s supports the application-type %d\n",
430                 GNUNET_i2s (other), type->type);
431   }
432
433   struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
434
435   while (tunnel != NULL)
436   {
437     struct tunnel_list_element *next = tunnel->next;
438
439     for (i = 0; i < ntohs (*num); i++)
440     {
441       if (ntohs (ports[i]) == tunnel->tunnel.application_type)
442       {
443         GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
444                                      handle->pending_by_type_tunnels.tail,
445                                      tunnel);
446         GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
447                                            handle->established_tunnels.tail,
448                                            handle->established_tunnels.tail,
449                                            tunnel);
450         memcpy (&tunnel->tunnel.peer, other,
451                 sizeof (struct GNUNET_PeerIdentity));
452         tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
453                                         &tunnel->tunnel.peer, atsi);
454         GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
455         break;
456       }
457     }
458     if (ntohs (ports[i]) == tunnel->tunnel.application_type)
459       tunnel = next;
460     else
461       tunnel = tunnel->next;
462   }
463   return GNUNET_OK;
464 }
465
466 /**
467  * Receive a message from core.
468  */
469 static int
470 core_receive (void *cls, const struct GNUNET_PeerIdentity *other,
471               const struct GNUNET_MessageHeader *message,
472               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
473 {
474   struct GNUNET_MESH_Handle *handle = cls;
475   struct tunnel_message *tmessage = (struct tunnel_message *) message;
476   struct GNUNET_MessageHeader *rmessage =
477       (struct GNUNET_MessageHeader *) (tmessage + 1);
478
479   struct GNUNET_MESH_MessageHandler *handler;
480
481   for (handler = handle->handlers; handler->callback != NULL; handler++)
482   {
483     if ((ntohs (rmessage->type) == handler->type) &&
484         ((handler->expected_size == 0) ||
485          (handler->expected_size == ntohs (rmessage->size))))
486     {
487       break;
488     }
489   }
490
491   /* handler->callback handles this message */
492
493   /* If no handler was found, drop the message but keep the channel open */
494   if (handler->callback == NULL)
495   {
496     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497                 "Received message of type %d from peer %s; dropping it.\n",
498                 ntohs (rmessage->type), GNUNET_i2s (other));
499     return GNUNET_OK;
500   }
501
502   struct tunnel_list_element *tunnel = handle->established_tunnels.head;
503
504   while (tunnel != NULL)
505   {
506     if (tunnel->tunnel.id.id == tmessage->id.id &&
507         (0 ==
508          memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
509                  sizeof (struct GNUNET_PeerIdentity))) &&
510         (0 ==
511          memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
512                  sizeof (struct GNUNET_PeerIdentity))))
513       break;
514     tunnel = tunnel->next;
515   }
516
517   /* if no tunnel was found: create a new inbound tunnel */
518   if (tunnel == NULL)
519   {
520     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521                 "New inbound tunnel from peer %s; first message has type %d.\n",
522                 GNUNET_i2s (other), ntohs (rmessage->type));
523     tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
524     tunnel->tunnel.connect_handler = NULL;
525     tunnel->tunnel.disconnect_handler = NULL;
526     tunnel->tunnel.handler_cls = NULL;
527     tunnel->tunnel.ctx = NULL;
528     tunnel->tunnel.handle = handle;
529     memcpy (&tunnel->tunnel.peer, other, sizeof (struct GNUNET_PeerIdentity));
530     memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
531
532     GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
533                                        handle->established_tunnels.tail,
534                                        handle->established_tunnels.tail,
535                                        tunnel);
536   }
537   else
538     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539                 "Inbound message from peer %s; type %d.\n", GNUNET_i2s (other),
540                 ntohs (rmessage->type));
541
542   return handler->callback (handle->cls, &tunnel->tunnel, &tunnel->tunnel.ctx,
543                             other, rmessage, atsi);
544 }
545
546 struct GNUNET_MESH_Tunnel *
547 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
548                                           struct GNUNET_TIME_Relative timeout,
549                                           GNUNET_MESH_ApplicationType
550                                           application_type,
551                                           GNUNET_MESH_TunnelConnectHandler
552                                           connect_handler,
553                                           GNUNET_MESH_TunnelDisconnectHandler
554                                           disconnect_handler, void *handler_cls)
555 {
556   /* Look in the list of connected peers */
557   struct peer_list_element *element = handle->connected_peers.head;
558
559   while (element != NULL)
560   {
561     struct type_list_element *i;
562
563     for (i = element->type_head; i != NULL; i = i->next)
564       if (application_type == i->type)
565         return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
566                                                      &element->peer,
567                                                      connect_handler,
568                                                      disconnect_handler,
569                                                      handler_cls);
570     element = element->next;
571   }
572
573   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n",
574               application_type);
575
576   /* Put into pending list */
577   struct tunnel_list_element *tunnel =
578       GNUNET_malloc (sizeof (struct tunnel_list_element));
579
580   tunnel->tunnel.connect_handler = connect_handler;
581   tunnel->tunnel.disconnect_handler = disconnect_handler;
582   tunnel->tunnel.handler_cls = handler_cls;
583   tunnel->tunnel.ctx = NULL;
584   tunnel->tunnel.handle = handle;
585   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
586           sizeof (struct GNUNET_PeerIdentity));
587   tunnel->tunnel.id.id = current_id++;
588   tunnel->tunnel.application_type = application_type;
589
590   GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
591                                      handle->pending_by_type_tunnels.tail,
592                                      handle->pending_by_type_tunnels.tail,
593                                      tunnel);
594   return &tunnel->tunnel;
595 }
596
597
598
599 struct GNUNET_MESH_Tunnel *
600 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
601                                       struct GNUNET_TIME_Relative timeout,
602                                       unsigned int num_peers,
603                                       const struct GNUNET_PeerIdentity *peers,
604                                       GNUNET_MESH_TunnelConnectHandler
605                                       connect_handler,
606                                       GNUNET_MESH_TunnelDisconnectHandler
607                                       disconnect_handler, void *handler_cls)
608 {
609   if (num_peers != 1)
610     return NULL;
611
612   struct tunnel_list_element *tunnel =
613       GNUNET_malloc (sizeof (struct tunnel_list_element));
614
615   tunnel->tunnel.connect_handler = connect_handler;
616   tunnel->tunnel.disconnect_handler = disconnect_handler;
617   tunnel->tunnel.handler_cls = handler_cls;
618   tunnel->tunnel.ctx = NULL;
619   tunnel->tunnel.handle = handle;
620   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
621           sizeof (struct GNUNET_PeerIdentity));
622   memcpy (&tunnel->tunnel.id.target, peers,
623           sizeof (struct GNUNET_PeerIdentity));
624   tunnel->tunnel.id.id = current_id++;
625   memcpy (&tunnel->tunnel.peer, peers, sizeof (struct GNUNET_PeerIdentity));
626
627   struct peer_list_element *element = handle->connected_peers.head;
628
629   while (element != NULL)
630   {
631     if (0 ==
632         memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
633       break;
634     element = element->next;
635   }
636
637   if (element != NULL)
638   {
639     /* we are connected to this peer */
640     GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
641                                        handle->established_tunnels.tail,
642                                        handle->established_tunnels.tail,
643                                        tunnel);
644     GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel);
645   }
646   else if (0 ==
647            memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
648   {
649     /* we are the peer */
650     GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
651                                        handle->established_tunnels.tail,
652                                        handle->established_tunnels.tail,
653                                        tunnel);
654     GNUNET_SCHEDULER_add_now (send_self_connect, tunnel);
655   }
656   else
657   {
658     /* we are not connected to this peer */
659     GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
660                                        handle->pending_tunnels.tail,
661                                        handle->pending_tunnels.tail, tunnel);
662     GNUNET_TRANSPORT_try_connect (handle->transport, peers);
663   }
664
665   return &tunnel->tunnel;
666 }
667
668 const struct GNUNET_PeerIdentity *
669 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel)
670 {
671   return &tunnel->peer;
672 }
673
674 static size_t
675 core_notify (void *cls, size_t size, void *buf)
676 {
677   struct notify_cls *ncls = cls;
678   struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
679
680   if (NULL == buf)
681     return ncls->notify (ncls->notify_cls, 0, NULL);
682
683   tunnel->notify_handle = NULL;
684   struct tunnel_message *message = buf;
685   void *cbuf = (void *) &message[1];
686
687   GNUNET_assert (NULL != ncls->notify);
688
689   size_t sent =
690       ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message),
691                     cbuf);
692
693   GNUNET_free (ncls);
694
695   if (0 == sent)
696     return 0;
697
698   sent += sizeof (struct tunnel_message);
699
700   message->hdr.type = htons (GNUNET_MESSAGE_TYPE_MESH);
701   message->hdr.size = htons (sent);
702   memcpy (&message->id, &tunnel->id, sizeof (struct tunnel_id));
703   return sent;
704 }
705
706
707 /**
708  * Ask the mesh to call "notify" once it is ready to transmit the
709  * given number of bytes to the specified "target".  If we are not yet
710  * connected to the specified peer, a call to this function will cause
711  * us to try to establish a connection.
712  *
713  * @param tunnel tunnel to use for transmission
714  * @param cork is corking allowed for this transmission?
715  * @param priority how important is the message?
716  * @param maxdelay how long can the message wait?
717  * @param target destination for the message, NULL for multicast to all tunnel targets
718  * @param notify_size how many bytes of buffer space does notify want?
719  * @param notify function to call when buffer space is available;
720  *        will be called with NULL on timeout or if the overall queue
721  *        for this peer is larger than queue_size and this is currently
722  *        the message with the lowest priority
723  * @param notify_cls closure for notify
724  * @return non-NULL if the notify callback was queued,
725  *         NULL if we can not even queue the request (insufficient
726  *         memory); if NULL is returned, "notify" will NOT be called.
727  */
728 struct GNUNET_MESH_TransmitHandle *
729 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
730                                    uint32_t priority,
731                                    struct GNUNET_TIME_Relative maxdelay,
732                                    const struct GNUNET_PeerIdentity *target
733                                    __attribute__ ((unused)), size_t notify_size,
734                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
735                                    void *notify_cls)
736 {
737   if (NULL != tunnel->notify_handle)
738   {
739     GNUNET_break (0);
740     return NULL;
741   }
742
743   struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
744
745   cls->notify_cls = notify_cls;
746   GNUNET_assert (NULL != notify);
747   cls->notify = notify;
748   cls->tunnel = tunnel;
749
750   tunnel->notify_handle =
751       (struct GNUNET_MESH_TransmitHandle *)
752       GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
753                                          maxdelay, &tunnel->peer,
754                                          notify_size +
755                                          sizeof (struct tunnel_message),
756                                          &core_notify, (void *) cls);
757
758   return tunnel->notify_handle;
759 }
760
761 void
762 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
763 {
764   GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
765                                              *) th);
766 }
767
768 void
769 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
770 {
771   tunnel->app_head = head;
772 }
773
774 void
775 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
776 {
777   tunnel->app_tail = tail;
778 }
779
780 void *
781 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
782 {
783   return tunnel->app_head;
784 }
785
786 void *
787 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
788 {
789   return tunnel->app_head;
790 }
791
792 void
793 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
794 {
795   tunnel->app_data = data;
796 }
797
798 void *
799 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
800 {
801   return tunnel->app_data;
802 }
803
804
805 void
806 build_hello_message (struct GNUNET_MESH_Handle *handle,
807                      const GNUNET_MESH_ApplicationType *stypes)
808 {
809   int num = 0;
810   const GNUNET_MESH_ApplicationType *t;
811
812   for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ;
813
814   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
815
816   handle->hello_message_size = sizeof (uint16_t) +      /* For the number of types */
817       num * sizeof (GNUNET_MESH_ApplicationType);       /* For the types */
818
819   uint16_t *nums = GNUNET_malloc (handle->hello_message_size);
820   GNUNET_MESH_ApplicationType *types =
821       (GNUNET_MESH_ApplicationType *) (nums + 1);
822
823   *nums = htons (num);
824
825   int i;
826
827   for (i = 0; i < num; i++)
828   {
829     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n",
830                 stypes[i]);
831     types[i] = htons (stypes[i]);
832   }
833
834   handle->hello_message = nums;
835 }
836
837
838 struct GNUNET_MESH_Handle *
839 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
840                      GNUNET_MESH_TunnelEndHandler cleaner,
841                      const struct GNUNET_MESH_MessageHandler *handlers,
842                      const GNUNET_MESH_ApplicationType *stypes)
843 {
844   struct GNUNET_MESH_Handle *ret =
845       GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
846
847   ret->connected_to_core = GNUNET_NO;
848   ret->connected_peers.head = NULL;
849   ret->connected_peers.tail = NULL;
850   ret->cleaner = cleaner;
851   ret->cls = cls;
852
853   const struct GNUNET_MESH_MessageHandler *it;
854   unsigned int len = 1;
855
856   for (it = handlers; it->callback != NULL; it++)
857   {
858     len++;
859   }
860
861   ret->handlers =
862       GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
863   memset (ret->handlers, 0, len * sizeof (struct GNUNET_MESH_MessageHandler));
864   memcpy (ret->handlers, handlers,
865           len * sizeof (struct GNUNET_MESH_MessageHandler));
866
867   build_hello_message (ret, stypes);
868
869   static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
870     {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
871     {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
872     {NULL, 0, 0}
873   };
874
875   ret->core =
876       GNUNET_CORE_connect (cfg, 42, ret, &core_startup, &core_connect,
877                            &core_disconnect, NULL, NULL, GNUNET_NO, NULL,
878                            GNUNET_NO, core_handlers);
879   ret->transport =
880     GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
881   return ret;
882 }
883
884 void
885 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
886 {
887   GNUNET_free (handle->handlers);
888   GNUNET_free (handle->hello_message);
889   GNUNET_CORE_disconnect (handle->core);
890   GNUNET_TRANSPORT_disconnect (handle->transport);
891
892   struct peer_list_element *element = handle->connected_peers.head;
893
894   while (element != NULL)
895   {
896     struct peer_list_element *next = element->next;
897
898     while (element->type_head != NULL)
899     {
900       struct type_list_element *tail = element->type_tail;
901
902       GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
903                                    tail);
904       GNUNET_free (tail);
905     }
906     GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
907     GNUNET_SCHEDULER_cancel (element->sched);
908     GNUNET_free (element);
909     element = next;
910   }
911
912   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
913
914   while (tunnel != NULL)
915   {
916     struct tunnel_list_element *next = tunnel->next;
917
918     GNUNET_free (tunnel);
919     tunnel = next;
920   }
921   tunnel = handle->established_tunnels.head;;
922   while (tunnel != NULL)
923   {
924     struct tunnel_list_element *next = tunnel->next;
925
926     GNUNET_free (tunnel);
927     tunnel = next;
928   }
929
930   GNUNET_free (handle);
931 }
932
933 /* end of mesh_api.c */