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
244 * FIXME-BART: is this flag used? Seems dead right now...
252 * Opaque handle to a tunnel.
254 struct GNUNET_MESH_Tunnel
260 struct GNUNET_MESH_Tunnel *next;
265 struct GNUNET_MESH_Tunnel *prev;
268 * Handle to the mesh this tunnel belongs to
270 struct GNUNET_MESH_Handle *mesh;
273 * Local ID of the tunnel
275 MESH_TunnelNumber tid;
283 * Other end of the tunnel.
288 * Any data the caller wants to put in here
293 * Size of packet queued in this tunnel
295 unsigned int packet_size;
298 * Is the tunnel allowed to buffer?
303 * Is the tunnel realiable?
308 * If reliable, is the tunnel out of order?
313 * Are we allowed to send to the service?
321 * Implementation state for mesh's message queue.
326 * The current transmit handle, or NULL
327 * if no transmit is active.
329 struct GNUNET_MESH_TransmitHandle *th;
332 * Tunnel to send the data over.
334 struct GNUNET_MESH_Tunnel *tunnel;
338 /******************************************************************************/
339 /*********************** DECLARATIONS *************************/
340 /******************************************************************************/
343 * Function called to send a message to the service.
344 * "buf" will be NULL and "size" zero if the socket was closed for writing in
347 * @param cls closure, the mesh handle
348 * @param size number of bytes available in buf
349 * @param buf where the callee should write the connect message
350 * @return number of bytes written to buf
353 send_callback (void *cls, size_t size, void *buf);
356 /******************************************************************************/
357 /*********************** AUXILIARY FUNCTIONS *************************/
358 /******************************************************************************/
361 * Check if transmission is a payload packet.
363 * @param th Transmission handle.
365 * @return GNUNET_YES if it is a payload packet,
366 * GNUNET_NO if it is a mesh management packet.
369 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
371 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
376 * Check whether there is any message ready in the queue and find the size.
378 * @param h Mesh handle.
380 * @return The size of the first ready message in the queue,
381 * 0 if there is none.
384 message_ready_size (struct GNUNET_MESH_Handle *h)
386 struct GNUNET_MESH_TransmitHandle *th;
387 struct GNUNET_MESH_Tunnel *t;
389 for (th = h->th_head; NULL != th; th = th->next)
392 if (GNUNET_NO == th_is_payload (th))
394 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
397 if (GNUNET_YES == t->allow_send)
399 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
408 * Get the tunnel handler for the tunnel specified by id from the given handle
409 * @param h Mesh handle
410 * @param tid ID of the wanted tunnel
411 * @return handle to the required tunnel or NULL if not found
413 static struct GNUNET_MESH_Tunnel *
414 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
416 struct GNUNET_MESH_Tunnel *t;
430 * Create a new tunnel and insert it in the tunnel list of the mesh handle
431 * @param h Mesh handle
432 * @param tid desired tid of the tunnel, 0 to assign one automatically
433 * @return handle to the created tunnel
435 static struct GNUNET_MESH_Tunnel *
436 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
438 struct GNUNET_MESH_Tunnel *t;
440 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
441 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
445 t->tid = h->next_tid;
446 while (NULL != retrieve_tunnel (h, h->next_tid))
449 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
450 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
457 t->allow_send = GNUNET_NO;
458 t->nobuffer = GNUNET_NO;
464 * Destroy the specified tunnel.
465 * - Destroys all peers, calling the disconnect callback on each if needed
466 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
467 * - Calls cleaner if tunnel was inbound
468 * - Frees all memory used
470 * @param t Pointer to the tunnel.
471 * @param call_cleaner Whether to call the cleaner handler.
473 * @return Handle to the required tunnel or NULL if not found.
476 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
478 struct GNUNET_MESH_Handle *h;
479 struct GNUNET_MESH_TransmitHandle *th;
480 struct GNUNET_MESH_TransmitHandle *next;
487 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "destroy_tunnel %X\n",
492 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
494 /* signal tunnel destruction */
495 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
496 h->cleaner (h->cls, t, t->ctx);
498 /* check that clients did not leave messages behind in the queue */
499 for (th = h->th_head; NULL != th; th = next)
504 /* Clients should have aborted their requests already.
505 * Management traffic should be ok, as clients can't cancel that */
506 GNUNET_break (GNUNET_NO == th_is_payload(th));
507 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
509 /* clean up request */
510 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
511 GNUNET_SCHEDULER_cancel (th->timeout_task);
515 /* if there are no more pending requests with mesh service, cancel active request */
516 /* Note: this should be unnecessary... */
517 if ((0 == message_ready_size (h)) && (NULL != h->th))
519 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
524 GNUNET_PEER_change_rc (t->peer, -1);
531 * Notify client that the transmission has timed out
534 * @param tc task context
537 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
539 struct GNUNET_MESH_TransmitHandle *th = cls;
540 struct GNUNET_MESH_Handle *mesh;
542 mesh = th->tunnel->mesh;
543 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
544 th->tunnel->packet_size = 0;
545 if (GNUNET_YES == th_is_payload (th))
546 th->notify (th->notify_cls, 0, NULL);
548 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
550 /* nothing ready to transmit, no point in asking for transmission */
551 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
558 * Add a transmit handle to the transmission queue and set the
561 * @param h mesh handle with the queue head and tail
562 * @param th handle to the packet to be transmitted
565 add_to_queue (struct GNUNET_MESH_Handle *h,
566 struct GNUNET_MESH_TransmitHandle *th)
568 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
569 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
572 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
573 (th->timeout), &timeout_transmission, th);
578 * Auxiliary function to send an already constructed packet to the service.
579 * Takes care of creating a new queue element, copying the message and
580 * calling the tmt_rdy function if necessary.
582 * @param h mesh handle
583 * @param msg message to transmit
584 * @param tunnel tunnel this send is related to (NULL if N/A)
587 send_packet (struct GNUNET_MESH_Handle *h,
588 const struct GNUNET_MessageHeader *msg,
589 struct GNUNET_MESH_Tunnel *tunnel);
593 * Send an ack on the tunnel to confirm the processing of a message.
595 * @param t Tunnel on which to send the ACK.
598 send_ack (struct GNUNET_MESH_Tunnel *t)
600 struct GNUNET_MESH_LocalAck msg;
602 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on tunnel %X\n", t->tid);
603 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
604 msg.header.size = htons (sizeof (msg));
605 msg.tunnel_id = htonl (t->tid);
608 t->mesh->acks_sent++;
611 send_packet (t->mesh, &msg.header, t);
618 * Reconnect callback: tries to reconnect again after a failer previous
620 * @param cls closure (mesh handle)
621 * @param tc task context
624 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
628 * Send a connect packet to the service with the applications and types
629 * requested by the user.
631 * @param h The mesh handle.
635 send_connect (struct GNUNET_MESH_Handle *h)
639 size = sizeof (struct GNUNET_MESH_ClientConnect);
640 size += h->n_ports * sizeof (uint32_t);
642 char buf[size] GNUNET_ALIGN;
643 struct GNUNET_MESH_ClientConnect *msg;
647 /* build connection packet */
648 msg = (struct GNUNET_MESH_ClientConnect *) buf;
649 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
650 msg->header.size = htons (size);
651 ports = (uint32_t *) &msg[1];
652 for (i = 0; i < h->n_ports; i++)
654 ports[i] = htonl (h->ports[i]);
655 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Sending %lu bytes long message with %u ports\n",
660 ntohs (msg->header.size), h->n_ports);
661 send_packet (h, &msg->header, NULL);
667 * Reconnect to the service, retransmit all infomation to try to restore the
670 * @param h handle to the mesh
672 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
675 do_reconnect (struct GNUNET_MESH_Handle *h)
677 struct GNUNET_MESH_Tunnel *t;
679 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
680 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
681 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
682 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
683 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
688 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
691 if (NULL != h->client)
693 GNUNET_CLIENT_disconnect (h->client);
697 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
698 if (h->client == NULL)
700 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
703 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
704 GNUNET_TIME_relative_multiply
705 (h->reconnect_time, 2));
706 LOG (GNUNET_ERROR_TYPE_DEBUG,
707 "Next retry in %s\n",
708 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
715 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
718 /* Rebuild all tunnels */
719 for (t = h->tunnels_head; NULL != t; t = t->next)
721 struct GNUNET_MESH_TunnelMessage tmsg;
724 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
726 /* Tunnel was created by service (incoming tunnel) */
727 /* TODO: Notify service of missing tunnel, to request
728 * creator to recreate path (find a path to him via DHT?)
732 t->allow_send = GNUNET_NO;
733 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
734 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
735 tmsg.tunnel_id = htonl (t->tid);
736 tmsg.port = htonl (t->port);
737 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
740 if (GNUNET_YES == t->nobuffer)
741 options |= GNUNET_MESH_OPTION_NOBUFFER;
743 if (GNUNET_YES == t->reliable)
744 options |= GNUNET_MESH_OPTION_RELIABLE;
746 tmsg.opt = htonl (options);
747 send_packet (h, &tmsg.header, t);
753 * Reconnect callback: tries to reconnect again after a failer previous
755 * @param cls closure (mesh handle)
756 * @param tc task context
759 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
761 struct GNUNET_MESH_Handle *h = cls;
763 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
764 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
771 * Reconnect to the service, retransmit all infomation to try to restore the
774 * @param h handle to the mesh
776 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
779 reconnect (struct GNUNET_MESH_Handle *h)
781 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
782 h->in_receive = GNUNET_NO;
783 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
784 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
789 /******************************************************************************/
790 /*********************** RECEIVE HANDLERS ****************************/
791 /******************************************************************************/
794 * Process the new tunnel notification and add it to the tunnels in the handle
796 * @param h The mesh handle
797 * @param msg A message with the details of the new incoming tunnel
800 process_tunnel_created (struct GNUNET_MESH_Handle *h,
801 const struct GNUNET_MESH_TunnelMessage *msg)
803 struct GNUNET_MESH_Tunnel *t;
804 MESH_TunnelNumber tid;
807 tid = ntohl (msg->tunnel_id);
808 port = ntohl (msg->port);
809 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X:%u\n", tid, port);
810 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
815 if (NULL != h->new_tunnel)
817 t = create_tunnel (h, tid);
818 t->allow_send = GNUNET_NO;
819 t->peer = GNUNET_PEER_intern (&msg->peer);
823 if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
824 t->nobuffer = GNUNET_YES;
826 t->nobuffer = GNUNET_NO;
827 if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
828 t->reliable = GNUNET_YES;
830 t->reliable = GNUNET_NO;
831 if (GNUNET_YES == t->reliable &&
832 0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
833 t->ooorder = GNUNET_YES;
835 t->ooorder = GNUNET_NO;
836 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
837 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
838 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
842 struct GNUNET_MESH_TunnelMessage d_msg;
844 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
846 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
847 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
848 d_msg.tunnel_id = msg->tunnel_id;
849 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
853 send_packet (h, &d_msg.header, NULL);
860 * Process the tunnel destroy notification and free associated resources
862 * @param h The mesh handle
863 * @param msg A message with the details of the tunnel being destroyed
866 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
867 const struct GNUNET_MESH_TunnelMessage *msg)
869 struct GNUNET_MESH_Tunnel *t;
870 MESH_TunnelNumber tid;
872 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel from service\n");
873 tid = ntohl (msg->tunnel_id);
874 t = retrieve_tunnel (h, tid);
878 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X unknown\n", tid);
881 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
882 destroy_tunnel (t, GNUNET_YES);
887 * Process the incoming data packets, call appropriate handlers.
889 * @param h The mesh handle
890 * @param message A message encapsulating the data
893 process_incoming_data (struct GNUNET_MESH_Handle *h,
894 const struct GNUNET_MessageHeader *message)
896 const struct GNUNET_MessageHeader *payload;
897 const struct GNUNET_MESH_MessageHandler *handler;
898 struct GNUNET_MESH_LocalData *dmsg;
899 struct GNUNET_MESH_Tunnel *t;
903 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
905 dmsg = (struct GNUNET_MESH_LocalData *) message;
907 t = retrieve_tunnel (h, ntohl (dmsg->tid));
908 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
911 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
912 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
915 LOG (GNUNET_ERROR_TYPE_DEBUG,
916 " %s data on tunnel %s [%X]\n",
917 t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? "fwd" : "bck",
918 GNUNET_i2s (GNUNET_PEER_resolve2(t->peer)),
920 type = ntohs (payload->type);
921 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %u\n", type);
922 for (i = 0; i < h->n_handlers; i++)
924 handler = &h->message_handlers[i];
925 LOG (GNUNET_ERROR_TYPE_DEBUG,
926 " checking handler for type %u\n",
928 if (handler->type == type)
931 handler->callback (h->cls, t, &t->ctx, payload))
933 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
934 GNUNET_MESH_tunnel_destroy (t);
939 LOG (GNUNET_ERROR_TYPE_DEBUG,
940 "callback completed successfully\n");
948 * Process a local ACK message, enabling the client to send
949 * more data to the service.
951 * @param h Mesh handle.
952 * @param message Message itself.
955 process_ack (struct GNUNET_MESH_Handle *h,
956 const struct GNUNET_MessageHeader *message)
958 struct GNUNET_MESH_LocalAck *msg;
959 struct GNUNET_MESH_Tunnel *t;
960 MESH_TunnelNumber tid;
962 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
964 msg = (struct GNUNET_MESH_LocalAck *) message;
965 tid = ntohl (msg->tunnel_id);
966 t = retrieve_tunnel (h, tid);
969 LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown tunnel %X\n", tid);
972 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X!\n", t->tid);
973 t->allow_send = GNUNET_YES;
974 if (NULL == h->th && 0 < t->packet_size)
976 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
978 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
979 GNUNET_TIME_UNIT_FOREVER_REL,
980 GNUNET_YES, &send_callback, h);
986 * Process a local reply about info on all tunnels, pass info to the user.
988 * @param h Mesh handle.
989 * @param message Message itself.
992 process_get_tunnels (struct GNUNET_MESH_Handle *h,
993 const struct GNUNET_MessageHeader *message)
995 struct GNUNET_MESH_LocalMonitor *msg;
997 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
999 if (NULL == h->tunnels_cb)
1001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1005 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1006 if (ntohs (message->size) !=
1007 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1008 sizeof (struct GNUNET_PeerIdentity)))
1010 GNUNET_break_op (0);
1011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1012 "Get tunnels message: size %hu - expected %u\n",
1013 ntohs (message->size),
1014 sizeof (struct GNUNET_MESH_LocalMonitor));
1017 h->tunnels_cb (h->tunnels_cls,
1018 ntohl (msg->tunnel_id),
1026 * Process a local monitor_tunnel reply, pass info to the user.
1028 * @param h Mesh handle.
1029 * @param message Message itself.
1032 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1033 const struct GNUNET_MessageHeader *message)
1035 struct GNUNET_MESH_LocalMonitor *msg;
1038 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1040 if (NULL == h->tunnel_cb)
1042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1046 /* Verify message sanity */
1047 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1048 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1049 if (ntohs (message->size) != esize)
1051 GNUNET_break_op (0);
1052 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1053 "Show tunnel message: size %hu - expected %u\n",
1054 ntohs (message->size),
1057 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1058 h->tunnel_cb = NULL;
1059 h->tunnel_cls = NULL;
1064 h->tunnel_cb (h->tunnel_cls,
1071 * Function to process all messages received from the service
1073 * @param cls closure
1074 * @param msg message received, NULL on timeout or fatal error
1077 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1079 struct GNUNET_MESH_Handle *h = cls;
1084 LOG (GNUNET_ERROR_TYPE_DEBUG,
1085 "Mesh service disconnected, reconnecting\n", h);
1089 type = ntohs (msg->type);
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1091 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1092 GNUNET_MESH_DEBUG_M2S (type));
1095 /* Notify of a new incoming tunnel */
1096 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1097 process_tunnel_created (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1099 /* Notify of a tunnel disconnection */
1100 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1101 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1103 /* Notify of a new data packet in the tunnel */
1104 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1105 process_incoming_data (h, msg);
1107 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1108 process_ack (h, msg);
1110 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1111 process_get_tunnels (h, msg);
1113 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1114 process_show_tunnel (h, msg);
1117 /* We shouldn't get any other packages, log and ignore */
1118 LOG (GNUNET_ERROR_TYPE_WARNING,
1119 "unsolicited message form service (type %s)\n",
1120 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1123 if (GNUNET_YES == h->in_receive)
1125 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1126 GNUNET_TIME_UNIT_FOREVER_REL);
1130 LOG (GNUNET_ERROR_TYPE_DEBUG,
1131 "in receive off, not calling CLIENT_receive\n");
1136 /******************************************************************************/
1137 /************************ SEND FUNCTIONS ****************************/
1138 /******************************************************************************/
1141 * Function called to send a message to the service.
1142 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1145 * @param cls closure, the mesh handle
1146 * @param size number of bytes available in buf
1147 * @param buf where the callee should write the connect message
1148 * @return number of bytes written to buf
1151 send_callback (void *cls, size_t size, void *buf)
1153 struct GNUNET_MESH_Handle *h = cls;
1154 struct GNUNET_MESH_TransmitHandle *th;
1155 struct GNUNET_MESH_TransmitHandle *next;
1156 struct GNUNET_MESH_Tunnel *t;
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1164 if ((0 == size) || (NULL == buf))
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1173 nsize = message_ready_size (h);
1174 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1177 if (GNUNET_YES == th_is_payload (th))
1179 struct GNUNET_MESH_LocalData *dmsg;
1180 struct GNUNET_MessageHeader *mh;
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1183 if (GNUNET_NO == t->allow_send)
1185 /* This tunnel is not ready to transmit yet, try next message */
1190 GNUNET_assert (size >= th->size);
1191 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1192 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1193 psize = th->notify (th->notify_cls,
1194 size - sizeof (struct GNUNET_MESH_LocalData),
1198 psize += sizeof (struct GNUNET_MESH_LocalData);
1199 GNUNET_assert (size >= psize);
1200 dmsg->header.size = htons (psize);
1201 dmsg->tid = htonl (t->tid);
1202 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1203 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1204 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1205 t->allow_send = GNUNET_NO;
1209 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 "# callback returned size 0, "
1211 "application canceled transmission\n");
1216 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1218 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh internal traffic, type %s\n",
1219 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1220 memcpy (cbuf, &th[1], th->size);
1223 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1224 GNUNET_SCHEDULER_cancel (th->timeout_task);
1225 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1228 nsize = message_ready_size (h);
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1235 size = message_ready_size (h);
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1240 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1241 GNUNET_TIME_UNIT_FOREVER_REL,
1242 GNUNET_YES, &send_callback, h);
1246 if (NULL != h->th_head)
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1249 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1251 if (GNUNET_NO == h->in_receive)
1253 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1254 h->in_receive = GNUNET_YES;
1255 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1256 GNUNET_TIME_UNIT_FOREVER_REL);
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1264 * Auxiliary function to send an already constructed packet to the service.
1265 * Takes care of creating a new queue element, copying the message and
1266 * calling the tmt_rdy function if necessary.
1268 * @param h mesh handle
1269 * @param msg message to transmit
1270 * @param tunnel tunnel this send is related to (NULL if N/A)
1273 send_packet (struct GNUNET_MESH_Handle *h,
1274 const struct GNUNET_MessageHeader *msg,
1275 struct GNUNET_MESH_Tunnel *tunnel)
1277 struct GNUNET_MESH_TransmitHandle *th;
1280 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1281 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1282 msize = ntohs (msg->size);
1283 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1284 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1286 th->tunnel = tunnel;
1287 memcpy (&th[1], msg, msize);
1288 add_to_queue (h, th);
1289 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1292 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1294 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1295 GNUNET_TIME_UNIT_FOREVER_REL,
1296 GNUNET_YES, &send_callback, h);
1300 /******************************************************************************/
1301 /********************** API CALL DEFINITIONS *************************/
1302 /******************************************************************************/
1304 struct GNUNET_MESH_Handle *
1305 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1306 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1307 GNUNET_MESH_TunnelEndHandler cleaner,
1308 const struct GNUNET_MESH_MessageHandler *handlers,
1309 const uint32_t *ports)
1311 struct GNUNET_MESH_Handle *h;
1313 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1314 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1315 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1317 h->new_tunnel = new_tunnel;
1318 h->cleaner = cleaner;
1319 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1320 if (h->client == NULL)
1327 h->message_handlers = handlers;
1329 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1330 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1331 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1333 if (NULL != ports && ports[0] != 0 && NULL == new_tunnel)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "no new tunnel handler given, ports parameter is useless!!\n");
1339 if ((NULL == ports || ports[0] == 0) && NULL != new_tunnel)
1342 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 "no ports given, new tunnel handler will never be called!!\n");
1345 /* count handlers */
1346 for (h->n_handlers = 0;
1347 handlers && handlers[h->n_handlers].type;
1349 for (h->n_ports = 0;
1350 ports && ports[h->n_ports];
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1359 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1361 struct GNUNET_MESH_Tunnel *t;
1362 struct GNUNET_MESH_Tunnel *aux;
1363 struct GNUNET_MESH_TransmitHandle *th;
1365 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1368 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1369 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1372 t = handle->tunnels_head;
1376 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1381 destroy_tunnel (t, GNUNET_YES);
1384 while ( (th = handle->th_head) != NULL)
1386 struct GNUNET_MessageHeader *msg;
1388 /* Make sure it is an allowed packet (everything else should have been
1389 * already canceled).
1391 GNUNET_break (GNUNET_NO == th_is_payload (th));
1392 msg = (struct GNUNET_MessageHeader *) &th[1];
1393 switch (ntohs(msg->type))
1395 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1396 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1397 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1398 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1402 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1406 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1410 if (NULL != handle->th)
1412 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1415 if (NULL != handle->client)
1417 GNUNET_CLIENT_disconnect (handle->client);
1418 handle->client = NULL;
1420 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1422 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1423 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1425 GNUNET_free (handle);
1430 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1431 * and to broadcast).
1433 * @param h mesh handle
1434 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1435 * @param peer peer identity the tunnel should go to
1436 * @param port Port number.
1437 * @param nobuffer Flag for disabling buffering on relay nodes.
1438 * @param reliable Flag for end-to-end reliability.
1440 * @return handle to the tunnel
1442 struct GNUNET_MESH_Tunnel *
1443 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1445 const struct GNUNET_PeerIdentity *peer,
1450 struct GNUNET_MESH_Tunnel *t;
1451 struct GNUNET_MESH_TunnelMessage msg;
1453 LOG (GNUNET_ERROR_TYPE_DEBUG,
1454 "Creating new tunnel to %s:%u\n",
1455 GNUNET_i2s (peer), port);
1456 t = create_tunnel (h, 0);
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1459 t->ctx = tunnel_ctx;
1460 t->peer = GNUNET_PEER_intern (peer);
1461 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1462 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1463 msg.tunnel_id = htonl (t->tid);
1464 msg.port = htonl (port);
1467 if (GNUNET_YES == reliable)
1468 msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
1469 if (GNUNET_YES == nobuffer)
1470 msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
1471 msg.opt = htonl (msg.opt);
1473 send_packet (h, &msg.header, t);
1479 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1481 struct GNUNET_MESH_Handle *h;
1482 struct GNUNET_MESH_TunnelMessage msg;
1483 struct GNUNET_MESH_TransmitHandle *th;
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1488 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1489 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1490 msg.tunnel_id = htonl (tunnel->tid);
1491 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1497 struct GNUNET_MESH_TransmitHandle *aux;
1498 if (th->tunnel == tunnel)
1501 /* FIXME call the handler? */
1502 if (GNUNET_YES == th_is_payload (th))
1503 th->notify (th->notify_cls, 0, NULL);
1504 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1512 destroy_tunnel (tunnel, GNUNET_YES);
1513 send_packet (h, &msg.header, NULL);
1518 * Get information about a tunnel.
1520 * @param tunnel Tunnel handle.
1521 * @param option Query (GNUNET_MESH_OPTION_*).
1522 * @param ... dependant on option, currently not used
1524 * @return Union with an answer to the query.
1526 const union GNUNET_MESH_TunnelInfo *
1527 GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel,
1528 enum MeshTunnelOption option, ...)
1530 const union GNUNET_MESH_TunnelInfo *ret;
1534 case GNUNET_MESH_OPTION_NOBUFFER:
1535 ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->nobuffer;
1537 case GNUNET_MESH_OPTION_RELIABLE:
1538 ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->reliable;
1540 case GNUNET_MESH_OPTION_OOORDER:
1541 ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->ooorder;
1543 case GNUNET_MESH_OPTION_PEER:
1544 ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->peer;
1554 struct GNUNET_MESH_TransmitHandle *
1555 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1556 struct GNUNET_TIME_Relative maxdelay,
1558 GNUNET_CONNECTION_TransmitReadyNotify notify,
1561 struct GNUNET_MESH_TransmitHandle *th;
1563 GNUNET_assert (NULL != tunnel);
1564 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", tunnel->allow_send);
1567 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1570 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1571 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1572 GNUNET_assert (NULL != notify);
1573 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1574 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1575 th->tunnel = tunnel;
1576 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1577 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1578 tunnel->packet_size = th->size;
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1580 th->notify = notify;
1581 th->notify_cls = notify_cls;
1582 add_to_queue (tunnel->mesh, th);
1583 if (NULL != tunnel->mesh->th)
1585 if (GNUNET_NO == tunnel->allow_send)
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1589 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1590 GNUNET_TIME_UNIT_FOREVER_REL,
1591 GNUNET_YES, &send_callback,
1593 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1599 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1601 struct GNUNET_MESH_Handle *mesh;
1603 th->tunnel->packet_size = 0;
1604 mesh = th->tunnel->mesh;
1605 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1606 GNUNET_SCHEDULER_cancel (th->timeout_task);
1607 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1609 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1611 /* queue empty, no point in asking for transmission */
1612 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1618 GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel)
1625 * Request information about the running mesh peer.
1626 * The callback will be called for every tunnel known to the service,
1627 * listing all active peers that blong to the tunnel.
1629 * If called again on the same handle, it will overwrite the previous
1630 * callback and cls. To retrieve the cls, monitor_cancel must be
1633 * WARNING: unstable API, likely to change in the future!
1635 * @param h Handle to the mesh peer.
1636 * @param callback Function to call with the requested data.
1637 * @param callback_cls Closure for @c callback.
1640 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1641 GNUNET_MESH_TunnelsCB callback,
1644 struct GNUNET_MessageHeader msg;
1646 msg.size = htons (sizeof (msg));
1647 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1648 send_packet (h, &msg, NULL);
1649 h->tunnels_cb = callback;
1650 h->tunnels_cls = callback_cls;
1657 * Cancel a monitor request. The monitor callback will not be called.
1659 * @param h Mesh handle.
1661 * @return Closure given to GNUNET_MESH_monitor, if any.
1664 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1668 cls = h->tunnels_cls;
1669 h->tunnels_cb = NULL;
1670 h->tunnels_cls = NULL;
1676 * Request information about a specific tunnel of the running mesh peer.
1678 * WARNING: unstable API, likely to change in the future!
1679 * FIXME Add destination option.
1681 * @param h Handle to the mesh peer.
1682 * @param initiator ID of the owner of the tunnel.
1683 * @param tunnel_number Tunnel number.
1684 * @param callback Function to call with the requested data.
1685 * @param callback_cls Closure for @c callback.
1688 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1689 struct GNUNET_PeerIdentity *initiator,
1690 unsigned int tunnel_number,
1691 GNUNET_MESH_TunnelCB callback,
1694 struct GNUNET_MESH_LocalMonitor msg;
1696 msg.header.size = htons (sizeof (msg));
1697 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1698 msg.owner = *initiator;
1699 msg.tunnel_id = htonl (tunnel_number);
1701 send_packet (h, &msg.header, NULL);
1702 h->tunnel_cb = callback;
1703 h->tunnel_cls = callback_cls;
1710 * Function called to notify a client about the connection
1711 * begin ready to queue more data. "buf" will be
1712 * NULL and "size" zero if the connection was closed for
1713 * writing in the meantime.
1715 * @param cls closure
1716 * @param size number of bytes available in buf
1717 * @param buf where the callee should write the message
1718 * @return number of bytes written to buf
1721 mesh_mq_ntr (void *cls, size_t size,
1724 struct GNUNET_MQ_Handle *mq = cls;
1725 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1726 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1729 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "mesh-mq",
1730 "writing message (t: %u, s: %u) to buffer\n",
1731 ntohs (msg->type), ntohs (msg->size));
1736 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1739 msize = ntohs (msg->size);
1740 GNUNET_assert (msize <= size);
1741 memcpy (buf, msg, msize);
1742 GNUNET_MQ_impl_send_continue (mq);
1748 * Signature of functions implementing the
1749 * sending functionality of a message queue.
1751 * @param mq the message queue
1752 * @param msg the message to send
1753 * @param impl_state state of the implementation
1756 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1757 const struct GNUNET_MessageHeader *msg, void *impl_state)
1759 struct MeshMQState *state = impl_state;
1761 GNUNET_assert (NULL == state->th);
1762 GNUNET_MQ_impl_send_commit (mq);
1763 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "mesh-mq",
1764 "calling ntr for message (t: %u, s: %u)\n",
1765 ntohs (msg->type), ntohs (msg->size));
1767 GNUNET_MESH_notify_transmit_ready (state->tunnel,
1768 /* FIXME: add option for corking */
1770 GNUNET_TIME_UNIT_FOREVER_REL,
1778 * Signature of functions implementing the
1779 * destruction of a message queue.
1780 * Implementations must not free 'mq', but should
1781 * take care of 'impl_state'.
1783 * @param mq the message queue to destroy
1784 * @param impl_state state of the implementation
1787 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1789 struct MeshMQState *state = impl_state;
1791 if (NULL != state->th)
1792 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1794 GNUNET_free (state);
1799 * Create a message queue for a mesh tunnel.
1800 * The message queue can only be used to transmit messages,
1801 * not to receive them.
1803 * @param tunnel the tunnel to create the message qeue for
1804 * @return a message queue to messages over the tunnel
1806 struct GNUNET_MQ_Handle *
1807 GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel)
1809 struct GNUNET_MQ_Handle *mq;
1810 struct MeshMQState *state;
1812 state = GNUNET_new (struct MeshMQState);
1813 state->tunnel = tunnel;
1815 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
1816 mesh_mq_destroy_impl,
1817 NULL, /* FIXME: cancel impl. */
1819 NULL, /* no msg handlers */
1820 NULL, /* no err handlers */
1821 NULL); /* no handler cls */