Indent
[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
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_new.c
23  * @brief mesh api: client implementation of mesh service
24  * @author Bartlomiej Polot
25  *
26  * STRUCTURE:
27  * - CONSTANTS
28  * - DATA STRUCTURES
29  * - AUXILIARY FUNCTIONS
30  * - RECEIVE HANDLERS
31  * - SEND FUNCTIONS
32  * - API CALL DEFINITIONS
33  */
34
35 #ifdef __cplusplus
36
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44
45 #include "platform.h"
46 #include "gnunet_common.h"
47 #include "gnunet_client_lib.h"
48 #include "gnunet_util_lib.h"
49 #include "gnunet_peer_lib.h"
50 #include "gnunet_mesh_service_new.h"
51 #include "mesh.h"
52 #include "mesh_protocol.h"
53
54 #define MESH_API_MAX_QUEUE 10
55
56 /******************************************************************************/
57 /************************      DATA STRUCTURES     ****************************/
58 /******************************************************************************/
59
60 /**
61  * Transmission queue to the service
62  */
63 struct GNUNET_MESH_queue
64 {
65     /**
66      * Double Linked list
67      */
68   struct GNUNET_MESH_queue *next;
69   struct GNUNET_MESH_queue *prev;
70
71     /**
72      * Size of the data to follow
73      */
74   uint16_t size;
75
76     /**
77      * Data itself
78      */
79   void *data;
80 };
81
82
83 /**
84  * Opaque handle to the service.
85  */
86 struct GNUNET_MESH_Handle
87 {
88     /**
89      * Handle to the server connection, to send messages later
90      */
91   struct GNUNET_CLIENT_Connection *client;
92
93     /**
94      * Set of handlers used for processing incoming messages in the tunnels
95      */
96   const struct GNUNET_MESH_MessageHandler *message_handlers;
97   int n_handlers;
98
99     /**
100      * Set of applications that should be claimed to be offered at this node.
101      * Note that this is just informative, the appropiate handlers must be
102      * registered independently and the mapping is up to the developer of the
103      * client application.
104      */
105   const GNUNET_MESH_ApplicationType *applications;
106   int n_applications;
107
108     /**
109      * Double linked list of the tunnels this client is connected to.
110      */
111   struct GNUNET_MESH_Tunnel *tunnels_head;
112   struct GNUNET_MESH_Tunnel *tunnels_tail;
113
114     /**
115      * tid of the next tunnel to create (to avoid reusing IDs often)
116      */
117   MESH_TunnelNumber next_tid;
118
119     /**
120      * Callback for tunnel disconnection
121      */
122   GNUNET_MESH_TunnelEndHandler *cleaner;
123
124     /**
125      * Handle to cancel pending transmissions in case of disconnection
126      */
127   struct GNUNET_CLIENT_TransmitHandle *th;
128
129     /**
130      * Closure for all the handlers given by the client
131      */
132   void *cls;
133
134     /**
135      * Messages to send to the service
136      */
137   struct GNUNET_MESH_queue *queue_head;
138   struct GNUNET_MESH_queue *queue_tail;
139 };
140
141 /**
142  * Opaque handle to a tunnel.
143  */
144 struct GNUNET_MESH_Tunnel
145 {
146
147     /**
148      * DLL
149      */
150   struct GNUNET_MESH_Tunnel *next;
151   struct GNUNET_MESH_Tunnel *prev;
152
153     /**
154      * Local ID of the tunnel
155      */
156   MESH_TunnelNumber tid;
157
158     /**
159      * Owner of the tunnel
160      */
161   GNUNET_PEER_Id owner;
162
163     /**
164      * Callback to execute when peers connect to the tunnel
165      */
166   GNUNET_MESH_TunnelConnectHandler connect_handler;
167
168     /**
169      * Callback to execute when peers disconnect to the tunnel
170      */
171   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
172
173     /**
174      * All peers added to the tunnel
175      */
176   GNUNET_PEER_Id *peers;
177
178     /**
179      * Number of peer added to the tunnel
180      */
181   uint32_t npeers;
182
183     /**
184      * Closure for the connect/disconnect handlers
185      */
186   void *cls;
187
188     /**
189      * Handle to the mesh this tunnel belongs to
190      */
191   struct GNUNET_MESH_Handle *mesh;
192 };
193
194 struct GNUNET_MESH_TransmitHandle
195 {
196   struct GNUNET_MESH_Tunnel *t;
197   struct GNUNET_MESH_queue *q;
198 };
199
200 /******************************************************************************/
201 /***********************     AUXILIARY FUNCTIONS      *************************/
202 /******************************************************************************/
203
204 /**
205  * Get the tunnel handler for the tunnel specified by id from the given handle
206  * @param h Mesh handle
207  * @param tid ID of the wanted tunnel
208  * @return handle to the required tunnel or NULL if not found
209  */
210 static struct GNUNET_MESH_Tunnel *
211 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
212 {
213   struct GNUNET_MESH_Tunnel *t;
214
215   t = h->tunnels_head;
216   while (t != NULL)
217   {
218     if (t->tid == tid)
219       return t;
220     t = t->next;
221   }
222   return NULL;
223 }
224
225 /**
226  * Get the length of the transmission queue
227  * @param h mesh handle whose queue is to be measured
228  */
229 static uint32_t
230 get_queue_length (struct GNUNET_MESH_Handle *h)
231 {
232   struct GNUNET_MESH_queue *q;
233   uint32_t i;
234
235   /* count */
236   for (q = h->queue_head, i = 0; NULL != q; q = q->next, i++) ;
237
238   return i;
239 }
240
241
242 /******************************************************************************/
243 /***********************      RECEIVE HANDLERS     ****************************/
244 /******************************************************************************/
245
246 /**
247  * Process the new tunnel notification and add it to the tunnels in the handle
248  *
249  * @param h     The mesh handle
250  * @param msg   A message with the details of the new incoming tunnel
251  */
252 static void
253 process_tunnel_create (struct GNUNET_MESH_Handle *h,
254                        const struct GNUNET_MESH_TunnelMessage *msg)
255 {
256   struct GNUNET_MESH_Tunnel *t;
257   MESH_TunnelNumber tid;
258
259   tid = ntohl (msg->tunnel_id);
260   if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)
261   {
262     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263                 "MESH: received an incoming tunnel with tid in local range (%X)\n",
264                 tid);
265     GNUNET_break_op (0);
266     return;                     //FIXME abort? reconnect?
267   }
268   t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
269   t->cls = h->cls;
270   t->mesh = h;
271   t->tid = tid;
272
273   return;
274 }
275
276
277 /**
278  * Process the new peer event and notify the upper level of it
279  *
280  * @param h     The mesh handle
281  * @param msg   A message with the details of the peer event
282  */
283 static void
284 process_peer_event (struct GNUNET_MESH_Handle *h,
285                     const struct GNUNET_MESH_PeerControl *msg)
286 {
287   struct GNUNET_MESH_Tunnel *t;
288   uint16_t size;
289
290   size = ntohs (msg->header.size);
291   if (size != sizeof (struct GNUNET_MESH_PeerControl))
292   {
293     GNUNET_break_op (0);
294     return;
295   }
296   t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
297   if (NULL == t)
298   {
299     GNUNET_break_op (0);
300     return;
301   }
302   if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED == msg->header.type)
303   {
304     if (NULL != t->connect_handler)
305     {
306       t->connect_handler (t->cls, &msg->peer, NULL);    /* FIXME atsi */
307     }
308   }
309   else
310   {
311     if (NULL != t->disconnect_handler)
312     {
313       t->disconnect_handler (t->cls, &msg->peer);
314     }
315   }
316 }
317
318
319 /**
320  * Process the incoming data packets
321  *
322  * @param h     The mesh handle
323  * @param msh   A message encapsulating the data
324  */
325 static void
326 process_incoming_data (struct GNUNET_MESH_Handle *h,
327                        const struct GNUNET_MessageHeader *message)
328 {
329   const struct GNUNET_MessageHeader *payload;
330   const struct GNUNET_MESH_MessageHandler *handler;
331   const struct GNUNET_PeerIdentity *peer;
332   struct GNUNET_MESH_Unicast *ucast;
333   struct GNUNET_MESH_Multicast *mcast;
334   struct GNUNET_MESH_ToOrigin *to_orig;
335   struct GNUNET_MESH_Tunnel *t;
336   uint16_t type;
337   int i;
338
339   type = ntohs (message->type);
340   switch (type)
341   {
342   case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
343     ucast = (struct GNUNET_MESH_Unicast *) message;
344     t = retrieve_tunnel (h, ntohl (ucast->tid));
345     payload = (struct GNUNET_MessageHeader *) &ucast[1];
346     peer = &ucast->oid;
347     break;
348   case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
349     mcast = (struct GNUNET_MESH_Multicast *) message;
350     t = retrieve_tunnel (h, ntohl (mcast->tid));
351     payload = (struct GNUNET_MessageHeader *) &mcast[1];
352     peer = &mcast->oid;
353     break;
354   case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
355     to_orig = (struct GNUNET_MESH_ToOrigin *) message;
356     t = retrieve_tunnel (h, ntohl (to_orig->tid));
357     payload = (struct GNUNET_MessageHeader *) &to_orig[1];
358     peer = &to_orig->sender;
359     break;
360   default:
361     GNUNET_break_op (0);
362     return;
363   }
364   if (NULL == t)
365   {
366     GNUNET_break_op (0);
367     return;
368   }
369   for (i = 0; i < h->n_handlers; i++)
370   {
371     handler = &h->message_handlers[i];
372     if (handler->type == type)
373     {
374       if (GNUNET_OK == handler->callback (h->cls, t, NULL,      /* FIXME ctx */
375                                           peer, payload, NULL)) /* FIXME atsi */
376       {
377         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378                     "MESH: callback completed successfully\n");
379       }
380       else
381       {
382         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383                     "MESH: callback caused disconnection\n");
384         GNUNET_MESH_disconnect (h);
385       }
386     }
387   }
388   return;
389 }
390
391
392 /**
393  * Function to process all messages received from the service
394  *
395  * @param cls closure
396  * @param msg message received, NULL on timeout or fatal error
397  */
398 static void
399 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
400 {
401   struct GNUNET_MESH_Handle *h = cls;
402
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n");
404   if (msg == NULL)
405   {
406     GNUNET_break_op (0);
407     return;
408   }
409
410   switch (ntohs (msg->type))
411   {
412     /* Notify of a new incoming tunnel */
413   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
414     process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg);
415     break;
416     /* Notify of a new peer or a peer disconnect in the tunnel */
417   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED:
418   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED:
419     process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
420     break;
421     /* Notify of a new data packet in the tunnel */
422   case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
423   case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
424   case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
425     process_incoming_data (h, msg);
426     break;
427     /* We shouldn't get any other packages, log and ignore */
428   default:
429     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
430                 "MESH: unsolicited message form service (type %d)\n",
431                 ntohs (msg->type));
432   }
433
434   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: message processed\n");
435   GNUNET_CLIENT_receive (h->client, &msg_received, h,
436                          GNUNET_TIME_UNIT_FOREVER_REL);
437   return;
438 }
439
440
441 /******************************************************************************/
442 /************************       SEND FUNCTIONS     ****************************/
443 /******************************************************************************/
444
445 /**
446  * Function called to send a message to the service.
447  * "buf" will be NULL and "size" zero if the socket was closed for writing in
448  * the meantime.
449  *
450  * @param cls closure, the mesh handle
451  * @param size number of bytes available in buf
452  * @param buf where the callee should write the connect message
453  * @return number of bytes written to buf
454  */
455 static size_t
456 send_raw (void *cls, size_t size, void *buf)
457 {
458   struct GNUNET_MESH_Handle *h = cls;
459   struct GNUNET_MESH_queue *q;
460
461   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() Buffer %u\n", size);
462   h->th = NULL;
463   if (0 == size || NULL == buf)
464   {
465     // FIXME: disconnect, reconnect, retry?
466     return 0;
467   }
468   q = h->queue_head;
469   if (sizeof (struct GNUNET_MessageHeader) > size)
470   {
471     GNUNET_break (0);
472     GNUNET_assert (sizeof (struct GNUNET_MessageHeader) > q->size);
473     h->th =
474         GNUNET_CLIENT_notify_transmit_ready (h->client, q->size,
475                                              GNUNET_TIME_UNIT_FOREVER_REL,
476                                              GNUNET_YES, &send_raw, h);
477     return 0;
478   }
479   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh:   type: %i\n",
480               ntohs (((struct GNUNET_MessageHeader *) q->data)->type));
481   memcpy (buf, q->data, q->size);
482   GNUNET_free (q->data);
483   size = q->size;
484   GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, q);
485   GNUNET_free (q);
486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh:   size: %u\n", size);
487
488   if (NULL != h->queue_head)
489   {
490     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh:   next size: %u\n",
491                 h->queue_head->size);
492     h->th =
493         GNUNET_CLIENT_notify_transmit_ready (h->client, h->queue_head->size,
494                                              GNUNET_TIME_UNIT_FOREVER_REL,
495                                              GNUNET_YES, &send_raw, h);
496   }
497   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() END\n");
498
499   return size;
500 }
501
502 /**
503  * Auxiliary function to send a packet to the service
504  * Takes care of creating a new queue element and calling the tmt_rdy function
505  * if necessary.
506  * @param h mesh handle
507  * @param size size of the packet to transmit
508  * @param data packet itself
509  */
510 static void
511 send_packet (struct GNUNET_MESH_Handle *h, size_t size, void *data)
512 {
513   struct GNUNET_MESH_queue *q;
514
515   q = GNUNET_malloc (sizeof (struct GNUNET_MESH_queue));
516   q->size = size;
517   q->data = data;
518   GNUNET_CONTAINER_DLL_insert_tail (h->queue_head, h->queue_tail, q);
519   if (NULL == h->th)
520   {
521     h->th =
522         GNUNET_CLIENT_notify_transmit_ready (h->client, size,
523                                              GNUNET_TIME_UNIT_FOREVER_REL,
524                                              GNUNET_YES, &send_raw, h);
525   }
526 }
527
528 /******************************************************************************/
529 /**********************      API CALL DEFINITIONS     *************************/
530 /******************************************************************************/
531
532 /**
533  * Connect to the mesh service.
534  *
535  * @param cfg configuration to use
536  * @param cls closure for the various callbacks that follow
537  *            (including handlers in the handlers array)
538  * @param cleaner function called when an *inbound* tunnel is destroyed
539  * @param handlers callbacks for messages we care about, NULL-terminated
540  *                 note that the mesh is allowed to drop notifications about
541  *                 inbound messages if the client does not process them fast
542  *                 enough (for this notification type, a bounded queue is used)
543  * @param stypes Application Types the client claims to offer
544  * @return handle to the mesh service
545  *         NULL on error (in this case, init is never called)
546  */
547 struct GNUNET_MESH_Handle *
548 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
549                      GNUNET_MESH_TunnelEndHandler cleaner,
550                      const struct GNUNET_MESH_MessageHandler *handlers,
551                      const GNUNET_MESH_ApplicationType *stypes)
552 {
553   struct GNUNET_MESH_Handle *h;
554   struct GNUNET_MESH_ClientConnect *msg;
555   GNUNET_MESH_ApplicationType *apps;
556   uint16_t napps;
557   uint16_t *types;
558   uint16_t ntypes;
559   size_t size;
560
561   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect()\n");
562   h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
563
564   h->cleaner = cleaner;
565   h->client = GNUNET_CLIENT_connect ("mesh", cfg);
566   if (h->client == NULL)
567   {
568     GNUNET_break (0);
569     GNUNET_free (h);
570     return NULL;
571   }
572
573   h->cls = cls;
574   h->message_handlers = handlers;
575   h->applications = stypes;
576   h->next_tid = 0x80000000;
577
578   /* count handlers and apps, calculate size */
579   for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
580   for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
581   size = sizeof (struct GNUNET_MESH_ClientConnect);
582   size += h->n_handlers * sizeof (uint16_t);
583   size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
584
585   /* build connection packet */
586   msg = GNUNET_malloc (size);
587   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
588   msg->header.size = htons (size);
589   types = (uint16_t *) & msg[1];
590   for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
591   {
592     types[ntypes] = h->message_handlers[ntypes].type;
593   }
594   apps = (GNUNET_MESH_ApplicationType *) &types[ntypes];
595   for (napps = 0; napps < h->n_applications; napps++)
596   {
597     apps[napps] = h->applications[napps];
598   }
599   msg->applications = htons (napps);
600   msg->types = htons (ntypes);
601
602   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603               "mesh: Sending %lu bytes long message %d types and %d apps\n",
604               ntohs (msg->header.size), ntypes, napps);
605
606   send_packet (h, size, msg);
607
608   GNUNET_CLIENT_receive (h->client, &msg_received, h,
609                          GNUNET_TIME_UNIT_FOREVER_REL);
610
611   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect() END\n");
612
613   return h;
614 }
615
616
617 /**
618  * Disconnect from the mesh service.
619  *
620  * @param handle connection to mesh to disconnect
621  */
622 void
623 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
624 {
625   if (NULL != handle->th)
626   {
627     GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
628   }
629   if (NULL != handle->client)
630   {
631     GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
632   }
633   GNUNET_free (handle);
634 }
635
636
637 /**
638  * Create a new tunnel (we're initiator and will be allowed to add/remove peers
639  * and to broadcast).
640  *
641  * @param h mesh handle
642  * @param connect_handler function to call when peers are actually connected
643  * @param disconnect_handler function to call when peers are disconnected
644  * @param handler_cls closure for connect/disconnect handlers
645  */
646 struct GNUNET_MESH_Tunnel *
647 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
648                            GNUNET_MESH_TunnelConnectHandler connect_handler,
649                            GNUNET_MESH_TunnelDisconnectHandler
650                            disconnect_handler, void *handler_cls)
651 {
652   struct GNUNET_MESH_Tunnel *t;
653   struct GNUNET_MESH_TunnelMessage *msg;
654
655   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
656   t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
657
658   t->connect_handler = connect_handler;
659   t->disconnect_handler = disconnect_handler;
660   t->cls = handler_cls;
661   t->mesh = h;
662   t->tid = h->next_tid++;
663   h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;      // keep in range
664
665   msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_TunnelMessage));
666   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
667   msg->header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
668   msg->tunnel_id = htonl (t->tid);
669
670   send_packet (h, sizeof (struct GNUNET_MESH_TunnelMessage), msg);
671
672   return t;
673 }
674
675
676 /**
677  * Destroy an existing tunnel.
678  *
679  * @param tun tunnel handle
680  */
681 void
682 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun)
683 {
684   struct GNUNET_MESH_Handle *h;
685   struct GNUNET_MESH_TunnelMessage *msg;
686
687   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
688
689   h = tun->mesh;
690   msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_TunnelMessage));
691   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
692   msg->header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
693   msg->tunnel_id = htonl (tun->tid);
694
695   GNUNET_free (tun);
696
697   send_packet (h, sizeof (struct GNUNET_MESH_TunnelMessage), msg);
698 }
699
700
701 /**
702  * Request that a peer should be added to the tunnel.  The existing
703  * connect handler will be called ONCE with either success or failure.
704  *
705  * @param tunnel handle to existing tunnel
706  * @param timeout how long to try to establish a connection
707  * @param peer peer to add
708  */
709 void
710 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
711                                       struct GNUNET_TIME_Relative timeout,
712                                       const struct GNUNET_PeerIdentity *peer)
713 {
714   struct GNUNET_MESH_PeerControl *msg;
715   GNUNET_PEER_Id peer_id;
716   int i;
717
718   peer_id = GNUNET_PEER_intern (peer);
719   for (i = 0; i < tunnel->npeers; i++)
720   {
721     if (tunnel->peers[i] == peer_id)
722     {
723       GNUNET_PEER_change_rc (peer_id, -1);
724       return;
725     }
726   }
727   tunnel->npeers++;
728   tunnel->peers =
729       GNUNET_realloc (tunnel->peers, tunnel->npeers * sizeof (GNUNET_PEER_Id));
730   tunnel->peers[tunnel->npeers - 1] = peer_id;
731
732   msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PeerControl));
733   msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
734   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD);
735   msg->tunnel_id = htonl (tunnel->tid);
736   memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity));
737
738   send_packet (tunnel->mesh, sizeof (struct GNUNET_MESH_PeerControl), msg);
739
740 //   tunnel->connect_handler (tunnel->cls, peer, NULL); FIXME call this later
741 //   TODO: remember timeout
742   return;
743 }
744
745
746 /**
747  * Request that a peer should be removed from the tunnel.  The existing
748  * disconnect handler will be called ONCE if we were connected.
749  *
750  * @param tunnel handle to existing tunnel
751  * @param peer peer to remove
752  */
753 void
754 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
755                                       const struct GNUNET_PeerIdentity *peer)
756 {
757   struct GNUNET_MESH_PeerControl *msg;
758   GNUNET_PEER_Id peer_id;
759   int i;
760
761   peer_id = GNUNET_PEER_search (peer);
762   if (0 == peer_id)
763     return;
764   for (i = 0; i < tunnel->npeers; i++)
765   {
766     if (tunnel->peers[i] == peer_id)
767     {
768       GNUNET_PEER_change_rc (peer_id, -1);
769       tunnel->npeers--;
770       while (i < tunnel->npeers)
771       {
772         tunnel->peers[i] = tunnel->peers[i + 1];
773         i++;
774       }
775       tunnel->peers =
776           GNUNET_realloc (tunnel->peers,
777                           tunnel->npeers * sizeof (GNUNET_PEER_Id));
778       msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PeerControl));
779       msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
780       msg->header.type =
781           htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL);
782       msg->tunnel_id = htonl (tunnel->tid);
783       memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity));
784
785       send_packet (tunnel->mesh, sizeof (struct GNUNET_MESH_PeerControl), msg);
786
787       return;
788     }
789   }
790   //   TODO: remember timeout
791   return;
792 }
793
794
795 /**
796  * Request that the mesh should try to connect to a peer supporting the given
797  * message type.
798  *
799  * @param tunnel handle to existing tunnel
800  * @param timeout how long to try to establish a connection
801  * @param app_type application type that must be supported by the peer (MESH
802  *                 should discover peer in proximity handling this type)
803  */
804 void
805 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
806                                           struct GNUNET_TIME_Relative timeout,
807                                           GNUNET_MESH_ApplicationType app_type)
808 {
809   struct GNUNET_MESH_ConnectPeerByType *msg;
810
811   msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_ConnectPeerByType));
812   msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
813   msg->header.type =
814       htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE);
815   msg->tunnel_id = htonl (tunnel->tid);
816   msg->type = htonl (app_type);
817
818   send_packet (tunnel->mesh, sizeof (struct GNUNET_MESH_ConnectPeerByType),
819                msg);
820   //   TODO: remember timeout
821   return;
822 }
823
824
825 /**
826  * Ask the mesh to call "notify" once it is ready to transmit the
827  * given number of bytes to the specified "target".  If we are not yet
828  * connected to the specified peer, a call to this function will cause
829  * us to try to establish a connection.
830  *
831  * @param tunnel tunnel to use for transmission
832  * @param cork is corking allowed for this transmission?
833  * @param priority how important is the message?
834  * @param maxdelay how long can the message wait?
835  * @param target destination for the message,
836  *               NULL for multicast to all tunnel targets
837  * @param notify_size how many bytes of buffer space does notify want?
838  * @param notify function to call when buffer space is available;
839  *        will be called with NULL on timeout or if the overall queue
840  *        for this peer is larger than queue_size and this is currently
841  *        the message with the lowest priority
842  * @param notify_cls closure for notify
843  * @return non-NULL if the notify callback was queued,
844  *         NULL if we can not even queue the request (insufficient
845  *         memory); if NULL is returned, "notify" will NOT be called.
846  */
847 struct GNUNET_MESH_TransmitHandle *
848 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
849                                    uint32_t priority,
850                                    struct GNUNET_TIME_Relative maxdelay,
851                                    const struct GNUNET_PeerIdentity *target,
852                                    size_t notify_size,
853                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
854                                    void *notify_cls)
855 {
856   struct GNUNET_MESH_TransmitHandle *handle;
857
858   handle = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
859   handle->t = tunnel;
860   handle->q = GNUNET_malloc (sizeof (struct GNUNET_MESH_queue));
861   handle->q->size = notify_size;
862   handle->q->data = GNUNET_malloc (notify_size);
863
864   if (get_queue_length (tunnel->mesh) < MESH_API_MAX_QUEUE)
865   {
866     notify (notify_cls, notify_size, handle->q->data);
867     GNUNET_CONTAINER_DLL_insert_tail (tunnel->mesh->queue_head,
868                                       tunnel->mesh->queue_tail, handle->q);
869   }
870   else
871   {
872     // TODO dataless - queue
873   }
874
875   return handle;
876 }
877
878
879 /**
880  * Cancel the specified transmission-ready notification.
881  *
882  * @param th handle that was returned by "notify_transmit_ready".
883  */
884 void
885 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
886 {
887   GNUNET_CONTAINER_DLL_remove (th->t->mesh->queue_head, th->t->mesh->queue_tail,
888                                th->q);
889   // TODO remove from dataless queue
890   GNUNET_free (th->q->data);
891   GNUNET_free (th->q);
892   GNUNET_free (th);
893 }
894
895
896 #if 0                           /* keep Emacsens' auto-indent happy */
897 {
898 #endif
899 #ifdef __cplusplus
900 }
901 #endif