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 allowed to buffer?
307 * Are we allowed to send to the service?
315 * Implementation state for mesh's message queue.
320 * The current transmit handle, or NULL
321 * if no transmit is active.
323 struct GNUNET_MESH_TransmitHandle *th;
326 * Tunnel to send the data over.
328 struct GNUNET_MESH_Tunnel *tunnel;
332 /******************************************************************************/
333 /*********************** DECLARATIONS *************************/
334 /******************************************************************************/
337 * Function called to send a message to the service.
338 * "buf" will be NULL and "size" zero if the socket was closed for writing in
341 * @param cls closure, the mesh handle
342 * @param size number of bytes available in buf
343 * @param buf where the callee should write the connect message
344 * @return number of bytes written to buf
347 send_callback (void *cls, size_t size, void *buf);
350 /******************************************************************************/
351 /*********************** AUXILIARY FUNCTIONS *************************/
352 /******************************************************************************/
355 * Check if transmission is a payload packet.
357 * @param th Transmission handle.
359 * @return GNUNET_YES if it is a payload packet,
360 * GNUNET_NO if it is a mesh management packet.
363 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
365 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
370 * Check whether there is any message ready in the queue and find the size.
372 * @param h Mesh handle.
374 * @return The size of the first ready message in the queue,
375 * 0 if there is none.
378 message_ready_size (struct GNUNET_MESH_Handle *h)
380 struct GNUNET_MESH_TransmitHandle *th;
381 struct GNUNET_MESH_Tunnel *t;
383 for (th = h->th_head; NULL != th; th = th->next)
386 if (GNUNET_NO == th_is_payload (th))
388 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
391 if (GNUNET_YES == t->allow_send)
393 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
402 * Get the tunnel handler for the tunnel specified by id from the given handle
403 * @param h Mesh handle
404 * @param tid ID of the wanted tunnel
405 * @return handle to the required tunnel or NULL if not found
407 static struct GNUNET_MESH_Tunnel *
408 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
410 struct GNUNET_MESH_Tunnel *t;
424 * Create a new tunnel and insert it in the tunnel list of the mesh handle
425 * @param h Mesh handle
426 * @param tid desired tid of the tunnel, 0 to assign one automatically
427 * @return handle to the created tunnel
429 static struct GNUNET_MESH_Tunnel *
430 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
432 struct GNUNET_MESH_Tunnel *t;
434 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
435 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
439 t->tid = h->next_tid;
440 while (NULL != retrieve_tunnel (h, h->next_tid))
443 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
444 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
451 t->allow_send = GNUNET_NO;
452 t->buffering = GNUNET_YES;
458 * Destroy the specified tunnel.
459 * - Destroys all peers, calling the disconnect callback on each if needed
460 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
461 * - Calls cleaner if tunnel was inbound
462 * - Frees all memory used
464 * @param t Pointer to the tunnel.
465 * @param call_cleaner Whether to call the cleaner handler.
467 * @return Handle to the required tunnel or NULL if not found.
470 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
472 struct GNUNET_MESH_Handle *h;
473 struct GNUNET_MESH_TransmitHandle *th;
474 struct GNUNET_MESH_TransmitHandle *next;
476 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
485 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
487 /* signal tunnel destruction */
488 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
489 h->cleaner (h->cls, t, t->ctx);
491 /* check that clients did not leave messages behind in the queue */
492 for (th = h->th_head; NULL != th; th = next)
497 /* Clients should have aborted their requests already.
498 * Management traffic should be ok, as clients can't cancel that */
499 GNUNET_break (GNUNET_NO == th_is_payload(th));
500 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
502 /* clean up request */
503 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
504 GNUNET_SCHEDULER_cancel (th->timeout_task);
508 /* if there are no more pending requests with mesh service, cancel active request */
509 /* Note: this should be unnecessary... */
510 if ((0 == message_ready_size (h)) && (NULL != h->th))
512 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
517 GNUNET_PEER_change_rc (t->peer, -1);
524 * Notify client that the transmission has timed out
527 * @param tc task context
530 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
532 struct GNUNET_MESH_TransmitHandle *th = cls;
533 struct GNUNET_MESH_Handle *mesh;
535 mesh = th->tunnel->mesh;
536 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
537 th->tunnel->packet_size = 0;
538 if (GNUNET_YES == th_is_payload (th))
539 th->notify (th->notify_cls, 0, NULL);
541 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
543 /* nothing ready to transmit, no point in asking for transmission */
544 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
551 * Add a transmit handle to the transmission queue and set the
554 * @param h mesh handle with the queue head and tail
555 * @param th handle to the packet to be transmitted
558 add_to_queue (struct GNUNET_MESH_Handle *h,
559 struct GNUNET_MESH_TransmitHandle *th)
561 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
562 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
565 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
566 (th->timeout), &timeout_transmission, th);
571 * Auxiliary function to send an already constructed packet to the service.
572 * Takes care of creating a new queue element, copying the message and
573 * calling the tmt_rdy function if necessary.
575 * @param h mesh handle
576 * @param msg message to transmit
577 * @param tunnel tunnel this send is related to (NULL if N/A)
580 send_packet (struct GNUNET_MESH_Handle *h,
581 const struct GNUNET_MessageHeader *msg,
582 struct GNUNET_MESH_Tunnel *tunnel);
586 * Send an ack on the tunnel to confirm the processing of a message.
588 * @param t Tunnel on which to send the ACK.
591 send_ack (struct GNUNET_MESH_Tunnel *t)
593 struct GNUNET_MESH_LocalAck msg;
595 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on tunnel %X\n", t->tid);
596 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
597 msg.header.size = htons (sizeof (msg));
598 msg.tunnel_id = htonl (t->tid);
601 t->mesh->acks_sent++;
604 send_packet (t->mesh, &msg.header, t);
611 * Reconnect callback: tries to reconnect again after a failer previous
613 * @param cls closure (mesh handle)
614 * @param tc task context
617 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
621 * Send a connect packet to the service with the applications and types
622 * requested by the user.
624 * @param h The mesh handle.
628 send_connect (struct GNUNET_MESH_Handle *h)
632 size = sizeof (struct GNUNET_MESH_ClientConnect);
633 size += h->n_ports * sizeof (uint32_t);
635 char buf[size] GNUNET_ALIGN;
636 struct GNUNET_MESH_ClientConnect *msg;
640 /* build connection packet */
641 msg = (struct GNUNET_MESH_ClientConnect *) buf;
642 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
643 msg->header.size = htons (size);
644 ports = (uint32_t *) &msg[1];
645 for (i = 0; i < h->n_ports; i++)
647 ports[i] = htonl (h->ports[i]);
648 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
651 LOG (GNUNET_ERROR_TYPE_DEBUG,
652 "Sending %lu bytes long message with %u ports\n",
653 ntohs (msg->header.size), h->n_ports);
654 send_packet (h, &msg->header, NULL);
660 * Reconnect to the service, retransmit all infomation to try to restore the
663 * @param h handle to the mesh
665 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
668 do_reconnect (struct GNUNET_MESH_Handle *h)
670 struct GNUNET_MESH_Tunnel *t;
672 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
674 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
675 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
681 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
684 if (NULL != h->client)
686 GNUNET_CLIENT_disconnect (h->client);
690 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
691 if (h->client == NULL)
693 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
696 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
697 GNUNET_TIME_relative_multiply
698 (h->reconnect_time, 2));
699 LOG (GNUNET_ERROR_TYPE_DEBUG,
700 "Next retry in %s\n",
701 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
708 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
711 /* Rebuild all tunnels */
712 for (t = h->tunnels_head; NULL != t; t = t->next)
714 struct GNUNET_MESH_TunnelMessage tmsg;
717 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
719 /* Tunnel was created by service (incoming tunnel) */
720 /* TODO: Notify service of missing tunnel, to request
721 * creator to recreate path (find a path to him via DHT?)
725 t->allow_send = GNUNET_NO;
726 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
727 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
728 tmsg.tunnel_id = htonl (t->tid);
729 tmsg.port = htonl (t->port);
730 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
733 if (GNUNET_NO == t->buffering)
734 options |= GNUNET_MESH_OPTION_NOBUFFER;
736 if (GNUNET_YES == t->reliable)
737 options |= GNUNET_MESH_OPTION_RELIABLE;
739 tmsg.options = htonl (options);
740 send_packet (h, &tmsg.header, t);
746 * Reconnect callback: tries to reconnect again after a failer previous
748 * @param cls closure (mesh handle)
749 * @param tc task context
752 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
754 struct GNUNET_MESH_Handle *h = cls;
756 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
757 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
764 * Reconnect to the service, retransmit all infomation to try to restore the
767 * @param h handle to the mesh
769 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
772 reconnect (struct GNUNET_MESH_Handle *h)
774 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
775 h->in_receive = GNUNET_NO;
776 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
777 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
782 /******************************************************************************/
783 /*********************** RECEIVE HANDLERS ****************************/
784 /******************************************************************************/
787 * Process the new tunnel notification and add it to the tunnels in the handle
789 * @param h The mesh handle
790 * @param msg A message with the details of the new incoming tunnel
793 process_tunnel_created (struct GNUNET_MESH_Handle *h,
794 const struct GNUNET_MESH_TunnelNotification *msg)
796 struct GNUNET_MESH_Tunnel *t;
797 MESH_TunnelNumber tid;
799 tid = ntohl (msg->tunnel_id);
800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
801 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
806 if (NULL != h->new_tunnel)
808 t = create_tunnel (h, tid);
809 t->allow_send = GNUNET_NO;
810 t->peer = GNUNET_PEER_intern (&msg->peer);
813 t->port = ntohl (msg->port);
814 if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
815 t->buffering = GNUNET_NO;
817 t->buffering = GNUNET_YES;
818 if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
819 t->reliable = GNUNET_YES;
821 t->reliable = GNUNET_NO;
822 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
823 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
824 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
828 struct GNUNET_MESH_TunnelMessage d_msg;
830 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
832 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
833 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
834 d_msg.tunnel_id = msg->tunnel_id;
835 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
839 send_packet (h, &d_msg.header, NULL);
846 * Process the tunnel destroy notification and free associated resources
848 * @param h The mesh handle
849 * @param msg A message with the details of the tunnel being destroyed
852 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
853 const struct GNUNET_MESH_TunnelMessage *msg)
855 struct GNUNET_MESH_Tunnel *t;
856 MESH_TunnelNumber tid;
858 tid = ntohl (msg->tunnel_id);
859 t = retrieve_tunnel (h, tid);
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
866 destroy_tunnel (t, GNUNET_YES);
872 * Process the incoming data packets, call appropriate handlers.
874 * @param h The mesh handle
875 * @param message A message encapsulating the data
878 process_incoming_data (struct GNUNET_MESH_Handle *h,
879 const struct GNUNET_MessageHeader *message)
881 const struct GNUNET_MessageHeader *payload;
882 const struct GNUNET_MESH_MessageHandler *handler;
883 const struct GNUNET_PeerIdentity *peer;
884 struct GNUNET_PeerIdentity id;
885 struct GNUNET_MESH_LocalData *dmsg;
886 struct GNUNET_MESH_Tunnel *t;
890 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
892 dmsg = (struct GNUNET_MESH_LocalData *) message;
894 t = retrieve_tunnel (h, ntohl (dmsg->tid));
895 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
896 GNUNET_PEER_resolve (t->peer, &id);
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 (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 for (i = 0; i < h->n_handlers; i++)
910 handler = &h->message_handlers[i];
911 if (handler->type == type)
914 handler->callback (h->cls, t, &t->ctx, payload))
916 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
917 GNUNET_MESH_tunnel_destroy (t);
922 LOG (GNUNET_ERROR_TYPE_DEBUG,
923 "callback completed successfully\n");
931 * Process a local ACK message, enabling the client to send
932 * more data to the service.
934 * @param h Mesh handle.
935 * @param message Message itself.
938 process_ack (struct GNUNET_MESH_Handle *h,
939 const struct GNUNET_MessageHeader *message)
941 struct GNUNET_MESH_LocalAck *msg;
942 struct GNUNET_MESH_Tunnel *t;
943 MESH_TunnelNumber tid;
945 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
947 msg = (struct GNUNET_MESH_LocalAck *) message;
948 tid = ntohl (msg->tunnel_id);
949 t = retrieve_tunnel (h, tid);
952 LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown tunnel %X\n", tid);
955 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X!\n", t->tid);
956 t->allow_send = GNUNET_YES;
957 if (NULL == h->th && 0 < t->packet_size)
959 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
961 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
962 GNUNET_TIME_UNIT_FOREVER_REL,
963 GNUNET_YES, &send_callback, h);
969 * Process a local reply about info on all tunnels, pass info to the user.
971 * @param h Mesh handle.
972 * @param message Message itself.
975 process_get_tunnels (struct GNUNET_MESH_Handle *h,
976 const struct GNUNET_MessageHeader *message)
978 struct GNUNET_MESH_LocalMonitor *msg;
980 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
982 if (NULL == h->tunnels_cb)
984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
988 msg = (struct GNUNET_MESH_LocalMonitor *) message;
989 if (ntohs (message->size) !=
990 (sizeof (struct GNUNET_MESH_LocalMonitor) +
991 sizeof (struct GNUNET_PeerIdentity)))
994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
995 "Get tunnels message: size %hu - expected %u\n",
996 ntohs (message->size),
997 sizeof (struct GNUNET_MESH_LocalMonitor));
1000 h->tunnels_cb (h->tunnels_cls,
1001 ntohl (msg->tunnel_id),
1009 * Process a local monitor_tunnel reply, pass info to the user.
1011 * @param h Mesh handle.
1012 * @param message Message itself.
1015 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1016 const struct GNUNET_MessageHeader *message)
1018 struct GNUNET_MESH_LocalMonitor *msg;
1021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1023 if (NULL == h->tunnel_cb)
1025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1029 /* Verify message sanity */
1030 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1031 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1032 if (ntohs (message->size) != esize)
1034 GNUNET_break_op (0);
1035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1036 "Show tunnel message: size %hu - expected %u\n",
1037 ntohs (message->size),
1040 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1041 h->tunnel_cb = NULL;
1042 h->tunnel_cls = NULL;
1047 h->tunnel_cb (h->tunnel_cls,
1054 * Function to process all messages received from the service
1056 * @param cls closure
1057 * @param msg message received, NULL on timeout or fatal error
1060 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1062 struct GNUNET_MESH_Handle *h = cls;
1066 LOG (GNUNET_ERROR_TYPE_DEBUG,
1067 "Mesh service disconnected, reconnecting\n", h);
1071 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1072 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1074 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1075 switch (ntohs (msg->type))
1077 /* Notify of a new incoming tunnel */
1078 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1079 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1081 /* Notify of a tunnel disconnection */
1082 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1083 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1085 /* Notify of a new data packet in the tunnel */
1086 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1087 process_incoming_data (h, msg);
1089 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1090 process_ack (h, msg);
1092 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1093 process_get_tunnels (h, msg);
1095 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1096 process_show_tunnel (h, msg);
1099 /* We shouldn't get any other packages, log and ignore */
1100 LOG (GNUNET_ERROR_TYPE_WARNING,
1101 "unsolicited message form service (type %s)\n",
1102 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1105 if (GNUNET_YES == h->in_receive)
1107 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1108 GNUNET_TIME_UNIT_FOREVER_REL);
1112 LOG (GNUNET_ERROR_TYPE_DEBUG,
1113 "in receive off, not calling CLIENT_receive\n");
1118 /******************************************************************************/
1119 /************************ SEND FUNCTIONS ****************************/
1120 /******************************************************************************/
1123 * Function called to send a message to the service.
1124 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1127 * @param cls closure, the mesh handle
1128 * @param size number of bytes available in buf
1129 * @param buf where the callee should write the connect message
1130 * @return number of bytes written to buf
1133 send_callback (void *cls, size_t size, void *buf)
1135 struct GNUNET_MESH_Handle *h = cls;
1136 struct GNUNET_MESH_TransmitHandle *th;
1137 struct GNUNET_MESH_TransmitHandle *next;
1138 struct GNUNET_MESH_Tunnel *t;
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1146 if ((0 == size) || (NULL == buf))
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1155 nsize = message_ready_size (h);
1156 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1159 if (GNUNET_YES == th_is_payload (th))
1161 struct GNUNET_MESH_LocalData *dmsg;
1162 struct GNUNET_MessageHeader *mh;
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1165 if (GNUNET_NO == t->allow_send)
1167 /* This tunnel is not ready to transmit yet, try next message */
1172 t->allow_send = GNUNET_NO;
1173 GNUNET_assert (size >= th->size);
1174 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1175 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1176 psize = th->notify (th->notify_cls,
1177 size - sizeof (struct GNUNET_MESH_LocalData),
1181 psize += sizeof (struct GNUNET_MESH_LocalData);
1182 GNUNET_assert (size >= psize);
1183 dmsg->header.size = htons (psize);
1184 dmsg->tid = htonl (t->tid);
1186 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1187 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1188 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1193 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1195 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh traffic, type %s\n",
1196 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1197 memcpy (cbuf, &th[1], th->size);
1200 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1201 GNUNET_SCHEDULER_cancel (th->timeout_task);
1202 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1205 nsize = message_ready_size (h);
1210 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1212 size = message_ready_size (h);
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1217 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1218 GNUNET_TIME_UNIT_FOREVER_REL,
1219 GNUNET_YES, &send_callback, h);
1223 if (NULL != h->th_head)
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1228 if (GNUNET_NO == h->in_receive)
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1231 h->in_receive = GNUNET_YES;
1232 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1233 GNUNET_TIME_UNIT_FOREVER_REL);
1235 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1241 * Auxiliary function to send an already constructed packet to the service.
1242 * Takes care of creating a new queue element, copying the message and
1243 * calling the tmt_rdy function if necessary.
1245 * @param h mesh handle
1246 * @param msg message to transmit
1247 * @param tunnel tunnel this send is related to (NULL if N/A)
1250 send_packet (struct GNUNET_MESH_Handle *h,
1251 const struct GNUNET_MessageHeader *msg,
1252 struct GNUNET_MESH_Tunnel *tunnel)
1254 struct GNUNET_MESH_TransmitHandle *th;
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1258 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1259 msize = ntohs (msg->size);
1260 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1261 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1263 th->tunnel = tunnel;
1264 memcpy (&th[1], msg, msize);
1265 add_to_queue (h, th);
1266 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1269 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1271 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1272 GNUNET_TIME_UNIT_FOREVER_REL,
1273 GNUNET_YES, &send_callback, h);
1277 /******************************************************************************/
1278 /********************** API CALL DEFINITIONS *************************/
1279 /******************************************************************************/
1281 struct GNUNET_MESH_Handle *
1282 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1283 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1284 GNUNET_MESH_TunnelEndHandler cleaner,
1285 const struct GNUNET_MESH_MessageHandler *handlers,
1286 const uint32_t *ports)
1288 struct GNUNET_MESH_Handle *h;
1290 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1291 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1292 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1294 h->new_tunnel = new_tunnel;
1295 h->cleaner = cleaner;
1296 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1297 if (h->client == NULL)
1304 h->message_handlers = handlers;
1306 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1307 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1308 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1310 /* count handlers */
1311 for (h->n_handlers = 0;
1312 handlers && handlers[h->n_handlers].type;
1314 for (h->n_ports = 0;
1315 ports && ports[h->n_ports];
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1324 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1326 struct GNUNET_MESH_Tunnel *t;
1327 struct GNUNET_MESH_Tunnel *aux;
1328 struct GNUNET_MESH_TransmitHandle *th;
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1333 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1334 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1337 t = handle->tunnels_head;
1341 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1346 destroy_tunnel (t, GNUNET_YES);
1349 while ( (th = handle->th_head) != NULL)
1351 struct GNUNET_MessageHeader *msg;
1353 /* Make sure it is an allowed packet (everything else should have been
1354 * already canceled).
1356 GNUNET_break (GNUNET_NO == th_is_payload (th));
1357 msg = (struct GNUNET_MessageHeader *) &th[1];
1358 switch (ntohs(msg->type))
1360 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1361 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1362 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1363 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1367 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1371 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1375 if (NULL != handle->th)
1377 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1380 if (NULL != handle->client)
1382 GNUNET_CLIENT_disconnect (handle->client);
1383 handle->client = NULL;
1385 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1387 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1388 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1390 GNUNET_free (handle);
1395 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1396 * and to broadcast).
1398 * @param h mesh handle
1399 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1400 * @param peer peer identity the tunnel should go to
1401 * @param port Port number.
1402 * @param buffer Flag for buffering on relay nodes.
1403 * @param reliable Flag for end-to-end reliability.
1405 * @return handle to the tunnel
1407 struct GNUNET_MESH_Tunnel *
1408 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1410 const struct GNUNET_PeerIdentity *peer,
1415 struct GNUNET_MESH_Tunnel *t;
1416 struct GNUNET_MESH_TunnelMessage msg;
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1419 t = create_tunnel (h, 0);
1420 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1422 t->ctx = tunnel_ctx;
1423 t->peer = GNUNET_PEER_intern (peer);
1424 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1425 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1426 msg.tunnel_id = htonl (t->tid);
1427 msg.port = htonl (port);
1431 send_packet (h, &msg.header, t);
1437 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1439 struct GNUNET_MESH_Handle *h;
1440 struct GNUNET_MESH_TunnelMessage msg;
1441 struct GNUNET_MESH_TransmitHandle *th;
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1446 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1447 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1448 msg.tunnel_id = htonl (tunnel->tid);
1449 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1455 struct GNUNET_MESH_TransmitHandle *aux;
1456 if (th->tunnel == tunnel)
1459 /* FIXME call the handler? */
1460 if (GNUNET_YES == th_is_payload (th))
1461 th->notify (th->notify_cls, 0, NULL);
1462 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1470 destroy_tunnel (tunnel, GNUNET_YES);
1471 send_packet (h, &msg.header, NULL);
1476 * Get information about a tunnel.
1478 * The existing end callback for the tunnel will be called immediately.
1479 * Any pending outgoing messages will be sent but no incoming messages will be
1480 * accepted and no data callbacks will be called.
1482 * @param tunnel Tunnel handle.
1484 * @return Allocated, {0, NULL} terminated set of tunnel properties.
1486 struct MeshTunnelInfo *
1487 GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel)
1489 struct MeshTunnelInfo *ret;
1491 ret = GNUNET_malloc (sizeof (struct MeshTunnelInfo) * 3);
1492 ret[0].prop = GNUNET_MESH_OPTION_NOBUFFER;
1493 ret[0].value = &tunnel->buffering; // FIXME return ¬buffering ("nobuffer")
1494 ret[1].prop = GNUNET_MESH_OPTION_RELIABLE;
1495 ret[1].value = &tunnel->reliable;
1497 ret[2].value = NULL;
1502 struct GNUNET_MESH_TransmitHandle *
1503 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1504 struct GNUNET_TIME_Relative maxdelay,
1506 GNUNET_CONNECTION_TransmitReadyNotify notify,
1509 struct GNUNET_MESH_TransmitHandle *th;
1511 GNUNET_assert (NULL != tunnel);
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", tunnel->allow_send);
1515 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1518 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1519 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1520 GNUNET_assert (NULL != notify);
1521 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1522 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1523 th->tunnel = tunnel;
1524 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1525 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1526 tunnel->packet_size = th->size;
1527 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1528 th->notify = notify;
1529 th->notify_cls = notify_cls;
1530 add_to_queue (tunnel->mesh, th);
1531 if (NULL != tunnel->mesh->th)
1533 if (GNUNET_NO == tunnel->allow_send)
1535 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1537 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1538 GNUNET_TIME_UNIT_FOREVER_REL,
1539 GNUNET_YES, &send_callback,
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1547 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1549 struct GNUNET_MESH_Handle *mesh;
1551 th->tunnel->packet_size = 0;
1552 mesh = th->tunnel->mesh;
1553 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1554 GNUNET_SCHEDULER_cancel (th->timeout_task);
1555 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1557 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1559 /* queue empty, no point in asking for transmission */
1560 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1566 GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel)
1573 * Request information about the running mesh peer.
1574 * The callback will be called for every tunnel known to the service,
1575 * listing all active peers that blong to the tunnel.
1577 * If called again on the same handle, it will overwrite the previous
1578 * callback and cls. To retrieve the cls, monitor_cancel must be
1581 * WARNING: unstable API, likely to change in the future!
1583 * @param h Handle to the mesh peer.
1584 * @param callback Function to call with the requested data.
1585 * @param callback_cls Closure for @c callback.
1588 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1589 GNUNET_MESH_TunnelsCB callback,
1592 struct GNUNET_MessageHeader msg;
1594 msg.size = htons (sizeof (msg));
1595 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1596 send_packet (h, &msg, NULL);
1597 h->tunnels_cb = callback;
1598 h->tunnels_cls = callback_cls;
1605 * Cancel a monitor request. The monitor callback will not be called.
1607 * @param h Mesh handle.
1609 * @return Closure given to GNUNET_MESH_monitor, if any.
1612 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1616 cls = h->tunnels_cls;
1617 h->tunnels_cb = NULL;
1618 h->tunnels_cls = NULL;
1624 * Request information about a specific tunnel of the running mesh peer.
1626 * WARNING: unstable API, likely to change in the future!
1627 * FIXME Add destination option.
1629 * @param h Handle to the mesh peer.
1630 * @param initiator ID of the owner of the tunnel.
1631 * @param tunnel_number Tunnel number.
1632 * @param callback Function to call with the requested data.
1633 * @param callback_cls Closure for @c callback.
1636 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1637 struct GNUNET_PeerIdentity *initiator,
1638 unsigned int tunnel_number,
1639 GNUNET_MESH_TunnelCB callback,
1642 struct GNUNET_MESH_LocalMonitor msg;
1644 msg.header.size = htons (sizeof (msg));
1645 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1646 msg.owner = *initiator;
1647 msg.tunnel_id = htonl (tunnel_number);
1649 send_packet (h, &msg.header, NULL);
1650 h->tunnel_cb = callback;
1651 h->tunnel_cls = callback_cls;
1658 * Function called to notify a client about the connection
1659 * begin ready to queue more data. "buf" will be
1660 * NULL and "size" zero if the connection was closed for
1661 * writing in the meantime.
1663 * @param cls closure
1664 * @param size number of bytes available in buf
1665 * @param buf where the callee should write the message
1666 * @return number of bytes written to buf
1669 mesh_mq_ntr (void *cls, size_t size,
1672 struct GNUNET_MQ_Handle *mq = cls;
1673 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1674 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1680 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1683 msize = ntohs (msg->size);
1684 GNUNET_assert (msize <= size);
1685 memcpy (buf, msg, msize);
1686 GNUNET_MQ_impl_send_continue (mq);
1692 * Signature of functions implementing the
1693 * sending functionality of a message queue.
1695 * @param mq the message queue
1696 * @param msg the message to send
1697 * @param impl_state state of the implementation
1700 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1701 const struct GNUNET_MessageHeader *msg, void *impl_state)
1703 struct MeshMQState *state = impl_state;
1705 GNUNET_assert (NULL == state->th);
1706 GNUNET_MQ_impl_send_commit (mq);
1708 GNUNET_MESH_notify_transmit_ready (state->tunnel,
1709 /* FIXME: add option for corking */
1711 GNUNET_TIME_UNIT_FOREVER_REL,
1719 * Signature of functions implementing the
1720 * destruction of a message queue.
1721 * Implementations must not free 'mq', but should
1722 * take care of 'impl_state'.
1724 * @param mq the message queue to destroy
1725 * @param impl_state state of the implementation
1728 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1730 struct MeshMQState *state = impl_state;
1732 if (NULL != state->th)
1733 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1735 GNUNET_free (state);
1740 * Create a message queue for a mesh tunnel.
1741 * The message queue can only be used to transmit messages,
1742 * not to receive them.
1744 * @param tunnel the tunnel to create the message qeue for
1745 * @return a message queue to messages over the tunnel
1747 struct GNUNET_MQ_Handle *
1748 GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel)
1750 struct GNUNET_MQ_Handle *mq;
1751 struct MeshMQState *state;
1753 state = GNUNET_new (struct MeshMQState);
1754 state->tunnel = tunnel;
1756 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
1757 mesh_mq_destroy_impl,
1758 NULL, /* FIXME: cancel impl. */
1760 NULL, /* no msg handlers */
1761 NULL, /* no err handlers */
1762 NULL); /* no handler cls */