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 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__)
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 * Target of the message, 0 for multicast. This field
99 * is only valid if 'notify' is non-NULL.
101 GNUNET_PEER_Id target;
104 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
111 * Opaque handle to the service.
113 struct GNUNET_MESH_Handle
117 * Handle to the server connection, to send messages later
119 struct GNUNET_CLIENT_Connection *client;
122 * Set of handlers used for processing incoming messages in the tunnels
124 const struct GNUNET_MESH_MessageHandler *message_handlers;
127 * Set of applications that should be claimed to be offered at this node.
128 * Note that this is just informative, the appropiate handlers must be
129 * registered independently and the mapping is up to the developer of the
130 * client application.
132 const GNUNET_MESH_ApplicationType *applications;
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 * Number of handlers in the handlers array.
182 unsigned int n_handlers;
185 * Number of applications in the applications array.
187 unsigned int n_applications;
190 * Have we started the task to receive messages from the service
191 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
196 * Configuration given by the client, in case of reconnection
198 const struct GNUNET_CONFIGURATION_Handle *cfg;
201 * Time to the next reconnect in case one reconnect fails
203 struct GNUNET_TIME_Relative reconnect_time;
206 * Task for trying to reconnect.
208 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
213 GNUNET_MESH_MonitorCB monitor_cb;
216 * Monitor 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 * Callback to execute when peers connect to the tunnel
269 GNUNET_MESH_PeerConnectHandler connect_handler;
272 * Callback to execute when peers disconnect from the tunnel
274 GNUNET_MESH_PeerDisconnectHandler disconnect_handler;
277 * Closure for the connect/disconnect handlers
282 * Handle to the mesh this tunnel belongs to
284 struct GNUNET_MESH_Handle *mesh;
287 * Local ID of the tunnel
289 MESH_TunnelNumber tid;
292 * Owner of the tunnel. 0 if the tunnel is the local client.
294 GNUNET_PEER_Id owner;
297 * All peers added to the tunnel
299 struct GNUNET_MESH_Peer **peers;
302 * List of application types that have been requested for this tunnel
304 GNUNET_MESH_ApplicationType *apps;
307 * Any data the caller wants to put in here
312 * Number of peers added to the tunnel
317 * Size of packet queued in this tunnel
319 unsigned int packet_size;
322 * Number of applications requested this tunnel
327 * Is the tunnel throttled to the slowest peer?
332 * Is the tunnel allowed to buffer?
337 * Next packet ID to send.
339 uint32_t next_send_pid;
342 * Maximum allowed PID to send (ACK recevied).
344 uint32_t max_send_pid;
347 * Last pid received from the service.
349 uint32_t last_recv_pid;
352 * Which ACK value have we last sent to the service?
354 uint32_t max_recv_pid;
358 /******************************************************************************/
359 /*********************** DECLARATIONS *************************/
360 /******************************************************************************/
363 * Function called to send a message to the service.
364 * "buf" will be NULL and "size" zero if the socket was closed for writing in
367 * @param cls closure, the mesh handle
368 * @param size number of bytes available in buf
369 * @param buf where the callee should write the connect message
370 * @return number of bytes written to buf
373 send_callback (void *cls, size_t size, void *buf);
376 /******************************************************************************/
377 /*********************** AUXILIARY FUNCTIONS *************************/
378 /******************************************************************************/
381 * Check if transmission is a payload packet.
383 * @param th Transmission handle.
385 * @return GNUNET_YES if it is a payload packet,
386 * GNUNET_NO if it is a mesh management packet.
389 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
391 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
396 * Check whether there is any message ready in the queue and find the size.
398 * @param h Mesh handle.
400 * @return The size of the first ready message in the queue,
401 * 0 if there is none.
404 message_ready_size (struct GNUNET_MESH_Handle *h)
406 struct GNUNET_MESH_TransmitHandle *th;
407 struct GNUNET_MESH_Tunnel *t;
409 for (th = h->th_head; NULL != th; th = th->next)
412 if (GNUNET_NO == th_is_payload (th))
414 LOG (GNUNET_ERROR_TYPE_DEBUG, " message internal\n");
417 if (GNUNET_NO == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
419 LOG (GNUNET_ERROR_TYPE_DEBUG, " message payload ok (%u <= %u)\n",
420 t->next_send_pid, t->max_send_pid);
429 * Get the tunnel handler for the tunnel specified by id from the given handle
430 * @param h Mesh handle
431 * @param tid ID of the wanted tunnel
432 * @return handle to the required tunnel or NULL if not found
434 static struct GNUNET_MESH_Tunnel *
435 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
437 struct GNUNET_MESH_Tunnel *t;
451 * Create a new tunnel and insert it in the tunnel list of the mesh handle
452 * @param h Mesh handle
453 * @param tid desired tid of the tunnel, 0 to assign one automatically
454 * @return handle to the created tunnel
456 static struct GNUNET_MESH_Tunnel *
457 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
459 struct GNUNET_MESH_Tunnel *t;
461 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
462 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
466 t->tid = h->next_tid;
467 while (NULL != retrieve_tunnel (h, h->next_tid))
470 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
471 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
478 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
479 t->last_recv_pid = (uint32_t) -1;
480 t->buffering = GNUNET_YES;
486 * Destroy the specified tunnel.
487 * - Destroys all peers, calling the disconnect callback on each if needed
488 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
489 * - Calls cleaner if tunnel was inbound
490 * - Frees all memory used
492 * @param t Pointer to the tunnel.
493 * @param call_cleaner Whether to call the cleaner handler.
495 * @return Handle to the required tunnel or NULL if not found.
498 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
500 struct GNUNET_MESH_Handle *h;
501 struct GNUNET_PeerIdentity pi;
502 struct GNUNET_MESH_TransmitHandle *th;
503 struct GNUNET_MESH_TransmitHandle *next;
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
515 /* disconnect all peers */
516 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
517 for (i = 0; i < t->npeers; i++)
519 if ( (NULL != t->disconnect_handler) && t->peers[i]->connected)
521 GNUNET_PEER_resolve (t->peers[i]->id, &pi);
522 t->disconnect_handler (t->cls, &pi);
524 GNUNET_PEER_change_rc (t->peers[i]->id, -1);
525 GNUNET_free (t->peers[i]);
528 /* signal tunnel destruction */
529 if ( (NULL != h->cleaner) && (0 != t->owner) && (GNUNET_YES == call_cleaner) )
530 h->cleaner (h->cls, t, t->ctx);
532 /* check that clients did not leave messages behind in the queue */
533 for (th = h->th_head; NULL != th; th = next)
538 /* Clients should have aborted their requests already.
539 * Management traffic should be ok, as clients can't cancel that */
540 GNUNET_break (GNUNET_NO == th_is_payload(th));
541 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
543 /* clean up request */
544 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
545 GNUNET_SCHEDULER_cancel (th->timeout_task);
549 /* if there are no more pending requests with mesh service, cancel active request */
550 /* Note: this should be unnecessary... */
551 if ((0 == message_ready_size (h)) && (NULL != h->th))
553 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
559 GNUNET_free (t->peers);
561 GNUNET_PEER_change_rc (t->owner, -1);
562 if (0 != t->napps && t->apps)
563 GNUNET_free (t->apps);
570 * Get the peer descriptor for the peer with id from the given tunnel
571 * @param t Tunnel handle
572 * @param id Short form ID of the wanted peer
573 * @return handle to the requested peer or NULL if not found
575 static struct GNUNET_MESH_Peer *
576 retrieve_peer (struct GNUNET_MESH_Tunnel *t, GNUNET_PEER_Id id)
580 for (i = 0; i < t->npeers; i++)
581 if (t->peers[i]->id == id)
588 * Add a peer into a tunnel
589 * @param t Tunnel handle
590 * @param pi Full ID of the new peer
591 * @return handle to the newly created peer
593 static struct GNUNET_MESH_Peer *
594 add_peer_to_tunnel (struct GNUNET_MESH_Tunnel *t,
595 const struct GNUNET_PeerIdentity *pi)
597 struct GNUNET_MESH_Peer *p;
605 id = GNUNET_PEER_intern (pi);
607 p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
610 GNUNET_array_append (t->peers, t->npeers, p);
616 * Remove a peer from a tunnel
617 * @param p Peer handle
620 remove_peer_from_tunnel (struct GNUNET_MESH_Peer *p)
624 for (i = 0; i < p->t->npeers; i++)
626 if (p->t->peers[i] == p)
629 if (i == p->t->npeers)
634 p->t->peers[i] = p->t->peers[p->t->npeers - 1];
635 GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
640 * Notify client that the transmission has timed out
643 * @param tc task context
646 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
648 struct GNUNET_MESH_TransmitHandle *th = cls;
649 struct GNUNET_MESH_Handle *mesh;
651 mesh = th->tunnel->mesh;
652 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
653 th->tunnel->packet_size = 0;
654 if (GNUNET_YES == th_is_payload (th))
655 th->notify (th->notify_cls, 0, NULL);
657 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
659 /* nothing ready to transmit, no point in asking for transmission */
660 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
667 * Add a transmit handle to the transmission queue and set the
670 * @param h mesh handle with the queue head and tail
671 * @param th handle to the packet to be transmitted
674 add_to_queue (struct GNUNET_MESH_Handle *h,
675 struct GNUNET_MESH_TransmitHandle *th)
677 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
678 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
681 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
682 (th->timeout), &timeout_transmission, th);
687 * Auxiliary function to send an already constructed packet to the service.
688 * Takes care of creating a new queue element, copying the message and
689 * calling the tmt_rdy function if necessary.
691 * @param h mesh handle
692 * @param msg message to transmit
693 * @param tunnel tunnel this send is related to (NULL if N/A)
696 send_packet (struct GNUNET_MESH_Handle *h,
697 const struct GNUNET_MessageHeader *msg,
698 struct GNUNET_MESH_Tunnel *tunnel);
702 * Send an ack on the tunnel to confirm the processing of a message.
704 * @param h Mesh handle.
705 * @param t Tunnel on which to send the ACK.
708 send_ack (struct GNUNET_MESH_Handle *h, struct GNUNET_MESH_Tunnel *t)
710 struct GNUNET_MESH_LocalAck msg;
713 delta = t->max_recv_pid - t->last_recv_pid;
714 if (delta > ACK_THRESHOLD)
716 LOG (GNUNET_ERROR_TYPE_DEBUG,
717 "Not sending ACK on tunnel %X: ACK: %u, PID: %u, buffer %u\n",
718 t->tid, t->max_recv_pid, t->last_recv_pid, delta);
721 if (GNUNET_YES == t->buffering)
722 t->max_recv_pid = t->last_recv_pid + INITIAL_WINDOW_SIZE;
724 t->max_recv_pid = t->last_recv_pid + 1;
725 LOG (GNUNET_ERROR_TYPE_DEBUG,
726 "Sending ACK on tunnel %X: %u\n",
727 t->tid, t->max_recv_pid);
728 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
729 msg.header.size = htons (sizeof (msg));
730 msg.tunnel_id = htonl (t->tid);
731 msg.max_pid = htonl (t->max_recv_pid);
734 t->mesh->acks_sent++;
737 send_packet (h, &msg.header, t);
744 * Reconnect callback: tries to reconnect again after a failer previous
746 * @param cls closure (mesh handle)
747 * @param tc task context
750 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
754 * Send a connect packet to the service with the applications and types
755 * requested by the user.
757 * @param h The mesh handle.
761 send_connect (struct GNUNET_MESH_Handle *h)
765 size = sizeof (struct GNUNET_MESH_ClientConnect);
766 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
767 size += h->n_handlers * sizeof (uint16_t);
769 char buf[size] GNUNET_ALIGN;
770 struct GNUNET_MESH_ClientConnect *msg;
771 GNUNET_MESH_ApplicationType *apps;
776 /* build connection packet */
777 msg = (struct GNUNET_MESH_ClientConnect *) buf;
778 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
779 msg->header.size = htons (size);
780 apps = (GNUNET_MESH_ApplicationType *) &msg[1];
781 for (napps = 0; napps < h->n_applications; napps++)
783 apps[napps] = htonl (h->applications[napps]);
784 LOG (GNUNET_ERROR_TYPE_DEBUG, " app %u\n",
785 h->applications[napps]);
787 types = (uint16_t *) & apps[napps];
788 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
790 types[ntypes] = htons (h->message_handlers[ntypes].type);
791 LOG (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
792 h->message_handlers[ntypes].type);
794 msg->applications = htons (napps);
795 msg->types = htons (ntypes);
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Sending %lu bytes long message %d types and %d apps\n",
798 ntohs (msg->header.size), ntypes, napps);
799 send_packet (h, &msg->header, NULL);
805 * Reconnect to the service, retransmit all infomation to try to restore the
808 * @param h handle to the mesh
810 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
813 do_reconnect (struct GNUNET_MESH_Handle *h)
815 struct GNUNET_MESH_Tunnel *t;
818 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
819 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
820 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
821 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
822 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
827 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
830 if (NULL != h->client)
832 GNUNET_CLIENT_disconnect (h->client);
836 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
837 if (h->client == NULL)
839 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
842 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
843 GNUNET_TIME_relative_multiply
844 (h->reconnect_time, 2));
845 LOG (GNUNET_ERROR_TYPE_DEBUG,
846 "Next retry in %s\n",
847 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
854 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
857 /* Rebuild all tunnels */
858 for (t = h->tunnels_head; NULL != t; t = t->next)
860 struct GNUNET_MESH_TunnelMessage tmsg;
861 struct GNUNET_MESH_PeerControl pmsg;
863 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
865 /* Tunnel was created by service (incoming tunnel) */
866 /* TODO: Notify service of missing tunnel, to request
867 * creator to recreate path (find a path to him via DHT?)
871 t->next_send_pid = 0;
872 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
873 t->last_recv_pid = (uint32_t) -1;
874 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
875 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
876 tmsg.tunnel_id = htonl (t->tid);
877 send_packet (h, &tmsg.header, t);
879 pmsg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
880 pmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
881 pmsg.tunnel_id = htonl (t->tid);
883 /* Reconnect all peers */
884 /* If the tunnel was "by type", dont connect individual peers */
885 for (i = 0; i < t->npeers && 0 == t->napps; i++)
887 GNUNET_PEER_resolve (t->peers[i]->id, &pmsg.peer);
888 if (NULL != t->disconnect_handler && t->peers[i]->connected)
889 t->disconnect_handler (t->cls, &pmsg.peer);
890 send_packet (t->mesh, &pmsg.header, t);
892 /* Reconnect all types, if any */
893 for (i = 0; i < t->napps; i++)
895 struct GNUNET_MESH_ConnectPeerByType msg;
897 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
898 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
899 msg.tunnel_id = htonl (t->tid);
900 msg.type = htonl (t->apps[i]);
901 send_packet (t->mesh, &msg.header, t);
903 if (GNUNET_NO == t->buffering)
904 GNUNET_MESH_tunnel_buffer (t, GNUNET_NO);
905 if (GNUNET_YES == t->speed_min)
906 GNUNET_MESH_tunnel_speed_min (t);
912 * Reconnect callback: tries to reconnect again after a failer previous
914 * @param cls closure (mesh handle)
915 * @param tc task context
918 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
920 struct GNUNET_MESH_Handle *h = cls;
922 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
923 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
930 * Reconnect to the service, retransmit all infomation to try to restore the
933 * @param h handle to the mesh
935 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
938 reconnect (struct GNUNET_MESH_Handle *h)
940 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
941 h->in_receive = GNUNET_NO;
942 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
943 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
948 /******************************************************************************/
949 /*********************** RECEIVE HANDLERS ****************************/
950 /******************************************************************************/
953 * Process the new tunnel notification and add it to the tunnels in the handle
955 * @param h The mesh handle
956 * @param msg A message with the details of the new incoming tunnel
959 process_tunnel_created (struct GNUNET_MESH_Handle *h,
960 const struct GNUNET_MESH_TunnelNotification *msg)
962 struct GNUNET_MESH_Tunnel *t;
963 MESH_TunnelNumber tid;
965 tid = ntohl (msg->tunnel_id);
966 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
967 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
972 if (NULL != h->new_tunnel)
974 struct GNUNET_ATS_Information atsi;
976 t = create_tunnel (h, tid);
977 t->owner = GNUNET_PEER_intern (&msg->peer);
979 t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
980 t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
982 t->peers[0]->connected = 1;
983 t->peers[0]->id = t->owner;
984 GNUNET_PEER_change_rc (t->owner, 1);
987 if ((msg->opt & MESH_TUNNEL_OPT_NOBUFFER) != 0)
988 t->buffering = GNUNET_NO;
990 t->buffering = GNUNET_YES;
991 if ((msg->opt & MESH_TUNNEL_OPT_SPEED_MIN) != 0)
992 t->speed_min = GNUNET_YES;
995 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
996 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, &atsi);
997 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
1001 struct GNUNET_MESH_TunnelMessage d_msg;
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
1005 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1006 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1007 d_msg.tunnel_id = msg->tunnel_id;
1009 send_packet (h, &d_msg.header, NULL);
1016 * Process the tunnel destroy notification and free associated resources
1018 * @param h The mesh handle
1019 * @param msg A message with the details of the tunnel being destroyed
1022 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
1023 const struct GNUNET_MESH_TunnelMessage *msg)
1025 struct GNUNET_MESH_Tunnel *t;
1026 MESH_TunnelNumber tid;
1028 tid = ntohl (msg->tunnel_id);
1029 t = retrieve_tunnel (h, tid);
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
1040 destroy_tunnel (t, GNUNET_YES);
1046 * Process the new peer event and notify the upper level of it
1048 * @param h The mesh handle
1049 * @param msg A message with the details of the peer event
1052 process_peer_event (struct GNUNET_MESH_Handle *h,
1053 const struct GNUNET_MESH_PeerControl *msg)
1055 struct GNUNET_MESH_Tunnel *t;
1056 struct GNUNET_MESH_Peer *p;
1057 struct GNUNET_ATS_Information atsi;
1061 LOG (GNUNET_ERROR_TYPE_DEBUG, "processig peer event\n");
1062 size = ntohs (msg->header.size);
1063 if (size != sizeof (struct GNUNET_MESH_PeerControl))
1068 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
1074 id = GNUNET_PEER_search (&msg->peer);
1075 if ((p = retrieve_peer (t, id)) == NULL)
1076 p = add_peer_to_tunnel (t, &msg->peer);
1077 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == ntohs (msg->header.type))
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding peer\n");
1080 if (NULL != t->connect_handler)
1084 t->connect_handler (t->cls, &msg->peer, &atsi);
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing peer\n");
1091 if (NULL != t->disconnect_handler && p->connected)
1093 t->disconnect_handler (t->cls, &msg->peer);
1095 remove_peer_from_tunnel (p);
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "processing peer event END\n");
1103 * Process the incoming data packets
1105 * @param h The mesh handle
1106 * @param message A message encapsulating the data
1108 * @return GNUNET_YES if everything went fine
1109 * GNUNET_NO if client closed connection (h no longer valid)
1112 process_incoming_data (struct GNUNET_MESH_Handle *h,
1113 const struct GNUNET_MessageHeader *message)
1115 const struct GNUNET_MessageHeader *payload;
1116 const struct GNUNET_MESH_MessageHandler *handler;
1117 const struct GNUNET_PeerIdentity *peer;
1118 struct GNUNET_MESH_Unicast *ucast;
1119 struct GNUNET_MESH_Multicast *mcast;
1120 struct GNUNET_MESH_ToOrigin *to_orig;
1121 struct GNUNET_MESH_Tunnel *t;
1126 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
1127 type = ntohs (message->type);
1130 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1131 ucast = (struct GNUNET_MESH_Unicast *) message;
1133 t = retrieve_tunnel (h, ntohl (ucast->tid));
1134 payload = (struct GNUNET_MessageHeader *) &ucast[1];
1136 pid = ntohl (ucast->pid);
1137 LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%X]\n",
1138 GNUNET_i2s (peer), ntohl (ucast->tid));
1140 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1141 mcast = (struct GNUNET_MESH_Multicast *) message;
1142 t = retrieve_tunnel (h, ntohl (mcast->tid));
1143 payload = (struct GNUNET_MessageHeader *) &mcast[1];
1145 pid = ntohl (mcast->pid);
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " mcast on tunnel %s [%X]\n",
1147 GNUNET_i2s (peer), ntohl (mcast->tid));
1149 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1150 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
1151 t = retrieve_tunnel (h, ntohl (to_orig->tid));
1152 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
1153 peer = &to_orig->sender;
1154 pid = ntohl (to_orig->pid);
1155 LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%X]\n",
1156 GNUNET_i2s (peer), ntohl (to_orig->tid));
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", pid);
1165 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
1170 GMC_is_pid_bigger(pid, t->max_recv_pid))
1173 LOG (GNUNET_ERROR_TYPE_WARNING,
1174 " unauthorized message! (%u, max %u)\n",
1175 pid, t->max_recv_pid);
1176 // FIXME fc what now? accept? reject?
1179 t->last_recv_pid = pid;
1180 type = ntohs (payload->type);
1182 for (i = 0; i < h->n_handlers; i++)
1184 handler = &h->message_handlers[i];
1185 if (handler->type == type)
1187 struct GNUNET_ATS_Information atsi;
1192 handler->callback (h->cls, t, &t->ctx, peer, payload, &atsi))
1194 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
1195 GNUNET_MESH_disconnect (h);
1200 LOG (GNUNET_ERROR_TYPE_DEBUG,
1201 "callback completed successfully\n");
1210 * Process a local ACK message, enabling the client to send
1211 * more data to the service.
1213 * @param h Mesh handle.
1214 * @param message Message itself.
1217 process_ack (struct GNUNET_MESH_Handle *h,
1218 const struct GNUNET_MessageHeader *message)
1220 struct GNUNET_MESH_LocalAck *msg;
1221 struct GNUNET_MESH_Tunnel *t;
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
1226 msg = (struct GNUNET_MESH_LocalAck *) message;
1228 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
1232 LOG (GNUNET_ERROR_TYPE_WARNING,
1233 "ACK on unknown tunnel %X\n",
1234 ntohl (msg->tunnel_id));
1237 ack = ntohl (msg->max_pid);
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X, ack %u!\n", t->tid, ack);
1239 if (GNUNET_YES == GMC_is_pid_bigger(ack, t->max_send_pid))
1240 t->max_send_pid = ack;
1243 if (NULL == h->th && 0 < t->packet_size)
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n", t->tid, ack);
1247 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
1248 GNUNET_TIME_UNIT_FOREVER_REL,
1249 GNUNET_YES, &send_callback, h);
1255 * Process a local monitor reply, pass info to the user.
1257 * @param h Mesh handle.
1258 * @param message Message itself.
1261 process_monitor (struct GNUNET_MESH_Handle *h,
1262 const struct GNUNET_MessageHeader *message)
1264 struct GNUNET_MESH_LocalMonitor *msg;
1267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Monitor messasge received\n");
1269 if (NULL == h->monitor_cb)
1271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1275 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1276 npeers = ntohl (msg->npeers);
1277 if (ntohs (message->size) !=
1278 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1279 npeers * sizeof (struct GNUNET_PeerIdentity)))
1281 GNUNET_break_op (0);
1282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1283 "Monitor message: size %hu - expected %u (%u peers)\n",
1284 ntohs (message->size),
1285 sizeof (struct GNUNET_MESH_LocalMonitor) +
1286 npeers * sizeof (struct GNUNET_PeerIdentity),
1290 h->monitor_cb (h->monitor_cls,
1292 ntohl (msg->tunnel_id),
1293 (struct GNUNET_PeerIdentity *) &msg[1],
1299 * Function to process all messages received from the service
1301 * @param cls closure
1302 * @param msg message received, NULL on timeout or fatal error
1305 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1307 struct GNUNET_MESH_Handle *h = cls;
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 "Mesh service disconnected, reconnecting\n", h);
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1317 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1319 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1320 switch (ntohs (msg->type))
1322 /* Notify of a new incoming tunnel */
1323 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1324 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1326 /* Notify of a tunnel disconnection */
1327 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1328 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1330 /* Notify of a new peer or a peer disconnect in the tunnel */
1331 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD:
1332 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL:
1333 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
1335 /* Notify of a new data packet in the tunnel */
1336 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1337 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1338 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1339 if (GNUNET_NO == process_incoming_data (h, msg))
1342 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1343 process_ack (h, msg);
1345 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR:
1346 process_monitor (h, msg);
1349 /* We shouldn't get any other packages, log and ignore */
1350 LOG (GNUNET_ERROR_TYPE_WARNING,
1351 "unsolicited message form service (type %hu)\n",
1354 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1355 if (GNUNET_YES == h->in_receive)
1357 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1358 GNUNET_TIME_UNIT_FOREVER_REL);
1362 LOG (GNUNET_ERROR_TYPE_DEBUG,
1363 "in receive off, not calling CLIENT_receive\n");
1368 /******************************************************************************/
1369 /************************ SEND FUNCTIONS ****************************/
1370 /******************************************************************************/
1373 * Function called to send a message to the service.
1374 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1377 * @param cls closure, the mesh handle
1378 * @param size number of bytes available in buf
1379 * @param buf where the callee should write the connect message
1380 * @return number of bytes written to buf
1383 send_callback (void *cls, size_t size, void *buf)
1385 struct GNUNET_MESH_Handle *h = cls;
1386 struct GNUNET_MESH_TransmitHandle *th;
1387 struct GNUNET_MESH_TransmitHandle *next;
1388 struct GNUNET_MESH_Tunnel *t;
1394 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
1396 if ((0 == size) || (NULL == buf))
1398 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL send callback on %p\n", h);
1405 nsize = message_ready_size (h);
1406 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1409 if (GNUNET_YES == th_is_payload (th))
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload\n");
1412 if (GNUNET_YES == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
1414 /* This tunnel is not ready to transmit yet, try next message */
1419 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1421 /* traffic to origin */
1422 struct GNUNET_MESH_ToOrigin to;
1423 struct GNUNET_MessageHeader *mh;
1425 GNUNET_assert (size >= th->size);
1426 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
1427 psize = th->notify (th->notify_cls, size - sizeof (to), mh);
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin, type %s\n",
1429 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1432 psize += sizeof (to);
1433 GNUNET_assert (size >= psize);
1434 to.header.size = htons (psize);
1435 to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1436 to.tid = htonl (t->tid);
1437 to.pid = htonl (t->next_send_pid);
1439 memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1440 memset (&to.sender, 0, sizeof (struct GNUNET_PeerIdentity));
1441 memcpy (cbuf, &to, sizeof (to));
1444 else if (th->target == 0)
1447 struct GNUNET_MESH_Multicast mc;
1448 struct GNUNET_MessageHeader *mh;
1450 GNUNET_assert (size >= th->size);
1451 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (mc)];
1452 psize = th->notify (th->notify_cls, size - sizeof (mc), mh);
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, " multicast, type %s\n",
1454 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1457 psize += sizeof (mc);
1458 GNUNET_assert (size >= psize);
1459 mc.header.size = htons (psize);
1460 mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1461 mc.tid = htonl (t->tid);
1462 mc.pid = htonl (t->next_send_pid);
1464 memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1465 memcpy (cbuf, &mc, sizeof (mc));
1471 struct GNUNET_MESH_Unicast uc;
1472 struct GNUNET_MessageHeader *mh;
1474 GNUNET_assert (size >= th->size);
1475 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
1476 psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
1477 LOG (GNUNET_ERROR_TYPE_DEBUG, " unicast, type %s\n",
1478 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1481 psize += sizeof (uc);
1482 GNUNET_assert (size >= psize);
1483 uc.header.size = htons (psize);
1484 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1485 uc.tid = htonl (t->tid);
1486 uc.pid = htonl (t->next_send_pid);
1488 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1489 GNUNET_PEER_resolve (th->target, &uc.destination);
1490 memcpy (cbuf, &uc, sizeof (uc));
1497 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, " mesh traffic, type %s\n",
1500 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1501 memcpy (cbuf, &th[1], th->size);
1504 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1505 GNUNET_SCHEDULER_cancel (th->timeout_task);
1506 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1509 nsize = message_ready_size (h);
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size: %u\n", tsize);
1516 size = message_ready_size (h);
1519 LOG (GNUNET_ERROR_TYPE_DEBUG, " next size: %u\n", size);
1521 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1522 GNUNET_TIME_UNIT_FOREVER_REL,
1523 GNUNET_YES, &send_callback, h);
1527 if (NULL != h->th_head)
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " can't transmit any more\n");
1530 LOG (GNUNET_ERROR_TYPE_DEBUG, " nothing left to transmit\n");
1532 if (GNUNET_NO == h->in_receive)
1534 LOG (GNUNET_ERROR_TYPE_DEBUG, " start receiving from service\n");
1535 h->in_receive = GNUNET_YES;
1536 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1537 GNUNET_TIME_UNIT_FOREVER_REL);
1539 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
1545 * Auxiliary function to send an already constructed packet to the service.
1546 * Takes care of creating a new queue element, copying the message and
1547 * calling the tmt_rdy function if necessary.
1549 * @param h mesh handle
1550 * @param msg message to transmit
1551 * @param tunnel tunnel this send is related to (NULL if N/A)
1554 send_packet (struct GNUNET_MESH_Handle *h,
1555 const struct GNUNET_MessageHeader *msg,
1556 struct GNUNET_MESH_Tunnel *tunnel)
1558 struct GNUNET_MESH_TransmitHandle *th;
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1562 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1563 msize = ntohs (msg->size);
1564 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1565 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1567 th->tunnel = tunnel;
1568 memcpy (&th[1], msg, msize);
1569 add_to_queue (h, th);
1570 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1575 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1576 GNUNET_TIME_UNIT_FOREVER_REL,
1577 GNUNET_YES, &send_callback, h);
1581 /******************************************************************************/
1582 /********************** API CALL DEFINITIONS *************************/
1583 /******************************************************************************/
1586 * Connect to the mesh service.
1588 * @param cfg configuration to use
1589 * @param cls closure for the various callbacks that follow
1590 * (including handlers in the handlers array)
1591 * @param new_tunnel function called when an *inbound* tunnel is created
1592 * @param cleaner function called when an *inbound* tunnel is destroyed by the
1593 * remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
1594 * is called on the tunnel
1595 * @param handlers callbacks for messages we care about, NULL-terminated
1596 * note that the mesh is allowed to drop notifications about
1597 * inbound messages if the client does not process them fast
1598 * enough (for this notification type, a bounded queue is used)
1599 * @param stypes list of the applications that this client claims to provide
1600 * @return handle to the mesh service NULL on error
1601 * (in this case, init is never called)
1603 struct GNUNET_MESH_Handle *
1604 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1605 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1606 GNUNET_MESH_TunnelEndHandler cleaner,
1607 const struct GNUNET_MESH_MessageHandler *handlers,
1608 const GNUNET_MESH_ApplicationType *stypes)
1610 struct GNUNET_MESH_Handle *h;
1612 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1613 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1614 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1616 h->new_tunnel = new_tunnel;
1617 h->cleaner = cleaner;
1618 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1619 if (h->client == NULL)
1627 h->applications = stypes;
1628 h->message_handlers = handlers;
1629 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1630 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1631 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1633 /* count handlers and apps, calculate size */
1634 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
1635 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1643 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
1644 * disconnect callbacks will be called on any still connected peers, notifying
1645 * about their disconnection. The registered inbound tunnel cleaner will be
1646 * called should any inbound tunnels still exist.
1648 * @param handle connection to mesh to disconnect
1651 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1653 struct GNUNET_MESH_Tunnel *t;
1654 struct GNUNET_MESH_Tunnel *aux;
1655 struct GNUNET_MESH_TransmitHandle *th;
1657 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1660 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1661 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1664 t = handle->tunnels_head;
1668 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1671 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1673 destroy_tunnel (t, GNUNET_YES);
1676 while ( (th = handle->th_head) != NULL)
1678 struct GNUNET_MessageHeader *msg;
1680 /* Make sure it is an allowed packet (everything else should have been
1681 * already canceled).
1683 GNUNET_break (GNUNET_NO == th_is_payload (th));
1684 msg = (struct GNUNET_MessageHeader *) &th[1];
1685 switch (ntohs(msg->type))
1687 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1688 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1689 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR:
1693 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1697 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1701 if (NULL != handle->th)
1703 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1706 if (NULL != handle->client)
1708 GNUNET_CLIENT_disconnect (handle->client);
1709 handle->client = NULL;
1711 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1713 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1714 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1716 GNUNET_free (handle);
1721 * Announce to ther peer the availability of services described by the regex,
1722 * in order to be reachable to other peers via connect_by_string.
1724 * Note that the first 8 characters are considered to be part of a prefix,
1725 * (for instance 'gnunet://'). If you put a variable part in there (*, +. ()),
1726 * all matching strings will be stored in the DHT.
1728 * @param h Handle to mesh.
1729 * @param regex String with the regular expression describing local services.
1730 * @param compression_characters How many characters can be assigned to one
1731 * edge of the graph. The bigger the variability
1732 * of the data, the smaller this parameter should
1734 * For maximum compression, use strlen (regex)
1735 * or 0 (special value). Use with care!
1738 GNUNET_MESH_announce_regex (struct GNUNET_MESH_Handle *h,
1740 unsigned int compression_characters)
1742 struct GNUNET_MESH_RegexAnnounce *msg;
1746 len = strlen (regex);
1747 msgsize = sizeof(struct GNUNET_MESH_RegexAnnounce) + len;
1748 GNUNET_assert (UINT16_MAX > msgsize);
1751 char buffer[msgsize];
1753 msg = (struct GNUNET_MESH_RegexAnnounce *) buffer;
1754 msg->header.size = htons (msgsize);
1755 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX);
1756 msg->compression_characters = htons (compression_characters);
1757 memcpy (&msg[1], regex, len);
1759 send_packet(h, &msg->header, NULL);
1764 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1765 * and to broadcast).
1767 * @param h mesh handle
1768 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1769 * @param connect_handler function to call when peers are actually connected
1770 * @param disconnect_handler function to call when peers are disconnected
1771 * @param handler_cls closure for connect/disconnect handlers
1773 struct GNUNET_MESH_Tunnel *
1774 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
1775 GNUNET_MESH_PeerConnectHandler connect_handler,
1776 GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
1779 struct GNUNET_MESH_Tunnel *t;
1780 struct GNUNET_MESH_TunnelMessage msg;
1782 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1783 t = create_tunnel (h, 0);
1784 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1785 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1786 t->connect_handler = connect_handler;
1787 t->disconnect_handler = disconnect_handler;
1788 t->cls = handler_cls;
1789 t->ctx = tunnel_ctx;
1790 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1791 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1792 msg.tunnel_id = htonl (t->tid);
1793 send_packet (h, &msg.header, t);
1799 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
1802 * @param tunnel tunnel handle
1805 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1807 struct GNUNET_MESH_Handle *h;
1808 struct GNUNET_MESH_TunnelMessage msg;
1809 struct GNUNET_MESH_TransmitHandle *th;
1811 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1814 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1815 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1816 msg.tunnel_id = htonl (tunnel->tid);
1820 struct GNUNET_MESH_TransmitHandle *aux;
1821 if (th->tunnel == tunnel)
1824 /* FIXME call the handler? */
1825 if (GNUNET_YES == th_is_payload (th))
1826 th->notify (th->notify_cls, 0, NULL);
1827 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1835 destroy_tunnel (tunnel, GNUNET_NO);
1836 send_packet (h, &msg.header, NULL);
1840 * Request that the tunnel data rate is limited to the speed of the slowest
1843 * @param tunnel Tunnel affected.
1846 GNUNET_MESH_tunnel_speed_min (struct GNUNET_MESH_Tunnel *tunnel)
1848 struct GNUNET_MESH_TunnelMessage msg;
1849 struct GNUNET_MESH_Handle *h;
1852 tunnel->speed_min = GNUNET_YES;
1854 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN);
1855 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1856 msg.tunnel_id = htonl (tunnel->tid);
1858 send_packet (h, &msg.header, NULL);
1863 * Request that the tunnel data rate is limited to the speed of the fastest
1864 * receiver. This is the default behavior.
1866 * @param tunnel Tunnel affected.
1869 GNUNET_MESH_tunnel_speed_max (struct GNUNET_MESH_Tunnel *tunnel)
1871 struct GNUNET_MESH_TunnelMessage msg;
1872 struct GNUNET_MESH_Handle *h;
1875 tunnel->speed_min = GNUNET_NO;
1877 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX);
1878 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1879 msg.tunnel_id = htonl (tunnel->tid);
1881 send_packet (h, &msg.header, NULL);
1885 * Turn on/off the buffering status of the tunnel.
1887 * @param tunnel Tunnel affected.
1888 * @param buffer GNUNET_YES to turn buffering on (default),
1889 * GNUNET_NO otherwise.
1892 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer)
1894 struct GNUNET_MESH_TunnelMessage msg;
1895 struct GNUNET_MESH_Handle *h;
1898 tunnel->buffering = buffer;
1899 tunnel->max_send_pid = tunnel->next_send_pid;
1901 if (GNUNET_YES == buffer)
1902 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER);
1904 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER);
1905 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1906 msg.tunnel_id = htonl (tunnel->tid);
1908 send_packet (h, &msg.header, NULL);
1912 * Request that a peer should be added to the tunnel. The existing
1913 * connect handler will be called ONCE with either success or failure.
1914 * This function should NOT be called again with the same peer before the
1915 * connect handler is called.
1917 * @param tunnel handle to existing tunnel
1918 * @param peer peer to add
1921 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
1922 const struct GNUNET_PeerIdentity *peer)
1924 struct GNUNET_MESH_PeerControl msg;
1925 GNUNET_PEER_Id peer_id;
1928 peer_id = GNUNET_PEER_intern (peer);
1929 for (i = 0; i < tunnel->npeers; i++)
1931 if (tunnel->peers[i]->id == peer_id)
1933 /* Peer already exists in tunnel */
1934 GNUNET_PEER_change_rc (peer_id, -1);
1939 if (NULL == add_peer_to_tunnel (tunnel, peer))
1942 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1943 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1944 msg.tunnel_id = htonl (tunnel->tid);
1946 send_packet (tunnel->mesh, &msg.header, tunnel);
1953 * Request that a peer should be removed from the tunnel. The existing
1954 * disconnect handler will be called ONCE if we were connected.
1956 * @param tunnel handle to existing tunnel
1957 * @param peer peer to remove
1960 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
1961 const struct GNUNET_PeerIdentity *peer)
1963 struct GNUNET_MESH_PeerControl msg;
1964 GNUNET_PEER_Id peer_id;
1967 peer_id = GNUNET_PEER_search (peer);
1973 for (i = 0; i < tunnel->npeers; i++)
1974 if (tunnel->peers[i]->id == peer_id)
1976 if (i == tunnel->npeers)
1981 if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
1982 tunnel->disconnect_handler (tunnel->cls, peer);
1983 GNUNET_PEER_change_rc (peer_id, -1);
1984 GNUNET_free (tunnel->peers[i]);
1985 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
1986 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
1988 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1989 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1990 msg.tunnel_id = htonl (tunnel->tid);
1991 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1992 send_packet (tunnel->mesh, &msg.header, tunnel);
1997 * Request that the mesh should try to connect to a peer supporting the given
2000 * @param tunnel handle to existing tunnel
2001 * @param app_type application type that must be supported by the peer (MESH
2002 * should discover peer in proximity handling this type)
2005 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
2006 GNUNET_MESH_ApplicationType app_type)
2008 struct GNUNET_MESH_ConnectPeerByType msg;
2010 GNUNET_array_append (tunnel->apps, tunnel->napps, app_type);
2012 LOG (GNUNET_ERROR_TYPE_DEBUG, "* CONNECT BY TYPE *\n");
2013 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
2014 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
2015 msg.tunnel_id = htonl (tunnel->tid);
2016 msg.type = htonl (app_type);
2017 send_packet (tunnel->mesh, &msg.header, tunnel);
2022 * Request that the mesh should try to connect to a peer matching the
2023 * description given in the service string.
2025 * FIXME: allow multiple? how to deal with reconnect?
2027 * @param tunnel handle to existing tunnel
2028 * @param description string describing the destination node requirements
2031 GNUNET_MESH_peer_request_connect_by_string (struct GNUNET_MESH_Tunnel *tunnel,
2032 const char *description)
2034 struct GNUNET_MESH_ConnectPeerByString *m;
2038 len = strlen (description);
2039 msgsize = sizeof(struct GNUNET_MESH_ConnectPeerByString) + len;
2040 GNUNET_assert (UINT16_MAX > msgsize);
2042 char buffer[msgsize];
2044 m = (struct GNUNET_MESH_ConnectPeerByString *) buffer;
2045 m->header.size = htons (msgsize);
2046 m->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING);
2047 m->tunnel_id = htonl (tunnel->tid);
2048 memcpy(&m[1], description, len);
2050 send_packet (tunnel->mesh, &m->header, tunnel);
2056 * Request that the given peer isn't added to this tunnel in calls to
2057 * connect_by_* calls, (due to misbehaviour, bad performance, ...).
2059 * @param tunnel handle to existing tunnel.
2060 * @param peer peer identity of the peer which should be blacklisted
2064 GNUNET_MESH_peer_blacklist (struct GNUNET_MESH_Tunnel *tunnel,
2065 const struct GNUNET_PeerIdentity *peer)
2067 struct GNUNET_MESH_PeerControl msg;
2069 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2070 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST);
2071 msg.tunnel_id = htonl (tunnel->tid);
2073 send_packet (tunnel->mesh, &msg.header, tunnel);
2080 * Request that the given peer isn't blacklisted anymore from this tunnel,
2081 * and therefore can be added in future calls to connect_by_*.
2082 * The peer must have been previously blacklisted for this tunnel.
2084 * @param tunnel handle to existing tunnel.
2085 * @param peer peer identity of the peer which shouldn't be blacklisted
2086 * for the tunnel anymore.
2089 GNUNET_MESH_peer_unblacklist (struct GNUNET_MESH_Tunnel *tunnel,
2090 const struct GNUNET_PeerIdentity *peer)
2092 struct GNUNET_MESH_PeerControl msg;
2094 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2095 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST);
2096 msg.tunnel_id = htonl (tunnel->tid);
2098 send_packet (tunnel->mesh, &msg.header, tunnel);
2105 * Ask the mesh to call "notify" once it is ready to transmit the
2106 * given number of bytes to the specified tunnel or target.
2107 * Only one call can be active at any time, to issue another request,
2108 * wait for the callback or cancel the current request.
2110 * @param tunnel tunnel to use for transmission
2111 * @param cork is corking allowed for this transmission?
2112 * @param maxdelay how long can the message wait?
2113 * @param target destination for the message
2114 * NULL for multicast to all tunnel targets
2115 * @param notify_size how many bytes of buffer space does notify want?
2116 * @param notify function to call when buffer space is available;
2117 * will be called with NULL on timeout or if the overall queue
2118 * for this peer is larger than queue_size and this is currently
2119 * the message with the lowest priority
2120 * @param notify_cls closure for notify
2121 * @return non-NULL if the notify callback was queued,
2122 * NULL if we can not even queue the request (insufficient
2123 * memory); if NULL is returned, "notify" will NOT be called.
2125 struct GNUNET_MESH_TransmitHandle *
2126 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
2127 struct GNUNET_TIME_Relative maxdelay,
2128 const struct GNUNET_PeerIdentity *target,
2130 GNUNET_CONNECTION_TransmitReadyNotify notify,
2133 struct GNUNET_MESH_TransmitHandle *th;
2136 GNUNET_assert (NULL != tunnel);
2137 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
2139 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2140 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
2141 else if (NULL != target)
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %s\n", GNUNET_i2s (target));
2144 LOG (GNUNET_ERROR_TYPE_DEBUG, " target multicast\n");
2145 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
2146 GNUNET_assert (NULL != notify);
2147 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
2148 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
2149 th->tunnel = tunnel;
2150 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
2151 th->target = GNUNET_PEER_intern (target);
2152 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2153 overhead = sizeof (struct GNUNET_MESH_ToOrigin);
2154 else if (NULL == target)
2155 overhead = sizeof (struct GNUNET_MESH_Multicast);
2157 overhead = sizeof (struct GNUNET_MESH_Unicast);
2158 tunnel->packet_size = th->size = notify_size + overhead;
2159 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
2160 th->notify = notify;
2161 th->notify_cls = notify_cls;
2162 add_to_queue (tunnel->mesh, th);
2163 if (NULL != tunnel->mesh->th)
2165 if (GMC_is_pid_bigger(tunnel->next_send_pid, tunnel->max_send_pid))
2167 LOG (GNUNET_ERROR_TYPE_DEBUG, " call notify tmt rdy\n");
2169 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
2170 GNUNET_TIME_UNIT_FOREVER_REL,
2171 GNUNET_YES, &send_callback,
2173 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
2179 * Cancel the specified transmission-ready notification.
2181 * @param th handle that was returned by "notify_transmit_ready".
2184 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
2186 struct GNUNET_MESH_Handle *mesh;
2188 th->tunnel->packet_size = 0;
2189 mesh = th->tunnel->mesh;
2190 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2191 GNUNET_SCHEDULER_cancel (th->timeout_task);
2192 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
2194 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
2196 /* queue empty, no point in asking for transmission */
2197 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
2204 * Request information about the running mesh peer.
2206 * @param h Handle to the mesh peer.
2207 * @param callback Function to call with the requested data.
2208 * @param monitor_cls Closure for @c callback.
2211 GNUNET_MESH_monitor (struct GNUNET_MESH_Handle *h,
2212 GNUNET_MESH_MonitorCB callback,
2215 struct GNUNET_MessageHeader msg;
2217 msg.size = htons (sizeof (msg));
2218 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR);
2219 send_packet (h, &msg, NULL);
2220 h->monitor_cb = callback;
2221 h->monitor_cls = monitor_cls;
2228 * Cancel a monitor request. The monitor callback will not be called.
2230 * @param h Mesh handle.
2232 * @return Closure given to GNUNET_MESH_monitor, if any.
2235 GNUNET_MESH_monitor_cancel (struct GNUNET_MESH_Handle *h)
2239 cls = h->monitor_cls;
2240 h->monitor_cb = NULL;
2241 h->monitor_cls = NULL;
2247 * Transition API for tunnel ctx management
2250 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
2256 * Transition API for tunnel ctx management
2259 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)