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