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