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/mesh2_api.c
20 * @brief mesh2 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_mesh2_service.h"
40 #include "mesh2_protocol.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "mesh2-api",__VA_ARGS__)
44 /* FIXME: use dynamic values, expose in API */
45 #define INITIAL_WINDOW_SIZE 4
46 #define ACK_THRESHOLD 2
49 #define DEBUG_ACK GNUNET_YES
51 /******************************************************************************/
52 /************************ DATA STRUCTURES ****************************/
53 /******************************************************************************/
56 * Transmission queue to the service
58 struct GNUNET_MESH_TransmitHandle
64 struct GNUNET_MESH_TransmitHandle *next;
69 struct GNUNET_MESH_TransmitHandle *prev;
72 * Tunnel this message is sent on / for (may be NULL for control messages).
74 struct GNUNET_MESH_Tunnel *tunnel;
77 * Callback to obtain the message to transmit, or NULL if we
78 * got the message in 'data'. Notice that messages built
79 * by 'notify' need to be encapsulated with information about
82 GNUNET_CONNECTION_TransmitReadyNotify notify;
85 * Closure for 'notify'
90 * How long is this message valid. Once the timeout has been
91 * reached, the message must no longer be sent. If this
92 * is a message with a 'notify' callback set, the 'notify'
93 * function should be called with 'buf' NULL and size 0.
95 struct GNUNET_TIME_Absolute timeout;
98 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
100 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
103 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
110 * Opaque handle to the service.
112 struct GNUNET_MESH_Handle
116 * Handle to the server connection, to send messages later
118 struct GNUNET_CLIENT_Connection *client;
121 * Set of handlers used for processing incoming messages in the tunnels
123 const struct GNUNET_MESH_MessageHandler *message_handlers;
126 * Number of handlers in the handlers array.
128 unsigned int n_handlers;
133 const uint32_t *ports;
138 unsigned int n_ports;
141 * Double linked list of the tunnels this client is connected to, head.
143 struct GNUNET_MESH_Tunnel *tunnels_head;
146 * Double linked list of the tunnels this client is connected to, tail.
148 struct GNUNET_MESH_Tunnel *tunnels_tail;
151 * Callback for inbound tunnel creation
153 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
156 * Callback for inbound tunnel disconnection
158 GNUNET_MESH_TunnelEndHandler *cleaner;
161 * Handle to cancel pending transmissions in case of disconnection
163 struct GNUNET_CLIENT_TransmitHandle *th;
166 * Closure for all the handlers given by the client
171 * Messages to send to the service, head.
173 struct GNUNET_MESH_TransmitHandle *th_head;
176 * Messages to send to the service, tail.
178 struct GNUNET_MESH_TransmitHandle *th_tail;
181 * tid of the next tunnel to create (to avoid reusing IDs often)
183 MESH_TunnelNumber next_tid;
186 * Have we started the task to receive messages from the service
187 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
192 * Configuration given by the client, in case of reconnection
194 const struct GNUNET_CONFIGURATION_Handle *cfg;
197 * Time to the next reconnect in case one reconnect fails
199 struct GNUNET_TIME_Relative reconnect_time;
202 * Task for trying to reconnect.
204 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
209 GNUNET_MESH_TunnelsCB tunnels_cb;
212 * Monitor callback closure.
219 GNUNET_MESH_TunnelCB tunnel_cb;
222 * Tunnel callback closure.
227 unsigned int acks_sent;
228 unsigned int acks_recv;
234 * Description of a peer
236 struct GNUNET_MESH_Peer
239 * ID of the peer in short form
244 * Tunnel this peer belongs to
246 struct GNUNET_MESH_Tunnel *t;
249 * Flag indicating whether service has informed about its connection
257 * Opaque handle to a tunnel.
259 struct GNUNET_MESH_Tunnel
265 struct GNUNET_MESH_Tunnel *next;
270 struct GNUNET_MESH_Tunnel *prev;
273 * Handle to the mesh this tunnel belongs to
275 struct GNUNET_MESH_Handle *mesh;
278 * Local ID of the tunnel
280 MESH_TunnelNumber tid;
288 * Other end of the tunnel.
293 * Any data the caller wants to put in here
298 * Size of packet queued in this tunnel
300 unsigned int packet_size;
303 * Is the tunnel allowed to buffer?
308 * Maximum allowed PID to send (last ACK recevied).
310 uint32_t last_ack_recv;
313 * Last PID received from the service.
315 uint32_t last_pid_recv;
318 * Last packet ID sent to the service.
320 uint32_t last_pid_sent;
323 * Last ACK value sent to the service: how much are we willing to accept?
325 uint32_t last_ack_sent;
329 /******************************************************************************/
330 /*********************** DECLARATIONS *************************/
331 /******************************************************************************/
334 * Function called to send a message to the service.
335 * "buf" will be NULL and "size" zero if the socket was closed for writing in
338 * @param cls closure, the mesh handle
339 * @param size number of bytes available in buf
340 * @param buf where the callee should write the connect message
341 * @return number of bytes written to buf
344 send_callback (void *cls, size_t size, void *buf);
347 /******************************************************************************/
348 /*********************** AUXILIARY FUNCTIONS *************************/
349 /******************************************************************************/
352 * Check if transmission is a payload packet.
354 * @param th Transmission handle.
356 * @return GNUNET_YES if it is a payload packet,
357 * GNUNET_NO if it is a mesh management packet.
360 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
362 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
367 * Check whether there is any message ready in the queue and find the size.
369 * @param h Mesh handle.
371 * @return The size of the first ready message in the queue,
372 * 0 if there is none.
375 message_ready_size (struct GNUNET_MESH_Handle *h)
377 struct GNUNET_MESH_TransmitHandle *th;
378 struct GNUNET_MESH_Tunnel *t;
380 for (th = h->th_head; NULL != th; th = th->next)
383 if (GNUNET_NO == th_is_payload (th))
385 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
388 if (GNUNET_YES == GMC_is_pid_bigger(t->last_ack_recv, t->last_pid_sent))
390 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok (%u <= %u)\n",
391 t->last_pid_sent, t->last_ack_recv);
400 * Get the tunnel handler for the tunnel specified by id from the given handle
401 * @param h Mesh handle
402 * @param tid ID of the wanted tunnel
403 * @return handle to the required tunnel or NULL if not found
405 static struct GNUNET_MESH_Tunnel *
406 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
408 struct GNUNET_MESH_Tunnel *t;
422 * Create a new tunnel and insert it in the tunnel list of the mesh handle
423 * @param h Mesh handle
424 * @param tid desired tid of the tunnel, 0 to assign one automatically
425 * @return handle to the created tunnel
427 static struct GNUNET_MESH_Tunnel *
428 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
430 struct GNUNET_MESH_Tunnel *t;
432 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
433 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
437 t->tid = h->next_tid;
438 while (NULL != retrieve_tunnel (h, h->next_tid))
441 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
442 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
449 t->last_ack_recv = (uint32_t) -1;
450 t->last_pid_recv = (uint32_t) -1;
451 t->buffering = GNUNET_YES;
457 * Destroy the specified tunnel.
458 * - Destroys all peers, calling the disconnect callback on each if needed
459 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
460 * - Calls cleaner if tunnel was inbound
461 * - Frees all memory used
463 * @param t Pointer to the tunnel.
464 * @param call_cleaner Whether to call the cleaner handler.
466 * @return Handle to the required tunnel or NULL if not found.
469 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
471 struct GNUNET_MESH_Handle *h;
472 struct GNUNET_MESH_TransmitHandle *th;
473 struct GNUNET_MESH_TransmitHandle *next;
475 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
484 /* free all peer's ID */
485 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
486 GNUNET_PEER_change_rc (t->peer, -1);
488 /* signal tunnel destruction */
489 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
490 h->cleaner (h->cls, t, t->ctx);
492 /* check that clients did not leave messages behind in the queue */
493 for (th = h->th_head; NULL != th; th = next)
498 /* Clients should have aborted their requests already.
499 * Management traffic should be ok, as clients can't cancel that */
500 GNUNET_break (GNUNET_NO == th_is_payload(th));
501 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
503 /* clean up request */
504 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
505 GNUNET_SCHEDULER_cancel (th->timeout_task);
509 /* if there are no more pending requests with mesh service, cancel active request */
510 /* Note: this should be unnecessary... */
511 if ((0 == message_ready_size (h)) && (NULL != h->th))
513 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
518 GNUNET_PEER_change_rc (t->peer, -1);
525 * Notify client that the transmission has timed out
528 * @param tc task context
531 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
533 struct GNUNET_MESH_TransmitHandle *th = cls;
534 struct GNUNET_MESH_Handle *mesh;
536 mesh = th->tunnel->mesh;
537 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
538 th->tunnel->packet_size = 0;
539 if (GNUNET_YES == th_is_payload (th))
540 th->notify (th->notify_cls, 0, NULL);
542 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
544 /* nothing ready to transmit, no point in asking for transmission */
545 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
552 * Add a transmit handle to the transmission queue and set the
555 * @param h mesh handle with the queue head and tail
556 * @param th handle to the packet to be transmitted
559 add_to_queue (struct GNUNET_MESH_Handle *h,
560 struct GNUNET_MESH_TransmitHandle *th)
562 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
563 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
566 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
567 (th->timeout), &timeout_transmission, th);
572 * Auxiliary function to send an already constructed packet to the service.
573 * Takes care of creating a new queue element, copying the message and
574 * calling the tmt_rdy function if necessary.
576 * @param h mesh handle
577 * @param msg message to transmit
578 * @param tunnel tunnel this send is related to (NULL if N/A)
581 send_packet (struct GNUNET_MESH_Handle *h,
582 const struct GNUNET_MessageHeader *msg,
583 struct GNUNET_MESH_Tunnel *tunnel);
587 * Send an ack on the tunnel to confirm the processing of a message.
589 * @param h Mesh handle.
590 * @param t Tunnel on which to send the ACK.
593 send_ack (struct GNUNET_MESH_Handle *h, struct GNUNET_MESH_Tunnel *t)
595 struct GNUNET_MESH_LocalAck msg;
598 delta = t->last_ack_sent - t->last_pid_recv;
599 if (delta > ACK_THRESHOLD)
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "Not sending ACK on tunnel %X: ACK: %u, PID: %u, buffer %u\n",
603 t->tid, t->last_ack_sent, t->last_pid_recv, delta);
606 if (GNUNET_YES == t->buffering)
607 t->last_ack_sent = t->last_pid_recv + INITIAL_WINDOW_SIZE;
609 t->last_ack_sent = t->last_pid_recv + 1;
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "Sending ACK on tunnel %X: %u\n",
612 t->tid, t->last_ack_sent);
613 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
614 msg.header.size = htons (sizeof (msg));
615 msg.tunnel_id = htonl (t->tid);
616 msg.max_pid = htonl (t->last_ack_sent);
619 t->mesh->acks_sent++;
622 send_packet (h, &msg.header, t);
629 * Reconnect callback: tries to reconnect again after a failer previous
631 * @param cls closure (mesh handle)
632 * @param tc task context
635 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
639 * Send a connect packet to the service with the applications and types
640 * requested by the user.
642 * @param h The mesh handle.
646 send_connect (struct GNUNET_MESH_Handle *h)
650 size = sizeof (struct GNUNET_MESH_ClientConnect);
651 size += h->n_ports * sizeof (uint32_t);
653 char buf[size] GNUNET_ALIGN;
654 struct GNUNET_MESH_ClientConnect *msg;
658 /* build connection packet */
659 msg = (struct GNUNET_MESH_ClientConnect *) buf;
660 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
661 msg->header.size = htons (size);
662 ports = (uint32_t *) &msg[1];
663 for (i = 0; i < h->n_ports; i++)
665 ports[i] = htonl (h->ports[i]);
666 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
669 LOG (GNUNET_ERROR_TYPE_DEBUG,
670 "Sending %lu bytes long message with %u ports\n",
671 ntohs (msg->header.size), h->n_ports);
672 send_packet (h, &msg->header, NULL);
678 * Reconnect to the service, retransmit all infomation to try to restore the
681 * @param h handle to the mesh
683 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
686 do_reconnect (struct GNUNET_MESH_Handle *h)
688 struct GNUNET_MESH_Tunnel *t;
690 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
691 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
692 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
693 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
694 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
699 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
702 if (NULL != h->client)
704 GNUNET_CLIENT_disconnect (h->client);
708 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
709 if (h->client == NULL)
711 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
714 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
715 GNUNET_TIME_relative_multiply
716 (h->reconnect_time, 2));
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Next retry in %s\n",
719 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
726 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
729 /* Rebuild all tunnels */
730 for (t = h->tunnels_head; NULL != t; t = t->next)
732 struct GNUNET_MESH_TunnelMessage tmsg;
734 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
736 /* Tunnel was created by service (incoming tunnel) */
737 /* TODO: Notify service of missing tunnel, to request
738 * creator to recreate path (find a path to him via DHT?)
742 t->last_ack_sent = (uint32_t) -1;
743 t->last_pid_sent = (uint32_t) -1;
744 t->last_ack_recv = (uint32_t) -1;
745 t->last_pid_recv = (uint32_t) -1;
746 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
747 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
748 tmsg.tunnel_id = htonl (t->tid);
749 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
750 send_packet (h, &tmsg.header, t);
752 if (GNUNET_NO == t->buffering)
753 GNUNET_MESH_tunnel_buffer (t, GNUNET_NO);
759 * Reconnect callback: tries to reconnect again after a failer previous
761 * @param cls closure (mesh handle)
762 * @param tc task context
765 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct GNUNET_MESH_Handle *h = cls;
769 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
770 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
777 * Reconnect to the service, retransmit all infomation to try to restore the
780 * @param h handle to the mesh
782 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
785 reconnect (struct GNUNET_MESH_Handle *h)
787 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
788 h->in_receive = GNUNET_NO;
789 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
790 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
795 /******************************************************************************/
796 /*********************** RECEIVE HANDLERS ****************************/
797 /******************************************************************************/
800 * Process the new tunnel notification and add it to the tunnels in the handle
802 * @param h The mesh handle
803 * @param msg A message with the details of the new incoming tunnel
806 process_tunnel_created (struct GNUNET_MESH_Handle *h,
807 const struct GNUNET_MESH_TunnelNotification *msg)
809 struct GNUNET_MESH_Tunnel *t;
810 MESH_TunnelNumber tid;
812 tid = ntohl (msg->tunnel_id);
813 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
814 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
819 if (NULL != h->new_tunnel)
821 t = create_tunnel (h, tid);
822 t->peer = GNUNET_PEER_intern (&msg->peer);
823 GNUNET_PEER_change_rc (t->peer, 1);
826 t->port = ntohl (msg->port);
827 if (0 != (msg->opt & MESH_TUNNEL_OPT_NOBUFFER))
828 t->buffering = GNUNET_NO;
830 t->buffering = GNUNET_YES;
831 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
832 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
833 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
837 struct GNUNET_MESH_TunnelMessage d_msg;
839 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
841 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
842 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
843 d_msg.tunnel_id = msg->tunnel_id;
845 send_packet (h, &d_msg.header, NULL);
852 * Process the tunnel destroy notification and free associated resources
854 * @param h The mesh handle
855 * @param msg A message with the details of the tunnel being destroyed
858 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
859 const struct GNUNET_MESH_TunnelMessage *msg)
861 struct GNUNET_MESH_Tunnel *t;
862 MESH_TunnelNumber tid;
864 tid = ntohl (msg->tunnel_id);
865 t = retrieve_tunnel (h, tid);
871 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
872 destroy_tunnel (t, GNUNET_YES);
878 * Process the incoming data packets
880 * @param h The mesh handle
881 * @param message A message encapsulating the data
883 * @return GNUNET_YES if everything went fine
884 * GNUNET_NO if client closed connection (h no longer valid)
887 process_incoming_data (struct GNUNET_MESH_Handle *h,
888 const struct GNUNET_MessageHeader *message)
890 const struct GNUNET_MessageHeader *payload;
891 const struct GNUNET_MESH_MessageHandler *handler;
892 const struct GNUNET_PeerIdentity *peer;
893 struct GNUNET_PeerIdentity id;
894 struct GNUNET_MESH_Unicast *ucast;
895 struct GNUNET_MESH_ToOrigin *to_orig;
896 struct GNUNET_MESH_Tunnel *t;
901 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
902 type = ntohs (message->type);
905 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
906 ucast = (struct GNUNET_MESH_Unicast *) message;
908 t = retrieve_tunnel (h, ntohl (ucast->tid));
909 payload = (struct GNUNET_MessageHeader *) &ucast[1];
911 pid = ntohl (ucast->pid);
912 LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%X]\n",
913 GNUNET_i2s (peer), ntohl (ucast->tid));
915 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
916 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
917 t = retrieve_tunnel (h, ntohl (to_orig->tid));
918 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
919 GNUNET_PEER_resolve (t->peer, &id);
921 pid = ntohl (to_orig->pid);
922 LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%X]\n",
923 GNUNET_i2s (peer), ntohl (to_orig->tid));
929 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", pid);
932 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
933 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
937 GMC_is_pid_bigger(pid, t->last_ack_sent))
940 LOG (GNUNET_ERROR_TYPE_WARNING,
941 " unauthorized message! (%u, ACK %u)\n",
942 pid, t->last_ack_sent);
943 // FIXME fc what now? accept? reject?
946 t->last_pid_recv = pid;
947 type = ntohs (payload->type);
949 for (i = 0; i < h->n_handlers; i++)
951 handler = &h->message_handlers[i];
952 if (handler->type == type)
955 handler->callback (h->cls, t, &t->ctx, peer, payload))
957 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
958 GNUNET_MESH_disconnect (h);
963 LOG (GNUNET_ERROR_TYPE_DEBUG,
964 "callback completed successfully\n");
973 * Process a local ACK message, enabling the client to send
974 * more data to the service.
976 * @param h Mesh handle.
977 * @param message Message itself.
980 process_ack (struct GNUNET_MESH_Handle *h,
981 const struct GNUNET_MessageHeader *message)
983 struct GNUNET_MESH_LocalAck *msg;
984 struct GNUNET_MESH_Tunnel *t;
987 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
989 msg = (struct GNUNET_MESH_LocalAck *) message;
991 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
995 LOG (GNUNET_ERROR_TYPE_WARNING,
996 "ACK on unknown tunnel %X\n",
997 ntohl (msg->tunnel_id));
1000 ack = ntohl (msg->max_pid);
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X, ack %u!\n", t->tid, ack);
1002 if (GNUNET_YES == GMC_is_pid_bigger(ack, t->last_ack_recv))
1003 t->last_ack_recv = ack;
1006 if (NULL == h->th && 0 < t->packet_size)
1008 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n", t->tid, ack);
1010 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
1011 GNUNET_TIME_UNIT_FOREVER_REL,
1012 GNUNET_YES, &send_callback, h);
1018 * Process a local reply about info on all tunnels, pass info to the user.
1020 * @param h Mesh handle.
1021 * @param message Message itself.
1024 process_get_tunnels (struct GNUNET_MESH_Handle *h,
1025 const struct GNUNET_MessageHeader *message)
1027 struct GNUNET_MESH_LocalMonitor *msg;
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
1031 if (NULL == h->tunnels_cb)
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1037 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1038 if (ntohs (message->size) !=
1039 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1040 sizeof (struct GNUNET_PeerIdentity)))
1042 GNUNET_break_op (0);
1043 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1044 "Get tunnels message: size %hu - expected %u\n",
1045 ntohs (message->size),
1046 sizeof (struct GNUNET_MESH_LocalMonitor));
1049 h->tunnels_cb (h->tunnels_cls,
1050 ntohl (msg->tunnel_id),
1058 * Process a local monitor_tunnel reply, pass info to the user.
1060 * @param h Mesh handle.
1061 * @param message Message itself.
1064 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1065 const struct GNUNET_MessageHeader *message)
1067 struct GNUNET_MESH_LocalMonitor *msg;
1070 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1072 if (NULL == h->tunnel_cb)
1074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1078 /* Verify message sanity */
1079 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1080 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1081 if (ntohs (message->size) != esize)
1083 GNUNET_break_op (0);
1084 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1085 "Show tunnel message: size %hu - expected %u\n",
1086 ntohs (message->size),
1089 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1090 h->tunnel_cb = NULL;
1091 h->tunnel_cls = NULL;
1096 h->tunnel_cb (h->tunnel_cls,
1103 * Function to process all messages received from the service
1105 * @param cls closure
1106 * @param msg message received, NULL on timeout or fatal error
1109 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1111 struct GNUNET_MESH_Handle *h = cls;
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "Mesh service disconnected, reconnecting\n", h);
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1121 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1123 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1124 switch (ntohs (msg->type))
1126 /* Notify of a new incoming tunnel */
1127 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1128 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1130 /* Notify of a tunnel disconnection */
1131 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1132 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1134 /* Notify of a new data packet in the tunnel */
1135 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1136 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1137 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1138 if (GNUNET_NO == process_incoming_data (h, msg))
1141 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1142 process_ack (h, msg);
1144 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1145 process_get_tunnels (h, msg);
1147 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1148 process_show_tunnel (h, msg);
1151 /* We shouldn't get any other packages, log and ignore */
1152 LOG (GNUNET_ERROR_TYPE_WARNING,
1153 "unsolicited message form service (type %s)\n",
1154 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1156 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1157 if (GNUNET_YES == h->in_receive)
1159 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1160 GNUNET_TIME_UNIT_FOREVER_REL);
1164 LOG (GNUNET_ERROR_TYPE_DEBUG,
1165 "in receive off, not calling CLIENT_receive\n");
1170 /******************************************************************************/
1171 /************************ SEND FUNCTIONS ****************************/
1172 /******************************************************************************/
1175 * Function called to send a message to the service.
1176 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1179 * @param cls closure, the mesh handle
1180 * @param size number of bytes available in buf
1181 * @param buf where the callee should write the connect message
1182 * @return number of bytes written to buf
1185 send_callback (void *cls, size_t size, void *buf)
1187 struct GNUNET_MESH_Handle *h = cls;
1188 struct GNUNET_MESH_TransmitHandle *th;
1189 struct GNUNET_MESH_TransmitHandle *next;
1190 struct GNUNET_MESH_Tunnel *t;
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1197 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1198 if ((0 == size) || (NULL == buf))
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1207 nsize = message_ready_size (h);
1208 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1211 if (GNUNET_YES == th_is_payload (th))
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1214 if (GNUNET_NO == GMC_is_pid_bigger(t->last_ack_recv, t->last_pid_sent))
1216 /* This tunnel is not ready to transmit yet, try next message */
1221 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1223 /* traffic to origin */
1224 struct GNUNET_MESH_ToOrigin to;
1225 struct GNUNET_MessageHeader *mh;
1227 GNUNET_assert (size >= th->size);
1228 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
1229 psize = th->notify (th->notify_cls, size - sizeof (to), mh);
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, "# to origin, type %s\n",
1231 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1234 psize += sizeof (to);
1235 GNUNET_assert (size >= psize);
1236 to.header.size = htons (psize);
1237 to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1238 to.tid = htonl (t->tid);
1239 to.pid = htonl (t->last_pid_sent + 1);
1241 memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1242 memcpy (cbuf, &to, sizeof (to));
1248 struct GNUNET_MESH_Unicast uc;
1249 struct GNUNET_MessageHeader *mh;
1251 GNUNET_assert (size >= th->size);
1252 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
1253 psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
1254 LOG (GNUNET_ERROR_TYPE_DEBUG, "# unicast, type %s\n",
1255 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1258 psize += sizeof (uc);
1259 GNUNET_assert (size >= psize);
1260 uc.header.size = htons (psize);
1261 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1262 uc.tid = htonl (t->tid);
1263 uc.pid = htonl (t->last_pid_sent + 1);
1265 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1266 memcpy (cbuf, &uc, sizeof (uc));
1273 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1275 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh traffic, type %s\n",
1276 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1277 memcpy (cbuf, &th[1], th->size);
1280 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1281 GNUNET_SCHEDULER_cancel (th->timeout_task);
1282 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1285 nsize = message_ready_size (h);
1290 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1292 size = message_ready_size (h);
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1297 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1298 GNUNET_TIME_UNIT_FOREVER_REL,
1299 GNUNET_YES, &send_callback, h);
1303 if (NULL != h->th_head)
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1308 if (GNUNET_NO == h->in_receive)
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1311 h->in_receive = GNUNET_YES;
1312 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1313 GNUNET_TIME_UNIT_FOREVER_REL);
1315 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1321 * Auxiliary function to send an already constructed packet to the service.
1322 * Takes care of creating a new queue element, copying the message and
1323 * calling the tmt_rdy function if necessary.
1325 * @param h mesh handle
1326 * @param msg message to transmit
1327 * @param tunnel tunnel this send is related to (NULL if N/A)
1330 send_packet (struct GNUNET_MESH_Handle *h,
1331 const struct GNUNET_MessageHeader *msg,
1332 struct GNUNET_MESH_Tunnel *tunnel)
1334 struct GNUNET_MESH_TransmitHandle *th;
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1338 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1339 msize = ntohs (msg->size);
1340 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1341 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1343 th->tunnel = tunnel;
1344 memcpy (&th[1], msg, msize);
1345 add_to_queue (h, th);
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1349 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1351 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1352 GNUNET_TIME_UNIT_FOREVER_REL,
1353 GNUNET_YES, &send_callback, h);
1357 /******************************************************************************/
1358 /********************** API CALL DEFINITIONS *************************/
1359 /******************************************************************************/
1361 struct GNUNET_MESH_Handle *
1362 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1363 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1364 GNUNET_MESH_TunnelEndHandler cleaner,
1365 const struct GNUNET_MESH_MessageHandler *handlers,
1366 const uint32_t *ports)
1368 struct GNUNET_MESH_Handle *h;
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1371 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1374 h->new_tunnel = new_tunnel;
1375 h->cleaner = cleaner;
1376 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1377 if (h->client == NULL)
1384 h->message_handlers = handlers;
1386 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1387 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1388 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1390 /* count handlers */
1391 for (h->n_handlers = 0;
1392 handlers && handlers[h->n_handlers].type;
1394 for (h->n_ports = 0;
1395 ports && ports[h->n_ports];
1398 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1404 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
1405 * disconnect callbacks will be called on any still connected peers, notifying
1406 * about their disconnection. The registered inbound tunnel cleaner will be
1407 * called should any inbound tunnels still exist.
1409 * @param handle connection to mesh to disconnect
1412 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1414 struct GNUNET_MESH_Tunnel *t;
1415 struct GNUNET_MESH_Tunnel *aux;
1416 struct GNUNET_MESH_TransmitHandle *th;
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1421 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1422 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1425 t = handle->tunnels_head;
1429 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1434 destroy_tunnel (t, GNUNET_YES);
1437 while ( (th = handle->th_head) != NULL)
1439 struct GNUNET_MessageHeader *msg;
1441 /* Make sure it is an allowed packet (everything else should have been
1442 * already canceled).
1444 GNUNET_break (GNUNET_NO == th_is_payload (th));
1445 msg = (struct GNUNET_MessageHeader *) &th[1];
1446 switch (ntohs(msg->type))
1448 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1449 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1450 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1451 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1455 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1459 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1463 if (NULL != handle->th)
1465 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1468 if (NULL != handle->client)
1470 GNUNET_CLIENT_disconnect (handle->client);
1471 handle->client = NULL;
1473 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1475 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1476 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1478 GNUNET_free (handle);
1482 struct GNUNET_MESH_Tunnel *
1483 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1485 const struct GNUNET_PeerIdentity *peer,
1488 struct GNUNET_MESH_Tunnel *t;
1489 struct GNUNET_MESH_TunnelMessage msg;
1491 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1492 t = create_tunnel (h, 0);
1493 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1495 t->ctx = tunnel_ctx;
1496 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1497 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1498 msg.tunnel_id = htonl (t->tid);
1499 msg.port = htonl (port);
1501 send_packet (h, &msg.header, t);
1507 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
1510 * @param tunnel tunnel handle
1513 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1515 struct GNUNET_MESH_Handle *h;
1516 struct GNUNET_MESH_TunnelMessage msg;
1517 struct GNUNET_MESH_TransmitHandle *th;
1519 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1522 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1523 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1524 msg.tunnel_id = htonl (tunnel->tid);
1528 struct GNUNET_MESH_TransmitHandle *aux;
1529 if (th->tunnel == tunnel)
1532 /* FIXME call the handler? */
1533 if (GNUNET_YES == th_is_payload (th))
1534 th->notify (th->notify_cls, 0, NULL);
1535 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1543 destroy_tunnel (tunnel, GNUNET_NO);
1544 send_packet (h, &msg.header, NULL);
1549 * Turn on/off the buffering status of the tunnel.
1551 * @param tunnel Tunnel affected.
1552 * @param buffer GNUNET_YES to turn buffering on (default),
1553 * GNUNET_NO otherwise.
1556 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer)
1558 struct GNUNET_MESH_TunnelMessage msg;
1559 struct GNUNET_MESH_Handle *h;
1562 tunnel->buffering = buffer;
1564 if (GNUNET_YES == buffer)
1565 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER);
1567 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER);
1568 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1569 msg.tunnel_id = htonl (tunnel->tid);
1571 send_packet (h, &msg.header, NULL);
1575 struct GNUNET_MESH_TransmitHandle *
1576 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1577 struct GNUNET_TIME_Relative maxdelay,
1579 GNUNET_CONNECTION_TransmitReadyNotify notify,
1582 struct GNUNET_MESH_TransmitHandle *th;
1585 GNUNET_assert (NULL != tunnel);
1586 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1588 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1592 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1593 GNUNET_assert (NULL != notify);
1594 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1595 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1596 th->tunnel = tunnel;
1597 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1598 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1599 overhead = sizeof (struct GNUNET_MESH_ToOrigin);
1601 overhead = sizeof (struct GNUNET_MESH_Unicast);
1602 tunnel->packet_size = th->size = notify_size + overhead;
1603 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1604 th->notify = notify;
1605 th->notify_cls = notify_cls;
1606 add_to_queue (tunnel->mesh, th);
1607 if (NULL != tunnel->mesh->th)
1609 if (!GMC_is_pid_bigger (tunnel->last_ack_recv, tunnel->last_pid_sent))
1611 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1613 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1614 GNUNET_TIME_UNIT_FOREVER_REL,
1615 GNUNET_YES, &send_callback,
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1623 * Cancel the specified transmission-ready notification.
1625 * @param th handle that was returned by "notify_transmit_ready".
1628 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1630 struct GNUNET_MESH_Handle *mesh;
1632 th->tunnel->packet_size = 0;
1633 mesh = th->tunnel->mesh;
1634 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1635 GNUNET_SCHEDULER_cancel (th->timeout_task);
1636 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1638 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1640 /* queue empty, no point in asking for transmission */
1641 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1648 * Request information about the running mesh peer.
1649 * The callback will be called for every tunnel known to the service,
1650 * listing all active peers that blong to the tunnel.
1652 * If called again on the same handle, it will overwrite the previous
1653 * callback and cls. To retrieve the cls, monitor_cancel must be
1656 * WARNING: unstable API, likely to change in the future!
1658 * @param h Handle to the mesh peer.
1659 * @param callback Function to call with the requested data.
1660 * @param callback_cls Closure for @c callback.
1663 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1664 GNUNET_MESH_TunnelsCB callback,
1667 struct GNUNET_MessageHeader msg;
1669 msg.size = htons (sizeof (msg));
1670 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1671 send_packet (h, &msg, NULL);
1672 h->tunnels_cb = callback;
1673 h->tunnels_cls = callback_cls;
1680 * Cancel a monitor request. The monitor callback will not be called.
1682 * @param h Mesh handle.
1684 * @return Closure given to GNUNET_MESH_monitor, if any.
1687 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1691 cls = h->tunnels_cls;
1692 h->tunnels_cb = NULL;
1693 h->tunnels_cls = NULL;
1699 * Request information about a specific tunnel of the running mesh peer.
1701 * WARNING: unstable API, likely to change in the future!
1702 * FIXME Add destination option.
1704 * @param h Handle to the mesh peer.
1705 * @param initiator ID of the owner of the tunnel.
1706 * @param tunnel_number Tunnel number.
1707 * @param callback Function to call with the requested data.
1708 * @param callback_cls Closure for @c callback.
1711 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1712 struct GNUNET_PeerIdentity *initiator,
1713 unsigned int tunnel_number,
1714 GNUNET_MESH_TunnelCB callback,
1717 struct GNUNET_MESH_LocalMonitor msg;
1719 msg.header.size = htons (sizeof (msg));
1720 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1721 msg.owner = *initiator;
1722 msg.tunnel_id = htonl (tunnel_number);
1724 send_packet (h, &msg.header, NULL);
1725 h->tunnel_cb = callback;
1726 h->tunnel_cls = callback_cls;
1733 * Transition API for tunnel ctx management
1736 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
1742 * Transition API for tunnel ctx management
1745 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)