WiP
[oweals/gnunet.git] / src / mesh / mesh_api_new.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4      GNUnet is free software; you can redistribute it and/or modify
5      it under the terms of the GNU General Public License as published
6      by the Free Software Foundation; either version 3, or (at your
7      option) any later version.
8      GNUnet is distributed in the hope that it will be useful, but
9      WITHOUT ANY WARRANTY; without even the implied warranty of
10      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11      General Public License for more details.
12      You should have received a copy of the GNU General Public License
13      along with GNUnet; see the file COPYING.  If not, write to the
14      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15      Boston, MA 02111-1307, USA.
16 */
17
18 /**
19  * @file mesh/mesh_api_new.c
20  * @brief mesh api: client implementation of mesh service
21  * @author Bartlomiej Polot
22  *
23  * TODO:
24  * - callbacks to client missing on certain events
25  * - processing messages from service is incomplete
26  * - Check priorities to cancel traffic data
27  *
28  * STRUCTURE:
29  * - CONSTANTS
30  * - DATA STRUCTURES
31  * - AUXILIARY FUNCTIONS
32  * - RECEIVE HANDLERS
33  * - SEND FUNCTIONS
34  * - API CALL DEFINITIONS
35  */
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44 #include "platform.h"
45 #include "gnunet_common.h"
46 #include "gnunet_client_lib.h"
47 #include "gnunet_util_lib.h"
48 #include "gnunet_peer_lib.h"
49 #include "gnunet_mesh_service_new.h"
50 #include "mesh.h"
51 #include "mesh_protocol.h"
52
53 #define DEBUG GNUNET_YES
54
55 /******************************************************************************/
56 /************************      DATA STRUCTURES     ****************************/
57 /******************************************************************************/
58
59 /**
60  * Transmission queue to the service
61  */
62 struct GNUNET_MESH_TransmitHandle
63 {
64
65     /**
66      * Double Linked list
67      */
68   struct GNUNET_MESH_TransmitHandle *next;
69
70     /**
71      * Double Linked list
72      */
73   struct GNUNET_MESH_TransmitHandle *prev;
74
75     /**
76      * Tunnel this message is sent over (may be NULL for control messages).
77      */
78   struct GNUNET_MESH_Tunnel *tunnel;
79
80     /**
81      * Callback to obtain the message to transmit, or NULL if we
82      * got the message in 'data'.  Notice that messages built
83      * by 'notify' need to be encapsulated with information about
84      * the 'target'.
85      */
86   GNUNET_CONNECTION_TransmitReadyNotify notify;
87
88     /**
89      * Closure for 'notify'
90      */
91   void *notify_cls;
92
93     /**
94      * How long is this message valid.  Once the timeout has been
95      * reached, the message must no longer be sent.  If this
96      * is a message with a 'notify' callback set, the 'notify'
97      * function should be called with 'buf' NULL and size 0.
98      */
99   struct GNUNET_TIME_Absolute timeout;
100
101     /**
102      * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
103      */
104   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
105
106     /**
107      * Priority of the message.  The queue is sorted by priority,
108      * control messages have the maximum priority (UINT32_MAX).
109      */
110   uint32_t priority;
111
112     /**
113      * Target of the message, 0 for broadcast.  This field
114      * is only valid if 'notify' is non-NULL.
115      */
116   GNUNET_PEER_Id target;
117
118     /**
119      * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
120      */
121   size_t size;
122 };
123
124
125 /**
126  * Opaque handle to the service.
127  */
128 struct GNUNET_MESH_Handle
129 {
130
131     /**
132      * Handle to the server connection, to send messages later
133      */
134   struct GNUNET_CLIENT_Connection *client;
135
136     /**
137      * Set of handlers used for processing incoming messages in the tunnels
138      */
139   const struct GNUNET_MESH_MessageHandler *message_handlers;
140
141     /**
142      * Set of applications that should be claimed to be offered at this node.
143      * Note that this is just informative, the appropiate handlers must be
144      * registered independently and the mapping is up to the developer of the
145      * client application.
146      */
147   const GNUNET_MESH_ApplicationType *applications;
148
149     /**
150      * Double linked list of the tunnels this client is connected to.
151      */
152   struct GNUNET_MESH_Tunnel *tunnels_head;
153   struct GNUNET_MESH_Tunnel *tunnels_tail;
154
155     /**
156      * Callback for inbound tunnel creation
157      */
158   GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
159
160     /**
161      * Callback for inbound tunnel disconnection
162      */
163   GNUNET_MESH_TunnelEndHandler *cleaner;
164
165     /**
166      * Handle to cancel pending transmissions in case of disconnection
167      */
168   struct GNUNET_CLIENT_TransmitHandle *th;
169
170     /**
171      * Closure for all the handlers given by the client
172      */
173   void *cls;
174
175     /**
176      * Messages to send to the service
177      */
178   struct GNUNET_MESH_TransmitHandle *th_head;
179   struct GNUNET_MESH_TransmitHandle *th_tail;
180
181     /**
182      * tid of the next tunnel to create (to avoid reusing IDs often)
183      */
184   MESH_TunnelNumber next_tid;
185   unsigned int n_handlers;
186   unsigned int n_applications;
187   unsigned int max_queue_size;
188
189     /**
190      * Have we started the task to receive messages from the service
191      * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
192      */
193   int in_receive;
194
195     /**
196      * Number of packets queued
197      */
198   unsigned int npackets;
199
200   /**
201    * Configuration given by the client, in case of reconnection
202    */
203   const struct GNUNET_CONFIGURATION_Handle *cfg;
204 };
205
206
207 /**
208  * Description of a peer
209  */
210 struct GNUNET_MESH_Peer
211 {
212     /**
213      * ID of the peer in short form
214      */
215   GNUNET_PEER_Id id;
216
217   /**
218    * Tunnel this peer belongs to
219    */
220   struct GNUNET_MESH_Tunnel *t;
221
222   /**
223    * Flag indicating whether service has informed about its connection
224    */
225   int connected;
226
227 };
228
229
230 /**
231  * Opaque handle to a tunnel.
232  */
233 struct GNUNET_MESH_Tunnel
234 {
235
236     /**
237      * DLL
238      */
239   struct GNUNET_MESH_Tunnel *next;
240   struct GNUNET_MESH_Tunnel *prev;
241
242     /**
243      * Callback to execute when peers connect to the tunnel
244      */
245   GNUNET_MESH_TunnelConnectHandler connect_handler;
246
247     /**
248      * Callback to execute when peers disconnect from the tunnel
249      */
250   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
251
252     /**
253      * Closure for the connect/disconnect handlers
254      */
255   void *cls;
256
257     /**
258      * Handle to the mesh this tunnel belongs to
259      */
260   struct GNUNET_MESH_Handle *mesh;
261
262     /**
263      * Local ID of the tunnel
264      */
265   MESH_TunnelNumber tid;
266
267     /**
268      * Owner of the tunnel
269      */
270   GNUNET_PEER_Id owner;
271
272     /**
273      * All peers added to the tunnel
274      */
275   struct GNUNET_MESH_Peer **peers;
276
277   /**
278    * List of application types that have been requested for this tunnel
279    */
280   GNUNET_MESH_ApplicationType *apps;
281
282   /**
283    * Any data the caller wants to put in here
284    */
285   void *ctx;
286
287   /**
288      * Number of peers added to the tunnel
289      */
290   unsigned int npeers;
291
292     /**
293      * Number of packets queued in this tunnel
294      */
295   unsigned int npackets;
296
297     /**
298      * Number of applications requested this tunnel
299      */
300   unsigned int napps;
301
302 };
303
304
305 /******************************************************************************/
306 /***********************     AUXILIARY FUNCTIONS      *************************/
307 /******************************************************************************/
308
309 /**
310  * Get the tunnel handler for the tunnel specified by id from the given handle
311  * @param h Mesh handle
312  * @param tid ID of the wanted tunnel
313  * @return handle to the required tunnel or NULL if not found
314  */
315 static struct GNUNET_MESH_Tunnel *
316 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
317 {
318   struct GNUNET_MESH_Tunnel *t;
319
320   t = h->tunnels_head;
321   while (t != NULL)
322   {
323     if (t->tid == tid)
324       return t;
325     t = t->next;
326   }
327   return NULL;
328 }
329
330
331 /**
332  * Create a new tunnel and insert it in the tunnel list of the mesh handle
333  * @param h Mesh handle
334  * @param tid desired tid of the tunnel, 0 to assign one automatically
335  * @return handle to the created tunnel
336  */
337 static struct GNUNET_MESH_Tunnel *
338 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
339 {
340   struct GNUNET_MESH_Tunnel *t;
341
342   t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
343   GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
344   t->mesh = h;
345   if (0 == tid)
346   {
347     t->tid = h->next_tid;
348     while (NULL != retrieve_tunnel (h, h->next_tid))
349     {
350       h->next_tid++;
351       h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
352       h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
353     }
354   }
355   else
356   {
357     t->tid = tid;
358   }
359   return t;
360 }
361
362
363 /**
364  * Destroy the specified tunnel.
365  * - Destroys all peers, calling the disconnect callback on each if needed
366  * - Cancels all outgoing traffic for that tunnel, calling respective notifys
367  * - Calls cleaner if tunnel was inbound
368  * - Frees all memory used
369  * @param tid ID of the wanted tunnel
370  * @return handle to the required tunnel or NULL if not found
371  */
372 static void
373 destroy_tunnel (struct GNUNET_MESH_Tunnel *t)
374 {
375   struct GNUNET_MESH_Handle *h;
376   struct GNUNET_PeerIdentity pi;
377   struct GNUNET_MESH_TransmitHandle *th;
378   struct GNUNET_MESH_TransmitHandle *aux;
379   unsigned int i;
380
381   if (NULL == t)
382   {
383     if (th->tunnel == t)
384     {
385       aux = th->next;
386       GNUNET_CONTAINER_DLL_remove(h->th_head, h->th_tail, th);
387       if (NULL == h->th_head && NULL != h->th)
388       {
389         GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
390         h->th = NULL;
391       }
392       if (NULL != th->notify)
393           th->notify(th->notify_cls, 0, NULL);
394       if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
395           GNUNET_SCHEDULER_cancel(th->timeout_task);
396       GNUNET_free (th);
397       th = aux;
398     }
399     else
400     {
401       th = th->next;
402     }
403   }
404   h = t->mesh;
405   /* TODO remove data packets from queue */
406   GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
407   for (i = 0; i < t->npeers; i++)
408   {
409     if (NULL != t->disconnect_handler && t->peers[i]->connected)
410     {
411       GNUNET_PEER_resolve (t->peers[i]->id, &pi);
412       t->disconnect_handler (t->cls, &pi);
413     }
414     GNUNET_PEER_change_rc (t->peers[i]->id, -1);
415     GNUNET_free (t->peers[i]);
416   }
417   if (t->npeers > 0)
418       GNUNET_free (t->peers);
419   if (NULL != h->cleaner && 0 != t->owner)
420     h->cleaner (h->cls, t, t->ctx);
421   if (0 != t->owner)
422     GNUNET_PEER_change_rc (t->owner, -1);
423   if (0 != t->napps && t->apps)
424     GNUNET_free (t->apps);
425   GNUNET_free (t);
426   return;
427 }
428
429 /**
430  * Get the peer descriptor for the peer with id from the given tunnel
431  * @param t Tunnel handle
432  * @param id Short form ID of the wanted peer
433  * @return handle to the requested peer or NULL if not found
434  */
435 static struct GNUNET_MESH_Peer *
436 retrieve_peer (struct GNUNET_MESH_Tunnel *t, GNUNET_PEER_Id id)
437 {
438   unsigned int i;
439
440   for (i = 0; i < t->npeers; i++)
441     if (t->peers[i]->id == id)
442       return t->peers[i];
443   return NULL;
444 }
445
446
447 /**
448  * Add a peer into a tunnel
449  * @param t Tunnel handle
450  * @param pi Full ID of the new peer
451  * @return handle to the newly created peer
452  */
453 static struct GNUNET_MESH_Peer *
454 add_peer_to_tunnel (struct GNUNET_MESH_Tunnel *t,
455                     const struct GNUNET_PeerIdentity *pi)
456 {
457   struct GNUNET_MESH_Peer *p;
458   GNUNET_PEER_Id id;
459
460   if (0 != t->owner)
461   {
462     GNUNET_break (0);
463     return NULL;
464   }
465   id = GNUNET_PEER_intern (pi);
466
467   p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
468   p->id = id;
469   p->t = t;
470   GNUNET_array_append (t->peers, t->npeers, p);
471   return p;
472 }
473
474
475 /**
476  * Remove a peer from a tunnel
477  * @param t Tunnel handle
478  * @param p Peer handle
479  */
480 static void
481 remove_peer_from_tunnel (struct GNUNET_MESH_Peer *p)
482 {
483   unsigned int i;
484
485   for (i = 0; i < p->t->npeers; i++)
486   {
487     if (p->t->peers[i] == p)
488       break;
489   }
490   if (i == p->t->npeers)
491   {
492     GNUNET_break (0);
493     return;
494   }
495   p->t->peers[i] = p->t->peers[p->t->npeers - 1];
496   GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
497 }
498
499
500 /**
501  * Notify client that the transmission has timed out
502  * @param cls closure
503  * @param tc task context
504  */
505 static void
506 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
507 {
508   struct GNUNET_MESH_TransmitHandle *th = cls;
509   struct GNUNET_MESH_Handle *mesh;
510
511   mesh = th->tunnel->mesh;
512   GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
513   if (th->notify != NULL)
514     th->notify (th->notify_cls, 0, NULL);
515   GNUNET_free (th);
516   if ((NULL == mesh->th_head) && (NULL != mesh->th))
517   {
518     /* queue empty, no point in asking for transmission */
519     GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
520     mesh->th = NULL;
521   }
522 }
523
524
525 /**
526  * Add a transmit handle to the transmission queue by priority and set the
527  * timeout if needed.
528  *
529  * @param h mesh handle with the queue head and tail
530  * @param q handle to the packet to be transmitted
531  */
532 static void
533 add_to_queue (struct GNUNET_MESH_Handle *h,
534               struct GNUNET_MESH_TransmitHandle *th)
535 {
536   struct GNUNET_MESH_TransmitHandle *p;
537
538   p = h->th_head;
539   while ((NULL != p) && (th->priority <= p->priority))
540     p = p->next;
541   if (NULL == p)
542     p = h->th_tail;
543   else
544     p = p->prev;
545   GNUNET_CONTAINER_DLL_insert_after (h->th_head, h->th_tail, p, th);
546   if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
547     return;
548   th->timeout_task =
549       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
550                                     (th->timeout), &timeout_transmission, th);
551 }
552
553
554 static void
555 send_packet (struct GNUNET_MESH_Handle *h,
556              const struct GNUNET_MessageHeader *msg);
557
558
559 /**
560  * Reconnect to the service, retransmit all infomation to try to restore the
561  * original state.
562  *
563  * @param h handle to the mesh
564  *
565  * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
566  */
567 static int
568 reconnect (struct GNUNET_MESH_Handle *h)
569 {
570   struct GNUNET_MESH_Tunnel *t;
571   unsigned int i;
572
573   h->in_receive = GNUNET_NO;
574   /* disconnect */
575   if (NULL != h->th)
576   {
577     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
578   }
579   if (NULL != h->client)
580   {
581     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
582   }
583
584   /* connect again */
585   h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
586   if (h->client == NULL)
587   {
588     /* FIXME: panic? exponential backoff retry? */
589     GNUNET_break (0);
590     return GNUNET_NO;
591   }
592   /* Rebuild all tunnels */
593   for (t = h->tunnels_head; NULL != t; t = t->next)
594   {
595     struct GNUNET_MESH_TunnelMessage tmsg;
596     struct GNUNET_MESH_PeerControl pmsg;
597
598     tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
599     tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
600     tmsg.tunnel_id = htonl (t->tid);
601     send_packet (h, &tmsg.header);
602
603     pmsg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
604     pmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
605     pmsg.tunnel_id = htonl (t->tid);
606
607     /* Reconnect all peers */
608     for (i = 0; i < t->npeers; i++)
609     {
610       GNUNET_PEER_resolve (t->peers[i]->id, &pmsg.peer);
611       if (NULL != t->disconnect_handler && t->peers[i]->connected)
612         t->disconnect_handler (t->cls, &pmsg.peer);
613       /* If the tunnel was "by type", dont connect individual peers */
614       if (0 == t->napps)
615         send_packet (t->mesh, &pmsg.header);
616     }
617     /* Reconnect all types, if any  */
618     for (i = 0; i < t->napps; i++)
619     {
620       struct GNUNET_MESH_ConnectPeerByType msg;
621
622       msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
623       msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
624       msg.tunnel_id = htonl (t->tid);
625       msg.type = htonl (t->apps[i]);
626       send_packet (t->mesh, &msg.header);
627     }
628   }
629   return GNUNET_YES;
630 }
631
632
633 /******************************************************************************/
634 /***********************      RECEIVE HANDLERS     ****************************/
635 /******************************************************************************/
636
637 /**
638  * Process the new tunnel notification and add it to the tunnels in the handle
639  *
640  * @param h     The mesh handle
641  * @param msg   A message with the details of the new incoming tunnel
642  */
643 static void
644 process_tunnel_create (struct GNUNET_MESH_Handle *h,
645                        const struct GNUNET_MESH_TunnelMessage *msg)
646 {
647   struct GNUNET_MESH_Tunnel *t;
648   struct GNUNET_TRANSPORT_ATS_Information atsi;
649   MESH_TunnelNumber tid;
650
651   tid = ntohl (msg->tunnel_id);
652   if (tid <= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
653   {
654     GNUNET_break (0);
655     return;
656   }
657   t = create_tunnel (h, tid);
658   t->owner = GNUNET_PEER_intern (&msg->peer);
659   t->npeers = 1;
660   t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
661   t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
662   t->peers[0]->t = t;
663   t->peers[0]->connected = 1;
664   t->peers[0]->id = t->owner;
665   t->mesh = h;
666   t->tid = tid;
667   if (NULL != h->new_tunnel)
668   {
669     atsi.type = 0;
670     atsi.value = 0;
671     t->ctx = h->new_tunnel(h->cls, t, &msg->peer, &atsi);
672   }
673   GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
674   return;
675 }
676
677
678 /**
679  * Process the tunnel destroy notification and free associated resources
680  *
681  * @param h     The mesh handle
682  * @param msg   A message with the details of the tunnel being destroyed
683  */
684 static void
685 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
686                         const struct GNUNET_MESH_TunnelMessage *msg)
687 {
688   struct GNUNET_MESH_Tunnel *t;
689   MESH_TunnelNumber tid;
690
691   tid = ntohl (msg->tunnel_id);
692   t = retrieve_tunnel (h, tid);
693
694   if (NULL == t)
695   {
696     GNUNET_break(0);
697     return;
698   }
699   if (0 == t->owner)
700   {
701     GNUNET_break(0);
702   }
703
704   destroy_tunnel(t);
705   return;
706 }
707
708
709 /**
710  * Process the new peer event and notify the upper level of it
711  *
712  * @param h     The mesh handle
713  * @param msg   A message with the details of the peer event
714  */
715 static void
716 process_peer_event (struct GNUNET_MESH_Handle *h,
717                     const struct GNUNET_MESH_PeerControl *msg)
718 {
719   struct GNUNET_MESH_Tunnel *t;
720   struct GNUNET_MESH_Peer *p;
721   struct GNUNET_TRANSPORT_ATS_Information atsi;
722   GNUNET_PEER_Id id;
723   uint16_t size;
724
725   size = ntohs (msg->header.size);
726   if (size != sizeof (struct GNUNET_MESH_PeerControl))
727   {
728     GNUNET_break (0);
729     return;
730   }
731   t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
732   if (NULL == t)
733   {
734     GNUNET_break(0);
735     return;
736   }
737   id = GNUNET_PEER_search (&msg->peer);
738   if ((p = retrieve_peer (t, id)) == NULL)
739     p = add_peer_to_tunnel (t, &msg->peer);
740   if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == msg->header.type)
741   {
742     if (NULL != t->connect_handler)
743     {
744       atsi.type = 0;
745       atsi.value = 0;
746       t->connect_handler (t->cls, &msg->peer, &atsi);
747     }
748     p->connected = 1;
749   }
750   else
751   {
752     if (NULL != t->disconnect_handler && p->connected)
753     {
754       t->disconnect_handler (t->cls, &msg->peer);
755     }
756     remove_peer_from_tunnel (p);
757     GNUNET_free (p);
758   }
759 }
760
761
762 /**
763  * Process the incoming data packets
764  *
765  * @param h     The mesh handle
766  * @param msh   A message encapsulating the data
767  */
768 static void
769 process_incoming_data (struct GNUNET_MESH_Handle *h,
770                        const struct GNUNET_MessageHeader *message)
771 {
772   const struct GNUNET_MessageHeader *payload;
773   const struct GNUNET_MESH_MessageHandler *handler;
774   const struct GNUNET_PeerIdentity *peer;
775   struct GNUNET_MESH_Unicast *ucast;
776   struct GNUNET_MESH_Multicast *mcast;
777   struct GNUNET_MESH_ToOrigin *to_orig;
778   struct GNUNET_MESH_Tunnel *t;
779   uint16_t type;
780   int i;
781
782   type = ntohs (message->type);
783   switch (type)
784   {
785   case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
786     ucast = (struct GNUNET_MESH_Unicast *) message;
787     t = retrieve_tunnel (h, ntohl (ucast->tid));
788     payload = (struct GNUNET_MessageHeader *) &ucast[1];
789     peer = &ucast->oid;
790     break;
791   case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
792     mcast = (struct GNUNET_MESH_Multicast *) message;
793     t = retrieve_tunnel (h, ntohl (mcast->tid));
794     payload = (struct GNUNET_MessageHeader *) &mcast[1];
795     peer = &mcast->oid;
796     break;
797   case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
798     to_orig = (struct GNUNET_MESH_ToOrigin *) message;
799     t = retrieve_tunnel (h, ntohl (to_orig->tid));
800     payload = (struct GNUNET_MessageHeader *) &to_orig[1];
801     peer = &to_orig->sender;
802     break;
803   default:
804     GNUNET_break (0);
805     return;
806   }
807   if (NULL == t)
808   {
809     GNUNET_break (0);
810     return;
811   }
812   for (i = 0; i < h->n_handlers; i++)
813   {
814     handler = &h->message_handlers[i];
815     if (handler->type == type)
816     {
817       struct GNUNET_TRANSPORT_ATS_Information atsi;
818
819       atsi.type = 0;
820       atsi.value = 0;
821       if (GNUNET_OK ==
822           handler->callback (h->cls, t, &t->ctx, peer, payload, &atsi))
823       {
824         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825                     "MESH: callback completed successfully\n");
826       }
827       else
828       {
829         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830                     "MESH: callback caused disconnection\n");
831         GNUNET_MESH_disconnect (h);
832       }
833     }
834   }
835 }
836
837
838 /**
839  * Function to process all messages received from the service
840  *
841  * @param cls closure
842  * @param msg message received, NULL on timeout or fatal error
843  */
844 static void
845 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
846 {
847   struct GNUNET_MESH_Handle *h = cls;
848
849   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n");
850   if (msg == NULL)
851   {
852     reconnect (h);
853     return;
854   }
855   switch (ntohs (msg->type))
856   {
857     /* Notify of a new incoming tunnel */
858   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
859     process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg);
860     break;
861     /* Notify of a tunnel disconnection */
862   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
863     process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
864     break;
865     /* Notify of a new peer or a peer disconnect in the tunnel */
866   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD:
867   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL:
868     process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
869     break;
870     /* Notify of a new data packet in the tunnel */
871   case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
872   case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
873   case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
874     process_incoming_data (h, msg);
875     break;
876     /* We shouldn't get any other packages, log and ignore */
877   default:
878     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
879                 "MESH: unsolicited message form service (type %d)\n",
880                 ntohs (msg->type));
881   }
882   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: message processed\n");
883   GNUNET_CLIENT_receive (h->client, &msg_received, h,
884                          GNUNET_TIME_UNIT_FOREVER_REL);
885 }
886
887
888 /******************************************************************************/
889 /************************       SEND FUNCTIONS     ****************************/
890 /******************************************************************************/
891
892 /**
893  * Function called to send a message to the service.
894  * "buf" will be NULL and "size" zero if the socket was closed for writing in
895  * the meantime.
896  *
897  * @param cls closure, the mesh handle
898  * @param size number of bytes available in buf
899  * @param buf where the callee should write the connect message
900  * @return number of bytes written to buf
901  */
902 static size_t
903 send_callback (void *cls, size_t size, void *buf)
904 {
905   struct GNUNET_MESH_Handle *h = cls;
906   struct GNUNET_MESH_TransmitHandle *th;
907   char *cbuf = buf;
908   size_t tsize;
909   size_t psize;
910
911   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() Buffer %u\n", size);
912   h->th = NULL;
913   if ((0 == size) || (NULL == buf))
914   {
915     reconnect (h);
916     return 0;
917   }
918   tsize = 0;
919   while ((NULL != (th = h->th_head)) && (size >= th->size))
920   {
921 #if DEBUG
922     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh:     type: %u\n",
923                 ntohs (((struct GNUNET_MessageHeader *)&th[1])->type));
924     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh:     size: %u\n",
925                 ntohs (((struct GNUNET_MessageHeader *)&th[1])->size));
926 #endif
927     if (NULL != th->notify)
928     {
929       if (th->target == 0)
930       {
931         /* multicast */
932         struct GNUNET_MESH_Multicast mc;
933
934         GNUNET_assert (size >= sizeof (mc) + th->size);
935         psize =
936             th->notify (th->notify_cls, size - sizeof (mc), &cbuf[sizeof (mc)]);
937         if (psize > 0)
938         {
939           mc.header.size = htons (sizeof (mc) + th->size);
940           mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
941           mc.tid = htonl (th->tunnel->tid);
942           memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity)); /* myself */
943           memcpy (cbuf, &mc, sizeof (mc));
944           psize = th->size + sizeof (mc);
945         }
946       }
947       else
948       {
949         /* unicast */
950         struct GNUNET_MESH_Unicast uc;
951
952         GNUNET_assert (size >= sizeof (uc) + th->size);
953         psize =
954             th->notify (th->notify_cls, size - sizeof (uc), &cbuf[sizeof (uc)]);
955         if (psize > 0)
956         {
957           uc.header.size = htons (sizeof (uc) + th->size);
958           uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
959           uc.tid = htonl (th->tunnel->tid);
960           memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));     /* myself */
961           GNUNET_PEER_resolve (th->target, &uc.destination);
962           memcpy (cbuf, &uc, sizeof (uc));
963           psize = th->size + sizeof (uc);
964         }
965       }
966     }
967     else
968     {
969       memcpy (cbuf, &th[1], th->size);
970       psize = th->size;
971     }
972     if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
973       GNUNET_SCHEDULER_cancel (th->timeout_task);
974     GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
975     GNUNET_free (th);
976     cbuf += psize;
977     size -= psize;
978     tsize += psize;
979   }
980   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh:   total size: %u\n", tsize);
981   if (NULL != (th = h->th_head))
982   {
983     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh:   next size: %u\n", th->size);
984     h->th =
985         GNUNET_CLIENT_notify_transmit_ready (h->client, th->size,
986                                              GNUNET_TIME_UNIT_FOREVER_REL,
987                                              GNUNET_YES, &send_callback, h);
988   }
989   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() END\n");
990   if (GNUNET_NO == h->in_receive)
991   {
992     h->in_receive = GNUNET_YES;
993     GNUNET_CLIENT_receive (h->client, &msg_received, h,
994                            GNUNET_TIME_UNIT_FOREVER_REL);
995   }
996   return tsize;
997 }
998
999
1000 /**
1001  * Auxiliary function to send an already constructed packet to the service.
1002  * Takes care of creating a new queue element, copying the message and
1003  * calling the tmt_rdy function if necessary.
1004  * @param h mesh handle
1005  * @param msg message to transmit
1006  */
1007 static void
1008 send_packet (struct GNUNET_MESH_Handle *h,
1009              const struct GNUNET_MessageHeader *msg)
1010 {
1011   struct GNUNET_MESH_TransmitHandle *th;
1012   size_t msize;
1013
1014   msize = ntohs (msg->size);
1015   th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1016   th->priority = UINT32_MAX;
1017   th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1018   th->size = msize;
1019   memcpy (&th[1], msg, msize);
1020   add_to_queue (h, th);
1021   if (NULL != h->th)
1022     return;
1023   h->th =
1024       GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1025                                            GNUNET_TIME_UNIT_FOREVER_REL,
1026                                            GNUNET_YES, &send_callback, h);
1027 }
1028
1029
1030 /******************************************************************************/
1031 /**********************      API CALL DEFINITIONS     *************************/
1032 /******************************************************************************/
1033
1034 /**
1035  * Connect to the mesh service.
1036  *
1037  * @param cfg configuration to use
1038  * @param queue_size size of the data message queue, shared among all tunnels
1039  *                   (each tunnel is guaranteed to accept at least one message,
1040  *                    no matter what is the status of other tunnels)
1041  * @param cls closure for the various callbacks that follow
1042  *            (including handlers in the handlers array)
1043  * @param new_tunnel function called when an *inbound* tunnel is created
1044  * @param cleaner function called when an *inbound* tunnel is destroyed
1045  * @param handlers callbacks for messages we care about, NULL-terminated
1046  *                note that the mesh is allowed to drop notifications about
1047  *                inbound messages if the client does not process them fast
1048  *                enough (for this notification type, a bounded queue is used)
1049  * @param stypes list of the applications that this client claims to provide
1050  * @return handle to the mesh service NULL on error
1051  *         (in this case, init is never called)
1052  */
1053 struct GNUNET_MESH_Handle *
1054 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1055                      unsigned int queue_size, void *cls,
1056                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1057                      GNUNET_MESH_TunnelEndHandler cleaner,
1058                      const struct GNUNET_MESH_MessageHandler *handlers,
1059                      const GNUNET_MESH_ApplicationType *stypes)
1060 {
1061   struct GNUNET_MESH_Handle *h;
1062   struct GNUNET_MESH_ClientConnect *msg;
1063   GNUNET_MESH_ApplicationType *apps;
1064   uint16_t napps;
1065   uint16_t *types;
1066   uint16_t ntypes;
1067   size_t size;
1068
1069   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect()\n");
1070   h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1071   h->cfg = cfg;
1072   h->max_queue_size = queue_size;
1073   h->new_tunnel = new_tunnel;
1074   h->cleaner = cleaner;
1075   h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1076   if (h->client == NULL)
1077   {
1078     GNUNET_break (0);
1079     GNUNET_free (h);
1080     return NULL;
1081   }
1082   h->cls = cls;
1083   h->message_handlers = handlers;
1084   h->applications = stypes;
1085   h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1086
1087   /* count handlers and apps, calculate size */
1088   for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
1089   for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
1090   size = sizeof (struct GNUNET_MESH_ClientConnect);
1091   size += h->n_handlers * sizeof (uint16_t);
1092   size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
1093
1094   {
1095     char buf[size];
1096
1097     /* build connection packet */
1098     msg = (struct GNUNET_MESH_ClientConnect *) buf;
1099     msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
1100     msg->header.size = htons (size);
1101     types = (uint16_t *) & msg[1];
1102     for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
1103       types[ntypes] = h->message_handlers[ntypes].type;
1104     apps = (GNUNET_MESH_ApplicationType *) &types[ntypes];
1105     for (napps = 0; napps < h->n_applications; napps++)
1106       apps[napps] = h->applications[napps];
1107     msg->applications = htons (napps);
1108     msg->types = htons (ntypes);
1109     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110                 "mesh: Sending %lu bytes long message %d types and %d apps\n",
1111                 ntohs (msg->header.size), ntypes, napps);
1112     send_packet (h, &msg->header);
1113   }
1114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect() END\n");
1115   return h;
1116 }
1117
1118
1119 /**
1120  * Disconnect from the mesh service.
1121  *
1122  * @param handle connection to mesh to disconnect
1123  */
1124 void
1125 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1126 {
1127   struct GNUNET_MESH_Tunnel *t;
1128
1129   for (t = handle->tunnels_head; NULL != t; t = t->next)
1130   {
1131     destroy_tunnel (t);
1132   }
1133   if (NULL != handle->th)
1134   {
1135     GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1136   }
1137   if (NULL != handle->client)
1138   {
1139     GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1140   }
1141   GNUNET_free (handle);
1142 }
1143
1144
1145 /**
1146  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1147  * and to broadcast).
1148  *
1149  * @param h mesh handle
1150  * @param tunnel_ctx client's tunnel context to associate with the tunnel
1151  * @param connect_handler function to call when peers are actually connected
1152  * @param disconnect_handler function to call when peers are disconnected
1153  * @param handler_cls closure for connect/disconnect handlers
1154  */
1155 struct GNUNET_MESH_Tunnel *
1156 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
1157                            GNUNET_MESH_TunnelConnectHandler connect_handler,
1158                            GNUNET_MESH_TunnelDisconnectHandler
1159                            disconnect_handler, void *handler_cls)
1160 {
1161   struct GNUNET_MESH_Tunnel *t;
1162   struct GNUNET_MESH_TunnelMessage msg;
1163
1164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
1165   t = create_tunnel (h, 0);
1166   t->connect_handler = connect_handler;
1167   t->disconnect_handler = disconnect_handler;
1168   t->cls = handler_cls;
1169   t->ctx = tunnel_ctx;
1170   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1171   msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1172   msg.tunnel_id = htonl (t->tid);
1173   send_packet (h, &msg.header);
1174   return t;
1175 }
1176
1177
1178 /**
1179  * Destroy an existing tunnel.
1180  *
1181  * @param tun tunnel handle
1182  */
1183 void
1184 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *t)
1185 {
1186   struct GNUNET_MESH_Handle *h;
1187   struct GNUNET_MESH_TunnelMessage msg;
1188
1189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
1190   h = t->mesh;
1191
1192   if (0 != t->owner)
1193     GNUNET_PEER_change_rc (t->owner, -1);
1194
1195   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1196   msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1197   msg.tunnel_id = htonl (t->tid);
1198   destroy_tunnel (t);
1199   send_packet (h, &msg.header);
1200 }
1201
1202
1203 /**
1204  * Request that a peer should be added to the tunnel.  The existing
1205  * connect handler will be called ONCE with either success or failure.
1206  * This function should NOT be called again with the same peer before the
1207  * connect handler is called.
1208  *
1209  * @param tunnel handle to existing tunnel
1210  * @param timeout how long to try to establish a connection
1211  * @param peer peer to add
1212  */
1213 void
1214 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
1215                                       const struct GNUNET_PeerIdentity *peer)
1216 {
1217   struct GNUNET_MESH_PeerControl msg;
1218   GNUNET_PEER_Id peer_id;
1219   unsigned int i;
1220
1221   peer_id = GNUNET_PEER_intern (peer);
1222   for (i = 0; i < tunnel->npeers; i++)
1223   {
1224     if (tunnel->peers[i]->id == peer_id)
1225     {
1226       GNUNET_PEER_change_rc (peer_id, -1);
1227       GNUNET_break (0);
1228       return;
1229     }
1230   }
1231   if (NULL == add_peer_to_tunnel (tunnel, peer))
1232     return;
1233
1234   msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1235   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1236   msg.tunnel_id = htonl (tunnel->tid);
1237   msg.peer = *peer;
1238   send_packet (tunnel->mesh, &msg.header);
1239
1240   return;
1241 }
1242
1243
1244 /**
1245  * Request that a peer should be removed from the tunnel.  The existing
1246  * disconnect handler will be called ONCE if we were connected.
1247  *
1248  * @param tunnel handle to existing tunnel
1249  * @param peer peer to remove
1250  */
1251 void
1252 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
1253                                       const struct GNUNET_PeerIdentity *peer)
1254 {
1255   struct GNUNET_MESH_PeerControl msg;
1256   GNUNET_PEER_Id peer_id;
1257   unsigned int i;
1258
1259   peer_id = GNUNET_PEER_search (peer);
1260   if (0 == peer_id)
1261   {
1262     GNUNET_break (0);
1263     return;
1264   }
1265   for (i = 0; i < tunnel->npeers; i++)
1266     if (tunnel->peers[i]->id == peer_id)
1267       break;
1268   if (i == tunnel->npeers)
1269   {
1270     GNUNET_break (0);
1271     return;
1272   }
1273   if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
1274     tunnel->disconnect_handler (tunnel->cls, peer);
1275   GNUNET_PEER_change_rc (peer_id, -1);
1276   GNUNET_free (tunnel->peers[i]);
1277   tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
1278   GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
1279
1280   msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1281   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1282   msg.tunnel_id = htonl (tunnel->tid);
1283   memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1284   send_packet (tunnel->mesh, &msg.header);
1285 }
1286
1287
1288 /**
1289  * Request that the mesh should try to connect to a peer supporting the given
1290  * message type.
1291  *
1292  * @param tunnel handle to existing tunnel
1293  * @param app_type application type that must be supported by the peer (MESH
1294  *                 should discover peer in proximity handling this type)
1295  */
1296 void
1297 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
1298                                           GNUNET_MESH_ApplicationType app_type)
1299 {
1300   struct GNUNET_MESH_ConnectPeerByType msg;
1301
1302   GNUNET_array_append (tunnel->apps, tunnel->napps, app_type);
1303
1304   msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
1305   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
1306   msg.tunnel_id = htonl (tunnel->tid);
1307   msg.type = htonl (app_type);
1308   send_packet (tunnel->mesh, &msg.header);
1309 }
1310
1311
1312 /**
1313  * Ask the mesh to call "notify" once it is ready to transmit the
1314  * given number of bytes to the specified "target".  If we are not yet
1315  * connected to the specified peer, a call to this function will cause
1316  * us to try to establish a connection.
1317  *
1318  * @param tunnel tunnel to use for transmission
1319  * @param cork is corking allowed for this transmission?
1320  * @param priority how important is the message?
1321  * @param maxdelay how long can the message wait?
1322  * @param target destination for the message,
1323  *               NULL for multicast to all tunnel targets
1324  * @param notify_size how many bytes of buffer space does notify want?
1325  * @param notify function to call when buffer space is available;
1326  *        will be called with NULL on timeout or if the overall queue
1327  *        for this peer is larger than queue_size and this is currently
1328  *        the message with the lowest priority
1329  * @param notify_cls closure for notify
1330  * @return non-NULL if the notify callback was queued,
1331  *         NULL if we can not even queue the request (insufficient
1332  *         memory); if NULL is returned, "notify" will NOT be called.
1333  */
1334 struct GNUNET_MESH_TransmitHandle *
1335 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1336                                    uint32_t priority,
1337                                    struct GNUNET_TIME_Relative maxdelay,
1338                                    const struct GNUNET_PeerIdentity *target,
1339                                    size_t notify_size,
1340                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
1341                                    void *notify_cls)
1342 {
1343   struct GNUNET_MESH_TransmitHandle *th;
1344   size_t overhead;
1345
1346   GNUNET_assert(NULL != notify);
1347   if (tunnel->mesh->npackets >= tunnel->mesh->max_queue_size &&
1348       tunnel->npackets > 0)
1349     return NULL;                /* queue full */
1350   tunnel->npackets++;
1351   tunnel->mesh->npackets++;
1352   th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1353   th->tunnel = tunnel;
1354   th->priority = priority;
1355   th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1356   th->target = GNUNET_PEER_intern (target);
1357   overhead =
1358       (NULL ==
1359        target) ? sizeof (struct GNUNET_MESH_Multicast) : sizeof (struct
1360                                                                  GNUNET_MESH_Unicast);
1361   th->size = notify_size + overhead;
1362   th->notify = notify;
1363   th->notify_cls = notify_cls;
1364   add_to_queue (tunnel->mesh, th);
1365   return th;
1366 }
1367
1368
1369 /**
1370  * Cancel the specified transmission-ready notification.
1371  *
1372  * @param th handle that was returned by "notify_transmit_ready".
1373  */
1374 void
1375 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1376 {
1377   struct GNUNET_MESH_Handle *mesh;
1378
1379   mesh = th->tunnel->mesh;
1380   if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1381     GNUNET_SCHEDULER_cancel (th->timeout_task);
1382   GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1383   GNUNET_free (th);
1384   if ((NULL == mesh->th_head) && (NULL != mesh->th))
1385   {
1386     /* queue empty, no point in asking for transmission */
1387     GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1388     mesh->th = NULL;
1389   }
1390 }
1391
1392
1393 #if 0                           /* keep Emacsens' auto-indent happy */
1394 {
1395 #endif
1396 #ifdef __cplusplus
1397 }
1398 #endif