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.
19 * @file mesh/mesh_api_enc.c
20 * @brief mesh api: client implementation of new mesh service
21 * @author Bartlomiej Polot
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_peer_lib.h"
29 #include "gnunet_mesh_service_enc.h"
31 #include "mesh_protocol.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
34 #define DEBUG_ACK GNUNET_YES
36 /******************************************************************************/
37 /************************ DATA STRUCTURES ****************************/
38 /******************************************************************************/
41 * Transmission queue to the service
43 struct GNUNET_MESH_TransmitHandle
49 struct GNUNET_MESH_TransmitHandle *next;
54 struct GNUNET_MESH_TransmitHandle *prev;
57 * Tunnel this message is sent on / for (may be NULL for control messages).
59 struct GNUNET_MESH_Tunnel *tunnel;
62 * Callback to obtain the message to transmit, or NULL if we
63 * got the message in 'data'. Notice that messages built
64 * by 'notify' need to be encapsulated with information about
67 GNUNET_CONNECTION_TransmitReadyNotify notify;
70 * Closure for 'notify'
75 * How long is this message valid. Once the timeout has been
76 * reached, the message must no longer be sent. If this
77 * is a message with a 'notify' callback set, the 'notify'
78 * function should be called with 'buf' NULL and size 0.
80 struct GNUNET_TIME_Absolute timeout;
83 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
85 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
88 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
95 * Opaque handle to the service.
97 struct GNUNET_MESH_Handle
101 * Handle to the server connection, to send messages later
103 struct GNUNET_CLIENT_Connection *client;
106 * Set of handlers used for processing incoming messages in the tunnels
108 const struct GNUNET_MESH_MessageHandler *message_handlers;
111 * Number of handlers in the handlers array.
113 unsigned int n_handlers;
118 const uint32_t *ports;
123 unsigned int n_ports;
126 * Double linked list of the tunnels this client is connected to, head.
128 struct GNUNET_MESH_Tunnel *tunnels_head;
131 * Double linked list of the tunnels this client is connected to, tail.
133 struct GNUNET_MESH_Tunnel *tunnels_tail;
136 * Callback for inbound tunnel creation
138 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
141 * Callback for inbound tunnel disconnection
143 GNUNET_MESH_TunnelEndHandler *cleaner;
146 * Handle to cancel pending transmissions in case of disconnection
148 struct GNUNET_CLIENT_TransmitHandle *th;
151 * Closure for all the handlers given by the client
156 * Messages to send to the service, head.
158 struct GNUNET_MESH_TransmitHandle *th_head;
161 * Messages to send to the service, tail.
163 struct GNUNET_MESH_TransmitHandle *th_tail;
166 * tid of the next tunnel to create (to avoid reusing IDs often)
168 MESH_TunnelNumber next_tid;
171 * Have we started the task to receive messages from the service
172 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
177 * Configuration given by the client, in case of reconnection
179 const struct GNUNET_CONFIGURATION_Handle *cfg;
182 * Time to the next reconnect in case one reconnect fails
184 struct GNUNET_TIME_Relative reconnect_time;
187 * Task for trying to reconnect.
189 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
194 GNUNET_MESH_TunnelsCB tunnels_cb;
197 * Monitor callback closure.
204 GNUNET_MESH_TunnelCB tunnel_cb;
207 * Tunnel callback closure.
212 unsigned int acks_sent;
213 unsigned int acks_recv;
219 * Description of a peer
221 struct GNUNET_MESH_Peer
224 * ID of the peer in short form
229 * Tunnel this peer belongs to
231 struct GNUNET_MESH_Tunnel *t;
234 * Flag indicating whether service has informed about its connection
242 * Opaque handle to a tunnel.
244 struct GNUNET_MESH_Tunnel
250 struct GNUNET_MESH_Tunnel *next;
255 struct GNUNET_MESH_Tunnel *prev;
258 * Handle to the mesh this tunnel belongs to
260 struct GNUNET_MESH_Handle *mesh;
263 * Local ID of the tunnel
265 MESH_TunnelNumber tid;
273 * Other end of the tunnel.
278 * Any data the caller wants to put in here
283 * Size of packet queued in this tunnel
285 unsigned int packet_size;
288 * Is the tunnel allowed to buffer?
293 * Is the tunnel realiable?
298 * If reliable, is the tunnel out of order?
303 * Are we allowed to send to the service?
311 * Implementation state for mesh's message queue.
316 * The current transmit handle, or NULL
317 * if no transmit is active.
319 struct GNUNET_MESH_TransmitHandle *th;
322 * Tunnel to send the data over.
324 struct GNUNET_MESH_Tunnel *tunnel;
328 /******************************************************************************/
329 /*********************** DECLARATIONS *************************/
330 /******************************************************************************/
333 * Function called to send a message to the service.
334 * "buf" will be NULL and "size" zero if the socket was closed for writing in
337 * @param cls closure, the mesh handle
338 * @param size number of bytes available in buf
339 * @param buf where the callee should write the connect message
340 * @return number of bytes written to buf
343 send_callback (void *cls, size_t size, void *buf);
346 /******************************************************************************/
347 /*********************** AUXILIARY FUNCTIONS *************************/
348 /******************************************************************************/
351 * Check if transmission is a payload packet.
353 * @param th Transmission handle.
355 * @return GNUNET_YES if it is a payload packet,
356 * GNUNET_NO if it is a mesh management packet.
359 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
361 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
366 * Check whether there is any message ready in the queue and find the size.
368 * @param h Mesh handle.
370 * @return The size of the first ready message in the queue,
371 * 0 if there is none.
374 message_ready_size (struct GNUNET_MESH_Handle *h)
376 struct GNUNET_MESH_TransmitHandle *th;
377 struct GNUNET_MESH_Tunnel *t;
379 for (th = h->th_head; NULL != th; th = th->next)
382 if (GNUNET_NO == th_is_payload (th))
384 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
387 if (GNUNET_YES == t->allow_send)
389 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
398 * Get the tunnel handler for the tunnel specified by id from the given handle
399 * @param h Mesh handle
400 * @param tid ID of the wanted tunnel
401 * @return handle to the required tunnel or NULL if not found
403 static struct GNUNET_MESH_Tunnel *
404 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
406 struct GNUNET_MESH_Tunnel *t;
420 * Create a new tunnel and insert it in the tunnel list of the mesh handle
421 * @param h Mesh handle
422 * @param tid desired tid of the tunnel, 0 to assign one automatically
423 * @return handle to the created tunnel
425 static struct GNUNET_MESH_Tunnel *
426 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
428 struct GNUNET_MESH_Tunnel *t;
430 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
431 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
435 t->tid = h->next_tid;
436 while (NULL != retrieve_tunnel (h, h->next_tid))
439 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
440 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
447 t->allow_send = GNUNET_NO;
448 t->nobuffer = GNUNET_NO;
454 * Destroy the specified tunnel.
455 * - Destroys all peers, calling the disconnect callback on each if needed
456 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
457 * - Calls cleaner if tunnel was inbound
458 * - Frees all memory used
460 * @param t Pointer to the tunnel.
461 * @param call_cleaner Whether to call the cleaner handler.
463 * @return Handle to the required tunnel or NULL if not found.
466 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
468 struct GNUNET_MESH_Handle *h;
469 struct GNUNET_MESH_TransmitHandle *th;
470 struct GNUNET_MESH_TransmitHandle *next;
472 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
481 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
483 /* signal tunnel destruction */
484 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
485 h->cleaner (h->cls, t, t->ctx);
487 /* check that clients did not leave messages behind in the queue */
488 for (th = h->th_head; NULL != th; th = next)
493 /* Clients should have aborted their requests already.
494 * Management traffic should be ok, as clients can't cancel that */
495 GNUNET_break (GNUNET_NO == th_is_payload(th));
496 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
498 /* clean up request */
499 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
500 GNUNET_SCHEDULER_cancel (th->timeout_task);
504 /* if there are no more pending requests with mesh service, cancel active request */
505 /* Note: this should be unnecessary... */
506 if ((0 == message_ready_size (h)) && (NULL != h->th))
508 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
513 GNUNET_PEER_change_rc (t->peer, -1);
520 * Notify client that the transmission has timed out
523 * @param tc task context
526 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
528 struct GNUNET_MESH_TransmitHandle *th = cls;
529 struct GNUNET_MESH_Handle *mesh;
531 mesh = th->tunnel->mesh;
532 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
533 th->tunnel->packet_size = 0;
534 if (GNUNET_YES == th_is_payload (th))
535 th->notify (th->notify_cls, 0, NULL);
537 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
539 /* nothing ready to transmit, no point in asking for transmission */
540 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
547 * Add a transmit handle to the transmission queue and set the
550 * @param h mesh handle with the queue head and tail
551 * @param th handle to the packet to be transmitted
554 add_to_queue (struct GNUNET_MESH_Handle *h,
555 struct GNUNET_MESH_TransmitHandle *th)
557 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
558 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
561 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
562 (th->timeout), &timeout_transmission, th);
567 * Auxiliary function to send an already constructed packet to the service.
568 * Takes care of creating a new queue element, copying the message and
569 * calling the tmt_rdy function if necessary.
571 * @param h mesh handle
572 * @param msg message to transmit
573 * @param tunnel tunnel this send is related to (NULL if N/A)
576 send_packet (struct GNUNET_MESH_Handle *h,
577 const struct GNUNET_MessageHeader *msg,
578 struct GNUNET_MESH_Tunnel *tunnel);
582 * Send an ack on the tunnel to confirm the processing of a message.
584 * @param t Tunnel on which to send the ACK.
587 send_ack (struct GNUNET_MESH_Tunnel *t)
589 struct GNUNET_MESH_LocalAck msg;
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on tunnel %X\n", t->tid);
592 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
593 msg.header.size = htons (sizeof (msg));
594 msg.tunnel_id = htonl (t->tid);
597 t->mesh->acks_sent++;
600 send_packet (t->mesh, &msg.header, t);
607 * Reconnect callback: tries to reconnect again after a failer previous
609 * @param cls closure (mesh handle)
610 * @param tc task context
613 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
617 * Send a connect packet to the service with the applications and types
618 * requested by the user.
620 * @param h The mesh handle.
624 send_connect (struct GNUNET_MESH_Handle *h)
628 size = sizeof (struct GNUNET_MESH_ClientConnect);
629 size += h->n_ports * sizeof (uint32_t);
631 char buf[size] GNUNET_ALIGN;
632 struct GNUNET_MESH_ClientConnect *msg;
636 /* build connection packet */
637 msg = (struct GNUNET_MESH_ClientConnect *) buf;
638 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
639 msg->header.size = htons (size);
640 ports = (uint32_t *) &msg[1];
641 for (i = 0; i < h->n_ports; i++)
643 ports[i] = htonl (h->ports[i]);
644 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "Sending %lu bytes long message with %u ports\n",
649 ntohs (msg->header.size), h->n_ports);
650 send_packet (h, &msg->header, NULL);
656 * Reconnect to the service, retransmit all infomation to try to restore the
659 * @param h handle to the mesh
661 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
664 do_reconnect (struct GNUNET_MESH_Handle *h)
666 struct GNUNET_MESH_Tunnel *t;
668 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
669 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
670 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
671 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
672 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
677 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
680 if (NULL != h->client)
682 GNUNET_CLIENT_disconnect (h->client);
686 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
687 if (h->client == NULL)
689 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
692 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
693 GNUNET_TIME_relative_multiply
694 (h->reconnect_time, 2));
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "Next retry in %s\n",
697 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
704 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
707 /* Rebuild all tunnels */
708 for (t = h->tunnels_head; NULL != t; t = t->next)
710 struct GNUNET_MESH_TunnelMessage tmsg;
713 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
715 /* Tunnel was created by service (incoming tunnel) */
716 /* TODO: Notify service of missing tunnel, to request
717 * creator to recreate path (find a path to him via DHT?)
721 t->allow_send = GNUNET_NO;
722 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
723 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
724 tmsg.tunnel_id = htonl (t->tid);
725 tmsg.port = htonl (t->port);
726 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
729 if (GNUNET_YES == t->nobuffer)
730 options |= GNUNET_MESH_OPTION_NOBUFFER;
732 if (GNUNET_YES == t->reliable)
733 options |= GNUNET_MESH_OPTION_RELIABLE;
735 tmsg.opt = htonl (options);
736 send_packet (h, &tmsg.header, t);
742 * Reconnect callback: tries to reconnect again after a failer previous
744 * @param cls closure (mesh handle)
745 * @param tc task context
748 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
750 struct GNUNET_MESH_Handle *h = cls;
752 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
753 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
760 * Reconnect to the service, retransmit all infomation to try to restore the
763 * @param h handle to the mesh
765 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
768 reconnect (struct GNUNET_MESH_Handle *h)
770 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
771 h->in_receive = GNUNET_NO;
772 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
773 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
778 /******************************************************************************/
779 /*********************** RECEIVE HANDLERS ****************************/
780 /******************************************************************************/
783 * Process the new tunnel notification and add it to the tunnels in the handle
785 * @param h The mesh handle
786 * @param msg A message with the details of the new incoming tunnel
789 process_tunnel_created (struct GNUNET_MESH_Handle *h,
790 const struct GNUNET_MESH_TunnelMessage *msg)
792 struct GNUNET_MESH_Tunnel *t;
793 MESH_TunnelNumber tid;
796 tid = ntohl (msg->tunnel_id);
797 port = ntohl (msg->port);
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X:%u\n", tid, port);
799 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
804 if (NULL != h->new_tunnel)
806 t = create_tunnel (h, tid);
807 t->allow_send = GNUNET_NO;
808 t->peer = GNUNET_PEER_intern (&msg->peer);
812 if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
813 t->nobuffer = GNUNET_YES;
815 t->nobuffer = GNUNET_NO;
816 if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
817 t->reliable = GNUNET_YES;
819 t->reliable = GNUNET_NO;
820 if (GNUNET_YES == t->reliable &&
821 0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
822 t->ooorder = GNUNET_YES;
824 t->ooorder = GNUNET_NO;
825 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
826 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
827 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
831 struct GNUNET_MESH_TunnelMessage d_msg;
833 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
835 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
836 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
837 d_msg.tunnel_id = msg->tunnel_id;
838 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
842 send_packet (h, &d_msg.header, NULL);
849 * Process the tunnel destroy notification and free associated resources
851 * @param h The mesh handle
852 * @param msg A message with the details of the tunnel being destroyed
855 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
856 const struct GNUNET_MESH_TunnelMessage *msg)
858 struct GNUNET_MESH_Tunnel *t;
859 MESH_TunnelNumber tid;
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel from service\n");
862 tid = ntohl (msg->tunnel_id);
863 t = retrieve_tunnel (h, tid);
867 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X unknown\n", tid);
870 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
871 destroy_tunnel (t, GNUNET_YES);
876 * Process the incoming data packets, call appropriate handlers.
878 * @param h The mesh handle
879 * @param message A message encapsulating the data
882 process_incoming_data (struct GNUNET_MESH_Handle *h,
883 const struct GNUNET_MessageHeader *message)
885 const struct GNUNET_MessageHeader *payload;
886 const struct GNUNET_MESH_MessageHandler *handler;
887 struct GNUNET_MESH_LocalData *dmsg;
888 struct GNUNET_MESH_Tunnel *t;
892 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
894 dmsg = (struct GNUNET_MESH_LocalData *) message;
896 t = retrieve_tunnel (h, ntohl (dmsg->tid));
897 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
898 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on tunnel %s [%X]\n",
899 t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? "fwd" : "bck",
900 GNUNET_i2s (GNUNET_PEER_resolve2(t->peer)), ntohl (dmsg->tid));
903 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
904 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
907 type = ntohs (payload->type);
908 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %u\n", type);
909 for (i = 0; i < h->n_handlers; i++)
911 handler = &h->message_handlers[i];
912 LOG (GNUNET_ERROR_TYPE_DEBUG,
913 " checking handler for type %u\n",
915 if (handler->type == type)
918 handler->callback (h->cls, t, &t->ctx, payload))
920 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
921 GNUNET_MESH_tunnel_destroy (t);
926 LOG (GNUNET_ERROR_TYPE_DEBUG,
927 "callback completed successfully\n");
935 * Process a local ACK message, enabling the client to send
936 * more data to the service.
938 * @param h Mesh handle.
939 * @param message Message itself.
942 process_ack (struct GNUNET_MESH_Handle *h,
943 const struct GNUNET_MessageHeader *message)
945 struct GNUNET_MESH_LocalAck *msg;
946 struct GNUNET_MESH_Tunnel *t;
947 MESH_TunnelNumber tid;
949 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
951 msg = (struct GNUNET_MESH_LocalAck *) message;
952 tid = ntohl (msg->tunnel_id);
953 t = retrieve_tunnel (h, tid);
956 LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown tunnel %X\n", tid);
959 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X!\n", t->tid);
960 t->allow_send = GNUNET_YES;
961 if (NULL == h->th && 0 < t->packet_size)
963 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
965 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
966 GNUNET_TIME_UNIT_FOREVER_REL,
967 GNUNET_YES, &send_callback, h);
973 * Process a local reply about info on all tunnels, pass info to the user.
975 * @param h Mesh handle.
976 * @param message Message itself.
979 process_get_tunnels (struct GNUNET_MESH_Handle *h,
980 const struct GNUNET_MessageHeader *message)
982 struct GNUNET_MESH_LocalMonitor *msg;
984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
986 if (NULL == h->tunnels_cb)
988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
992 msg = (struct GNUNET_MESH_LocalMonitor *) message;
993 if (ntohs (message->size) !=
994 (sizeof (struct GNUNET_MESH_LocalMonitor) +
995 sizeof (struct GNUNET_PeerIdentity)))
998 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
999 "Get tunnels message: size %hu - expected %u\n",
1000 ntohs (message->size),
1001 sizeof (struct GNUNET_MESH_LocalMonitor));
1004 h->tunnels_cb (h->tunnels_cls,
1005 ntohl (msg->tunnel_id),
1013 * Process a local monitor_tunnel reply, pass info to the user.
1015 * @param h Mesh handle.
1016 * @param message Message itself.
1019 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1020 const struct GNUNET_MessageHeader *message)
1022 struct GNUNET_MESH_LocalMonitor *msg;
1025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1027 if (NULL == h->tunnel_cb)
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1033 /* Verify message sanity */
1034 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1035 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1036 if (ntohs (message->size) != esize)
1038 GNUNET_break_op (0);
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1040 "Show tunnel message: size %hu - expected %u\n",
1041 ntohs (message->size),
1044 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1045 h->tunnel_cb = NULL;
1046 h->tunnel_cls = NULL;
1051 h->tunnel_cb (h->tunnel_cls,
1058 * Function to process all messages received from the service
1060 * @param cls closure
1061 * @param msg message received, NULL on timeout or fatal error
1064 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1066 struct GNUNET_MESH_Handle *h = cls;
1071 LOG (GNUNET_ERROR_TYPE_DEBUG,
1072 "Mesh service disconnected, reconnecting\n", h);
1076 type = ntohs (msg->type);
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1078 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1079 GNUNET_MESH_DEBUG_M2S (type));
1082 /* Notify of a new incoming tunnel */
1083 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1084 process_tunnel_created (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1086 /* Notify of a tunnel disconnection */
1087 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1088 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1090 /* Notify of a new data packet in the tunnel */
1091 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1092 process_incoming_data (h, msg);
1094 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1095 process_ack (h, msg);
1097 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1098 process_get_tunnels (h, msg);
1100 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1101 process_show_tunnel (h, msg);
1104 /* We shouldn't get any other packages, log and ignore */
1105 LOG (GNUNET_ERROR_TYPE_WARNING,
1106 "unsolicited message form service (type %s)\n",
1107 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1110 if (GNUNET_YES == h->in_receive)
1112 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1113 GNUNET_TIME_UNIT_FOREVER_REL);
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "in receive off, not calling CLIENT_receive\n");
1123 /******************************************************************************/
1124 /************************ SEND FUNCTIONS ****************************/
1125 /******************************************************************************/
1128 * Function called to send a message to the service.
1129 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1132 * @param cls closure, the mesh handle
1133 * @param size number of bytes available in buf
1134 * @param buf where the callee should write the connect message
1135 * @return number of bytes written to buf
1138 send_callback (void *cls, size_t size, void *buf)
1140 struct GNUNET_MESH_Handle *h = cls;
1141 struct GNUNET_MESH_TransmitHandle *th;
1142 struct GNUNET_MESH_TransmitHandle *next;
1143 struct GNUNET_MESH_Tunnel *t;
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1151 if ((0 == size) || (NULL == buf))
1153 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1160 nsize = message_ready_size (h);
1161 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1164 if (GNUNET_YES == th_is_payload (th))
1166 struct GNUNET_MESH_LocalData *dmsg;
1167 struct GNUNET_MessageHeader *mh;
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1170 if (GNUNET_NO == t->allow_send)
1172 /* This tunnel is not ready to transmit yet, try next message */
1177 GNUNET_assert (size >= th->size);
1178 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1179 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1180 psize = th->notify (th->notify_cls,
1181 size - sizeof (struct GNUNET_MESH_LocalData),
1185 psize += sizeof (struct GNUNET_MESH_LocalData);
1186 GNUNET_assert (size >= psize);
1187 dmsg->header.size = htons (psize);
1188 dmsg->tid = htonl (t->tid);
1189 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1190 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1191 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1192 t->allow_send = GNUNET_NO;
1196 LOG (GNUNET_ERROR_TYPE_DEBUG,
1197 "# callback returned size 0, "
1198 "application canceled transmission\n");
1203 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1205 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh internal traffic, type %s\n",
1206 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1207 memcpy (cbuf, &th[1], th->size);
1210 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1211 GNUNET_SCHEDULER_cancel (th->timeout_task);
1212 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1215 nsize = message_ready_size (h);
1220 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1222 size = message_ready_size (h);
1225 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1227 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1228 GNUNET_TIME_UNIT_FOREVER_REL,
1229 GNUNET_YES, &send_callback, h);
1233 if (NULL != h->th_head)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1238 if (GNUNET_NO == h->in_receive)
1240 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1241 h->in_receive = GNUNET_YES;
1242 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1243 GNUNET_TIME_UNIT_FOREVER_REL);
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1251 * Auxiliary function to send an already constructed packet to the service.
1252 * Takes care of creating a new queue element, copying the message and
1253 * calling the tmt_rdy function if necessary.
1255 * @param h mesh handle
1256 * @param msg message to transmit
1257 * @param tunnel tunnel this send is related to (NULL if N/A)
1260 send_packet (struct GNUNET_MESH_Handle *h,
1261 const struct GNUNET_MessageHeader *msg,
1262 struct GNUNET_MESH_Tunnel *tunnel)
1264 struct GNUNET_MESH_TransmitHandle *th;
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1268 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1269 msize = ntohs (msg->size);
1270 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1271 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1273 th->tunnel = tunnel;
1274 memcpy (&th[1], msg, msize);
1275 add_to_queue (h, th);
1276 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1279 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1281 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1282 GNUNET_TIME_UNIT_FOREVER_REL,
1283 GNUNET_YES, &send_callback, h);
1287 /******************************************************************************/
1288 /********************** API CALL DEFINITIONS *************************/
1289 /******************************************************************************/
1291 struct GNUNET_MESH_Handle *
1292 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1293 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1294 GNUNET_MESH_TunnelEndHandler cleaner,
1295 const struct GNUNET_MESH_MessageHandler *handlers,
1296 const uint32_t *ports)
1298 struct GNUNET_MESH_Handle *h;
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1301 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1302 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1304 h->new_tunnel = new_tunnel;
1305 h->cleaner = cleaner;
1306 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1307 if (h->client == NULL)
1314 h->message_handlers = handlers;
1316 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1317 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1318 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1320 if (NULL != ports && ports[0] != 0 && NULL == new_tunnel)
1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "no new tunnel handler given, ports parameter is useless!!\n");
1326 if ((NULL == ports || ports[0] == 0) && NULL != new_tunnel)
1329 LOG (GNUNET_ERROR_TYPE_DEBUG,
1330 "no ports given, new tunnel handler will never be called!!\n");
1332 /* count handlers */
1333 for (h->n_handlers = 0;
1334 handlers && handlers[h->n_handlers].type;
1336 for (h->n_ports = 0;
1337 ports && ports[h->n_ports];
1340 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1346 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1348 struct GNUNET_MESH_Tunnel *t;
1349 struct GNUNET_MESH_Tunnel *aux;
1350 struct GNUNET_MESH_TransmitHandle *th;
1352 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1355 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1356 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1359 t = handle->tunnels_head;
1363 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1368 destroy_tunnel (t, GNUNET_YES);
1371 while ( (th = handle->th_head) != NULL)
1373 struct GNUNET_MessageHeader *msg;
1375 /* Make sure it is an allowed packet (everything else should have been
1376 * already canceled).
1378 GNUNET_break (GNUNET_NO == th_is_payload (th));
1379 msg = (struct GNUNET_MessageHeader *) &th[1];
1380 switch (ntohs(msg->type))
1382 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1383 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1384 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1385 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1389 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1393 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1397 if (NULL != handle->th)
1399 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1402 if (NULL != handle->client)
1404 GNUNET_CLIENT_disconnect (handle->client);
1405 handle->client = NULL;
1407 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1409 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1410 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1412 GNUNET_free (handle);
1417 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1418 * and to broadcast).
1420 * @param h mesh handle
1421 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1422 * @param peer peer identity the tunnel should go to
1423 * @param port Port number.
1424 * @param nobuffer Flag for disabling buffering on relay nodes.
1425 * @param reliable Flag for end-to-end reliability.
1427 * @return handle to the tunnel
1429 struct GNUNET_MESH_Tunnel *
1430 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1432 const struct GNUNET_PeerIdentity *peer,
1437 struct GNUNET_MESH_Tunnel *t;
1438 struct GNUNET_MESH_TunnelMessage msg;
1440 LOG (GNUNET_ERROR_TYPE_DEBUG,
1441 "Creating new tunnel to %s:%u\n",
1442 GNUNET_i2s (peer), port);
1443 t = create_tunnel (h, 0);
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1445 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1446 t->ctx = tunnel_ctx;
1447 t->peer = GNUNET_PEER_intern (peer);
1448 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1449 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1450 msg.tunnel_id = htonl (t->tid);
1451 msg.port = htonl (port);
1454 if (GNUNET_YES == reliable)
1455 msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
1456 if (GNUNET_YES == nobuffer)
1457 msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
1458 msg.opt = htonl (msg.opt);
1460 send_packet (h, &msg.header, t);
1466 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1468 struct GNUNET_MESH_Handle *h;
1469 struct GNUNET_MESH_TunnelMessage msg;
1470 struct GNUNET_MESH_TransmitHandle *th;
1472 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1475 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1476 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1477 msg.tunnel_id = htonl (tunnel->tid);
1478 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1484 struct GNUNET_MESH_TransmitHandle *aux;
1485 if (th->tunnel == tunnel)
1488 /* FIXME call the handler? */
1489 if (GNUNET_YES == th_is_payload (th))
1490 th->notify (th->notify_cls, 0, NULL);
1491 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1499 destroy_tunnel (tunnel, GNUNET_YES);
1500 send_packet (h, &msg.header, NULL);
1505 * Get information about a tunnel.
1507 * @param tunnel Tunnel handle.
1508 * @param option Query (GNUNET_MESH_OPTION_*).
1509 * @param ... dependant on option, currently not used
1511 * @return Union with an answer to the query.
1513 const union MeshTunnelInfo *
1514 GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel,
1515 enum MeshTunnelOption option, ...)
1517 const union MeshTunnelInfo *ret;
1521 case GNUNET_MESH_OPTION_NOBUFFER:
1522 ret = (const union MeshTunnelInfo *) &tunnel->nobuffer;
1524 case GNUNET_MESH_OPTION_RELIABLE:
1525 ret = (const union MeshTunnelInfo *) &tunnel->reliable;
1527 case GNUNET_MESH_OPTION_OOORDER:
1528 ret = (const union MeshTunnelInfo *) &tunnel->ooorder;
1530 case GNUNET_MESH_OPTION_PEER:
1531 ret = (const union MeshTunnelInfo *) &tunnel->peer;
1541 struct GNUNET_MESH_TransmitHandle *
1542 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1543 struct GNUNET_TIME_Relative maxdelay,
1545 GNUNET_CONNECTION_TransmitReadyNotify notify,
1548 struct GNUNET_MESH_TransmitHandle *th;
1550 GNUNET_assert (NULL != tunnel);
1551 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", tunnel->allow_send);
1554 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1555 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1558 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1559 GNUNET_assert (NULL != notify);
1560 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1561 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1562 th->tunnel = tunnel;
1563 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1564 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1565 tunnel->packet_size = th->size;
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1567 th->notify = notify;
1568 th->notify_cls = notify_cls;
1569 add_to_queue (tunnel->mesh, th);
1570 if (NULL != tunnel->mesh->th)
1572 if (GNUNET_NO == tunnel->allow_send)
1574 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1576 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1577 GNUNET_TIME_UNIT_FOREVER_REL,
1578 GNUNET_YES, &send_callback,
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1586 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1588 struct GNUNET_MESH_Handle *mesh;
1590 th->tunnel->packet_size = 0;
1591 mesh = th->tunnel->mesh;
1592 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1593 GNUNET_SCHEDULER_cancel (th->timeout_task);
1594 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1596 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1598 /* queue empty, no point in asking for transmission */
1599 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1605 GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel)
1612 * Request information about the running mesh peer.
1613 * The callback will be called for every tunnel known to the service,
1614 * listing all active peers that blong to the tunnel.
1616 * If called again on the same handle, it will overwrite the previous
1617 * callback and cls. To retrieve the cls, monitor_cancel must be
1620 * WARNING: unstable API, likely to change in the future!
1622 * @param h Handle to the mesh peer.
1623 * @param callback Function to call with the requested data.
1624 * @param callback_cls Closure for @c callback.
1627 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1628 GNUNET_MESH_TunnelsCB callback,
1631 struct GNUNET_MessageHeader msg;
1633 msg.size = htons (sizeof (msg));
1634 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1635 send_packet (h, &msg, NULL);
1636 h->tunnels_cb = callback;
1637 h->tunnels_cls = callback_cls;
1644 * Cancel a monitor request. The monitor callback will not be called.
1646 * @param h Mesh handle.
1648 * @return Closure given to GNUNET_MESH_monitor, if any.
1651 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1655 cls = h->tunnels_cls;
1656 h->tunnels_cb = NULL;
1657 h->tunnels_cls = NULL;
1663 * Request information about a specific tunnel of the running mesh peer.
1665 * WARNING: unstable API, likely to change in the future!
1666 * FIXME Add destination option.
1668 * @param h Handle to the mesh peer.
1669 * @param initiator ID of the owner of the tunnel.
1670 * @param tunnel_number Tunnel number.
1671 * @param callback Function to call with the requested data.
1672 * @param callback_cls Closure for @c callback.
1675 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1676 struct GNUNET_PeerIdentity *initiator,
1677 unsigned int tunnel_number,
1678 GNUNET_MESH_TunnelCB callback,
1681 struct GNUNET_MESH_LocalMonitor msg;
1683 msg.header.size = htons (sizeof (msg));
1684 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1685 msg.owner = *initiator;
1686 msg.tunnel_id = htonl (tunnel_number);
1688 send_packet (h, &msg.header, NULL);
1689 h->tunnel_cb = callback;
1690 h->tunnel_cls = callback_cls;
1697 * Function called to notify a client about the connection
1698 * begin ready to queue more data. "buf" will be
1699 * NULL and "size" zero if the connection was closed for
1700 * writing in the meantime.
1702 * @param cls closure
1703 * @param size number of bytes available in buf
1704 * @param buf where the callee should write the message
1705 * @return number of bytes written to buf
1708 mesh_mq_ntr (void *cls, size_t size,
1711 struct GNUNET_MQ_Handle *mq = cls;
1712 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1713 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1719 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1722 msize = ntohs (msg->size);
1723 GNUNET_assert (msize <= size);
1724 memcpy (buf, msg, msize);
1725 GNUNET_MQ_impl_send_continue (mq);
1731 * Signature of functions implementing the
1732 * sending functionality of a message queue.
1734 * @param mq the message queue
1735 * @param msg the message to send
1736 * @param impl_state state of the implementation
1739 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1740 const struct GNUNET_MessageHeader *msg, void *impl_state)
1742 struct MeshMQState *state = impl_state;
1744 GNUNET_assert (NULL == state->th);
1745 GNUNET_MQ_impl_send_commit (mq);
1747 GNUNET_MESH_notify_transmit_ready (state->tunnel,
1748 /* FIXME: add option for corking */
1750 GNUNET_TIME_UNIT_FOREVER_REL,
1758 * Signature of functions implementing the
1759 * destruction of a message queue.
1760 * Implementations must not free 'mq', but should
1761 * take care of 'impl_state'.
1763 * @param mq the message queue to destroy
1764 * @param impl_state state of the implementation
1767 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1769 struct MeshMQState *state = impl_state;
1771 if (NULL != state->th)
1772 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1774 GNUNET_free (state);
1779 * Create a message queue for a mesh tunnel.
1780 * The message queue can only be used to transmit messages,
1781 * not to receive them.
1783 * @param tunnel the tunnel to create the message qeue for
1784 * @return a message queue to messages over the tunnel
1786 struct GNUNET_MQ_Handle *
1787 GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel)
1789 struct GNUNET_MQ_Handle *mq;
1790 struct MeshMQState *state;
1792 state = GNUNET_new (struct MeshMQState);
1793 state->tunnel = tunnel;
1795 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
1796 mesh_mq_destroy_impl,
1797 NULL, /* FIXME: cancel impl. */
1799 NULL, /* no msg handlers */
1800 NULL, /* no err handlers */
1801 NULL); /* no handler cls */