2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
4 GNUnet is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; either version 3, or (at your
7 option) any later version.
8 GNUnet is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with GNUnet; see the file COPYING. If not, write to the
14 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15 Boston, MA 02111-1307, USA.
19 * @file mesh/mesh_api.c
20 * @brief mesh api: client implementation of new mesh service
21 * @author Bartlomiej Polot
26 * - AUXILIARY FUNCTIONS
29 * - API CALL DEFINITIONS
31 * TODO: add regex to reconnect
34 #include "gnunet_common.h"
35 #include "gnunet_client_lib.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_peer_lib.h"
38 #include "gnunet_mesh_service.h"
40 #include "mesh_protocol.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
43 #define DEBUG_ACK GNUNET_YES
45 /******************************************************************************/
46 /************************ DATA STRUCTURES ****************************/
47 /******************************************************************************/
50 * Transmission queue to the service
52 struct GNUNET_MESH_TransmitHandle
58 struct GNUNET_MESH_TransmitHandle *next;
63 struct GNUNET_MESH_TransmitHandle *prev;
66 * Tunnel this message is sent on / for (may be NULL for control messages).
68 struct GNUNET_MESH_Tunnel *tunnel;
71 * Callback to obtain the message to transmit, or NULL if we
72 * got the message in 'data'. Notice that messages built
73 * by 'notify' need to be encapsulated with information about
76 GNUNET_CONNECTION_TransmitReadyNotify notify;
79 * Closure for 'notify'
84 * How long is this message valid. Once the timeout has been
85 * reached, the message must no longer be sent. If this
86 * is a message with a 'notify' callback set, the 'notify'
87 * function should be called with 'buf' NULL and size 0.
89 struct GNUNET_TIME_Absolute timeout;
92 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
94 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
97 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
104 * Opaque handle to the service.
106 struct GNUNET_MESH_Handle
110 * Handle to the server connection, to send messages later
112 struct GNUNET_CLIENT_Connection *client;
115 * Set of handlers used for processing incoming messages in the tunnels
117 const struct GNUNET_MESH_MessageHandler *message_handlers;
120 * Number of handlers in the handlers array.
122 unsigned int n_handlers;
127 const uint32_t *ports;
132 unsigned int n_ports;
135 * Double linked list of the tunnels this client is connected to, head.
137 struct GNUNET_MESH_Tunnel *tunnels_head;
140 * Double linked list of the tunnels this client is connected to, tail.
142 struct GNUNET_MESH_Tunnel *tunnels_tail;
145 * Callback for inbound tunnel creation
147 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
150 * Callback for inbound tunnel disconnection
152 GNUNET_MESH_TunnelEndHandler *cleaner;
155 * Handle to cancel pending transmissions in case of disconnection
157 struct GNUNET_CLIENT_TransmitHandle *th;
160 * Closure for all the handlers given by the client
165 * Messages to send to the service, head.
167 struct GNUNET_MESH_TransmitHandle *th_head;
170 * Messages to send to the service, tail.
172 struct GNUNET_MESH_TransmitHandle *th_tail;
175 * tid of the next tunnel to create (to avoid reusing IDs often)
177 MESH_TunnelNumber next_tid;
180 * Have we started the task to receive messages from the service
181 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
186 * Configuration given by the client, in case of reconnection
188 const struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Time to the next reconnect in case one reconnect fails
193 struct GNUNET_TIME_Relative reconnect_time;
196 * Task for trying to reconnect.
198 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
203 GNUNET_MESH_TunnelsCB tunnels_cb;
206 * Monitor callback closure.
213 GNUNET_MESH_TunnelCB tunnel_cb;
216 * Tunnel callback closure.
221 unsigned int acks_sent;
222 unsigned int acks_recv;
228 * Description of a peer
230 struct GNUNET_MESH_Peer
233 * ID of the peer in short form
238 * Tunnel this peer belongs to
240 struct GNUNET_MESH_Tunnel *t;
243 * Flag indicating whether service has informed about its connection
251 * Opaque handle to a tunnel.
253 struct GNUNET_MESH_Tunnel
259 struct GNUNET_MESH_Tunnel *next;
264 struct GNUNET_MESH_Tunnel *prev;
267 * Handle to the mesh this tunnel belongs to
269 struct GNUNET_MESH_Handle *mesh;
272 * Local ID of the tunnel
274 MESH_TunnelNumber tid;
282 * Other end of the tunnel.
287 * Any data the caller wants to put in here
292 * Size of packet queued in this tunnel
294 unsigned int packet_size;
297 * Is the tunnel allowed to buffer?
302 * Is the tunnel allowed to buffer?
307 * Maximum allowed PID to send (last ACK recevied).
309 uint32_t last_ack_recv;
312 * Last PID received from the service.
314 uint32_t last_pid_recv;
317 * Last packet ID sent to the service.
319 uint32_t last_pid_sent;
322 * Last ACK value sent to the service: how much are we willing to accept?
324 uint32_t last_ack_sent;
329 * Implementation state for mesh's message queue.
334 * The current transmit handle, or NULL
335 * if no transmit is active.
337 struct GNUNET_MESH_TransmitHandle *th;
340 * Tunnel to send the data over.
342 struct GNUNET_MESH_Tunnel *tunnel;
346 /******************************************************************************/
347 /*********************** DECLARATIONS *************************/
348 /******************************************************************************/
351 * Function called to send a message to the service.
352 * "buf" will be NULL and "size" zero if the socket was closed for writing in
355 * @param cls closure, the mesh handle
356 * @param size number of bytes available in buf
357 * @param buf where the callee should write the connect message
358 * @return number of bytes written to buf
361 send_callback (void *cls, size_t size, void *buf);
364 /******************************************************************************/
365 /*********************** AUXILIARY FUNCTIONS *************************/
366 /******************************************************************************/
369 * Check if transmission is a payload packet.
371 * @param th Transmission handle.
373 * @return GNUNET_YES if it is a payload packet,
374 * GNUNET_NO if it is a mesh management packet.
377 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
379 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
384 * Check whether there is any message ready in the queue and find the size.
386 * @param h Mesh handle.
388 * @return The size of the first ready message in the queue,
389 * 0 if there is none.
392 message_ready_size (struct GNUNET_MESH_Handle *h)
394 struct GNUNET_MESH_TransmitHandle *th;
395 struct GNUNET_MESH_Tunnel *t;
397 for (th = h->th_head; NULL != th; th = th->next)
400 if (GNUNET_NO == th_is_payload (th))
402 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
405 if (GNUNET_YES == GMC_is_pid_bigger(t->last_ack_recv, t->last_pid_sent))
407 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok (%u =< %u)\n",
408 t->last_pid_sent + 1, t->last_ack_recv);
417 * Get the tunnel handler for the tunnel specified by id from the given handle
418 * @param h Mesh handle
419 * @param tid ID of the wanted tunnel
420 * @return handle to the required tunnel or NULL if not found
422 static struct GNUNET_MESH_Tunnel *
423 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
425 struct GNUNET_MESH_Tunnel *t;
439 * Create a new tunnel and insert it in the tunnel list of the mesh handle
440 * @param h Mesh handle
441 * @param tid desired tid of the tunnel, 0 to assign one automatically
442 * @return handle to the created tunnel
444 static struct GNUNET_MESH_Tunnel *
445 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
447 struct GNUNET_MESH_Tunnel *t;
449 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
450 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
454 t->tid = h->next_tid;
455 while (NULL != retrieve_tunnel (h, h->next_tid))
458 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
459 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
466 t->last_ack_recv = (uint32_t) -1;
467 t->last_pid_recv = (uint32_t) -1;
468 t->last_ack_sent = (uint32_t) -1;
469 t->last_pid_sent = (uint32_t) -1;
470 t->buffering = GNUNET_YES;
476 * Destroy the specified tunnel.
477 * - Destroys all peers, calling the disconnect callback on each if needed
478 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
479 * - Calls cleaner if tunnel was inbound
480 * - Frees all memory used
482 * @param t Pointer to the tunnel.
483 * @param call_cleaner Whether to call the cleaner handler.
485 * @return Handle to the required tunnel or NULL if not found.
488 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
490 struct GNUNET_MESH_Handle *h;
491 struct GNUNET_MESH_TransmitHandle *th;
492 struct GNUNET_MESH_TransmitHandle *next;
494 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
503 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
505 /* signal tunnel destruction */
506 if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
507 h->cleaner (h->cls, t, t->ctx);
509 /* check that clients did not leave messages behind in the queue */
510 for (th = h->th_head; NULL != th; th = next)
515 /* Clients should have aborted their requests already.
516 * Management traffic should be ok, as clients can't cancel that */
517 GNUNET_break (GNUNET_NO == th_is_payload(th));
518 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
520 /* clean up request */
521 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
522 GNUNET_SCHEDULER_cancel (th->timeout_task);
526 /* if there are no more pending requests with mesh service, cancel active request */
527 /* Note: this should be unnecessary... */
528 if ((0 == message_ready_size (h)) && (NULL != h->th))
530 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
535 GNUNET_PEER_change_rc (t->peer, -1);
542 * Notify client that the transmission has timed out
545 * @param tc task context
548 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
550 struct GNUNET_MESH_TransmitHandle *th = cls;
551 struct GNUNET_MESH_Handle *mesh;
553 mesh = th->tunnel->mesh;
554 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
555 th->tunnel->packet_size = 0;
556 if (GNUNET_YES == th_is_payload (th))
557 th->notify (th->notify_cls, 0, NULL);
559 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
561 /* nothing ready to transmit, no point in asking for transmission */
562 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
569 * Add a transmit handle to the transmission queue and set the
572 * @param h mesh handle with the queue head and tail
573 * @param th handle to the packet to be transmitted
576 add_to_queue (struct GNUNET_MESH_Handle *h,
577 struct GNUNET_MESH_TransmitHandle *th)
579 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
580 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
583 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
584 (th->timeout), &timeout_transmission, th);
589 * Auxiliary function to send an already constructed packet to the service.
590 * Takes care of creating a new queue element, copying the message and
591 * calling the tmt_rdy function if necessary.
593 * @param h mesh handle
594 * @param msg message to transmit
595 * @param tunnel tunnel this send is related to (NULL if N/A)
598 send_packet (struct GNUNET_MESH_Handle *h,
599 const struct GNUNET_MessageHeader *msg,
600 struct GNUNET_MESH_Tunnel *tunnel);
604 * Send an ack on the tunnel to confirm the processing of a message.
606 * @param t Tunnel on which to send the ACK.
609 send_ack (struct GNUNET_MESH_Tunnel *t)
611 struct GNUNET_MESH_LocalAck msg;
613 t->last_ack_sent = t->last_pid_recv + 1;
614 LOG (GNUNET_ERROR_TYPE_DEBUG,
615 "Sending ACK on tunnel %X: %u\n",
616 t->tid, t->last_ack_sent);
617 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
618 msg.header.size = htons (sizeof (msg));
619 msg.tunnel_id = htonl (t->tid);
620 msg.ack = htonl (t->last_ack_sent);
623 t->mesh->acks_sent++;
626 send_packet (t->mesh, &msg.header, t);
633 * Reconnect callback: tries to reconnect again after a failer previous
635 * @param cls closure (mesh handle)
636 * @param tc task context
639 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
643 * Send a connect packet to the service with the applications and types
644 * requested by the user.
646 * @param h The mesh handle.
650 send_connect (struct GNUNET_MESH_Handle *h)
654 size = sizeof (struct GNUNET_MESH_ClientConnect);
655 size += h->n_ports * sizeof (uint32_t);
657 char buf[size] GNUNET_ALIGN;
658 struct GNUNET_MESH_ClientConnect *msg;
662 /* build connection packet */
663 msg = (struct GNUNET_MESH_ClientConnect *) buf;
664 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
665 msg->header.size = htons (size);
666 ports = (uint32_t *) &msg[1];
667 for (i = 0; i < h->n_ports; i++)
669 ports[i] = htonl (h->ports[i]);
670 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
674 "Sending %lu bytes long message with %u ports\n",
675 ntohs (msg->header.size), h->n_ports);
676 send_packet (h, &msg->header, NULL);
682 * Reconnect to the service, retransmit all infomation to try to restore the
685 * @param h handle to the mesh
687 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
690 do_reconnect (struct GNUNET_MESH_Handle *h)
692 struct GNUNET_MESH_Tunnel *t;
694 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
695 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
696 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
697 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
698 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
703 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
706 if (NULL != h->client)
708 GNUNET_CLIENT_disconnect (h->client);
712 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
713 if (h->client == NULL)
715 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
718 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
719 GNUNET_TIME_relative_multiply
720 (h->reconnect_time, 2));
721 LOG (GNUNET_ERROR_TYPE_DEBUG,
722 "Next retry in %s\n",
723 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
730 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
733 /* Rebuild all tunnels */
734 for (t = h->tunnels_head; NULL != t; t = t->next)
736 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->last_ack_sent = (uint32_t) -1;
748 t->last_pid_sent = (uint32_t) -1;
749 t->last_ack_recv = (uint32_t) -1;
750 t->last_pid_recv = (uint32_t) -1;
751 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
752 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
753 tmsg.tunnel_id = htonl (t->tid);
754 GNUNET_PEER_resolve (t->peer, &tmsg.peer);
757 if (GNUNET_NO == t->buffering)
758 options |= GNUNET_MESH_OPTION_NOBUFFER;
760 if (GNUNET_YES == t->reliable)
761 options |= GNUNET_MESH_OPTION_RELIABLE;
763 tmsg.options = htonl (options);
764 send_packet (h, &tmsg.header, t);
770 * Reconnect callback: tries to reconnect again after a failer previous
772 * @param cls closure (mesh handle)
773 * @param tc task context
776 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
778 struct GNUNET_MESH_Handle *h = cls;
780 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
781 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
788 * Reconnect to the service, retransmit all infomation to try to restore the
791 * @param h handle to the mesh
793 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
796 reconnect (struct GNUNET_MESH_Handle *h)
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
799 h->in_receive = GNUNET_NO;
800 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
801 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
806 /******************************************************************************/
807 /*********************** RECEIVE HANDLERS ****************************/
808 /******************************************************************************/
811 * Process the new tunnel notification and add it to the tunnels in the handle
813 * @param h The mesh handle
814 * @param msg A message with the details of the new incoming tunnel
817 process_tunnel_created (struct GNUNET_MESH_Handle *h,
818 const struct GNUNET_MESH_TunnelNotification *msg)
820 struct GNUNET_MESH_Tunnel *t;
821 MESH_TunnelNumber tid;
823 tid = ntohl (msg->tunnel_id);
824 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
825 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
830 if (NULL != h->new_tunnel)
832 t = create_tunnel (h, tid);
833 t->last_ack_sent = 0;
834 t->peer = GNUNET_PEER_intern (&msg->peer);
837 t->port = ntohl (msg->port);
838 if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
839 t->buffering = GNUNET_NO;
841 t->buffering = GNUNET_YES;
842 if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
843 t->reliable = GNUNET_YES;
845 t->reliable = GNUNET_NO;
846 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
847 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
848 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
852 struct GNUNET_MESH_TunnelMessage d_msg;
854 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
856 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
857 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
858 d_msg.tunnel_id = msg->tunnel_id;
859 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
863 send_packet (h, &d_msg.header, NULL);
870 * Process the tunnel destroy notification and free associated resources
872 * @param h The mesh handle
873 * @param msg A message with the details of the tunnel being destroyed
876 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
877 const struct GNUNET_MESH_TunnelMessage *msg)
879 struct GNUNET_MESH_Tunnel *t;
880 MESH_TunnelNumber tid;
882 tid = ntohl (msg->tunnel_id);
883 t = retrieve_tunnel (h, tid);
889 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
890 destroy_tunnel (t, GNUNET_YES);
896 * Process the incoming data packets, call appropriate handlers.
898 * @param h The mesh handle
899 * @param message A message encapsulating the data
902 process_incoming_data (struct GNUNET_MESH_Handle *h,
903 const struct GNUNET_MessageHeader *message)
905 const struct GNUNET_MessageHeader *payload;
906 const struct GNUNET_MESH_MessageHandler *handler;
907 const struct GNUNET_PeerIdentity *peer;
908 struct GNUNET_PeerIdentity id;
909 struct GNUNET_MESH_Data *dmsg;
910 struct GNUNET_MESH_Tunnel *t;
915 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
916 type = ntohs (message->type);
919 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
920 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
921 dmsg = (struct GNUNET_MESH_Data *) message;
923 t = retrieve_tunnel (h, ntohl (dmsg->tid));
924 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
925 GNUNET_PEER_resolve (t->peer, &id);
927 pid = ntohl (dmsg->pid);
928 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on tunnel %s [%X]\n",
929 type == GNUNET_MESSAGE_TYPE_MESH_UNICAST ? "fwd" : "bck",
930 GNUNET_i2s (peer), ntohl (dmsg->tid));
936 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", pid);
939 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
940 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
944 GMC_is_pid_bigger(pid, t->last_ack_sent))
947 LOG (GNUNET_ERROR_TYPE_DEBUG,
948 " unauthorized message! (%u, ACK %u)\n",
949 pid, t->last_ack_sent);
950 // FIXME fc what now? accept? reject?
952 t->last_pid_recv = pid;
953 type = ntohs (payload->type);
954 for (i = 0; i < h->n_handlers; i++)
956 handler = &h->message_handlers[i];
957 if (handler->type == type)
960 handler->callback (h->cls, t, &t->ctx, payload))
962 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
963 GNUNET_MESH_tunnel_destroy (t);
968 LOG (GNUNET_ERROR_TYPE_DEBUG,
969 "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;
989 MESH_TunnelNumber tid;
992 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
994 msg = (struct GNUNET_MESH_LocalAck *) message;
995 tid = ntohl (msg->tunnel_id);
996 t = retrieve_tunnel (h, tid);
999 LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown tunnel %X\n", tid);
1002 ack = ntohl (msg->ack);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X, ack %u!\n", t->tid, ack);
1004 if (GNUNET_YES == GMC_is_pid_bigger(ack, t->last_ack_recv))
1005 t->last_ack_recv = ack;
1008 if (NULL == h->th && 0 < t->packet_size)
1010 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n", t->tid, ack);
1012 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
1013 GNUNET_TIME_UNIT_FOREVER_REL,
1014 GNUNET_YES, &send_callback, h);
1020 * Process a local reply about info on all tunnels, pass info to the user.
1022 * @param h Mesh handle.
1023 * @param message Message itself.
1026 process_get_tunnels (struct GNUNET_MESH_Handle *h,
1027 const struct GNUNET_MessageHeader *message)
1029 struct GNUNET_MESH_LocalMonitor *msg;
1031 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
1033 if (NULL == h->tunnels_cb)
1035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1039 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1040 if (ntohs (message->size) !=
1041 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1042 sizeof (struct GNUNET_PeerIdentity)))
1044 GNUNET_break_op (0);
1045 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1046 "Get tunnels message: size %hu - expected %u\n",
1047 ntohs (message->size),
1048 sizeof (struct GNUNET_MESH_LocalMonitor));
1051 h->tunnels_cb (h->tunnels_cls,
1052 ntohl (msg->tunnel_id),
1060 * Process a local monitor_tunnel reply, pass info to the user.
1062 * @param h Mesh handle.
1063 * @param message Message itself.
1066 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1067 const struct GNUNET_MessageHeader *message)
1069 struct GNUNET_MESH_LocalMonitor *msg;
1072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1074 if (NULL == h->tunnel_cb)
1076 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1080 /* Verify message sanity */
1081 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1082 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1083 if (ntohs (message->size) != esize)
1085 GNUNET_break_op (0);
1086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1087 "Show tunnel message: size %hu - expected %u\n",
1088 ntohs (message->size),
1091 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1092 h->tunnel_cb = NULL;
1093 h->tunnel_cls = NULL;
1098 h->tunnel_cb (h->tunnel_cls,
1105 * Function to process all messages received from the service
1107 * @param cls closure
1108 * @param msg message received, NULL on timeout or fatal error
1111 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1113 struct GNUNET_MESH_Handle *h = cls;
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Mesh service disconnected, reconnecting\n", h);
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1123 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1125 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1126 switch (ntohs (msg->type))
1128 /* Notify of a new incoming tunnel */
1129 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1130 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1132 /* Notify of a tunnel disconnection */
1133 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1134 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1136 /* Notify of a new data packet in the tunnel */
1137 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1138 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1139 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 struct GNUNET_MESH_Data *dmsg;
1214 struct GNUNET_MessageHeader *mh;
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1217 if (GNUNET_NO == GMC_is_pid_bigger (t->last_ack_recv, t->last_pid_sent))
1219 /* This tunnel is not ready to transmit yet, try next message */
1224 GNUNET_assert (size >= th->size);
1225 dmsg = (struct GNUNET_MESH_Data *) cbuf;
1226 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1227 psize = th->notify (th->notify_cls,
1228 size - sizeof (struct GNUNET_MESH_Data),
1232 psize += sizeof (struct GNUNET_MESH_Data);
1233 GNUNET_assert (size >= psize);
1234 dmsg->header.size = htons (psize);
1235 dmsg->tid = htonl (t->tid);
1236 dmsg->pid = htonl (t->last_pid_sent + 1);
1238 memset (&dmsg->oid, 0, sizeof (struct GNUNET_PeerIdentity));
1241 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1243 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, "# to origin, type %s\n",
1245 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1249 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1250 LOG (GNUNET_ERROR_TYPE_DEBUG, "# unicast, type %s\n",
1251 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1256 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh traffic, type %s\n",
1259 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1260 memcpy (cbuf, &th[1], th->size);
1263 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1264 GNUNET_SCHEDULER_cancel (th->timeout_task);
1265 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1268 nsize = message_ready_size (h);
1273 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1275 size = message_ready_size (h);
1278 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1280 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1281 GNUNET_TIME_UNIT_FOREVER_REL,
1282 GNUNET_YES, &send_callback, h);
1286 if (NULL != h->th_head)
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1289 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1291 if (GNUNET_NO == h->in_receive)
1293 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1294 h->in_receive = GNUNET_YES;
1295 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1296 GNUNET_TIME_UNIT_FOREVER_REL);
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1304 * Auxiliary function to send an already constructed packet to the service.
1305 * Takes care of creating a new queue element, copying the message and
1306 * calling the tmt_rdy function if necessary.
1308 * @param h mesh handle
1309 * @param msg message to transmit
1310 * @param tunnel tunnel this send is related to (NULL if N/A)
1313 send_packet (struct GNUNET_MESH_Handle *h,
1314 const struct GNUNET_MessageHeader *msg,
1315 struct GNUNET_MESH_Tunnel *tunnel)
1317 struct GNUNET_MESH_TransmitHandle *th;
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1321 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1322 msize = ntohs (msg->size);
1323 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1324 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1326 th->tunnel = tunnel;
1327 memcpy (&th[1], msg, msize);
1328 add_to_queue (h, th);
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1334 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1335 GNUNET_TIME_UNIT_FOREVER_REL,
1336 GNUNET_YES, &send_callback, h);
1340 /******************************************************************************/
1341 /********************** API CALL DEFINITIONS *************************/
1342 /******************************************************************************/
1344 struct GNUNET_MESH_Handle *
1345 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1346 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1347 GNUNET_MESH_TunnelEndHandler cleaner,
1348 const struct GNUNET_MESH_MessageHandler *handlers,
1349 const uint32_t *ports)
1351 struct GNUNET_MESH_Handle *h;
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1354 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1357 h->new_tunnel = new_tunnel;
1358 h->cleaner = cleaner;
1359 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1360 if (h->client == NULL)
1367 h->message_handlers = handlers;
1369 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1370 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1371 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1373 /* count handlers */
1374 for (h->n_handlers = 0;
1375 handlers && handlers[h->n_handlers].type;
1377 for (h->n_ports = 0;
1378 ports && ports[h->n_ports];
1381 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1387 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1389 struct GNUNET_MESH_Tunnel *t;
1390 struct GNUNET_MESH_Tunnel *aux;
1391 struct GNUNET_MESH_TransmitHandle *th;
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1396 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1397 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1400 t = handle->tunnels_head;
1404 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1407 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1409 destroy_tunnel (t, GNUNET_YES);
1412 while ( (th = handle->th_head) != NULL)
1414 struct GNUNET_MessageHeader *msg;
1416 /* Make sure it is an allowed packet (everything else should have been
1417 * already canceled).
1419 GNUNET_break (GNUNET_NO == th_is_payload (th));
1420 msg = (struct GNUNET_MessageHeader *) &th[1];
1421 switch (ntohs(msg->type))
1423 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1424 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1425 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1426 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1430 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1434 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1438 if (NULL != handle->th)
1440 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1443 if (NULL != handle->client)
1445 GNUNET_CLIENT_disconnect (handle->client);
1446 handle->client = NULL;
1448 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1450 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1451 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1453 GNUNET_free (handle);
1458 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1459 * and to broadcast).
1461 * @param h mesh handle
1462 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1463 * @param peer peer identity the tunnel should go to
1464 * @param port Port number.
1465 * @param buffer Flag for buffering on relay nodes.
1466 * @param reliable Flag for end-to-end reliability.
1468 * @return handle to the tunnel
1470 struct GNUNET_MESH_Tunnel *
1471 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1473 const struct GNUNET_PeerIdentity *peer,
1478 struct GNUNET_MESH_Tunnel *t;
1479 struct GNUNET_MESH_TunnelMessage msg;
1481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1482 t = create_tunnel (h, 0);
1483 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1484 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1485 t->ctx = tunnel_ctx;
1486 t->peer = GNUNET_PEER_intern (peer);
1487 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1488 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1489 msg.tunnel_id = htonl (t->tid);
1490 msg.port = htonl (port);
1492 t->last_ack_sent = 0;
1493 send_packet (h, &msg.header, t);
1499 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1501 struct GNUNET_MESH_Handle *h;
1502 struct GNUNET_MESH_TunnelMessage msg;
1503 struct GNUNET_MESH_TransmitHandle *th;
1505 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1508 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1509 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1510 msg.tunnel_id = htonl (tunnel->tid);
1511 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1517 struct GNUNET_MESH_TransmitHandle *aux;
1518 if (th->tunnel == tunnel)
1521 /* FIXME call the handler? */
1522 if (GNUNET_YES == th_is_payload (th))
1523 th->notify (th->notify_cls, 0, NULL);
1524 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1532 destroy_tunnel (tunnel, GNUNET_YES);
1533 send_packet (h, &msg.header, NULL);
1538 * Get information about a tunnel.
1540 * The existing end callback for the tunnel will be called immediately.
1541 * Any pending outgoing messages will be sent but no incoming messages will be
1542 * accepted and no data callbacks will be called.
1544 * @param tunnel Tunnel handle.
1546 * @return Allocated, {0, NULL} terminated set of tunnel properties.
1548 struct MeshTunnelInfo *
1549 GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel)
1551 struct MeshTunnelInfo *ret;
1553 ret = GNUNET_malloc (sizeof (struct MeshTunnelInfo) * 3);
1554 ret[0].prop = GNUNET_MESH_OPTION_NOBUFFER;
1555 ret[0].value = &tunnel->buffering; // FIXME return ¬buffering ("nobuffer")
1556 ret[1].prop = GNUNET_MESH_OPTION_RELIABLE;
1557 ret[1].value = &tunnel->reliable;
1559 ret[2].value = NULL;
1564 struct GNUNET_MESH_TransmitHandle *
1565 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1566 struct GNUNET_TIME_Relative maxdelay,
1568 GNUNET_CONNECTION_TransmitReadyNotify notify,
1571 struct GNUNET_MESH_TransmitHandle *th;
1573 GNUNET_assert (NULL != tunnel);
1574 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1575 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
1576 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1577 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1581 GNUNET_assert (NULL != notify);
1582 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
1583 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1584 th->tunnel = tunnel;
1585 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1586 th->size = notify_size + sizeof (struct GNUNET_MESH_Data);
1587 tunnel->packet_size = th->size;
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1589 th->notify = notify;
1590 th->notify_cls = notify_cls;
1591 add_to_queue (tunnel->mesh, th);
1592 if (NULL != tunnel->mesh->th)
1594 if (!GMC_is_pid_bigger (tunnel->last_ack_recv, tunnel->last_pid_sent))
1596 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1598 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1599 GNUNET_TIME_UNIT_FOREVER_REL,
1600 GNUNET_YES, &send_callback,
1602 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1608 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1610 struct GNUNET_MESH_Handle *mesh;
1612 th->tunnel->packet_size = 0;
1613 mesh = th->tunnel->mesh;
1614 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1615 GNUNET_SCHEDULER_cancel (th->timeout_task);
1616 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1618 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1620 /* queue empty, no point in asking for transmission */
1621 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1627 GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel)
1634 * Request information about the running mesh peer.
1635 * The callback will be called for every tunnel known to the service,
1636 * listing all active peers that blong to the tunnel.
1638 * If called again on the same handle, it will overwrite the previous
1639 * callback and cls. To retrieve the cls, monitor_cancel must be
1642 * WARNING: unstable API, likely to change in the future!
1644 * @param h Handle to the mesh peer.
1645 * @param callback Function to call with the requested data.
1646 * @param callback_cls Closure for @c callback.
1649 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1650 GNUNET_MESH_TunnelsCB callback,
1653 struct GNUNET_MessageHeader msg;
1655 msg.size = htons (sizeof (msg));
1656 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1657 send_packet (h, &msg, NULL);
1658 h->tunnels_cb = callback;
1659 h->tunnels_cls = callback_cls;
1666 * Cancel a monitor request. The monitor callback will not be called.
1668 * @param h Mesh handle.
1670 * @return Closure given to GNUNET_MESH_monitor, if any.
1673 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1677 cls = h->tunnels_cls;
1678 h->tunnels_cb = NULL;
1679 h->tunnels_cls = NULL;
1685 * Request information about a specific tunnel of the running mesh peer.
1687 * WARNING: unstable API, likely to change in the future!
1688 * FIXME Add destination option.
1690 * @param h Handle to the mesh peer.
1691 * @param initiator ID of the owner of the tunnel.
1692 * @param tunnel_number Tunnel number.
1693 * @param callback Function to call with the requested data.
1694 * @param callback_cls Closure for @c callback.
1697 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
1698 struct GNUNET_PeerIdentity *initiator,
1699 unsigned int tunnel_number,
1700 GNUNET_MESH_TunnelCB callback,
1703 struct GNUNET_MESH_LocalMonitor msg;
1705 msg.header.size = htons (sizeof (msg));
1706 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1707 msg.owner = *initiator;
1708 msg.tunnel_id = htonl (tunnel_number);
1710 send_packet (h, &msg.header, NULL);
1711 h->tunnel_cb = callback;
1712 h->tunnel_cls = callback_cls;
1719 * Function called to notify a client about the connection
1720 * begin ready to queue more data. "buf" will be
1721 * NULL and "size" zero if the connection was closed for
1722 * writing in the meantime.
1724 * @param cls closure
1725 * @param size number of bytes available in buf
1726 * @param buf where the callee should write the message
1727 * @return number of bytes written to buf
1730 mesh_mq_ntr (void *cls, size_t size,
1733 struct GNUNET_MQ_Handle *mq = cls;
1734 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1735 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1741 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1744 msize = ntohs (msg->size);
1745 GNUNET_assert (msize <= size);
1746 memcpy (buf, msg, msize);
1747 GNUNET_MQ_impl_send_continue (mq);
1753 * Signature of functions implementing the
1754 * sending functionality of a message queue.
1756 * @param mq the message queue
1757 * @param msg the message to send
1758 * @param impl_state state of the implementation
1761 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1762 const struct GNUNET_MessageHeader *msg, void *impl_state)
1764 struct MeshMQState *state = impl_state;
1766 GNUNET_assert (NULL == state->th);
1767 GNUNET_MQ_impl_send_commit (mq);
1769 GNUNET_MESH_notify_transmit_ready (state->tunnel,
1770 /* FIXME: add option for corking */
1772 GNUNET_TIME_UNIT_FOREVER_REL,
1780 * Signature of functions implementing the
1781 * destruction of a message queue.
1782 * Implementations must not free 'mq', but should
1783 * take care of 'impl_state'.
1785 * @param mq the message queue to destroy
1786 * @param impl_state state of the implementation
1789 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1791 struct MeshMQState *state = impl_state;
1793 if (NULL != state->th)
1794 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1796 GNUNET_free (state);
1801 * Create a message queue for a mesh tunnel.
1802 * The message queue can only be used to transmit messages,
1803 * not to receive them.
1805 * @param tunnel the tunnel to create the message qeue for
1806 * @return a message queue to messages over the tunnel
1808 struct GNUNET_MQ_Handle *
1809 GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel)
1811 struct GNUNET_MQ_Handle *mq;
1812 struct MeshMQState *state;
1814 state = GNUNET_new (struct MeshMQState);
1815 state->tunnel = tunnel;
1817 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
1818 mesh_mq_destroy_impl,
1819 NULL, /* FIXME: cancel impl. */
1821 NULL, /* no msg handlers */
1822 NULL, /* no err handlers */
1823 NULL); /* no handler cls */