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