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 #define DEBUG_ACK GNUNET_YES
46 /******************************************************************************/
47 /************************ DATA STRUCTURES ****************************/
48 /******************************************************************************/
51 * Transmission queue to the service
53 struct GNUNET_MESH_TransmitHandle
59 struct GNUNET_MESH_TransmitHandle *next;
64 struct GNUNET_MESH_TransmitHandle *prev;
67 * Tunnel this message is sent on / for (may be NULL for control messages).
69 struct GNUNET_MESH_Tunnel *tunnel;
72 * Callback to obtain the message to transmit, or NULL if we
73 * got the message in 'data'. Notice that messages built
74 * by 'notify' need to be encapsulated with information about
77 GNUNET_CONNECTION_TransmitReadyNotify notify;
80 * Closure for 'notify'
85 * How long is this message valid. Once the timeout has been
86 * reached, the message must no longer be sent. If this
87 * is a message with a 'notify' callback set, the 'notify'
88 * function should be called with 'buf' NULL and size 0.
90 struct GNUNET_TIME_Absolute timeout;
93 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
95 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
98 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
105 * Opaque handle to the service.
107 struct GNUNET_MESH_Handle
111 * Handle to the server connection, to send messages later
113 struct GNUNET_CLIENT_Connection *client;
116 * Set of handlers used for processing incoming messages in the tunnels
118 const struct GNUNET_MESH_MessageHandler *message_handlers;
121 * Double linked list of the tunnels this client is connected to, head.
123 struct GNUNET_MESH_Tunnel *tunnels_head;
126 * Double linked list of the tunnels this client is connected to, tail.
128 struct GNUNET_MESH_Tunnel *tunnels_tail;
131 * Callback for inbound tunnel creation
133 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
136 * Callback for inbound tunnel disconnection
138 GNUNET_MESH_TunnelEndHandler *cleaner;
141 * Handle to cancel pending transmissions in case of disconnection
143 struct GNUNET_CLIENT_TransmitHandle *th;
146 * Closure for all the handlers given by the client
151 * Messages to send to the service, head.
153 struct GNUNET_MESH_TransmitHandle *th_head;
156 * Messages to send to the service, tail.
158 struct GNUNET_MESH_TransmitHandle *th_tail;
161 * tid of the next tunnel to create (to avoid reusing IDs often)
163 MESH_TunnelNumber next_tid;
166 * Number of handlers in the handlers array.
168 unsigned int n_handlers;
171 * Have we started the task to receive messages from the service
172 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
177 * Configuration given by the client, in case of reconnection
179 const struct GNUNET_CONFIGURATION_Handle *cfg;
182 * Time to the next reconnect in case one reconnect fails
184 struct GNUNET_TIME_Relative reconnect_time;
187 * Task for trying to reconnect.
189 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
194 GNUNET_MESH_TunnelsCB tunnels_cb;
197 * Monitor callback closure.
204 GNUNET_MESH_TunnelCB tunnel_cb;
207 * Tunnel callback closure.
212 unsigned int acks_sent;
213 unsigned int acks_recv;
219 * Description of a peer
221 struct GNUNET_MESH_Peer
224 * ID of the peer in short form
229 * Tunnel this peer belongs to
231 struct GNUNET_MESH_Tunnel *t;
234 * Flag indicating whether service has informed about its connection
242 * Opaque handle to a tunnel.
244 struct GNUNET_MESH_Tunnel
250 struct GNUNET_MESH_Tunnel *next;
255 struct GNUNET_MESH_Tunnel *prev;
258 * Handle to the mesh this tunnel belongs to
260 struct GNUNET_MESH_Handle *mesh;
263 * Local ID of the tunnel
265 MESH_TunnelNumber tid;
268 * Other end of the tunnel.
273 * Any data the caller wants to put in here
278 * Size of packet queued in this tunnel
280 unsigned int packet_size;
283 * Is the tunnel allowed to buffer?
288 * Next packet ID to send.
290 uint32_t next_send_pid;
293 * Maximum allowed PID to send (ACK recevied).
295 uint32_t max_send_pid;
298 * Last pid received from the service.
300 uint32_t last_recv_pid;
303 * Which ACK value have we last sent to the service?
305 uint32_t max_recv_pid;
309 /******************************************************************************/
310 /*********************** DECLARATIONS *************************/
311 /******************************************************************************/
314 * Function called to send a message to the service.
315 * "buf" will be NULL and "size" zero if the socket was closed for writing in
318 * @param cls closure, the mesh handle
319 * @param size number of bytes available in buf
320 * @param buf where the callee should write the connect message
321 * @return number of bytes written to buf
324 send_callback (void *cls, size_t size, void *buf);
327 /******************************************************************************/
328 /*********************** AUXILIARY FUNCTIONS *************************/
329 /******************************************************************************/
332 * Check if transmission is a payload packet.
334 * @param th Transmission handle.
336 * @return GNUNET_YES if it is a payload packet,
337 * GNUNET_NO if it is a mesh management packet.
340 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
342 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
347 * Check whether there is any message ready in the queue and find the size.
349 * @param h Mesh handle.
351 * @return The size of the first ready message in the queue,
352 * 0 if there is none.
355 message_ready_size (struct GNUNET_MESH_Handle *h)
357 struct GNUNET_MESH_TransmitHandle *th;
358 struct GNUNET_MESH_Tunnel *t;
360 for (th = h->th_head; NULL != th; th = th->next)
363 if (GNUNET_NO == th_is_payload (th))
365 LOG (GNUNET_ERROR_TYPE_DEBUG, " message internal\n");
368 if (GNUNET_NO == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
370 LOG (GNUNET_ERROR_TYPE_DEBUG, " message payload ok (%u <= %u)\n",
371 t->next_send_pid, t->max_send_pid);
380 * Get the tunnel handler for the tunnel specified by id from the given handle
381 * @param h Mesh handle
382 * @param tid ID of the wanted tunnel
383 * @return handle to the required tunnel or NULL if not found
385 static struct GNUNET_MESH_Tunnel *
386 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
388 struct GNUNET_MESH_Tunnel *t;
402 * Create a new tunnel and insert it in the tunnel list of the mesh handle
403 * @param h Mesh handle
404 * @param tid desired tid of the tunnel, 0 to assign one automatically
405 * @return handle to the created tunnel
407 static struct GNUNET_MESH_Tunnel *
408 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
410 struct GNUNET_MESH_Tunnel *t;
412 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
413 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
417 t->tid = h->next_tid;
418 while (NULL != retrieve_tunnel (h, h->next_tid))
421 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
422 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
429 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
430 t->last_recv_pid = (uint32_t) -1;
431 t->buffering = GNUNET_YES;
437 * Destroy the specified tunnel.
438 * - Destroys all peers, calling the disconnect callback on each if needed
439 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
440 * - Calls cleaner if tunnel was inbound
441 * - Frees all memory used
443 * @param t Pointer to the tunnel.
444 * @param call_cleaner Whether to call the cleaner handler.
446 * @return Handle to the required tunnel or NULL if not found.
449 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
451 struct GNUNET_MESH_Handle *h;
452 struct GNUNET_MESH_TransmitHandle *th;
453 struct GNUNET_MESH_TransmitHandle *next;
455 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
464 /* free all peer's ID */
465 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
466 GNUNET_PEER_change_rc (t->peer, -1);
468 /* signal tunnel destruction */
469 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
470 h->cleaner (h->cls, t, t->ctx);
472 /* check that clients did not leave messages behind in the queue */
473 for (th = h->th_head; NULL != th; th = next)
478 /* Clients should have aborted their requests already.
479 * Management traffic should be ok, as clients can't cancel that */
480 GNUNET_break (GNUNET_NO == th_is_payload(th));
481 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
483 /* clean up request */
484 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
485 GNUNET_SCHEDULER_cancel (th->timeout_task);
489 /* if there are no more pending requests with mesh service, cancel active request */
490 /* Note: this should be unnecessary... */
491 if ((0 == message_ready_size (h)) && (NULL != h->th))
493 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
498 GNUNET_PEER_change_rc (t->peer, -1);
505 * Notify client that the transmission has timed out
508 * @param tc task context
511 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
513 struct GNUNET_MESH_TransmitHandle *th = cls;
514 struct GNUNET_MESH_Handle *mesh;
516 mesh = th->tunnel->mesh;
517 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
518 th->tunnel->packet_size = 0;
519 if (GNUNET_YES == th_is_payload (th))
520 th->notify (th->notify_cls, 0, NULL);
522 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
524 /* nothing ready to transmit, no point in asking for transmission */
525 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
532 * Add a transmit handle to the transmission queue and set the
535 * @param h mesh handle with the queue head and tail
536 * @param th handle to the packet to be transmitted
539 add_to_queue (struct GNUNET_MESH_Handle *h,
540 struct GNUNET_MESH_TransmitHandle *th)
542 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
543 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
546 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
547 (th->timeout), &timeout_transmission, th);
552 * Auxiliary function to send an already constructed packet to the service.
553 * Takes care of creating a new queue element, copying the message and
554 * calling the tmt_rdy function if necessary.
556 * @param h mesh handle
557 * @param msg message to transmit
558 * @param tunnel tunnel this send is related to (NULL if N/A)
561 send_packet (struct GNUNET_MESH_Handle *h,
562 const struct GNUNET_MessageHeader *msg,
563 struct GNUNET_MESH_Tunnel *tunnel);
567 * Send an ack on the tunnel to confirm the processing of a message.
569 * @param h Mesh handle.
570 * @param t Tunnel on which to send the ACK.
573 send_ack (struct GNUNET_MESH_Handle *h, struct GNUNET_MESH_Tunnel *t)
575 struct GNUNET_MESH_LocalAck msg;
578 delta = t->max_recv_pid - t->last_recv_pid;
579 if (delta > ACK_THRESHOLD)
581 LOG (GNUNET_ERROR_TYPE_DEBUG,
582 "Not sending ACK on tunnel %X: ACK: %u, PID: %u, buffer %u\n",
583 t->tid, t->max_recv_pid, t->last_recv_pid, delta);
586 if (GNUNET_YES == t->buffering)
587 t->max_recv_pid = t->last_recv_pid + INITIAL_WINDOW_SIZE;
589 t->max_recv_pid = t->last_recv_pid + 1;
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Sending ACK on tunnel %X: %u\n",
592 t->tid, t->max_recv_pid);
593 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
594 msg.header.size = htons (sizeof (msg));
595 msg.tunnel_id = htonl (t->tid);
596 msg.max_pid = htonl (t->max_recv_pid);
599 t->mesh->acks_sent++;
602 send_packet (h, &msg.header, t);
609 * Reconnect callback: tries to reconnect again after a failer previous
611 * @param cls closure (mesh handle)
612 * @param tc task context
615 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
619 * Send a connect packet to the service with the applications and types
620 * requested by the user.
622 * @param h The mesh handle.
626 send_connect (struct GNUNET_MESH_Handle *h)
630 size = sizeof (struct GNUNET_MESH_ClientConnect);
631 size += h->n_handlers * sizeof (uint16_t);
633 char buf[size] GNUNET_ALIGN;
634 struct GNUNET_MESH_ClientConnect *msg;
638 /* build connection packet */
639 msg = (struct GNUNET_MESH_ClientConnect *) buf;
640 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
641 msg->header.size = htons (size);
642 types = (uint16_t *) & msg[1];
643 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
645 types[ntypes] = htons (h->message_handlers[ntypes].type);
646 LOG (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
647 h->message_handlers[ntypes].type);
649 msg->types = htons (ntypes);
650 LOG (GNUNET_ERROR_TYPE_DEBUG,
651 "Sending %lu bytes long message %d types\n",
652 ntohs (msg->header.size), ntypes);
653 send_packet (h, &msg->header, NULL);
659 * Reconnect to the service, retransmit all infomation to try to restore the
662 * @param h handle to the mesh
664 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
667 do_reconnect (struct GNUNET_MESH_Handle *h)
669 struct GNUNET_MESH_Tunnel *t;
671 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
672 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
674 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
675 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
680 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
683 if (NULL != h->client)
685 GNUNET_CLIENT_disconnect (h->client);
689 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
690 if (h->client == NULL)
692 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
695 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
696 GNUNET_TIME_relative_multiply
697 (h->reconnect_time, 2));
698 LOG (GNUNET_ERROR_TYPE_DEBUG,
699 "Next retry in %s\n",
700 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
707 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
710 /* Rebuild all tunnels */
711 for (t = h->tunnels_head; NULL != t; t = t->next)
713 struct GNUNET_MESH_TunnelMessage tmsg;
715 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
717 /* Tunnel was created by service (incoming tunnel) */
718 /* TODO: Notify service of missing tunnel, to request
719 * creator to recreate path (find a path to him via DHT?)
723 t->next_send_pid = 0;
724 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
725 t->last_recv_pid = (uint32_t) -1;
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 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
730 send_packet (h, &tmsg.header, t);
732 if (GNUNET_NO == t->buffering)
733 GNUNET_MESH_tunnel_buffer (t, GNUNET_NO);
739 * Reconnect callback: tries to reconnect again after a failer previous
741 * @param cls closure (mesh handle)
742 * @param tc task context
745 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
747 struct GNUNET_MESH_Handle *h = cls;
749 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
750 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
757 * Reconnect to the service, retransmit all infomation to try to restore the
760 * @param h handle to the mesh
762 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
765 reconnect (struct GNUNET_MESH_Handle *h)
767 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
768 h->in_receive = GNUNET_NO;
769 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
770 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
775 /******************************************************************************/
776 /*********************** RECEIVE HANDLERS ****************************/
777 /******************************************************************************/
780 * Process the new tunnel notification and add it to the tunnels in the handle
782 * @param h The mesh handle
783 * @param msg A message with the details of the new incoming tunnel
786 process_tunnel_created (struct GNUNET_MESH_Handle *h,
787 const struct GNUNET_MESH_TunnelNotification *msg)
789 struct GNUNET_MESH_Tunnel *t;
790 MESH_TunnelNumber tid;
792 tid = ntohl (msg->tunnel_id);
793 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
794 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
799 if (NULL != h->new_tunnel)
801 t = create_tunnel (h, tid);
802 t->peer = GNUNET_PEER_intern (&msg->peer);
803 GNUNET_PEER_change_rc (t->peer, 1);
806 if ((msg->opt & MESH_TUNNEL_OPT_NOBUFFER) != 0)
807 t->buffering = GNUNET_NO;
809 t->buffering = GNUNET_YES;
810 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
811 t->ctx = h->new_tunnel (h->cls, t, &msg->peer);
812 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
816 struct GNUNET_MESH_TunnelMessage d_msg;
818 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
820 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
821 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
822 d_msg.tunnel_id = msg->tunnel_id;
824 send_packet (h, &d_msg.header, NULL);
831 * Process the tunnel destroy notification and free associated resources
833 * @param h The mesh handle
834 * @param msg A message with the details of the tunnel being destroyed
837 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
838 const struct GNUNET_MESH_TunnelMessage *msg)
840 struct GNUNET_MESH_Tunnel *t;
841 MESH_TunnelNumber tid;
843 tid = ntohl (msg->tunnel_id);
844 t = retrieve_tunnel (h, tid);
850 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
851 destroy_tunnel (t, GNUNET_YES);
857 * Process the incoming data packets
859 * @param h The mesh handle
860 * @param message A message encapsulating the data
862 * @return GNUNET_YES if everything went fine
863 * GNUNET_NO if client closed connection (h no longer valid)
866 process_incoming_data (struct GNUNET_MESH_Handle *h,
867 const struct GNUNET_MessageHeader *message)
869 const struct GNUNET_MessageHeader *payload;
870 const struct GNUNET_MESH_MessageHandler *handler;
871 const struct GNUNET_PeerIdentity *peer;
872 struct GNUNET_MESH_Unicast *ucast;
873 struct GNUNET_MESH_Multicast *mcast;
874 struct GNUNET_MESH_ToOrigin *to_orig;
875 struct GNUNET_MESH_Tunnel *t;
880 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
881 type = ntohs (message->type);
884 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
885 ucast = (struct GNUNET_MESH_Unicast *) message;
887 t = retrieve_tunnel (h, ntohl (ucast->tid));
888 payload = (struct GNUNET_MessageHeader *) &ucast[1];
890 pid = ntohl (ucast->pid);
891 LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%X]\n",
892 GNUNET_i2s (peer), ntohl (ucast->tid));
894 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
895 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
896 t = retrieve_tunnel (h, ntohl (to_orig->tid));
897 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
898 GNUNET_PEER_resolve (t->peer, &peer);
899 pid = ntohl (to_orig->pid);
900 LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%X]\n",
901 GNUNET_i2s (peer), ntohl (to_orig->tid));
907 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", pid);
910 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
911 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
915 GMC_is_pid_bigger(pid, t->max_recv_pid))
918 LOG (GNUNET_ERROR_TYPE_WARNING,
919 " unauthorized message! (%u, max %u)\n",
920 pid, t->max_recv_pid);
921 // FIXME fc what now? accept? reject?
924 t->last_recv_pid = pid;
925 type = ntohs (payload->type);
927 for (i = 0; i < h->n_handlers; i++)
929 handler = &h->message_handlers[i];
930 if (handler->type == type)
933 handler->callback (h->cls, t, &t->ctx, peer, payload))
935 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
936 GNUNET_MESH_disconnect (h);
941 LOG (GNUNET_ERROR_TYPE_DEBUG,
942 "callback completed successfully\n");
951 * Process a local ACK message, enabling the client to send
952 * more data to the service.
954 * @param h Mesh handle.
955 * @param message Message itself.
958 process_ack (struct GNUNET_MESH_Handle *h,
959 const struct GNUNET_MessageHeader *message)
961 struct GNUNET_MESH_LocalAck *msg;
962 struct GNUNET_MESH_Tunnel *t;
965 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
967 msg = (struct GNUNET_MESH_LocalAck *) message;
969 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
973 LOG (GNUNET_ERROR_TYPE_WARNING,
974 "ACK on unknown tunnel %X\n",
975 ntohl (msg->tunnel_id));
978 ack = ntohl (msg->max_pid);
979 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X, ack %u!\n", t->tid, ack);
980 if (GNUNET_YES == GMC_is_pid_bigger(ack, t->max_send_pid))
981 t->max_send_pid = ack;
984 if (NULL == h->th && 0 < t->packet_size)
986 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n", t->tid, ack);
988 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
989 GNUNET_TIME_UNIT_FOREVER_REL,
990 GNUNET_YES, &send_callback, h);
996 * Process a local reply about info on all tunnels, pass info to the user.
998 * @param h Mesh handle.
999 * @param message Message itself.
1002 process_get_tunnels (struct GNUNET_MESH_Handle *h,
1003 const struct GNUNET_MessageHeader *message)
1005 struct GNUNET_MESH_LocalMonitor *msg;
1007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
1009 if (NULL == h->tunnels_cb)
1011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1015 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1016 if (ntohs (message->size) !=
1017 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1018 sizeof (struct GNUNET_PeerIdentity)))
1020 GNUNET_break_op (0);
1021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1022 "Get tunnels message: size %hu - expected %u\n",
1023 ntohs (message->size),
1024 sizeof (struct GNUNET_MESH_LocalMonitor));
1027 h->tunnels_cb (h->tunnels_cls,
1028 ntohl (msg->tunnel_id),
1036 * Process a local monitor_tunnel reply, pass info to the user.
1038 * @param h Mesh handle.
1039 * @param message Message itself.
1042 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1043 const struct GNUNET_MessageHeader *message)
1045 struct GNUNET_MESH_LocalMonitor *msg;
1048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1050 if (NULL == h->tunnel_cb)
1052 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1056 /* Verify message sanity */
1057 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1058 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1059 if (ntohs (message->size) != esize)
1061 GNUNET_break_op (0);
1062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1063 "Show tunnel message: size %hu - expected %u\n",
1064 ntohs (message->size),
1067 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1068 h->tunnel_cb = NULL;
1069 h->tunnel_cls = NULL;
1074 h->tunnel_cb (h->tunnel_cls,
1081 * Function to process all messages received from the service
1083 * @param cls closure
1084 * @param msg message received, NULL on timeout or fatal error
1087 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1089 struct GNUNET_MESH_Handle *h = cls;
1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1094 "Mesh service disconnected, reconnecting\n", h);
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1099 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1101 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1102 switch (ntohs (msg->type))
1104 /* Notify of a new incoming tunnel */
1105 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1106 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1108 /* Notify of a tunnel disconnection */
1109 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1110 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1112 /* Notify of a new data packet in the tunnel */
1113 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1114 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1115 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1116 if (GNUNET_NO == process_incoming_data (h, msg))
1119 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1120 process_ack (h, msg);
1122 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1123 process_get_tunnels (h, msg);
1125 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1126 process_show_tunnel (h, msg);
1129 /* We shouldn't get any other packages, log and ignore */
1130 LOG (GNUNET_ERROR_TYPE_WARNING,
1131 "unsolicited message form service (type %s)\n",
1132 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1135 if (GNUNET_YES == h->in_receive)
1137 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1138 GNUNET_TIME_UNIT_FOREVER_REL);
1142 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "in receive off, not calling CLIENT_receive\n");
1148 /******************************************************************************/
1149 /************************ SEND FUNCTIONS ****************************/
1150 /******************************************************************************/
1153 * Function called to send a message to the service.
1154 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1157 * @param cls closure, the mesh handle
1158 * @param size number of bytes available in buf
1159 * @param buf where the callee should write the connect message
1160 * @return number of bytes written to buf
1163 send_callback (void *cls, size_t size, void *buf)
1165 struct GNUNET_MESH_Handle *h = cls;
1166 struct GNUNET_MESH_TransmitHandle *th;
1167 struct GNUNET_MESH_TransmitHandle *next;
1168 struct GNUNET_MESH_Tunnel *t;
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1175 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
1176 if ((0 == size) || (NULL == buf))
1178 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL send callback on %p\n", h);
1185 nsize = message_ready_size (h);
1186 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1189 if (GNUNET_YES == th_is_payload (th))
1191 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload\n");
1192 if (GNUNET_YES == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
1194 /* This tunnel is not ready to transmit yet, try next message */
1199 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1201 /* traffic to origin */
1202 struct GNUNET_MESH_ToOrigin to;
1203 struct GNUNET_MessageHeader *mh;
1205 GNUNET_assert (size >= th->size);
1206 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
1207 psize = th->notify (th->notify_cls, size - sizeof (to), mh);
1208 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin, type %s\n",
1209 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1212 psize += sizeof (to);
1213 GNUNET_assert (size >= psize);
1214 to.header.size = htons (psize);
1215 to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1216 to.tid = htonl (t->tid);
1217 to.pid = htonl (t->next_send_pid);
1219 memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1220 memcpy (cbuf, &to, sizeof (to));
1226 struct GNUNET_MESH_Unicast uc;
1227 struct GNUNET_MessageHeader *mh;
1229 GNUNET_assert (size >= th->size);
1230 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
1231 psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
1232 LOG (GNUNET_ERROR_TYPE_DEBUG, " unicast, type %s\n",
1233 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1236 psize += sizeof (uc);
1237 GNUNET_assert (size >= psize);
1238 uc.header.size = htons (psize);
1239 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1240 uc.tid = htonl (t->tid);
1241 uc.pid = htonl (t->next_send_pid);
1243 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1250 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1252 LOG (GNUNET_ERROR_TYPE_DEBUG, " mesh traffic, type %s\n",
1253 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1254 memcpy (cbuf, &th[1], th->size);
1257 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1258 GNUNET_SCHEDULER_cancel (th->timeout_task);
1259 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1262 nsize = message_ready_size (h);
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size: %u\n", tsize);
1269 size = message_ready_size (h);
1272 LOG (GNUNET_ERROR_TYPE_DEBUG, " next size: %u\n", size);
1274 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1275 GNUNET_TIME_UNIT_FOREVER_REL,
1276 GNUNET_YES, &send_callback, h);
1280 if (NULL != h->th_head)
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, " can't transmit any more\n");
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, " nothing left to transmit\n");
1285 if (GNUNET_NO == h->in_receive)
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, " start receiving from service\n");
1288 h->in_receive = GNUNET_YES;
1289 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1290 GNUNET_TIME_UNIT_FOREVER_REL);
1292 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
1298 * Auxiliary function to send an already constructed packet to the service.
1299 * Takes care of creating a new queue element, copying the message and
1300 * calling the tmt_rdy function if necessary.
1302 * @param h mesh handle
1303 * @param msg message to transmit
1304 * @param tunnel tunnel this send is related to (NULL if N/A)
1307 send_packet (struct GNUNET_MESH_Handle *h,
1308 const struct GNUNET_MessageHeader *msg,
1309 struct GNUNET_MESH_Tunnel *tunnel)
1311 struct GNUNET_MESH_TransmitHandle *th;
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1315 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1316 msize = ntohs (msg->size);
1317 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1318 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1320 th->tunnel = tunnel;
1321 memcpy (&th[1], msg, msize);
1322 add_to_queue (h, th);
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1328 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1329 GNUNET_TIME_UNIT_FOREVER_REL,
1330 GNUNET_YES, &send_callback, h);
1334 /******************************************************************************/
1335 /********************** API CALL DEFINITIONS *************************/
1336 /******************************************************************************/
1338 struct GNUNET_MESH_Handle *
1339 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1340 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1341 GNUNET_MESH_TunnelEndHandler cleaner,
1342 const struct GNUNET_MESH_MessageHandler *handlers)
1344 struct GNUNET_MESH_Handle *h;
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1347 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1348 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1350 h->new_tunnel = new_tunnel;
1351 h->cleaner = cleaner;
1352 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1353 if (h->client == NULL)
1360 h->message_handlers = handlers;
1361 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1362 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1363 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1365 /* count handlers */
1366 for (h->n_handlers = 0;
1367 handlers && handlers[h->n_handlers].type;
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1376 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
1377 * disconnect callbacks will be called on any still connected peers, notifying
1378 * about their disconnection. The registered inbound tunnel cleaner will be
1379 * called should any inbound tunnels still exist.
1381 * @param handle connection to mesh to disconnect
1384 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1386 struct GNUNET_MESH_Tunnel *t;
1387 struct GNUNET_MESH_Tunnel *aux;
1388 struct GNUNET_MESH_TransmitHandle *th;
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1393 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1394 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1397 t = handle->tunnels_head;
1401 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1406 destroy_tunnel (t, GNUNET_YES);
1409 while ( (th = handle->th_head) != NULL)
1411 struct GNUNET_MessageHeader *msg;
1413 /* Make sure it is an allowed packet (everything else should have been
1414 * already canceled).
1416 GNUNET_break (GNUNET_NO == th_is_payload (th));
1417 msg = (struct GNUNET_MessageHeader *) &th[1];
1418 switch (ntohs(msg->type))
1420 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1421 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1422 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1423 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1427 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1431 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1435 if (NULL != handle->th)
1437 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1440 if (NULL != handle->client)
1442 GNUNET_CLIENT_disconnect (handle->client);
1443 handle->client = NULL;
1445 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1447 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1448 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1450 GNUNET_free (handle);
1454 struct GNUNET_MESH_Tunnel *
1455 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1457 const struct GNUNET_PeerIdentity *peer)
1459 struct GNUNET_MESH_Tunnel *t;
1460 struct GNUNET_MESH_TunnelMessage msg;
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1463 t = create_tunnel (h, 0);
1464 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1466 t->ctx = tunnel_ctx;
1467 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1468 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1469 msg.tunnel_id = htonl (t->tid);
1470 send_packet (h, &msg.header, t);
1476 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
1479 * @param tunnel tunnel handle
1482 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1484 struct GNUNET_MESH_Handle *h;
1485 struct GNUNET_MESH_TunnelMessage msg;
1486 struct GNUNET_MESH_TransmitHandle *th;
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1491 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1492 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1493 msg.tunnel_id = htonl (tunnel->tid);
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_NO);
1513 send_packet (h, &msg.header, NULL);
1518 * Turn on/off the buffering status of the tunnel.
1520 * @param tunnel Tunnel affected.
1521 * @param buffer GNUNET_YES to turn buffering on (default),
1522 * GNUNET_NO otherwise.
1525 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer)
1527 struct GNUNET_MESH_TunnelMessage msg;
1528 struct GNUNET_MESH_Handle *h;
1531 tunnel->buffering = buffer;
1532 tunnel->max_send_pid = tunnel->next_send_pid;
1534 if (GNUNET_YES == buffer)
1535 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER);
1537 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER);
1538 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1539 msg.tunnel_id = htonl (tunnel->tid);
1541 send_packet (h, &msg.header, NULL);
1545 struct GNUNET_MESH_TransmitHandle *
1546 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1547 struct GNUNET_TIME_Relative maxdelay,
1549 GNUNET_CONNECTION_TransmitReadyNotify notify,
1552 struct GNUNET_MESH_TransmitHandle *th;
1555 GNUNET_assert (NULL != tunnel);
1556 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1558 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1562 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1563 GNUNET_assert (NULL != notify);
1564 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1565 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1566 th->tunnel = tunnel;
1567 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1568 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1569 overhead = sizeof (struct GNUNET_MESH_ToOrigin);
1571 overhead = sizeof (struct GNUNET_MESH_Unicast);
1572 tunnel->packet_size = th->size = notify_size + overhead;
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1574 th->notify = notify;
1575 th->notify_cls = notify_cls;
1576 add_to_queue (tunnel->mesh, th);
1577 if (NULL != tunnel->mesh->th)
1579 if (GMC_is_pid_bigger(tunnel->next_send_pid, tunnel->max_send_pid))
1581 LOG (GNUNET_ERROR_TYPE_DEBUG, " call notify tmt rdy\n");
1583 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1584 GNUNET_TIME_UNIT_FOREVER_REL,
1585 GNUNET_YES, &send_callback,
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1593 * Cancel the specified transmission-ready notification.
1595 * @param th handle that was returned by "notify_transmit_ready".
1598 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1600 struct GNUNET_MESH_Handle *mesh;
1602 th->tunnel->packet_size = 0;
1603 mesh = th->tunnel->mesh;
1604 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1605 GNUNET_SCHEDULER_cancel (th->timeout_task);
1606 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1608 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1610 /* queue empty, no point in asking for transmission */
1611 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1618 * Request information about the running mesh peer.
1619 * The callback will be called for every tunnel known to the service,
1620 * listing all active peers that blong to the tunnel.
1622 * If called again on the same handle, it will overwrite the previous
1623 * callback and cls. To retrieve the cls, monitor_cancel must be
1626 * WARNING: unstable API, likely to change in the future!
1628 * @param h Handle to the mesh peer.
1629 * @param callback Function to call with the requested data.
1630 * @param callback_cls Closure for @c callback.
1633 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1634 GNUNET_MESH_TunnelsCB callback,
1637 struct GNUNET_MessageHeader msg;
1639 msg.size = htons (sizeof (msg));
1640 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1641 send_packet (h, &msg, NULL);
1642 h->tunnels_cb = callback;
1643 h->tunnels_cls = callback_cls;
1650 * Cancel a monitor request. The monitor callback will not be called.
1652 * @param h Mesh handle.
1654 * @return Closure given to GNUNET_MESH_monitor, if any.
1657 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1661 cls = h->tunnels_cls;
1662 h->tunnels_cb = NULL;
1663 h->tunnels_cls = NULL;
1669 * Request information about a specific tunnel of the running mesh peer.
1671 * WARNING: unstable API, likely to change in the future!
1672 * FIXME Add destination option.
1674 * @param h Handle to the mesh peer.
1675 * @param initiator ID of the owner of the tunnel.
1676 * @param tunnel_number Tunnel number.
1677 * @param callback Function to call with the requested data.
1678 * @param callback_cls Closure for @c callback.
1681 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1682 struct GNUNET_PeerIdentity *initiator,
1683 unsigned int tunnel_number,
1684 GNUNET_MESH_TunnelCB callback,
1687 struct GNUNET_MESH_LocalMonitor msg;
1689 msg.header.size = htons (sizeof (msg));
1690 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1691 msg.owner = *initiator;
1692 msg.tunnel_id = htonl (tunnel_number);
1694 send_packet (h, &msg.header, NULL);
1695 h->tunnel_cb = callback;
1696 h->tunnel_cls = callback_cls;
1703 * Transition API for tunnel ctx management
1706 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
1712 * Transition API for tunnel ctx management
1715 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)