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;
211 unsigned int acks_sent;
212 unsigned int acks_recv;
218 * Description of a peer
220 struct GNUNET_MESH_Peer
223 * ID of the peer in short form
228 * Tunnel this peer belongs to
230 struct GNUNET_MESH_Tunnel *t;
233 * Flag indicating whether service has informed about its connection
241 * Opaque handle to a tunnel.
243 struct GNUNET_MESH_Tunnel
249 struct GNUNET_MESH_Tunnel *next;
254 struct GNUNET_MESH_Tunnel *prev;
257 * Callback to execute when peers connect to the tunnel
259 GNUNET_MESH_PeerConnectHandler connect_handler;
262 * Callback to execute when peers disconnect from the tunnel
264 GNUNET_MESH_PeerDisconnectHandler disconnect_handler;
267 * Closure for the connect/disconnect handlers
272 * Handle to the mesh this tunnel belongs to
274 struct GNUNET_MESH_Handle *mesh;
277 * Local ID of the tunnel
279 MESH_TunnelNumber tid;
282 * Owner of the tunnel. 0 if the tunnel is the local client.
284 GNUNET_PEER_Id owner;
287 * All peers added to the tunnel
289 struct GNUNET_MESH_Peer **peers;
292 * List of application types that have been requested for this tunnel
294 GNUNET_MESH_ApplicationType *apps;
297 * Any data the caller wants to put in here
302 * Number of peers added to the tunnel
307 * Size of packet queued in this tunnel
309 unsigned int packet_size;
312 * Number of applications requested this tunnel
317 * Is the tunnel throttled to the slowest peer?
322 * Is the tunnel allowed to buffer?
327 * Next packet ID to send.
329 uint32_t next_send_pid;
332 * Maximum allowed PID to send (ACK recevied).
334 uint32_t max_send_pid;
337 * Last pid received from the service.
339 uint32_t last_recv_pid;
342 * Which ACK value have we last sent to the service?
344 uint32_t max_recv_pid;
348 /******************************************************************************/
349 /*********************** DECLARATIONS *************************/
350 /******************************************************************************/
353 * Function called to send a message to the service.
354 * "buf" will be NULL and "size" zero if the socket was closed for writing in
357 * @param cls closure, the mesh handle
358 * @param size number of bytes available in buf
359 * @param buf where the callee should write the connect message
360 * @return number of bytes written to buf
363 send_callback (void *cls, size_t size, void *buf);
366 /******************************************************************************/
367 /*********************** AUXILIARY FUNCTIONS *************************/
368 /******************************************************************************/
371 * Check if transmission is a payload packet.
373 * @param th Transmission handle.
375 * @return GNUNET_YES if it is a payload packet,
376 * GNUNET_NO if it is a mesh management packet.
379 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
381 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
386 * Check whether there is any message ready in the queue and find the size.
388 * @param h Mesh handle.
390 * @return The size of the first ready message in the queue,
391 * 0 if there is none.
394 message_ready_size (struct GNUNET_MESH_Handle *h)
396 struct GNUNET_MESH_TransmitHandle *th;
397 struct GNUNET_MESH_Tunnel *t;
399 for (th = h->th_head; NULL != th; th = th->next)
402 if (GNUNET_NO == th_is_payload (th))
404 LOG (GNUNET_ERROR_TYPE_DEBUG, " message internal\n");
407 if (GNUNET_NO == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
409 LOG (GNUNET_ERROR_TYPE_DEBUG, " message payload ok (%u <= %u)\n",
410 t->next_send_pid, t->max_send_pid);
419 * Get the tunnel handler for the tunnel specified by id from the given handle
420 * @param h Mesh handle
421 * @param tid ID of the wanted tunnel
422 * @return handle to the required tunnel or NULL if not found
424 static struct GNUNET_MESH_Tunnel *
425 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
427 struct GNUNET_MESH_Tunnel *t;
441 * Create a new tunnel and insert it in the tunnel list of the mesh handle
442 * @param h Mesh handle
443 * @param tid desired tid of the tunnel, 0 to assign one automatically
444 * @return handle to the created tunnel
446 static struct GNUNET_MESH_Tunnel *
447 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
449 struct GNUNET_MESH_Tunnel *t;
451 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
452 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
456 t->tid = h->next_tid;
457 while (NULL != retrieve_tunnel (h, h->next_tid))
460 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
461 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
468 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
469 t->last_recv_pid = (uint32_t) -1;
475 * Destroy the specified tunnel.
476 * - Destroys all peers, calling the disconnect callback on each if needed
477 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
478 * - Calls cleaner if tunnel was inbound
479 * - Frees all memory used
481 * @param t Pointer to the tunnel.
482 * @param call_cleaner Whether to call the cleaner handler.
484 * @return Handle to the required tunnel or NULL if not found.
487 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
489 struct GNUNET_MESH_Handle *h;
490 struct GNUNET_PeerIdentity pi;
491 struct GNUNET_MESH_TransmitHandle *th;
492 struct GNUNET_MESH_TransmitHandle *next;
495 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
504 /* disconnect all peers */
505 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
506 for (i = 0; i < t->npeers; i++)
508 if ( (NULL != t->disconnect_handler) && t->peers[i]->connected)
510 GNUNET_PEER_resolve (t->peers[i]->id, &pi);
511 t->disconnect_handler (t->cls, &pi);
513 GNUNET_PEER_change_rc (t->peers[i]->id, -1);
514 GNUNET_free (t->peers[i]);
517 /* signal tunnel destruction */
518 if ( (NULL != h->cleaner) && (0 != t->owner) && (GNUNET_YES == call_cleaner) )
519 h->cleaner (h->cls, t, t->ctx);
521 /* check that clients did not leave messages behind in the queue */
522 for (th = h->th_head; NULL != th; th = next)
527 /* Clients should have aborted their requests already.
528 * Management traffic should be ok, as clients can't cancel that */
529 GNUNET_break (GNUNET_NO == th_is_payload(th));
530 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
532 /* clean up request */
533 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
534 GNUNET_SCHEDULER_cancel (th->timeout_task);
538 /* if there are no more pending requests with mesh service, cancel active request */
539 /* Note: this should be unnecessary... */
540 if ((0 == message_ready_size (h)) && (NULL != h->th))
542 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
548 GNUNET_free (t->peers);
550 GNUNET_PEER_change_rc (t->owner, -1);
551 if (0 != t->napps && t->apps)
552 GNUNET_free (t->apps);
559 * Get the peer descriptor for the peer with id from the given tunnel
560 * @param t Tunnel handle
561 * @param id Short form ID of the wanted peer
562 * @return handle to the requested peer or NULL if not found
564 static struct GNUNET_MESH_Peer *
565 retrieve_peer (struct GNUNET_MESH_Tunnel *t, GNUNET_PEER_Id id)
569 for (i = 0; i < t->npeers; i++)
570 if (t->peers[i]->id == id)
577 * Add a peer into a tunnel
578 * @param t Tunnel handle
579 * @param pi Full ID of the new peer
580 * @return handle to the newly created peer
582 static struct GNUNET_MESH_Peer *
583 add_peer_to_tunnel (struct GNUNET_MESH_Tunnel *t,
584 const struct GNUNET_PeerIdentity *pi)
586 struct GNUNET_MESH_Peer *p;
594 id = GNUNET_PEER_intern (pi);
596 p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
599 GNUNET_array_append (t->peers, t->npeers, p);
605 * Remove a peer from a tunnel
606 * @param p Peer handle
609 remove_peer_from_tunnel (struct GNUNET_MESH_Peer *p)
613 for (i = 0; i < p->t->npeers; i++)
615 if (p->t->peers[i] == p)
618 if (i == p->t->npeers)
623 p->t->peers[i] = p->t->peers[p->t->npeers - 1];
624 GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
629 * Notify client that the transmission has timed out
632 * @param tc task context
635 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
637 struct GNUNET_MESH_TransmitHandle *th = cls;
638 struct GNUNET_MESH_Handle *mesh;
640 mesh = th->tunnel->mesh;
641 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
642 th->tunnel->packet_size = 0;
643 if (GNUNET_YES == th_is_payload (th))
644 th->notify (th->notify_cls, 0, NULL);
646 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
648 /* nothing ready to transmit, no point in asking for transmission */
649 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
656 * Add a transmit handle to the transmission queue and set the
659 * @param h mesh handle with the queue head and tail
660 * @param th handle to the packet to be transmitted
663 add_to_queue (struct GNUNET_MESH_Handle *h,
664 struct GNUNET_MESH_TransmitHandle *th)
666 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
667 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
670 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
671 (th->timeout), &timeout_transmission, th);
676 * Auxiliary function to send an already constructed packet to the service.
677 * Takes care of creating a new queue element, copying the message and
678 * calling the tmt_rdy function if necessary.
680 * @param h mesh handle
681 * @param msg message to transmit
682 * @param tunnel tunnel this send is related to (NULL if N/A)
685 send_packet (struct GNUNET_MESH_Handle *h,
686 const struct GNUNET_MessageHeader *msg,
687 struct GNUNET_MESH_Tunnel *tunnel);
691 * Send an ack on the tunnel to confirm the processing of a message.
693 * @param h Mesh handle.
694 * @param t Tunnel on which to send the ACK.
697 send_ack (struct GNUNET_MESH_Handle *h, struct GNUNET_MESH_Tunnel *t)
699 struct GNUNET_MESH_LocalAck msg;
702 delta = t->max_recv_pid - t->last_recv_pid;
703 if (delta > ACK_THRESHOLD)
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "Not sending ACK on tunnel %X: ACK: %u, PID: %u, buffer %u\n",
707 t->tid, t->max_recv_pid, t->last_recv_pid, delta);
710 t->max_recv_pid = t->last_recv_pid + INITIAL_WINDOW_SIZE;
711 LOG (GNUNET_ERROR_TYPE_DEBUG,
712 "Sending ACK on tunnel %X: %u\n",
713 t->tid, t->max_recv_pid);
714 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
715 msg.header.size = htons (sizeof (msg));
716 msg.tunnel_id = htonl (t->tid);
717 msg.max_pid = htonl (t->max_recv_pid);
720 t->mesh->acks_sent++;
723 send_packet (h, &msg.header, t);
730 * Reconnect callback: tries to reconnect again after a failer previous
732 * @param cls closure (mesh handle)
733 * @param tc task context
736 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
740 * Send a connect packet to the service with the applications and types
741 * requested by the user.
743 * @param h The mesh handle.
747 send_connect (struct GNUNET_MESH_Handle *h)
751 size = sizeof (struct GNUNET_MESH_ClientConnect);
752 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
753 size += h->n_handlers * sizeof (uint16_t);
755 char buf[size] GNUNET_ALIGN;
756 struct GNUNET_MESH_ClientConnect *msg;
757 GNUNET_MESH_ApplicationType *apps;
762 /* build connection packet */
763 msg = (struct GNUNET_MESH_ClientConnect *) buf;
764 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
765 msg->header.size = htons (size);
766 apps = (GNUNET_MESH_ApplicationType *) &msg[1];
767 for (napps = 0; napps < h->n_applications; napps++)
769 apps[napps] = htonl (h->applications[napps]);
770 LOG (GNUNET_ERROR_TYPE_DEBUG, " app %u\n",
771 h->applications[napps]);
773 types = (uint16_t *) & apps[napps];
774 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
776 types[ntypes] = htons (h->message_handlers[ntypes].type);
777 LOG (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
778 h->message_handlers[ntypes].type);
780 msg->applications = htons (napps);
781 msg->types = htons (ntypes);
782 LOG (GNUNET_ERROR_TYPE_DEBUG,
783 "Sending %lu bytes long message %d types and %d apps\n",
784 ntohs (msg->header.size), ntypes, napps);
785 send_packet (h, &msg->header, NULL);
791 * Reconnect to the service, retransmit all infomation to try to restore the
794 * @param h handle to the mesh
796 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
799 do_reconnect (struct GNUNET_MESH_Handle *h)
801 struct GNUNET_MESH_Tunnel *t;
804 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
805 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
806 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
807 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
808 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
813 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
816 if (NULL != h->client)
818 GNUNET_CLIENT_disconnect (h->client);
822 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
823 if (h->client == NULL)
825 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
828 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
829 GNUNET_TIME_relative_multiply
830 (h->reconnect_time, 2));
831 LOG (GNUNET_ERROR_TYPE_DEBUG, " Next retry in %sms\n",
832 GNUNET_TIME_relative_to_string (h->reconnect_time));
838 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
841 /* Rebuild all tunnels */
842 for (t = h->tunnels_head; NULL != t; t = t->next)
844 struct GNUNET_MESH_TunnelMessage tmsg;
845 struct GNUNET_MESH_PeerControl pmsg;
847 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
849 /* Tunnel was created by service (incoming tunnel) */
850 /* TODO: Notify service of missing tunnel, to request
851 * creator to recreate path (find a path to him via DHT?)
855 t->next_send_pid = 0;
856 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
857 t->last_recv_pid = (uint32_t) -1;
858 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
859 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
860 tmsg.tunnel_id = htonl (t->tid);
861 send_packet (h, &tmsg.header, t);
863 pmsg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
864 pmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
865 pmsg.tunnel_id = htonl (t->tid);
867 /* Reconnect all peers */
868 /* If the tunnel was "by type", dont connect individual peers */
869 for (i = 0; i < t->npeers && 0 == t->napps; i++)
871 GNUNET_PEER_resolve (t->peers[i]->id, &pmsg.peer);
872 if (NULL != t->disconnect_handler && t->peers[i]->connected)
873 t->disconnect_handler (t->cls, &pmsg.peer);
874 send_packet (t->mesh, &pmsg.header, t);
876 /* Reconnect all types, if any */
877 for (i = 0; i < t->napps; i++)
879 struct GNUNET_MESH_ConnectPeerByType msg;
881 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
882 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
883 msg.tunnel_id = htonl (t->tid);
884 msg.type = htonl (t->apps[i]);
885 send_packet (t->mesh, &msg.header, t);
887 if (GNUNET_NO == t->buffering)
888 GNUNET_MESH_tunnel_buffer (t, GNUNET_NO);
889 if (GNUNET_YES == t->speed_min)
890 GNUNET_MESH_tunnel_speed_min (t);
896 * Reconnect callback: tries to reconnect again after a failer previous
898 * @param cls closure (mesh handle)
899 * @param tc task context
902 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
904 struct GNUNET_MESH_Handle *h = cls;
906 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
907 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
914 * Reconnect to the service, retransmit all infomation to try to restore the
917 * @param h handle to the mesh
919 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
922 reconnect (struct GNUNET_MESH_Handle *h)
924 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
925 h->in_receive = GNUNET_NO;
926 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
927 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
932 /******************************************************************************/
933 /*********************** RECEIVE HANDLERS ****************************/
934 /******************************************************************************/
937 * Process the new tunnel notification and add it to the tunnels in the handle
939 * @param h The mesh handle
940 * @param msg A message with the details of the new incoming tunnel
943 process_tunnel_created (struct GNUNET_MESH_Handle *h,
944 const struct GNUNET_MESH_TunnelNotification *msg)
946 struct GNUNET_MESH_Tunnel *t;
947 MESH_TunnelNumber tid;
949 tid = ntohl (msg->tunnel_id);
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
951 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
956 if (NULL != h->new_tunnel)
958 struct GNUNET_ATS_Information atsi;
960 t = create_tunnel (h, tid);
961 t->owner = GNUNET_PEER_intern (&msg->peer);
963 t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
964 t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
966 t->peers[0]->connected = 1;
967 t->peers[0]->id = t->owner;
968 GNUNET_PEER_change_rc (t->owner, 1);
973 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
974 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, &atsi);
975 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
979 struct GNUNET_MESH_TunnelMessage d_msg;
981 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
983 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
984 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
985 d_msg.tunnel_id = msg->tunnel_id;
987 send_packet (h, &d_msg.header, NULL);
994 * Process the tunnel destroy notification and free associated resources
996 * @param h The mesh handle
997 * @param msg A message with the details of the tunnel being destroyed
1000 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
1001 const struct GNUNET_MESH_TunnelMessage *msg)
1003 struct GNUNET_MESH_Tunnel *t;
1004 MESH_TunnelNumber tid;
1006 tid = ntohl (msg->tunnel_id);
1007 t = retrieve_tunnel (h, tid);
1017 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
1018 destroy_tunnel (t, GNUNET_YES);
1024 * Process the new peer event and notify the upper level of it
1026 * @param h The mesh handle
1027 * @param msg A message with the details of the peer event
1030 process_peer_event (struct GNUNET_MESH_Handle *h,
1031 const struct GNUNET_MESH_PeerControl *msg)
1033 struct GNUNET_MESH_Tunnel *t;
1034 struct GNUNET_MESH_Peer *p;
1035 struct GNUNET_ATS_Information atsi;
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, "processig peer event\n");
1040 size = ntohs (msg->header.size);
1041 if (size != sizeof (struct GNUNET_MESH_PeerControl))
1046 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
1052 id = GNUNET_PEER_search (&msg->peer);
1053 if ((p = retrieve_peer (t, id)) == NULL)
1054 p = add_peer_to_tunnel (t, &msg->peer);
1055 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == ntohs (msg->header.type))
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding peer\n");
1058 if (NULL != t->connect_handler)
1062 t->connect_handler (t->cls, &msg->peer, &atsi);
1068 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing peer\n");
1069 if (NULL != t->disconnect_handler && p->connected)
1071 t->disconnect_handler (t->cls, &msg->peer);
1073 remove_peer_from_tunnel (p);
1076 LOG (GNUNET_ERROR_TYPE_DEBUG, "processing peer event END\n");
1081 * Process the incoming data packets
1083 * @param h The mesh handle
1084 * @param message A message encapsulating the data
1086 * @return GNUNET_YES if everything went fine
1087 * GNUNET_NO if client closed connection (h no longer valid)
1090 process_incoming_data (struct GNUNET_MESH_Handle *h,
1091 const struct GNUNET_MessageHeader *message)
1093 const struct GNUNET_MessageHeader *payload;
1094 const struct GNUNET_MESH_MessageHandler *handler;
1095 const struct GNUNET_PeerIdentity *peer;
1096 struct GNUNET_MESH_Unicast *ucast;
1097 struct GNUNET_MESH_Multicast *mcast;
1098 struct GNUNET_MESH_ToOrigin *to_orig;
1099 struct GNUNET_MESH_Tunnel *t;
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
1105 type = ntohs (message->type);
1108 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1109 ucast = (struct GNUNET_MESH_Unicast *) message;
1111 t = retrieve_tunnel (h, ntohl (ucast->tid));
1112 payload = (struct GNUNET_MessageHeader *) &ucast[1];
1114 pid = ntohl (ucast->pid);
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%X]\n",
1116 GNUNET_i2s (peer), ntohl (ucast->tid));
1118 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1119 mcast = (struct GNUNET_MESH_Multicast *) message;
1120 t = retrieve_tunnel (h, ntohl (mcast->tid));
1121 payload = (struct GNUNET_MessageHeader *) &mcast[1];
1123 pid = ntohl (mcast->pid);
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, " mcast on tunnel %s [%X]\n",
1125 GNUNET_i2s (peer), ntohl (mcast->tid));
1127 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1128 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
1129 t = retrieve_tunnel (h, ntohl (to_orig->tid));
1130 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
1131 peer = &to_orig->sender;
1132 pid = ntohl (to_orig->pid);
1133 LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%X]\n",
1134 GNUNET_i2s (peer), ntohl (to_orig->tid));
1140 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", pid);
1143 /* Tunnel was ignored, probably service didn't get it yet */
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
1148 GMC_is_pid_bigger(pid, t->max_recv_pid))
1151 LOG (GNUNET_ERROR_TYPE_WARNING, " unauthorized message!\n");
1152 // FIXME fc what now? accept? reject?
1155 t->last_recv_pid = pid;
1156 type = ntohs (payload->type);
1158 for (i = 0; i < h->n_handlers; i++)
1160 handler = &h->message_handlers[i];
1161 if (handler->type == type)
1163 struct GNUNET_ATS_Information atsi;
1168 handler->callback (h->cls, t, &t->ctx, peer, payload, &atsi))
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
1171 GNUNET_MESH_disconnect (h);
1176 LOG (GNUNET_ERROR_TYPE_DEBUG,
1177 "callback completed successfully\n");
1186 * Process a local ACK message, enabling the client to send
1187 * more data to the service.
1189 * @param h Mesh handle.
1190 * @param message Message itself.
1193 process_ack (struct GNUNET_MESH_Handle *h,
1194 const struct GNUNET_MessageHeader *message)
1196 struct GNUNET_MESH_LocalAck *msg;
1197 struct GNUNET_MESH_Tunnel *t;
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
1202 msg = (struct GNUNET_MESH_LocalAck *) message;
1204 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
1208 LOG (GNUNET_ERROR_TYPE_WARNING,
1209 "ACK on unknown tunnel %X\n",
1210 ntohl (msg->tunnel_id));
1213 ack = ntohl (msg->max_pid);
1214 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X, ack %u!\n", t->tid, ack);
1215 if (GNUNET_YES == GMC_is_pid_bigger(ack, t->max_send_pid))
1216 t->max_send_pid = ack;
1219 if (NULL == h->th && 0 < t->packet_size)
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n", t->tid, ack);
1223 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
1224 GNUNET_TIME_UNIT_FOREVER_REL,
1225 GNUNET_YES, &send_callback, h);
1231 * Function to process all messages received from the service
1233 * @param cls closure
1234 * @param msg message received, NULL on timeout or fatal error
1237 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1239 struct GNUNET_MESH_Handle *h = cls;
1243 LOG (GNUNET_ERROR_TYPE_WARNING, "Received NULL msg on %p\n", h);
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1248 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1249 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1250 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1251 switch (ntohs (msg->type))
1253 /* Notify of a new incoming tunnel */
1254 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1255 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1257 /* Notify of a tunnel disconnection */
1258 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1259 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1261 /* Notify of a new peer or a peer disconnect in the tunnel */
1262 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD:
1263 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL:
1264 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
1266 /* Notify of a new data packet in the tunnel */
1267 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1268 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1269 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1270 if (GNUNET_NO == process_incoming_data (h, msg))
1273 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1274 process_ack (h, msg);
1277 /* We shouldn't get any other packages, log and ignore */
1278 LOG (GNUNET_ERROR_TYPE_WARNING,
1279 "unsolicited message form service (type %hu)\n",
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1283 if (GNUNET_YES == h->in_receive)
1285 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1286 GNUNET_TIME_UNIT_FOREVER_REL);
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "in receive off, not calling CLIENT_receive\n");
1296 /******************************************************************************/
1297 /************************ SEND FUNCTIONS ****************************/
1298 /******************************************************************************/
1301 * Function called to send a message to the service.
1302 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1305 * @param cls closure, the mesh handle
1306 * @param size number of bytes available in buf
1307 * @param buf where the callee should write the connect message
1308 * @return number of bytes written to buf
1311 send_callback (void *cls, size_t size, void *buf)
1313 struct GNUNET_MESH_Handle *h = cls;
1314 struct GNUNET_MESH_TransmitHandle *th;
1315 struct GNUNET_MESH_TransmitHandle *next;
1316 struct GNUNET_MESH_Tunnel *t;
1322 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
1324 if ((0 == size) || (NULL == buf))
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL send callback on %p\n", h);
1333 nsize = message_ready_size (h);
1334 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1337 if (GNUNET_YES == th_is_payload (th))
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload\n");
1340 if (GNUNET_YES == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
1342 /* This tunnel is not ready to transmit yet, try next message */
1347 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1349 /* traffic to origin */
1350 struct GNUNET_MESH_ToOrigin to;
1351 struct GNUNET_MessageHeader *mh;
1353 GNUNET_assert (size >= th->size);
1354 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
1355 psize = th->notify (th->notify_cls, size - sizeof (to), mh);
1356 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin, type %s\n",
1357 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1360 psize += sizeof (to);
1361 GNUNET_assert (size >= psize);
1362 to.header.size = htons (psize);
1363 to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1364 to.tid = htonl (t->tid);
1365 to.pid = htonl (t->next_send_pid);
1367 memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1368 memset (&to.sender, 0, sizeof (struct GNUNET_PeerIdentity));
1369 memcpy (cbuf, &to, sizeof (to));
1372 else if (th->target == 0)
1375 struct GNUNET_MESH_Multicast mc;
1376 struct GNUNET_MessageHeader *mh;
1378 GNUNET_assert (size >= th->size);
1379 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (mc)];
1380 psize = th->notify (th->notify_cls, size - sizeof (mc), mh);
1381 LOG (GNUNET_ERROR_TYPE_DEBUG, " multicast, type %s\n",
1382 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1385 psize += sizeof (mc);
1386 GNUNET_assert (size >= psize);
1387 mc.header.size = htons (psize);
1388 mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1389 mc.tid = htonl (t->tid);
1390 mc.pid = htonl (t->next_send_pid);
1392 memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1393 memcpy (cbuf, &mc, sizeof (mc));
1399 struct GNUNET_MESH_Unicast uc;
1400 struct GNUNET_MessageHeader *mh;
1402 GNUNET_assert (size >= th->size);
1403 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
1404 psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, " unicast, type %s\n",
1406 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1409 psize += sizeof (uc);
1410 GNUNET_assert (size >= psize);
1411 uc.header.size = htons (psize);
1412 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1413 uc.tid = htonl (t->tid);
1414 uc.pid = htonl (t->next_send_pid);
1416 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1417 GNUNET_PEER_resolve (th->target, &uc.destination);
1418 memcpy (cbuf, &uc, sizeof (uc));
1425 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, " mesh traffic, type %s\n",
1428 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1429 memcpy (cbuf, &th[1], th->size);
1432 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1433 GNUNET_SCHEDULER_cancel (th->timeout_task);
1434 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1437 nsize = message_ready_size (h);
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size: %u\n", tsize);
1444 size = message_ready_size (h);
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, " next size: %u\n", size);
1449 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1450 GNUNET_TIME_UNIT_FOREVER_REL,
1451 GNUNET_YES, &send_callback, h);
1455 if (NULL != h->th_head)
1456 LOG (GNUNET_ERROR_TYPE_DEBUG, " can't transmit any more\n");
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, " nothing left to transmit\n");
1460 if (GNUNET_NO == h->in_receive)
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, " start receiving from service\n");
1463 h->in_receive = GNUNET_YES;
1464 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1465 GNUNET_TIME_UNIT_FOREVER_REL);
1467 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
1473 * Auxiliary function to send an already constructed packet to the service.
1474 * Takes care of creating a new queue element, copying the message and
1475 * calling the tmt_rdy function if necessary.
1477 * @param h mesh handle
1478 * @param msg message to transmit
1479 * @param tunnel tunnel this send is related to (NULL if N/A)
1482 send_packet (struct GNUNET_MESH_Handle *h,
1483 const struct GNUNET_MessageHeader *msg,
1484 struct GNUNET_MESH_Tunnel *tunnel)
1486 struct GNUNET_MESH_TransmitHandle *th;
1489 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1490 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1491 msize = ntohs (msg->size);
1492 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1493 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1495 th->tunnel = tunnel;
1496 memcpy (&th[1], msg, msize);
1497 add_to_queue (h, th);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1501 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1503 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1504 GNUNET_TIME_UNIT_FOREVER_REL,
1505 GNUNET_YES, &send_callback, h);
1509 /******************************************************************************/
1510 /********************** API CALL DEFINITIONS *************************/
1511 /******************************************************************************/
1514 * Connect to the mesh service.
1516 * @param cfg configuration to use
1517 * @param cls closure for the various callbacks that follow
1518 * (including handlers in the handlers array)
1519 * @param new_tunnel function called when an *inbound* tunnel is created
1520 * @param cleaner function called when an *inbound* tunnel is destroyed by the
1521 * remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
1522 * is called on the tunnel
1523 * @param handlers callbacks for messages we care about, NULL-terminated
1524 * note that the mesh is allowed to drop notifications about
1525 * inbound messages if the client does not process them fast
1526 * enough (for this notification type, a bounded queue is used)
1527 * @param stypes list of the applications that this client claims to provide
1528 * @return handle to the mesh service NULL on error
1529 * (in this case, init is never called)
1531 struct GNUNET_MESH_Handle *
1532 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1533 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1534 GNUNET_MESH_TunnelEndHandler cleaner,
1535 const struct GNUNET_MESH_MessageHandler *handlers,
1536 const GNUNET_MESH_ApplicationType *stypes)
1538 struct GNUNET_MESH_Handle *h;
1540 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1541 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1542 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1544 h->new_tunnel = new_tunnel;
1545 h->cleaner = cleaner;
1546 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1547 if (h->client == NULL)
1555 h->applications = stypes;
1556 h->message_handlers = handlers;
1557 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1558 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1559 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1561 /* count handlers and apps, calculate size */
1562 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
1563 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1571 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
1572 * disconnect callbacks will be called on any still connected peers, notifying
1573 * about their disconnection. The registered inbound tunnel cleaner will be
1574 * called should any inbound tunnels still exist.
1576 * @param handle connection to mesh to disconnect
1579 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1581 struct GNUNET_MESH_Tunnel *t;
1582 struct GNUNET_MESH_Tunnel *aux;
1583 struct GNUNET_MESH_TransmitHandle *th;
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1588 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1589 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1592 t = handle->tunnels_head;
1596 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1601 destroy_tunnel (t, GNUNET_YES);
1604 while ( (th = handle->th_head) != NULL)
1606 struct GNUNET_MessageHeader *msg;
1608 /* Make sure it is an allowed packet (everything else should have been
1609 * already canceled).
1611 GNUNET_break (GNUNET_NO == th_is_payload (th));
1612 msg = (struct GNUNET_MessageHeader *) &th[1];
1613 switch (ntohs(msg->type))
1615 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1616 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1620 LOG (GNUNET_ERROR_TYPE_DEBUG, "unexpected msg %u\n",
1624 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1628 if (NULL != handle->th)
1630 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1633 if (NULL != handle->client)
1635 GNUNET_CLIENT_disconnect (handle->client);
1636 handle->client = NULL;
1638 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1640 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1641 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1643 GNUNET_free (handle);
1648 * Announce to ther peer the availability of services described by the regex,
1649 * in order to be reachable to other peers via connect_by_string.
1651 * Note that the first 8 characters are considered to be part of a prefix,
1652 * (for instance 'gnunet://'). If you put a variable part in there (*, +. ()),
1653 * all matching strings will be stored in the DHT.
1655 * @param h handle to mesh.
1656 * @param regex string with the regular expression describing local services.
1659 GNUNET_MESH_announce_regex (struct GNUNET_MESH_Handle *h,
1662 struct GNUNET_MessageHeader *msg;
1666 len = strlen (regex);
1667 msgsize = sizeof(struct GNUNET_MessageHeader) + len;
1668 GNUNET_assert (UINT16_MAX > msgsize);
1671 char buffer[msgsize];
1673 msg = (struct GNUNET_MessageHeader *) buffer;
1674 msg->size = htons (msgsize);
1675 msg->type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX);
1676 memcpy (&msg[1], regex, len);
1678 send_packet(h, msg, NULL);
1683 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1684 * and to broadcast).
1686 * @param h mesh handle
1687 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1688 * @param connect_handler function to call when peers are actually connected
1689 * @param disconnect_handler function to call when peers are disconnected
1690 * @param handler_cls closure for connect/disconnect handlers
1692 struct GNUNET_MESH_Tunnel *
1693 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
1694 GNUNET_MESH_PeerConnectHandler connect_handler,
1695 GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
1698 struct GNUNET_MESH_Tunnel *t;
1699 struct GNUNET_MESH_TunnelMessage msg;
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1702 t = create_tunnel (h, 0);
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1705 t->connect_handler = connect_handler;
1706 t->disconnect_handler = disconnect_handler;
1707 t->cls = handler_cls;
1708 t->ctx = tunnel_ctx;
1709 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1710 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1711 msg.tunnel_id = htonl (t->tid);
1712 send_packet (h, &msg.header, t);
1718 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
1721 * @param tunnel tunnel handle
1724 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1726 struct GNUNET_MESH_Handle *h;
1727 struct GNUNET_MESH_TunnelMessage msg;
1728 struct GNUNET_MESH_TransmitHandle *th;
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1733 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1734 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1735 msg.tunnel_id = htonl (tunnel->tid);
1739 struct GNUNET_MESH_TransmitHandle *aux;
1740 if (th->tunnel == tunnel)
1743 /* FIXME call the handler? */
1744 if (GNUNET_YES == th_is_payload (th))
1745 th->notify (th->notify_cls, 0, NULL);
1746 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1754 destroy_tunnel (tunnel, GNUNET_NO);
1755 send_packet (h, &msg.header, NULL);
1759 * Request that the tunnel data rate is limited to the speed of the slowest
1762 * @param tunnel Tunnel affected.
1765 GNUNET_MESH_tunnel_speed_min (struct GNUNET_MESH_Tunnel *tunnel)
1767 struct GNUNET_MESH_TunnelMessage msg;
1768 struct GNUNET_MESH_Handle *h;
1771 tunnel->speed_min = GNUNET_YES;
1773 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN);
1774 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1775 msg.tunnel_id = htonl (tunnel->tid);
1777 send_packet (h, &msg.header, NULL);
1782 * Request that the tunnel data rate is limited to the speed of the fastest
1783 * receiver. This is the default behavior.
1785 * @param tunnel Tunnel affected.
1788 GNUNET_MESH_tunnel_speed_max (struct GNUNET_MESH_Tunnel *tunnel)
1790 struct GNUNET_MESH_TunnelMessage msg;
1791 struct GNUNET_MESH_Handle *h;
1794 tunnel->speed_min = GNUNET_NO;
1796 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX);
1797 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1798 msg.tunnel_id = htonl (tunnel->tid);
1800 send_packet (h, &msg.header, NULL);
1804 * Turn on/off the buffering status of the tunnel.
1806 * @param tunnel Tunnel affected.
1807 * @param buffer GNUNET_YES to turn buffering on (default),
1808 * GNUNET_NO otherwise.
1811 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer)
1813 struct GNUNET_MESH_TunnelMessage msg;
1814 struct GNUNET_MESH_Handle *h;
1817 tunnel->buffering = buffer;
1819 if (GNUNET_YES == buffer)
1820 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER);
1822 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER);
1823 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1824 msg.tunnel_id = htonl (tunnel->tid);
1826 send_packet (h, &msg.header, NULL);
1830 * Request that a peer should be added to the tunnel. The existing
1831 * connect handler will be called ONCE with either success or failure.
1832 * This function should NOT be called again with the same peer before the
1833 * connect handler is called.
1835 * @param tunnel handle to existing tunnel
1836 * @param peer peer to add
1839 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
1840 const struct GNUNET_PeerIdentity *peer)
1842 struct GNUNET_MESH_PeerControl msg;
1843 GNUNET_PEER_Id peer_id;
1846 peer_id = GNUNET_PEER_intern (peer);
1847 for (i = 0; i < tunnel->npeers; i++)
1849 if (tunnel->peers[i]->id == peer_id)
1851 /* Peer already exists in tunnel */
1852 GNUNET_PEER_change_rc (peer_id, -1);
1857 if (NULL == add_peer_to_tunnel (tunnel, peer))
1860 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1861 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1862 msg.tunnel_id = htonl (tunnel->tid);
1864 send_packet (tunnel->mesh, &msg.header, tunnel);
1871 * Request that a peer should be removed from the tunnel. The existing
1872 * disconnect handler will be called ONCE if we were connected.
1874 * @param tunnel handle to existing tunnel
1875 * @param peer peer to remove
1878 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
1879 const struct GNUNET_PeerIdentity *peer)
1881 struct GNUNET_MESH_PeerControl msg;
1882 GNUNET_PEER_Id peer_id;
1885 peer_id = GNUNET_PEER_search (peer);
1891 for (i = 0; i < tunnel->npeers; i++)
1892 if (tunnel->peers[i]->id == peer_id)
1894 if (i == tunnel->npeers)
1899 if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
1900 tunnel->disconnect_handler (tunnel->cls, peer);
1901 GNUNET_PEER_change_rc (peer_id, -1);
1902 GNUNET_free (tunnel->peers[i]);
1903 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
1904 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
1906 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1907 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1908 msg.tunnel_id = htonl (tunnel->tid);
1909 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1910 send_packet (tunnel->mesh, &msg.header, tunnel);
1915 * Request that the mesh should try to connect to a peer supporting the given
1918 * @param tunnel handle to existing tunnel
1919 * @param app_type application type that must be supported by the peer (MESH
1920 * should discover peer in proximity handling this type)
1923 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
1924 GNUNET_MESH_ApplicationType app_type)
1926 struct GNUNET_MESH_ConnectPeerByType msg;
1928 GNUNET_array_append (tunnel->apps, tunnel->napps, app_type);
1930 LOG (GNUNET_ERROR_TYPE_DEBUG, "* CONNECT BY TYPE *\n");
1931 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
1932 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
1933 msg.tunnel_id = htonl (tunnel->tid);
1934 msg.type = htonl (app_type);
1935 send_packet (tunnel->mesh, &msg.header, tunnel);
1940 * Request that the mesh should try to connect to a peer matching the
1941 * description given in the service string.
1943 * FIXME: allow multiple? how to deal with reconnect?
1945 * @param tunnel handle to existing tunnel
1946 * @param description string describing the destination node requirements
1949 GNUNET_MESH_peer_request_connect_by_string (struct GNUNET_MESH_Tunnel *tunnel,
1950 const char *description)
1952 struct GNUNET_MESH_ConnectPeerByString *m;
1956 len = strlen (description);
1957 msgsize = sizeof(struct GNUNET_MESH_ConnectPeerByString) + len;
1958 GNUNET_assert (UINT16_MAX > msgsize);
1960 char buffer[msgsize];
1962 m = (struct GNUNET_MESH_ConnectPeerByString *) buffer;
1963 m->header.size = htons (msgsize);
1964 m->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING);
1965 m->tunnel_id = htonl (tunnel->tid);
1966 memcpy(&m[1], description, len);
1968 send_packet (tunnel->mesh, &m->header, tunnel);
1974 * Request that the given peer isn't added to this tunnel in calls to
1975 * connect_by_* calls, (due to misbehaviour, bad performance, ...).
1977 * @param tunnel handle to existing tunnel.
1978 * @param peer peer identity of the peer which should be blacklisted
1982 GNUNET_MESH_peer_blacklist (struct GNUNET_MESH_Tunnel *tunnel,
1983 const struct GNUNET_PeerIdentity *peer)
1985 struct GNUNET_MESH_PeerControl msg;
1987 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1988 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST);
1989 msg.tunnel_id = htonl (tunnel->tid);
1991 send_packet (tunnel->mesh, &msg.header, tunnel);
1998 * Request that the given peer isn't blacklisted anymore from this tunnel,
1999 * and therefore can be added in future calls to connect_by_*.
2000 * The peer must have been previously blacklisted for this tunnel.
2002 * @param tunnel handle to existing tunnel.
2003 * @param peer peer identity of the peer which shouldn't be blacklisted
2004 * for the tunnel anymore.
2007 GNUNET_MESH_peer_unblacklist (struct GNUNET_MESH_Tunnel *tunnel,
2008 const struct GNUNET_PeerIdentity *peer)
2010 struct GNUNET_MESH_PeerControl msg;
2012 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2013 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST);
2014 msg.tunnel_id = htonl (tunnel->tid);
2016 send_packet (tunnel->mesh, &msg.header, tunnel);
2023 * Ask the mesh to call "notify" once it is ready to transmit the
2024 * given number of bytes to the specified tunnel or target.
2025 * Only one call can be active at any time, to issue another request,
2026 * wait for the callback or cancel the current request.
2028 * @param tunnel tunnel to use for transmission
2029 * @param cork is corking allowed for this transmission?
2030 * @param maxdelay how long can the message wait?
2031 * @param target destination for the message
2032 * NULL for multicast to all tunnel targets
2033 * @param notify_size how many bytes of buffer space does notify want?
2034 * @param notify function to call when buffer space is available;
2035 * will be called with NULL on timeout or if the overall queue
2036 * for this peer is larger than queue_size and this is currently
2037 * the message with the lowest priority
2038 * @param notify_cls closure for notify
2039 * @return non-NULL if the notify callback was queued,
2040 * NULL if we can not even queue the request (insufficient
2041 * memory); if NULL is returned, "notify" will NOT be called.
2043 struct GNUNET_MESH_TransmitHandle *
2044 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
2045 struct GNUNET_TIME_Relative maxdelay,
2046 const struct GNUNET_PeerIdentity *target,
2048 GNUNET_CONNECTION_TransmitReadyNotify notify,
2051 struct GNUNET_MESH_TransmitHandle *th;
2054 GNUNET_assert (NULL != tunnel);
2055 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
2056 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
2057 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2058 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
2059 else if (NULL != target)
2060 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %s\n", GNUNET_i2s (target));
2062 LOG (GNUNET_ERROR_TYPE_DEBUG, " target multicast\n");
2063 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
2064 GNUNET_assert (NULL != notify);
2065 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
2066 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
2067 th->tunnel = tunnel;
2068 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
2069 th->target = GNUNET_PEER_intern (target);
2070 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2071 overhead = sizeof (struct GNUNET_MESH_ToOrigin);
2072 else if (NULL == target)
2073 overhead = sizeof (struct GNUNET_MESH_Multicast);
2075 overhead = sizeof (struct GNUNET_MESH_Unicast);
2076 tunnel->packet_size = th->size = notify_size + overhead;
2077 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
2078 th->notify = notify;
2079 th->notify_cls = notify_cls;
2080 add_to_queue (tunnel->mesh, th);
2081 if (NULL != tunnel->mesh->th)
2083 if (GMC_is_pid_bigger(tunnel->next_send_pid, tunnel->max_send_pid))
2085 LOG (GNUNET_ERROR_TYPE_DEBUG, " call notify tmt rdy\n");
2087 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
2088 GNUNET_TIME_UNIT_FOREVER_REL,
2089 GNUNET_YES, &send_callback,
2091 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
2097 * Cancel the specified transmission-ready notification.
2099 * @param th handle that was returned by "notify_transmit_ready".
2102 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
2104 struct GNUNET_MESH_Handle *mesh;
2106 th->tunnel->packet_size = 0;
2107 mesh = th->tunnel->mesh;
2108 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2109 GNUNET_SCHEDULER_cancel (th->timeout_task);
2110 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
2112 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
2114 /* queue empty, no point in asking for transmission */
2115 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
2122 * Transition API for tunnel ctx management
2125 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
2131 * Transition API for tunnel ctx management
2134 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)