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