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 * Number of handlers in the handlers array.
123 unsigned int n_handlers;
133 unsigned int n_ports;
136 * Double linked list of the tunnels this client is connected to, head.
138 struct GNUNET_MESH_Tunnel *tunnels_head;
141 * Double linked list of the tunnels this client is connected to, tail.
143 struct GNUNET_MESH_Tunnel *tunnels_tail;
146 * Callback for inbound tunnel creation
148 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
151 * Callback for inbound tunnel disconnection
153 GNUNET_MESH_TunnelEndHandler *cleaner;
156 * Handle to cancel pending transmissions in case of disconnection
158 struct GNUNET_CLIENT_TransmitHandle *th;
161 * Closure for all the handlers given by the client
166 * Messages to send to the service, head.
168 struct GNUNET_MESH_TransmitHandle *th_head;
171 * Messages to send to the service, tail.
173 struct GNUNET_MESH_TransmitHandle *th_tail;
176 * tid of the next tunnel to create (to avoid reusing IDs often)
178 MESH_TunnelNumber next_tid;
181 * Have we started the task to receive messages from the service
182 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
187 * Configuration given by the client, in case of reconnection
189 const struct GNUNET_CONFIGURATION_Handle *cfg;
192 * Time to the next reconnect in case one reconnect fails
194 struct GNUNET_TIME_Relative reconnect_time;
197 * Task for trying to reconnect.
199 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
204 GNUNET_MESH_TunnelsCB tunnels_cb;
207 * Monitor callback closure.
214 GNUNET_MESH_TunnelCB tunnel_cb;
217 * Tunnel callback closure.
222 unsigned int acks_sent;
223 unsigned int acks_recv;
229 * Description of a peer
231 struct GNUNET_MESH_Peer
234 * ID of the peer in short form
239 * Tunnel this peer belongs to
241 struct GNUNET_MESH_Tunnel *t;
244 * Flag indicating whether service has informed about its connection
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 * Next packet ID to send.
305 uint32_t next_send_pid;
308 * Maximum allowed PID to send (ACK recevied).
310 uint32_t max_send_pid;
313 * Last pid received from the service.
315 uint32_t last_recv_pid;
318 * Which ACK value have we last sent to the service?
320 uint32_t max_recv_pid;
324 /******************************************************************************/
325 /*********************** DECLARATIONS *************************/
326 /******************************************************************************/
329 * Function called to send a message to the service.
330 * "buf" will be NULL and "size" zero if the socket was closed for writing in
333 * @param cls closure, the mesh handle
334 * @param size number of bytes available in buf
335 * @param buf where the callee should write the connect message
336 * @return number of bytes written to buf
339 send_callback (void *cls, size_t size, void *buf);
342 /******************************************************************************/
343 /*********************** AUXILIARY FUNCTIONS *************************/
344 /******************************************************************************/
347 * Check if transmission is a payload packet.
349 * @param th Transmission handle.
351 * @return GNUNET_YES if it is a payload packet,
352 * GNUNET_NO if it is a mesh management packet.
355 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
357 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
362 * Check whether there is any message ready in the queue and find the size.
364 * @param h Mesh handle.
366 * @return The size of the first ready message in the queue,
367 * 0 if there is none.
370 message_ready_size (struct GNUNET_MESH_Handle *h)
372 struct GNUNET_MESH_TransmitHandle *th;
373 struct GNUNET_MESH_Tunnel *t;
375 for (th = h->th_head; NULL != th; th = th->next)
378 if (GNUNET_NO == th_is_payload (th))
380 LOG (GNUNET_ERROR_TYPE_DEBUG, " message internal\n");
383 if (GNUNET_NO == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
385 LOG (GNUNET_ERROR_TYPE_DEBUG, " message payload ok (%u <= %u)\n",
386 t->next_send_pid, t->max_send_pid);
395 * Get the tunnel handler for the tunnel specified by id from the given handle
396 * @param h Mesh handle
397 * @param tid ID of the wanted tunnel
398 * @return handle to the required tunnel or NULL if not found
400 static struct GNUNET_MESH_Tunnel *
401 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
403 struct GNUNET_MESH_Tunnel *t;
417 * Create a new tunnel and insert it in the tunnel list of the mesh handle
418 * @param h Mesh handle
419 * @param tid desired tid of the tunnel, 0 to assign one automatically
420 * @return handle to the created tunnel
422 static struct GNUNET_MESH_Tunnel *
423 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
425 struct GNUNET_MESH_Tunnel *t;
427 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
428 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
432 t->tid = h->next_tid;
433 while (NULL != retrieve_tunnel (h, h->next_tid))
436 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
437 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
444 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
445 t->last_recv_pid = (uint32_t) -1;
446 t->buffering = GNUNET_YES;
452 * Destroy the specified tunnel.
453 * - Destroys all peers, calling the disconnect callback on each if needed
454 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
455 * - Calls cleaner if tunnel was inbound
456 * - Frees all memory used
458 * @param t Pointer to the tunnel.
459 * @param call_cleaner Whether to call the cleaner handler.
461 * @return Handle to the required tunnel or NULL if not found.
464 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
466 struct GNUNET_MESH_Handle *h;
467 struct GNUNET_MESH_TransmitHandle *th;
468 struct GNUNET_MESH_TransmitHandle *next;
470 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
479 /* free all peer's ID */
480 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
481 GNUNET_PEER_change_rc (t->peer, -1);
483 /* signal tunnel destruction */
484 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
485 h->cleaner (h->cls, t, t->ctx);
487 /* check that clients did not leave messages behind in the queue */
488 for (th = h->th_head; NULL != th; th = next)
493 /* Clients should have aborted their requests already.
494 * Management traffic should be ok, as clients can't cancel that */
495 GNUNET_break (GNUNET_NO == th_is_payload(th));
496 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
498 /* clean up request */
499 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
500 GNUNET_SCHEDULER_cancel (th->timeout_task);
504 /* if there are no more pending requests with mesh service, cancel active request */
505 /* Note: this should be unnecessary... */
506 if ((0 == message_ready_size (h)) && (NULL != h->th))
508 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
513 GNUNET_PEER_change_rc (t->peer, -1);
520 * Notify client that the transmission has timed out
523 * @param tc task context
526 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
528 struct GNUNET_MESH_TransmitHandle *th = cls;
529 struct GNUNET_MESH_Handle *mesh;
531 mesh = th->tunnel->mesh;
532 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
533 th->tunnel->packet_size = 0;
534 if (GNUNET_YES == th_is_payload (th))
535 th->notify (th->notify_cls, 0, NULL);
537 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
539 /* nothing ready to transmit, no point in asking for transmission */
540 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
547 * Add a transmit handle to the transmission queue and set the
550 * @param h mesh handle with the queue head and tail
551 * @param th handle to the packet to be transmitted
554 add_to_queue (struct GNUNET_MESH_Handle *h,
555 struct GNUNET_MESH_TransmitHandle *th)
557 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
558 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
561 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
562 (th->timeout), &timeout_transmission, th);
567 * Auxiliary function to send an already constructed packet to the service.
568 * Takes care of creating a new queue element, copying the message and
569 * calling the tmt_rdy function if necessary.
571 * @param h mesh handle
572 * @param msg message to transmit
573 * @param tunnel tunnel this send is related to (NULL if N/A)
576 send_packet (struct GNUNET_MESH_Handle *h,
577 const struct GNUNET_MessageHeader *msg,
578 struct GNUNET_MESH_Tunnel *tunnel);
582 * Send an ack on the tunnel to confirm the processing of a message.
584 * @param h Mesh handle.
585 * @param t Tunnel on which to send the ACK.
588 send_ack (struct GNUNET_MESH_Handle *h, struct GNUNET_MESH_Tunnel *t)
590 struct GNUNET_MESH_LocalAck msg;
593 delta = t->max_recv_pid - t->last_recv_pid;
594 if (delta > ACK_THRESHOLD)
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "Not sending ACK on tunnel %X: ACK: %u, PID: %u, buffer %u\n",
598 t->tid, t->max_recv_pid, t->last_recv_pid, delta);
601 if (GNUNET_YES == t->buffering)
602 t->max_recv_pid = t->last_recv_pid + INITIAL_WINDOW_SIZE;
604 t->max_recv_pid = t->last_recv_pid + 1;
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "Sending ACK on tunnel %X: %u\n",
607 t->tid, t->max_recv_pid);
608 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
609 msg.header.size = htons (sizeof (msg));
610 msg.tunnel_id = htonl (t->tid);
611 msg.max_pid = htonl (t->max_recv_pid);
614 t->mesh->acks_sent++;
617 send_packet (h, &msg.header, t);
624 * Reconnect callback: tries to reconnect again after a failer previous
626 * @param cls closure (mesh handle)
627 * @param tc task context
630 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
634 * Send a connect packet to the service with the applications and types
635 * requested by the user.
637 * @param h The mesh handle.
641 send_connect (struct GNUNET_MESH_Handle *h)
645 size = sizeof (struct GNUNET_MESH_ClientConnect);
646 size += h->n_handlers * sizeof (uint16_t);
648 char buf[size] GNUNET_ALIGN;
649 struct GNUNET_MESH_ClientConnect *msg;
654 /* build connection packet */
655 msg = (struct GNUNET_MESH_ClientConnect *) buf;
656 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
657 msg->header.size = htons (size);
658 msg->types = htons (h->n_handlers);
659 msg->ports = htons (h->n_ports);
660 types = (uint16_t *) &msg[1];
661 for (i = 0; i < h->n_handlers; i++)
663 types[i] = htons (h->message_handlers[i].type);
664 LOG (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
665 h->message_handlers[i].type);
667 ports = (uint32_t *) &types[h->n_handlers];
668 for (i = 0; i < h->n_ports; i++)
670 ports[i] = htonl (h->ports[i]);
671 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
674 LOG (GNUNET_ERROR_TYPE_DEBUG,
675 "Sending %lu bytes long message %u types and %u ports\n",
676 ntohs (msg->header.size), h->n_handlers, h->n_ports);
677 send_packet (h, &msg->header, NULL);
683 * Reconnect to the service, retransmit all infomation to try to restore the
686 * @param h handle to the mesh
688 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
691 do_reconnect (struct GNUNET_MESH_Handle *h)
693 struct GNUNET_MESH_Tunnel *t;
695 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
696 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
697 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
698 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
699 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
704 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
707 if (NULL != h->client)
709 GNUNET_CLIENT_disconnect (h->client);
713 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
714 if (h->client == NULL)
716 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
719 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
720 GNUNET_TIME_relative_multiply
721 (h->reconnect_time, 2));
722 LOG (GNUNET_ERROR_TYPE_DEBUG,
723 "Next retry in %s\n",
724 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
731 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
734 /* Rebuild all tunnels */
735 for (t = h->tunnels_head; NULL != t; t = t->next)
737 struct GNUNET_MESH_TunnelMessage tmsg;
739 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
741 /* Tunnel was created by service (incoming tunnel) */
742 /* TODO: Notify service of missing tunnel, to request
743 * creator to recreate path (find a path to him via DHT?)
747 t->next_send_pid = 0;
748 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
749 t->last_recv_pid = (uint32_t) -1;
750 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
751 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
752 tmsg.tunnel_id = htonl (t->tid);
753 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
754 send_packet (h, &tmsg.header, t);
756 if (GNUNET_NO == t->buffering)
757 GNUNET_MESH_tunnel_buffer (t, GNUNET_NO);
763 * Reconnect callback: tries to reconnect again after a failer previous
765 * @param cls closure (mesh handle)
766 * @param tc task context
769 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
771 struct GNUNET_MESH_Handle *h = cls;
773 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
774 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
781 * Reconnect to the service, retransmit all infomation to try to restore the
784 * @param h handle to the mesh
786 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
789 reconnect (struct GNUNET_MESH_Handle *h)
791 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
792 h->in_receive = GNUNET_NO;
793 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
794 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
799 /******************************************************************************/
800 /*********************** RECEIVE HANDLERS ****************************/
801 /******************************************************************************/
804 * Process the new tunnel notification and add it to the tunnels in the handle
806 * @param h The mesh handle
807 * @param msg A message with the details of the new incoming tunnel
810 process_tunnel_created (struct GNUNET_MESH_Handle *h,
811 const struct GNUNET_MESH_TunnelNotification *msg)
813 struct GNUNET_MESH_Tunnel *t;
814 MESH_TunnelNumber tid;
816 tid = ntohl (msg->tunnel_id);
817 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
818 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
823 if (NULL != h->new_tunnel)
825 t = create_tunnel (h, tid);
826 t->peer = GNUNET_PEER_intern (&msg->peer);
827 GNUNET_PEER_change_rc (t->peer, 1);
830 t->port = ntohl (msg->port);
831 if ((msg->opt & MESH_TUNNEL_OPT_NOBUFFER) != 0)
832 t->buffering = GNUNET_NO;
834 t->buffering = GNUNET_YES;
835 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
836 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
837 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
841 struct GNUNET_MESH_TunnelMessage d_msg;
843 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
845 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
846 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
847 d_msg.tunnel_id = msg->tunnel_id;
849 send_packet (h, &d_msg.header, NULL);
856 * Process the tunnel destroy notification and free associated resources
858 * @param h The mesh handle
859 * @param msg A message with the details of the tunnel being destroyed
862 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
863 const struct GNUNET_MESH_TunnelMessage *msg)
865 struct GNUNET_MESH_Tunnel *t;
866 MESH_TunnelNumber tid;
868 tid = ntohl (msg->tunnel_id);
869 t = retrieve_tunnel (h, tid);
875 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
876 destroy_tunnel (t, GNUNET_YES);
882 * Process the incoming data packets
884 * @param h The mesh handle
885 * @param message A message encapsulating the data
887 * @return GNUNET_YES if everything went fine
888 * GNUNET_NO if client closed connection (h no longer valid)
891 process_incoming_data (struct GNUNET_MESH_Handle *h,
892 const struct GNUNET_MessageHeader *message)
894 const struct GNUNET_MessageHeader *payload;
895 const struct GNUNET_MESH_MessageHandler *handler;
896 const struct GNUNET_PeerIdentity *peer;
897 struct GNUNET_PeerIdentity id;
898 struct GNUNET_MESH_Unicast *ucast;
899 struct GNUNET_MESH_ToOrigin *to_orig;
900 struct GNUNET_MESH_Tunnel *t;
905 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
906 type = ntohs (message->type);
909 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
910 ucast = (struct GNUNET_MESH_Unicast *) message;
912 t = retrieve_tunnel (h, ntohl (ucast->tid));
913 payload = (struct GNUNET_MessageHeader *) &ucast[1];
915 pid = ntohl (ucast->pid);
916 LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%X]\n",
917 GNUNET_i2s (peer), ntohl (ucast->tid));
919 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
920 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
921 t = retrieve_tunnel (h, ntohl (to_orig->tid));
922 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
923 GNUNET_PEER_resolve (t->peer, &id);
925 pid = ntohl (to_orig->pid);
926 LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%X]\n",
927 GNUNET_i2s (peer), ntohl (to_orig->tid));
933 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", pid);
936 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
937 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
941 GMC_is_pid_bigger(pid, t->max_recv_pid))
944 LOG (GNUNET_ERROR_TYPE_WARNING,
945 " unauthorized message! (%u, max %u)\n",
946 pid, t->max_recv_pid);
947 // FIXME fc what now? accept? reject?
950 t->last_recv_pid = pid;
951 type = ntohs (payload->type);
953 for (i = 0; i < h->n_handlers; i++)
955 handler = &h->message_handlers[i];
956 if (handler->type == type)
959 handler->callback (h->cls, t, &t->ctx, peer, payload))
961 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
962 GNUNET_MESH_disconnect (h);
967 LOG (GNUNET_ERROR_TYPE_DEBUG,
968 "callback completed successfully\n");
977 * Process a local ACK message, enabling the client to send
978 * more data to the service.
980 * @param h Mesh handle.
981 * @param message Message itself.
984 process_ack (struct GNUNET_MESH_Handle *h,
985 const struct GNUNET_MessageHeader *message)
987 struct GNUNET_MESH_LocalAck *msg;
988 struct GNUNET_MESH_Tunnel *t;
991 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
993 msg = (struct GNUNET_MESH_LocalAck *) message;
995 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
999 LOG (GNUNET_ERROR_TYPE_WARNING,
1000 "ACK on unknown tunnel %X\n",
1001 ntohl (msg->tunnel_id));
1004 ack = ntohl (msg->max_pid);
1005 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X, ack %u!\n", t->tid, ack);
1006 if (GNUNET_YES == GMC_is_pid_bigger(ack, t->max_send_pid))
1007 t->max_send_pid = ack;
1010 if (NULL == h->th && 0 < t->packet_size)
1012 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n", t->tid, ack);
1014 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
1015 GNUNET_TIME_UNIT_FOREVER_REL,
1016 GNUNET_YES, &send_callback, h);
1022 * Process a local reply about info on all tunnels, pass info to the user.
1024 * @param h Mesh handle.
1025 * @param message Message itself.
1028 process_get_tunnels (struct GNUNET_MESH_Handle *h,
1029 const struct GNUNET_MessageHeader *message)
1031 struct GNUNET_MESH_LocalMonitor *msg;
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
1035 if (NULL == h->tunnels_cb)
1037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1041 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1042 if (ntohs (message->size) !=
1043 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1044 sizeof (struct GNUNET_PeerIdentity)))
1046 GNUNET_break_op (0);
1047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1048 "Get tunnels message: size %hu - expected %u\n",
1049 ntohs (message->size),
1050 sizeof (struct GNUNET_MESH_LocalMonitor));
1053 h->tunnels_cb (h->tunnels_cls,
1054 ntohl (msg->tunnel_id),
1062 * Process a local monitor_tunnel reply, pass info to the user.
1064 * @param h Mesh handle.
1065 * @param message Message itself.
1068 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1069 const struct GNUNET_MessageHeader *message)
1071 struct GNUNET_MESH_LocalMonitor *msg;
1074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1076 if (NULL == h->tunnel_cb)
1078 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1082 /* Verify message sanity */
1083 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1084 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1085 if (ntohs (message->size) != esize)
1087 GNUNET_break_op (0);
1088 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1089 "Show tunnel message: size %hu - expected %u\n",
1090 ntohs (message->size),
1093 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1094 h->tunnel_cb = NULL;
1095 h->tunnel_cls = NULL;
1100 h->tunnel_cb (h->tunnel_cls,
1107 * Function to process all messages received from the service
1109 * @param cls closure
1110 * @param msg message received, NULL on timeout or fatal error
1113 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1115 struct GNUNET_MESH_Handle *h = cls;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG,
1120 "Mesh service disconnected, reconnecting\n", h);
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1125 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1126 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1127 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1128 switch (ntohs (msg->type))
1130 /* Notify of a new incoming tunnel */
1131 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1132 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1134 /* Notify of a tunnel disconnection */
1135 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1136 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1138 /* Notify of a new data packet in the tunnel */
1139 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1140 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1141 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1142 if (GNUNET_NO == process_incoming_data (h, msg))
1145 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1146 process_ack (h, msg);
1148 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1149 process_get_tunnels (h, msg);
1151 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1152 process_show_tunnel (h, msg);
1155 /* We shouldn't get any other packages, log and ignore */
1156 LOG (GNUNET_ERROR_TYPE_WARNING,
1157 "unsolicited message form service (type %s)\n",
1158 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1161 if (GNUNET_YES == h->in_receive)
1163 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1164 GNUNET_TIME_UNIT_FOREVER_REL);
1168 LOG (GNUNET_ERROR_TYPE_DEBUG,
1169 "in receive off, not calling CLIENT_receive\n");
1174 /******************************************************************************/
1175 /************************ SEND FUNCTIONS ****************************/
1176 /******************************************************************************/
1179 * Function called to send a message to the service.
1180 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1183 * @param cls closure, the mesh handle
1184 * @param size number of bytes available in buf
1185 * @param buf where the callee should write the connect message
1186 * @return number of bytes written to buf
1189 send_callback (void *cls, size_t size, void *buf)
1191 struct GNUNET_MESH_Handle *h = cls;
1192 struct GNUNET_MESH_TransmitHandle *th;
1193 struct GNUNET_MESH_TransmitHandle *next;
1194 struct GNUNET_MESH_Tunnel *t;
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
1202 if ((0 == size) || (NULL == buf))
1204 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL send callback on %p\n", h);
1211 nsize = message_ready_size (h);
1212 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1215 if (GNUNET_YES == th_is_payload (th))
1217 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload\n");
1218 if (GNUNET_YES == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
1220 /* This tunnel is not ready to transmit yet, try next message */
1225 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1227 /* traffic to origin */
1228 struct GNUNET_MESH_ToOrigin to;
1229 struct GNUNET_MessageHeader *mh;
1231 GNUNET_assert (size >= th->size);
1232 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
1233 psize = th->notify (th->notify_cls, size - sizeof (to), mh);
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin, type %s\n",
1235 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1238 psize += sizeof (to);
1239 GNUNET_assert (size >= psize);
1240 to.header.size = htons (psize);
1241 to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1242 to.tid = htonl (t->tid);
1243 to.pid = htonl (t->next_send_pid);
1245 memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1246 memcpy (cbuf, &to, sizeof (to));
1252 struct GNUNET_MESH_Unicast uc;
1253 struct GNUNET_MessageHeader *mh;
1255 GNUNET_assert (size >= th->size);
1256 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
1257 psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, " unicast, type %s\n",
1259 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1262 psize += sizeof (uc);
1263 GNUNET_assert (size >= psize);
1264 uc.header.size = htons (psize);
1265 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1266 uc.tid = htonl (t->tid);
1267 uc.pid = htonl (t->next_send_pid);
1269 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1276 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1278 LOG (GNUNET_ERROR_TYPE_DEBUG, " mesh traffic, type %s\n",
1279 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1280 memcpy (cbuf, &th[1], th->size);
1283 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1284 GNUNET_SCHEDULER_cancel (th->timeout_task);
1285 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1288 nsize = message_ready_size (h);
1293 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size: %u\n", tsize);
1295 size = message_ready_size (h);
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, " next size: %u\n", size);
1300 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1301 GNUNET_TIME_UNIT_FOREVER_REL,
1302 GNUNET_YES, &send_callback, h);
1306 if (NULL != h->th_head)
1307 LOG (GNUNET_ERROR_TYPE_DEBUG, " can't transmit any more\n");
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, " nothing left to transmit\n");
1311 if (GNUNET_NO == h->in_receive)
1313 LOG (GNUNET_ERROR_TYPE_DEBUG, " start receiving from service\n");
1314 h->in_receive = GNUNET_YES;
1315 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1316 GNUNET_TIME_UNIT_FOREVER_REL);
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
1324 * Auxiliary function to send an already constructed packet to the service.
1325 * Takes care of creating a new queue element, copying the message and
1326 * calling the tmt_rdy function if necessary.
1328 * @param h mesh handle
1329 * @param msg message to transmit
1330 * @param tunnel tunnel this send is related to (NULL if N/A)
1333 send_packet (struct GNUNET_MESH_Handle *h,
1334 const struct GNUNET_MessageHeader *msg,
1335 struct GNUNET_MESH_Tunnel *tunnel)
1337 struct GNUNET_MESH_TransmitHandle *th;
1340 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1341 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1342 msize = ntohs (msg->size);
1343 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1344 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1346 th->tunnel = tunnel;
1347 memcpy (&th[1], msg, msize);
1348 add_to_queue (h, th);
1349 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1352 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1354 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1355 GNUNET_TIME_UNIT_FOREVER_REL,
1356 GNUNET_YES, &send_callback, h);
1360 /******************************************************************************/
1361 /********************** API CALL DEFINITIONS *************************/
1362 /******************************************************************************/
1364 struct GNUNET_MESH_Handle *
1365 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1366 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1367 GNUNET_MESH_TunnelEndHandler cleaner,
1368 const struct GNUNET_MESH_MessageHandler *handlers,
1371 struct GNUNET_MESH_Handle *h;
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1374 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1377 h->new_tunnel = new_tunnel;
1378 h->cleaner = cleaner;
1379 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1380 if (h->client == NULL)
1387 h->message_handlers = handlers;
1389 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1390 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1391 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1393 /* count handlers */
1394 for (h->n_handlers = 0;
1395 handlers && handlers[h->n_handlers].type;
1397 for (h->n_ports = 0;
1398 ports && ports[h->n_ports];
1401 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1407 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
1408 * disconnect callbacks will be called on any still connected peers, notifying
1409 * about their disconnection. The registered inbound tunnel cleaner will be
1410 * called should any inbound tunnels still exist.
1412 * @param handle connection to mesh to disconnect
1415 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1417 struct GNUNET_MESH_Tunnel *t;
1418 struct GNUNET_MESH_Tunnel *aux;
1419 struct GNUNET_MESH_TransmitHandle *th;
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1424 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1425 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1428 t = handle->tunnels_head;
1432 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1437 destroy_tunnel (t, GNUNET_YES);
1440 while ( (th = handle->th_head) != NULL)
1442 struct GNUNET_MessageHeader *msg;
1444 /* Make sure it is an allowed packet (everything else should have been
1445 * already canceled).
1447 GNUNET_break (GNUNET_NO == th_is_payload (th));
1448 msg = (struct GNUNET_MessageHeader *) &th[1];
1449 switch (ntohs(msg->type))
1451 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1452 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1453 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1454 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1458 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1462 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1466 if (NULL != handle->th)
1468 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1471 if (NULL != handle->client)
1473 GNUNET_CLIENT_disconnect (handle->client);
1474 handle->client = NULL;
1476 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1478 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1479 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1481 GNUNET_free (handle);
1485 struct GNUNET_MESH_Tunnel *
1486 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1488 const struct GNUNET_PeerIdentity *peer,
1491 struct GNUNET_MESH_Tunnel *t;
1492 struct GNUNET_MESH_TunnelMessage msg;
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1495 t = create_tunnel (h, 0);
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1497 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1498 t->ctx = tunnel_ctx;
1499 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1500 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1501 msg.tunnel_id = htonl (t->tid);
1502 msg.port = htonl (port);
1504 send_packet (h, &msg.header, t);
1510 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
1513 * @param tunnel tunnel handle
1516 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1518 struct GNUNET_MESH_Handle *h;
1519 struct GNUNET_MESH_TunnelMessage msg;
1520 struct GNUNET_MESH_TransmitHandle *th;
1522 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1525 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1526 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1527 msg.tunnel_id = htonl (tunnel->tid);
1531 struct GNUNET_MESH_TransmitHandle *aux;
1532 if (th->tunnel == tunnel)
1535 /* FIXME call the handler? */
1536 if (GNUNET_YES == th_is_payload (th))
1537 th->notify (th->notify_cls, 0, NULL);
1538 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1546 destroy_tunnel (tunnel, GNUNET_NO);
1547 send_packet (h, &msg.header, NULL);
1552 * Turn on/off the buffering status of the tunnel.
1554 * @param tunnel Tunnel affected.
1555 * @param buffer GNUNET_YES to turn buffering on (default),
1556 * GNUNET_NO otherwise.
1559 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer)
1561 struct GNUNET_MESH_TunnelMessage msg;
1562 struct GNUNET_MESH_Handle *h;
1565 tunnel->buffering = buffer;
1566 tunnel->max_send_pid = tunnel->next_send_pid;
1568 if (GNUNET_YES == buffer)
1569 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER);
1571 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER);
1572 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1573 msg.tunnel_id = htonl (tunnel->tid);
1575 send_packet (h, &msg.header, NULL);
1579 struct GNUNET_MESH_TransmitHandle *
1580 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1581 struct GNUNET_TIME_Relative maxdelay,
1583 GNUNET_CONNECTION_TransmitReadyNotify notify,
1586 struct GNUNET_MESH_TransmitHandle *th;
1589 GNUNET_assert (NULL != tunnel);
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1592 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1593 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1596 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1597 GNUNET_assert (NULL != notify);
1598 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1599 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1600 th->tunnel = tunnel;
1601 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1602 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1603 overhead = sizeof (struct GNUNET_MESH_ToOrigin);
1605 overhead = sizeof (struct GNUNET_MESH_Unicast);
1606 tunnel->packet_size = th->size = notify_size + overhead;
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1608 th->notify = notify;
1609 th->notify_cls = notify_cls;
1610 add_to_queue (tunnel->mesh, th);
1611 if (NULL != tunnel->mesh->th)
1613 if (GMC_is_pid_bigger(tunnel->next_send_pid, tunnel->max_send_pid))
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, " call notify tmt rdy\n");
1617 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1618 GNUNET_TIME_UNIT_FOREVER_REL,
1619 GNUNET_YES, &send_callback,
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1627 * Cancel the specified transmission-ready notification.
1629 * @param th handle that was returned by "notify_transmit_ready".
1632 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1634 struct GNUNET_MESH_Handle *mesh;
1636 th->tunnel->packet_size = 0;
1637 mesh = th->tunnel->mesh;
1638 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1639 GNUNET_SCHEDULER_cancel (th->timeout_task);
1640 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1642 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1644 /* queue empty, no point in asking for transmission */
1645 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1652 * Request information about the running mesh peer.
1653 * The callback will be called for every tunnel known to the service,
1654 * listing all active peers that blong to the tunnel.
1656 * If called again on the same handle, it will overwrite the previous
1657 * callback and cls. To retrieve the cls, monitor_cancel must be
1660 * WARNING: unstable API, likely to change in the future!
1662 * @param h Handle to the mesh peer.
1663 * @param callback Function to call with the requested data.
1664 * @param callback_cls Closure for @c callback.
1667 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1668 GNUNET_MESH_TunnelsCB callback,
1671 struct GNUNET_MessageHeader msg;
1673 msg.size = htons (sizeof (msg));
1674 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1675 send_packet (h, &msg, NULL);
1676 h->tunnels_cb = callback;
1677 h->tunnels_cls = callback_cls;
1684 * Cancel a monitor request. The monitor callback will not be called.
1686 * @param h Mesh handle.
1688 * @return Closure given to GNUNET_MESH_monitor, if any.
1691 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1695 cls = h->tunnels_cls;
1696 h->tunnels_cb = NULL;
1697 h->tunnels_cls = NULL;
1703 * Request information about a specific tunnel of the running mesh peer.
1705 * WARNING: unstable API, likely to change in the future!
1706 * FIXME Add destination option.
1708 * @param h Handle to the mesh peer.
1709 * @param initiator ID of the owner of the tunnel.
1710 * @param tunnel_number Tunnel number.
1711 * @param callback Function to call with the requested data.
1712 * @param callback_cls Closure for @c callback.
1715 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1716 struct GNUNET_PeerIdentity *initiator,
1717 unsigned int tunnel_number,
1718 GNUNET_MESH_TunnelCB callback,
1721 struct GNUNET_MESH_LocalMonitor msg;
1723 msg.header.size = htons (sizeof (msg));
1724 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1725 msg.owner = *initiator;
1726 msg.tunnel_id = htonl (tunnel_number);
1728 send_packet (h, &msg.header, NULL);
1729 h->tunnel_cb = callback;
1730 h->tunnel_cls = callback_cls;
1737 * Transition API for tunnel ctx management
1740 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
1746 * Transition API for tunnel ctx management
1749 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)