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