use new transport API, minor core API change
[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
33 struct tunnel_id
34 {
35   uint32_t id GNUNET_PACKED;
36   struct GNUNET_PeerIdentity initiator;
37   struct GNUNET_PeerIdentity target;
38 };
39
40 static uint32_t current_id = 0;
41
42 struct tunnel_message
43 {
44   struct GNUNET_MessageHeader hdr;
45   struct tunnel_id id;
46   /* followed by another GNUNET_MessageHeader */
47 };
48
49 struct notify_cls
50 {
51   void* notify_cls;
52   GNUNET_CONNECTION_TransmitReadyNotify notify;
53   struct GNUNET_MESH_Tunnel *tunnel;
54 };
55
56 struct GNUNET_MESH_Tunnel
57 {
58   /* The other peer this tunnel leads to; just unicast for the moment! */
59   struct GNUNET_PeerIdentity peer;
60
61   struct tunnel_id id;
62
63   /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
64   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
65   GNUNET_MESH_TunnelConnectHandler connect_handler;
66   void *handler_cls;
67
68   struct GNUNET_MESH_Handle* handle;
69
70   /* The message-type requested for this tunnel. Is only needed for pending
71    * by_tupe-tunnels
72    */
73   uint16_t message_type;
74
75   /* The context of the receive-function. */
76   void *ctx;
77 };
78
79 struct tunnel_list_element
80 {
81   struct GNUNET_MESH_Tunnel tunnel;
82   struct tunnel_list_element *next, *prev;
83 };
84
85 struct tunnel_list
86 {
87   struct tunnel_list_element *head, *tail;
88 };
89
90 struct peer_list_element
91 {
92   struct GNUNET_PeerIdentity peer;
93
94   /* how many Message-Types can this peer receive */
95   unsigned int num_types;
96
97   /* array of message-types */
98   GNUNET_MESH_ApplicationType *types;
99
100   struct GNUNET_TRANSPORT_ATS_Information atsi;
101   struct peer_list_element *next, *prev;
102 };
103
104 struct peer_list
105 {
106   struct peer_list_element *head, *tail;
107 };
108
109 struct GNUNET_MESH_Handle
110 {
111   struct GNUNET_CORE_Handle *core;
112   struct GNUNET_MESH_MessageHandler *handlers;
113   struct GNUNET_PeerIdentity myself;
114   unsigned int connected_to_core;
115   struct peer_list connected_peers;
116   struct tunnel_list established_tunnels;
117   struct tunnel_list pending_tunnels;
118   struct tunnel_list pending_by_type_tunnels;
119   void *cls;
120   GNUNET_MESH_TunnelEndHandler *cleaner;
121   size_t hello_message_size;
122   uint16_t *hello_message;
123 };
124
125 static void
126 send_end_connect(void* cls,
127                      const struct GNUNET_SCHEDULER_TaskContext* tc)
128 {
129   struct GNUNET_MESH_Tunnel* tunnel = cls;
130
131   tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
132 }
133
134 static void
135 send_self_connect(void* cls,
136                         const struct GNUNET_SCHEDULER_TaskContext* tc)
137 {
138   struct GNUNET_MESH_Tunnel* tunnel = cls;
139
140   tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
141   GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
142 }
143
144 static void
145 call_connect_handler (void *cls,
146                       const struct GNUNET_SCHEDULER_TaskContext *tc)
147 {
148   struct GNUNET_MESH_Tunnel *tunnel = cls;
149
150   tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer,
151                            NULL);
152   GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
153 }
154
155 static void
156 core_startup (void *cls,
157               struct GNUNET_CORE_Handle *core,
158               const struct GNUNET_PeerIdentity *my_identity,
159               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
160 {
161   struct GNUNET_MESH_Handle *handle = cls;
162   memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
163   handle->connected_to_core = GNUNET_YES;
164 }
165
166 static size_t
167 send_hello_message (void *cls, size_t size, void *buf)
168 {
169   struct GNUNET_MESH_Handle *handle = cls;
170   struct GNUNET_MessageHeader *hdr = buf;
171
172   size_t sent = sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size;
173
174   hdr->type = htons(GNUNET_MESSAGE_TYPE_MESH_HELLO);
175   hdr->size = htons(size);
176
177   memcpy(hdr+1, handle->hello_message, handle->hello_message_size);
178   return sent;
179 }
180
181
182 /**
183  * Core calls this if we are connected to a new peer.
184  *
185  * The peer is added to the connected_peers-list.
186  *
187  */
188 static void
189 core_connect (void *cls,
190               const struct GNUNET_PeerIdentity *peer,
191               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
192 {
193   struct GNUNET_MESH_Handle *handle = cls;
194
195   /* Send a hello to this peer */
196   GNUNET_CORE_notify_transmit_ready(handle->core,
197                                     GNUNET_NO,
198                                     42,
199                                     GNUNET_TIME_UNIT_SECONDS,
200                                     peer,
201                                     sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size,
202                                     &send_hello_message,
203                                     cls);
204
205   /* put the new peer into the list of connected peers */
206   struct peer_list_element *element =
207     GNUNET_malloc (sizeof (struct peer_list_element));
208   memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
209
210   if (NULL != atsi)
211     memcpy (&element->atsi, atsi,
212             sizeof (struct GNUNET_TRANSPORT_ATS_Information));
213
214   GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
215                                      handle->connected_peers.tail,
216                                      handle->connected_peers.tail, element);
217
218   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
219   while (tunnel != NULL)
220     {
221       if (0 ==
222           memcmp (&tunnel->tunnel.peer, peer,
223                   sizeof (struct GNUNET_PeerIdentity)))
224         {
225           struct tunnel_list_element *next = tunnel->next;
226           GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
227                                        handle->pending_tunnels.tail, tunnel);
228           GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
229                                              handle->established_tunnels.tail,
230                                              handle->established_tunnels.tail,
231                                              tunnel);
232           tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
233                                           peer, atsi);
234           GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
235           tunnel = next;
236         }
237       else
238         tunnel = tunnel->next;
239     }
240 }
241
242 /**
243  * Core calls this if we disconnect a peer
244  *
245  * Remove this peer from the list of connected peers
246  * Close all tunnels this peer belongs to
247  */
248 static void
249 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
250 {
251   struct GNUNET_MESH_Handle *handle = cls;
252
253   struct peer_list_element *element = handle->connected_peers.head;
254   while (element != NULL)
255     {
256       if (0 ==
257           memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
258         break;
259       element = element->next;
260     }
261   if (element != NULL)
262     {
263       GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
264                                    handle->connected_peers.tail, element);
265       GNUNET_free_non_null(element->types);
266       GNUNET_free (element);
267     }
268
269   struct tunnel_list_element *telement = handle->established_tunnels.head;
270   while (telement != NULL)
271     {
272       if (0 ==
273           memcmp (&telement->tunnel.peer, peer,
274                   sizeof (struct GNUNET_PeerIdentity)))
275         {
276           /* disconnect tunnels */
277           /* outbound tunnels */
278           if (telement->tunnel.connect_handler != NULL && NULL != telement->tunnel.disconnect_handler)
279             telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
280                                                  peer);
281           /* inbound tunnels */
282           else if (NULL != handle->cleaner)
283             handle->cleaner (handle->cls, &telement->tunnel,
284                              &telement->tunnel.ctx);
285
286           struct tunnel_list_element *next = telement->next;
287           GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
288                                        handle->established_tunnels.tail,
289                                        telement);
290           GNUNET_free (telement);
291           telement = next;
292         }
293       else
294         {
295           telement = telement->next;
296         }
297     }
298 }
299
300 /**
301  * Receive a message from core.
302  * This is a hello-message, containing the message-types the other peer can receive
303  */
304 static int
305 receive_hello (void *cls,
306                const struct GNUNET_PeerIdentity *other,
307                const struct GNUNET_MessageHeader *message,
308                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
309 {
310   struct GNUNET_MESH_Handle *handle = cls;
311   uint16_t *num = (uint16_t *) (message + 1);
312   uint16_t *ports = num + 1;
313   unsigned int i;
314
315   struct peer_list_element *element = handle->connected_peers.head;
316   while (element != NULL)
317     {
318       if (0 ==
319           memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
320         break;
321       element = element->next;
322     }
323
324   /* TODO: add, not replace! */
325   /* TODO: if this changes anything: send new hello */
326   element->num_types = *num;
327   element->types = GNUNET_malloc (*num * sizeof (GNUNET_MESH_ApplicationType));
328
329   for (i = 0; i < *num; i++)
330     element->types[i] = (GNUNET_MESH_ApplicationType)ntohs (ports[i]);
331
332   struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
333   while (tunnel != NULL)
334     {
335       struct tunnel_list_element *next = tunnel->next;
336       for (i = 0; i < *num; i++)
337         {
338           if (ntohs (ports[i]) == tunnel->tunnel.message_type)
339             {
340               GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
341                                            handle->pending_tunnels.tail,
342                                            tunnel);
343               GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.
344                                                  head,
345                                                  handle->established_tunnels.
346                                                  tail,
347                                                  handle->established_tunnels.
348                                                  tail, tunnel);
349               tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
350                                               &tunnel->tunnel.peer, atsi);
351               GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
352               break;
353             }
354         }
355       if (ntohs (ports[i]) == tunnel->tunnel.message_type)
356         tunnel = next;
357       else
358         tunnel = tunnel->next;
359     }
360   return GNUNET_OK;
361 }
362
363 /**
364  * Receive a message from core.
365  */
366 static int
367 core_receive (void *cls,
368               const struct GNUNET_PeerIdentity *other,
369               const struct GNUNET_MessageHeader *message,
370               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
371 {
372   struct GNUNET_MESH_Handle *handle = cls;
373   struct tunnel_message *tmessage = (struct tunnel_message *) message;
374   struct GNUNET_MessageHeader *rmessage =
375     (struct GNUNET_MessageHeader *) (tmessage + 1);
376
377   struct GNUNET_MESH_MessageHandler *handler;
378
379   for (handler = handle->handlers; handler->callback != NULL; handler++)
380     {
381       if ( (ntohs (rmessage->type) == handler->type)
382            && ( (handler->expected_size == 0)
383                 || (handler->expected_size == ntohs (rmessage->size))) )
384         {
385           break;
386         }
387     }
388
389   /* handler->callback handles this message */
390
391   /* If no handler was found, drop the message but keep the channel open */
392   if (handler->callback == NULL)
393     return GNUNET_OK;
394
395   struct tunnel_list_element *tunnel = handle->established_tunnels.head;
396
397   while (tunnel != NULL)
398     {
399       if (tunnel->tunnel.id.id == tmessage->id.id &&
400           (0 ==
401            memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
402                    sizeof (struct GNUNET_PeerIdentity)))
403           && (0 ==
404               memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
405                       sizeof (struct GNUNET_PeerIdentity))))
406         break;
407       tunnel = tunnel->next;
408     }
409
410   /* if no tunnel was found: create a new inbound tunnel */
411   if (tunnel == NULL)
412     {
413       tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
414       tunnel->tunnel.connect_handler = NULL;
415       tunnel->tunnel.disconnect_handler = NULL;
416       tunnel->tunnel.handler_cls = NULL;
417       tunnel->tunnel.ctx = NULL;
418       tunnel->tunnel.handle = handle;
419       memcpy (&tunnel->tunnel.peer, other,
420               sizeof (struct GNUNET_PeerIdentity));
421       memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
422
423       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
424                                          handle->established_tunnels.tail,
425                                          handle->established_tunnels.tail,
426                                          tunnel);
427     }
428
429   return handler->callback (handle->cls, &tunnel->tunnel,
430                             &tunnel->tunnel.ctx, other, rmessage, atsi);
431 }
432
433 struct GNUNET_MESH_Tunnel *
434 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
435                                           struct GNUNET_TIME_Relative timeout,
436                                           GNUNET_MESH_ApplicationType message_type,
437                                           GNUNET_MESH_TunnelConnectHandler
438                                           connect_handler,
439                                           GNUNET_MESH_TunnelDisconnectHandler
440                                           disconnect_handler,
441                                           void *handler_cls)
442 {
443   /* Look in the list of connected peers */
444   struct peer_list_element *element = handle->connected_peers.head;
445   while (element != NULL)
446     {
447       unsigned int i;
448       for (i = 0; i < element->num_types; i++)
449         if (message_type == element->types[i])
450           return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
451                                                        &handle->myself,
452                                                        connect_handler,
453                                                        disconnect_handler,
454                                                        handler_cls);
455       element = element->next;
456     }
457
458   /* Put into pending list */
459   struct tunnel_list_element *tunnel =
460     GNUNET_malloc (sizeof (struct tunnel_list_element));
461
462   tunnel->tunnel.connect_handler = connect_handler;
463   tunnel->tunnel.disconnect_handler = disconnect_handler;
464   tunnel->tunnel.handler_cls = handler_cls;
465   tunnel->tunnel.ctx = NULL;
466   tunnel->tunnel.handle = handle;
467   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
468           sizeof (struct GNUNET_PeerIdentity));
469   tunnel->tunnel.id.id = current_id++;
470   tunnel->tunnel.message_type = message_type;
471
472   GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
473                                      handle->pending_by_type_tunnels.tail,
474                                      handle->pending_by_type_tunnels.tail,
475                                      tunnel);
476   return &tunnel->tunnel;
477 }
478
479
480
481 struct GNUNET_MESH_Tunnel *
482 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
483                                       struct GNUNET_TIME_Relative timeout,
484                                       unsigned int num_peers,
485                                       const struct GNUNET_PeerIdentity *peers,
486                                       GNUNET_MESH_TunnelConnectHandler
487                                       connect_handler,
488                                       GNUNET_MESH_TunnelDisconnectHandler
489                                       disconnect_handler, void *handler_cls)
490 {
491   if (num_peers != 1)
492     return NULL;
493
494   struct tunnel_list_element *tunnel =
495     GNUNET_malloc (sizeof (struct tunnel_list_element));
496
497   tunnel->tunnel.connect_handler = connect_handler;
498   tunnel->tunnel.disconnect_handler = disconnect_handler;
499   tunnel->tunnel.handler_cls = handler_cls;
500   tunnel->tunnel.ctx = NULL;
501   tunnel->tunnel.handle = handle;
502   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
503           sizeof (struct GNUNET_PeerIdentity));
504   memcpy (&tunnel->tunnel.id.target, peers,
505           sizeof (struct GNUNET_PeerIdentity));
506   tunnel->tunnel.id.id = current_id++;
507   memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity));
508
509   struct peer_list_element *element = handle->connected_peers.head;
510   while (element != NULL)
511     {
512       if (0 ==
513           memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
514         break;
515       element = element->next;
516     }
517
518   if (element != NULL)
519     {
520       /* we are connected to this peer */
521       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
522                                          handle->established_tunnels.tail,
523                                          handle->established_tunnels.tail,
524                                          tunnel);
525       GNUNET_SCHEDULER_add_now(call_connect_handler, tunnel);
526     }
527   else if (0 ==
528            memcmp (peers, &handle->myself,
529                    sizeof (struct GNUNET_PeerIdentity)))
530     {
531       /* we are the peer */
532       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
533                                          handle->established_tunnels.tail,
534                                          handle->established_tunnels.tail,
535                                          tunnel);
536       GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
537     }
538   else
539     {
540       /* we are not connected to this peer */
541       GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
542                                          handle->pending_tunnels.tail,
543                                          handle->pending_tunnels.tail,
544                                          tunnel);
545       (void) GNUNET_CORE_peer_request_connect (handle->core,
546                                                peers,
547                                                NULL, NULL);
548     }
549
550   return &tunnel->tunnel;
551 }
552
553 const struct GNUNET_PeerIdentity*
554 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
555 {
556   return &tunnel->peer;
557 }
558
559 static size_t
560 core_notify(void* cls, size_t size, void* buf)
561 {
562   struct notify_cls *ncls = cls;
563   struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
564   struct tunnel_message* message = buf;
565   void* cbuf = (void*) &message[1];
566   GNUNET_assert(NULL != ncls->notify);
567
568   size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
569
570   GNUNET_free(ncls);
571
572   if (0 == sent) return 0;
573
574   sent += sizeof(struct tunnel_message);
575
576   message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
577   message->hdr.size = htons(sent);
578   memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
579   return sent;
580 }
581
582
583 /**
584  * Ask the mesh to call "notify" once it is ready to transmit the
585  * given number of bytes to the specified "target".  If we are not yet
586  * connected to the specified peer, a call to this function will cause
587  * us to try to establish a connection.
588  *
589  * @param tunnel tunnel to use for transmission
590  * @param cork is corking allowed for this transmission?
591  * @param priority how important is the message?
592  * @param maxdelay how long can the message wait?
593  * @param target destination for the message, NULL for multicast to all tunnel targets 
594  * @param notify_size how many bytes of buffer space does notify want?
595  * @param notify function to call when buffer space is available;
596  *        will be called with NULL on timeout or if the overall queue
597  *        for this peer is larger than queue_size and this is currently
598  *        the message with the lowest priority
599  * @param notify_cls closure for notify
600  * @return non-NULL if the notify callback was queued,
601  *         NULL if we can not even queue the request (insufficient
602  *         memory); if NULL is returned, "notify" will NOT be called.
603  */
604 struct GNUNET_MESH_TransmitHandle *
605 GNUNET_MESH_notify_transmit_ready (struct
606                                    GNUNET_MESH_Tunnel
607                                    *tunnel,
608                                    int cork,
609                                    uint32_t priority,
610                                    struct
611                                    GNUNET_TIME_Relative
612                                    maxdelay,
613                                    const struct GNUNET_PeerIdentity *target,
614                                    size_t
615                                    notify_size,
616                                    GNUNET_CONNECTION_TransmitReadyNotify
617                                    notify, void *notify_cls)
618 {
619   struct notify_cls *cls = GNUNET_malloc(sizeof(struct notify_cls));
620   cls->notify_cls = notify_cls;
621   GNUNET_assert(NULL != notify);
622   cls->notify = notify;
623   cls->tunnel = tunnel;
624   GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
625                                     cork,
626                                     priority,
627                                     maxdelay,
628                                     &tunnel->peer,
629                                     notify_size + sizeof(struct tunnel_message),
630                                     &core_notify,
631                                     (void*)cls);
632
633   /* aborting is not implemented yet */
634   return (struct GNUNET_MESH_TransmitHandle*) 1;
635 }
636
637 void build_hello_message(struct GNUNET_MESH_Handle* handle, int num)
638 {
639   handle->hello_message_size = sizeof(uint16_t) + /* For the number of types */
640     num * sizeof(uint16_t); /* For the types */
641
642   uint16_t *nums = GNUNET_malloc(handle->hello_message_size);
643   uint16_t *types = nums + 1;
644
645   *nums = num;
646
647   unsigned int i;
648   for(i = 0; i < num; i++)
649     types[i] = handle->handlers[i].type;
650
651   handle->hello_message = nums;
652 }
653
654
655 struct GNUNET_MESH_Handle *
656 GNUNET_MESH_connect (const struct
657                      GNUNET_CONFIGURATION_Handle
658                      *cfg, void *cls,
659                      GNUNET_MESH_TunnelEndHandler
660                      cleaner,
661                      const struct GNUNET_MESH_MessageHandler *handlers,
662                      const GNUNET_MESH_ApplicationType *stypes)
663 {
664   struct GNUNET_MESH_Handle *ret =
665     GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
666
667   ret->connected_to_core = GNUNET_NO;
668   ret->connected_peers.head = NULL;
669   ret->connected_peers.tail = NULL;
670   ret->cleaner = cleaner;
671   ret->cls = cls;
672
673   const struct GNUNET_MESH_MessageHandler *it;
674   unsigned int len = 1;
675   for (it = handlers; it->callback != NULL; it++)
676     {
677       len++;
678     }
679
680   ret->handlers =
681     GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
682   memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler));
683   memcpy (ret->handlers, handlers,
684           len * sizeof (struct GNUNET_MESH_MessageHandler));
685
686   build_hello_message(ret, len);
687
688   const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
689     {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
690     {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
691     {NULL, 0, 0}
692   };
693
694   ret->core = GNUNET_CORE_connect (cfg,
695                                    42,
696                                    ret,
697                                    &core_startup,
698                                    &core_connect,
699                                    &core_disconnect,
700                                    NULL,
701                                    NULL,
702                                    GNUNET_NO, NULL, GNUNET_NO, core_handlers);
703   return ret;
704 }
705
706 void
707 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
708 {
709   GNUNET_free (handle->handlers);
710   GNUNET_free (handle->hello_message);
711   GNUNET_CORE_disconnect (handle->core);
712
713   struct peer_list_element *element = handle->connected_peers.head;
714   while (element != NULL)
715     {
716       struct peer_list_element *next = element->next;
717       GNUNET_free_non_null(element->types);
718       GNUNET_free (element);
719       element = next;
720     }
721
722   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
723   while (tunnel != NULL)
724     {
725       struct tunnel_list_element *next = tunnel->next;
726       GNUNET_free (tunnel);
727       tunnel = next;
728     }
729   tunnel = handle->established_tunnels.head;;
730   while (tunnel != NULL)
731     {
732       struct tunnel_list_element *next = tunnel->next;
733       GNUNET_free (tunnel);
734       tunnel = next;
735     }
736
737   GNUNET_free (handle);
738 }
739
740 /* end of mesh_api.c */