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