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.c
20 * @brief mesh api: client implementation of new mesh service
21 * @author Bartlomiej Polot
26 * - AUXILIARY FUNCTIONS
29 * - API CALL DEFINITIONS
31 * TODO: add regex to reconnect
34 #include "gnunet_common.h"
35 #include "gnunet_client_lib.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_peer_lib.h"
38 #include "gnunet_mesh_service.h"
40 #include "mesh_protocol.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
43 #define DEBUG_ACK GNUNET_YES
45 /******************************************************************************/
46 /************************ DATA STRUCTURES ****************************/
47 /******************************************************************************/
50 * Transmission queue to the service
52 struct GNUNET_MESH_TransmitHandle
58 struct GNUNET_MESH_TransmitHandle *next;
63 struct GNUNET_MESH_TransmitHandle *prev;
66 * Tunnel this message is sent on / for (may be NULL for control messages).
68 struct GNUNET_MESH_Tunnel *tunnel;
71 * Callback to obtain the message to transmit, or NULL if we
72 * got the message in 'data'. Notice that messages built
73 * by 'notify' need to be encapsulated with information about
76 GNUNET_CONNECTION_TransmitReadyNotify notify;
79 * Closure for 'notify'
84 * How long is this message valid. Once the timeout has been
85 * reached, the message must no longer be sent. If this
86 * is a message with a 'notify' callback set, the 'notify'
87 * function should be called with 'buf' NULL and size 0.
89 struct GNUNET_TIME_Absolute timeout;
92 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
94 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
97 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
104 * Opaque handle to the service.
106 struct GNUNET_MESH_Handle
110 * Handle to the server connection, to send messages later
112 struct GNUNET_CLIENT_Connection *client;
115 * Set of handlers used for processing incoming messages in the tunnels
117 const struct GNUNET_MESH_MessageHandler *message_handlers;
120 * Number of handlers in the handlers array.
122 unsigned int n_handlers;
127 const uint32_t *ports;
132 unsigned int n_ports;
135 * Double linked list of the tunnels this client is connected to, head.
137 struct GNUNET_MESH_Tunnel *tunnels_head;
140 * Double linked list of the tunnels this client is connected to, tail.
142 struct GNUNET_MESH_Tunnel *tunnels_tail;
145 * Callback for inbound tunnel creation
147 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
150 * Callback for inbound tunnel disconnection
152 GNUNET_MESH_TunnelEndHandler *cleaner;
155 * Handle to cancel pending transmissions in case of disconnection
157 struct GNUNET_CLIENT_TransmitHandle *th;
160 * Closure for all the handlers given by the client
165 * Messages to send to the service, head.
167 struct GNUNET_MESH_TransmitHandle *th_head;
170 * Messages to send to the service, tail.
172 struct GNUNET_MESH_TransmitHandle *th_tail;
175 * tid of the next tunnel to create (to avoid reusing IDs often)
177 MESH_TunnelNumber next_tid;
180 * Have we started the task to receive messages from the service
181 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
186 * Configuration given by the client, in case of reconnection
188 const struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Time to the next reconnect in case one reconnect fails
193 struct GNUNET_TIME_Relative reconnect_time;
196 * Task for trying to reconnect.
198 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
203 GNUNET_MESH_TunnelsCB tunnels_cb;
206 * Monitor callback closure.
213 GNUNET_MESH_TunnelCB tunnel_cb;
216 * Tunnel callback closure.
221 unsigned int acks_sent;
222 unsigned int acks_recv;
228 * Description of a peer
230 struct GNUNET_MESH_Peer
233 * ID of the peer in short form
238 * Tunnel this peer belongs to
240 struct GNUNET_MESH_Tunnel *t;
243 * Flag indicating whether service has informed about its connection
251 * Opaque handle to a tunnel.
253 struct GNUNET_MESH_Tunnel
259 struct GNUNET_MESH_Tunnel *next;
264 struct GNUNET_MESH_Tunnel *prev;
267 * Handle to the mesh this tunnel belongs to
269 struct GNUNET_MESH_Handle *mesh;
272 * Local ID of the tunnel
274 MESH_TunnelNumber tid;
282 * Other end of the tunnel.
287 * Any data the caller wants to put in here
292 * Size of packet queued in this tunnel
294 unsigned int packet_size;
297 * Is the tunnel allowed to buffer?
302 * Is the tunnel realiable?
307 * If reliable, is the tunnel out of order?
312 * Are we allowed to send to the service?
320 * Implementation state for mesh's message queue.
325 * The current transmit handle, or NULL
326 * if no transmit is active.
328 struct GNUNET_MESH_TransmitHandle *th;
331 * Tunnel to send the data over.
333 struct GNUNET_MESH_Tunnel *tunnel;
337 /******************************************************************************/
338 /*********************** DECLARATIONS *************************/
339 /******************************************************************************/
342 * Function called to send a message to the service.
343 * "buf" will be NULL and "size" zero if the socket was closed for writing in
346 * @param cls closure, the mesh handle
347 * @param size number of bytes available in buf
348 * @param buf where the callee should write the connect message
349 * @return number of bytes written to buf
352 send_callback (void *cls, size_t size, void *buf);
355 /******************************************************************************/
356 /*********************** AUXILIARY FUNCTIONS *************************/
357 /******************************************************************************/
360 * Check if transmission is a payload packet.
362 * @param th Transmission handle.
364 * @return GNUNET_YES if it is a payload packet,
365 * GNUNET_NO if it is a mesh management packet.
368 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
370 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
375 * Check whether there is any message ready in the queue and find the size.
377 * @param h Mesh handle.
379 * @return The size of the first ready message in the queue,
380 * 0 if there is none.
383 message_ready_size (struct GNUNET_MESH_Handle *h)
385 struct GNUNET_MESH_TransmitHandle *th;
386 struct GNUNET_MESH_Tunnel *t;
388 for (th = h->th_head; NULL != th; th = th->next)
391 if (GNUNET_NO == th_is_payload (th))
393 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
396 if (GNUNET_YES == t->allow_send)
398 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
407 * Get the tunnel handler for the tunnel specified by id from the given handle
408 * @param h Mesh handle
409 * @param tid ID of the wanted tunnel
410 * @return handle to the required tunnel or NULL if not found
412 static struct GNUNET_MESH_Tunnel *
413 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
415 struct GNUNET_MESH_Tunnel *t;
429 * Create a new tunnel and insert it in the tunnel list of the mesh handle
430 * @param h Mesh handle
431 * @param tid desired tid of the tunnel, 0 to assign one automatically
432 * @return handle to the created tunnel
434 static struct GNUNET_MESH_Tunnel *
435 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
437 struct GNUNET_MESH_Tunnel *t;
439 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
440 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
444 t->tid = h->next_tid;
445 while (NULL != retrieve_tunnel (h, h->next_tid))
448 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
449 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
456 t->allow_send = GNUNET_NO;
457 t->nobuffer = GNUNET_NO;
463 * Destroy the specified tunnel.
464 * - Destroys all peers, calling the disconnect callback on each if needed
465 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
466 * - Calls cleaner if tunnel was inbound
467 * - Frees all memory used
469 * @param t Pointer to the tunnel.
470 * @param call_cleaner Whether to call the cleaner handler.
472 * @return Handle to the required tunnel or NULL if not found.
475 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
477 struct GNUNET_MESH_Handle *h;
478 struct GNUNET_MESH_TransmitHandle *th;
479 struct GNUNET_MESH_TransmitHandle *next;
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
490 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
492 /* signal tunnel destruction */
493 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
494 h->cleaner (h->cls, t, t->ctx);
496 /* check that clients did not leave messages behind in the queue */
497 for (th = h->th_head; NULL != th; th = next)
502 /* Clients should have aborted their requests already.
503 * Management traffic should be ok, as clients can't cancel that */
504 GNUNET_break (GNUNET_NO == th_is_payload(th));
505 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
507 /* clean up request */
508 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
509 GNUNET_SCHEDULER_cancel (th->timeout_task);
513 /* if there are no more pending requests with mesh service, cancel active request */
514 /* Note: this should be unnecessary... */
515 if ((0 == message_ready_size (h)) && (NULL != h->th))
517 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
522 GNUNET_PEER_change_rc (t->peer, -1);
529 * Notify client that the transmission has timed out
532 * @param tc task context
535 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
537 struct GNUNET_MESH_TransmitHandle *th = cls;
538 struct GNUNET_MESH_Handle *mesh;
540 mesh = th->tunnel->mesh;
541 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
542 th->tunnel->packet_size = 0;
543 if (GNUNET_YES == th_is_payload (th))
544 th->notify (th->notify_cls, 0, NULL);
546 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
548 /* nothing ready to transmit, no point in asking for transmission */
549 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
556 * Add a transmit handle to the transmission queue and set the
559 * @param h mesh handle with the queue head and tail
560 * @param th handle to the packet to be transmitted
563 add_to_queue (struct GNUNET_MESH_Handle *h,
564 struct GNUNET_MESH_TransmitHandle *th)
566 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
567 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
570 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
571 (th->timeout), &timeout_transmission, th);
576 * Auxiliary function to send an already constructed packet to the service.
577 * Takes care of creating a new queue element, copying the message and
578 * calling the tmt_rdy function if necessary.
580 * @param h mesh handle
581 * @param msg message to transmit
582 * @param tunnel tunnel this send is related to (NULL if N/A)
585 send_packet (struct GNUNET_MESH_Handle *h,
586 const struct GNUNET_MessageHeader *msg,
587 struct GNUNET_MESH_Tunnel *tunnel);
591 * Send an ack on the tunnel to confirm the processing of a message.
593 * @param t Tunnel on which to send the ACK.
596 send_ack (struct GNUNET_MESH_Tunnel *t)
598 struct GNUNET_MESH_LocalAck msg;
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on tunnel %X\n", t->tid);
601 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
602 msg.header.size = htons (sizeof (msg));
603 msg.tunnel_id = htonl (t->tid);
606 t->mesh->acks_sent++;
609 send_packet (t->mesh, &msg.header, t);
616 * Reconnect callback: tries to reconnect again after a failer previous
618 * @param cls closure (mesh handle)
619 * @param tc task context
622 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
626 * Send a connect packet to the service with the applications and types
627 * requested by the user.
629 * @param h The mesh handle.
633 send_connect (struct GNUNET_MESH_Handle *h)
637 size = sizeof (struct GNUNET_MESH_ClientConnect);
638 size += h->n_ports * sizeof (uint32_t);
640 char buf[size] GNUNET_ALIGN;
641 struct GNUNET_MESH_ClientConnect *msg;
645 /* build connection packet */
646 msg = (struct GNUNET_MESH_ClientConnect *) buf;
647 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
648 msg->header.size = htons (size);
649 ports = (uint32_t *) &msg[1];
650 for (i = 0; i < h->n_ports; i++)
652 ports[i] = htonl (h->ports[i]);
653 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
656 LOG (GNUNET_ERROR_TYPE_DEBUG,
657 "Sending %lu bytes long message with %u ports\n",
658 ntohs (msg->header.size), h->n_ports);
659 send_packet (h, &msg->header, NULL);
665 * Reconnect to the service, retransmit all infomation to try to restore the
668 * @param h handle to the mesh
670 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
673 do_reconnect (struct GNUNET_MESH_Handle *h)
675 struct GNUNET_MESH_Tunnel *t;
677 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
678 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
679 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
680 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
681 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
686 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
689 if (NULL != h->client)
691 GNUNET_CLIENT_disconnect (h->client);
695 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
696 if (h->client == NULL)
698 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
701 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
702 GNUNET_TIME_relative_multiply
703 (h->reconnect_time, 2));
704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "Next retry in %s\n",
706 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
713 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
716 /* Rebuild all tunnels */
717 for (t = h->tunnels_head; NULL != t; t = t->next)
719 struct GNUNET_MESH_TunnelMessage tmsg;
722 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
724 /* Tunnel was created by service (incoming tunnel) */
725 /* TODO: Notify service of missing tunnel, to request
726 * creator to recreate path (find a path to him via DHT?)
730 t->allow_send = GNUNET_NO;
731 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
732 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
733 tmsg.tunnel_id = htonl (t->tid);
734 tmsg.port = htonl (t->port);
735 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
738 if (GNUNET_YES == t->nobuffer)
739 options |= GNUNET_MESH_OPTION_NOBUFFER;
741 if (GNUNET_YES == t->reliable)
742 options |= GNUNET_MESH_OPTION_RELIABLE;
744 tmsg.opt = htonl (options);
745 send_packet (h, &tmsg.header, t);
751 * Reconnect callback: tries to reconnect again after a failer previous
753 * @param cls closure (mesh handle)
754 * @param tc task context
757 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
759 struct GNUNET_MESH_Handle *h = cls;
761 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
762 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
769 * Reconnect to the service, retransmit all infomation to try to restore the
772 * @param h handle to the mesh
774 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
777 reconnect (struct GNUNET_MESH_Handle *h)
779 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
780 h->in_receive = GNUNET_NO;
781 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
782 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
787 /******************************************************************************/
788 /*********************** RECEIVE HANDLERS ****************************/
789 /******************************************************************************/
792 * Process the new tunnel notification and add it to the tunnels in the handle
794 * @param h The mesh handle
795 * @param msg A message with the details of the new incoming tunnel
798 process_tunnel_created (struct GNUNET_MESH_Handle *h,
799 const struct GNUNET_MESH_TunnelMessage *msg)
801 struct GNUNET_MESH_Tunnel *t;
802 MESH_TunnelNumber tid;
804 tid = ntohl (msg->tunnel_id);
805 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
806 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
811 if (NULL != h->new_tunnel)
813 t = create_tunnel (h, tid);
814 t->allow_send = GNUNET_NO;
815 t->peer = GNUNET_PEER_intern (&msg->peer);
818 t->port = ntohl (msg->port);
819 if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
820 t->nobuffer = GNUNET_YES;
822 t->nobuffer = GNUNET_NO;
823 if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
824 t->reliable = GNUNET_YES;
826 t->reliable = GNUNET_NO;
827 if (GNUNET_YES == t->reliable &&
828 0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
829 t->ooorder = GNUNET_YES;
831 t->ooorder = GNUNET_NO;
832 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
833 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
834 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
838 struct GNUNET_MESH_TunnelMessage d_msg;
840 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
842 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
843 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
844 d_msg.tunnel_id = msg->tunnel_id;
845 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
849 send_packet (h, &d_msg.header, NULL);
856 * Process the tunnel destroy notification and free associated resources
858 * @param h The mesh handle
859 * @param msg A message with the details of the tunnel being destroyed
862 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
863 const struct GNUNET_MESH_TunnelMessage *msg)
865 struct GNUNET_MESH_Tunnel *t;
866 MESH_TunnelNumber tid;
868 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel from service\n");
869 tid = ntohl (msg->tunnel_id);
870 t = retrieve_tunnel (h, tid);
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X unknown\n", tid);
877 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
878 destroy_tunnel (t, GNUNET_YES);
884 * Process the incoming data packets, call appropriate handlers.
886 * @param h The mesh handle
887 * @param message A message encapsulating the data
890 process_incoming_data (struct GNUNET_MESH_Handle *h,
891 const struct GNUNET_MessageHeader *message)
893 const struct GNUNET_MessageHeader *payload;
894 const struct GNUNET_MESH_MessageHandler *handler;
895 const struct GNUNET_PeerIdentity *peer;
896 struct GNUNET_PeerIdentity id;
897 struct GNUNET_MESH_LocalData *dmsg;
898 struct GNUNET_MESH_Tunnel *t;
902 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
904 dmsg = (struct GNUNET_MESH_LocalData *) message;
906 t = retrieve_tunnel (h, ntohl (dmsg->tid));
907 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
908 GNUNET_PEER_resolve (t->peer, &id);
910 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on tunnel %s [%X]\n",
911 t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? "fwd" : "bck",
912 GNUNET_i2s (peer), ntohl (dmsg->tid));
915 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
916 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
919 type = ntohs (payload->type);
920 for (i = 0; i < h->n_handlers; i++)
922 handler = &h->message_handlers[i];
923 if (handler->type == type)
926 handler->callback (h->cls, t, &t->ctx, payload))
928 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
929 GNUNET_MESH_tunnel_destroy (t);
934 LOG (GNUNET_ERROR_TYPE_DEBUG,
935 "callback completed successfully\n");
943 * Process a local ACK message, enabling the client to send
944 * more data to the service.
946 * @param h Mesh handle.
947 * @param message Message itself.
950 process_ack (struct GNUNET_MESH_Handle *h,
951 const struct GNUNET_MessageHeader *message)
953 struct GNUNET_MESH_LocalAck *msg;
954 struct GNUNET_MESH_Tunnel *t;
955 MESH_TunnelNumber tid;
957 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
959 msg = (struct GNUNET_MESH_LocalAck *) message;
960 tid = ntohl (msg->tunnel_id);
961 t = retrieve_tunnel (h, tid);
964 LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown tunnel %X\n", tid);
967 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X!\n", t->tid);
968 t->allow_send = GNUNET_YES;
969 if (NULL == h->th && 0 < t->packet_size)
971 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
973 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
974 GNUNET_TIME_UNIT_FOREVER_REL,
975 GNUNET_YES, &send_callback, h);
981 * Process a local reply about info on all tunnels, pass info to the user.
983 * @param h Mesh handle.
984 * @param message Message itself.
987 process_get_tunnels (struct GNUNET_MESH_Handle *h,
988 const struct GNUNET_MessageHeader *message)
990 struct GNUNET_MESH_LocalMonitor *msg;
992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
994 if (NULL == h->tunnels_cb)
996 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1000 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1001 if (ntohs (message->size) !=
1002 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1003 sizeof (struct GNUNET_PeerIdentity)))
1005 GNUNET_break_op (0);
1006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1007 "Get tunnels message: size %hu - expected %u\n",
1008 ntohs (message->size),
1009 sizeof (struct GNUNET_MESH_LocalMonitor));
1012 h->tunnels_cb (h->tunnels_cls,
1013 ntohl (msg->tunnel_id),
1021 * Process a local monitor_tunnel reply, pass info to the user.
1023 * @param h Mesh handle.
1024 * @param message Message itself.
1027 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1028 const struct GNUNET_MessageHeader *message)
1030 struct GNUNET_MESH_LocalMonitor *msg;
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1035 if (NULL == h->tunnel_cb)
1037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1041 /* Verify message sanity */
1042 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1043 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1044 if (ntohs (message->size) != esize)
1046 GNUNET_break_op (0);
1047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1048 "Show tunnel message: size %hu - expected %u\n",
1049 ntohs (message->size),
1052 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1053 h->tunnel_cb = NULL;
1054 h->tunnel_cls = NULL;
1059 h->tunnel_cb (h->tunnel_cls,
1066 * Function to process all messages received from the service
1068 * @param cls closure
1069 * @param msg message received, NULL on timeout or fatal error
1072 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1074 struct GNUNET_MESH_Handle *h = cls;
1079 LOG (GNUNET_ERROR_TYPE_DEBUG,
1080 "Mesh service disconnected, reconnecting\n", h);
1084 type = ntohs (msg->type);
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1087 GNUNET_MESH_DEBUG_M2S (type));
1090 /* Notify of a new incoming tunnel */
1091 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1092 process_tunnel_created (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1094 /* Notify of a tunnel disconnection */
1095 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1096 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1098 /* Notify of a new data packet in the tunnel */
1099 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1100 process_incoming_data (h, msg);
1102 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1103 process_ack (h, msg);
1105 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1106 process_get_tunnels (h, msg);
1108 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1109 process_show_tunnel (h, msg);
1112 /* We shouldn't get any other packages, log and ignore */
1113 LOG (GNUNET_ERROR_TYPE_WARNING,
1114 "unsolicited message form service (type %s)\n",
1115 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1118 if (GNUNET_YES == h->in_receive)
1120 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1121 GNUNET_TIME_UNIT_FOREVER_REL);
1125 LOG (GNUNET_ERROR_TYPE_DEBUG,
1126 "in receive off, not calling CLIENT_receive\n");
1131 /******************************************************************************/
1132 /************************ SEND FUNCTIONS ****************************/
1133 /******************************************************************************/
1136 * Function called to send a message to the service.
1137 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1140 * @param cls closure, the mesh handle
1141 * @param size number of bytes available in buf
1142 * @param buf where the callee should write the connect message
1143 * @return number of bytes written to buf
1146 send_callback (void *cls, size_t size, void *buf)
1148 struct GNUNET_MESH_Handle *h = cls;
1149 struct GNUNET_MESH_TransmitHandle *th;
1150 struct GNUNET_MESH_TransmitHandle *next;
1151 struct GNUNET_MESH_Tunnel *t;
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1158 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1159 if ((0 == size) || (NULL == buf))
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1168 nsize = message_ready_size (h);
1169 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1172 if (GNUNET_YES == th_is_payload (th))
1174 struct GNUNET_MESH_LocalData *dmsg;
1175 struct GNUNET_MessageHeader *mh;
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1178 if (GNUNET_NO == t->allow_send)
1180 /* This tunnel is not ready to transmit yet, try next message */
1185 t->allow_send = GNUNET_NO;
1186 GNUNET_assert (size >= th->size);
1187 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1188 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1189 psize = th->notify (th->notify_cls,
1190 size - sizeof (struct GNUNET_MESH_LocalData),
1194 psize += sizeof (struct GNUNET_MESH_LocalData);
1195 GNUNET_assert (size >= psize);
1196 dmsg->header.size = htons (psize);
1197 dmsg->tid = htonl (t->tid);
1199 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1201 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1206 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1208 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh traffic, type %s\n",
1209 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1210 memcpy (cbuf, &th[1], th->size);
1213 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1214 GNUNET_SCHEDULER_cancel (th->timeout_task);
1215 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1218 nsize = message_ready_size (h);
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1225 size = message_ready_size (h);
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1230 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1231 GNUNET_TIME_UNIT_FOREVER_REL,
1232 GNUNET_YES, &send_callback, h);
1236 if (NULL != h->th_head)
1237 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1241 if (GNUNET_NO == h->in_receive)
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1244 h->in_receive = GNUNET_YES;
1245 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1246 GNUNET_TIME_UNIT_FOREVER_REL);
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1254 * Auxiliary function to send an already constructed packet to the service.
1255 * Takes care of creating a new queue element, copying the message and
1256 * calling the tmt_rdy function if necessary.
1258 * @param h mesh handle
1259 * @param msg message to transmit
1260 * @param tunnel tunnel this send is related to (NULL if N/A)
1263 send_packet (struct GNUNET_MESH_Handle *h,
1264 const struct GNUNET_MessageHeader *msg,
1265 struct GNUNET_MESH_Tunnel *tunnel)
1267 struct GNUNET_MESH_TransmitHandle *th;
1270 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1271 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1272 msize = ntohs (msg->size);
1273 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1274 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1276 th->tunnel = tunnel;
1277 memcpy (&th[1], msg, msize);
1278 add_to_queue (h, th);
1279 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1284 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1285 GNUNET_TIME_UNIT_FOREVER_REL,
1286 GNUNET_YES, &send_callback, h);
1290 /******************************************************************************/
1291 /********************** API CALL DEFINITIONS *************************/
1292 /******************************************************************************/
1294 struct GNUNET_MESH_Handle *
1295 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1296 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1297 GNUNET_MESH_TunnelEndHandler cleaner,
1298 const struct GNUNET_MESH_MessageHandler *handlers,
1299 const uint32_t *ports)
1301 struct GNUNET_MESH_Handle *h;
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1304 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1307 h->new_tunnel = new_tunnel;
1308 h->cleaner = cleaner;
1309 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1310 if (h->client == NULL)
1317 h->message_handlers = handlers;
1319 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1320 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1321 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1323 /* count handlers */
1324 for (h->n_handlers = 0;
1325 handlers && handlers[h->n_handlers].type;
1327 for (h->n_ports = 0;
1328 ports && ports[h->n_ports];
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1337 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1339 struct GNUNET_MESH_Tunnel *t;
1340 struct GNUNET_MESH_Tunnel *aux;
1341 struct GNUNET_MESH_TransmitHandle *th;
1343 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1346 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1347 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1350 t = handle->tunnels_head;
1354 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1359 destroy_tunnel (t, GNUNET_YES);
1362 while ( (th = handle->th_head) != NULL)
1364 struct GNUNET_MessageHeader *msg;
1366 /* Make sure it is an allowed packet (everything else should have been
1367 * already canceled).
1369 GNUNET_break (GNUNET_NO == th_is_payload (th));
1370 msg = (struct GNUNET_MessageHeader *) &th[1];
1371 switch (ntohs(msg->type))
1373 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1374 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1375 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1376 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1380 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1384 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1388 if (NULL != handle->th)
1390 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1393 if (NULL != handle->client)
1395 GNUNET_CLIENT_disconnect (handle->client);
1396 handle->client = NULL;
1398 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1400 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1401 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1403 GNUNET_free (handle);
1408 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1409 * and to broadcast).
1411 * @param h mesh handle
1412 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1413 * @param peer peer identity the tunnel should go to
1414 * @param port Port number.
1415 * @param nobuffer Flag for disabling buffering on relay nodes.
1416 * @param reliable Flag for end-to-end reliability.
1418 * @return handle to the tunnel
1420 struct GNUNET_MESH_Tunnel *
1421 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1423 const struct GNUNET_PeerIdentity *peer,
1428 struct GNUNET_MESH_Tunnel *t;
1429 struct GNUNET_MESH_TunnelMessage msg;
1431 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1432 t = create_tunnel (h, 0);
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1435 t->ctx = tunnel_ctx;
1436 t->peer = GNUNET_PEER_intern (peer);
1437 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1438 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1439 msg.tunnel_id = htonl (t->tid);
1440 msg.port = htonl (port);
1443 if (GNUNET_YES == reliable)
1444 msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
1445 if (GNUNET_YES == nobuffer)
1446 msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
1447 msg.opt = htonl (msg.opt);
1449 send_packet (h, &msg.header, t);
1455 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1457 struct GNUNET_MESH_Handle *h;
1458 struct GNUNET_MESH_TunnelMessage msg;
1459 struct GNUNET_MESH_TransmitHandle *th;
1461 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1464 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1465 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1466 msg.tunnel_id = htonl (tunnel->tid);
1467 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1473 struct GNUNET_MESH_TransmitHandle *aux;
1474 if (th->tunnel == tunnel)
1477 /* FIXME call the handler? */
1478 if (GNUNET_YES == th_is_payload (th))
1479 th->notify (th->notify_cls, 0, NULL);
1480 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1488 destroy_tunnel (tunnel, GNUNET_YES);
1489 send_packet (h, &msg.header, NULL);
1494 * Get information about a tunnel.
1496 * @param tunnel Tunnel handle.
1497 * @param option Query (GNUNET_MESH_OPTION_*).
1498 * @param ... dependant on option, currently not used
1500 * @return Union with an answer to the query.
1502 const union MeshTunnelInfo *
1503 GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel,
1504 enum MeshTunnelOption option, ...)
1506 const union MeshTunnelInfo *ret;
1510 case GNUNET_MESH_OPTION_NOBUFFER:
1511 ret = (const union MeshTunnelInfo *) &tunnel->nobuffer;
1513 case GNUNET_MESH_OPTION_RELIABLE:
1514 ret = (const union MeshTunnelInfo *) &tunnel->reliable;
1516 case GNUNET_MESH_OPTION_OOORDER:
1517 ret = (const union MeshTunnelInfo *) &tunnel->ooorder;
1519 case GNUNET_MESH_OPTION_PEER:
1520 ret = (const union MeshTunnelInfo *) &tunnel->peer;
1530 struct GNUNET_MESH_TransmitHandle *
1531 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1532 struct GNUNET_TIME_Relative maxdelay,
1534 GNUNET_CONNECTION_TransmitReadyNotify notify,
1537 struct GNUNET_MESH_TransmitHandle *th;
1539 GNUNET_assert (NULL != tunnel);
1540 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1542 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", tunnel->allow_send);
1543 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1546 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1548 GNUNET_assert (NULL != notify);
1549 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1550 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1551 th->tunnel = tunnel;
1552 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1553 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1554 tunnel->packet_size = th->size;
1555 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1556 th->notify = notify;
1557 th->notify_cls = notify_cls;
1558 add_to_queue (tunnel->mesh, th);
1559 if (NULL != tunnel->mesh->th)
1561 if (GNUNET_NO == tunnel->allow_send)
1563 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1565 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1566 GNUNET_TIME_UNIT_FOREVER_REL,
1567 GNUNET_YES, &send_callback,
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1575 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1577 struct GNUNET_MESH_Handle *mesh;
1579 th->tunnel->packet_size = 0;
1580 mesh = th->tunnel->mesh;
1581 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1582 GNUNET_SCHEDULER_cancel (th->timeout_task);
1583 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1585 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1587 /* queue empty, no point in asking for transmission */
1588 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1594 GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel)
1601 * Request information about the running mesh peer.
1602 * The callback will be called for every tunnel known to the service,
1603 * listing all active peers that blong to the tunnel.
1605 * If called again on the same handle, it will overwrite the previous
1606 * callback and cls. To retrieve the cls, monitor_cancel must be
1609 * WARNING: unstable API, likely to change in the future!
1611 * @param h Handle to the mesh peer.
1612 * @param callback Function to call with the requested data.
1613 * @param callback_cls Closure for @c callback.
1616 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1617 GNUNET_MESH_TunnelsCB callback,
1620 struct GNUNET_MessageHeader msg;
1622 msg.size = htons (sizeof (msg));
1623 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1624 send_packet (h, &msg, NULL);
1625 h->tunnels_cb = callback;
1626 h->tunnels_cls = callback_cls;
1633 * Cancel a monitor request. The monitor callback will not be called.
1635 * @param h Mesh handle.
1637 * @return Closure given to GNUNET_MESH_monitor, if any.
1640 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1644 cls = h->tunnels_cls;
1645 h->tunnels_cb = NULL;
1646 h->tunnels_cls = NULL;
1652 * Request information about a specific tunnel of the running mesh peer.
1654 * WARNING: unstable API, likely to change in the future!
1655 * FIXME Add destination option.
1657 * @param h Handle to the mesh peer.
1658 * @param initiator ID of the owner of the tunnel.
1659 * @param tunnel_number Tunnel number.
1660 * @param callback Function to call with the requested data.
1661 * @param callback_cls Closure for @c callback.
1664 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1665 struct GNUNET_PeerIdentity *initiator,
1666 unsigned int tunnel_number,
1667 GNUNET_MESH_TunnelCB callback,
1670 struct GNUNET_MESH_LocalMonitor msg;
1672 msg.header.size = htons (sizeof (msg));
1673 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1674 msg.owner = *initiator;
1675 msg.tunnel_id = htonl (tunnel_number);
1677 send_packet (h, &msg.header, NULL);
1678 h->tunnel_cb = callback;
1679 h->tunnel_cls = callback_cls;
1686 * Function called to notify a client about the connection
1687 * begin ready to queue more data. "buf" will be
1688 * NULL and "size" zero if the connection was closed for
1689 * writing in the meantime.
1691 * @param cls closure
1692 * @param size number of bytes available in buf
1693 * @param buf where the callee should write the message
1694 * @return number of bytes written to buf
1697 mesh_mq_ntr (void *cls, size_t size,
1700 struct GNUNET_MQ_Handle *mq = cls;
1701 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1702 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1708 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1711 msize = ntohs (msg->size);
1712 GNUNET_assert (msize <= size);
1713 memcpy (buf, msg, msize);
1714 GNUNET_MQ_impl_send_continue (mq);
1720 * Signature of functions implementing the
1721 * sending functionality of a message queue.
1723 * @param mq the message queue
1724 * @param msg the message to send
1725 * @param impl_state state of the implementation
1728 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1729 const struct GNUNET_MessageHeader *msg, void *impl_state)
1731 struct MeshMQState *state = impl_state;
1733 GNUNET_assert (NULL == state->th);
1734 GNUNET_MQ_impl_send_commit (mq);
1736 GNUNET_MESH_notify_transmit_ready (state->tunnel,
1737 /* FIXME: add option for corking */
1739 GNUNET_TIME_UNIT_FOREVER_REL,
1747 * Signature of functions implementing the
1748 * destruction of a message queue.
1749 * Implementations must not free 'mq', but should
1750 * take care of 'impl_state'.
1752 * @param mq the message queue to destroy
1753 * @param impl_state state of the implementation
1756 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1758 struct MeshMQState *state = impl_state;
1760 if (NULL != state->th)
1761 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1763 GNUNET_free (state);
1768 * Create a message queue for a mesh tunnel.
1769 * The message queue can only be used to transmit messages,
1770 * not to receive them.
1772 * @param tunnel the tunnel to create the message qeue for
1773 * @return a message queue to messages over the tunnel
1775 struct GNUNET_MQ_Handle *
1776 GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel)
1778 struct GNUNET_MQ_Handle *mq;
1779 struct MeshMQState *state;
1781 state = GNUNET_new (struct MeshMQState);
1782 state->tunnel = tunnel;
1784 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
1785 mesh_mq_destroy_impl,
1786 NULL, /* FIXME: cancel impl. */
1788 NULL, /* no msg handlers */
1789 NULL, /* no err handlers */
1790 NULL); /* no handler cls */