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